initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderBlock.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2007 David Smith (catfish.man@gmail.com)
5  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef RenderBlock_h
24 #define RenderBlock_h
25
26 #include "GapRects.h"
27 #include "PODIntervalTree.h"
28 #include "RenderBox.h"
29 #include "RenderLineBoxList.h"
30 #include "RootInlineBox.h"
31 #include "TextRun.h"
32 #include <wtf/OwnPtr.h>
33 #include <wtf/ListHashSet.h>
34
35 namespace WebCore {
36
37 class BidiContext;
38 class ColumnInfo;
39 class InlineIterator;
40 class LayoutStateMaintainer;
41 class LazyLineBreakIterator;
42 class LineLayoutState;
43 class LineWidth;
44 class RenderInline;
45 class RenderText;
46
47 struct BidiRun;
48 struct PaintInfo;
49 class LineInfo;
50 class RenderRubyRun;
51
52 template <class Iterator, class Run> class BidiResolver;
53 template <class Run> class BidiRunList;
54 template <class Iterator> struct MidpointState;
55 typedef BidiResolver<InlineIterator, BidiRun> InlineBidiResolver;
56 typedef MidpointState<InlineIterator> LineMidpointState;
57
58 enum CaretType { CursorCaret, DragCaret };
59
60 enum TextRunFlag {
61     DefaultTextRunFlags = 0,
62     RespectDirection = 1 << 0,
63     RespectDirectionOverride = 1 << 1
64 };
65
66 typedef unsigned TextRunFlags;
67
68 class RenderBlock : public RenderBox {
69 public:
70     friend class LineLayoutState;
71 #ifndef NDEBUG
72     // Used by the PODIntervalTree for debugging the FloatingObject.
73     template <class> friend struct ValueToString;
74 #endif
75
76     RenderBlock(Node*);
77     virtual ~RenderBlock();
78
79     const RenderObjectChildList* children() const { return &m_children; }
80     RenderObjectChildList* children() { return &m_children; }
81
82     bool beingDestroyed() const { return m_beingDestroyed; }
83
84     // These two functions are overridden for inline-block.
85     virtual LayoutUnit lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
86     virtual LayoutUnit baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
87
88     RenderLineBoxList* lineBoxes() { return &m_lineBoxes; }
89     const RenderLineBoxList* lineBoxes() const { return &m_lineBoxes; }
90
91     InlineFlowBox* firstLineBox() const { return m_lineBoxes.firstLineBox(); }
92     InlineFlowBox* lastLineBox() const { return m_lineBoxes.lastLineBox(); }
93
94     void deleteLineBoxTree();
95
96     virtual void addChild(RenderObject* newChild, RenderObject* beforeChild = 0);
97     virtual void removeChild(RenderObject*);
98
99     enum BlockLayoutPass { NormalLayoutPass, PositionedFloatLayoutPass };
100     virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0, BlockLayoutPass = NormalLayoutPass);
101
102     void insertPositionedObject(RenderBox*);
103     void removePositionedObject(RenderBox*);
104     void removePositionedObjects(RenderBlock*);
105
106     typedef ListHashSet<RenderBox*, 4> PositionedObjectsListHashSet;
107     PositionedObjectsListHashSet* positionedObjects() const { return m_positionedObjects.get(); }
108
109     void addPercentHeightDescendant(RenderBox*);
110     static void removePercentHeightDescendant(RenderBox*);
111     HashSet<RenderBox*>* percentHeightDescendants() const;
112 #if !ASSERT_DISABLED
113     static bool hasPercentHeightDescendant(RenderBox*);
114 #endif
115
116     RootInlineBox* createAndAppendRootInlineBox();
117
118     bool generatesLineBoxesForInlineChild(RenderObject*);
119
120     void markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove = 0, bool inLayout = true);
121     void markSiblingsWithFloatsForLayout();
122     void markPositionedObjectsForLayout();
123     virtual void markForPaginationRelayoutIfNeeded();
124     
125     bool containsFloats() { return m_floatingObjects && !m_floatingObjects->set().isEmpty(); }
126     bool containsFloat(RenderBox*);
127
128     LayoutUnit availableLogicalWidthForLine(LayoutUnit position, bool firstLine) const;
129     LayoutUnit logicalRightOffsetForLine(LayoutUnit position, bool firstLine) const { return logicalRightOffsetForLine(position, logicalRightOffsetForContent(), firstLine); }
130     LayoutUnit logicalLeftOffsetForLine(LayoutUnit position, bool firstLine) const { return logicalLeftOffsetForLine(position, logicalLeftOffsetForContent(), firstLine); }
131     LayoutUnit startOffsetForLine(LayoutUnit position, bool firstLine) const { return style()->isLeftToRightDirection() ? logicalLeftOffsetForLine(position, firstLine) : logicalRightOffsetForLine(position, firstLine); }
132     LayoutUnit startAlignedOffsetForLine(RenderBox* child, LayoutUnit position, bool firstLine);
133     LayoutUnit textIndentOffset() const;
134
135     virtual VisiblePosition positionForPoint(const LayoutPoint&);
136     
137     // Block flows subclass availableWidth to handle multi column layout (shrinking the width available to children when laying out.)
138     virtual LayoutUnit availableLogicalWidth() const;
139
140     LayoutPoint flipForWritingModeIncludingColumns(const LayoutPoint&) const;
141     void adjustStartEdgeForWritingModeIncludingColumns(LayoutRect&) const;
142
143     RootInlineBox* firstRootBox() const { return static_cast<RootInlineBox*>(firstLineBox()); }
144     RootInlineBox* lastRootBox() const { return static_cast<RootInlineBox*>(lastLineBox()); }
145
146     bool containsNonZeroBidiLevel() const;
147
148     GapRects selectionGapRectsForRepaint(RenderBoxModelObject* repaintContainer);
149     LayoutRect logicalLeftSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
150                                        RenderObject* selObj, LayoutUnit logicalLeft, LayoutUnit logicalTop, LayoutUnit logicalHeight, const PaintInfo*);
151     LayoutRect logicalRightSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
152                                         RenderObject* selObj, LayoutUnit logicalRight, LayoutUnit logicalTop, LayoutUnit logicalHeight, const PaintInfo*);
153     void getSelectionGapInfo(SelectionState, bool& leftGap, bool& rightGap);
154     LayoutRect logicalRectToPhysicalRect(const LayoutPoint& physicalPosition, const LayoutRect& logicalRect);
155         
156     // Helper methods for computing line counts and heights for line counts.
157     RootInlineBox* lineAtIndex(int);
158     int lineCount();
159     int heightForLineCount(int);
160     void clearTruncation();
161
162     void adjustRectForColumns(LayoutRect&) const;
163     virtual void adjustForColumns(LayoutSize&, const LayoutPoint&) const;
164
165     void addContinuationWithOutline(RenderInline*);
166     bool paintsContinuationOutline(RenderInline*);
167
168     virtual RenderBoxModelObject* virtualContinuation() const { return continuation(); }
169     bool isAnonymousBlockContinuation() const { return continuation() && isAnonymousBlock(); }
170     RenderInline* inlineElementContinuation() const;
171     RenderBlock* blockElementContinuation() const;
172
173     using RenderBoxModelObject::continuation;
174     using RenderBoxModelObject::setContinuation;
175
176     // This function is a convenience helper for creating an anonymous block that inherits its
177     // style from this RenderBlock.
178     RenderBlock* createAnonymousBlock(bool isFlexibleBox = false) const;
179     RenderBlock* createAnonymousColumnsBlock() const;
180     RenderBlock* createAnonymousColumnSpanBlock() const;
181     RenderBlock* createAnonymousBlockWithSameTypeAs(RenderBlock* otherAnonymousBlock) const;
182     
183     static void appendRunsForObject(BidiRunList<BidiRun>&, int start, int end, RenderObject*, InlineBidiResolver&);
184
185     static TextRun constructTextRun(RenderObject* context, const Font&, const String&, RenderStyle*,
186                                     TextRun::ExpansionBehavior = TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, TextRunFlags = DefaultTextRunFlags);
187
188     static TextRun constructTextRun(RenderObject* context, const Font&, const UChar*, int length, RenderStyle*,
189                                     TextRun::ExpansionBehavior = TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, TextRunFlags = DefaultTextRunFlags);
190
191     ColumnInfo* columnInfo() const;
192     int columnGap() const;
193     
194     // These two functions take the ColumnInfo* to avoid repeated lookups of the info in the global HashMap.
195     unsigned columnCount(ColumnInfo*) const;
196     LayoutRect columnRectAt(ColumnInfo*, unsigned) const;
197
198     int paginationStrut() const { return m_rareData ? m_rareData->m_paginationStrut : 0; }
199     void setPaginationStrut(int);
200     
201     // The page logical offset is the object's offset from the top of the page in the page progression
202     // direction (so an x-offset in vertical text and a y-offset for horizontal text).
203     int pageLogicalOffset() const { return m_rareData ? m_rareData->m_pageLogicalOffset : 0; }
204     void setPageLogicalOffset(int);
205
206     // Accessors for logical width/height and margins in the containing block's block-flow direction.
207     enum ApplyLayoutDeltaMode { ApplyLayoutDelta, DoNotApplyLayoutDelta };
208     LayoutUnit logicalWidthForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->width() : child->height(); }
209     LayoutUnit logicalHeightForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->height() : child->width(); }
210     LayoutUnit logicalTopForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->y() : child->x(); }
211     LayoutUnit logicalLeftForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->x() : child->y(); }
212     void setLogicalLeftForChild(RenderBox* child, LayoutUnit logicalLeft, ApplyLayoutDeltaMode = DoNotApplyLayoutDelta);
213     void setLogicalTopForChild(RenderBox* child, LayoutUnit logicalTop, ApplyLayoutDeltaMode = DoNotApplyLayoutDelta);
214     LayoutUnit marginBeforeForChild(RenderBoxModelObject* child) const;
215     LayoutUnit marginAfterForChild(RenderBoxModelObject* child) const;
216     LayoutUnit marginStartForChild(RenderBoxModelObject* child) const;
217     LayoutUnit marginEndForChild(RenderBoxModelObject* child) const;
218     LayoutUnit marginLogicalLeftForChild(RenderBoxModelObject* child) const;
219     LayoutUnit marginLogicalRightForChild(RenderBoxModelObject* child) const;
220     void setMarginStartForChild(RenderBox* child, LayoutUnit);
221     void setMarginEndForChild(RenderBox* child, LayoutUnit);
222     void setMarginBeforeForChild(RenderBox* child, LayoutUnit);
223     void setMarginAfterForChild(RenderBox* child, LayoutUnit);
224     LayoutUnit collapsedMarginBeforeForChild(RenderBox* child) const;
225     LayoutUnit collapsedMarginAfterForChild(RenderBox* child) const;
226
227     void updateLogicalWidthForAlignment(const ETextAlign&, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount);
228
229     virtual void updateFirstLetter();
230
231     class MarginValues {
232     public:
233         MarginValues(LayoutUnit beforePos, LayoutUnit beforeNeg, LayoutUnit afterPos, LayoutUnit afterNeg)
234             : m_positiveMarginBefore(beforePos)
235             , m_negativeMarginBefore(beforeNeg)
236             , m_positiveMarginAfter(afterPos)
237             , m_negativeMarginAfter(afterNeg)
238         { }
239         
240         LayoutUnit positiveMarginBefore() const { return m_positiveMarginBefore; }
241         LayoutUnit negativeMarginBefore() const { return m_negativeMarginBefore; }
242         LayoutUnit positiveMarginAfter() const { return m_positiveMarginAfter; }
243         LayoutUnit negativeMarginAfter() const { return m_negativeMarginAfter; }
244         
245         void setPositiveMarginBefore(LayoutUnit pos) { m_positiveMarginBefore = pos; }
246         void setNegativeMarginBefore(LayoutUnit neg) { m_negativeMarginBefore = neg; }
247         void setPositiveMarginAfter(LayoutUnit pos) { m_positiveMarginAfter = pos; }
248         void setNegativeMarginAfter(LayoutUnit neg) { m_negativeMarginAfter = neg; }
249     
250     private:
251         LayoutUnit m_positiveMarginBefore;
252         LayoutUnit m_negativeMarginBefore;
253         LayoutUnit m_positiveMarginAfter;
254         LayoutUnit m_negativeMarginAfter;
255     };
256     MarginValues marginValuesForChild(RenderBox* child);
257
258     virtual void scrollbarsChanged(bool /*horizontalScrollbarChanged*/, bool /*verticalScrollbarChanged*/) { };
259
260     int logicalRightOffsetForContent() const { return isHorizontalWritingMode() ? borderLeft() + paddingLeft() + availableLogicalWidth() : borderTop() + paddingTop() + availableLogicalWidth(); }
261     int logicalLeftOffsetForContent() const { return isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
262
263 #ifndef NDEBUG
264     void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0) const;
265 #endif
266
267 protected:
268     virtual void willBeDestroyed();
269
270     // These functions are only used internally to manipulate the render tree structure via remove/insert/appendChildNode.
271     // Since they are typically called only to move objects around within anonymous blocks (which only have layers in
272     // the case of column spans), the default for fullRemoveInsert is false rather than true.
273     void moveChildTo(RenderBlock* to, RenderObject* child, bool fullRemoveInsert = false)
274     {
275         return moveChildTo(to, child, 0, fullRemoveInsert);
276     }
277     void moveChildTo(RenderBlock* to, RenderObject* child, RenderObject* beforeChild, bool fullRemoveInsert = false);
278     void moveAllChildrenTo(RenderBlock* to, bool fullRemoveInsert = false)
279     {
280         return moveAllChildrenTo(to, 0, fullRemoveInsert);
281     }
282     void moveAllChildrenTo(RenderBlock* to, RenderObject* beforeChild, bool fullRemoveInsert = false)
283     {
284         return moveChildrenTo(to, firstChild(), 0, beforeChild, fullRemoveInsert);
285     }
286     // Move all of the kids from |startChild| up to but excluding |endChild|.  0 can be passed as the endChild to denote
287     // that all the kids from |startChild| onwards should be added.
288     void moveChildrenTo(RenderBlock* to, RenderObject* startChild, RenderObject* endChild, bool fullRemoveInsert = false)
289     {
290         return moveChildrenTo(to, startChild, endChild, 0, fullRemoveInsert);
291     }
292     void moveChildrenTo(RenderBlock* to, RenderObject* startChild, RenderObject* endChild, RenderObject* beforeChild, bool fullRemoveInsert = false);
293     
294     LayoutUnit maxPositiveMarginBefore() const { return m_rareData ? m_rareData->m_margins.positiveMarginBefore() : RenderBlockRareData::positiveMarginBeforeDefault(this); }
295     LayoutUnit maxNegativeMarginBefore() const { return m_rareData ? m_rareData->m_margins.negativeMarginBefore() : RenderBlockRareData::negativeMarginBeforeDefault(this); }
296     LayoutUnit maxPositiveMarginAfter() const { return m_rareData ? m_rareData->m_margins.positiveMarginAfter() : RenderBlockRareData::positiveMarginAfterDefault(this); }
297     LayoutUnit maxNegativeMarginAfter() const { return m_rareData ? m_rareData->m_margins.negativeMarginAfter() : RenderBlockRareData::negativeMarginAfterDefault(this); }
298     
299     void setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg);
300     void setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg);
301
302     void initMaxMarginValues()
303     {
304         if (m_rareData) {
305             m_rareData->m_margins = MarginValues(RenderBlockRareData::positiveMarginBeforeDefault(this) , RenderBlockRareData::negativeMarginBeforeDefault(this),
306                                                  RenderBlockRareData::positiveMarginAfterDefault(this), RenderBlockRareData::negativeMarginAfterDefault(this));
307             m_rareData->m_paginationStrut = 0;
308         }
309     }
310
311     virtual void layout();
312
313     bool layoutPositionedObjects(bool relayoutChildren);
314
315     virtual void paint(PaintInfo&, const LayoutPoint&);
316     virtual void paintObject(PaintInfo&, const LayoutPoint&);
317
318     void adjustForRegionFittingIfNeeded(LayoutUnit logicalTop, LayoutUnit& rightOffset) const;
319
320     LayoutUnit logicalRightOffsetForLine(LayoutUnit position, LayoutUnit fixedOffset, bool applyTextIndent = true, LayoutUnit* logicalHeightRemaining = 0) const;
321     LayoutUnit logicalLeftOffsetForLine(LayoutUnit position, LayoutUnit fixedOffset, bool applyTextIndent = true, LayoutUnit* logicalHeightRemaining = 0) const;
322
323     virtual ETextAlign textAlignmentForLine(bool endsWithSoftBreak) const;
324     virtual void adjustInlineDirectionLineBounds(int /* expansionOpportunityCount */, float& /* logicalLeft */, float& /* logicalWidth */) const { }
325
326     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
327
328     virtual void computePreferredLogicalWidths();
329
330     virtual LayoutUnit firstLineBoxBaseline() const;
331     virtual LayoutUnit lastLineBoxBaseline() const;
332
333     virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&);
334
335     // Delay update scrollbar until finishDelayRepaint() will be
336     // called. This function is used when a flexbox is laying out its
337     // descendant. If multiple calls are made to startDelayRepaint(),
338     // finishDelayRepaint() will do nothing until finishDelayRepaint()
339     // is called the same number of times.
340     static void startDelayUpdateScrollInfo();
341     static void finishDelayUpdateScrollInfo();
342
343     virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
344     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
345
346     virtual bool hasLineIfEmpty() const;
347     
348     bool simplifiedLayout();
349     void simplifiedNormalFlowLayout();
350
351     void computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats = false);
352     virtual void addOverflowFromChildren();
353     void addOverflowFromFloats();
354     void addOverflowFromPositionedObjects();
355     void addOverflowFromBlockChildren();
356     void addOverflowFromInlineChildren();
357
358     virtual void addFocusRingRects(Vector<LayoutRect>&, const LayoutPoint&);
359
360 #if ENABLE(SVG)
361     // Only used by RenderSVGText, which explicitely overrides RenderBlock::layoutBlock(), do NOT use for anything else.
362     void forceLayoutInlineChildren()
363     {
364         LayoutUnit repaintLogicalTop = 0;
365         LayoutUnit repaintLogicalBottom = 0;
366         layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom);
367     }
368 #endif
369
370 private:
371     virtual RenderObjectChildList* virtualChildren() { return children(); }
372     virtual const RenderObjectChildList* virtualChildren() const { return children(); }
373
374     virtual const char* renderName() const;
375
376     virtual bool isRenderBlock() const { return true; }
377     virtual bool isBlockFlow() const { return (!isInline() || isReplaced()) && !isTable(); }
378     virtual bool isInlineBlockOrInlineTable() const { return isInline() && isReplaced(); }
379
380     void makeChildrenNonInline(RenderObject* insertionPoint = 0);
381     virtual void removeLeftoverAnonymousBlock(RenderBlock* child);
382
383     virtual void dirtyLinesFromChangedChild(RenderObject* child) { m_lineBoxes.dirtyLinesFromChangedChild(this, child); }
384
385     void addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild);
386     void addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild);
387     void addChildToAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild);
388     virtual void addChildIgnoringAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild = 0);
389     
390     virtual bool isSelfCollapsingBlock() const;
391
392     virtual LayoutUnit collapsedMarginBefore() const { return maxPositiveMarginBefore() - maxNegativeMarginBefore(); }
393     virtual LayoutUnit collapsedMarginAfter() const { return maxPositiveMarginAfter() - maxNegativeMarginAfter(); }
394
395     virtual void repaintOverhangingFloats(bool paintAllDescendants);
396
397     void layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom);
398     void layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom);
399     BidiRun* handleTrailingSpaces(BidiRunList<BidiRun>&, BidiContext*);
400
401     virtual void borderFitAdjust(LayoutRect&) const; // Shrink the box in which the border paints if border-fit is set.
402
403     virtual void updateBeforeAfterContent(PseudoId);
404
405     virtual RootInlineBox* createRootInlineBox(); // Subclassed by SVG and Ruby.
406
407     // Called to lay out the legend for a fieldset or the ruby text of a ruby run.
408     virtual RenderObject* layoutSpecialExcludedChild(bool /*relayoutChildren*/) { return 0; }
409
410     struct FloatWithRect {
411         FloatWithRect(RenderBox* f)
412             : object(f)
413             , rect(IntRect(f->x() - f->marginLeft(), f->y() - f->marginTop(), f->width() + f->marginLeft() + f->marginRight(), f->height() + f->marginTop() + f->marginBottom()))
414             , everHadLayout(f->m_everHadLayout)
415         {
416         }
417
418         RenderBox* object;
419         IntRect rect;
420         bool everHadLayout;
421     };
422
423     struct FloatingObject {
424         WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED;
425     public:
426         // FloatLeftRight is a mask to query for both left and right but not positioned.
427         // FloatAll is a mask to query for all types of floats.
428         enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3, FloatPositioned = 4, FloatAll = 7 };
429
430         FloatingObject(EFloat type)
431             : m_renderer(0)
432             , m_originatingLine(0)
433             , m_paginationStrut(0)
434             , m_shouldPaint(false)
435             , m_isDescendant(false)
436             , m_isPlaced(false)
437 #ifndef NDEBUG
438             , m_isInPlacedTree(false)
439 #endif
440         {
441             ASSERT(type != NoFloat);
442             if (type == LeftFloat)
443                 m_type = FloatLeft;
444             else if (type == RightFloat)
445                 m_type = FloatRight;
446             else if (type == PositionedFloat)
447                 m_type = FloatPositioned;
448         }
449
450         FloatingObject(Type type, const LayoutRect& frameRect)
451             : m_renderer(0)
452             , m_originatingLine(0)
453             , m_frameRect(frameRect)
454             , m_paginationStrut(0)
455             , m_type(type)
456             , m_shouldPaint(type != FloatPositioned)
457             , m_isDescendant(false)
458             , m_isPlaced(true)
459 #ifndef NDEBUG
460             , m_isInPlacedTree(false)
461 #endif
462         {
463         }
464
465         Type type() const { return static_cast<Type>(m_type); }
466         RenderBox* renderer() const { return m_renderer; }
467         
468         bool isPlaced() const { return m_isPlaced; }
469         void setIsPlaced(bool placed = true) { m_isPlaced = placed; }
470
471         LayoutUnit x() const { ASSERT(isPlaced()); return m_frameRect.x(); }
472         LayoutUnit maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); }
473         LayoutUnit y() const { ASSERT(isPlaced()); return m_frameRect.y(); }
474         LayoutUnit maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); }
475         LayoutUnit width() const { return m_frameRect.width(); }
476         LayoutUnit height() const { return m_frameRect.height(); }
477
478         void setX(LayoutUnit x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); }
479         void setY(LayoutUnit y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); }
480         void setWidth(LayoutUnit width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); }
481         void setHeight(LayoutUnit height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); }
482
483         const LayoutRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; }
484         void setFrameRect(const LayoutRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; }
485
486 #ifndef NDEBUG
487         bool isInPlacedTree() const { return m_isInPlacedTree; }
488         void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; }
489 #endif
490
491         RenderBox* m_renderer;
492         RootInlineBox* m_originatingLine;
493         LayoutRect m_frameRect;
494         int m_paginationStrut;
495         unsigned m_type : 3; // Type (left/right aligned or positioned)
496         bool m_shouldPaint : 1;
497         bool m_isDescendant : 1;
498         bool m_isPlaced : 1;
499 #ifndef NDEBUG
500         bool m_isInPlacedTree : 1;
501 #endif
502     };
503
504     LayoutPoint flipFloatForWritingMode(const FloatingObject*, const LayoutPoint&) const;
505
506     LayoutUnit logicalTopForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->y() : child->x(); }
507     LayoutUnit logicalBottomForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->maxY() : child->maxX(); }
508     LayoutUnit logicalLeftForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->x() : child->y(); }
509     LayoutUnit logicalRightForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->maxX() : child->maxY(); }
510     LayoutUnit logicalWidthForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->width() : child->height(); }
511     void setLogicalTopForFloat(FloatingObject* child, LayoutUnit logicalTop)
512     {
513         if (isHorizontalWritingMode())
514             child->setY(logicalTop);
515         else
516             child->setX(logicalTop);
517     }
518     void setLogicalLeftForFloat(FloatingObject* child, LayoutUnit logicalLeft)
519     {
520         if (isHorizontalWritingMode())
521             child->setX(logicalLeft);
522         else
523             child->setY(logicalLeft);
524     }
525     void setLogicalHeightForFloat(FloatingObject* child, LayoutUnit logicalHeight)
526     {
527         if (isHorizontalWritingMode())
528             child->setHeight(logicalHeight);
529         else
530             child->setWidth(logicalHeight);
531     }
532     void setLogicalWidthForFloat(FloatingObject* child, LayoutUnit logicalWidth)
533     {
534         if (isHorizontalWritingMode())
535             child->setWidth(logicalWidth);
536         else
537             child->setHeight(logicalWidth);
538     }
539
540     LayoutUnit xPositionForFloatIncludingMargin(const FloatingObject* child) const
541     {
542         if (isHorizontalWritingMode())
543             return child->x() + child->renderer()->marginLeft();
544         else
545             return child->x() + marginBeforeForChild(child->renderer());
546     }
547         
548     LayoutUnit yPositionForFloatIncludingMargin(const FloatingObject* child) const
549     {
550         if (isHorizontalWritingMode())
551             return child->y() + marginBeforeForChild(child->renderer());
552         else
553             return child->y() + child->renderer()->marginTop();
554     }
555
556     // The following functions' implementations are in RenderBlockLineLayout.cpp.
557     typedef std::pair<RenderText*, LazyLineBreakIterator> LineBreakIteratorInfo;
558     class LineBreaker {
559     public:
560         LineBreaker(RenderBlock* block)
561             : m_block(block)
562         {
563             reset();
564         }
565
566         InlineIterator nextLineBreak(InlineBidiResolver&, LineInfo&, LineBreakIteratorInfo&, FloatingObject* lastFloatFromPreviousLine, unsigned consecutiveHyphenatedLines);
567
568         bool lineWasHyphenated() { return m_hyphenated; }
569         const Vector<RenderBox*>& positionedObjects() { return m_positionedObjects; }
570         EClear clear() { return m_clear; }
571     private:
572         void reset();
573         
574         void skipTrailingWhitespace(InlineIterator&, const LineInfo&);
575         void skipLeadingWhitespace(InlineBidiResolver&, const LineInfo&, FloatingObject* lastFloatFromPreviousLine, LineWidth&);
576         
577         RenderBlock* m_block;
578         bool m_hyphenated;
579         EClear m_clear;
580         Vector<RenderBox*> m_positionedObjects;
581     };
582
583     void checkFloatsInCleanLine(RootInlineBox*, Vector<FloatWithRect>&, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat);
584     RootInlineBox* determineStartPosition(LineLayoutState&, InlineBidiResolver&);
585     void determineEndPosition(LineLayoutState&, RootInlineBox* startBox, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus);
586     bool matchedEndLine(LineLayoutState&, const InlineBidiResolver&, const InlineIterator& endLineStart, const BidiStatus& endLineStatus);
587
588     RootInlineBox* constructLine(BidiRunList<BidiRun>&, const LineInfo&);
589     InlineFlowBox* createLineBoxes(RenderObject*, const LineInfo&, InlineBox* childBox);
590
591     void setMarginsForRubyRun(BidiRun*, RenderRubyRun*, RenderObject*, const LineInfo&);
592
593     void computeInlineDirectionPositionsForLine(RootInlineBox*, const LineInfo&, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap&, VerticalPositionCache&);
594     void computeBlockDirectionPositionsForLine(RootInlineBox*, BidiRun*, GlyphOverflowAndFallbackFontsMap&, VerticalPositionCache&);
595     void deleteEllipsisLineBoxes();
596     void checkLinesForTextOverflow();
597
598     // Positions new floats and also adjust all floats encountered on the line if any of them
599     // have to move to the next page/column.
600     bool positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineWidth&);
601     void appendFloatingObjectToLastLine(FloatingObject*);
602
603     // End of functions defined in RenderBlockLineLayout.cpp.
604
605     void paintFloats(PaintInfo&, const LayoutPoint&, bool preservePhase = false);
606     void paintContents(PaintInfo&, const LayoutPoint&);
607     void paintColumnContents(PaintInfo&, const LayoutPoint&, bool paintFloats = false);
608     void paintColumnRules(PaintInfo&, const LayoutPoint&);
609     void paintChildren(PaintInfo&, const LayoutPoint&);
610     void paintEllipsisBoxes(PaintInfo&, const LayoutPoint&);
611     void paintSelection(PaintInfo&, const LayoutPoint&);
612     void paintCaret(PaintInfo&, const LayoutPoint&, CaretType);
613
614     FloatingObject* insertFloatingObject(RenderBox*);
615     void removeFloatingObject(RenderBox*);
616     void removeFloatingObjectsBelow(FloatingObject*, int logicalOffset);
617     
618     // Called from lineWidth, to position the floats added in the last line.
619     // Returns true if and only if it has positioned any floats.
620     bool positionNewFloats();
621
622     bool hasPositionedFloats() const { return m_hasPositionedFloats; }
623     void addPositionedFloats();
624     bool positionedFloatsNeedRelayout();
625
626     void clearFloats(BlockLayoutPass);
627     LayoutUnit getClearDelta(RenderBox* child, LayoutUnit yPos);
628
629     virtual bool avoidsFloats() const;
630
631     bool hasOverhangingFloats() { return parent() && !hasColumns() && containsFloats() && lowestFloatLogicalBottomIncludingPositionedFloats() > logicalHeight(); }
632     bool hasOverhangingFloat(RenderBox*);
633     void addIntrudingFloats(RenderBlock* prev, LayoutUnit xoffset, LayoutUnit yoffset);
634     LayoutUnit addOverhangingFloats(RenderBlock* child, bool makeChildPaintOtherFloats);
635
636     LayoutUnit lowestFloatLogicalBottom() const { return lowestFloatLogicalBottom(FloatingObject::FloatLeftRight); }
637     LayoutUnit lowestFloatLogicalBottomIncludingPositionedFloats() const { return lowestFloatLogicalBottom(FloatingObject::FloatAll); }
638     LayoutUnit lowestFloatLogicalBottom(FloatingObject::Type) const;
639     LayoutUnit nextFloatLogicalBottomBelow(LayoutUnit) const;
640     
641     virtual bool hitTestColumns(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
642     virtual bool hitTestContents(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
643     bool hitTestFloats(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset);
644
645     virtual bool isPointInOverflowControl(HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset);
646
647     void computeInlinePreferredLogicalWidths();
648     void computeBlockPreferredLogicalWidths();
649
650     // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
651     // children.
652     virtual RenderBlock* firstLineBlock() const;
653
654     virtual LayoutRect rectWithOutlineForRepaint(RenderBoxModelObject* repaintContainer, LayoutUnit outlineWidth) const;
655     virtual RenderStyle* outlineStyleForRepaint() const;
656     
657     virtual RenderObject* hoverAncestor() const;
658     virtual void updateDragState(bool dragOn);
659     virtual void childBecameNonInline(RenderObject* child);
660
661     virtual LayoutRect selectionRectForRepaint(RenderBoxModelObject* repaintContainer, bool /*clipToVisibleContent*/)
662     {
663         return selectionGapRectsForRepaint(repaintContainer);
664     }
665     virtual bool shouldPaintSelectionGaps() const;
666     bool isSelectionRoot() const;
667     GapRects selectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
668                            LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo* = 0);
669     GapRects inlineSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
670                                  LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo*);
671     GapRects blockSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
672                                 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo*);
673     LayoutRect blockSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
674                                  LayoutUnit lastLogicalTop, LayoutUnit lastLogicalLeft, LayoutUnit lastLogicalRight, LayoutUnit logicalBottom, const PaintInfo*);
675     LayoutUnit logicalLeftSelectionOffset(RenderBlock* rootBlock, LayoutUnit position);
676     LayoutUnit logicalRightSelectionOffset(RenderBlock* rootBlock, LayoutUnit position);
677     
678     virtual void absoluteRects(Vector<LayoutRect>&, const LayoutPoint& accumulatedOffset);
679     virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed);
680
681     LayoutUnit desiredColumnWidth() const;
682     unsigned desiredColumnCount() const;
683     void setDesiredColumnCountAndWidth(int count, LayoutUnit width);
684
685     void paintContinuationOutlines(PaintInfo&, const LayoutPoint&);
686
687     virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0);
688
689     void adjustPointToColumnContents(LayoutPoint&) const;
690     void adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const; // Helper function for borderFitAdjust
691
692     void markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest = 0);
693
694     void newLine(EClear);
695
696     Position positionForBox(InlineBox*, bool start = true) const;
697     VisiblePosition positionForPointWithInlineChildren(const LayoutPoint&);
698
699     // Adjust from painting offsets to the local coords of this renderer
700     void offsetForContents(LayoutPoint&) const;
701
702     void calcColumnWidth();
703     bool layoutColumns(bool hasSpecifiedPageLogicalHeight, int pageLogicalHeight, LayoutStateMaintainer&);
704     void makeChildrenAnonymousColumnBlocks(RenderObject* beforeChild, RenderBlock* newBlockBox, RenderObject* newChild);
705
706     bool expandsToEncloseOverhangingFloats() const;
707
708     void updateScrollInfoAfterLayout();
709
710     RenderObject* splitAnonymousBlocksAroundChild(RenderObject* beforeChild);
711     void splitBlocks(RenderBlock* fromBlock, RenderBlock* toBlock, RenderBlock* middleBlock,
712                      RenderObject* beforeChild, RenderBoxModelObject* oldCont);
713     void splitFlow(RenderObject* beforeChild, RenderBlock* newBlockBox,
714                    RenderObject* newChild, RenderBoxModelObject* oldCont);
715     RenderBlock* clone() const;
716     RenderBlock* continuationBefore(RenderObject* beforeChild);
717     RenderBlock* containingColumnsBlock(bool allowAnonymousColumnBlock = true);
718     RenderBlock* columnsBlockForSpanningElement(RenderObject* newChild);
719
720     class MarginInfo {
721         // Collapsing flags for whether we can collapse our margins with our children's margins.
722         bool m_canCollapseWithChildren : 1;
723         bool m_canCollapseMarginBeforeWithChildren : 1;
724         bool m_canCollapseMarginAfterWithChildren : 1;
725
726         // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
727         // margins in our container.  Table cells and the body are the common examples. We
728         // also have a custom style property for Safari RSS to deal with TypePad blog articles.
729         bool m_quirkContainer : 1;
730
731         // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.  
732         // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
733         // always be collapsing with one another.  This variable can remain set to true through multiple iterations 
734         // as long as we keep encountering self-collapsing blocks.
735         bool m_atBeforeSideOfBlock : 1;
736
737         // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
738         bool m_atAfterSideOfBlock : 1;
739
740         // These variables are used to detect quirky margins that we need to collapse away (in table cells
741         // and in the body element).
742         bool m_marginBeforeQuirk : 1;
743         bool m_marginAfterQuirk : 1;
744         bool m_determinedMarginBeforeQuirk : 1;
745
746         // These flags track the previous maximal positive and negative margins.
747         LayoutUnit m_positiveMargin;
748         LayoutUnit m_negativeMargin;
749
750     public:
751         MarginInfo(RenderBlock*, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding);
752
753         void setAtBeforeSideOfBlock(bool b) { m_atBeforeSideOfBlock = b; }
754         void setAtAfterSideOfBlock(bool b) { m_atAfterSideOfBlock = b; }
755         void clearMargin() { m_positiveMargin = m_negativeMargin = 0; }
756         void setMarginBeforeQuirk(bool b) { m_marginBeforeQuirk = b; }
757         void setMarginAfterQuirk(bool b) { m_marginAfterQuirk = b; }
758         void setDeterminedMarginBeforeQuirk(bool b) { m_determinedMarginBeforeQuirk = b; }
759         void setPositiveMargin(int p) { m_positiveMargin = p; }
760         void setNegativeMargin(int n) { m_negativeMargin = n; }
761         void setPositiveMarginIfLarger(int p) { if (p > m_positiveMargin) m_positiveMargin = p; }
762         void setNegativeMarginIfLarger(int n) { if (n > m_negativeMargin) m_negativeMargin = n; }
763
764         void setMargin(int p, int n) { m_positiveMargin = p; m_negativeMargin = n; }
765
766         bool atBeforeSideOfBlock() const { return m_atBeforeSideOfBlock; }
767         bool canCollapseWithMarginBefore() const { return m_atBeforeSideOfBlock && m_canCollapseMarginBeforeWithChildren; }
768         bool canCollapseWithMarginAfter() const { return m_atAfterSideOfBlock && m_canCollapseMarginAfterWithChildren; }
769         bool canCollapseMarginBeforeWithChildren() const { return m_canCollapseMarginBeforeWithChildren; }
770         bool canCollapseMarginAfterWithChildren() const { return m_canCollapseMarginAfterWithChildren; }
771         bool quirkContainer() const { return m_quirkContainer; }
772         bool determinedMarginBeforeQuirk() const { return m_determinedMarginBeforeQuirk; }
773         bool marginBeforeQuirk() const { return m_marginBeforeQuirk; }
774         bool marginAfterQuirk() const { return m_marginAfterQuirk; }
775         LayoutUnit positiveMargin() const { return m_positiveMargin; }
776         LayoutUnit negativeMargin() const { return m_negativeMargin; }
777         LayoutUnit margin() const { return m_positiveMargin - m_negativeMargin; }
778     };
779
780     void layoutBlockChild(RenderBox* child, MarginInfo&, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom);
781     void adjustPositionedBlock(RenderBox* child, const MarginInfo&);
782     void adjustFloatingBlock(const MarginInfo&);
783     bool handleSpecialChild(RenderBox* child, const MarginInfo&);
784     bool handleFloatingChild(RenderBox* child, const MarginInfo&);
785     bool handlePositionedChild(RenderBox* child, const MarginInfo&);
786     bool handleRunInChild(RenderBox* child);
787     LayoutUnit collapseMargins(RenderBox* child, MarginInfo&);
788     LayoutUnit clearFloatsIfNeeded(RenderBox* child, MarginInfo&, int oldTopPosMargin, int oldTopNegMargin, int yPos);
789     LayoutUnit estimateLogicalTopPosition(RenderBox* child, const MarginInfo&);
790     void determineLogicalLeftPositionForChild(RenderBox* child);
791     void handleAfterSideOfBlock(LayoutUnit top, LayoutUnit bottom, MarginInfo&);
792     void setCollapsedBottomMargin(const MarginInfo&);
793     // End helper functions and structs used by layoutBlockChildren.
794
795     // Helper function for layoutInlineChildren()
796     RootInlineBox* createLineBoxesFromBidiRuns(BidiRunList<BidiRun>&, const InlineIterator& end, LineInfo&, VerticalPositionCache&, BidiRun* trailingSpaceRun);
797     void layoutRunsAndFloats(LineLayoutState&, bool hasInlineChild);
798     void layoutRunsAndFloatsInRange(LineLayoutState&, InlineBidiResolver&, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines);
799     void linkToEndLineIfNeeded(LineLayoutState&);
800     static void repaintDirtyFloats(Vector<FloatWithRect>& floats);
801
802     // Pagination routines.
803     
804     // Returns the logicalOffset at the top of the next page. If the offset passed in is already at the top of the current page,
805     // then nextPageLogicalTopExcludingBoundaryPoint will still move to the top of the next page. nextPageLogicalTopIncludingBoundaryPoint
806     // will not.
807     //
808     // For a page height of 800px, the first function will return 800 if the value passed in is 0. The second function will simply return 0.
809     LayoutUnit nextPageLogicalTopExcludingBoundaryPoint(LayoutUnit logicalOffset) const;
810     LayoutUnit nextPageLogicalTopIncludingBoundaryPoint(LayoutUnit logicalOffset) const;
811
812     int applyBeforeBreak(RenderBox* child, int logicalOffset); // If the child has a before break, then return a new yPos that shifts to the top of the next page/column.
813     int applyAfterBreak(RenderBox* child, int logicalOffset, MarginInfo& marginInfo); // If the child has an after break, then return a new offset that shifts to the top of the next page/column.
814     LayoutUnit adjustForUnsplittableChild(RenderBox* child, LayoutUnit logicalOffset, bool includeMargins = false); // If the child is unsplittable and can't fit on the current page, return the top of the next page/column.
815     void adjustLinePositionForPagination(RootInlineBox*, LayoutUnit& deltaOffset); // Computes a deltaOffset value that put a line at the top of the next page if it doesn't fit on the current page.
816
817     struct FloatingObjectHashFunctions {
818         static unsigned hash(FloatingObject* key) { return DefaultHash<RenderBox*>::Hash::hash(key->m_renderer); }
819         static bool equal(FloatingObject* a, FloatingObject* b) { return a->m_renderer == b->m_renderer; }
820         static const bool safeToCompareToEmptyOrDeleted = true;
821     };
822     struct FloatingObjectHashTranslator {
823         static unsigned hash(RenderBox* key) { return DefaultHash<RenderBox*>::Hash::hash(key); }
824         static bool equal(FloatingObject* a, RenderBox* b) { return a->m_renderer == b; }
825     };
826     typedef ListHashSet<FloatingObject*, 4, FloatingObjectHashFunctions> FloatingObjectSet;
827     typedef FloatingObjectSet::const_iterator FloatingObjectSetIterator;
828     typedef PODInterval<LayoutUnit, FloatingObject*> FloatingObjectInterval;
829     typedef PODIntervalTree<LayoutUnit, FloatingObject*> FloatingObjectTree;
830     
831     template <FloatingObject::Type FloatTypeValue>
832     class FloatIntervalSearchAdapter {
833     public:
834         typedef FloatingObjectInterval IntervalType;
835         
836         FloatIntervalSearchAdapter(const RenderBlock* renderer, LayoutUnit value, LayoutUnit& offset, LayoutUnit* heightRemaining)
837             : m_renderer(renderer)
838             , m_value(value)
839             , m_offset(offset)
840             , m_heightRemaining(heightRemaining)
841         {
842         }
843         
844         inline LayoutUnit lowValue() const { return m_value; }
845         inline LayoutUnit highValue() const { return m_value; }
846         void collectIfNeeded(const IntervalType&) const;
847
848     private:
849         const RenderBlock* m_renderer;
850         LayoutUnit m_value;
851         LayoutUnit& m_offset;
852         LayoutUnit* m_heightRemaining;
853     };
854
855     class FloatingObjects {
856     public:
857         FloatingObjects(bool horizontalWritingMode)
858             : m_placedFloatsTree(UninitializedTree)
859             , m_leftObjectsCount(0)
860             , m_rightObjectsCount(0)
861             , m_positionedObjectsCount(0)
862             , m_horizontalWritingMode(horizontalWritingMode)
863         {
864         }
865
866         void clear();
867         void add(FloatingObject*);
868         void remove(FloatingObject*);
869         void addPlacedObject(FloatingObject*);
870         void removePlacedObject(FloatingObject*);
871         void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; }
872
873         bool hasLeftObjects() const { return m_leftObjectsCount > 0; }
874         bool hasRightObjects() const { return m_rightObjectsCount > 0; }
875         bool hasPositionedObjects() const { return m_positionedObjectsCount > 0; }
876         const FloatingObjectSet& set() const { return m_set; }
877         const FloatingObjectTree& placedFloatsTree()
878         {
879             computePlacedFloatsTreeIfNeeded();
880             return m_placedFloatsTree; 
881         }
882     private:
883         void computePlacedFloatsTree();
884         inline void computePlacedFloatsTreeIfNeeded()
885         {
886             if (!m_placedFloatsTree.isInitialized())
887                 computePlacedFloatsTree();
888         }
889         void increaseObjectsCount(FloatingObject::Type);
890         void decreaseObjectsCount(FloatingObject::Type);
891         FloatingObjectInterval intervalForFloatingObject(FloatingObject*);
892
893         FloatingObjectSet m_set;
894         FloatingObjectTree m_placedFloatsTree;
895         unsigned m_leftObjectsCount;
896         unsigned m_rightObjectsCount;
897         unsigned m_positionedObjectsCount;
898         bool m_horizontalWritingMode;
899     };
900     OwnPtr<FloatingObjects> m_floatingObjects;
901     
902     typedef PositionedObjectsListHashSet::const_iterator Iterator;
903     OwnPtr<PositionedObjectsListHashSet> m_positionedObjects;
904
905     // Allocated only when some of these fields have non-default values
906     struct RenderBlockRareData {
907         WTF_MAKE_NONCOPYABLE(RenderBlockRareData); WTF_MAKE_FAST_ALLOCATED;
908     public:
909         RenderBlockRareData(const RenderBlock* block) 
910             : m_margins(positiveMarginBeforeDefault(block), negativeMarginBeforeDefault(block), positiveMarginAfterDefault(block), negativeMarginAfterDefault(block))
911             , m_paginationStrut(0)
912             , m_pageLogicalOffset(0)
913         { 
914         }
915
916         static int positiveMarginBeforeDefault(const RenderBlock* block)
917         { 
918             return std::max<LayoutUnit>(block->marginBefore(), 0);
919         }
920         
921         static int negativeMarginBeforeDefault(const RenderBlock* block)
922         { 
923             return std::max<LayoutUnit>(-block->marginBefore(), 0);
924         }
925         static int positiveMarginAfterDefault(const RenderBlock* block)
926         {
927             return std::max<LayoutUnit>(block->marginAfter(), 0);
928         }
929         static int negativeMarginAfterDefault(const RenderBlock* block)
930         {
931             return std::max<LayoutUnit>(-block->marginAfter(), 0);
932         }
933         
934         MarginValues m_margins;
935         int m_paginationStrut;
936         int m_pageLogicalOffset;
937      };
938
939     OwnPtr<RenderBlockRareData> m_rareData;
940
941     RenderObjectChildList m_children;
942     RenderLineBoxList m_lineBoxes;   // All of the root line boxes created for this block flow.  For example, <div>Hello<br>world.</div> will have two total lines for the <div>.
943
944     mutable signed m_lineHeight : 30;
945     bool m_beingDestroyed : 1;
946     bool m_hasPositionedFloats : 1;
947
948     // RenderRubyBase objects need to be able to split and merge, moving their children around
949     // (calling moveChildTo, moveAllChildrenTo, and makeChildrenNonInline).
950     friend class RenderRubyBase;
951     friend class LineWidth; // Needs to know FloatingObject
952
953 private:
954     // Used to store state between styleWillChange and styleDidChange
955     static bool s_canPropagateFloatIntoSibling;
956 };
957
958 inline RenderBlock* toRenderBlock(RenderObject* object)
959
960     ASSERT(!object || object->isRenderBlock());
961     return static_cast<RenderBlock*>(object);
962 }
963
964 inline const RenderBlock* toRenderBlock(const RenderObject* object)
965
966     ASSERT(!object || object->isRenderBlock());
967     return static_cast<const RenderBlock*>(object);
968 }
969
970 // This will catch anyone doing an unnecessary cast.
971 void toRenderBlock(const RenderBlock*);
972
973 #ifndef NDEBUG
974 // These structures are used by PODIntervalTree for debugging purposes.
975 template <> struct ValueToString<int> {
976     static String string(const int value);
977 };
978 template<> struct ValueToString<RenderBlock::FloatingObject*> {
979     static String string(const RenderBlock::FloatingObject*);
980 };
981 #endif
982
983 } // namespace WebCore
984
985 #endif // RenderBlock_h