2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "core/layout/LayoutBoxModelObject.h"
25 #include "core/layout/LayoutObject.h"
26 #include "core/layout/api/LineLayoutBoxModel.h"
27 #include "core/layout/api/LineLayoutItem.h"
28 #include "core/layout/api/SelectionState.h"
29 #include "platform/graphics/paint/DisplayItemClient.h"
30 #include "platform/text/TextDirection.h"
38 enum MarkLineBoxes
{ MarkLineBoxesDirty
, DontMarkLineBoxes
};
40 // InlineBox represents a rectangle that occurs on a line. It corresponds to
41 // some LayoutObject (i.e., it represents a portion of that LayoutObject).
43 WTF_MAKE_NONCOPYABLE(InlineBox
);
45 InlineBox(LayoutObject
& obj
)
52 , m_hasBadParent(false)
57 InlineBox(LayoutObject
& obj
, LayoutPoint topLeft
, LayoutUnit logicalWidth
, bool firstLine
, bool constructed
,
58 bool dirty
, bool extracted
, bool isHorizontal
, InlineBox
* next
, InlineBox
* prev
, InlineFlowBox
* parent
)
64 , m_logicalWidth(logicalWidth
)
65 , m_bitfields(firstLine
, constructed
, dirty
, extracted
, isHorizontal
)
67 , m_hasBadParent(false)
74 virtual void destroy();
76 virtual void deleteLine();
77 virtual void extractLine();
78 virtual void attachLine();
80 virtual bool isLineBreak() const { return false; }
82 // These methods are called when the caller wants to move the position of InlineBox without full layout of it.
83 // The implementation should update the position of the whole subtree (e.g. position of descendants and overflow etc.
84 // should also be moved accordingly).
85 virtual void move(const LayoutSize
& delta
);
86 void moveInLogicalDirection(const LayoutSize
& deltaInLogicalDirection
) { move(isHorizontal() ? deltaInLogicalDirection
: deltaInLogicalDirection
.transposedSize()); }
87 void moveInInlineDirection(LayoutUnit delta
) { moveInLogicalDirection(LayoutSize(delta
, LayoutUnit())); }
88 void moveInBlockDirection(LayoutUnit delta
) { moveInLogicalDirection(LayoutSize(LayoutUnit(), delta
)); }
90 virtual void paint(const PaintInfo
&, const LayoutPoint
&, LayoutUnit lineTop
, LayoutUnit lineBottom
);
91 virtual bool nodeAtPoint(HitTestResult
&, const HitTestLocation
& locationInContainer
, const LayoutPoint
& accumulatedOffset
, LayoutUnit lineTop
, LayoutUnit lineBottom
);
93 // InlineBoxes are allocated out of the rendering partition.
94 void* operator new(size_t);
95 void operator delete(void*);
98 void showTreeForThis() const;
99 void showLineTreeForThis() const;
101 virtual void showBox(int = 0) const;
102 virtual void showLineTreeAndMark(const InlineBox
* = nullptr, const char* = nullptr, const InlineBox
* = nullptr, const char* = nullptr, const LayoutObject
* = nullptr, int = 0) const;
105 virtual const char* boxName() const;
106 virtual String
debugName() const;
108 bool isText() const { return m_bitfields
.isText(); }
109 void setIsText(bool isText
) { m_bitfields
.setIsText(isText
); }
111 virtual bool isInlineFlowBox() const { return false; }
112 virtual bool isInlineTextBox() const { return false; }
113 virtual bool isRootInlineBox() const { return false; }
115 virtual bool isSVGInlineTextBox() const { return false; }
116 virtual bool isSVGInlineFlowBox() const { return false; }
117 virtual bool isSVGRootInlineBox() const { return false; }
119 bool hasVirtualLogicalHeight() const { return m_bitfields
.hasVirtualLogicalHeight(); }
120 void setHasVirtualLogicalHeight() { m_bitfields
.setHasVirtualLogicalHeight(true); }
121 virtual LayoutUnit
virtualLogicalHeight() const
123 ASSERT_NOT_REACHED();
127 bool isHorizontal() const { return m_bitfields
.isHorizontal(); }
128 void setIsHorizontal(bool isHorizontal
) { m_bitfields
.setIsHorizontal(isHorizontal
); }
130 virtual LayoutRect
calculateBoundaries() const
132 ASSERT_NOT_REACHED();
136 bool isConstructed() { return m_bitfields
.constructed(); }
137 virtual void setConstructed() { m_bitfields
.setConstructed(true); }
139 void setExtracted(bool extracted
= true) { m_bitfields
.setExtracted(extracted
); }
141 void setFirstLineStyleBit(bool firstLine
) { m_bitfields
.setFirstLine(firstLine
); }
142 bool isFirstLineStyle() const { return m_bitfields
.firstLine(); }
144 void remove(MarkLineBoxes
= MarkLineBoxesDirty
);
146 InlineBox
* nextOnLine() const { return m_next
; }
147 InlineBox
* prevOnLine() const { return m_prev
; }
148 void setNextOnLine(InlineBox
* next
)
150 ASSERT(m_parent
|| !next
);
153 void setPrevOnLine(InlineBox
* prev
)
155 ASSERT(m_parent
|| !prev
);
158 bool nextOnLineExists() const;
160 virtual bool isLeaf() const { return true; }
162 InlineBox
* nextLeafChild() const;
163 InlineBox
* prevLeafChild() const;
165 // Helper functions for editing and hit-testing code.
166 // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
167 // Position and inline box, offset pair is moved to RenderedPosition.
168 InlineBox
* nextLeafChildIgnoringLineBreak() const;
169 InlineBox
* prevLeafChildIgnoringLineBreak() const;
171 // TODO(pilgrim): This will be removed as part of the Line Layout API refactoring crbug.com/499321
172 LayoutObject
& layoutObject() const { return m_layoutObject
; }
173 LineLayoutItem
lineLayoutItem() const { return LineLayoutItem(&m_layoutObject
); }
175 InlineFlowBox
* parent() const
177 ASSERT(!m_hasBadParent
);
180 void setParent(InlineFlowBox
* par
) { m_parent
= par
; }
182 const RootInlineBox
& root() const;
183 RootInlineBox
& root();
185 // x() is the left side of the box in the containing block's coordinate system.
186 void setX(LayoutUnit x
) { m_topLeft
.setX(x
); }
187 LayoutUnit
x() const { return m_topLeft
.x(); }
188 LayoutUnit
left() const { return m_topLeft
.x(); }
190 // y() is the top side of the box in the containing block's coordinate system.
191 void setY(LayoutUnit y
) { m_topLeft
.setY(y
); }
192 LayoutUnit
y() const { return m_topLeft
.y(); }
193 LayoutUnit
top() const { return m_topLeft
.y(); }
195 const LayoutPoint
& topLeft() const { return m_topLeft
; }
197 LayoutUnit
width() const { return isHorizontal() ? logicalWidth() : hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight(); }
198 LayoutUnit
height() const { return isHorizontal() ? hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight() : logicalWidth(); }
199 LayoutSize
size() const { return LayoutSize(width(), height()); }
200 LayoutUnit
right() const { return left() + width(); }
201 LayoutUnit
bottom() const { return top() + height(); }
203 // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
204 LayoutUnit
logicalLeft() const { return isHorizontal() ? m_topLeft
.x() : m_topLeft
.y(); }
205 LayoutUnit
logicalRight() const { return logicalLeft() + logicalWidth(); }
206 void setLogicalLeft(LayoutUnit left
)
213 int pixelSnappedLogicalLeft() const { return logicalLeft(); }
214 int pixelSnappedLogicalRight() const { return logicalRight().ceil(); }
215 int pixelSnappedLogicalTop() const { return logicalTop(); }
216 int pixelSnappedLogicalBottom() const { return logicalBottom().ceil(); }
218 // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
219 LayoutUnit
logicalTop() const { return isHorizontal() ? m_topLeft
.y() : m_topLeft
.x(); }
220 LayoutUnit
logicalBottom() const { return logicalTop() + logicalHeight(); }
221 void setLogicalTop(LayoutUnit top
)
229 // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
230 void setLogicalWidth(LayoutUnit w
) { m_logicalWidth
= w
; }
231 LayoutUnit
logicalWidth() const { return m_logicalWidth
; }
233 // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
234 LayoutUnit
logicalHeight() const;
236 LayoutRect
logicalFrameRect() const { return isHorizontal() ? LayoutRect(m_topLeft
.x(), m_topLeft
.y(), m_logicalWidth
, logicalHeight()) : LayoutRect(m_topLeft
.y(), m_topLeft
.x(), m_logicalWidth
, logicalHeight()); }
238 virtual int baselinePosition(FontBaseline baselineType
) const;
239 virtual LayoutUnit
lineHeight() const;
241 virtual int caretMinOffset() const;
242 virtual int caretMaxOffset() const;
244 unsigned char bidiLevel() const { return m_bitfields
.bidiEmbeddingLevel(); }
245 void setBidiLevel(unsigned char level
) { m_bitfields
.setBidiEmbeddingLevel(level
); }
246 TextDirection
direction() const { return bidiLevel() % 2 ? RTL
: LTR
; }
247 bool isLeftToRightDirection() const { return direction() == LTR
; }
248 int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
249 int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
251 virtual void clearTruncation() { }
253 bool isDirty() const { return m_bitfields
.dirty(); }
254 virtual void markDirty() { m_bitfields
.setDirty(true); }
256 virtual void dirtyLineBoxes();
258 virtual SelectionState
selectionState() const;
260 virtual bool canAccommodateEllipsis(bool ltr
, int blockEdge
, int ellipsisWidth
) const;
261 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
262 virtual LayoutUnit
placeEllipsisBox(bool ltr
, LayoutUnit visibleLeftEdge
, LayoutUnit visibleRightEdge
, LayoutUnit ellipsisWidth
, LayoutUnit
&truncatedWidth
, bool&);
265 void setHasBadParent();
268 int expansion() const { return m_bitfields
.expansion(); }
270 bool visibleToHitTestRequest(const HitTestRequest
& request
) const { return lineLayoutItem().visibleToHitTestRequest(request
); }
272 EVerticalAlign
verticalAlign() const { return lineLayoutItem().isText() ? ComputedStyle::initialVerticalAlign() : lineLayoutItem().style(m_bitfields
.firstLine())->verticalAlign(); }
274 // Use with caution! The type is not checked!
275 LineLayoutBoxModel
boxModelObject() const
277 if (!lineLayoutItem().isText())
278 return LineLayoutBoxModel(toLayoutBoxModelObject(&m_layoutObject
));
279 return LineLayoutBoxModel(nullptr);
282 LayoutPoint
locationIncludingFlipping();
284 // Converts from a rect in the logical space of the InlineBox to one in the physical space
285 // of the containing block. The logical space of an InlineBox may be transposed for vertical text and
286 // flipped for right-to-left text.
287 void logicalRectToPhysicalRect(LayoutRect
&);
289 void flipForWritingMode(FloatRect
&);
290 FloatPoint
flipForWritingMode(const FloatPoint
&);
291 void flipForWritingMode(LayoutRect
&);
292 LayoutPoint
flipForWritingMode(const LayoutPoint
&);
294 bool knownToHaveNoOverflow() const { return m_bitfields
.knownToHaveNoOverflow(); }
295 void clearKnownToHaveNoOverflow();
297 bool dirOverride() const { return m_bitfields
.dirOverride(); }
298 void setDirOverride(bool dirOverride
) { m_bitfields
.setDirOverride(dirOverride
); }
300 DisplayItemClient
displayItemClient() const { return toDisplayItemClient(this); }
302 #define ADD_BOOLEAN_BITFIELD(name, Name) \
304 unsigned m_##name : 1;\
306 bool name() const { return m_##name; }\
307 void set##Name(bool name) { m_##name = name; }\
309 class InlineBoxBitfields {
310 DISALLOW_ALLOCATION();
312 InlineBoxBitfields(bool firstLine
= false, bool constructed
= false, bool dirty
= false, bool extracted
= false, bool isHorizontal
= true)
313 : m_firstLine(firstLine
)
314 , m_constructed(constructed
)
315 , m_bidiEmbeddingLevel(0)
317 , m_extracted(extracted
)
318 , m_hasVirtualLogicalHeight(false)
319 , m_isHorizontal(isHorizontal
)
320 , m_endsWithBreak(false)
321 , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
322 , m_knownToHaveNoOverflow(true)
323 , m_hasEllipsisBoxOrHyphen(false)
324 , m_dirOverride(false)
326 , m_determinedIfNextOnLineExists(false)
327 , m_nextOnLineExists(false)
332 // Some of these bits are actually for subclasses and moved here to compact the structures.
334 ADD_BOOLEAN_BITFIELD(firstLine
, FirstLine
);
335 ADD_BOOLEAN_BITFIELD(constructed
, Constructed
);
338 unsigned m_bidiEmbeddingLevel
: 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
341 unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel
; }
342 void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel
) { m_bidiEmbeddingLevel
= bidiEmbeddingLevel
; }
344 ADD_BOOLEAN_BITFIELD(dirty
, Dirty
);
345 ADD_BOOLEAN_BITFIELD(extracted
, Extracted
);
346 ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight
, HasVirtualLogicalHeight
);
347 ADD_BOOLEAN_BITFIELD(isHorizontal
, IsHorizontal
);
349 ADD_BOOLEAN_BITFIELD(endsWithBreak
, EndsWithBreak
); // Whether the line ends with a <br>.
350 // shared between RootInlineBox and InlineTextBox
351 ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion
, HasSelectedChildrenOrCanHaveLeadingExpansion
);
352 ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow
, KnownToHaveNoOverflow
);
353 ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen
, HasEllipsisBoxOrHyphen
);
355 ADD_BOOLEAN_BITFIELD(dirOverride
, DirOverride
);
356 ADD_BOOLEAN_BITFIELD(isText
, IsText
); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
359 mutable unsigned m_determinedIfNextOnLineExists
: 1;
362 bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists
; }
363 void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists
) const { m_determinedIfNextOnLineExists
= determinedIfNextOnLineExists
; }
366 mutable unsigned m_nextOnLineExists
: 1;
369 bool nextOnLineExists() const { return m_nextOnLineExists
; }
370 void setNextOnLineExists(bool nextOnLineExists
) const { m_nextOnLineExists
= nextOnLineExists
; }
373 unsigned m_expansion
: 12; // for justified text
376 signed expansion() const { return m_expansion
; }
377 void setExpansion(signed expansion
) { m_expansion
= expansion
; }
379 #undef ADD_BOOLEAN_BITFIELD
382 // Converts the given (top-left) position from the logical space of the InlineBox to the physical space of the
383 // containing block. The size indicates the size of the box whose point is being flipped.
384 LayoutPoint
logicalPositionToPhysicalPoint(const LayoutPoint
&, const LayoutSize
&);
386 InlineBox
* m_next
; // The next element on the same line as us.
387 InlineBox
* m_prev
; // The previous element on the same line as us.
389 InlineFlowBox
* m_parent
; // The box that contains us.
390 LayoutObject
& m_layoutObject
;
394 bool endsWithBreak() const { return m_bitfields
.endsWithBreak(); }
395 void setEndsWithBreak(bool endsWithBreak
) { m_bitfields
.setEndsWithBreak(endsWithBreak
); }
396 bool hasEllipsisBox() const { return m_bitfields
.hasEllipsisBoxOrHyphen(); }
397 bool hasSelectedChildren() const { return m_bitfields
.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
398 void setHasSelectedChildren(bool hasSelectedChildren
) { m_bitfields
.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren
); }
399 void setHasEllipsisBox(bool hasEllipsisBox
) { m_bitfields
.setHasEllipsisBoxOrHyphen(hasEllipsisBox
); }
402 bool hasHyphen() const { return m_bitfields
.hasEllipsisBoxOrHyphen(); }
403 void setHasHyphen(bool hasHyphen
) { m_bitfields
.setHasEllipsisBoxOrHyphen(hasHyphen
); }
404 bool canHaveLeadingExpansion() const { return m_bitfields
.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
405 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion
) { m_bitfields
.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion
); }
406 signed expansion() { return m_bitfields
.expansion(); }
407 void setExpansion(signed expansion
) { m_bitfields
.setExpansion(expansion
); }
409 // For InlineFlowBox and InlineTextBox
410 bool extracted() const { return m_bitfields
.extracted(); }
412 LayoutPoint m_topLeft
;
413 LayoutUnit m_logicalWidth
;
416 InlineBoxBitfields m_bitfields
;
424 inline InlineBox::~InlineBox()
430 inline void InlineBox::setHasBadParent()
432 m_hasBadParent
= true;
436 #define DEFINE_INLINE_BOX_TYPE_CASTS(typeName) \
437 DEFINE_TYPE_CASTS(typeName, InlineBox, box, box->is##typeName(), box.is##typeName())
439 // Allow equality comparisons of InlineBox's by reference or pointer, interchangeably.
440 DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(InlineBox
)
445 // Outside the WebCore namespace for ease of invocation from gdb.
446 void showTree(const blink::InlineBox
*);
447 void showLineTree(const blink::InlineBox
*);
450 #endif // InlineBox_h