summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/line/inline_box.h
blob: 2d5dc2026b9c5b69b979f744f303dfe92bb60972 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 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.
 *
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LINE_INLINE_BOX_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LINE_INLINE_BOX_H_

#include "base/macros.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_box_model.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_item.h"
#include "third_party/blink/renderer/platform/fonts/font_vertical_position_type.h"
#include "third_party/blink/renderer/platform/graphics/paint/display_item_client.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"

namespace blink {

class HitTestRequest;
class HitTestResult;
class InlineFlowBox;
class LayoutObject;
class RootInlineBox;

enum MarkLineBoxes { kMarkLineBoxesDirty, kDontMarkLineBoxes };

// InlineBox represents a rectangle that occurs on a line.  It corresponds to
// some LayoutObject (i.e., it represents a portion of that LayoutObject).
class CORE_EXPORT InlineBox : public DisplayItemClient {
 public:
  InlineBox(LineLayoutItem obj)
      : next_(nullptr),
        prev_(nullptr),
        parent_(nullptr),
        line_layout_item_(obj),
        logical_width_() {}

  InlineBox(LineLayoutItem item,
            LayoutPoint top_left,
            LayoutUnit logical_width,
            bool first_line,
            bool constructed,
            bool dirty,
            bool extracted,
            bool is_horizontal,
            InlineBox* next,
            InlineBox* prev,
            InlineFlowBox* parent)
      : next_(next),
        prev_(prev),
        parent_(parent),
        line_layout_item_(item),
        location_(top_left),
        logical_width_(logical_width),
        bitfields_(first_line, constructed, dirty, extracted, is_horizontal) {}

  ~InlineBox() override;

  virtual void Destroy();

  virtual void DeleteLine();
  virtual void ExtractLine();
  virtual void AttachLine();

  virtual bool IsLineBreak() const { return false; }

  // These methods are called when the caller wants to move the position of
  // InlineBox without full layout of it. The implementation should update the
  // position of the whole subtree (e.g. position of descendants and overflow
  // etc. should also be moved accordingly).
  virtual void Move(const LayoutSize& delta);
  DISABLE_CFI_PERF void MoveInLogicalDirection(
      const LayoutSize& delta_in_logical_direction) {
    Move(IsHorizontal() ? delta_in_logical_direction
                        : delta_in_logical_direction.TransposedSize());
  }
  void MoveInInlineDirection(LayoutUnit delta) {
    MoveInLogicalDirection(LayoutSize(delta, LayoutUnit()));
  }
  void MoveInBlockDirection(LayoutUnit delta) {
    MoveInLogicalDirection(LayoutSize(LayoutUnit(), delta));
  }

  virtual void Paint(const PaintInfo&,
                     const LayoutPoint&,
                     LayoutUnit line_top,
                     LayoutUnit line_bottom) const;
  virtual bool NodeAtPoint(HitTestResult&,
                           const HitTestLocation&,
                           const PhysicalOffset& accumulated_offset,
                           LayoutUnit line_top,
                           LayoutUnit line_bottom);

  // InlineBoxes are allocated out of the rendering partition.
  void* operator new(size_t);
  void operator delete(void*);

#if DCHECK_IS_ON()
  void ShowTreeForThis() const;
  void ShowLineTreeForThis() const;

  virtual void DumpBox(StringBuilder&) const;
  virtual void DumpLineTreeAndMark(StringBuilder&,
                                   const InlineBox* = nullptr,
                                   const char* = nullptr,
                                   const InlineBox* = nullptr,
                                   const char* = nullptr,
                                   const LayoutObject* = nullptr,
                                   int = 0) const;
#endif

  virtual const char* BoxName() const;

  // DisplayItemClient methods
  String DebugName() const override;
  IntRect VisualRect() const override;
  IntRect PartialInvalidationVisualRect() const override;

  bool IsText() const { return bitfields_.IsText(); }
  void SetIsText(bool is_text) { bitfields_.SetIsText(is_text); }

