Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / generic / nsBlockReflowContext.cpp
blob65fdc3d29d6f560b79d9f8d6036f17f3e9df00fd
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 "BlockReflowState.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 # include "nsBlockDebugFlags.h" // For NOISY_BLOCK_DIR_MARGINS
22 #endif
24 nsBlockReflowContext::nsBlockReflowContext(nsPresContext* aPresContext,
25 const ReflowInput& aParentRI)
26 : mPresContext(aPresContext),
27 mOuterReflowInput(aParentRI),
28 mFrame(nullptr),
29 mSpace(aParentRI.GetWritingMode()),
30 mICoord(0),
31 mBCoord(0),
32 mMetrics(aParentRI) {}
34 static nsIFrame* DescendIntoBlockLevelFrame(nsIFrame* aFrame) {
35 LayoutFrameType type = aFrame->Type();
36 if (type == LayoutFrameType::ColumnSet) {
37 static_cast<nsColumnSetFrame*>(aFrame)->DrainOverflowColumns();
38 nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
39 if (child) {
40 return DescendIntoBlockLevelFrame(child);
43 return aFrame;
46 bool nsBlockReflowContext::ComputeCollapsedBStartMargin(
47 const ReflowInput& aRI, nsCollapsingMargin* aMargin,
48 nsIFrame* aClearanceFrame, bool* aMayNeedRetry, bool* aBlockIsEmpty) {
49 WritingMode wm = aRI.GetWritingMode();
50 WritingMode parentWM = mMetrics.GetWritingMode();
52 // Include block-start element of frame's margin
53 aMargin->Include(aRI.ComputedLogicalMargin(parentWM).BStart(parentWM));
55 // The inclusion of the block-end margin when empty is done by the caller
56 // since it doesn't need to be done by the top-level (non-recursive)
57 // caller.
59 #ifdef NOISY_BLOCK_DIR_MARGINS
60 aRI.mFrame->ListTag(stdout);
61 printf(": %d => %d\n", aRI.ComputedLogicalMargin(wm).BStart(wm),
62 aMargin->get());
63 #endif
65 bool dirtiedLine = false;
66 bool setBlockIsEmpty = false;
68 // Calculate the frame's generational block-start-margin from its child
69 // blocks. Note that if the frame has a non-zero block-start-border or
70 // block-start-padding then this step is skipped because it will be a margin
71 // root. It is also skipped if the frame is a margin root for other
72 // reasons.
73 nsIFrame* frame = DescendIntoBlockLevelFrame(aRI.mFrame);
74 nsPresContext* prescontext = frame->PresContext();
75 nsBlockFrame* block = nullptr;
76 if (0 == aRI.ComputedLogicalBorderPadding(wm).BStart(wm)) {
77 block = do_QueryFrame(frame);
78 if (block) {
79 bool bStartMarginRoot, unused;
80 block->IsMarginRoot(&bStartMarginRoot, &unused);
81 if (bStartMarginRoot) {
82 block = nullptr;
87 // iterate not just through the lines of 'block' but also its
88 // overflow lines and the normal and overflow lines of its next in
89 // flows. Note that this will traverse some frames more than once:
90 // for example, if A contains B and A->nextinflow contains
91 // B->nextinflow, we'll traverse B->nextinflow twice. But this is
92 // OK because our traversal is idempotent.
93 for (; block; block = static_cast<nsBlockFrame*>(block->GetNextInFlow())) {
94 for (int overflowLines = 0; overflowLines <= 1; ++overflowLines) {
95 nsBlockFrame::LineIterator line;
96 nsBlockFrame::LineIterator line_end;
97 bool anyLines = true;
98 if (overflowLines) {
99 nsBlockFrame::FrameLines* frames = block->GetOverflowLines();
100 nsLineList* lines = frames ? &frames->mLines : nullptr;
101 if (!lines) {
102 anyLines = false;
103 } else {
104 line = lines->begin();
105 line_end = lines->end();
107 } else {
108 line = block->LinesBegin();
109 line_end = block->LinesEnd();
111 for (; anyLines && line != line_end; ++line) {
112 if (!aClearanceFrame && line->HasClearance()) {
113 // If we don't have a clearance frame, then we're computing
114 // the collapsed margin in the first pass, assuming that all
115 // lines have no clearance. So clear their clearance flags.
116 line->ClearHasClearance();
117 line->MarkDirty();
118 dirtiedLine = true;
121 bool isEmpty;
122 if (line->IsInline()) {
123 isEmpty = line->IsEmpty();
124 } else {
125 nsIFrame* kid = line->mFirstChild;
126 if (kid == aClearanceFrame) {
127 line->SetHasClearance();
128 line->MarkDirty();
129 dirtiedLine = true;
130 if (!setBlockIsEmpty && aBlockIsEmpty) {
131 setBlockIsEmpty = true;
132 *aBlockIsEmpty = false;
134 goto done;
136 // Here is where we recur. Now that we have determined that a
137 // generational collapse is required we need to compute the
138 // child blocks margin and so in so that we can look into
139 // it. For its margins to be computed we need to have a reflow
140 // input for it.
142 // We may have to construct an extra reflow input here if
143 // we drilled down through a block wrapper. At the moment
144 // we can only drill down one level so we only have to support
145 // one extra reflow input.
146 const ReflowInput* outerReflowInput = &aRI;
147 if (frame != aRI.mFrame) {
148 NS_ASSERTION(frame->GetParent() == aRI.mFrame,
149 "Can only drill through one level of block wrapper");
150 LogicalSize availSpace = aRI.ComputedSize(frame->GetWritingMode());
151 outerReflowInput =
152 new ReflowInput(prescontext, aRI, frame, availSpace);
155 LogicalSize availSpace =
156 outerReflowInput->ComputedSize(kid->GetWritingMode());
157 ReflowInput innerReflowInput(prescontext, *outerReflowInput, kid,
158 availSpace);
159 // Record that we're being optimistic by assuming the kid
160 // has no clearance
161 if (kid->StyleDisplay()->mClear != StyleClear::None ||
162 !nsBlockFrame::BlockCanIntersectFloats(kid)) {
163 *aMayNeedRetry = true;
165 if (ComputeCollapsedBStartMargin(innerReflowInput, aMargin,
166 aClearanceFrame, aMayNeedRetry,
167 &isEmpty)) {
168 line->MarkDirty();
169 dirtiedLine = true;
171 if (isEmpty) {
172 LogicalMargin innerMargin =
173 innerReflowInput.ComputedLogicalMargin(parentWM);
174 aMargin->Include(innerMargin.BEnd(parentWM));
177 if (outerReflowInput != &aRI) {
178 delete const_cast<ReflowInput*>(outerReflowInput);
181 if (!isEmpty) {
182 if (!setBlockIsEmpty && aBlockIsEmpty) {
183 setBlockIsEmpty = true;
184 *aBlockIsEmpty = false;
186 goto done;
189 if (!setBlockIsEmpty && aBlockIsEmpty) {
190 // The first time we reach here is when this is the first block
191 // and we have processed all its normal lines.
192 setBlockIsEmpty = true;
193 // All lines are empty, or we wouldn't be here!
194 *aBlockIsEmpty = aRI.mFrame->IsSelfEmpty();
198 done:
200 if (!setBlockIsEmpty && aBlockIsEmpty) {
201 *aBlockIsEmpty = aRI.mFrame->IsEmpty();
204 #ifdef NOISY_BLOCK_DIR_MARGINS
205 aRI.mFrame->ListTag(stdout);
206 printf(": => %d\n", aMargin->get());
207 #endif
209 return dirtiedLine;
212 void nsBlockReflowContext::ReflowBlock(const LogicalRect& aSpace,
213 bool aApplyBStartMargin,
214 nsCollapsingMargin& aPrevMargin,
215 nscoord aClearance, nsLineBox* aLine,
216 ReflowInput& aFrameRI,
217 nsReflowStatus& aFrameReflowStatus,
218 BlockReflowState& aState) {
219 mFrame = aFrameRI.mFrame;
220 mWritingMode = aState.mReflowInput.GetWritingMode();
221 mContainerSize = aState.ContainerSize();
222 mSpace = aSpace;
224 if (!aState.IsAdjacentWithBStart()) {
225 aFrameRI.mFlags.mIsTopOfPage = false; // make sure this is cleared
228 if (aApplyBStartMargin) {
229 mBStartMargin = aPrevMargin;
231 #ifdef NOISY_BLOCK_DIR_MARGINS
232 mOuterReflowInput.mFrame->ListTag(stdout);
233 printf(": reflowing ");
234 mFrame->ListTag(stdout);
235 printf(" margin => %d, clearance => %d\n", mBStartMargin.get(), aClearance);
236 #endif
238 // Adjust the available size if it's constrained so that the
239 // child frame doesn't think it can reflow into its margin area.
240 if (mWritingMode.IsOrthogonalTo(mFrame->GetWritingMode())) {
241 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableISize()) {
242 aFrameRI.SetAvailableISize(std::max(
243 0, aFrameRI.AvailableISize() - mBStartMargin.get() - aClearance));
245 } else {
246 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableBSize()) {
247 aFrameRI.SetAvailableBSize(std::max(
248 0, aFrameRI.AvailableBSize() - mBStartMargin.get() - aClearance));
251 } else {
252 // nsBlockFrame::ReflowBlock might call us multiple times with
253 // *different* values of aApplyBStartMargin.
254 mBStartMargin.Zero();
257 nscoord tI = 0, tB = 0;
258 // The values of x and y do not matter for floats, so don't bother
259 // calculating them. Floats are guaranteed to have their own float
260 // manager, so tI and tB don't matter. mICoord and mBCoord don't
261 // matter becacuse they are only used in PlaceBlock, which is not used
262 // for floats.
263 if (aLine) {
264 // Compute inline/block coordinate where reflow will begin. Use the
265 // rules from 10.3.3 to determine what to apply. At this point in the
266 // reflow auto inline-start/end margins will have a zero value.
267 LogicalMargin usedMargin = aFrameRI.ComputedLogicalMargin(mWritingMode);
268 mICoord = mSpace.IStart(mWritingMode) + usedMargin.IStart(mWritingMode);
269 mBCoord = mSpace.BStart(mWritingMode) + mBStartMargin.get() + aClearance;
271 LogicalRect space(
272 mWritingMode, mICoord, mBCoord,
273 mSpace.ISize(mWritingMode) - usedMargin.IStartEnd(mWritingMode),
274 mSpace.BSize(mWritingMode) - usedMargin.BStartEnd(mWritingMode));
275 tI = space.LineLeft(mWritingMode, mContainerSize);
276 tB = mBCoord;
278 if (!mFrame->HasAnyStateBits(NS_BLOCK_FLOAT_MGR)) {
279 aFrameRI.mBlockDelta =
280 mOuterReflowInput.mBlockDelta + mBCoord - aLine->BStart();
284 #ifdef DEBUG
285 mMetrics.ISize(mWritingMode) = nscoord(0xdeadbeef);
286 mMetrics.BSize(mWritingMode) = nscoord(0xdeadbeef);
287 #endif
289 mOuterReflowInput.mFloatManager->Translate(tI, tB);
290 mFrame->Reflow(mPresContext, mMetrics, aFrameRI, aFrameReflowStatus);
291 mOuterReflowInput.mFloatManager->Translate(-tI, -tB);
293 #ifdef DEBUG
294 if (!aFrameReflowStatus.IsInlineBreakBefore()) {
295 if ((ABSURD_SIZE(mMetrics.ISize(mWritingMode)) ||
296 ABSURD_SIZE(mMetrics.BSize(mWritingMode))) &&
297 !mFrame->GetParent()->IsAbsurdSizeAssertSuppressed()) {
298 printf("nsBlockReflowContext: ");
299 mFrame->ListTag(stdout);
300 printf(" metrics=%d,%d!\n", mMetrics.ISize(mWritingMode),
301 mMetrics.BSize(mWritingMode));
303 if ((mMetrics.ISize(mWritingMode) == nscoord(0xdeadbeef)) ||
304 (mMetrics.BSize(mWritingMode) == nscoord(0xdeadbeef))) {
305 printf("nsBlockReflowContext: ");
306 mFrame->ListTag(stdout);
307 printf(" didn't set i/b %d,%d!\n", mMetrics.ISize(mWritingMode),
308 mMetrics.BSize(mWritingMode));
311 #endif
313 if (!mFrame->HasOverflowAreas()) {
314 mMetrics.SetOverflowAreasToDesiredBounds();
317 if (!aFrameReflowStatus.IsInlineBreakBefore() &&
318 !aFrameRI.WillReflowAgainForClearance() &&
319 aFrameReflowStatus.IsFullyComplete()) {
320 // If mFrame is fully-complete and has a next-in-flow, we need to delete
321 // them now. Do not do this when a break-before is signaled or when a
322 // clearance frame is discovered in mFrame's subtree because mFrame is going
323 // to get reflowed again (whether the frame is (in)complete is undefined in
324 // that case anyway).
325 if (nsIFrame* kidNextInFlow = mFrame->GetNextInFlow()) {
326 // Remove all of the childs next-in-flows. Make sure that we ask
327 // the right parent to do the removal (it's possible that the
328 // parent is not this because we are executing pullup code).
329 // Floats will eventually be removed via nsBlockFrame::RemoveFloat
330 // which detaches the placeholder from the float.
331 nsOverflowContinuationTracker::AutoFinish fini(aState.mOverflowTracker,
332 mFrame);
333 nsIFrame::DestroyContext context(mPresContext->PresShell());
334 kidNextInFlow->GetParent()->DeleteNextInFlowChild(context, kidNextInFlow,
335 true);
341 * Attempt to place the block frame within the available space. If
342 * it fits, apply inline-dir ("horizontal") positioning (CSS 10.3.3),
343 * collapse margins (CSS2 8.3.1). Also apply relative positioning.
345 bool nsBlockReflowContext::PlaceBlock(const ReflowInput& aReflowInput,
346 bool aForceFit, nsLineBox* aLine,
347 nsCollapsingMargin& aBEndMarginResult,
348 OverflowAreas& aOverflowAreas,
349 const nsReflowStatus& aReflowStatus) {
350 // Compute collapsed block-end margin value.
351 WritingMode parentWM = mMetrics.GetWritingMode();
353 // Don't apply the block-end margin if the block has a *later* sibling across
354 // column-span split.
355 if (aReflowStatus.IsComplete() && !mFrame->HasColumnSpanSiblings()) {
356 aBEndMarginResult = mMetrics.mCarriedOutBEndMargin;
357 aBEndMarginResult.Include(
358 aReflowInput.ComputedLogicalMargin(parentWM).BEnd(parentWM));
359 } else {
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();
376 if (empty) {
377 // Collapse the block-end margin with the block-start margin that was
378 // already applied.
379 aBEndMarginResult.Include(mBStartMargin);
381 #ifdef NOISY_BLOCK_DIR_MARGINS
382 printf(" ");
383 mOuterReflowInput.mFrame->ListTag(stdout);
384 printf(": ");
385 mFrame->ListTag(stdout);
386 printf(
387 " -- collapsing block start & end margin together; BStart=%d "
388 "spaceBStart=%d\n",
389 mBCoord, mSpace.BStart(mWritingMode));
390 #endif
391 // Section 8.3.1 of CSS 2.1 says that blocks with adjoining
392 // "top/bottom" (i.e. block-start/end) margins whose top margin collapses
393 // with their parent's top margin should have their top border-edge at the
394 // top border-edge of their parent. We actually don't have to do
395 // anything special to make this happen. In that situation,
396 // nsBlockFrame::ShouldApplyBStartMargin will have returned false,
397 // and mBStartMargin and aClearance will have been zero in
398 // ReflowBlock.
400 // If we did apply our block-start margin, but now we're collapsing it
401 // into the block-end margin, we need to back up the containing
402 // block's bCoord-advance by our block-start margin so that it doesn't get
403 // counted twice. Note that here we're allowing the line's bounds
404 // to become different from the block's position; we do this
405 // because the containing block will place the next line at the
406 // line's BEnd, and it must place the next line at a different
407 // point from where this empty block will be.
408 backupContainingBlockAdvance = mBStartMargin.get();
411 // See if the frame fit. If it's the first frame or empty then it
412 // always fits. If the block-size is unconstrained then it always fits,
413 // even if there's some sort of integer overflow that makes bCoord +
414 // mMetrics.BSize() appear to go beyond the available block size.
415 if (!empty && !aForceFit &&
416 mSpace.BSize(mWritingMode) != NS_UNCONSTRAINEDSIZE) {
417 nscoord bEnd =
418 mBCoord - backupContainingBlockAdvance + mMetrics.BSize(mWritingMode);
419 if (bEnd > mSpace.BEnd(mWritingMode)) {
420 // didn't fit, we must acquit.
421 mFrame->DidReflow(mPresContext, &aReflowInput);
422 return false;
426 aLine->SetBounds(mWritingMode, mICoord,
427 mBCoord - backupContainingBlockAdvance,
428 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode),
429 mContainerSize);
431 // Now place the frame and complete the reflow process
432 nsContainerFrame::FinishReflowChild(
433 mFrame, mPresContext, mMetrics, &aReflowInput, mWritingMode,
434 LogicalPoint(mWritingMode, mICoord, mBCoord), mContainerSize,
435 nsIFrame::ReflowChildFlags::ApplyRelativePositioning);
437 aOverflowAreas = mMetrics.mOverflowAreas + mFrame->GetPosition();
439 return true;