1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21 #ifndef InlineBox_h
22 #define InlineBox_h
23
24 #include "core/rendering/RenderBoxModelObject.h"
25 #include "platform/text/TextDirection.h"
26
27 namespace WebCore {
28
29 class HitTestRequest;
30 class HitTestResult;
31 class RootInlineBox;
32
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).
35 class InlineBox {
36 public:
InlineBox(RenderObject * obj)37 InlineBox(RenderObject* obj)
38 : m_next(0)
39 , m_prev(0)
40 , m_parent(0)
41 , m_renderer(obj)
42 , m_logicalWidth(0)
43 #ifndef NDEBUG
44 , m_hasBadParent(false)
45 #endif
46 {
47 }
48
InlineBox(RenderObject * obj,FloatPoint topLeft,float logicalWidth,bool firstLine,bool constructed,bool dirty,bool extracted,bool isHorizontal,InlineBox * next,InlineBox * prev,InlineFlowBox * parent)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)
51 : m_next(next)
52 , m_prev(prev)
53 , m_parent(parent)
54 , m_renderer(obj)
55 , m_topLeft(topLeft)
56 , m_logicalWidth(logicalWidth)
57 , m_bitfields(firstLine, constructed, dirty, extracted, isHorizontal)
58 #ifndef NDEBUG
59 , m_hasBadParent(false)
60 #endif
61 {
62 }
63
64 virtual ~InlineBox();
65
destroy()66 virtual void destroy() { delete this; }
67
68 virtual void deleteLine();
69 virtual void extractLine();
70 virtual void attachLine();
71
isLineBreak()72 virtual bool isLineBreak() const { return false; }
73
74 virtual void adjustPosition(float dx, float dy);
adjustLogicalPosition(float deltaLogicalLeft,float deltaLogicalTop)75 void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
76 {
77 if (isHorizontal())
78 adjustPosition(deltaLogicalLeft, deltaLogicalTop);
79 else
80 adjustPosition(deltaLogicalTop, deltaLogicalLeft);
81 }
adjustLineDirectionPosition(float delta)82 void adjustLineDirectionPosition(float delta)
83 {
84 if (isHorizontal())
85 adjustPosition(delta, 0);
86 else
87 adjustPosition(0, delta);
88 }
adjustBlockDirectionPosition(float delta)89 void adjustBlockDirectionPosition(float delta)
90 {
91 if (isHorizontal())
92 adjustPosition(0, delta);
93 else
94 adjustPosition(delta, 0);
95 }
96
97 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
98 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
99
100 // InlineBoxes are allocated out of the rendering partition.
101 void* operator new(size_t);
102 void operator delete(void*);
103
104 #ifndef NDEBUG
105 void showTreeForThis() const;
106 void showLineTreeForThis() const;
107
108 virtual void showBox(int = 0) const;
109 virtual void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0, int = 0) const;
110 virtual const char* boxName() const;
111 #endif
112
isText()113 bool isText() const { return m_bitfields.isText(); }
setIsText(bool isText)114 void setIsText(bool isText) { m_bitfields.setIsText(isText); }
115
isInlineFlowBox()116 virtual bool isInlineFlowBox() const { return false; }
isInlineTextBox()117 virtual bool isInlineTextBox() const { return false; }
isRootInlineBox()118 virtual bool isRootInlineBox() const { return false; }
119
isSVGInlineTextBox()120 virtual bool isSVGInlineTextBox() const { return false; }
isSVGInlineFlowBox()121 virtual bool isSVGInlineFlowBox() const { return false; }
isSVGRootInlineBox()122 virtual bool isSVGRootInlineBox() const { return false; }
123
hasVirtualLogicalHeight()124 bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
setHasVirtualLogicalHeight()125 void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
virtualLogicalHeight()126 virtual float virtualLogicalHeight() const
127 {
128 ASSERT_NOT_REACHED();
129 return 0;
130 }
131
isHorizontal()132 bool isHorizontal() const { return m_bitfields.isHorizontal(); }
setIsHorizontal(bool isHorizontal)133 void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
134
calculateBoundaries()135 virtual FloatRect calculateBoundaries() const
136 {
137 ASSERT_NOT_REACHED();
138 return FloatRect();
139 }
140
isConstructed()141 bool isConstructed() { return m_bitfields.constructed(); }
setConstructed()142 virtual void setConstructed() { m_bitfields.setConstructed(true); }
143
144 void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
145
setFirstLineStyleBit(bool firstLine)146 void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
isFirstLineStyle()147 bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
148
149 void remove();
150
nextOnLine()151 InlineBox* nextOnLine() const { return m_next; }
prevOnLine()152 InlineBox* prevOnLine() const { return m_prev; }
setNextOnLine(InlineBox * next)153 void setNextOnLine(InlineBox* next)
154 {
155 ASSERT(m_parent || !next);
156 m_next = next;
157 }
setPrevOnLine(InlineBox * prev)158 void setPrevOnLine(InlineBox* prev)
159 {
160 ASSERT(m_parent || !prev);
161 m_prev = prev;
162 }
163 bool nextOnLineExists() const;
164
isLeaf()165 virtual bool isLeaf() const { return true; }
166
167 InlineBox* nextLeafChild() const;
168 InlineBox* prevLeafChild() const;
169
170 // Helper functions for editing and hit-testing code.
171 // FIXME: These two functions should be moved to RenderedPosition once the code to convert between
172 // Position and inline box, offset pair is moved to RenderedPosition.
173 InlineBox* nextLeafChildIgnoringLineBreak() const;
174 InlineBox* prevLeafChildIgnoringLineBreak() const;
175
renderer()176 RenderObject* renderer() const { return m_renderer; }
177
parent()178 InlineFlowBox* parent() const
179 {
180 ASSERT(!m_hasBadParent);
181 return m_parent;
182 }
setParent(InlineFlowBox * par)183 void setParent(InlineFlowBox* par) { m_parent = par; }
184
185 const RootInlineBox* root() const;
186 RootInlineBox* root();
187
188 // x() is the left side of the box in the containing block's coordinate system.
setX(float x)189 void setX(float x) { m_topLeft.setX(x); }
x()190 float x() const { return m_topLeft.x(); }
left()191 float left() const { return m_topLeft.x(); }
192
193 // y() is the top side of the box in the containing block's coordinate system.
setY(float y)194 void setY(float y) { m_topLeft.setY(y); }
y()195 float y() const { return m_topLeft.y(); }
top()196 float top() const { return m_topLeft.y(); }
197
topLeft()198 const FloatPoint& topLeft() const { return m_topLeft; }
199
width()200 float width() const { return isHorizontal() ? logicalWidth() : hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight(); }
height()201 float height() const { return isHorizontal() ? hasVirtualLogicalHeight() ? virtualLogicalHeight() : logicalHeight() : logicalWidth(); }
size()202 FloatSize size() const { return FloatSize(width(), height()); }
right()203 float right() const { return left() + width(); }
bottom()204 float bottom() const { return top() + height(); }
205
206 // The logicalLeft position is the left edge of the line box in a horizontal line and the top edge in a vertical line.
logicalLeft()207 float logicalLeft() const { return isHorizontal() ? m_topLeft.x() : m_topLeft.y(); }
logicalRight()208 float logicalRight() const { return logicalLeft() + logicalWidth(); }
setLogicalLeft(float left)209 void setLogicalLeft(float left)
210 {
211 if (isHorizontal())
212 setX(left);
213 else
214 setY(left);
215 }
pixelSnappedLogicalLeft()216 int pixelSnappedLogicalLeft() const { return logicalLeft(); }
pixelSnappedLogicalRight()217 int pixelSnappedLogicalRight() const { return ceilf(logicalRight()); }
pixelSnappedLogicalTop()218 int pixelSnappedLogicalTop() const { return logicalTop(); }
pixelSnappedLogicalBottom()219 int pixelSnappedLogicalBottom() const { return ceilf(logicalBottom()); }
220
221 // The logicalTop[ position is the top edge of the line box in a horizontal line and the left edge in a vertical line.
logicalTop()222 float logicalTop() const { return isHorizontal() ? m_topLeft.y() : m_topLeft.x(); }
logicalBottom()223 float logicalBottom() const { return logicalTop() + logicalHeight(); }
setLogicalTop(float top)224 void setLogicalTop(float top)
225 {
226 if (isHorizontal())
227 setY(top);
228 else
229 setX(top);
230 }
231
232 // The logical width is our extent in the line's overall inline direction, i.e., width for horizontal text and height for vertical text.
setLogicalWidth(float w)233 void setLogicalWidth(float w) { m_logicalWidth = w; }
logicalWidth()234 float logicalWidth() const { return m_logicalWidth; }
235
236 // The logical height is our extent in the block flow direction, i.e., height for horizontal text and width for vertical text.
237 float logicalHeight() const;
238
logicalFrameRect()239 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()); }
240
241 virtual int baselinePosition(FontBaseline baselineType) const;
242 virtual LayoutUnit lineHeight() const;
243
244 virtual int caretMinOffset() const;
245 virtual int caretMaxOffset() const;
246
bidiLevel()247 unsigned char bidiLevel() const { return m_bitfields.bidiEmbeddingLevel(); }
setBidiLevel(unsigned char level)248 void setBidiLevel(unsigned char level) { m_bitfields.setBidiEmbeddingLevel(level); }
direction()249 TextDirection direction() const { return bidiLevel() % 2 ? RTL : LTR; }
isLeftToRightDirection()250 bool isLeftToRightDirection() const { return direction() == LTR; }
caretLeftmostOffset()251 int caretLeftmostOffset() const { return isLeftToRightDirection() ? caretMinOffset() : caretMaxOffset(); }
caretRightmostOffset()252 int caretRightmostOffset() const { return isLeftToRightDirection() ? caretMaxOffset() : caretMinOffset(); }
253
clearTruncation()254 virtual void clearTruncation() { }
255
isDirty()256 bool isDirty() const { return m_bitfields.dirty(); }
257 virtual void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
258
259 virtual void dirtyLineBoxes();
260
261 virtual RenderObject::SelectionState selectionState();
262
263 virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const;
264 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
265 virtual float placeEllipsisBox(bool ltr, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool&);
266
267 #ifndef NDEBUG
268 void setHasBadParent();
269 #endif
270
expansion()271 int expansion() const { return m_bitfields.expansion(); }
272
visibleToHitTestRequest(const HitTestRequest & request)273 bool visibleToHitTestRequest(const HitTestRequest& request) const { return renderer()->visibleToHitTestRequest(request); }
274
verticalAlign()275 EVerticalAlign verticalAlign() const { return renderer()->style(m_bitfields.firstLine())->verticalAlign(); }
276
277 // Use with caution! The type is not checked!
boxModelObject()278 RenderBoxModelObject* boxModelObject() const
279 {
280 if (!m_renderer->isText())
281 return toRenderBoxModelObject(m_renderer);
282 return 0;
283 }
284
285 FloatPoint locationIncludingFlipping();
286 void flipForWritingMode(FloatRect&);
287 FloatPoint flipForWritingMode(const FloatPoint&);
288 void flipForWritingMode(LayoutRect&);
289 LayoutPoint flipForWritingMode(const LayoutPoint&);
290
knownToHaveNoOverflow()291 bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
292 void clearKnownToHaveNoOverflow();
293
dirOverride()294 bool dirOverride() const { return m_bitfields.dirOverride(); }
setDirOverride(bool dirOverride)295 void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
296
297 #define ADD_BOOLEAN_BITFIELD(name, Name) \
298 private:\
299 unsigned m_##name : 1;\
300 public:\
301 bool name() const { return m_##name; }\
302 void set##Name(bool name) { m_##name = name; }\
303
304 class InlineBoxBitfields {
305 public:
306 InlineBoxBitfields(bool firstLine = false, bool constructed = false, bool dirty = false, bool extracted = false, bool isHorizontal = true)
m_firstLine(firstLine)307 : m_firstLine(firstLine)
308 , m_constructed(constructed)
309 , m_bidiEmbeddingLevel(0)
310 , m_dirty(dirty)
311 , m_extracted(extracted)
312 , m_hasVirtualLogicalHeight(false)
313 , m_isHorizontal(isHorizontal)
314 , m_endsWithBreak(false)
315 , m_hasSelectedChildrenOrCanHaveLeadingExpansion(false)
316 , m_knownToHaveNoOverflow(true)
317 , m_hasEllipsisBoxOrHyphen(false)
318 , m_dirOverride(false)
319 , m_isText(false)
320 , m_determinedIfNextOnLineExists(false)
321 , m_nextOnLineExists(false)
322 , m_expansion(0)
323 {
324 }
325
326 // Some of these bits are actually for subclasses and moved here to compact the structures.
327 // for this class
328 ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
329 ADD_BOOLEAN_BITFIELD(constructed, Constructed);
330
331 private:
332 unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
333
334 public:
bidiEmbeddingLevel()335 unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel)336 void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
337
338 ADD_BOOLEAN_BITFIELD(dirty, Dirty);
339 ADD_BOOLEAN_BITFIELD(extracted, Extracted);
340 ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
341 ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
342 // for RootInlineBox
343 ADD_BOOLEAN_BITFIELD(endsWithBreak, EndsWithBreak); // Whether the line ends with a <br>.
344 // shared between RootInlineBox and InlineTextBox
345 ADD_BOOLEAN_BITFIELD(hasSelectedChildrenOrCanHaveLeadingExpansion, HasSelectedChildrenOrCanHaveLeadingExpansion);
346 ADD_BOOLEAN_BITFIELD(knownToHaveNoOverflow, KnownToHaveNoOverflow);
347 ADD_BOOLEAN_BITFIELD(hasEllipsisBoxOrHyphen, HasEllipsisBoxOrHyphen);
348 // for InlineTextBox
349 ADD_BOOLEAN_BITFIELD(dirOverride, DirOverride);
350 ADD_BOOLEAN_BITFIELD(isText, IsText); // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
351
352 private:
353 mutable unsigned m_determinedIfNextOnLineExists : 1;
354
355 public:
determinedIfNextOnLineExists()356 bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists)357 void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
358
359 private:
360 mutable unsigned m_nextOnLineExists : 1;
361
362 public:
nextOnLineExists()363 bool nextOnLineExists() const { return m_nextOnLineExists; }
setNextOnLineExists(bool nextOnLineExists)364 void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
365
366 private:
367 signed m_expansion : 12; // for justified text
368
369 public:
expansion()370 signed expansion() const { return m_expansion; }
setExpansion(signed expansion)371 void setExpansion(signed expansion) { m_expansion = expansion; }
372 };
373 #undef ADD_BOOLEAN_BITFIELD
374
375 private:
376 InlineBox* m_next; // The next element on the same line as us.
377 InlineBox* m_prev; // The previous element on the same line as us.
378
379 InlineFlowBox* m_parent; // The box that contains us.
380
381 protected:
382 // For RootInlineBox
endsWithBreak()383 bool endsWithBreak() const { return m_bitfields.endsWithBreak(); }
setEndsWithBreak(bool endsWithBreak)384 void setEndsWithBreak(bool endsWithBreak) { m_bitfields.setEndsWithBreak(endsWithBreak); }
hasEllipsisBox()385 bool hasEllipsisBox() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
hasSelectedChildren()386 bool hasSelectedChildren() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
setHasSelectedChildren(bool hasSelectedChildren)387 void setHasSelectedChildren(bool hasSelectedChildren) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(hasSelectedChildren); }
setHasEllipsisBox(bool hasEllipsisBox)388 void setHasEllipsisBox(bool hasEllipsisBox) { m_bitfields.setHasEllipsisBoxOrHyphen(hasEllipsisBox); }
389
390 // For InlineTextBox
hasHyphen()391 bool hasHyphen() const { return m_bitfields.hasEllipsisBoxOrHyphen(); }
setHasHyphen(bool hasHyphen)392 void setHasHyphen(bool hasHyphen) { m_bitfields.setHasEllipsisBoxOrHyphen(hasHyphen); }
canHaveLeadingExpansion()393 bool canHaveLeadingExpansion() const { return m_bitfields.hasSelectedChildrenOrCanHaveLeadingExpansion(); }
setCanHaveLeadingExpansion(bool canHaveLeadingExpansion)394 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_bitfields.setHasSelectedChildrenOrCanHaveLeadingExpansion(canHaveLeadingExpansion); }
expansion()395 signed expansion() { return m_bitfields.expansion(); }
setExpansion(signed expansion)396 void setExpansion(signed expansion) { m_bitfields.setExpansion(expansion); }
397
398 // For InlineFlowBox and InlineTextBox
extracted()399 bool extracted() const { return m_bitfields.extracted(); }
400
401 RenderObject* m_renderer;
402
403 FloatPoint m_topLeft;
404 float m_logicalWidth;
405
406 private:
407 InlineBoxBitfields m_bitfields;
408
409 #ifndef NDEBUG
410 bool m_hasBadParent;
411 #endif
412 };
413
414 #ifdef NDEBUG
~InlineBox()415 inline InlineBox::~InlineBox()
416 {
417 }
418 #endif
419
420 #ifndef NDEBUG
setHasBadParent()421 inline void InlineBox::setHasBadParent()
422 {
423 m_hasBadParent = true;
424 }
425 #endif
426
427 #define DEFINE_INLINE_BOX_TYPE_CASTS(typeName) \
428 DEFINE_TYPE_CASTS(typeName, InlineBox, box, box->is##typeName(), box.is##typeName())
429
430 } // namespace WebCore
431
432 #ifndef NDEBUG
433 // Outside the WebCore namespace for ease of invocation from gdb.
434 void showTree(const WebCore::InlineBox*);
435 void showLineTree(const WebCore::InlineBox*);
436 #endif
437
438 #endif // InlineBox_h
439