  virtual bool IsInlineFlowBox() const { return false; }
  virtual bool IsInlineTextBox() const { return false; }
  virtual bool IsRootInlineBox() const { return false; }

  virtual bool IsSVGInlineTextBox() const { return false; }
  virtual bool IsSVGInlineFlowBox() const { return false; }
  virtual bool IsSVGRootInlineBox() const { return false; }

  bool HasVirtualLogicalHeight() const {
    return bitfields_.HasVirtualLogicalHeight();
  }
  void SetHasVirtualLogicalHeight() {
    bitfields_.SetHasVirtualLogicalHeight(true);
  }
  virtual LayoutUnit VirtualLogicalHeight() const {
    NOTREACHED();
    return LayoutUnit();
  }

  bool IsHorizontal() const { return bitfields_.IsHorizontal(); }
  void SetIsHorizontal(bool is_horizontal) {
    bitfields_.SetIsHorizontal(is_horizontal);
  }

  bool IsConstructed() { return bitfields_.Constructed(); }
  virtual void SetConstructed() { bitfields_.SetConstructed(true); }

  void SetExtracted(bool extracted = true) {
    bitfields_.SetExtracted(extracted);
  }

  void SetFirstLineStyleBit(bool first_line) {
    bitfields_.SetFirstLine(first_line);
  }
  bool IsFirstLineStyle() const { return bitfields_.FirstLine(); }
  const ComputedStyle& LineStyleRef() const {
    return GetLineLayoutItem().StyleRef(IsFirstLineStyle());
  }

  void Remove(MarkLineBoxes = kMarkLineBoxesDirty);

  InlineBox* NextOnLine() const { return next_; }
  InlineBox* PrevOnLine() const { return prev_; }
  void SetNextOnLine(InlineBox* next) {
    DCHECK(parent_ || !next);
    next_ = next;
  }
  void SetPrevOnLine(InlineBox* prev) {
    DCHECK(parent_ || !prev);
    prev_ = prev;
  }

  virtual bool IsLeaf() const { return true; }

  InlineBox* NextLeafChild() const;
  InlineBox* PrevLeafChild() const;

  // Helper functions for editing and hit-testing code.
  InlineBox* NextLeafChildIgnoringLineBreak() const;
  InlineBox* PrevLeafChildIgnoringLineBreak() const;

  LineLayoutItem GetLineLayoutItem() const { return line_layout_item_; }

  InlineFlowBox* Parent() const {
#if DCHECK_IS_ON()
    DCHECK(!has_bad_parent_);
#endif
    return parent_;
  }

  void SetParent(InlineFlowBox* par) { parent_ = par; }

  const RootInlineBox& Root() const;
  RootInlineBox& Root();

  // x() is the location of the box in the containing block's "physical
  // coordinates with flipped block-flow direction".
  // See ../README.md#Coordinate-Spaces for the definition.
  void SetX(LayoutUnit x) { location_.SetX(x); }
  LayoutUnit X() const { return location_.X(); }

  // y() is the top side of the box in the containing block's physical
  // coordinates. It's actually in the same coordinate space as x() but for y()
  // physical coordinates and "physical coordinates with flipped block-flow
  // direction" are the same.
  void SetY(LayoutUnit y) { location_.SetY(y); }
  LayoutUnit Y() const { return location_.Y(); }

  // x() and y() in one LayoutPoint, in the containing block's "physical
  // coordinates with flipped block-flow direction".
  const LayoutPoint& Location() const { return location_; }

  LayoutUnit Width() const {
    return IsHorizontal() ? LogicalWidth() : LogicalHeight();
  }
  LayoutUnit Height() const {
    return IsHorizontal() ? LogicalHeight() : LogicalWidth();
  }
  LayoutSize Size() const { return LayoutSize(Width(), Height()); }

