summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/line/inline_box.cc
blob: 70f6d1c644e4145839268626b0ad1e9924ed6654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/core/layout/line/inline_box.h"

#include "third_party/blink/renderer/core/layout/api/line_layout_api_shim.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_block_flow.h"
#include "third_party/blink/renderer/core/layout/hit_test_location.h"
#include "third_party/blink/renderer/core/layout/layout_block_flow.h"
#include "third_party/blink/renderer/core/layout/line/inline_flow_box.h"
#include "third_party/blink/renderer/core/layout/line/root_inline_box.h"
#include "third_party/blink/renderer/core/paint/block_painter.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/platform/fonts/font_metrics.h"
#include "third_party/blink/renderer/platform/wtf/allocator/partitions.h"

#ifndef NDEBUG
#include <stdio.h>
#endif

namespace blink {

class LayoutObject;

struct SameSizeAsInlineBox : DisplayItemClient {
  ~SameSizeAsInlineBox() override = default;
  void* a[4];
  LayoutPoint b;
  LayoutUnit c;
  uint32_t bitfields;
#if DCHECK_IS_ON()
  bool f;
#endif
};

static_assert(sizeof(InlineBox) == sizeof(SameSizeAsInlineBox),
              "InlineBox should stay small");

#if DCHECK_IS_ON()
InlineBox::~InlineBox() {
  if (!has_bad_parent_ && parent_)
    parent_->SetHasBadChildList();
}
#endif

DISABLE_CFI_PERF
void InlineBox::Destroy() {
  // We do not need to issue invalidations if the page is being destroyed
  // since these objects will never be repainted.
  if (!line_layout_item_.DocumentBeingDestroyed()) {
    SetLineLayoutItemShouldDoFullPaintInvalidationIfNeeded();

    // TODO(crbug.com/619630): Make this fast.
    line_layout_item_.SlowSetPaintingLayerNeedsRepaint();
  }

  delete this;
}

void InlineBox::Remove(MarkLineBoxes mark_line_boxes) {
  if (Parent())
    Parent()->RemoveChild(this, mark_line_boxes);
}

void* InlineBox::operator new(size_t sz) {
  return WTF::Partitions::LayoutPartition()->Alloc(
      sz, WTF_HEAP_PROFILER_TYPE_NAME(InlineBox));
}

void InlineBox::operator delete(void* ptr) {
  base::PartitionFree(ptr);
}

const char* InlineBox::BoxName() const {
  return "InlineBox";
}

String InlineBox::DebugName() const {
  return BoxName();
}

LayoutRect InlineBox::VisualRect() const {
  return GetLineLayoutItem().VisualRectForInlineBox();
}

LayoutRect InlineBox::PartialInvalidationVisualRect() const {
  return GetLineLayoutItem().PartialInvalidationVisualRectForInlineBox();
}

#ifndef NDEBUG
void InlineBox::ShowTreeForThis() const {
  GetLineLayoutItem().ShowTreeForThis();
}

void InlineBox::ShowLineTreeForThis() const {
  const LayoutBlock* containing_block =
      LineLayoutAPIShim::LayoutObjectFrom(GetLineLayoutItem())
          ->InclusiveContainingBlock();
  if (containing_block) {
    LineLayoutBox(const_cast<LayoutBlock*>(containing_block))
        .ShowLineTreeAndMark(this, "*");
  }
}

void InlineBox::DumpLineTreeAndMark(StringBuilder& string_builder,
                                    const InlineBox* marked_box1,
                                    const char* marked_label1,
                                    const InlineBox* marked_box2,
                                    const char* marked_label2,
                                    const LayoutObject* obj,
                                    int depth) const {
  StringBuilder string_inlinebox;
  if (this == marked_box1)
    string_inlinebox.Append(marked_label1);
  if (this == marked_box2)
    string_inlinebox.Append(marked_label2);
  if (GetLineLayoutItem().IsEqual(obj))
    string_inlinebox.Append("*");
  while ((int)string_inlinebox.length() < (depth * 2))
    string_inlinebox.Append(" ");

  DumpBox(string_inlinebox);
  string_builder.Append('\n');
  string_builder.Append(string_inlinebox);
}

void InlineBox::DumpBox(StringBuilder& string_inlinebox) const {
  string_inlinebox.Append(String::Format("%s %p", BoxName(), this));
  while (string_inlinebox.length() < kShowTreeCharacterOffset)
    string_inlinebox.Append(" ");
  string_inlinebox.Append(
      String::Format("\t%s %p {pos=%g,%g size=%g,%g} baseline=%i/%i",
                     GetLineLayoutItem().DecoratedName().Ascii().data(),
                     GetLineLayoutItem().DebugPointer(), X().ToFloat(),
                     Y().ToFloat(), Width().ToFloat(), Height().ToFloat(),
                     BaselinePosition(kAlphabeticBaseline).ToInt(),
                     BaselinePosition(kIdeographicBaseline).ToInt()));
}
#endif

LayoutUnit InlineBox::LogicalHeight() const {
  if (HasVirtualLogicalHeight())
    return VirtualLogicalHeight();

  const SimpleFontData* font_data =
      GetLineLayoutItem().Style(IsFirstLineStyle())->GetFont().PrimaryFont();
  if (GetLineLayoutItem().IsText()) {
    DCHECK(font_data);
    return bitfields_.IsText() && font_data
               ? LayoutUnit(font_data->GetFontMetrics().Height())
               : LayoutUnit();
  }
  if (GetLineLayoutItem().IsBox() && Parent()) {
    return IsHorizontal() ? LineLayoutBox(GetLineLayoutItem()).Size().Height()
                          : LineLayoutBox(GetLineLayoutItem()).Size().Width();
  }

  DCHECK(IsInlineFlowBox());
  LineLayoutBoxModel flow_object = BoxModelObject();
  DCHECK(font_data);
  LayoutUnit result(font_data ? font_data->GetFontMetrics().Height() : 0);
  if (Parent())
    result += flow_object.BorderAndPaddingLogicalHeight();
  return result;
}

LayoutUnit InlineBox::BaselinePosition(FontBaseline baseline_type) const {
  return BoxModelObject().BaselinePosition(
      baseline_type, bitfields_.FirstLine(),
      IsHorizontal() ? kHorizontalLine : kVerticalLine,
      kPositionOnContainingLine);
}

LayoutUnit InlineBox::LineHeight() const {
  return BoxModelObject().LineHeight(
      bitfields_.FirstLine(), IsHorizontal() ? kHorizontalLine : kVerticalLine,
      kPositionOnContainingLine);
}

int InlineBox::CaretMinOffset() const {
  return GetLineLayoutItem().CaretMinOffset();
}

int InlineBox::CaretMaxOffset() const {
  return GetLineLayoutItem().CaretMaxOffset();
}

void InlineBox::DirtyLineBoxes() {
  MarkDirty();
  for (InlineFlowBox* curr = Parent(); curr && !curr->IsDirty();
       curr = curr->Parent())
    curr->MarkDirty();
}

void InlineBox::DeleteLine() {
  if (!bitfields_.Extracted() && GetLineLayoutItem().IsBox())
    LineLayoutBox(GetLineLayoutItem()).SetInlineBoxWrapper(nullptr);
  Destroy();
}

void InlineBox::ExtractLine() {
  bitfields_.SetExtracted(true);
  if (GetLineLayoutItem().IsBox())
    LineLayoutBox(GetLineLayoutItem()).SetInlineBoxWrapper(nullptr);
}

void InlineBox::AttachLine() {
  bitfields_.SetExtracted(false);
  if (GetLineLayoutItem().IsBox())
    LineLayoutBox(GetLineLayoutItem()).SetInlineBoxWrapper(this);
}

void InlineBox::Move(const LayoutSize& delta) {
  location_.Move(delta);

  if (GetLineLayoutItem().IsAtomicInlineLevel())
    LineLayoutBox(GetLineLayoutItem()).Move(delta.Width(), delta.Height());

  SetLineLayoutItemShouldDoFullPaintInvalidationIfNeeded();
}

void InlineBox::Paint(const PaintInfo& paint_info,
                      const LayoutPoint&,
                      LayoutUnit,
                      LayoutUnit) const {
  BlockPainter::PaintInlineBox(*this, paint_info);
}

bool InlineBox::NodeAtPoint(HitTestResult& result,
                            const HitTestLocation& location_in_container,
                            const LayoutPoint& accumulated_offset,
                            LayoutUnit /* lineTop */,
                            LayoutUnit /* lineBottom */) {
  // Hit test all phases of replaced elements atomically, as though the replaced
  // element established its own stacking context. (See Appendix E.2, section
  // 6.4 on inline block/table elements in the CSS2.1 specification.)
  LayoutPoint child_point = accumulated_offset;
  // Faster than calling containingBlock().
  if (Parent()->GetLineLayoutItem().HasFlippedBlocksWritingMode())
    child_point =
        GetLineLayoutItem().ContainingBlock().FlipForWritingModeForChild(
            LineLayoutBox(GetLineLayoutItem()), child_point);

  return GetLineLayoutItem().HitTestAllPhases(result, location_in_container,
                                              child_point);
}

const RootInlineBox& InlineBox::Root() const {
  if (parent_)
    return parent_->Root();
  DCHECK(IsRootInlineBox());
  return static_cast<const RootInlineBox&>(*this);
}

RootInlineBox& InlineBox::Root() {
  if (parent_)
    return parent_->Root();
  DCHECK(IsRootInlineBox());
  return static_cast<RootInlineBox&>(*this);
}

InlineBox* InlineBox::NextLeafChild() const {
  InlineBox* leaf = nullptr;
  for (InlineBox* box = NextOnLine(); box && !leaf; box = box->NextOnLine())
    leaf = box->IsLeaf() ? box : ToInlineFlowBox(box)->FirstLeafChild();
  if (!leaf && Parent())
    leaf = Parent()->NextLeafChild();
  return leaf;
}

InlineBox* InlineBox::PrevLeafChild() const {
  InlineBox* leaf = nullptr;
  for (InlineBox* box = PrevOnLine(); box && !leaf; box = box->PrevOnLine())
    leaf = box->IsLeaf() ? box : ToInlineFlowBox(box)->LastLeafChild();
  if (!leaf && Parent())
    leaf = Parent()->PrevLeafChild();
  return leaf;
}

InlineBox* InlineBox::NextLeafChildIgnoringLineBreak() const {
  InlineBox* leaf = NextLeafChild();
  return (leaf && leaf->IsLineBreak()) ? nullptr : leaf;
}

InlineBox* InlineBox::PrevLeafChildIgnoringLineBreak() const {
  InlineBox* leaf = PrevLeafChild();
  return (leaf && leaf->IsLineBreak()) ? nullptr : leaf;
}

bool InlineBox::IsSelected() const {
  return GetLineLayoutItem().IsSelected();
}

bool InlineBox::CanAccommodateEllipsis(bool ltr,
                                       LayoutUnit block_edge,
                                       LayoutUnit ellipsis_width) const {
  // Non-atomic inline-level elements can always accommodate an ellipsis.
  // Skip list markers and try the next box.
  if (!GetLineLayoutItem().IsAtomicInlineLevel() ||
      GetLineLayoutItem().IsListMarker())
    return true;

  LayoutRect box_rect(X(), LayoutUnit(), logical_width_, LayoutUnit(10));
  LayoutRect ellipsis_rect(ltr ? block_edge - ellipsis_width : block_edge,
                           LayoutUnit(), ellipsis_width, LayoutUnit(10));
  return !(box_rect.Intersects(ellipsis_rect));
}

LayoutUnit InlineBox::PlaceEllipsisBox(bool,
                                       LayoutUnit,
                                       LayoutUnit,
                                       LayoutUnit,
                                       LayoutUnit& truncated_width,
                                       InlineBox**,
                                       LayoutUnit) {
  // Use -1 to mean "we didn't set the position."
  truncated_width += LogicalWidth();
  return LayoutUnit(-1);
}

void InlineBox::ClearKnownToHaveNoOverflow() {
  bitfields_.SetKnownToHaveNoOverflow(false);
  if (Parent() && Parent()->KnownToHaveNoOverflow())
    Parent()->ClearKnownToHaveNoOverflow();
}

LayoutPoint InlineBox::PhysicalLocation() const {
  LayoutRect rect(Location(), Size());
  FlipForWritingMode(rect);
  return rect.Location();
}

void InlineBox::FlipForWritingMode(FloatRect& rect) const {
  if (!UNLIKELY(GetLineLayoutItem().HasFlippedBlocksWritingMode()))
    return;
  Root().Block().FlipForWritingMode(rect);
}

FloatPoint InlineBox::FlipForWritingMode(const FloatPoint& point) const {
  if (!UNLIKELY(GetLineLayoutItem().HasFlippedBlocksWritingMode()))
    return point;
  return Root().Block().FlipForWritingMode(point);
}

void InlineBox::FlipForWritingMode(LayoutRect& rect) const {
  if (!UNLIKELY(GetLineLayoutItem().HasFlippedBlocksWritingMode()))
    return;
  Root().Block().FlipForWritingMode(rect);
}

LayoutPoint InlineBox::FlipForWritingMode(const LayoutPoint& point) const {
  if (!UNLIKELY(GetLineLayoutItem().HasFlippedBlocksWritingMode()))
    return point;
  return Root().Block().FlipForWritingMode(point);
}

void InlineBox::SetShouldDoFullPaintInvalidationRecursively() {
  GetLineLayoutItem().SetShouldDoFullPaintInvalidation();
  if (!IsInlineFlowBox())
    return;
  for (InlineBox* child = ToInlineFlowBox(this)->FirstChild(); child;
       child = child->NextOnLine())
    child->SetShouldDoFullPaintInvalidationRecursively();
}

void InlineBox::SetLineLayoutItemShouldDoFullPaintInvalidationIfNeeded() {
  // For RootInlineBox, we only need to invalidate if it's using the first line
  // style. Otherwise it paints nothing so we don't need to invalidate it.
  if (!IsRootInlineBox() || IsFirstLineStyle())
    line_layout_item_.SetShouldDoFullPaintInvalidation();
}

bool CanUseInlineBox(const LayoutObject& node) {
  DCHECK(node.IsText() || node.IsInline() || node.IsLayoutBlockFlow());
  return !RuntimeEnabledFeatures::LayoutNGEnabled() ||
         !node.ContainingNGBlockFlow();
}

}  // namespace blink

#ifndef NDEBUG

void showTree(const blink::InlineBox* b) {
  if (b)
    b->ShowTreeForThis();
  else
    fprintf(stderr, "Cannot showTree for (nil) InlineBox.\n");
}

void showLineTree(const blink::InlineBox* b) {
  if (b)
    b->ShowLineTreeForThis();
  else
    fprintf(stderr, "Cannot showLineTree for (nil) InlineBox.\n");
}

#endif