File: InlineBox.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (575 lines) | stat: -rw-r--r-- 20,263 bytes parent folder | download
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * 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 InlineBox_h
#define InlineBox_h

#include "core/CoreExport.h"
#include "core/layout/api/LineLayoutBoxModel.h"
#include "core/layout/api/LineLayoutItem.h"
#include "core/layout/api/SelectionState.h"
#include "platform/graphics/paint/DisplayItemClient.h"
#include "platform/text/TextDirection.h"

namespace blink {

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

enum MarkLineBoxes { MarkLineBoxesDirty, DontMarkLineBoxes };

// 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 {
  WTF_MAKE_NONCOPYABLE(InlineBox);

 public:
  InlineBox(LineLayoutItem obj)
      : m_next(nullptr),
        m_prev(nullptr),
        m_parent(nullptr),
        m_lineLayoutItem(obj),
        m_logicalWidth()
  {
  }

  InlineBox(LineLayoutItem item,
            LayoutPoint topLeft,
            LayoutUnit logicalWidth,
            bool firstLine,
            bool constructed,
            bool dirty,
            bool extracted,
            bool isHorizontal,
            InlineBox* next,
            InlineBox* prev,
            InlineFlowBox* parent)
      : m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal),
        m_next(next),
        m_prev(prev),
        m_parent(parent),
        m_lineLayoutItem(item),
        m_location(topLeft),
        m_logicalWidth(logicalWidth)
  {
  }

  virtual ~InlineBox();

  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& deltaInLogicalDirection) {
    move(isHorizontal() ? deltaInLogicalDirection
                        : deltaInLogicalDirection.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 lineTop,
                     LayoutUnit lineBottom) const;
  virtual bool nodeAtPoint(HitTestResult&,
                           const HitTestLocation& locationInContainer,
                           const LayoutPoint& accumulatedOffset,
                           LayoutUnit lineTop,
                           LayoutUnit lineBottom);

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

#ifndef NDEBUG
  void showTreeForThis() const;
  void showLineTreeForThis() const;

  virtual void showBox(int = 0) const;
  virtual void showLineTreeAndMark(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;
  LayoutRect visualRect() const override;

  bool isText() const { return m_bitfields.isText(); }
  void setIsText(bool isText) { m_bitfields.setIsText(isText); }

  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 m_bitfields.hasVirtualLogicalHeight();
  }
  void setHasVirtualLogicalHeight() {
    m_bitfields.setHasVirtualLogicalHeight(true);
  }
  virtual LayoutUnit virtualLogicalHeight() const {
    ASSERT_NOT_REACHED();
    return LayoutUnit();
  }

  bool isHorizontal() const { return m_bitfields.isHorizontal(); }
  void setIsHorizontal(bool isHorizontal) {
    m_bitfields.setIsHorizontal(isHorizontal);
  }

  bool isConstructed() { return m_bitfields.constructed(); }
  virtual void setConstructed() { m_bitfields.setConstructed(true); }

  void setExtracted(bool extracted = true) {
    m_bitfields.setExtracted(extracted);
  }

  void setFirstLineStyleBit(bool firstLine) {
    m_bitfields.setFirstLine(firstLine);
  }
  bool isFirstLineStyle() const { return m_bitfields.firstLine(); }

  void remove(MarkLineBoxes = MarkLineBoxesDirty);

  InlineBox* nextOnLine() const { return m_next; }
  InlineBox* prevOnLine() const { return m_prev; }
  void setNextOnLine(InlineBox* next) {
    ASSERT(m_parent || !next);
    m_next = next;
  }
  void setPrevOnLine(InlineBox* prev) {
    ASSERT(m_parent || !prev);
    m_prev = prev;
  }
  bool nextOnLineExists() const;

  virtual bool isLeaf() const { return true; }

  InlineBox* nextLeafChild() const;
  InlineBox* prevLeafChild() const;

  // Helper functions for editing and hit-testing code.
  // FIXME: These two functions should be moved to RenderedPosition once the
  // code to convert between Position and inline box, offset pair is moved to
  // RenderedPosition.
  InlineBox* nextLeafChildIgnoringLineBreak() const;
  InlineBox* prevLeafChildIgnoringLineBreak() const;

  LineLayoutItem getLineLayoutItem() const { return m_lineLayoutItem; }

  InlineFlowBox* parent() const {
    ASSERT(!m_hasBadParent);
    return m_parent;
  }
  void setParent(InlineFlowBox* par) { m_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) { m_location.setX(x); }
  LayoutUnit x() const { return m_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) { m_location.setY(y); }
  LayoutUnit y() const { return m_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 m_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() ? m_location.x() : m_location.y();
  }
  LayoutUnit logicalRight() const { return logicalLeft() + logicalWidth(); }
  void setLogicalLeft(LayoutUnit left) {
    if (isHorizontal())
      setX(left);
    else
      setY(left);
  }
  int pixelSnappedLogicalLeft() const { return logicalLeft().toInt(); }
  int pixelSnappedLogicalRight() const { return logicalRight().ceil(); }
  int pixelSnappedLogicalTop() const { return logicalTop().toInt(); }
  int pixelSnappedLogicalBottom() const { return logicalBottom().ceil(); }

  // 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() ? m_location.y() : m_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) { m_logicalWidth = w; }
  LayoutUnit logicalWidth() const { return m_logicalWidth; }

  // 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(m_location.x(), m_location.y(),
                                       m_logicalWidth, logicalHeight())
                          : LayoutRect(m_location.y(), m_location.x(),
                                       m_logicalWidth, logicalHeight());
  }

  virtual int baselinePosition(FontBaseline baselineType) const;
  virtual LayoutUnit lineHeight() const;

  virtual int caretMinOffset() const;
  virtual int caretMaxOffset() const;

  unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
  void setBidiLevel(unsigned char level) {
    m_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 m_bitfields.dirty(); }
  virtual void markDirty() { m_bitfields.setDirty(true); }

  virtual void dirtyLineBoxes();

  virtual SelectionState getSelectionState() const;

  virtual bool canAccommodateEllipsis(bool ltr,
                                      int blockEdge,
                                      int ellipsisWidth) const;
  // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
  virtual LayoutUnit placeEllipsisBox(bool ltr,
                                      LayoutUnit visibleLeftEdge,
                                      LayoutUnit visibleRightEdge,
                                      LayoutUnit ellipsisWidth,
                                      LayoutUnit& truncatedWidth,
                                      bool&);

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

  int expansion() const { return m_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() ? ComputedStyle::initialVerticalAlign()
                               : getLineLayoutItem()
                                     .style(m_bitfields.firstLine())
                                     ->verticalAlign();
  }

  // Use with caution! The type is not checked!
  LineLayoutBoxModel boxModelObject() const {
    if (!getLineLayoutItem().isText())
      return LineLayoutBoxModel(m_lineLayoutItem);
    return LineLayoutBoxModel(nullptr);
  }

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

  // Converts from a rect in the logical space of the InlineBox to one in the
  // physical space of the containing block. The logical space of an InlineBox
  // may be transposed for vertical text and flipped for right-to-left text.
  void logicalRectToPhysicalRect(LayoutRect&) const;

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

  bool knownToHaveNoOverflow() const {
    return m_bitfields.knownToHaveNoOverflow();
  }
  void clearKnownToHaveNoOverflow();

  bool dirOverride() const { return m_bitfields.dirOverride(); }
  void setDirOverride(bool dirOverride) {
    m_bitfields.setDirOverride(dirOverride);
  }

  // Set all LineLayoutItems in the inline box subtree should do full paint
  // invalidation.
  void setShouldDoFullPaintInvalidationRecursively();

