Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / InlineBox.h
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 "RenderBoxModelObject.h"
25 #include "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:
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
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
66     virtual void destroy(RenderArena*);
67
68     virtual void deleteLine(RenderArena*);
69     virtual void extractLine();
70     virtual void attachLine();
71
72     virtual bool isLineBreak() const { return false; }
73
74     virtual void adjustPosition(float dx, float dy);
75     void adjustLogicalPosition(float deltaLogicalLeft, float deltaLogicalTop)
76     {
77         if (isHorizontal())
78             adjustPosition(deltaLogicalLeft, deltaLogicalTop);
79         else
80             adjustPosition(deltaLogicalTop, deltaLogicalLeft);
81     }
82     void adjustLineDirectionPosition(float delta)
83     {
84         if (isHorizontal())
85             adjustPosition(delta, 0);
86         else
87             adjustPosition(0, delta);
88     }
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 HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
99
100     // Overloaded new operator.
101     void* operator new(size_t, RenderArena*);
102
103     // Overridden to prevent the normal delete from being called.
104     void operator delete(void*, size_t);
105
106 private:
107     // The normal operator new is disallowed.
108     void* operator new(size_t) throw();
109
110 public:
111 #ifndef NDEBUG
112     void showTreeForThis() const;
113     void showLineTreeForThis() const;
114     
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;
118 #endif
119
120     bool isText() const { return m_bitfields.isText(); }
121     void setIsText(bool isText) { m_bitfields.setIsText(isText); }
122  
123     virtual bool isInlineFlowBox() const { return false; }
124     virtual bool isInlineTextBox() const { return false; }
125     virtual bool isRootInlineBox() const { return false; }
126 #if ENABLE(SVG)
127     virtual bool isSVGInlineTextBox() const { return false; }
128     virtual bool isSVGInlineFlowBox() const { return false; }
129     virtual bool isSVGRootInlineBox() const { return false; }
130 #endif
131
132     bool hasVirtualLogicalHeight() const { return m_bitfields.hasVirtualLogicalHeight(); }
133     void setHasVirtualLogicalHeight() { m_bitfields.setHasVirtualLogicalHeight(true); }
134     virtual float virtualLogicalHeight() const
135     {
136         ASSERT_NOT_REACHED();
137         return 0;
138     }
139
140     bool isHorizontal() const { return m_bitfields.isHorizontal(); }
141     void setIsHorizontal(bool isHorizontal) { m_bitfields.setIsHorizontal(isHorizontal); }
142
143     virtual FloatRect calculateBoundaries() const
144     {
145         ASSERT_NOT_REACHED();
146         return FloatRect();
147     }
148
149     bool isConstructed() { return m_bitfields.constructed(); }
150     virtual void setConstructed() { m_bitfields.setConstructed(true); }
151
152     void setExtracted(bool extracted = true) { m_bitfields.setExtracted(extracted); }
153     
154     void setFirstLineStyleBit(bool firstLine) { m_bitfields.setFirstLine(firstLine); }
155     bool isFirstLineStyle() const { return m_bitfields.firstLine(); }
156
157     void remove();
158
159     InlineBox* nextOnLine() const { return m_next; }
160     InlineBox* prevOnLine() const { return m_prev; }
161     void setNextOnLine(InlineBox* next)
162     {
163         ASSERT(m_parent || !next);
164         m_next = next;
165     }
166     void setPrevOnLine(InlineBox* prev)
167     {
168         ASSERT(m_parent || !prev);
169         m_prev = prev;
170     }
171     bool nextOnLineExists() const;
172
173     virtual bool isLeaf() const { return true; }
174     
175     InlineBox* nextLeafChild() const;
176     InlineBox* prevLeafChild() const;
177
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;
183
184     RenderObject* renderer() const { return m_renderer; }
185
186     InlineFlowBox* parent() const
187     {
188         ASSERT(!m_hasBadParent);
189         return m_parent;
190     }
191     void setParent(InlineFlowBox* par) { m_parent = par; }
192
193     const RootInlineBox* root() const;
194     RootInlineBox* root();
195
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(); }
200
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(); }
205
206     const FloatPoint& topLeft() const { return m_topLeft; }
207
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(); }
213
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)
218     {
219         if (isHorizontal())
220             setX(left);
221         else
222             setY(left);
223     }
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()); }
228
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)
233     {
234         if (isHorizontal())
235             setY(top);
236         else
237             setX(top);
238     }
239
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; }
243
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;
246
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()); }
248
249     virtual LayoutUnit baselinePosition(FontBaseline baselineType) const;
250     virtual LayoutUnit lineHeight() const;
251
252     virtual int caretMinOffset() const;
253     virtual int caretMaxOffset() const;
254
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(); }
261
262     virtual void clearTruncation() { }
263
264     bool isDirty() const { return m_bitfields.dirty(); }
265     void markDirty(bool dirty = true) { m_bitfields.setDirty(dirty); }
266
267     virtual void dirtyLineBoxes();
268     
269     virtual RenderObject::SelectionState selectionState();
270
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&);
274
275 #ifndef NDEBUG
276     void setHasBadParent();
277 #endif
278
279     int expansion() const { return m_bitfields.expansion(); }
280
281     bool visibleToHitTesting() const { return renderer()->style()->visibility() == VISIBLE && renderer()->style()->pointerEvents() != PE_NONE; }
282     
283     EVerticalAlign verticalAlign() const { return renderer()->style(m_bitfields.firstLine())->verticalAlign(); }
284
285     // Use with caution! The type is not checked!
286     RenderBoxModelObject* boxModelObject() const
287     { 
288         if (!m_renderer->isText())
289             return toRenderBoxModelObject(m_renderer);
290         return 0;
291     }
292
293     FloatPoint locationIncludingFlipping();
294     void flipForWritingMode(FloatRect&);
295     FloatPoint flipForWritingMode(const FloatPoint&);
296     void flipForWritingMode(LayoutRect&);
297     LayoutPoint flipForWritingMode(const LayoutPoint&);
298
299     bool knownToHaveNoOverflow() const { return m_bitfields.knownToHaveNoOverflow(); }
300     void clearKnownToHaveNoOverflow();
301
302     bool dirOverride() const { return m_bitfields.dirOverride(); }
303     void setDirOverride(bool dirOverride) { m_bitfields.setDirOverride(dirOverride); }
304
305 private:
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.
308
309     InlineFlowBox* m_parent; // The box that contains us.
310
311 public:
312     RenderObject* m_renderer;
313
314     FloatPoint m_topLeft;
315     float m_logicalWidth;
316
317 #define ADD_BOOLEAN_BITFIELD(name, Name) \
318     private:\
319     unsigned m_##name : 1;\
320     public:\
321     bool name() const { return m_##name; }\
322     void set##Name(bool name) { m_##name = name; }\
323
324     class InlineBoxBitfields {
325     public:
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)
330             , m_dirty(dirty)
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)
339             , m_isText(false)
340             , m_determinedIfNextOnLineExists(false)
341             , m_nextOnLineExists(false)
342             , m_expansion(0)
343         {
344         }
345
346         // Some of these bits are actually for subclasses and moved here to compact the structures.
347         // for this class
348         ADD_BOOLEAN_BITFIELD(firstLine, FirstLine);
349         ADD_BOOLEAN_BITFIELD(constructed, Constructed);
350
351     private:
352         unsigned m_bidiEmbeddingLevel : 6; // The maximium bidi level is 62: http://unicode.org/reports/tr9/#Explicit_Levels_and_Directions
353
354     public:
355         unsigned char bidiEmbeddingLevel() const { return m_bidiEmbeddingLevel; }
356         void setBidiEmbeddingLevel(unsigned char bidiEmbeddingLevel) { m_bidiEmbeddingLevel = bidiEmbeddingLevel; }
357
358         ADD_BOOLEAN_BITFIELD(dirty, Dirty);
359         ADD_BOOLEAN_BITFIELD(extracted, Extracted);
360         ADD_BOOLEAN_BITFIELD(hasVirtualLogicalHeight, HasVirtualLogicalHeight);
361         ADD_BOOLEAN_BITFIELD(isHorizontal, IsHorizontal);
362         // for RootInlineBox
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);
368         // for InlineTextBox
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.
371
372     private:
373         mutable unsigned m_determinedIfNextOnLineExists : 1;
374
375     public:
376         bool determinedIfNextOnLineExists() const { return m_determinedIfNextOnLineExists; }
377         void setDeterminedIfNextOnLineExists(bool determinedIfNextOnLineExists) const { m_determinedIfNextOnLineExists = determinedIfNextOnLineExists; }
378
379     private:
380         mutable unsigned m_nextOnLineExists : 1;
381         
382     public:
383         bool nextOnLineExists() const { return m_nextOnLineExists; }
384         void setNextOnLineExists(bool nextOnLineExists) const { m_nextOnLineExists = nextOnLineExists; }
385
386     private:
387         signed m_expansion : 12; // for justified text
388         
389     public:
390         signed expansion() const { return m_expansion; }
391         void setExpansion(signed expansion) { m_expansion = expansion; }
392     };
393 #undef ADD_BOOLEAN_BITFIELD
394
395 private:
396     InlineBoxBitfields m_bitfields;
397
398 protected:
399     // For RootInlineBox
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); }
406
407     // For InlineTextBox
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); }
414     
415     // For InlineFlowBox and InlineTextBox
416     bool extracted() const { return m_bitfields.extracted(); }
417
418 #ifndef NDEBUG
419 private:
420     bool m_hasBadParent;
421 #endif
422 };
423
424 #ifdef NDEBUG
425 inline InlineBox::~InlineBox()
426 {
427 }
428 #endif
429
430 #ifndef NDEBUG
431 inline void InlineBox::setHasBadParent()
432 {
433     m_hasBadParent = true;
434 }
435 #endif
436
437 } // namespace WebCore
438
439 #ifndef NDEBUG
440 // Outside the WebCore namespace for ease of invocation from gdb.
441 void showTree(const WebCore::InlineBox*);
442 void showLineTree(const WebCore::InlineBox*);
443 #endif
444
445 #endif // InlineBox_h