Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsBlockReflowContext.cpp
blob638e7ae76f7ee9d0036631b9cfa5190f0d992f50
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;
20 #ifdef DEBUG
21 #undef NOISY_MAX_ELEMENT_SIZE
22 #undef REALLY_NOISY_MAX_ELEMENT_SIZE
23 #undef NOISY_BLOCK_DIR_MARGINS
24 #else
25 #undef NOISY_MAX_ELEMENT_SIZE
26 #undef REALLY_NOISY_MAX_ELEMENT_SIZE
27 #undef NOISY_BLOCK_DIR_MARGINS
28 #endif
30 nsBlockReflowContext::nsBlockReflowContext(nsPresContext* aPresContext,
31 const nsHTMLReflowState& aParentRS)
32 : mPresContext(aPresContext),
33 mOuterReflowState(aParentRS),
34 mSpace(aParentRS.GetWritingMode()),
35 mMetrics(aParentRS)
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();
45 if (child) {
46 return DescendIntoBlockLevelFrame(child);
49 return aFrame;
52 bool
53 nsBlockReflowContext::ComputeCollapsedBStartMargin(const nsHTMLReflowState& aRS,
54 nsCollapsingMargin* aMargin,
55 nsIFrame* aClearanceFrame,
56 bool* aMayNeedRetry,
57 bool* aBlockIsEmpty)
59 WritingMode wm = aRS.GetWritingMode();
60 // Include frame's block-start margin
61 aMargin->Include(aRS.ComputedLogicalMargin().BStart(wm));
63 // The inclusion of the block-end margin when empty is done by the caller
64 // since it doesn't need to be done by the top-level (non-recursive)
65 // caller.
67 #ifdef NOISY_BLOCKDIR_MARGINS
68 nsFrame::ListTag(stdout, aRS.frame);
69 printf(": %d => %d\n", aRS.ComputedLogicalMargin().BStart(wm), aMargin->get());
70 #endif
72 bool dirtiedLine = false;
73 bool setBlockIsEmpty = false;
75 // Calculate the frame's generational block-start-margin from its child
76 // blocks. Note that if the frame has a non-zero block-start-border or
77 // block-start-padding then this step is skipped because it will be a margin
78 // root. It is also skipped if the frame is a margin root for other
79 // reasons.
80 nsIFrame* frame = DescendIntoBlockLevelFrame(aRS.frame);
81 nsPresContext* prescontext = frame->PresContext();
82 nsBlockFrame* block = nullptr;
83 if (0 == aRS.ComputedLogicalBorderPadding().BStart(wm)) {
84 block = nsLayoutUtils::GetAsBlock(frame);
85 if (block) {
86 bool bStartMarginRoot, unused;
87 block->IsMarginRoot(&bStartMarginRoot, &unused);
88 if (bStartMarginRoot) {
89 block = nullptr;
94 // iterate not just through the lines of 'block' but also its
95 // overflow lines and the normal and overflow lines of its next in
96 // flows. Note that this will traverse some frames more than once:
97 // for example, if A contains B and A->nextinflow contains
98 // B->nextinflow, we'll traverse B->nextinflow twice. But this is
99 // OK because our traversal is idempotent.
100 for ( ;block; block = static_cast<nsBlockFrame*>(block->GetNextInFlow())) {
101 for (int overflowLines = 0; overflowLines <= 1; ++overflowLines) {
102 nsBlockFrame::line_iterator line;
103 nsBlockFrame::line_iterator line_end;
104 bool anyLines = true;
105 if (overflowLines) {
106 nsBlockFrame::FrameLines* frames = block->GetOverflowLines();
107 nsLineList* lines = frames ? &frames->mLines : nullptr;
108 if (!lines) {
109 anyLines = false;
110 } else {
111 line = lines->begin();
112 line_end = lines->end();
114 } else {
115 line = block->begin_lines();
116 line_end = block->end_lines();
118 for (; anyLines && line != line_end; ++line) {
119 if (!aClearanceFrame && line->HasClearance()) {
120 // If we don't have a clearance frame, then we're computing
121 // the collapsed margin in the first pass, assuming that all
122 // lines have no clearance. So clear their clearance flags.
123 line->ClearHasClearance();
124 line->MarkDirty();
125 dirtiedLine = true;
128 bool isEmpty;
129 if (line->IsInline()) {
130 isEmpty = line->IsEmpty();
131 } else {
132 nsIFrame* kid = line->mFirstChild;
133 if (kid == aClearanceFrame) {
134 line->SetHasClearance();
135 line->MarkDirty();
136 dirtiedLine = true;
137 goto done;
139 // Here is where we recur. Now that we have determined that a
140 // generational collapse is required we need to compute the
141 // child blocks margin and so in so that we can look into
142 // it. For its margins to be computed we need to have a reflow
143 // state for it.
145 // We may have to construct an extra reflow state here if
146 // we drilled down through a block wrapper. At the moment
147 // we can only drill down one level so we only have to support
148 // one extra reflow state.
149 const nsHTMLReflowState* outerReflowState = &aRS;
150 if (frame != aRS.frame) {
151 NS_ASSERTION(frame->GetParent() == aRS.frame,
152 "Can only drill through one level of block wrapper");
153 LogicalSize availSpace = aRS.ComputedSize(frame->GetWritingMode());
154 outerReflowState = new nsHTMLReflowState(prescontext,
155 aRS, frame, availSpace);
158 LogicalSize availSpace =
159 outerReflowState->ComputedSize(kid->GetWritingMode());
160 nsHTMLReflowState innerReflowState(prescontext,
161 *outerReflowState, kid,
162 availSpace);
163 // Record that we're being optimistic by assuming the kid
164 // has no clearance
165 if (kid->StyleDisplay()->mBreakType != NS_STYLE_CLEAR_NONE) {
166 *aMayNeedRetry = true;
168 if (ComputeCollapsedBStartMargin(innerReflowState, aMargin,
169 aClearanceFrame, aMayNeedRetry,
170 &isEmpty)) {
171 line->MarkDirty();
172 dirtiedLine = true;
174 if (isEmpty) {
175 WritingMode innerWM = innerReflowState.GetWritingMode();
176 LogicalMargin innerMargin =
177 innerReflowState.ComputedLogicalMargin().ConvertTo(wm, innerWM);
178 aMargin->Include(innerMargin.BEnd(wm));
181 if (outerReflowState != &aRS) {
182 delete const_cast<nsHTMLReflowState*>(outerReflowState);
185 if (!isEmpty) {
186 if (!setBlockIsEmpty && aBlockIsEmpty) {
187 setBlockIsEmpty = true;
188 *aBlockIsEmpty = false;
190 goto done;
193 if (!setBlockIsEmpty && aBlockIsEmpty) {
194 // The first time we reach here is when this is the first block
195 // and we have processed all its normal lines.
196 setBlockIsEmpty = true;
197 // All lines are empty, or we wouldn't be here!
198 *aBlockIsEmpty = aRS.frame->IsSelfEmpty();
202 done:
204 if (!setBlockIsEmpty && aBlockIsEmpty) {
205 *aBlockIsEmpty = aRS.frame->IsEmpty();
208 #ifdef NOISY_BLOCKDIR_MARGINS
209 nsFrame::ListTag(stdout, aRS.frame);
210 printf(": => %d\n", aMargin->get());
211 #endif
213 return dirtiedLine;
216 void
217 nsBlockReflowContext::ReflowBlock(const nsRect& aSpace,
218 bool aApplyBStartMargin,
219 nsCollapsingMargin& aPrevMargin,
220 nscoord aClearance,
221 bool aIsAdjacentWithBStart,
222 nsLineBox* aLine,
223 nsHTMLReflowState& aFrameRS,
224 nsReflowStatus& aFrameReflowStatus,
225 nsBlockReflowState& aState)
227 mFrame = aFrameRS.frame;
228 mWritingMode = aState.mReflowState.GetWritingMode();
229 mContainerWidth = aState.mContainerWidth;
230 mSpace = LogicalRect(mWritingMode, aSpace, mContainerWidth);
232 if (!aIsAdjacentWithBStart) {
233 aFrameRS.mFlags.mIsTopOfPage = false; // make sure this is cleared
236 if (aApplyBStartMargin) {
237 mBStartMargin = aPrevMargin;
239 #ifdef NOISY_BLOCKDIR_MARGINS
240 nsFrame::ListTag(stdout, mOuterReflowState.frame);
241 printf(": reflowing ");
242 nsFrame::ListTag(stdout, mFrame);
243 printf(" margin => %d, clearance => %d\n", mBStartMargin.get(), aClearance);
244 #endif
246 // Adjust the available block size if it's constrained so that the
247 // child frame doesn't think it can reflow into its margin area.
248 if (NS_UNCONSTRAINEDSIZE != aFrameRS.AvailableBSize()) {
249 aFrameRS.AvailableBSize() -= mBStartMargin.get() + aClearance;
253 nscoord tI = 0, tB = 0;
254 // The values of x and y do not matter for floats, so don't bother
255 // calculating them. Floats are guaranteed to have their own float
256 // manager, so tI and tB don't matter. mICoord and mBCoord don't
257 // matter becacuse they are only used in PlaceBlock, which is not used
258 // for floats.
259 if (aLine) {
260 // Compute inline/block coordinate where reflow will begin. Use the
261 // rules from 10.3.3 to determine what to apply. At this point in the
262 // reflow auto inline-start/end margins will have a zero value.
264 WritingMode frameWM = aFrameRS.GetWritingMode();
265 mICoord = tI =
266 mSpace.IStart(mWritingMode) +
267 aFrameRS.ComputedLogicalMargin().ConvertTo(mWritingMode,
268 frameWM).IStart(mWritingMode);
269 mBCoord = tB = mSpace.BStart(mWritingMode) +
270 mBStartMargin.get() + aClearance;
272 //XXX temporary until nsFloatManager is logicalized
273 tI = aSpace.x + aFrameRS.ComputedPhysicalMargin().left;
274 tB = aSpace.y + mBStartMargin.get() + aClearance;
276 if ((mFrame->GetStateBits() & NS_BLOCK_FLOAT_MGR) == 0)
277 aFrameRS.mBlockDelta =
278 mOuterReflowState.mBlockDelta + mBCoord - aLine->BStart();
281 // Let frame know that we are reflowing it
282 mFrame->WillReflow(mPresContext);
284 #ifdef DEBUG
285 mMetrics.ISize(mWritingMode) = nscoord(0xdeadbeef);
286 mMetrics.BSize(mWritingMode) = nscoord(0xdeadbeef);
287 #endif
289 mOuterReflowState.mFloatManager->Translate(tI, tB);
290 mFrame->Reflow(mPresContext, mMetrics, aFrameRS, aFrameReflowStatus);
291 mOuterReflowState.mFloatManager->Translate(-tI, -tB);
293 #ifdef DEBUG
294 if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus)) {
295 if (CRAZY_SIZE(mMetrics.ISize(mWritingMode)) ||
296 CRAZY_SIZE(mMetrics.BSize(mWritingMode))) {
297 printf("nsBlockReflowContext: ");
298 nsFrame::ListTag(stdout, mFrame);
299 printf(" metrics=%d,%d!\n",
300 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode));
302 if ((mMetrics.ISize(mWritingMode) == nscoord(0xdeadbeef)) ||
303 (mMetrics.BSize(mWritingMode) == nscoord(0xdeadbeef))) {
304 printf("nsBlockReflowContext: ");
305 nsFrame::ListTag(stdout, mFrame);
306 printf(" didn't set i/b %d,%d!\n",
307 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode));
310 #endif
312 if (!mFrame->HasOverflowAreas()) {
313 mMetrics.SetOverflowAreasToDesiredBounds();
316 if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus) ||
317 (mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) {
318 // If frame is complete and has a next-in-flow, we need to delete
319 // them now. Do not do this when a break-before is signaled because
320 // the frame is going to get reflowed again (and may end up wanting
321 // a next-in-flow where it ends up), unless it is an out of flow frame.
322 if (NS_FRAME_IS_FULLY_COMPLETE(aFrameReflowStatus)) {
323 nsIFrame* kidNextInFlow = mFrame->GetNextInFlow();
324 if (nullptr != kidNextInFlow) {
325 // Remove all of the childs next-in-flows. Make sure that we ask
326 // the right parent to do the removal (it's possible that the
327 // parent is not this because we are executing pullup code).
328 // Floats will eventually be removed via nsBlockFrame::RemoveFloat
329 // which detaches the placeholder from the float.
330 nsOverflowContinuationTracker::AutoFinish fini(aState.mOverflowTracker, mFrame);
331 kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
338 * Attempt to place the block frame within the available space. If
339 * it fits, apply inline-dir ("horizontal") positioning (CSS 10.3.3),
340 * collapse margins (CSS2 8.3.1). Also apply relative positioning.
342 bool
343 nsBlockReflowContext::PlaceBlock(const nsHTMLReflowState& aReflowState,
344 bool aForceFit,
345 nsLineBox* aLine,
346 nsCollapsingMargin& aBEndMarginResult,
347 nsOverflowAreas& aOverflowAreas,
348 nsReflowStatus aReflowStatus)
350 // Compute collapsed block-end margin value.
351 WritingMode wm = aReflowState.GetWritingMode();
352 WritingMode parentWM = mMetrics.GetWritingMode();
353 if (NS_FRAME_IS_COMPLETE(aReflowStatus)) {
354 aBEndMarginResult = mMetrics.mCarriedOutBottomMargin;
355 aBEndMarginResult.Include(aReflowState.ComputedLogicalMargin().BEnd(wm));
356 } else {
357 // The used bottom-margin is set to zero above a break.
358 aBEndMarginResult.Zero();
361 nscoord backupContainingBlockAdvance = 0;
363 // Check whether the block's block-end margin collapses with its block-start
364 // margin. See CSS 2.1 section 8.3.1; those rules seem to match
365 // nsBlockFrame::IsEmpty(). Any such block must have zero block-size so
366 // check that first. Note that a block can have clearance and still
367 // have adjoining block-start/end margins, because the clearance goes
368 // above the block-start margin.
369 // Mark the frame as non-dirty; it has been reflowed (or we wouldn't
370 // be here), and we don't want to assert in CachedIsEmpty()
371 mFrame->RemoveStateBits(NS_FRAME_IS_DIRTY);
372 bool empty = 0 == mMetrics.BSize(parentWM) && aLine->CachedIsEmpty();
373 if (empty) {
374 // Collapse the block-end margin with the block-start margin that was
375 // already applied.
376 aBEndMarginResult.Include(mBStartMargin);
378 #ifdef NOISY_BLOCKDIR_MARGINS
379 printf(" ");
380 nsFrame::ListTag(stdout, mOuterReflowState.frame);
381 printf(": ");
382 nsFrame::ListTag(stdout, mFrame);
383 printf(" -- collapsing block start & end margin together; BStart=%d spaceBStart=%d\n",
384 mBCoord, mSpace.BStart(mWritingMode));
385 #endif
386 // Section 8.3.1 of CSS 2.1 says that blocks with adjoining
387 // "top/bottom" (i.e. block-start/end) margins whose top margin collapses
388 // with their parent's top margin should have their top border-edge at the
389 // top border-edge of their parent. We actually don't have to do
390 // anything special to make this happen. In that situation,
391 // nsBlockFrame::ShouldApplyBStartMargin will have returned false,
392 // and mBStartMargin and aClearance will have been zero in
393 // ReflowBlock.
395 // If we did apply our block-start margin, but now we're collapsing it
396 // into the block-end margin, we need to back up the containing
397 // block's bCoord-advance by our block-start margin so that it doesn't get
398 // counted twice. Note that here we're allowing the line's bounds
399 // to become different from the block's position; we do this
400 // because the containing block will place the next line at the
401 // line's BEnd, and it must place the next line at a different
402 // point from where this empty block will be.
403 backupContainingBlockAdvance = mBStartMargin.get();
406 // See if the frame fit. If it's the first frame or empty then it
407 // always fits. If the block-size is unconstrained then it always fits,
408 // even if there's some sort of integer overflow that makes bCoord +
409 // mMetrics.BSize() appear to go beyond the available height.
410 if (!empty && !aForceFit &&
411 mSpace.BSize(mWritingMode) != NS_UNCONSTRAINEDSIZE) {
412 nscoord bEnd = mBCoord -
413 backupContainingBlockAdvance + mMetrics.BSize(mWritingMode);
414 if (bEnd > mSpace.BEnd(mWritingMode)) {
415 // didn't fit, we must acquit.
416 mFrame->DidReflow(mPresContext, &aReflowState,
417 nsDidReflowStatus::FINISHED);
418 return false;
422 aLine->SetBounds(mWritingMode,
423 mICoord, mBCoord - backupContainingBlockAdvance,
424 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode),
425 mContainerWidth);
427 // XXX temporary until other classes are logicalized
428 nsPoint position = LogicalRect(mWritingMode,
429 mICoord, mBCoord,
430 mMetrics.ISize(mWritingMode),
431 mMetrics.BSize(mWritingMode)).
432 GetPhysicalPosition(mWritingMode, mContainerWidth);
434 aReflowState.ApplyRelativePositioning(&position);
436 // Now place the frame and complete the reflow process
437 nsContainerFrame::FinishReflowChild(mFrame, mPresContext, mMetrics,
438 &aReflowState, position.x, position.y, 0);
440 aOverflowAreas = mMetrics.mOverflowAreas + position;
442 return true;