#define ADD_BOOLEAN_BITFIELD(name, Name) \
 private:                                \
  unsigned m_##name : 1;                 \
                                         \
 public:                                 \
  bool name() const { return m_##name; } \
  void set##Name(bool name) { m_##name = name; }

  class InlineBoxBitfields {
    DISALLOW_NEW();

   public:
    InlineBoxBitfields(bool firstLine = false,
                       bool constructed = false,
                       bool dirty = false,
                       bool extracted = false,
                       bool isHorizontal = true)
        : m_firstLine(firstLine),
          m_constructed(constructed),
          m_bidiEmbeddingLevel(0),
          m_dirty(dirty),
          m_extracted(extracted),
          m_hasVirtualLogicalHeight(false),
          m_isHorizontal(isHorizontal),
          m_endsWithBreak(false),
          m_hasSelectedChildrenOrCanHaveLeadingExpansion(false),
          m_knownToHaveNoOverflow(true),
          m_hasEllipsisBoxOrHyphen(false),
          m_dirOverride(false),
          m_isText(false),
          m_determinedIfNextOnLineExists(false),
          m_nextOnLineExists(false),
          m_expansion(0) {}

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

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

   public:
    unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
    void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) {
      m_bidiEmbeddingLevel = bidiEmbeddingLevel;
    }

    ADD_BOOLEAN_BITFIELD(dirty, Dirty);
    ADD_BOOLEAN_BITFIELD(extracted, Extracted);
    ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
    ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
    // for RootInlineBox
    ADD_BOOLEAN_BITFIELD(endsWithBreak,
                         EndsWithBreak);  // Whether the line ends with a <br>.
    // shared between RootInlineBox and InlineTextBox
    ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion,
                         HasSelectedChildrenOrCanHaveLeadingExpansion);

    // 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 m_overflow on
    // InlineFlowBox.
    ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
    ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
    // for InlineTextBox
    ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
    // Whether or not this object represents text with a non-zero height.
    // Includes non-image list markers, text boxes.
    ADD_BOOLEAN_BITFIELD(isText, IsText);

   private:
    mutable unsigned m_determinedIfNextOnLineExists : 1;

   public:
    bool determinedIfNextOnLineExists() const {
      return m_determinedIfNextOnLineExists;
    }
    void setDeterminedIfNextOnLineExists(
        bool determinedIfNextOnLineExists) const {
      m_determinedIfNextOnLineExists = determinedIfNextOnLineExists;
    }

   private:
    mutable unsigned m_nextOnLineExists : 1;

   public:
    bool nextOnLineExists() const { return m_nextOnLineExists; }
    void setNextOnLineExists(bool nextOnLineExists) const {
      m_nextOnLineExists = nextOnLineExists;
    }

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

   public:
    signed expansion() const { return m_expansion; }
    void setExpansion(signed expansion) { m_expansion = expansion; }
  };
