1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007 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 "RenderBoxModelObject.h"
25 #include "TextDirection.h"
26
27 namespace WebCore {
28
29 class HitTestRequest;
30 class HitTestResult;
31 class RootInlineBox;
32
33
34 // InlineBox represents a rectangle that occurs on a line. It corresponds to
35 // some RenderObject (i.e., it represents a portion of that RenderObject).
36 class InlineBox {
37 public:
InlineBox(RenderObject * obj)38 InlineBox(RenderObject* obj)
39 : m_next(0)
40 , m_prev(0)
41 , m_parent(0)
42 , m_renderer(obj)
43 , m_x(0)
44 , m_y(0)
45 , m_width(0)
46 , m_firstLine(false)
47 , m_constructed(false)
48 , m_bidiEmbeddingLevel(0)
49 , m_dirty(false)
50 , m_extracted(false)
51 #if ENABLE(SVG)
52 , m_hasVirtualHeight(false)
53 #endif
54 , m_endsWithBreak(false)
55 , m_hasSelectedChildren(false)
56 , m_hasEllipsisBox(false)
57 , m_dirOverride(false)
58 , m_isText(false)
59 , m_determinedIfNextOnLineExists(false)
60 , m_determinedIfPrevOnLineExists(false)
61 , m_nextOnLineExists(false)
62 , m_prevOnLineExists(false)
63 , m_toAdd(0)
64 #ifndef NDEBUG
65 , m_hasBadParent(false)
66 #endif
67 {
68 }
69
InlineBox(RenderObject * obj,int x,int y,int width,bool firstLine,bool constructed,bool dirty,bool extracted,InlineBox * next,InlineBox * prev,InlineFlowBox * parent)70 InlineBox(RenderObject* obj, int x, int y, int width, bool firstLine, bool constructed,
71 bool dirty, bool extracted, InlineBox* next, InlineBox* prev, InlineFlowBox* parent)
72 : m_next(next)
73 , m_prev(prev)
74 , m_parent(parent)
75 , m_renderer(obj)
76 , m_x(x)
77 , m_y(y)
78 , m_width(width)
79 , m_firstLine(firstLine)
80 , m_constructed(constructed)
81 , m_bidiEmbeddingLevel(0)
82 , m_dirty(dirty)
83 , m_extracted(extracted)
84 #if ENABLE(SVG)
85 , m_hasVirtualHeight(false)
86 #endif
87 , m_endsWithBreak(false)
88 , m_hasSelectedChildren(false)
89 , m_hasEllipsisBox(false)
90 , m_dirOverride(false)
91 , m_isText(false)
92 , m_determinedIfNextOnLineExists(false)
93 , m_determinedIfPrevOnLineExists(false)
94 , m_nextOnLineExists(false)
95 , m_prevOnLineExists(false)
96 , m_toAdd(0)
97 #ifndef NDEBUG
98 , m_hasBadParent(false)
99 #endif
100 {
101 }
102
103 virtual ~InlineBox();
104
105 virtual void destroy(RenderArena*);
106
107 virtual void deleteLine(RenderArena*);
108 virtual void extractLine();
109 virtual void attachLine();
110
isLineBreak()111 virtual bool isLineBreak() const { return false; }
112
113 virtual void adjustPosition(int dx, int dy);
114
115 virtual void paint(RenderObject::PaintInfo&, int tx, int ty);
116 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty);
117
118 // Overloaded new operator.
119 void* operator new(size_t, RenderArena*) throw();
120
121 // Overridden to prevent the normal delete from being called.
122 void operator delete(void*, size_t);
123
124 private:
125 // The normal operator new is disallowed.
126 void* operator new(size_t) throw();
127
128 public:
129 #ifndef NDEBUG
130 void showTreeForThis() const;
131 #endif
132
isText()133 bool isText() const { return m_isText; }
setIsText(bool b)134 void setIsText(bool b) { m_isText = b; }
135
isInlineBox()136 virtual bool isInlineBox() { return false; }
isInlineFlowBox()137 virtual bool isInlineFlowBox() const { return false; }
isInlineTextBox()138 virtual bool isInlineTextBox() { return false; }
isRootInlineBox()139 virtual bool isRootInlineBox() const { return false; }
140 #if ENABLE(SVG)
isSVGRootInlineBox()141 virtual bool isSVGRootInlineBox() { return false; }
142
hasVirtualHeight()143 bool hasVirtualHeight() const { return m_hasVirtualHeight; }
setHasVirtualHeight()144 void setHasVirtualHeight() { m_hasVirtualHeight = true; }
virtualHeight()145 virtual int virtualHeight() const { ASSERT_NOT_REACHED(); return 0; }
146 #endif
147
isConstructed()148 bool isConstructed() { return m_constructed; }
setConstructed()149 virtual void setConstructed()
150 {
151 m_constructed = true;
152 if (m_next)
153 m_next->setConstructed();
154 }
155
156 void setExtracted(bool b = true) { m_extracted = b; }
157
setFirstLineStyleBit(bool f)158 void setFirstLineStyleBit(bool f) { m_firstLine = f; }
isFirstLineStyle()159 bool isFirstLineStyle() const { return m_firstLine; }
160
161 void remove();
162
nextOnLine()163 InlineBox* nextOnLine() const { return m_next; }
prevOnLine()164 InlineBox* prevOnLine() const { return m_prev; }
setNextOnLine(InlineBox * next)165 void setNextOnLine(InlineBox* next)
166 {
167 ASSERT(m_parent || !next);
168 m_next = next;
169 }
setPrevOnLine(InlineBox * prev)170 void setPrevOnLine(InlineBox* prev)
171 {
172 ASSERT(m_parent || !prev);
173 m_prev = prev;
174 }
175 bool nextOnLineExists() const;
176 bool prevOnLineExists() const;
177
isLeaf()178 virtual bool isLeaf() const { return true; }
179
180 InlineBox* nextLeafChild() const;
181 InlineBox* prevLeafChild() const;
182
renderer()183 RenderObject* renderer() const { return m_renderer; }
184
parent()185 InlineFlowBox* parent() const
186 {
187 ASSERT(!m_hasBadParent);
188 return m_parent;
189 }
setParent(InlineFlowBox * par)190 void setParent(InlineFlowBox* par) { m_parent = par; }
191
192 const RootInlineBox* root() const;
193 RootInlineBox* root();
194
setWidth(int w)195 void setWidth(int w) { m_width = w; }
width()196 int width() const { return m_width; }
197
198 // x() is the left side of the box in the parent's coordinate system.
setX(int x)199 void setX(int x) { m_x = x; }
x()200 int x() const { return m_x; }
201
202 // y() is the top of the box in the parent's coordinate system.
setY(int y)203 void setY(int y) { m_y = y; }
y()204 int y() const { return m_y; }
205
206 int height() const;
207
baselinePosition(bool isRootLineBox)208 inline int baselinePosition(bool isRootLineBox) const { return renderer()->baselinePosition(m_firstLine, isRootLineBox); }
lineHeight(bool isRootLineBox)209 inline int lineHeight(bool isRootLineBox) const { return renderer()->lineHeight(m_firstLine, isRootLineBox); }
210
topOverflow()211 virtual int topOverflow() const { return y(); }
bottomOverflow()212 virtual int bottomOverflow() const { return y() + height(); }
leftOverflow()213 virtual int leftOverflow() const { return x(); }
rightOverflow()214 virtual int rightOverflow() const { return x() + width(); }
215
216 virtual int caretMinOffset() const;
217 virtual int caretMaxOffset() const;
218 virtual unsigned caretMaxRenderedOffset() const;
219
bidiLevel()220 unsigned char bidiLevel() const { return m_bidiEmbeddingLevel; }
setBidiLevel(unsigned char level)221 void setBidiLevel(unsigned char level) { m_bidiEmbeddingLevel = level; }
direction()222 TextDirection direction() const { return m_bidiEmbeddingLevel % 2 ? RTL : LTR; }
caretLeftmostOffset()223 int caretLeftmostOffset() const { return direction() == LTR ? caretMinOffset() : caretMaxOffset(); }
caretRightmostOffset()224 int caretRightmostOffset() const { return direction() == LTR ? caretMaxOffset() : caretMinOffset(); }
225
clearTruncation()226 virtual void clearTruncation() { }
227
isDirty()228 bool isDirty() const { return m_dirty; }
229 void markDirty(bool dirty = true) { m_dirty = dirty; }
230
231 void dirtyLineBoxes();
232
233 virtual RenderObject::SelectionState selectionState();
234
235 virtual bool canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth);
236 // visibleLeftEdge, visibleRightEdge are in the parent's coordinate system.
237 virtual int placeEllipsisBox(bool ltr, int visibleLeftEdge, int visibleRightEdge, int ellipsisWidth, bool&);
238
239 void setHasBadParent();
240
toAdd()241 int toAdd() const { return m_toAdd; }
242
visibleToHitTesting()243 bool visibleToHitTesting() const { return renderer()->style()->visibility() == VISIBLE && renderer()->style()->pointerEvents() != PE_NONE; }
244
245 // Use with caution! The type is not checked!
boxModelObject()246 RenderBoxModelObject* boxModelObject() const
247 {
248 if (!m_renderer->isText())
249 return toRenderBoxModelObject(m_renderer);
250 return 0;
251 }
252
253 private:
254 InlineBox* m_next; // The next element on the same line as us.
255 InlineBox* m_prev; // The previous element on the same line as us.
256
257 InlineFlowBox* m_parent; // The box that contains us.
258
259 public:
260 RenderObject* m_renderer;
261
262 int m_x;
263 int m_y;
264 int m_width;
265
266 // Some of these bits are actually for subclasses and moved here to compact the structures.
267
268 // for this class
269 protected:
270 bool m_firstLine : 1;
271 private:
272 bool m_constructed : 1;
273 unsigned char m_bidiEmbeddingLevel : 6;
274 protected:
275 bool m_dirty : 1;
276 bool m_extracted : 1;
277 bool m_hasVirtualHeight : 1;
278
279 // for RootInlineBox
280 bool m_endsWithBreak : 1; // Whether the line ends with a <br>.
281 bool m_hasSelectedChildren : 1; // Whether we have any children selected (this bit will also be set if the <br> that terminates our line is selected).
282 bool m_hasEllipsisBox : 1;
283
284 // for InlineTextBox
285 public:
286 bool m_dirOverride : 1;
287 bool m_isText : 1; // Whether or not this object represents text with a non-zero height. Includes non-image list markers, text boxes.
288 protected:
289 mutable bool m_determinedIfNextOnLineExists : 1;
290 mutable bool m_determinedIfPrevOnLineExists : 1;
291 mutable bool m_nextOnLineExists : 1;
292 mutable bool m_prevOnLineExists : 1;
293 int m_toAdd : 12; // for justified text
294
295 #ifndef NDEBUG
296 private:
297 bool m_hasBadParent;
298 #endif
299 };
300
301 #ifdef NDEBUG
~InlineBox()302 inline InlineBox::~InlineBox()
303 {
304 }
305 #endif
306
setHasBadParent()307 inline void InlineBox::setHasBadParent()
308 {
309 #ifndef NDEBUG
310 m_hasBadParent = true;
311 #endif
312 }
313
314 } // namespace WebCore
315
316 #ifndef NDEBUG
317 // Outside the WebCore namespace for ease of invocation from gdb.
318 void showTree(const WebCore::InlineBox*);
319 #endif
320
321 #endif // InlineBox_h
322