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/rendering/RenderBoxModelObject.h"
25 #include "platform/text/TextDirection.h"
33 enum MarkLineBoxes { MarkLineBoxesDirty, DontMarkLineBoxes };
35 // InlineBox represents a rectangle that occurs on a line. It corresponds to
36 // some RenderObject (i.e., it represents a portion of that RenderObject).
38 WTF_MAKE_NONCOPYABLE(InlineBox);
40 InlineBox(RenderObject& obj)
47 , m_hasBadParent(false)
52 InlineBox(RenderObject& obj, FloatPoint topLeft, float logicalWidth, bool firstLine, bool constructed,
53 bool dirty, bool extracted, bool isHorizontal, InlineBox* next, InlineBox* prev, InlineFlowBox* parent)
59 , m_logicalWidth(logicalWidth)
60 , m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal)
62 , m_hasBadParent(false)
69 virtual void destroy() { delete this; }
71 virtual void deleteLine();
72 virtual void extractLine();
73 virtual void attachLine();
75 virtual bool isLineBreak() const { return false; }
77 virtual void adjustPosition(float dx, float dy);
78 void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
81 adjustPosition(deltaLogicalLeft, deltaLogicalTop);
83 adjustPosition(deltaLogicalTop, deltaLogicalLeft);
85 void adjustLineDirectionPosition(float delta)
88 adjustPosition(delta, 0);
90 adjustPosition(0, delta);
92 void adjustBlockDirectionPosition(float delta)
95 adjustPosition(0, delta);
97 adjustPosition(delta, 0);
100 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
101 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
103 // InlineBoxes are allocated out of the rendering partition.
104 void* operator new(size_t);
105 void operator delete(void*);
108 void showTreeForThis() const;
109 void showLineTreeForThis() const;
111 virtual void showBox(int = 0) const;
112 virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
113 virtual const char* boxName() const;
116 bool isText() const { return m_bitfields.isText(); }
117 void setIsText(bool isText) { m_bitfields.setIsText(isText); }
119 virtual bool isInlineFlowBox() const { return false; }
120 virtual bool isInlineTextBox() const { return false; }
121 virtual bool isRootInlineBox() const { return false; }
123 virtual bool isSVGInlineTextBox() const { return false; }
124 virtual bool isSVGInlineFlowBox() const { return false; }
125 virtual bool isSVGRootInlineBox() const { return false; }
127 bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
128 void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
129 virtual float virtualLogicalHeight() const
131 ASSERT_NOT_REACHED();
135 bool isHorizontal() const { return m_bitfields.isHorizontal(); }
136 void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
138 virtual FloatRect calculateBoundaries() const
140 ASSERT_NOT_REACHED();
144 bool isConstructed() { return m_bitfields.constructed(); }
145 virtual void setConstructed() { m_bitfields.setConstructed(true); }
147 void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
149 void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
150 bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
152 void remove(MarkLineBoxes = MarkLineBoxesDirty);
154 InlineBox* nextOnLine() const { return m_next; }
155 InlineBox* prevOnLine() const { return m_prev; }
156 void setNextOnLine(InlineBox* next)
158 ASSERT(m_parent || !next);
161 void setPrevOnLine(InlineBox* prev)
163 ASSERT(m_parent || !prev);
166 bool nextOnLineExists() const;
168 virtual bool isLeaf() const { return true; }
170 InlineBox* nextLeafChild() const;
171 InlineBox* prevLeafChild() const;
173 // Helper functions for editing and hit-testing code.
174 // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
175 // Position and inline box, offset pair is moved to RenderedPosition.
176 InlineBox* nextLeafChildIgnoringLineBreak() const;
177 InlineBox* prevLeafChildIgnoringLineBreak() const;
179 RenderObject& renderer() const { return m_renderer; }
181 InlineFlowBox* parent() const
183 ASSERT(!m_hasBadParent);
186 void setParent(InlineFlowBox* par) { m_parent = par; }
188 const RootInlineBox& root() const;
189 RootInlineBox& root();
191 // x() is the left side of the box in the containing block's coordinate system.
192 void setX(float x) { m_topLeft.setX(x); }
193 float x() const { return m_topLeft.x(); }
194 float left() const { return m_topLeft.x(); }
196 // y() is the top side of the box in the containing block's coordinate system.
197 void setY(float y) { m_topLeft.setY(y); }
198 float y() const { return m_topLeft.y(); }
199 float top() const { return m_topLeft.y(); }
201 const FloatPoint& topLeft() const { return m_topLeft; }
203 float width() const { return isHorizontal() ? logicalWidth() : hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight(); }
204 float height() const { return isHorizontal() ? hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight() : logicalWidth(); }
205 FloatSize size() const { return FloatSize(width(), height()); }
206 float right() const { return left() + width(); }
207 float bottom() const { return top() + height(); }
209 // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
210 float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
211 float logicalRight() const { return logicalLeft() + logicalWidth(); }
212 void setLogicalLeft(float left)
219 int pixelSnappedLogicalLeft() const { return logicalLeft(); }
220 int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
221 int pixelSnappedLogicalTop() const { return logicalTop(); }
222 int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
224 // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
225 float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
226 float logicalBottom() const { return logicalTop() + logicalHeight(); }
227 void setLogicalTop(float top)
235 // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
236 void setLogicalWidth(float w) { m_logicalWidth = w; }
237 float logicalWidth() const { return m_logicalWidth; }
239 // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
240 float logicalHeight() const;
242 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()); }
244 virtual int baselinePosition(FontBaseline baselineType) const;
245 virtual LayoutUnit lineHeight() const;
247 virtual int caretMinOffset() const;
248 virtual int caretMaxOffset() const;
250 unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
251 void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
252 TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
253 bool isLeftToRightDirection() const { return direction() == LTR; }
254 int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
255 int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
257 virtual void clearTruncation() { }
259 bool isDirty() const { return m_bitfields.dirty(); }
260 virtual void markDirty() { m_bitfields.setDirty(true); }
262 virtual void dirtyLineBoxes();
264 virtual RenderObject::SelectionState selectionState();
266 virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
267 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
268 virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
271 void setHasBadParent();
274 int expansion() const { return m_bitfields.expansion(); }
276 bool visibleForTouchAction() const { return false; }
277 bool visibleToHitTestRequest(const HitTestRequest& request) const
279 if (request.touchAction() && !visibleForTouchAction())
281 return renderer().visibleToHitTestRequest(request);
284 EVerticalAlign verticalAlign() const { return renderer().style(m_bitfields.firstLine())->verticalAlign(); }
286 // Use with caution! The type is not checked!
287 RenderBoxModelObject* boxModelObject() const
289 if (!renderer().isText())
290 return toRenderBoxModelObject(&renderer());
294 FloatPoint locationIncludingFlipping();
295 void flipForWritingMode(FloatRect&);
296 FloatPoint flipForWritingMode(const FloatPoint&);
297 void flipForWritingMode(LayoutRect&);
298 LayoutPoint flipForWritingMode(const LayoutPoint&);
300 bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
301 void clearKnownToHaveNoOverflow();
303 bool dirOverride() const { return m_bitfields.dirOverride(); }
304 void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
306 #define ADD_BOOLEAN_BITFIELD(name, Name) \
308 unsigned m_##name : 1;\
310 bool name() const { return m_##name; }\
311 void set##Name(bool name) { m_##name = name; }\
313 class InlineBoxBitfields {
315 InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
316 : m_firstLine(firstLine)
317 , m_constructed(constructed)
318 , m_bidiEmbeddingLevel(0)
320 , m_extracted(extracted)
321 , m_hasVirtualLogicalHeight(false)
322 , m_isHorizontal(isHorizontal)
323 , m_endsWithBreak(false)
324 , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
325 , m_knownToHaveNoOverflow(true)
326 , m_hasEllipsisBoxOrHyphen(false)
327 , m_dirOverride(false)
329 , m_determinedIfNextOnLineExists(false)
330 , m_nextOnLineExists(false)
335 // Some of these bits are actually for subclasses and moved here to compact the structures.
337 ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
338 ADD_BOOLEAN_BITFIELD(constructed, Constructed);
341 unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
344 unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
345 void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
347 ADD_BOOLEAN_BITFIELD(dirty, Dirty);
348 ADD_BOOLEAN_BITFIELD(extracted, Extracted);
349 ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
350 ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
352 ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
353 // shared between RootInlineBox and InlineTextBox
354 ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
355 ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
356 ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
358 ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
359 ADD_BOOLEAN_BITFIELD(isText, IsText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
362 mutable unsigned m_determinedIfNextOnLineExists : 1;
365 bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
366 void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
369 mutable unsigned m_nextOnLineExists : 1;
372 bool nextOnLineExists() const { return m_nextOnLineExists; }
373 void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
376 signed m_expansion : 12; // for justified text
379 signed expansion() const { return m_expansion; }
380 void setExpansion(signed expansion) { m_expansion = expansion; }
382 #undef ADD_BOOLEAN_BITFIELD
385 InlineBox* m_next; // The next element on the same line as us.
386 InlineBox* m_prev; // The previous element on the same line as us.
388 InlineFlowBox* m_parent; // The box that contains us.
389 RenderObject& m_renderer;
393 bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
394 void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
395 bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
396 bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
397 void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
398 void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
401 bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
402 void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
403 bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
404 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
405 signed expansion() { return m_bitfields.expansion(); }
406 void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }
408 // For InlineFlowBox and InlineTextBox
409 bool extracted() const { return m_bitfields.extracted(); }
411 FloatPoint m_topLeft;
412 float m_logicalWidth;
415 InlineBoxBitfields m_bitfields;
423 inline InlineBox::~InlineBox()
429 inline void InlineBox::setHasBadParent()
431 m_hasBadParent = true;
435 #define DEFINE_INLINE_BOX_TYPE_CASTS(typeName) \
436 DEFINE_TYPE_CASTS(typeName, InlineBox, box, box->is##typeName(), box.is##typeName())
438 // Allow equality comparisons of InlineBox's by reference or pointer, interchangeably.
439 inline bool operator==(const InlineBox& a, const InlineBox& b) { return &a == &b; }
440 inline bool operator==(const InlineBox& a, const InlineBox* b) { return &a == b; }
441 inline bool operator==(const InlineBox* a, const InlineBox& b) { return a == &b; }
442 inline bool operator!=(const InlineBox& a, const InlineBox& b) { return !(a == b); }
443 inline bool operator!=(const InlineBox& a, const InlineBox* b) { return !(a == b); }
444 inline bool operator!=(const InlineBox* a, const InlineBox& b) { return !(a == b); }
446 } // namespace WebCore
449 // Outside the WebCore namespace for ease of invocation from gdb.
450 void showTree(const WebCore::InlineBox*);
451 void showLineTree(const WebCore::InlineBox*);
454 #endif // InlineBox_h