  // The logicalLeft position is the left edge of the line box in a horizontal
  // line and the top edge in a vertical line.
  LayoutUnit LogicalLeft() const {
    return IsHorizontal() ? location_.X() : location_.Y();
  }
  LayoutUnit LogicalRight() const { return LogicalLeft() + LogicalWidth(); }
  void SetLogicalLeft(LayoutUnit left) {
    if (IsHorizontal())
      SetX(left);
    else
      SetY(left);
  }

  // The logicalTop[ position is the top edge of the line box in a horizontal
  // line and the left edge in a vertical line.
  LayoutUnit LogicalTop() const {
    return IsHorizontal() ? location_.Y() : location_.X();
  }
  LayoutUnit LogicalBottom() const { return LogicalTop() + LogicalHeight(); }
  void SetLogicalTop(LayoutUnit top) {
    if (IsHorizontal())
      SetY(top);
    else
      SetX(top);
  }

  // The logical width is our extent in the line's overall inline direction,
  // i.e., width for horizontal text and height for vertical text.
  void SetLogicalWidth(LayoutUnit w) { logical_width_ = w; }
  LayoutUnit LogicalWidth() const { return logical_width_; }

  // The logical height is our extent in the block flow direction, i.e., height
  // for horizontal text and width for vertical text.
  LayoutUnit LogicalHeight() const;

  LayoutRect LogicalFrameRect() const {
    return IsHorizontal() ? LayoutRect(location_.X(), location_.Y(),
                                       logical_width_, LogicalHeight())
                          : LayoutRect(location_.Y(), location_.X(),
                                       logical_width_, LogicalHeight());
  }

  virtual LayoutUnit BaselinePosition(FontBaseline baseline_type) const;
  virtual LayoutUnit LineHeight() const;

  virtual int CaretMinOffset() const;
  virtual int CaretMaxOffset() const;

  unsigned char BidiLevel() const { return bitfields_.BidiEmbeddingLevel(); }
  void SetBidiLevel(unsigned char level) {
    bitfields_.SetBidiEmbeddingLevel(level);
  }
  TextDirection Direction() const {
    return BidiLevel() % 2 ? TextDirection::kRtl : TextDirection::kLtr;
  }
  bool IsLeftToRightDirection() const {
    return Direction() == TextDirection::kLtr;
  }
  int CaretLeftmostOffset() const {
    return IsLeftToRightDirection() ? CaretMinOffset() : CaretMaxOffset();
  }
  int CaretRightmostOffset() const {
    return IsLeftToRightDirection() ? CaretMaxOffset() : CaretMinOffset();
  }

  virtual void ClearTruncation() {}

  bool IsDirty() const { return bitfields_.Dirty(); }
  virtual void MarkDirty() { bitfields_.SetDirty(true); }

  virtual void DirtyLineBoxes();

  virtual bool IsSelected() const;

  virtual bool CanAccommodateEllipsis(bool ltr,
                                      LayoutUnit block_edge,
                                      LayoutUnit ellipsis_width) const;
  // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
  virtual LayoutUnit PlaceEllipsisBox(bool ltr,
                                      LayoutUnit visible_left_edge,
                                      LayoutUnit visible_right_edge,
                                      LayoutUnit ellipsis_width,
                                      LayoutUnit& truncated_width,
                                      InlineBox**,
                                      LayoutUnit logical_left_offset);

#if DCHECK_IS_ON()
  void SetHasBadParent();
#endif

  int Expansion() const { return bitfields_.Expansion(); }

  bool VisibleToHitTestRequest(const HitTestRequest& request) const {
    return GetLineLayoutItem().VisibleToHitTestRequest(request);
  }

  // Anonymous inline: https://drafts.csswg.org/css2/visuren.html#anonymous
  bool IsAnonymousInline() const {
    return GetLineLayoutItem().IsText() && GetLineLayoutItem().Parent() &&
           GetLineLayoutItem().Parent().IsBox();
  }
  EVerticalAlign VerticalAlign() const {
    return IsAnonymousInline()
               ? ComputedStyleInitialValues::InitialVerticalAlign()
               : GetLineLayoutItem()
                     .Style(bitfields_.FirstLine())
                     ->VerticalAlign();
  }

