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