Bug 1547759 - Add a flag to allow FinishReflowChild to handle relative positioning...
[gecko.git] / layout / generic / nsBlockReflowContext.cpp
blobc02888ac041923603314a675af9b1827e2a7ea82
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "BlockReflowInput.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 ReflowInput& aParentRI)
32 : mPresContext(aPresContext),
33 mOuterReflowInput(aParentRI),
34 mFrame(nullptr),
35 mSpace(aParentRI.GetWritingMode()),
36 mICoord(0),
37 mBCoord(0),
38 mMetrics(aParentRI) {}
40 static nsIFrame* DescendIntoBlockLevelFrame(nsIFrame* aFrame) {
41 LayoutFrameType type = aFrame->Type();
42 if (type == LayoutFrameType::ColumnSet) {
43 static_cast<nsColumnSetFrame*>(aFrame)->DrainOverflowColumns();
44 nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
45 if (child) {
46 return DescendIntoBlockLevelFrame(child);
49 return aFrame;
52 bool nsBlockReflowContext::ComputeCollapsedBStartMargin(
53 const ReflowInput& aRI, nsCollapsingMargin* aMargin,
54 nsIFrame* aClearanceFrame, bool* aMayNeedRetry, bool* aBlockIsEmpty) {
55 WritingMode wm = aRI.GetWritingMode();
56 WritingMode parentWM = mMetrics.GetWritingMode();
58 // Include block-start element of frame's margin
59 aMargin->Include(
60 aRI.ComputedLogicalMargin().ConvertTo(parentWM, wm).BStart(parentWM));
62 // The inclusion of the block-end margin when empty is done by the caller
63 // since it doesn't need to be done by the top-level (non-recursive)
64 // caller.
66 #ifdef NOISY_BLOCKDIR_MARGINS
67 aRI.mFrame->ListTag(stdout);
68 printf(": %d => %d\n", aRI.ComputedLogicalMargin().BStart(wm),
69 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(aRI.mFrame);
81 nsPresContext* prescontext = frame->PresContext();
82 nsBlockFrame* block = nullptr;
83 if (0 == aRI.ComputedLogicalBorderPadding().BStart(wm)) {
84 block = do_QueryFrame(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::LineIterator line;
103 nsBlockFrame::LineIterator 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->LinesBegin();
116 line_end = block->LinesEnd();
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 if (!setBlockIsEmpty && aBlockIsEmpty) {
138 setBlockIsEmpty = true;
139 *aBlockIsEmpty = false;
141 goto done;
143 // Here is where we recur. Now that we have determined that a
144 // generational collapse is required we need to compute the
145 // child blocks margin and so in so that we can look into
146 // it. For its margins to be computed we need to have a reflow
147 // input for it.
149 // We may have to construct an extra reflow input here if
150 // we drilled down through a block wrapper. At the moment
151 // we can only drill down one level so we only have to support
152 // one extra reflow input.
153 const ReflowInput* outerReflowInput = &aRI;
154 if (frame != aRI.mFrame) {
155 NS_ASSERTION(frame->GetParent() == aRI.mFrame,
156 "Can only drill through one level of block wrapper");
157 LogicalSize availSpace = aRI.ComputedSize(frame->GetWritingMode());
158 outerReflowInput =
159 new ReflowInput(prescontext, aRI, frame, availSpace);
162 LogicalSize availSpace =
163 outerReflowInput->ComputedSize(kid->GetWritingMode());
164 ReflowInput innerReflowInput(prescontext, *outerReflowInput, kid,
165 availSpace);
166 // Record that we're being optimistic by assuming the kid
167 // has no clearance
168 if (kid->StyleDisplay()->mBreakType != StyleClear::None ||
169 !nsBlockFrame::BlockCanIntersectFloats(kid)) {
170 *aMayNeedRetry = true;
172 if (ComputeCollapsedBStartMargin(innerReflowInput, aMargin,
173 aClearanceFrame, aMayNeedRetry,
174 &isEmpty)) {
175 line->MarkDirty();
176 dirtiedLine = true;
178 if (isEmpty) {
179 WritingMode innerWM = innerReflowInput.GetWritingMode();
180 LogicalMargin innerMargin =
181 innerReflowInput.ComputedLogicalMargin().ConvertTo(parentWM,
182 innerWM);
183 aMargin->Include(innerMargin.BEnd(parentWM));
186 if (outerReflowInput != &aRI) {
187 delete const_cast<ReflowInput*>(outerReflowInput);
190 if (!isEmpty) {
191 if (!setBlockIsEmpty && aBlockIsEmpty) {
192 setBlockIsEmpty = true;
193 *aBlockIsEmpty = false;
195 goto done;
198 if (!setBlockIsEmpty && aBlockIsEmpty) {
199 // The first time we reach here is when this is the first block
200 // and we have processed all its normal lines.
201 setBlockIsEmpty = true;
202 // All lines are empty, or we wouldn't be here!
203 *aBlockIsEmpty = aRI.mFrame->IsSelfEmpty();
207 done:
209 if (!setBlockIsEmpty && aBlockIsEmpty) {
210 *aBlockIsEmpty = aRI.mFrame->IsEmpty();
213 #ifdef NOISY_BLOCKDIR_MARGINS
214 aRI.mFrame->ListTag(stdout);
215 printf(": => %d\n", aMargin->get());
216 #endif
218 return dirtiedLine;
221 void nsBlockReflowContext::ReflowBlock(
222 const LogicalRect& aSpace, bool aApplyBStartMargin,
223 nsCollapsingMargin& aPrevMargin, nscoord aClearance,
224 bool aIsAdjacentWithBStart, nsLineBox* aLine, ReflowInput& aFrameRI,
225 nsReflowStatus& aFrameReflowStatus, BlockReflowInput& aState) {
226 mFrame = aFrameRI.mFrame;
227 mWritingMode = aState.mReflowInput.GetWritingMode();
228 mContainerSize = aState.ContainerSize();
229 mSpace = aSpace;
231 if (!aIsAdjacentWithBStart) {
232 aFrameRI.mFlags.mIsTopOfPage = false; // make sure this is cleared
235 if (aApplyBStartMargin) {
236 mBStartMargin = aPrevMargin;
238 #ifdef NOISY_BLOCKDIR_MARGINS
239 mOuterReflowInput.mFrame->ListTag(stdout);
240 printf(": reflowing ");
241 mFrame->ListTag(stdout);
242 printf(" margin => %d, clearance => %d\n", mBStartMargin.get(), aClearance);
243 #endif
245 // Adjust the available size if it's constrained so that the
246 // child frame doesn't think it can reflow into its margin area.
247 if (mWritingMode.IsOrthogonalTo(mFrame->GetWritingMode())) {
248 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableISize()) {
249 aFrameRI.AvailableISize() -= mBStartMargin.get() + aClearance;
251 } else {
252 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableBSize()) {
253 aFrameRI.AvailableBSize() -= mBStartMargin.get() + aClearance;
256 } else {
257 // nsBlockFrame::ReflowBlock might call us multiple times with
258 // *different* values of aApplyBStartMargin.
259 mBStartMargin.Zero();
262 nscoord tI = 0, tB = 0;
263 // The values of x and y do not matter for floats, so don't bother
264 // calculating them. Floats are guaranteed to have their own float
265 // manager, so tI and tB don't matter. mICoord and mBCoord don't
266 // matter becacuse they are only used in PlaceBlock, which is not used
267 // for floats.
268 if (aLine) {
269 // Compute inline/block coordinate where reflow will begin. Use the
270 // rules from 10.3.3 to determine what to apply. At this point in the
271 // reflow auto inline-start/end margins will have a zero value.
273 WritingMode frameWM = aFrameRI.GetWritingMode();
274 LogicalMargin usedMargin =
275 aFrameRI.ComputedLogicalMargin().ConvertTo(mWritingMode, frameWM);
276 mICoord = mSpace.IStart(mWritingMode) + usedMargin.IStart(mWritingMode);
277 mBCoord = mSpace.BStart(mWritingMode) + mBStartMargin.get() + aClearance;
279 LogicalRect space(
280 mWritingMode, mICoord, mBCoord,
281 mSpace.ISize(mWritingMode) - usedMargin.IStartEnd(mWritingMode),
282 mSpace.BSize(mWritingMode) - usedMargin.BStartEnd(mWritingMode));
283 tI = space.LineLeft(mWritingMode, mContainerSize);
284 tB = mBCoord;
286 if ((mFrame->GetStateBits() & NS_BLOCK_FLOAT_MGR) == 0)
287 aFrameRI.mBlockDelta =
288 mOuterReflowInput.mBlockDelta + mBCoord - aLine->BStart();
291 #ifdef DEBUG
292 mMetrics.ISize(mWritingMode) = nscoord(0xdeadbeef);
293 mMetrics.BSize(mWritingMode) = nscoord(0xdeadbeef);
294 #endif
296 mOuterReflowInput.mFloatManager->Translate(tI, tB);
297 mFrame->Reflow(mPresContext, mMetrics, aFrameRI, aFrameReflowStatus);
298 mOuterReflowInput.mFloatManager->Translate(-tI, -tB);
300 #ifdef DEBUG
301 if (!aFrameReflowStatus.IsInlineBreakBefore()) {
302 if ((CRAZY_SIZE(mMetrics.ISize(mWritingMode)) ||
303 CRAZY_SIZE(mMetrics.BSize(mWritingMode))) &&
304 !mFrame->GetParent()->IsCrazySizeAssertSuppressed()) {
305 printf("nsBlockReflowContext: ");
306 mFrame->ListTag(stdout);
307 printf(" metrics=%d,%d!\n", mMetrics.ISize(mWritingMode),
308 mMetrics.BSize(mWritingMode));
310 if ((mMetrics.ISize(mWritingMode) == nscoord(0xdeadbeef)) ||
311 (mMetrics.BSize(mWritingMode) == nscoord(0xdeadbeef))) {
312 printf("nsBlockReflowContext: ");
313 mFrame->ListTag(stdout);
314 printf(" didn't set i/b %d,%d!\n", mMetrics.ISize(mWritingMode),
315 mMetrics.BSize(mWritingMode));
318 #endif
320 if (!mFrame->HasOverflowAreas()) {
321 mMetrics.SetOverflowAreasToDesiredBounds();
324 if (!aFrameReflowStatus.IsInlineBreakBefore() &&
325 aFrameReflowStatus.IsFullyComplete()) {
326 // If frame is complete and has a next-in-flow, we need to delete
327 // them now. Do not do this when a break-before is signaled because
328 // the frame is going to get reflowed again (whether the frame is
329 // (in)complete is undefined in that case anyway).
330 if (nsIFrame* kidNextInFlow = mFrame->GetNextInFlow()) {
331 // Remove all of the childs next-in-flows. Make sure that we ask
332 // the right parent to do the removal (it's possible that the
333 // parent is not this because we are executing pullup code).
334 // Floats will eventually be removed via nsBlockFrame::RemoveFloat
335 // which detaches the placeholder from the float.
336 nsOverflowContinuationTracker::AutoFinish fini(aState.mOverflowTracker,
337 mFrame);
338 kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
344 * Attempt to place the block frame within the available space. If
345 * it fits, apply inline-dir ("horizontal") positioning (CSS 10.3.3),
346 * collapse margins (CSS2 8.3.1). Also apply relative positioning.
348 bool nsBlockReflowContext::PlaceBlock(const ReflowInput& aReflowInput,
349 bool aForceFit, nsLineBox* aLine,
350 nsCollapsingMargin& aBEndMarginResult,
351 nsOverflowAreas& aOverflowAreas,
352 const nsReflowStatus& aReflowStatus) {
353 // Compute collapsed block-end margin value.
354 WritingMode wm = aReflowInput.GetWritingMode();
355 WritingMode parentWM = mMetrics.GetWritingMode();
356 if (aReflowStatus.IsComplete()) {
357 aBEndMarginResult = mMetrics.mCarriedOutBEndMargin;
358 aBEndMarginResult.Include(aReflowInput.ComputedLogicalMargin()
359 .ConvertTo(parentWM, wm)
360 .BEnd(parentWM));
361 } else {
362 // The used block-end-margin is set to zero before a break.
363 aBEndMarginResult.Zero();
366 nscoord backupContainingBlockAdvance = 0;
368 // Check whether the block's block-end margin collapses with its block-start
369 // margin. See CSS 2.1 section 8.3.1; those rules seem to match
370 // nsBlockFrame::IsEmpty(). Any such block must have zero block-size so
371 // check that first. Note that a block can have clearance and still
372 // have adjoining block-start/end margins, because the clearance goes
373 // above the block-start margin.
374 // Mark the frame as non-dirty; it has been reflowed (or we wouldn't
375 // be here), and we don't want to assert in CachedIsEmpty()
376 mFrame->RemoveStateBits(NS_FRAME_IS_DIRTY);
377 bool empty = 0 == mMetrics.BSize(parentWM) && aLine->CachedIsEmpty();
378 if (empty) {
379 // Collapse the block-end margin with the block-start margin that was
380 // already applied.
381 aBEndMarginResult.Include(mBStartMargin);
383 #ifdef NOISY_BLOCKDIR_MARGINS
384 printf(" ");
385 mOuterReflowInput.mFrame->ListTag(stdout);
386 printf(": ");
387 mFrame->ListTag(stdout);
388 printf(
389 " -- collapsing block start & end margin together; BStart=%d "
390 "spaceBStart=%d\n",
391 mBCoord, mSpace.BStart(mWritingMode));
392 #endif
393 // Section 8.3.1 of CSS 2.1 says that blocks with adjoining
394 // "top/bottom" (i.e. block-start/end) margins whose top margin collapses
395 // with their parent's top margin should have their top border-edge at the
396 // top border-edge of their parent. We actually don't have to do
397 // anything special to make this happen. In that situation,
398 // nsBlockFrame::ShouldApplyBStartMargin will have returned false,
399 // and mBStartMargin and aClearance will have been zero in
400 // ReflowBlock.
402 // If we did apply our block-start margin, but now we're collapsing it
403 // into the block-end margin, we need to back up the containing
404 // block's bCoord-advance by our block-start margin so that it doesn't get
405 // counted twice. Note that here we're allowing the line's bounds
406 // to become different from the block's position; we do this
407 // because the containing block will place the next line at the
408 // line's BEnd, and it must place the next line at a different
409 // point from where this empty block will be.
410 backupContainingBlockAdvance = mBStartMargin.get();
413 // See if the frame fit. If it's the first frame or empty then it
414 // always fits. If the block-size is unconstrained then it always fits,
415 // even if there's some sort of integer overflow that makes bCoord +
416 // mMetrics.BSize() appear to go beyond the available block size.
417 if (!empty && !aForceFit &&
418 mSpace.BSize(mWritingMode) != NS_UNCONSTRAINEDSIZE) {
419 nscoord bEnd =
420 mBCoord - backupContainingBlockAdvance + mMetrics.BSize(mWritingMode);
421 if (bEnd > mSpace.BEnd(mWritingMode)) {
422 // didn't fit, we must acquit.
423 mFrame->DidReflow(mPresContext, &aReflowInput);
424 return false;
428 aLine->SetBounds(mWritingMode, mICoord,
429 mBCoord - backupContainingBlockAdvance,
430 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode),
431 mContainerSize);
433 WritingMode frameWM = mFrame->GetWritingMode();
434 LogicalPoint logPos =
435 LogicalPoint(mWritingMode, mICoord, mBCoord)
436 .ConvertTo(frameWM, mWritingMode,
437 mContainerSize - mMetrics.PhysicalSize());
439 // Now place the frame and complete the reflow process
440 nsContainerFrame::FinishReflowChild(
441 mFrame, mPresContext, mMetrics, &aReflowInput, frameWM, logPos,
442 mContainerSize, nsIFrame::ReflowChildFlags::ApplyRelativePositioning);
444 aOverflowAreas = mMetrics.mOverflowAreas + mFrame->GetPosition();
446 return true;