  // Use with caution! The type is not checked!
  LineLayoutBoxModel BoxModelObject() const {
    if (!GetLineLayoutItem().IsText())
      return LineLayoutBoxModel(line_layout_item_);
    return LineLayoutBoxModel(nullptr);
  }

  // Physical location of the top-left corner of the box in the containing
  // block.
  PhysicalOffset PhysicalLocation() const;

  // TODO(szager): The Rect versions should return a rect, not modify the
  // argument.
  void FlipForWritingMode(LayoutRect&) const;
  LayoutPoint FlipForWritingMode(const LayoutPoint&) const;

  // Returns trus if it is known that this box has no layout or visual
  // overflow. This is used as a fast-path to skip expensive overflow
  // recalc.
  bool KnownToHaveNoOverflow() const {
    return bitfields_.KnownToHaveNoOverflow();
  }
  void ClearKnownToHaveNoOverflow();

  bool DirOverride() const { return bitfields_.DirOverride(); }
  void SetDirOverride(bool dir_override) {
    bitfields_.SetDirOverride(dir_override);
  }

  // Set all LineLayoutItems in the inline box subtree should do full paint
  // invalidation and clear the first line style cache.
  void SetShouldDoFullPaintInvalidationForFirstLine();

#define ADD_BOOLEAN_BITFIELD(field_name_, MethodNameBase)               \
 public:                                                                \
  bool MethodNameBase() const { return field_name_; }                   \
  void Set##MethodNameBase(bool new_value) { field_name_ = new_value; } \
                                                                        \
 private:                                                               \
  unsigned field_name_ : 1

  class InlineBoxBitfields {
    DISALLOW_NEW();

   public:
    InlineBoxBitfields(bool first_line = false,
                       bool constructed = false,
                       bool dirty = false,
                       bool extracted = false,
                       bool is_horizontal = true)
        : first_line_(first_line),
          constructed_(constructed),
          bidi_embedding_level_(0),
          dirty_(dirty),
          extracted_(extracted),
          has_virtual_logical_height_(false),
          is_horizontal_(is_horizontal),
          ends_with_break_(false),
          can_have_leading_expansion_(false),
          known_to_have_no_overflow_(true),
          has_ellipsis_box_or_hyphen_(false),
          dir_override_(false),
          is_text_(false),
          expansion_(0) {}

    // Some of these bits are actually for subclasses and moved here to compact
    // the structures.
    // for this class
    ADD_BOOLEAN_BITFIELD(first_line_, FirstLine);
    ADD_BOOLEAN_BITFIELD(constructed_, Constructed);

   private:
    // The maximium bidi level is 62:
    // http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
    unsigned bidi_embedding_level_ : 6;

   public:
    unsigned char BidiEmbeddingLevel() const { return bidi_embedding_level_; }
    void SetBidiEmbeddingLevel(unsigned char bidi_embedding_level) {
      bidi_embedding_level_ = bidi_embedding_level;
    }

    ADD_BOOLEAN_BITFIELD(dirty_, Dirty);
    ADD_BOOLEAN_BITFIELD(extracted_, Extracted);
    ADD_BOOLEAN_BITFIELD(has_virtual_logical_height_, HasVirtualLogicalHeight);
    ADD_BOOLEAN_BITFIELD(is_horizontal_, IsHorizontal);
    // for RootInlineBox
    ADD_BOOLEAN_BITFIELD(ends_with_break_,
                         EndsWithBreak);  // Whether the line ends with a <br>.
    // shared between RootInlineBox and InlineTextBox
    ADD_BOOLEAN_BITFIELD(can_have_leading_expansion_, CanHaveLeadingExpansion);

