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.
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.
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.
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.
27 #include "PODIntervalTree.h"
28 #include "RenderBox.h"
29 #include "RenderLineBoxList.h"
30 #include "RootInlineBox.h"
32 #include <wtf/OwnPtr.h>
33 #include <wtf/ListHashSet.h>
40 class LayoutStateMaintainer;
41 class LazyLineBreakIterator;
42 class LineLayoutState;
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;
58 enum CaretType { CursorCaret, DragCaret };
61 DefaultTextRunFlags = 0,
62 RespectDirection = 1 << 0,
63 RespectDirectionOverride = 1 << 1
66 typedef unsigned TextRunFlags;
68 class RenderBlock : public RenderBox {
70 friend class LineLayoutState;
72 // Used by the PODIntervalTree for debugging the FloatingObject.
73 template <class> friend struct ValueToString;
77 virtual ~RenderBlock();
79 const RenderObjectChildList* children() const { return &m_children; }
80 RenderObjectChildList* children() { return &m_children; }
82 bool beingDestroyed() const { return m_beingDestroyed; }
84 // These two functions are overridden for inline-block.
85 virtual int lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
86 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const;
88 RenderLineBoxList* lineBoxes() { return &m_lineBoxes; }
89 const RenderLineBoxList* lineBoxes() const { return &m_lineBoxes; }
91 InlineFlowBox* firstLineBox() const { return m_lineBoxes.firstLineBox(); }
92 InlineFlowBox* lastLineBox() const { return m_lineBoxes.lastLineBox(); }
94 void deleteLineBoxTree();
96 virtual void addChild(RenderObject* newChild, RenderObject* beforeChild = 0);
97 virtual void removeChild(RenderObject*);
99 enum BlockLayoutPass { NormalLayoutPass, PositionedFloatLayoutPass };
100 virtual void layoutBlock(bool relayoutChildren, int pageLogicalHeight = 0, BlockLayoutPass = NormalLayoutPass);
102 void insertPositionedObject(RenderBox*);
103 void removePositionedObject(RenderBox*);
104 void removePositionedObjects(RenderBlock*);
106 typedef ListHashSet<RenderBox*, 4> PositionedObjectsListHashSet;
107 PositionedObjectsListHashSet* positionedObjects() const { return m_positionedObjects.get(); }
109 void addPercentHeightDescendant(RenderBox*);
110 static void removePercentHeightDescendant(RenderBox*);
111 HashSet<RenderBox*>* percentHeightDescendants() const;
113 static bool hasPercentHeightDescendant(RenderBox*);
116 RootInlineBox* createAndAppendRootInlineBox();
118 bool generatesLineBoxesForInlineChild(RenderObject*);
120 void markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove = 0, bool inLayout = true);
121 void markSiblingsWithFloatsForLayout();
122 void markPositionedObjectsForLayout();
123 virtual void markForPaginationRelayoutIfNeeded();
125 bool containsFloats() { return m_floatingObjects && !m_floatingObjects->set().isEmpty(); }
126 bool containsFloat(RenderBox*);
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); }
133 virtual VisiblePosition positionForPoint(const LayoutPoint&);
135 // Block flows subclass availableWidth to handle multi column layout (shrinking the width available to children when laying out.)
136 virtual LayoutUnit availableLogicalWidth() const;
138 LayoutPoint flipForWritingModeIncludingColumns(const LayoutPoint&) const;
139 void adjustStartEdgeForWritingModeIncludingColumns(LayoutRect&) const;
141 RootInlineBox* firstRootBox() const { return static_cast<RootInlineBox*>(firstLineBox()); }
142 RootInlineBox* lastRootBox() const { return static_cast<RootInlineBox*>(lastLineBox()); }
144 bool containsNonZeroBidiLevel() const;
146 GapRects selectionGapRectsForRepaint(RenderBoxModelObject* repaintContainer);
147 LayoutRect logicalLeftSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
148 RenderObject* selObj, LayoutUnit logicalLeft, LayoutUnit logicalTop, LayoutUnit logicalHeight, const PaintInfo*);
149 LayoutRect logicalRightSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
150 RenderObject* selObj, LayoutUnit logicalRight, LayoutUnit logicalTop, LayoutUnit logicalHeight, const PaintInfo*);
151 void getSelectionGapInfo(SelectionState, bool& leftGap, bool& rightGap);
152 LayoutRect logicalRectToPhysicalRect(const LayoutPoint& physicalPosition, const LayoutRect& logicalRect);
154 // Helper methods for computing line counts and heights for line counts.
155 RootInlineBox* lineAtIndex(int);
157 int heightForLineCount(int);
158 void clearTruncation();
160 void adjustRectForColumns(LayoutRect&) const;
161 virtual void adjustForColumns(LayoutSize&, const LayoutPoint&) const;
163 void addContinuationWithOutline(RenderInline*);
164 bool paintsContinuationOutline(RenderInline*);
166 virtual RenderBoxModelObject* virtualContinuation() const { return continuation(); }
167 bool isAnonymousBlockContinuation() const { return continuation() && isAnonymousBlock(); }
168 RenderInline* inlineElementContinuation() const;
169 RenderBlock* blockElementContinuation() const;
171 using RenderBoxModelObject::continuation;
172 using RenderBoxModelObject::setContinuation;
174 // This function is a convenience helper for creating an anonymous block that inherits its
175 // style from this RenderBlock.
176 RenderBlock* createAnonymousBlock(bool isFlexibleBox = false) const;
177 RenderBlock* createAnonymousColumnsBlock() const;
178 RenderBlock* createAnonymousColumnSpanBlock() const;
179 RenderBlock* createAnonymousBlockWithSameTypeAs(RenderBlock* otherAnonymousBlock) const;
181 static void appendRunsForObject(BidiRunList<BidiRun>&, int start, int end, RenderObject*, InlineBidiResolver&);
183 static TextRun constructTextRun(RenderObject* context, const Font&, const String&, RenderStyle*,
184 TextRun::ExpansionBehavior = TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, TextRunFlags = DefaultTextRunFlags);
186 static TextRun constructTextRun(RenderObject* context, const Font&, const UChar*, int length, RenderStyle*,
187 TextRun::ExpansionBehavior = TextRun::AllowTrailingExpansion | TextRun::ForbidLeadingExpansion, TextRunFlags = DefaultTextRunFlags);
189 ColumnInfo* columnInfo() const;
190 int columnGap() const;
192 // These two functions take the ColumnInfo* to avoid repeated lookups of the info in the global HashMap.
193 unsigned columnCount(ColumnInfo*) const;
194 LayoutRect columnRectAt(ColumnInfo*, unsigned) const;
196 int paginationStrut() const { return m_rareData ? m_rareData->m_paginationStrut : 0; }
197 void setPaginationStrut(int);
199 // The page logical offset is the object's offset from the top of the page in the page progression
200 // direction (so an x-offset in vertical text and a y-offset for horizontal text).
201 int pageLogicalOffset() const { return m_rareData ? m_rareData->m_pageLogicalOffset : 0; }
202 void setPageLogicalOffset(int);
204 // Accessors for logical width/height and margins in the containing block's block-flow direction.
205 enum ApplyLayoutDeltaMode { ApplyLayoutDelta, DoNotApplyLayoutDelta };
206 int logicalWidthForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->width() : child->height(); }
207 int logicalHeightForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->height() : child->width(); }
208 int logicalTopForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->y() : child->x(); }
209 int logicalLeftForChild(RenderBox* child) { return isHorizontalWritingMode() ? child->x() : child->y(); }
210 void setLogicalLeftForChild(RenderBox* child, int logicalLeft, ApplyLayoutDeltaMode = DoNotApplyLayoutDelta);
211 void setLogicalTopForChild(RenderBox* child, int logicalTop, ApplyLayoutDeltaMode = DoNotApplyLayoutDelta);
212 LayoutUnit marginBeforeForChild(RenderBoxModelObject* child) const;
213 LayoutUnit marginAfterForChild(RenderBoxModelObject* child) const;
214 LayoutUnit marginStartForChild(RenderBoxModelObject* child) const;
215 LayoutUnit marginEndForChild(RenderBoxModelObject* child) const;
216 LayoutUnit marginLogicalLeftForChild(RenderBoxModelObject* child) const;
217 LayoutUnit marginLogicalRightForChild(RenderBoxModelObject* child) const;
218 void setMarginStartForChild(RenderBox* child, LayoutUnit);
219 void setMarginEndForChild(RenderBox* child, LayoutUnit);
220 void setMarginBeforeForChild(RenderBox* child, LayoutUnit);
221 void setMarginAfterForChild(RenderBox* child, LayoutUnit);
222 int collapsedMarginBeforeForChild(RenderBox* child) const;
223 int collapsedMarginAfterForChild(RenderBox* child) const;
225 virtual void updateFirstLetter();
229 MarginValues(int beforePos, int beforeNeg, int afterPos, int afterNeg)
230 : m_positiveMarginBefore(beforePos)
231 , m_negativeMarginBefore(beforeNeg)
232 , m_positiveMarginAfter(afterPos)
233 , m_negativeMarginAfter(afterNeg)
236 int positiveMarginBefore() const { return m_positiveMarginBefore; }
237 int negativeMarginBefore() const { return m_negativeMarginBefore; }
238 int positiveMarginAfter() const { return m_positiveMarginAfter; }
239 int negativeMarginAfter() const { return m_negativeMarginAfter; }
241 void setPositiveMarginBefore(int pos) { m_positiveMarginBefore = pos; }
242 void setNegativeMarginBefore(int neg) { m_negativeMarginBefore = neg; }
243 void setPositiveMarginAfter(int pos) { m_positiveMarginAfter = pos; }
244 void setNegativeMarginAfter(int neg) { m_negativeMarginAfter = neg; }
247 int m_positiveMarginBefore;
248 int m_negativeMarginBefore;
249 int m_positiveMarginAfter;
250 int m_negativeMarginAfter;
252 MarginValues marginValuesForChild(RenderBox* child);
254 virtual void scrollbarsChanged(bool /*horizontalScrollbarChanged*/, bool /*verticalScrollbarChanged*/) { };
256 int logicalRightOffsetForContent() const { return isHorizontalWritingMode() ? borderLeft() + paddingLeft() + availableLogicalWidth() : borderTop() + paddingTop() + availableLogicalWidth(); }
257 int logicalLeftOffsetForContent() const { return isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
260 void showLineTreeAndMark(const InlineBox* = 0, const char* = 0, const InlineBox* = 0, const char* = 0, const RenderObject* = 0) const;
264 virtual void willBeDestroyed();
266 // These functions are only used internally to manipulate the render tree structure via remove/insert/appendChildNode.
267 // Since they are typically called only to move objects around within anonymous blocks (which only have layers in
268 // the case of column spans), the default for fullRemoveInsert is false rather than true.
269 void moveChildTo(RenderBlock* to, RenderObject* child, bool fullRemoveInsert = false)
271 return moveChildTo(to, child, 0, fullRemoveInsert);
273 void moveChildTo(RenderBlock* to, RenderObject* child, RenderObject* beforeChild, bool fullRemoveInsert = false);
274 void moveAllChildrenTo(RenderBlock* to, bool fullRemoveInsert = false)
276 return moveAllChildrenTo(to, 0, fullRemoveInsert);
278 void moveAllChildrenTo(RenderBlock* to, RenderObject* beforeChild, bool fullRemoveInsert = false)
280 return moveChildrenTo(to, firstChild(), 0, beforeChild, fullRemoveInsert);
282 // Move all of the kids from |startChild| up to but excluding |endChild|. 0 can be passed as the endChild to denote
283 // that all the kids from |startChild| onwards should be added.
284 void moveChildrenTo(RenderBlock* to, RenderObject* startChild, RenderObject* endChild, bool fullRemoveInsert = false)
286 return moveChildrenTo(to, startChild, endChild, 0, fullRemoveInsert);
288 void moveChildrenTo(RenderBlock* to, RenderObject* startChild, RenderObject* endChild, RenderObject* beforeChild, bool fullRemoveInsert = false);
290 int maxPositiveMarginBefore() const { return m_rareData ? m_rareData->m_margins.positiveMarginBefore() : RenderBlockRareData::positiveMarginBeforeDefault(this); }
291 int maxNegativeMarginBefore() const { return m_rareData ? m_rareData->m_margins.negativeMarginBefore() : RenderBlockRareData::negativeMarginBeforeDefault(this); }
292 int maxPositiveMarginAfter() const { return m_rareData ? m_rareData->m_margins.positiveMarginAfter() : RenderBlockRareData::positiveMarginAfterDefault(this); }
293 int maxNegativeMarginAfter() const { return m_rareData ? m_rareData->m_margins.negativeMarginAfter() : RenderBlockRareData::negativeMarginAfterDefault(this); }
295 void setMaxMarginBeforeValues(int pos, int neg);
296 void setMaxMarginAfterValues(int pos, int neg);
298 void initMaxMarginValues()
301 m_rareData->m_margins = MarginValues(RenderBlockRareData::positiveMarginBeforeDefault(this) , RenderBlockRareData::negativeMarginBeforeDefault(this),
302 RenderBlockRareData::positiveMarginAfterDefault(this), RenderBlockRareData::negativeMarginAfterDefault(this));
303 m_rareData->m_paginationStrut = 0;
307 virtual void layout();
309 bool layoutPositionedObjects(bool relayoutChildren);
311 virtual void paint(PaintInfo&, const LayoutPoint&);
312 virtual void paintObject(PaintInfo&, const LayoutPoint&);
314 LayoutUnit logicalRightOffsetForLine(LayoutUnit position, LayoutUnit fixedOffset, bool applyTextIndent = true, int* logicalHeightRemaining = 0) const;
315 LayoutUnit logicalLeftOffsetForLine(LayoutUnit position, LayoutUnit fixedOffset, bool applyTextIndent = true, int* logicalHeightRemaining = 0) const;
317 virtual ETextAlign textAlignmentForLine(bool endsWithSoftBreak) const;
318 virtual void adjustInlineDirectionLineBounds(int /* expansionOpportunityCount */, float& /* logicalLeft */, float& /* logicalWidth */) const { }
320 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
322 virtual void computePreferredLogicalWidths();
324 virtual int firstLineBoxBaseline() const;
325 virtual int lastLineBoxBaseline() const;
327 virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&);
329 // Delay update scrollbar until finishDelayRepaint() will be
330 // called. This function is used when a flexbox is laying out its
331 // descendant. If multiple calls are made to startDelayRepaint(),
332 // finishDelayRepaint() will do nothing until finishDelayRepaint()
333 // is called the same number of times.
334 static void startDelayUpdateScrollInfo();
335 static void finishDelayUpdateScrollInfo();
337 virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
338 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
340 virtual bool hasLineIfEmpty() const;
342 bool simplifiedLayout();
343 void simplifiedNormalFlowLayout();
345 void computeOverflow(int oldClientAfterEdge, bool recomputeFloats = false);
346 virtual void addOverflowFromChildren();
347 void addOverflowFromFloats();
348 void addOverflowFromPositionedObjects();
349 void addOverflowFromBlockChildren();
350 void addOverflowFromInlineChildren();
352 virtual void addFocusRingRects(Vector<LayoutRect>&, const LayoutPoint&);
355 // Only used by RenderSVGText, which explicitely overrides RenderBlock::layoutBlock(), do NOT use for anything else.
356 void forceLayoutInlineChildren()
358 int repaintLogicalTop = 0;
359 int repaintLogicalBottom = 0;
360 layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom);
365 virtual RenderObjectChildList* virtualChildren() { return children(); }
366 virtual const RenderObjectChildList* virtualChildren() const { return children(); }
368 virtual const char* renderName() const;
370 virtual bool isRenderBlock() const { return true; }
371 virtual bool isBlockFlow() const { return (!isInline() || isReplaced()) && !isTable(); }
372 virtual bool isInlineBlockOrInlineTable() const { return isInline() && isReplaced(); }
374 void makeChildrenNonInline(RenderObject* insertionPoint = 0);
375 virtual void removeLeftoverAnonymousBlock(RenderBlock* child);
377 virtual void dirtyLinesFromChangedChild(RenderObject* child) { m_lineBoxes.dirtyLinesFromChangedChild(this, child); }
379 void addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild);
380 void addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild);
381 void addChildToAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild);
382 virtual void addChildIgnoringAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild = 0);
384 virtual bool isSelfCollapsingBlock() const;
386 virtual LayoutUnit collapsedMarginBefore() const { return maxPositiveMarginBefore() - maxNegativeMarginBefore(); }
387 virtual LayoutUnit collapsedMarginAfter() const { return maxPositiveMarginAfter() - maxNegativeMarginAfter(); }
389 virtual void repaintOverhangingFloats(bool paintAllDescendants);
391 void layoutBlockChildren(bool relayoutChildren, int& maxFloatLogicalBottom);
392 void layoutInlineChildren(bool relayoutChildren, int& repaintLogicalTop, int& repaintLogicalBottom);
393 BidiRun* handleTrailingSpaces(BidiRunList<BidiRun>&, BidiContext*);
395 virtual void borderFitAdjust(IntRect&) const; // Shrink the box in which the border paints if border-fit is set.
397 virtual void updateBeforeAfterContent(PseudoId);
399 virtual RootInlineBox* createRootInlineBox(); // Subclassed by SVG and Ruby.
401 // Called to lay out the legend for a fieldset or the ruby text of a ruby run.
402 virtual RenderObject* layoutSpecialExcludedChild(bool /*relayoutChildren*/) { return 0; }
404 struct FloatWithRect {
405 FloatWithRect(RenderBox* f)
407 , rect(IntRect(f->x() - f->marginLeft(), f->y() - f->marginTop(), f->width() + f->marginLeft() + f->marginRight(), f->height() + f->marginTop() + f->marginBottom()))
408 , everHadLayout(f->m_everHadLayout)
417 struct FloatingObject {
418 WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED;
420 // FloatLeftRight is a mask to query for both left and right but not positioned.
421 // FloatAll is a mask to query for all types of floats.
422 enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3, FloatPositioned = 4, FloatAll = 7 };
424 FloatingObject(EFloat type)
426 , m_originatingLine(0)
427 , m_paginationStrut(0)
428 , m_shouldPaint(false)
429 , m_isDescendant(false)
432 , m_isInPlacedTree(false)
435 ASSERT(type != NoFloat);
436 if (type == LeftFloat)
438 else if (type == RightFloat)
440 else if (type == PositionedFloat)
441 m_type = FloatPositioned;
444 FloatingObject(Type type, const IntRect& frameRect)
446 , m_originatingLine(0)
447 , m_frameRect(frameRect)
448 , m_paginationStrut(0)
450 , m_shouldPaint(type != FloatPositioned)
451 , m_isDescendant(false)
454 , m_isInPlacedTree(false)
459 Type type() const { return static_cast<Type>(m_type); }
460 RenderBox* renderer() const { return m_renderer; }
462 bool isPlaced() const { return m_isPlaced; }
463 void setIsPlaced(bool placed = true) { m_isPlaced = placed; }
465 int x() const { ASSERT(isPlaced()); return m_frameRect.x(); }
466 int maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); }
467 int y() const { ASSERT(isPlaced()); return m_frameRect.y(); }
468 int maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); }
469 int width() const { return m_frameRect.width(); }
470 int height() const { return m_frameRect.height(); }
472 void setX(int x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); }
473 void setY(int y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); }
474 void setWidth(int width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); }
475 void setHeight(int height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); }
477 const IntRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; }
478 void setFrameRect(const IntRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; }
481 bool isInPlacedTree() const { return m_isInPlacedTree; }
482 void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; }
485 RenderBox* m_renderer;
486 RootInlineBox* m_originatingLine;
488 int m_paginationStrut;
489 unsigned m_type : 3; // Type (left/right aligned or positioned)
490 bool m_shouldPaint : 1;
491 bool m_isDescendant : 1;
494 bool m_isInPlacedTree : 1;
498 IntPoint flipFloatForWritingMode(const FloatingObject*, const IntPoint&) const;
500 int logicalTopForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->y() : child->x(); }
501 int logicalBottomForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->maxY() : child->maxX(); }
502 int logicalLeftForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->x() : child->y(); }
503 int logicalRightForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->maxX() : child->maxY(); }
504 int logicalWidthForFloat(const FloatingObject* child) const { return isHorizontalWritingMode() ? child->width() : child->height(); }
505 void setLogicalTopForFloat(FloatingObject* child, int logicalTop)
507 if (isHorizontalWritingMode())
508 child->setY(logicalTop);
510 child->setX(logicalTop);
512 void setLogicalLeftForFloat(FloatingObject* child, int logicalLeft)
514 if (isHorizontalWritingMode())
515 child->setX(logicalLeft);
517 child->setY(logicalLeft);
519 void setLogicalHeightForFloat(FloatingObject* child, int logicalHeight)
521 if (isHorizontalWritingMode())
522 child->setHeight(logicalHeight);
524 child->setWidth(logicalHeight);
526 void setLogicalWidthForFloat(FloatingObject* child, int logicalWidth)
528 if (isHorizontalWritingMode())
529 child->setWidth(logicalWidth);
531 child->setHeight(logicalWidth);
534 LayoutUnit xPositionForFloatIncludingMargin(const FloatingObject* child) const
536 if (isHorizontalWritingMode())
537 return child->x() + child->renderer()->marginLeft();
539 return child->x() + marginBeforeForChild(child->renderer());
542 LayoutUnit yPositionForFloatIncludingMargin(const FloatingObject* child) const
544 if (isHorizontalWritingMode())
545 return child->y() + marginBeforeForChild(child->renderer());
547 return child->y() + child->renderer()->marginTop();
550 // The following functions' implementations are in RenderBlockLineLayout.cpp.
551 typedef std::pair<RenderText*, LazyLineBreakIterator> LineBreakIteratorInfo;
554 LineBreaker(RenderBlock* block)
560 InlineIterator nextLineBreak(InlineBidiResolver&, LineInfo&, LineBreakIteratorInfo&, FloatingObject* lastFloatFromPreviousLine);
562 bool lineWasHyphenated() { return m_hyphenated; }
563 const Vector<RenderBox*>& positionedObjects() { return m_positionedObjects; }
564 EClear clear() { return m_clear; }
568 void skipTrailingWhitespace(InlineIterator&, const LineInfo&);
569 void skipLeadingWhitespace(InlineBidiResolver&, const LineInfo&, FloatingObject* lastFloatFromPreviousLine, LineWidth&);
571 RenderBlock* m_block;
574 Vector<RenderBox*> m_positionedObjects;
577 void checkFloatsInCleanLine(RootInlineBox*, Vector<FloatWithRect>&, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat);
578 RootInlineBox* determineStartPosition(LineLayoutState&, InlineBidiResolver&);
579 void determineEndPosition(LineLayoutState&, RootInlineBox* startBox, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus);
580 bool matchedEndLine(LineLayoutState&, const InlineBidiResolver&, const InlineIterator& endLineStart, const BidiStatus& endLineStatus);
582 RootInlineBox* constructLine(BidiRunList<BidiRun>&, const LineInfo&);
583 InlineFlowBox* createLineBoxes(RenderObject*, const LineInfo&, InlineBox* childBox);
585 void setMarginsForRubyRun(BidiRun*, RenderRubyRun*, RenderObject*, const LineInfo&);
587 void computeInlineDirectionPositionsForLine(RootInlineBox*, const LineInfo&, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap&, VerticalPositionCache&);
588 void computeBlockDirectionPositionsForLine(RootInlineBox*, BidiRun*, GlyphOverflowAndFallbackFontsMap&, VerticalPositionCache&);
589 void deleteEllipsisLineBoxes();
590 void checkLinesForTextOverflow();
592 // Positions new floats and also adjust all floats encountered on the line if any of them
593 // have to move to the next page/column.
594 bool positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineWidth&);
595 void appendFloatingObjectToLastLine(FloatingObject*);
597 // End of functions defined in RenderBlockLineLayout.cpp.
599 void paintFloats(PaintInfo&, const LayoutPoint&, bool preservePhase = false);
600 void paintContents(PaintInfo&, const LayoutPoint&);
601 void paintColumnContents(PaintInfo&, const LayoutPoint&, bool paintFloats = false);
602 void paintColumnRules(PaintInfo&, const LayoutPoint&);
603 void paintChildren(PaintInfo&, const LayoutPoint&);
604 void paintEllipsisBoxes(PaintInfo&, const LayoutPoint&);
605 void paintSelection(PaintInfo&, const LayoutPoint&);
606 void paintCaret(PaintInfo&, const LayoutPoint&, CaretType);
608 FloatingObject* insertFloatingObject(RenderBox*);
609 void removeFloatingObject(RenderBox*);
610 void removeFloatingObjectsBelow(FloatingObject*, int logicalOffset);
612 // Called from lineWidth, to position the floats added in the last line.
613 // Returns true if and only if it has positioned any floats.
614 bool positionNewFloats();
616 bool hasPositionedFloats() const { return m_hasPositionedFloats; }
617 void addPositionedFloats();
618 bool positionedFloatsNeedRelayout();
620 void clearFloats(BlockLayoutPass);
621 int getClearDelta(RenderBox* child, int yPos);
623 virtual bool avoidsFloats() const;
625 bool hasOverhangingFloats() { return parent() && !hasColumns() && containsFloats() && lowestFloatLogicalBottomIncludingPositionedFloats() > logicalHeight(); }
626 bool hasOverhangingFloat(RenderBox*);
627 void addIntrudingFloats(RenderBlock* prev, int xoffset, int yoffset);
628 int addOverhangingFloats(RenderBlock* child, int xoffset, int yoffset, bool makeChildPaintOtherFloats);
630 int lowestFloatLogicalBottom() const { return lowestFloatLogicalBottom(FloatingObject::FloatLeftRight); }
631 int lowestFloatLogicalBottomIncludingPositionedFloats() const { return lowestFloatLogicalBottom(FloatingObject::FloatAll); }
632 int lowestFloatLogicalBottom(FloatingObject::Type) const;
633 int nextFloatLogicalBottomBelow(int) const;
635 virtual bool hitTestColumns(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
636 virtual bool hitTestContents(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction);
637 bool hitTestFloats(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset);
639 virtual bool isPointInOverflowControl(HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset);
641 void computeInlinePreferredLogicalWidths();
642 void computeBlockPreferredLogicalWidths();
644 // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
646 virtual RenderBlock* firstLineBlock() const;
648 virtual IntRect rectWithOutlineForRepaint(RenderBoxModelObject* repaintContainer, int outlineWidth) const;
649 virtual RenderStyle* outlineStyleForRepaint() const;
651 virtual RenderObject* hoverAncestor() const;
652 virtual void updateDragState(bool dragOn);
653 virtual void childBecameNonInline(RenderObject* child);
655 virtual LayoutRect selectionRectForRepaint(RenderBoxModelObject* repaintContainer, bool /*clipToVisibleContent*/)
657 return selectionGapRectsForRepaint(repaintContainer);
659 virtual bool shouldPaintSelectionGaps() const;
660 bool isSelectionRoot() const;
661 GapRects selectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
662 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo* = 0);
663 GapRects inlineSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
664 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo*);
665 GapRects blockSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
666 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo*);
667 LayoutRect blockSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
668 LayoutUnit lastLogicalTop, LayoutUnit lastLogicalLeft, LayoutUnit lastLogicalRight, LayoutUnit logicalBottom, const PaintInfo*);
669 LayoutUnit logicalLeftSelectionOffset(RenderBlock* rootBlock, LayoutUnit position);
670 LayoutUnit logicalRightSelectionOffset(RenderBlock* rootBlock, LayoutUnit position);
672 virtual void absoluteRects(Vector<LayoutRect>&, const LayoutPoint& accumulatedOffset);
673 virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed);
675 LayoutUnit desiredColumnWidth() const;
676 unsigned desiredColumnCount() const;
677 void setDesiredColumnCountAndWidth(int count, int width);
679 void paintContinuationOutlines(PaintInfo&, const IntPoint&);
681 virtual IntRect localCaretRect(InlineBox*, int caretOffset, int* extraWidthToEndOfLine = 0);
683 void adjustPointToColumnContents(IntPoint&) const;
684 void adjustForBorderFit(int x, int& left, int& right) const; // Helper function for borderFitAdjust
686 void markLinesDirtyInBlockRange(int logicalTop, int logicalBottom, RootInlineBox* highest = 0);
688 void newLine(EClear);
690 Position positionForBox(InlineBox*, bool start = true) const;
691 VisiblePosition positionForPointWithInlineChildren(const LayoutPoint&);
693 // Adjust from painting offsets to the local coords of this renderer
694 void offsetForContents(IntPoint&) const;
696 void calcColumnWidth();
697 bool layoutColumns(bool hasSpecifiedPageLogicalHeight, int pageLogicalHeight, LayoutStateMaintainer&);
698 void makeChildrenAnonymousColumnBlocks(RenderObject* beforeChild, RenderBlock* newBlockBox, RenderObject* newChild);
700 bool expandsToEncloseOverhangingFloats() const;
702 void updateScrollInfoAfterLayout();
704 RenderObject* splitAnonymousBlocksAroundChild(RenderObject* beforeChild);
705 void splitBlocks(RenderBlock* fromBlock, RenderBlock* toBlock, RenderBlock* middleBlock,
706 RenderObject* beforeChild, RenderBoxModelObject* oldCont);
707 void splitFlow(RenderObject* beforeChild, RenderBlock* newBlockBox,
708 RenderObject* newChild, RenderBoxModelObject* oldCont);
709 RenderBlock* clone() const;
710 RenderBlock* continuationBefore(RenderObject* beforeChild);
711 RenderBlock* containingColumnsBlock(bool allowAnonymousColumnBlock = true);
712 RenderBlock* columnsBlockForSpanningElement(RenderObject* newChild);
715 // Collapsing flags for whether we can collapse our margins with our children's margins.
716 bool m_canCollapseWithChildren : 1;
717 bool m_canCollapseMarginBeforeWithChildren : 1;
718 bool m_canCollapseMarginAfterWithChildren : 1;
720 // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
721 // margins in our container. Table cells and the body are the common examples. We
722 // also have a custom style property for Safari RSS to deal with TypePad blog articles.
723 bool m_quirkContainer : 1;
725 // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.
726 // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
727 // always be collapsing with one another. This variable can remain set to true through multiple iterations
728 // as long as we keep encountering self-collapsing blocks.
729 bool m_atBeforeSideOfBlock : 1;
731 // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
732 bool m_atAfterSideOfBlock : 1;
734 // These variables are used to detect quirky margins that we need to collapse away (in table cells
735 // and in the body element).
736 bool m_marginBeforeQuirk : 1;
737 bool m_marginAfterQuirk : 1;
738 bool m_determinedMarginBeforeQuirk : 1;
740 // These flags track the previous maximal positive and negative margins.
741 int m_positiveMargin;
742 int m_negativeMargin;
745 MarginInfo(RenderBlock* b, int beforeBorderPadding, int afterBorderPadding);
747 void setAtBeforeSideOfBlock(bool b) { m_atBeforeSideOfBlock = b; }
748 void setAtAfterSideOfBlock(bool b) { m_atAfterSideOfBlock = b; }
749 void clearMargin() { m_positiveMargin = m_negativeMargin = 0; }
750 void setMarginBeforeQuirk(bool b) { m_marginBeforeQuirk = b; }
751 void setMarginAfterQuirk(bool b) { m_marginAfterQuirk = b; }
752 void setDeterminedMarginBeforeQuirk(bool b) { m_determinedMarginBeforeQuirk = b; }
753 void setPositiveMargin(int p) { m_positiveMargin = p; }
754 void setNegativeMargin(int n) { m_negativeMargin = n; }
755 void setPositiveMarginIfLarger(int p) { if (p > m_positiveMargin) m_positiveMargin = p; }
756 void setNegativeMarginIfLarger(int n) { if (n > m_negativeMargin) m_negativeMargin = n; }
758 void setMargin(int p, int n) { m_positiveMargin = p; m_negativeMargin = n; }
760 bool atBeforeSideOfBlock() const { return m_atBeforeSideOfBlock; }
761 bool canCollapseWithMarginBefore() const { return m_atBeforeSideOfBlock && m_canCollapseMarginBeforeWithChildren; }
762 bool canCollapseWithMarginAfter() const { return m_atAfterSideOfBlock && m_canCollapseMarginAfterWithChildren; }
763 bool canCollapseMarginBeforeWithChildren() const { return m_canCollapseMarginBeforeWithChildren; }
764 bool canCollapseMarginAfterWithChildren() const { return m_canCollapseMarginAfterWithChildren; }
765 bool quirkContainer() const { return m_quirkContainer; }
766 bool determinedMarginBeforeQuirk() const { return m_determinedMarginBeforeQuirk; }
767 bool marginBeforeQuirk() const { return m_marginBeforeQuirk; }
768 bool marginAfterQuirk() const { return m_marginAfterQuirk; }
769 int positiveMargin() const { return m_positiveMargin; }
770 int negativeMargin() const { return m_negativeMargin; }
771 int margin() const { return m_positiveMargin - m_negativeMargin; }
774 void layoutBlockChild(RenderBox* child, MarginInfo&, int& previousFloatLogicalBottom, int& maxFloatLogicalBottom);
775 void adjustPositionedBlock(RenderBox* child, const MarginInfo&);
776 void adjustFloatingBlock(const MarginInfo&);
777 bool handleSpecialChild(RenderBox* child, const MarginInfo&);
778 bool handleFloatingChild(RenderBox* child, const MarginInfo&);
779 bool handlePositionedChild(RenderBox* child, const MarginInfo&);
780 bool handleRunInChild(RenderBox* child);
781 int collapseMargins(RenderBox* child, MarginInfo&);
782 int clearFloatsIfNeeded(RenderBox* child, MarginInfo&, int oldTopPosMargin, int oldTopNegMargin, int yPos);
783 int estimateLogicalTopPosition(RenderBox* child, const MarginInfo&);
784 void determineLogicalLeftPositionForChild(RenderBox* child);
785 void handleAfterSideOfBlock(int top, int bottom, MarginInfo&);
786 void setCollapsedBottomMargin(const MarginInfo&);
787 // End helper functions and structs used by layoutBlockChildren.
789 // Helper function for layoutInlineChildren()
790 RootInlineBox* createLineBoxesFromBidiRuns(BidiRunList<BidiRun>&, const InlineIterator& end, LineInfo&, VerticalPositionCache&, BidiRun* trailingSpaceRun);
791 void layoutRunsAndFloats(LineLayoutState&, bool hasInlineChild);
792 void layoutRunsAndFloatsInRange(LineLayoutState&, InlineBidiResolver&, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus);
793 void linkToEndLineIfNeeded(LineLayoutState&);
794 static void repaintDirtyFloats(Vector<FloatWithRect>& floats);
796 // Pagination routines.
798 // Returns the logicalOffset at the top of the next page. If the offset passed in is already at the top of the current page,
799 // then nextPageLogicalTopExcludingBoundaryPoint will still move to the top of the next page. nextPageLogicalTopIncludingBoundaryPoint
802 // 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.
803 int nextPageLogicalTopExcludingBoundaryPoint(int logicalOffset) const;
804 int nextPageLogicalTopIncludingBoundaryPoint(int logicalOffset) const;
806 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.
807 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.
808 int adjustForUnsplittableChild(RenderBox* child, int 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.
809 void adjustLinePositionForPagination(RootInlineBox*, int& 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.
811 struct FloatingObjectHashFunctions {
812 static unsigned hash(FloatingObject* key) { return DefaultHash<RenderBox*>::Hash::hash(key->m_renderer); }
813 static bool equal(FloatingObject* a, FloatingObject* b) { return a->m_renderer == b->m_renderer; }
814 static const bool safeToCompareToEmptyOrDeleted = true;
816 struct FloatingObjectHashTranslator {
817 static unsigned hash(RenderBox* key) { return DefaultHash<RenderBox*>::Hash::hash(key); }
818 static bool equal(FloatingObject* a, RenderBox* b) { return a->m_renderer == b; }
820 typedef ListHashSet<FloatingObject*, 4, FloatingObjectHashFunctions> FloatingObjectSet;
821 typedef FloatingObjectSet::const_iterator FloatingObjectSetIterator;
822 typedef PODInterval<LayoutUnit, FloatingObject*> FloatingObjectInterval;
823 typedef PODIntervalTree<LayoutUnit, FloatingObject*> FloatingObjectTree;
825 template <FloatingObject::Type FloatTypeValue>
826 class FloatIntervalSearchAdapter {
828 typedef FloatingObjectInterval IntervalType;
830 FloatIntervalSearchAdapter(const RenderBlock* renderer, LayoutUnit value, LayoutUnit& offset, LayoutUnit* heightRemaining)
831 : m_renderer(renderer)
834 , m_heightRemaining(heightRemaining)
838 inline LayoutUnit lowValue() const { return m_value; }
839 inline LayoutUnit highValue() const { return m_value; }
840 void collectIfNeeded(const IntervalType&) const;
843 const RenderBlock* m_renderer;
845 LayoutUnit& m_offset;
846 LayoutUnit* m_heightRemaining;
849 class FloatingObjects {
851 FloatingObjects(bool horizontalWritingMode)
852 : m_placedFloatsTree(UninitializedTree)
853 , m_leftObjectsCount(0)
854 , m_rightObjectsCount(0)
855 , m_positionedObjectsCount(0)
856 , m_horizontalWritingMode(horizontalWritingMode)
861 void add(FloatingObject*);
862 void remove(FloatingObject*);
863 void addPlacedObject(FloatingObject*);
864 void removePlacedObject(FloatingObject*);
865 void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; }
867 bool hasLeftObjects() const { return m_leftObjectsCount > 0; }
868 bool hasRightObjects() const { return m_rightObjectsCount > 0; }
869 bool hasPositionedObjects() const { return m_positionedObjectsCount > 0; }
870 const FloatingObjectSet& set() const { return m_set; }
871 const FloatingObjectTree& placedFloatsTree()
873 computePlacedFloatsTreeIfNeeded();
874 return m_placedFloatsTree;
877 void computePlacedFloatsTree();
878 inline void computePlacedFloatsTreeIfNeeded()
880 if (!m_placedFloatsTree.isInitialized())
881 computePlacedFloatsTree();
883 void increaseObjectsCount(FloatingObject::Type);
884 void decreaseObjectsCount(FloatingObject::Type);
885 FloatingObjectInterval intervalForFloatingObject(FloatingObject*);
887 FloatingObjectSet m_set;
888 FloatingObjectTree m_placedFloatsTree;
889 unsigned m_leftObjectsCount;
890 unsigned m_rightObjectsCount;
891 unsigned m_positionedObjectsCount;
892 bool m_horizontalWritingMode;
894 OwnPtr<FloatingObjects> m_floatingObjects;
896 typedef PositionedObjectsListHashSet::const_iterator Iterator;
897 OwnPtr<PositionedObjectsListHashSet> m_positionedObjects;
899 // Allocated only when some of these fields have non-default values
900 struct RenderBlockRareData {
901 WTF_MAKE_NONCOPYABLE(RenderBlockRareData); WTF_MAKE_FAST_ALLOCATED;
903 RenderBlockRareData(const RenderBlock* block)
904 : m_margins(positiveMarginBeforeDefault(block), negativeMarginBeforeDefault(block), positiveMarginAfterDefault(block), negativeMarginAfterDefault(block))
905 , m_paginationStrut(0)
906 , m_pageLogicalOffset(0)
910 static int positiveMarginBeforeDefault(const RenderBlock* block)
912 return std::max<LayoutUnit>(block->marginBefore(), 0);
915 static int negativeMarginBeforeDefault(const RenderBlock* block)
917 return std::max<LayoutUnit>(-block->marginBefore(), 0);
919 static int positiveMarginAfterDefault(const RenderBlock* block)
921 return std::max<LayoutUnit>(block->marginAfter(), 0);
923 static int negativeMarginAfterDefault(const RenderBlock* block)
925 return std::max<LayoutUnit>(-block->marginAfter(), 0);
928 MarginValues m_margins;
929 int m_paginationStrut;
930 int m_pageLogicalOffset;
933 OwnPtr<RenderBlockRareData> m_rareData;
935 RenderObjectChildList m_children;
936 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>.
938 mutable signed m_lineHeight : 30;
939 bool m_beingDestroyed : 1;
940 bool m_hasPositionedFloats : 1;
942 // RenderRubyBase objects need to be able to split and merge, moving their children around
943 // (calling moveChildTo, moveAllChildrenTo, and makeChildrenNonInline).
944 friend class RenderRubyBase;
945 friend class LineWidth; // Needs to know FloatingObject
948 // Used to store state between styleWillChange and styleDidChange
949 static bool s_canPropagateFloatIntoSibling;
952 inline RenderBlock* toRenderBlock(RenderObject* object)
954 ASSERT(!object || object->isRenderBlock());
955 return static_cast<RenderBlock*>(object);
958 inline const RenderBlock* toRenderBlock(const RenderObject* object)
960 ASSERT(!object || object->isRenderBlock());
961 return static_cast<const RenderBlock*>(object);
964 // This will catch anyone doing an unnecessary cast.
965 void toRenderBlock(const RenderBlock*);
968 // These structures are used by PODIntervalTree for debugging purposes.
969 template <> struct ValueToString<int> {
970 static String string(const int value);
972 template<> struct ValueToString<RenderBlock::FloatingObject*> {
973 static String string(const RenderBlock::FloatingObject*);
977 } // namespace WebCore
979 #endif // RenderBlock_h