1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=2:et:sw=2:
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* class that a parent frame uses to reflow a block frame */
9 #include "nsBlockReflowContext.h"
10 #include "nsBlockReflowState.h"
11 #include "nsFloatManager.h"
12 #include "nsColumnSetFrame.h"
13 #include "nsContainerFrame.h"
14 #include "nsBlockFrame.h"
15 #include "nsLineBox.h"
16 #include "nsLayoutUtils.h"
18 using namespace mozilla
;
21 #undef NOISY_MAX_ELEMENT_SIZE
22 #undef REALLY_NOISY_MAX_ELEMENT_SIZE
23 #undef NOISY_BLOCK_DIR_MARGINS
25 #undef NOISY_MAX_ELEMENT_SIZE
26 #undef REALLY_NOISY_MAX_ELEMENT_SIZE
27 #undef NOISY_BLOCK_DIR_MARGINS
30 nsBlockReflowContext::nsBlockReflowContext(nsPresContext
* aPresContext
,
31 const nsHTMLReflowState
& aParentRS
)
32 : mPresContext(aPresContext
),
33 mOuterReflowState(aParentRS
),
34 mSpace(aParentRS
.GetWritingMode()),
39 static nsIFrame
* DescendIntoBlockLevelFrame(nsIFrame
* aFrame
)
41 nsIAtom
* type
= aFrame
->GetType();
42 if (type
== nsGkAtoms::columnSetFrame
) {
43 static_cast<nsColumnSetFrame
*>(aFrame
)->DrainOverflowColumns();
44 nsIFrame
* child
= aFrame
->GetFirstPrincipalChild();
46 return DescendIntoBlockLevelFrame(child
);
53 nsBlockReflowContext::ComputeCollapsedBStartMargin(const nsHTMLReflowState
& aRS
,
54 nsCollapsingMargin
* aMargin
,
55 nsIFrame
* aClearanceFrame
,
59 WritingMode wm
= aRS
.GetWritingMode();
60 WritingMode parentWM
= mMetrics
.GetWritingMode();
62 // Include block-start element of frame's margin
63 aMargin
->Include(aRS
.ComputedLogicalMargin().ConvertTo(parentWM
, wm
).BStart(parentWM
));
65 // The inclusion of the block-end margin when empty is done by the caller
66 // since it doesn't need to be done by the top-level (non-recursive)
69 #ifdef NOISY_BLOCKDIR_MARGINS
70 nsFrame::ListTag(stdout
, aRS
.frame
);
71 printf(": %d => %d\n", aRS
.ComputedLogicalMargin().BStart(wm
), aMargin
->get());
74 bool dirtiedLine
= false;
75 bool setBlockIsEmpty
= false;
77 // Calculate the frame's generational block-start-margin from its child
78 // blocks. Note that if the frame has a non-zero block-start-border or
79 // block-start-padding then this step is skipped because it will be a margin
80 // root. It is also skipped if the frame is a margin root for other
82 nsIFrame
* frame
= DescendIntoBlockLevelFrame(aRS
.frame
);
83 nsPresContext
* prescontext
= frame
->PresContext();
84 nsBlockFrame
* block
= nullptr;
85 if (0 == aRS
.ComputedLogicalBorderPadding().BStart(wm
)) {
86 block
= nsLayoutUtils::GetAsBlock(frame
);
88 bool bStartMarginRoot
, unused
;
89 block
->IsMarginRoot(&bStartMarginRoot
, &unused
);
90 if (bStartMarginRoot
) {
96 // iterate not just through the lines of 'block' but also its
97 // overflow lines and the normal and overflow lines of its next in
98 // flows. Note that this will traverse some frames more than once:
99 // for example, if A contains B and A->nextinflow contains
100 // B->nextinflow, we'll traverse B->nextinflow twice. But this is
101 // OK because our traversal is idempotent.
102 for ( ;block
; block
= static_cast<nsBlockFrame
*>(block
->GetNextInFlow())) {
103 for (int overflowLines
= 0; overflowLines
<= 1; ++overflowLines
) {
104 nsBlockFrame::line_iterator line
;
105 nsBlockFrame::line_iterator line_end
;
106 bool anyLines
= true;
108 nsBlockFrame::FrameLines
* frames
= block
->GetOverflowLines();
109 nsLineList
* lines
= frames
? &frames
->mLines
: nullptr;
113 line
= lines
->begin();
114 line_end
= lines
->end();
117 line
= block
->begin_lines();
118 line_end
= block
->end_lines();
120 for (; anyLines
&& line
!= line_end
; ++line
) {
121 if (!aClearanceFrame
&& line
->HasClearance()) {
122 // If we don't have a clearance frame, then we're computing
123 // the collapsed margin in the first pass, assuming that all
124 // lines have no clearance. So clear their clearance flags.
125 line
->ClearHasClearance();
131 if (line
->IsInline()) {
132 isEmpty
= line
->IsEmpty();
134 nsIFrame
* kid
= line
->mFirstChild
;
135 if (kid
== aClearanceFrame
) {
136 line
->SetHasClearance();
141 // Here is where we recur. Now that we have determined that a
142 // generational collapse is required we need to compute the
143 // child blocks margin and so in so that we can look into
144 // it. For its margins to be computed we need to have a reflow
147 // We may have to construct an extra reflow state here if
148 // we drilled down through a block wrapper. At the moment
149 // we can only drill down one level so we only have to support
150 // one extra reflow state.
151 const nsHTMLReflowState
* outerReflowState
= &aRS
;
152 if (frame
!= aRS
.frame
) {
153 NS_ASSERTION(frame
->GetParent() == aRS
.frame
,
154 "Can only drill through one level of block wrapper");
155 LogicalSize availSpace
= aRS
.ComputedSize(frame
->GetWritingMode());
156 outerReflowState
= new nsHTMLReflowState(prescontext
,
157 aRS
, frame
, availSpace
);
160 LogicalSize availSpace
=
161 outerReflowState
->ComputedSize(kid
->GetWritingMode());
162 nsHTMLReflowState
innerReflowState(prescontext
,
163 *outerReflowState
, kid
,
165 // Record that we're being optimistic by assuming the kid
167 if (kid
->StyleDisplay()->mBreakType
!= NS_STYLE_CLEAR_NONE
) {
168 *aMayNeedRetry
= true;
170 if (ComputeCollapsedBStartMargin(innerReflowState
, aMargin
,
171 aClearanceFrame
, aMayNeedRetry
,
177 WritingMode innerWM
= innerReflowState
.GetWritingMode();
178 LogicalMargin innerMargin
=
179 innerReflowState
.ComputedLogicalMargin().ConvertTo(parentWM
, innerWM
);
180 aMargin
->Include(innerMargin
.BEnd(parentWM
));
183 if (outerReflowState
!= &aRS
) {
184 delete const_cast<nsHTMLReflowState
*>(outerReflowState
);
188 if (!setBlockIsEmpty
&& aBlockIsEmpty
) {
189 setBlockIsEmpty
= true;
190 *aBlockIsEmpty
= false;
195 if (!setBlockIsEmpty
&& aBlockIsEmpty
) {
196 // The first time we reach here is when this is the first block
197 // and we have processed all its normal lines.
198 setBlockIsEmpty
= true;
199 // All lines are empty, or we wouldn't be here!
200 *aBlockIsEmpty
= aRS
.frame
->IsSelfEmpty();
206 if (!setBlockIsEmpty
&& aBlockIsEmpty
) {
207 *aBlockIsEmpty
= aRS
.frame
->IsEmpty();
210 #ifdef NOISY_BLOCKDIR_MARGINS
211 nsFrame::ListTag(stdout
, aRS
.frame
);
212 printf(": => %d\n", aMargin
->get());
219 nsBlockReflowContext::ReflowBlock(const nsRect
& aSpace
,
220 bool aApplyBStartMargin
,
221 nsCollapsingMargin
& aPrevMargin
,
223 bool aIsAdjacentWithBStart
,
225 nsHTMLReflowState
& aFrameRS
,
226 nsReflowStatus
& aFrameReflowStatus
,
227 nsBlockReflowState
& aState
)
229 mFrame
= aFrameRS
.frame
;
230 mWritingMode
= aState
.mReflowState
.GetWritingMode();
231 mContainerWidth
= aState
.mContainerWidth
;
232 mSpace
= LogicalRect(mWritingMode
, aSpace
, mContainerWidth
);
234 if (!aIsAdjacentWithBStart
) {
235 aFrameRS
.mFlags
.mIsTopOfPage
= false; // make sure this is cleared
238 if (aApplyBStartMargin
) {
239 mBStartMargin
= aPrevMargin
;
241 #ifdef NOISY_BLOCKDIR_MARGINS
242 nsFrame::ListTag(stdout
, mOuterReflowState
.frame
);
243 printf(": reflowing ");
244 nsFrame::ListTag(stdout
, mFrame
);
245 printf(" margin => %d, clearance => %d\n", mBStartMargin
.get(), aClearance
);
248 // Adjust the available block size if it's constrained so that the
249 // child frame doesn't think it can reflow into its margin area.
250 if (NS_UNCONSTRAINEDSIZE
!= aFrameRS
.AvailableBSize()) {
251 aFrameRS
.AvailableBSize() -= mBStartMargin
.get() + aClearance
;
255 nscoord tI
= 0, tB
= 0;
256 // The values of x and y do not matter for floats, so don't bother
257 // calculating them. Floats are guaranteed to have their own float
258 // manager, so tI and tB don't matter. mICoord and mBCoord don't
259 // matter becacuse they are only used in PlaceBlock, which is not used
262 // Compute inline/block coordinate where reflow will begin. Use the
263 // rules from 10.3.3 to determine what to apply. At this point in the
264 // reflow auto inline-start/end margins will have a zero value.
266 WritingMode frameWM
= aFrameRS
.GetWritingMode();
268 mSpace
.IStart(mWritingMode
) +
269 aFrameRS
.ComputedLogicalMargin().ConvertTo(mWritingMode
,
270 frameWM
).IStart(mWritingMode
);
271 mBCoord
= tB
= mSpace
.BStart(mWritingMode
) +
272 mBStartMargin
.get() + aClearance
;
274 //XXX temporary until nsFloatManager is logicalized
275 tI
= aSpace
.x
+ aFrameRS
.ComputedPhysicalMargin().left
;
276 tB
= aSpace
.y
+ mBStartMargin
.get() + aClearance
;
278 if ((mFrame
->GetStateBits() & NS_BLOCK_FLOAT_MGR
) == 0)
279 aFrameRS
.mBlockDelta
=
280 mOuterReflowState
.mBlockDelta
+ mBCoord
- aLine
->BStart();
283 // Let frame know that we are reflowing it
284 mFrame
->WillReflow(mPresContext
);
287 mMetrics
.ISize(mWritingMode
) = nscoord(0xdeadbeef);
288 mMetrics
.BSize(mWritingMode
) = nscoord(0xdeadbeef);
291 mOuterReflowState
.mFloatManager
->Translate(tI
, tB
);
292 mFrame
->Reflow(mPresContext
, mMetrics
, aFrameRS
, aFrameReflowStatus
);
293 mOuterReflowState
.mFloatManager
->Translate(-tI
, -tB
);
296 if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus
)) {
297 if (CRAZY_SIZE(mMetrics
.ISize(mWritingMode
)) ||
298 CRAZY_SIZE(mMetrics
.BSize(mWritingMode
))) {
299 printf("nsBlockReflowContext: ");
300 nsFrame::ListTag(stdout
, mFrame
);
301 printf(" metrics=%d,%d!\n",
302 mMetrics
.ISize(mWritingMode
), mMetrics
.BSize(mWritingMode
));
304 if ((mMetrics
.ISize(mWritingMode
) == nscoord(0xdeadbeef)) ||
305 (mMetrics
.BSize(mWritingMode
) == nscoord(0xdeadbeef))) {
306 printf("nsBlockReflowContext: ");
307 nsFrame::ListTag(stdout
, mFrame
);
308 printf(" didn't set i/b %d,%d!\n",
309 mMetrics
.ISize(mWritingMode
), mMetrics
.BSize(mWritingMode
));
314 if (!mFrame
->HasOverflowAreas()) {
315 mMetrics
.SetOverflowAreasToDesiredBounds();
318 if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus
) ||
319 (mFrame
->GetStateBits() & NS_FRAME_OUT_OF_FLOW
)) {
320 // If frame is complete and has a next-in-flow, we need to delete
321 // them now. Do not do this when a break-before is signaled because
322 // the frame is going to get reflowed again (and may end up wanting
323 // a next-in-flow where it ends up), unless it is an out of flow frame.
324 if (NS_FRAME_IS_FULLY_COMPLETE(aFrameReflowStatus
)) {
325 nsIFrame
* kidNextInFlow
= mFrame
->GetNextInFlow();
326 if (nullptr != kidNextInFlow
) {
327 // Remove all of the childs next-in-flows. Make sure that we ask
328 // the right parent to do the removal (it's possible that the
329 // parent is not this because we are executing pullup code).
330 // Floats will eventually be removed via nsBlockFrame::RemoveFloat
331 // which detaches the placeholder from the float.
332 nsOverflowContinuationTracker::AutoFinish
fini(aState
.mOverflowTracker
, mFrame
);
333 kidNextInFlow
->GetParent()->DeleteNextInFlowChild(kidNextInFlow
, true);
340 * Attempt to place the block frame within the available space. If
341 * it fits, apply inline-dir ("horizontal") positioning (CSS 10.3.3),
342 * collapse margins (CSS2 8.3.1). Also apply relative positioning.
345 nsBlockReflowContext::PlaceBlock(const nsHTMLReflowState
& aReflowState
,
348 nsCollapsingMargin
& aBEndMarginResult
,
349 nsOverflowAreas
& aOverflowAreas
,
350 nsReflowStatus aReflowStatus
)
352 // Compute collapsed block-end margin value.
353 WritingMode wm
= aReflowState
.GetWritingMode();
354 WritingMode parentWM
= mMetrics
.GetWritingMode();
355 if (NS_FRAME_IS_COMPLETE(aReflowStatus
)) {
356 aBEndMarginResult
= mMetrics
.mCarriedOutBEndMargin
;
357 aBEndMarginResult
.Include(aReflowState
.ComputedLogicalMargin().
358 ConvertTo(parentWM
, wm
).BEnd(parentWM
));
360 // The used block-end-margin is set to zero before a break.
361 aBEndMarginResult
.Zero();
364 nscoord backupContainingBlockAdvance
= 0;
366 // Check whether the block's block-end margin collapses with its block-start
367 // margin. See CSS 2.1 section 8.3.1; those rules seem to match
368 // nsBlockFrame::IsEmpty(). Any such block must have zero block-size so
369 // check that first. Note that a block can have clearance and still
370 // have adjoining block-start/end margins, because the clearance goes
371 // above the block-start margin.
372 // Mark the frame as non-dirty; it has been reflowed (or we wouldn't
373 // be here), and we don't want to assert in CachedIsEmpty()
374 mFrame
->RemoveStateBits(NS_FRAME_IS_DIRTY
);
375 bool empty
= 0 == mMetrics
.BSize(parentWM
) && aLine
->CachedIsEmpty();
377 // Collapse the block-end margin with the block-start margin that was
379 aBEndMarginResult
.Include(mBStartMargin
);
381 #ifdef NOISY_BLOCKDIR_MARGINS
383 nsFrame::ListTag(stdout
, mOuterReflowState
.frame
);
385 nsFrame::ListTag(stdout
, mFrame
);
386 printf(" -- collapsing block start & end margin together; BStart=%d spaceBStart=%d\n",
387 mBCoord
, mSpace
.BStart(mWritingMode
));
389 // Section 8.3.1 of CSS 2.1 says that blocks with adjoining
390 // "top/bottom" (i.e. block-start/end) margins whose top margin collapses
391 // with their parent's top margin should have their top border-edge at the
392 // top border-edge of their parent. We actually don't have to do
393 // anything special to make this happen. In that situation,
394 // nsBlockFrame::ShouldApplyBStartMargin will have returned false,
395 // and mBStartMargin and aClearance will have been zero in
398 // If we did apply our block-start margin, but now we're collapsing it
399 // into the block-end margin, we need to back up the containing
400 // block's bCoord-advance by our block-start margin so that it doesn't get
401 // counted twice. Note that here we're allowing the line's bounds
402 // to become different from the block's position; we do this
403 // because the containing block will place the next line at the
404 // line's BEnd, and it must place the next line at a different
405 // point from where this empty block will be.
406 backupContainingBlockAdvance
= mBStartMargin
.get();
409 // See if the frame fit. If it's the first frame or empty then it
410 // always fits. If the block-size is unconstrained then it always fits,
411 // even if there's some sort of integer overflow that makes bCoord +
412 // mMetrics.BSize() appear to go beyond the available block size.
413 if (!empty
&& !aForceFit
&&
414 mSpace
.BSize(mWritingMode
) != NS_UNCONSTRAINEDSIZE
) {
415 nscoord bEnd
= mBCoord
-
416 backupContainingBlockAdvance
+ mMetrics
.BSize(mWritingMode
);
417 if (bEnd
> mSpace
.BEnd(mWritingMode
)) {
418 // didn't fit, we must acquit.
419 mFrame
->DidReflow(mPresContext
, &aReflowState
,
420 nsDidReflowStatus::FINISHED
);
425 aLine
->SetBounds(mWritingMode
,
426 mICoord
, mBCoord
- backupContainingBlockAdvance
,
427 mMetrics
.ISize(mWritingMode
), mMetrics
.BSize(mWritingMode
),
430 // XXX temporary until other classes are logicalized
431 nsPoint position
= LogicalRect(mWritingMode
,
433 mMetrics
.ISize(mWritingMode
),
434 mMetrics
.BSize(mWritingMode
)).
435 GetPhysicalPosition(mWritingMode
, mContainerWidth
);
437 aReflowState
.ApplyRelativePositioning(&position
);
439 // Now place the frame and complete the reflow process
440 nsContainerFrame::FinishReflowChild(mFrame
, mPresContext
, mMetrics
,
441 &aReflowState
, position
.x
, position
.y
, 0);
443 aOverflowAreas
= mMetrics
.mOverflowAreas
+ position
;