    // This boolean will never be set if there is potential for overflow, but it
    // will be eagerly cleared in the opposite case. As such, it's a
    // conservative tracking of the absence of overflow.
    //
    // For whether we have overflow, callers should use |overflow_| on
    // InlineFlowBox.
    ADD_BOOLEAN_BITFIELD(known_to_have_no_overflow_, KnownToHaveNoOverflow);
    ADD_BOOLEAN_BITFIELD(has_ellipsis_box_or_hyphen_, HasEllipsisBoxOrHyphen);
    // for InlineTextBox
    ADD_BOOLEAN_BITFIELD(dir_override_, DirOverride);
    // Whether or not this object represents text with a non-zero height.
    // Includes non-image list markers, text boxes.
    ADD_BOOLEAN_BITFIELD(is_text_, IsText);

   private:
    unsigned expansion_ : 12;  // for justified text

   public:
    signed Expansion() const { return expansion_; }
    void SetExpansion(signed expansion) { expansion_ = expansion; }
  };
#undef ADD_BOOLEAN_BITFIELD

 private:
  void SetLineLayoutItemShouldDoFullPaintInvalidationIfNeeded();

  InlineBox* next_;  // The next element on the same line as us.
  InlineBox* prev_;  // The previous element on the same line as us.

  InlineFlowBox* parent_;  // The box that contains us.
  LineLayoutItem line_layout_item_;

 protected:
  // For RootInlineBox
  bool EndsWithBreak() const { return bitfields_.EndsWithBreak(); }
  void SetEndsWithBreak(bool ends_with_break) {
    bitfields_.SetEndsWithBreak(ends_with_break);
  }
  bool HasEllipsisBox() const { return bitfields_.HasEllipsisBoxOrHyphen(); }
  void SetHasEllipsisBox(bool has_ellipsis_box) {
    bitfields_.SetHasEllipsisBoxOrHyphen(has_ellipsis_box);
  }

  // For InlineTextBox
  bool HasHyphen() const { return bitfields_.HasEllipsisBoxOrHyphen(); }
  void SetHasHyphen(bool has_hyphen) {
    bitfields_.SetHasEllipsisBoxOrHyphen(has_hyphen);
  }
  bool CanHaveLeadingExpansion() const {
    return bitfields_.CanHaveLeadingExpansion();
  }
  void SetCanHaveLeadingExpansion(bool can_have_leading_expansion) {
    bitfields_.SetCanHaveLeadingExpansion(can_have_leading_expansion);
  }
  signed Expansion() { return bitfields_.Expansion(); }
  void SetExpansion(signed expansion) { bitfields_.SetExpansion(expansion); }

  // For InlineFlowBox and InlineTextBox
  bool Extracted() const { return bitfields_.Extracted(); }

  LayoutPoint location_;
  LayoutUnit logical_width_;

 private:
  InlineBoxBitfields bitfields_;

#if DCHECK_IS_ON()
  bool has_bad_parent_ = false;
#endif

  DISALLOW_COPY_AND_ASSIGN(InlineBox);
};

#if !DCHECK_IS_ON()
inline InlineBox::~InlineBox() {}
#endif

#if DCHECK_IS_ON()
inline void InlineBox::SetHasBadParent() {
  has_bad_parent_ = true;
}
#endif

#define DEFINE_INLINE_BOX_TYPE_CASTS(typeName)                     \
  DEFINE_TYPE_CASTS(typeName, InlineBox, box, box->Is##typeName(), \
                    box.Is##typeName())

// Allow equality comparisons of InlineBox's by reference or pointer,
// interchangeably.
DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(InlineBox)

// TODO(layout-dev): Once LayoutNG supports inline layout, we should remove
// |CanUseInlineBox()|.
bool CanUseInlineBox(const LayoutObject&);

}  // namespace blink

#if DCHECK_IS_ON()
// Outside the blink namespace for ease of invocation from gdb.
void showTree(const blink::InlineBox*);
void showLineTree(const blink::InlineBox*);
#endif

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LINE_INLINE_BOX_H_