#undef ADD_BOOLEAN_BITFIELD

 private:
  void setLineLayoutItemShouldDoFullPaintInvalidationIfNeeded();

  InlineBoxBitfields m_bitfields;

  InlineBox* m_next;  // The next element on the same line as us.
  InlineBox* m_prev;  // The previous element on the same line as us.

  InlineFlowBox* m_parent;  // The box that contains us.
  LineLayoutItem m_lineLayoutItem;

 protected:
  // For RootInlineBox
  bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
  void setEndsWithBreak(bool endsWithBreak) {
    m_bitfields.setEndsWithBreak(endsWithBreak);
  }
  bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
  bool hasSelectedChildren() const {
    return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion();
  }
  void setHasSelectedChildren(bool hasSelectedChildren) {
    m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(
        hasSelectedChildren);
  }
  void setHasEllipsisBox(bool hasEllipsisBox) {
    m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox);
  }

  // For InlineTextBox
  bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
  void setHasHyphen(bool hasHyphen) {
    m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen);
  }
  bool canHaveLeadingExpansion() const {
    return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion();
  }
  void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) {
    m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(
        canHaveLeadingExpansion);
  }
  signed expansion() { return m_bitfields.expansion(); }
  void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }

  // For InlineFlowBox and InlineTextBox
  bool extracted() const { return m_bitfields.extracted(); }

  LayoutPoint m_location;
  LayoutUnit m_logicalWidth;

 private:
#if DCHECK_IS_ON()
  bool m_hasBadParent = false;
#endif
};

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

#if DCHECK_IS_ON()
inline void InlineBox::setHasBadParent() {
  m_hasBadParent = 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)

}  // namespace blink

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

#endif  // InlineBox_h