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 "RenderBoxModelObject.h"
25 #include "TextDirection.h"
33 // InlineBox represents a rectangle that occurs on a line. It corresponds to
34 // some RenderObject (i.e., it represents a portion of that RenderObject).
36 WTF_MAKE_FAST_ALLOCATED;
40 void assertNotDeleted() const;
42 virtual void deleteLine() = 0;
43 virtual void extractLine() = 0;
44 virtual void attachLine() = 0;
46 virtual bool isLineBreak() const { return renderer().isLineBreak(); }
48 virtual void adjustPosition(float dx, float dy);
49 void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
52 adjustPosition(deltaLogicalLeft, deltaLogicalTop);
54 adjustPosition(deltaLogicalTop, deltaLogicalLeft);
56 void adjustLineDirectionPosition(float delta)
59 adjustPosition(delta, 0);
61 adjustPosition(0, delta);
63 void adjustBlockDirectionPosition(float delta)
66 adjustPosition(0, delta);
68 adjustPosition(delta, 0);
71 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom) = 0;
72 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) = 0;
75 void showTreeForThis() const;
76 void showLineTreeForThis() const;
78 virtual void showBox(int = 0) const;
79 virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
80 virtual const char* boxName() const;
83 bool behavesLikeText() const { return m_bitfields.behavesLikeText(); }
84 void setBehavesLikeText(bool behavesLikeText) { m_bitfields.setBehavesLikeText(behavesLikeText); }
86 virtual bool isInlineElementBox() const { return false; }
87 virtual bool isInlineFlowBox() const { return false; }
88 virtual bool isInlineTextBox() const { return false; }
89 virtual bool isRootInlineBox() const { return false; }
90 virtual bool isSVGInlineTextBox() const { return false; }
91 virtual bool isSVGInlineFlowBox() const { return false; }
92 virtual bool isSVGRootInlineBox() const { return false; }
94 bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
95 void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
96 virtual float virtualLogicalHeight() const
102 bool isHorizontal() const { return m_bitfields.isHorizontal(); }
103 void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
105 virtual FloatRect calculateBoundaries() const
107 ASSERT_NOT_REACHED();
111 bool isConstructed() { return m_bitfields.constructed(); }
112 virtual void setConstructed() { m_bitfields.setConstructed(true); }
114 void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
116 void setIsFirstLine(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
117 bool isFirstLine() const { return m_bitfields.firstLine(); }
119 void removeFromParent();
121 InlineBox* nextOnLine() const { return m_next; }
122 InlineBox* prevOnLine() const { return m_prev; }
123 void setNextOnLine(InlineBox* next)
125 ASSERT(m_parent || !next);
128 void setPrevOnLine(InlineBox* prev)
130 ASSERT(m_parent || !prev);
133 bool nextOnLineExists() const;
134 bool previousOnLineExists() const;
136 virtual bool isLeaf() const { return true; }
138 InlineBox* nextLeafChild() const;
139 InlineBox* prevLeafChild() const;
141 // Helper functions for editing and hit-testing code.
142 // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
143 // Position and inline box, offset pair is moved to RenderedPosition.
144 InlineBox* nextLeafChildIgnoringLineBreak() const;
145 InlineBox* prevLeafChildIgnoringLineBreak() const;
147 // FIXME: Hide this once all callers are using tighter types.
148 RenderObject& renderer() const { return m_renderer; }
150 InlineFlowBox* parent() const
153 ASSERT_WITH_SECURITY_IMPLICATION(!m_hasBadParent);
156 void setParent(InlineFlowBox* par) { m_parent = par; }
158 const RootInlineBox& root() const;
159 RootInlineBox& root();
161 // x() is the left side of the box in the containing block's coordinate system.
162 void setX(float x) { m_topLeft.setX(x); }
163 float x() const { return m_topLeft.x(); }
164 float left() const { return m_topLeft.x(); }
166 // y() is the top side of the box in the containing block's coordinate system.
167 void setY(float y) { m_topLeft.setY(y); }
168 float y() const { return m_topLeft.y(); }
169 float top() const { return m_topLeft.y(); }
171 const FloatPoint& topLeft() const { return m_topLeft; }
173 float width() const { return isHorizontal() ? logicalWidth() : logicalHeight(); }
174 float height() const { return isHorizontal() ? logicalHeight() : logicalWidth(); }
175 FloatSize size() const { return FloatSize(width(), height()); }
176 float right() const { return left() + width(); }
177 float bottom() const { return top() + height(); }
179 // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
180 float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
181 float logicalRight() const { return logicalLeft() + logicalWidth(); }
182 void setLogicalLeft(float left)
189 int pixelSnappedLogicalLeft() const { return logicalLeft(); }
190 int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
191 int pixelSnappedLogicalTop() const { return logicalTop(); }
192 int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
194 // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
195 float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
196 float logicalBottom() const { return logicalTop() + logicalHeight(); }
197 void setLogicalTop(float top)
205 // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
206 void setLogicalWidth(float w) { m_logicalWidth = w; }
207 float logicalWidth() const { return m_logicalWidth; }
209 // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
210 float logicalHeight() const;
212 FloatRect logicalFrameRect() const { return isHorizontal() ? FloatRect(m_topLeft.x(), m_topLeft.y(), m_logicalWidth, logicalHeight()) : FloatRect(m_topLeft.y(), m_topLeft.x(), m_logicalWidth, logicalHeight()); }
214 virtual int baselinePosition(FontBaseline baselineType) const;
215 virtual LayoutUnit lineHeight() const;
217 virtual int caretMinOffset() const;
218 virtual int caretMaxOffset() const;
220 unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
221 void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
222 TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
223 bool isLeftToRightDirection() const { return direction() == LTR; }
224 int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
225 int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
227 virtual void clearTruncation() { }
229 bool isDirty() const { return m_bitfields.dirty(); }
230 virtual void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
232 virtual void dirtyLineBoxes();
234 virtual RenderObject::SelectionState selectionState();
236 virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
237 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
238 virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
240 #if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
241 void setHasBadParent();
242 void invalidateParentChildList();
245 int expansion() const { return m_bitfields.expansion(); }
247 bool visibleToHitTesting() const { return renderer().style().visibility() == VISIBLE && renderer().style().pointerEvents() != PE_NONE; }
249 const RenderStyle& lineStyle() const { return m_bitfields.firstLine() ? renderer().firstLineStyle() : renderer().style(); }
251 EVerticalAlign verticalAlign() const { return lineStyle().verticalAlign(); }
253 // Use with caution! The type is not checked!
254 RenderBoxModelObject* boxModelObject() const
256 if (!m_renderer.isText())
257 return &toRenderBoxModelObject(m_renderer);
261 FloatPoint locationIncludingFlipping();
262 void flipForWritingMode(FloatRect&);
263 FloatPoint flipForWritingMode(const FloatPoint&);
264 void flipForWritingMode(LayoutRect&);
265 LayoutPoint flipForWritingMode(const LayoutPoint&);
267 bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
268 void clearKnownToHaveNoOverflow();
270 bool dirOverride() const { return m_bitfields.dirOverride(); }
271 void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
274 InlineBox* m_next; // The next element on the same line as us.
275 InlineBox* m_prev; // The previous element on the same line as us.
277 InlineFlowBox* m_parent; // The box that contains us.
279 RenderObject& m_renderer;
282 FloatPoint m_topLeft;
283 float m_logicalWidth;
285 #define ADD_BOOLEAN_BITFIELD(name, Name) \
287 unsigned m_##name : 1;\
289 bool name() const { return m_##name; }\
290 void set##Name(bool name) { m_##name = name; }\
292 class InlineBoxBitfields {
294 explicit InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
295 : m_firstLine(firstLine)
296 , m_constructed(constructed)
297 , m_bidiEmbeddingLevel(0)
299 , m_extracted(extracted)
300 , m_hasVirtualLogicalHeight(false)
301 , m_isHorizontal(isHorizontal)
302 , m_endsWithBreak(false)
303 , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
304 , m_knownToHaveNoOverflow(true)
305 , m_hasEllipsisBoxOrHyphen(false)
306 , m_dirOverride(false)
307 , m_behavesLikeText(false)
308 , m_determinedIfNextOnLineExists(false)
309 , m_nextOnLineExists(false)
314 // Some of these bits are actually for subclasses and moved here to compact the structures.
316 ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
317 ADD_BOOLEAN_BITFIELD(constructed, Constructed);
320 unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
323 unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
324 void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
326 ADD_BOOLEAN_BITFIELD(dirty, Dirty);
327 ADD_BOOLEAN_BITFIELD(extracted, Extracted);
328 ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
329 ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
331 ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
332 // shared between RootInlineBox and InlineTextBox
333 ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
334 ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
335 ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
337 ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
338 ADD_BOOLEAN_BITFIELD(behavesLikeText, BehavesLikeText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes, br.
341 mutable unsigned m_determinedIfNextOnLineExists : 1;
344 bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
345 void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
348 mutable unsigned m_nextOnLineExists : 1;
351 bool nextOnLineExists() const { return m_nextOnLineExists; }
352 void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
355 signed m_expansion : 12; // for justified text
358 signed expansion() const { return m_expansion; }
359 void setExpansion(signed expansion) { m_expansion = expansion; }
361 #undef ADD_BOOLEAN_BITFIELD
364 InlineBoxBitfields m_bitfields;
367 explicit InlineBox(RenderObject& renderer)
371 , m_renderer(renderer)
373 #if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
374 , m_deletionSentinel(deletionSentinelNotDeletedValue)
375 , m_hasBadParent(false)
380 InlineBox(RenderObject& renderer, FloatPoint topLeft, float logicalWidth, bool firstLine, bool constructed,
381 bool dirty, bool extracted, bool isHorizontal, InlineBox* next, InlineBox* prev, InlineFlowBox* parent)
385 , m_renderer(renderer)
387 , m_logicalWidth(logicalWidth)
388 , m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal)
389 #if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
390 , m_deletionSentinel(deletionSentinelNotDeletedValue)
391 , m_hasBadParent(false)
397 bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
398 void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
399 bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
400 bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
401 void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
402 void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
405 bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
406 void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
407 bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
408 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
409 int expansion() { return m_bitfields.expansion(); }
410 void setExpansion(int expansion) { m_bitfields.setExpansion(expansion); }
412 // For InlineFlowBox and InlineTextBox
413 bool extracted() const { return m_bitfields.extracted(); }
415 #if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
417 static const unsigned deletionSentinelNotDeletedValue = 0xF0F0F0F0U;
418 static const unsigned deletionSentinelDeletedValue = 0xF0DEADF0U;
419 unsigned m_deletionSentinel;
424 #define INLINE_BOX_OBJECT_TYPE_CASTS(ToValueTypeName, predicate) \
425 TYPE_CASTS_BASE(ToValueTypeName, InlineBox, object, object->predicate, object.predicate)
427 #if ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
429 inline InlineBox::~InlineBox()
433 inline void InlineBox::assertNotDeleted() const
439 } // namespace WebCore
442 // Outside the WebCore namespace for ease of invocation from gdb.
443 void showTree(const WebCore::InlineBox*);
444 void showLineTree(const WebCore::InlineBox*);
447 #endif // InlineBox_h