michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:cindent:ts=2:et:sw=2: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* class that a parent frame uses to reflow a block frame */ michael@0: michael@0: #include "nsBlockReflowContext.h" michael@0: #include "nsBlockReflowState.h" michael@0: #include "nsFloatManager.h" michael@0: #include "nsContainerFrame.h" michael@0: #include "nsBlockFrame.h" michael@0: #include "nsLineBox.h" michael@0: #include "nsLayoutUtils.h" michael@0: michael@0: #ifdef DEBUG michael@0: #undef NOISY_MAX_ELEMENT_SIZE michael@0: #undef REALLY_NOISY_MAX_ELEMENT_SIZE michael@0: #undef NOISY_VERTICAL_MARGINS michael@0: #else michael@0: #undef NOISY_MAX_ELEMENT_SIZE michael@0: #undef REALLY_NOISY_MAX_ELEMENT_SIZE michael@0: #undef NOISY_VERTICAL_MARGINS michael@0: #endif michael@0: michael@0: nsBlockReflowContext::nsBlockReflowContext(nsPresContext* aPresContext, michael@0: const nsHTMLReflowState& aParentRS) michael@0: : mPresContext(aPresContext), michael@0: mOuterReflowState(aParentRS), michael@0: mMetrics(aParentRS.GetWritingMode()) michael@0: { michael@0: } michael@0: michael@0: static nsIFrame* DescendIntoBlockLevelFrame(nsIFrame* aFrame) michael@0: { michael@0: nsIAtom* type = aFrame->GetType(); michael@0: if (type == nsGkAtoms::columnSetFrame) michael@0: return DescendIntoBlockLevelFrame(aFrame->GetFirstPrincipalChild()); michael@0: return aFrame; michael@0: } michael@0: michael@0: bool michael@0: nsBlockReflowContext::ComputeCollapsedTopMargin(const nsHTMLReflowState& aRS, michael@0: nsCollapsingMargin* aMargin, nsIFrame* aClearanceFrame, michael@0: bool* aMayNeedRetry, bool* aBlockIsEmpty) michael@0: { michael@0: // Include frame's top margin michael@0: aMargin->Include(aRS.ComputedPhysicalMargin().top); michael@0: michael@0: // The inclusion of the bottom margin when empty is done by the caller michael@0: // since it doesn't need to be done by the top-level (non-recursive) michael@0: // caller. michael@0: michael@0: #ifdef NOISY_VERTICAL_MARGINS michael@0: nsFrame::ListTag(stdout, aRS.frame); michael@0: printf(": %d => %d\n", aRS.ComputedPhysicalMargin().top, aMargin->get()); michael@0: #endif michael@0: michael@0: bool dirtiedLine = false; michael@0: bool setBlockIsEmpty = false; michael@0: michael@0: // Calculate the frame's generational top-margin from its child michael@0: // blocks. Note that if the frame has a non-zero top-border or michael@0: // top-padding then this step is skipped because it will be a margin michael@0: // root. It is also skipped if the frame is a margin root for other michael@0: // reasons. michael@0: nsIFrame* frame = DescendIntoBlockLevelFrame(aRS.frame); michael@0: nsPresContext* prescontext = frame->PresContext(); michael@0: nsBlockFrame* block = nullptr; michael@0: if (0 == aRS.ComputedPhysicalBorderPadding().top) { michael@0: block = nsLayoutUtils::GetAsBlock(frame); michael@0: if (block) { michael@0: bool topMarginRoot, unused; michael@0: block->IsMarginRoot(&topMarginRoot, &unused); michael@0: if (topMarginRoot) { michael@0: block = nullptr; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // iterate not just through the lines of 'block' but also its michael@0: // overflow lines and the normal and overflow lines of its next in michael@0: // flows. Note that this will traverse some frames more than once: michael@0: // for example, if A contains B and A->nextinflow contains michael@0: // B->nextinflow, we'll traverse B->nextinflow twice. But this is michael@0: // OK because our traversal is idempotent. michael@0: for ( ;block; block = static_cast(block->GetNextInFlow())) { michael@0: for (int overflowLines = 0; overflowLines <= 1; ++overflowLines) { michael@0: nsBlockFrame::line_iterator line; michael@0: nsBlockFrame::line_iterator line_end; michael@0: bool anyLines = true; michael@0: if (overflowLines) { michael@0: nsBlockFrame::FrameLines* frames = block->GetOverflowLines(); michael@0: nsLineList* lines = frames ? &frames->mLines : nullptr; michael@0: if (!lines) { michael@0: anyLines = false; michael@0: } else { michael@0: line = lines->begin(); michael@0: line_end = lines->end(); michael@0: } michael@0: } else { michael@0: line = block->begin_lines(); michael@0: line_end = block->end_lines(); michael@0: } michael@0: for (; anyLines && line != line_end; ++line) { michael@0: if (!aClearanceFrame && line->HasClearance()) { michael@0: // If we don't have a clearance frame, then we're computing michael@0: // the collapsed margin in the first pass, assuming that all michael@0: // lines have no clearance. So clear their clearance flags. michael@0: line->ClearHasClearance(); michael@0: line->MarkDirty(); michael@0: dirtiedLine = true; michael@0: } michael@0: michael@0: bool isEmpty; michael@0: if (line->IsInline()) { michael@0: isEmpty = line->IsEmpty(); michael@0: } else { michael@0: nsIFrame* kid = line->mFirstChild; michael@0: if (kid == aClearanceFrame) { michael@0: line->SetHasClearance(); michael@0: line->MarkDirty(); michael@0: dirtiedLine = true; michael@0: goto done; michael@0: } michael@0: // Here is where we recur. Now that we have determined that a michael@0: // generational collapse is required we need to compute the michael@0: // child blocks margin and so in so that we can look into michael@0: // it. For its margins to be computed we need to have a reflow michael@0: // state for it. michael@0: michael@0: // We may have to construct an extra reflow state here if michael@0: // we drilled down through a block wrapper. At the moment michael@0: // we can only drill down one level so we only have to support michael@0: // one extra reflow state. michael@0: const nsHTMLReflowState* outerReflowState = &aRS; michael@0: if (frame != aRS.frame) { michael@0: NS_ASSERTION(frame->GetParent() == aRS.frame, michael@0: "Can only drill through one level of block wrapper"); michael@0: nsSize availSpace(aRS.ComputedWidth(), aRS.ComputedHeight()); michael@0: outerReflowState = new nsHTMLReflowState(prescontext, michael@0: aRS, frame, availSpace); michael@0: } michael@0: { michael@0: nsSize availSpace(outerReflowState->ComputedWidth(), michael@0: outerReflowState->ComputedHeight()); michael@0: nsHTMLReflowState innerReflowState(prescontext, michael@0: *outerReflowState, kid, michael@0: availSpace); michael@0: // Record that we're being optimistic by assuming the kid michael@0: // has no clearance michael@0: if (kid->StyleDisplay()->mBreakType != NS_STYLE_CLEAR_NONE) { michael@0: *aMayNeedRetry = true; michael@0: } michael@0: if (ComputeCollapsedTopMargin(innerReflowState, aMargin, aClearanceFrame, aMayNeedRetry, &isEmpty)) { michael@0: line->MarkDirty(); michael@0: dirtiedLine = true; michael@0: } michael@0: if (isEmpty) michael@0: aMargin->Include(innerReflowState.ComputedPhysicalMargin().bottom); michael@0: } michael@0: if (outerReflowState != &aRS) { michael@0: delete const_cast(outerReflowState); michael@0: } michael@0: } michael@0: if (!isEmpty) { michael@0: if (!setBlockIsEmpty && aBlockIsEmpty) { michael@0: setBlockIsEmpty = true; michael@0: *aBlockIsEmpty = false; michael@0: } michael@0: goto done; michael@0: } michael@0: } michael@0: if (!setBlockIsEmpty && aBlockIsEmpty) { michael@0: // The first time we reach here is when this is the first block michael@0: // and we have processed all its normal lines. michael@0: setBlockIsEmpty = true; michael@0: // All lines are empty, or we wouldn't be here! michael@0: *aBlockIsEmpty = aRS.frame->IsSelfEmpty(); michael@0: } michael@0: } michael@0: } michael@0: done: michael@0: michael@0: if (!setBlockIsEmpty && aBlockIsEmpty) { michael@0: *aBlockIsEmpty = aRS.frame->IsEmpty(); michael@0: } michael@0: michael@0: #ifdef NOISY_VERTICAL_MARGINS michael@0: nsFrame::ListTag(stdout, aRS.frame); michael@0: printf(": => %d\n", aMargin->get()); michael@0: #endif michael@0: michael@0: return dirtiedLine; michael@0: } michael@0: michael@0: nsresult michael@0: nsBlockReflowContext::ReflowBlock(const nsRect& aSpace, michael@0: bool aApplyTopMargin, michael@0: nsCollapsingMargin& aPrevMargin, michael@0: nscoord aClearance, michael@0: bool aIsAdjacentWithTop, michael@0: nsLineBox* aLine, michael@0: nsHTMLReflowState& aFrameRS, michael@0: nsReflowStatus& aFrameReflowStatus, michael@0: nsBlockReflowState& aState) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: mFrame = aFrameRS.frame; michael@0: mSpace = aSpace; michael@0: michael@0: if (!aIsAdjacentWithTop) { michael@0: aFrameRS.mFlags.mIsTopOfPage = false; // make sure this is cleared michael@0: } michael@0: michael@0: if (aApplyTopMargin) { michael@0: mTopMargin = aPrevMargin; michael@0: michael@0: #ifdef NOISY_VERTICAL_MARGINS michael@0: nsFrame::ListTag(stdout, mOuterReflowState.frame); michael@0: printf(": reflowing "); michael@0: nsFrame::ListTag(stdout, mFrame); michael@0: printf(" margin => %d, clearance => %d\n", mTopMargin.get(), aClearance); michael@0: #endif michael@0: michael@0: // Adjust the available height if its constrained so that the michael@0: // child frame doesn't think it can reflow into its margin area. michael@0: if (NS_UNCONSTRAINEDSIZE != aFrameRS.AvailableHeight()) { michael@0: aFrameRS.AvailableHeight() -= mTopMargin.get() + aClearance; michael@0: } michael@0: } michael@0: michael@0: nscoord tx = 0, ty = 0; michael@0: // The values of x and y do not matter for floats, so don't bother calculating michael@0: // them. Floats are guaranteed to have their own float manager, so tx and ty michael@0: // don't matter. mX and mY don't matter becacuse they are only used in michael@0: // PlaceBlock, which is not used for floats. michael@0: if (aLine) { michael@0: // Compute x/y coordinate where reflow will begin. Use the rules michael@0: // from 10.3.3 to determine what to apply. At this point in the michael@0: // reflow auto left/right margins will have a zero value. michael@0: michael@0: mX = tx = mSpace.x + aFrameRS.ComputedPhysicalMargin().left; michael@0: mY = ty = mSpace.y + mTopMargin.get() + aClearance; michael@0: michael@0: if ((mFrame->GetStateBits() & NS_BLOCK_FLOAT_MGR) == 0) michael@0: aFrameRS.mBlockDelta = michael@0: mOuterReflowState.mBlockDelta + ty - aLine->BStart(); michael@0: } michael@0: michael@0: // Let frame know that we are reflowing it michael@0: mFrame->WillReflow(mPresContext); michael@0: michael@0: #ifdef DEBUG michael@0: mMetrics.Width() = nscoord(0xdeadbeef); michael@0: mMetrics.Height() = nscoord(0xdeadbeef); michael@0: #endif michael@0: michael@0: mOuterReflowState.mFloatManager->Translate(tx, ty); michael@0: rv = mFrame->Reflow(mPresContext, mMetrics, aFrameRS, aFrameReflowStatus); michael@0: mOuterReflowState.mFloatManager->Translate(-tx, -ty); michael@0: michael@0: #ifdef DEBUG michael@0: if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus)) { michael@0: if (CRAZY_SIZE(mMetrics.Width()) || CRAZY_SIZE(mMetrics.Height())) { michael@0: printf("nsBlockReflowContext: "); michael@0: nsFrame::ListTag(stdout, mFrame); michael@0: printf(" metrics=%d,%d!\n", mMetrics.Width(), mMetrics.Height()); michael@0: } michael@0: if ((mMetrics.Width() == nscoord(0xdeadbeef)) || michael@0: (mMetrics.Height() == nscoord(0xdeadbeef))) { michael@0: printf("nsBlockReflowContext: "); michael@0: nsFrame::ListTag(stdout, mFrame); michael@0: printf(" didn't set w/h %d,%d!\n", mMetrics.Width(), mMetrics.Height()); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (!mFrame->HasOverflowAreas()) { michael@0: mMetrics.SetOverflowAreasToDesiredBounds(); michael@0: } michael@0: michael@0: if (!NS_INLINE_IS_BREAK_BEFORE(aFrameReflowStatus) || michael@0: (mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) { michael@0: // If frame is complete and has a next-in-flow, we need to delete michael@0: // them now. Do not do this when a break-before is signaled because michael@0: // the frame is going to get reflowed again (and may end up wanting michael@0: // a next-in-flow where it ends up), unless it is an out of flow frame. michael@0: if (NS_FRAME_IS_FULLY_COMPLETE(aFrameReflowStatus)) { michael@0: nsIFrame* kidNextInFlow = mFrame->GetNextInFlow(); michael@0: if (nullptr != kidNextInFlow) { michael@0: // Remove all of the childs next-in-flows. Make sure that we ask michael@0: // the right parent to do the removal (it's possible that the michael@0: // parent is not this because we are executing pullup code). michael@0: // Floats will eventually be removed via nsBlockFrame::RemoveFloat michael@0: // which detaches the placeholder from the float. michael@0: nsOverflowContinuationTracker::AutoFinish fini(aState.mOverflowTracker, mFrame); michael@0: static_cast(kidNextInFlow->GetParent()) michael@0: ->DeleteNextInFlowChild(kidNextInFlow, true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Attempt to place the block frame within the available space. If michael@0: * it fits, apply horizontal positioning (CSS 10.3.3), collapse michael@0: * margins (CSS2 8.3.1). Also apply relative positioning. michael@0: */ michael@0: bool michael@0: nsBlockReflowContext::PlaceBlock(const nsHTMLReflowState& aReflowState, michael@0: bool aForceFit, michael@0: nsLineBox* aLine, michael@0: nsCollapsingMargin& aBottomMarginResult, michael@0: nsOverflowAreas& aOverflowAreas, michael@0: nsReflowStatus aReflowStatus, michael@0: nscoord aContainerWidth) michael@0: { michael@0: // Compute collapsed bottom margin value. michael@0: if (NS_FRAME_IS_COMPLETE(aReflowStatus)) { michael@0: aBottomMarginResult = mMetrics.mCarriedOutBottomMargin; michael@0: aBottomMarginResult.Include(aReflowState.ComputedPhysicalMargin().bottom); michael@0: } else { michael@0: // The used bottom-margin is set to zero above a break. michael@0: aBottomMarginResult.Zero(); michael@0: } michael@0: michael@0: nsPoint position(mX, mY); michael@0: nscoord backupContainingBlockAdvance = 0; michael@0: michael@0: // Check whether the block's bottom margin collapses with its top michael@0: // margin. See CSS 2.1 section 8.3.1; those rules seem to match michael@0: // nsBlockFrame::IsEmpty(). Any such block must have zero height so michael@0: // check that first. Note that a block can have clearance and still michael@0: // have adjoining top/bottom margins, because the clearance goes michael@0: // above the top margin. michael@0: // Mark the frame as non-dirty; it has been reflowed (or we wouldn't michael@0: // be here), and we don't want to assert in CachedIsEmpty() michael@0: mFrame->RemoveStateBits(NS_FRAME_IS_DIRTY); michael@0: bool empty = 0 == mMetrics.Height() && aLine->CachedIsEmpty(); michael@0: if (empty) { michael@0: // Collapse the bottom margin with the top margin that was already michael@0: // applied. michael@0: aBottomMarginResult.Include(mTopMargin); michael@0: michael@0: #ifdef NOISY_VERTICAL_MARGINS michael@0: printf(" "); michael@0: nsFrame::ListTag(stdout, mOuterReflowState.frame); michael@0: printf(": "); michael@0: nsFrame::ListTag(stdout, mFrame); michael@0: printf(" -- collapsing top & bottom margin together; y=%d spaceY=%d\n", michael@0: position.y, mSpace.y); michael@0: #endif michael@0: // Section 8.3.1 of CSS 2.1 says that blocks with adjoining michael@0: // top/bottom margins whose top margin collapses with their michael@0: // parent's top margin should have their top border-edge at the michael@0: // top border-edge of their parent. We actually don't have to do michael@0: // anything special to make this happen. In that situation, michael@0: // nsBlockFrame::ShouldApplyTopMargin will have returned false, michael@0: // and mTopMargin and aClearance will have been zero in michael@0: // ReflowBlock. michael@0: michael@0: // If we did apply our top margin, but now we're collapsing it michael@0: // into the bottom margin, we need to back up the containing michael@0: // block's y-advance by our top margin so that it doesn't get michael@0: // counted twice. Note that here we're allowing the line's bounds michael@0: // to become different from the block's position; we do this michael@0: // because the containing block will place the next line at the michael@0: // line's YMost, and it must place the next line at a different michael@0: // point from where this empty block will be. michael@0: backupContainingBlockAdvance = mTopMargin.get(); michael@0: } michael@0: michael@0: // See if the frame fit. If it's the first frame or empty then it michael@0: // always fits. If the height is unconstrained then it always fits, michael@0: // even if there's some sort of integer overflow that makes y + michael@0: // mMetrics.Height() appear to go beyond the available height. michael@0: if (!empty && !aForceFit && mSpace.height != NS_UNCONSTRAINEDSIZE) { michael@0: nscoord yMost = position.y - backupContainingBlockAdvance + mMetrics.Height(); michael@0: if (yMost > mSpace.YMost()) { michael@0: // didn't fit, we must acquit. michael@0: mFrame->DidReflow(mPresContext, &aReflowState, nsDidReflowStatus::FINISHED); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: aLine->SetBounds(aReflowState.GetWritingMode(), michael@0: nsRect(position.x, michael@0: position.y - backupContainingBlockAdvance, michael@0: mMetrics.Width(), michael@0: mMetrics.Height()), michael@0: aContainerWidth); michael@0: michael@0: aReflowState.ApplyRelativePositioning(&position); michael@0: michael@0: // Now place the frame and complete the reflow process michael@0: nsContainerFrame::FinishReflowChild(mFrame, mPresContext, mMetrics, michael@0: &aReflowState, position.x, position.y, 0); michael@0: michael@0: aOverflowAreas = mMetrics.mOverflowAreas + position; michael@0: michael@0: return true; michael@0: }