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).
37 InlineBox(RenderObject* obj)
44 , m_hasBadParent(false)
49 InlineBox(RenderObject* obj, FloatPoint topLeft, float logicalWidth, bool firstLine, bool constructed,
50 bool dirty, bool extracted, bool isHorizontal, InlineBox* next, InlineBox* prev, InlineFlowBox* parent)
56 , m_logicalWidth(logicalWidth)
57 , m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal)
59 , m_hasBadParent(false)
66 virtual void destroy(RenderArena*);
68 virtual void deleteLine(RenderArena*);
69 virtual void extractLine();
70 virtual void attachLine();
72 virtual bool isLineBreak() const { return false; }
74 virtual void adjustPosition(float dx, float dy);
75 void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
78 adjustPosition(deltaLogicalLeft, deltaLogicalTop);
80 adjustPosition(deltaLogicalTop, deltaLogicalLeft);
82 void adjustLineDirectionPosition(float delta)
85 adjustPosition(delta, 0);
87 adjustPosition(0, delta);
89 void adjustBlockDirectionPosition(float delta)
92 adjustPosition(0, delta);
94 adjustPosition(delta, 0);
97 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
98 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
100 // Overloaded new operator.
101 void* operator new(size_t, RenderArena*);
103 // Overridden to prevent the normal delete from being called.
104 void operator delete(void*, size_t);
107 // The normal operator new is disallowed.
108 void* operator new(size_t) throw();
112 void showTreeForThis() const;
113 void showLineTreeForThis() const;
115 virtual void showBox(int = 0) const;
116 virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
117 virtual const char* boxName() const;
120 bool isText() const { return m_bitfields.isText(); }
121 void setIsText(bool isText) { m_bitfields.setIsText(isText); }
123 virtual bool isInlineFlowBox() const { return false; }
124 virtual bool isInlineTextBox() const { return false; }
125 virtual bool isRootInlineBox() const { return false; }
127 virtual bool isSVGInlineTextBox() const { return false; }
128 virtual bool isSVGInlineFlowBox() const { return false; }
129 virtual bool isSVGRootInlineBox() const { return false; }
132 bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
133 void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
134 virtual float virtualLogicalHeight() const
136 ASSERT_NOT_REACHED();
140 bool isHorizontal() const { return m_bitfields.isHorizontal(); }
141 void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
143 virtual FloatRect calculateBoundaries() const
145 ASSERT_NOT_REACHED();
149 bool isConstructed() { return m_bitfields.constructed(); }
150 virtual void setConstructed() { m_bitfields.setConstructed(true); }
152 void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
154 void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
155 bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
159 InlineBox* nextOnLine() const { return m_next; }
160 InlineBox* prevOnLine() const { return m_prev; }
161 void setNextOnLine(InlineBox* next)
163 ASSERT(m_parent || !next);
166 void setPrevOnLine(InlineBox* prev)
168 ASSERT(m_parent || !prev);
171 bool nextOnLineExists() const;
173 virtual bool isLeaf() const { return true; }
175 InlineBox* nextLeafChild() const;
176 InlineBox* prevLeafChild() const;
178 // Helper functions for editing and hit-testing code.
179 // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
180 // Position and inline box, offset pair is moved to RenderedPosition.
181 InlineBox* nextLeafChildIgnoringLineBreak() const;
182 InlineBox* prevLeafChildIgnoringLineBreak() const;
184 RenderObject* renderer() const { return m_renderer; }
186 InlineFlowBox* parent() const
188 ASSERT(!m_hasBadParent);
191 void setParent(InlineFlowBox* par) { m_parent = par; }
193 const RootInlineBox* root() const;
194 RootInlineBox* root();
196 // x() is the left side of the box in the containing block's coordinate system.
197 void setX(float x) { m_topLeft.setX(x); }
198 float x() const { return m_topLeft.x(); }
199 float left() const { return m_topLeft.x(); }
201 // y() is the top side of the box in the containing block's coordinate system.
202 void setY(float y) { m_topLeft.setY(y); }
203 float y() const { return m_topLeft.y(); }
204 float top() const { return m_topLeft.y(); }
206 const FloatPoint& topLeft() const { return m_topLeft; }
208 float width() const { return isHorizontal() ? logicalWidth() : logicalHeight(); }
209 float height() const { return isHorizontal() ? logicalHeight() : logicalWidth(); }
210 FloatSize size() const { return FloatSize(width(), height()); }
211 float right() const { return left() + width(); }
212 float bottom() const { return top() + height(); }
214 // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
215 float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
216 float logicalRight() const { return logicalLeft() + logicalWidth(); }
217 void setLogicalLeft(float left)
224 int pixelSnappedLogicalLeft() const { return logicalLeft(); }
225 int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
226 int pixelSnappedLogicalTop() const { return logicalTop(); }
227 int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
229 // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
230 float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
231 float logicalBottom() const { return logicalTop() + logicalHeight(); }
232 void setLogicalTop(float top)
240 // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
241 void setLogicalWidth(float w) { m_logicalWidth = w; }
242 float logicalWidth() const { return m_logicalWidth; }
244 // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
245 float logicalHeight() const;
247 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()); }
249 virtual LayoutUnit baselinePosition(FontBaseline baselineType) const;
250 virtual LayoutUnit lineHeight() const;
252 virtual int caretMinOffset() const;
253 virtual int caretMaxOffset() const;
255 unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
256 void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
257 TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
258 bool isLeftToRightDirection() const { return direction() == LTR; }
259 int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
260 int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
262 virtual void clearTruncation() { }
264 bool isDirty() const { return m_bitfields.dirty(); }
265 void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
267 virtual void dirtyLineBoxes();
269 virtual RenderObject::SelectionState selectionState();
271 virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
272 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
273 virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
276 void setHasBadParent();
279 int expansion() const { return m_bitfields.expansion(); }
281 bool visibleToHitTesting() const { return renderer()->style()->visibility() == VISIBLE && renderer()->style()->pointerEvents() != PE_NONE; }
283 EVerticalAlign verticalAlign() const { return renderer()->style(m_bitfields.firstLine())->verticalAlign(); }
285 // Use with caution! The type is not checked!
286 RenderBoxModelObject* boxModelObject() const
288 if (!m_renderer->isText())
289 return toRenderBoxModelObject(m_renderer);
293 FloatPoint locationIncludingFlipping();
294 void flipForWritingMode(FloatRect&);
295 FloatPoint flipForWritingMode(const FloatPoint&);
296 void flipForWritingMode(LayoutRect&);
297 LayoutPoint flipForWritingMode(const LayoutPoint&);
299 bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
300 void clearKnownToHaveNoOverflow();
302 bool dirOverride() const { return m_bitfields.dirOverride(); }
303 void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
306 InlineBox* m_next; // The next element on the same line as us.
307 InlineBox* m_prev; // The previous element on the same line as us.
309 InlineFlowBox* m_parent; // The box that contains us.
312 RenderObject* m_renderer;
314 FloatPoint m_topLeft;
315 float m_logicalWidth;
317 #define ADD_BOOLEAN_BITFIELD(name, Name) \
319 unsigned m_##name : 1;\
321 bool name() const { return m_##name; }\
322 void set##Name(bool name) { m_##name = name; }\
324 class InlineBoxBitfields {
326 InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
327 : m_firstLine(firstLine)
328 , m_constructed(constructed)
329 , m_bidiEmbeddingLevel(0)
331 , m_extracted(extracted)
332 , m_hasVirtualLogicalHeight(false)
333 , m_isHorizontal(isHorizontal)
334 , m_endsWithBreak(false)
335 , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
336 , m_knownToHaveNoOverflow(true)
337 , m_hasEllipsisBoxOrHyphen(false)
338 , m_dirOverride(false)
340 , m_determinedIfNextOnLineExists(false)
341 , m_nextOnLineExists(false)
346 // Some of these bits are actually for subclasses and moved here to compact the structures.
348 ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
349 ADD_BOOLEAN_BITFIELD(constructed, Constructed);
352 unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
355 unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
356 void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
358 ADD_BOOLEAN_BITFIELD(dirty, Dirty);
359 ADD_BOOLEAN_BITFIELD(extracted, Extracted);
360 ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
361 ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
363 ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
364 // shared between RootInlineBox and InlineTextBox
365 ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
366 ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
367 ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
369 ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
370 ADD_BOOLEAN_BITFIELD(isText, IsText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
373 mutable unsigned m_determinedIfNextOnLineExists : 1;
376 bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
377 void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
380 mutable unsigned m_nextOnLineExists : 1;
383 bool nextOnLineExists() const { return m_nextOnLineExists; }
384 void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
387 signed m_expansion : 12; // for justified text
390 signed expansion() const { return m_expansion; }
391 void setExpansion(signed expansion) { m_expansion = expansion; }
393 #undef ADD_BOOLEAN_BITFIELD
396 InlineBoxBitfields m_bitfields;
400 bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
401 void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
402 bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
403 bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
404 void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
405 void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
408 bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
409 void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
410 bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
411 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
412 signed expansion() { return m_bitfields.expansion(); }
413 void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }
415 // For InlineFlowBox and InlineTextBox
416 bool extracted() const { return m_bitfields.extracted(); }
425 inline InlineBox::~InlineBox()
431 inline void InlineBox::setHasBadParent()
433 m_hasBadParent = true;
437 } // namespace WebCore
440 // Outside the WebCore namespace for ease of invocation from gdb.
441 void showTree(const WebCore::InlineBox*);
442 void showLineTree(const WebCore::InlineBox*);
445 #endif // InlineBox_h