layout/generic/nsLineLayout.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/generic/nsLineLayout.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2726 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* state and methods used while laying out a single line of a block frame */
    1.10 +
    1.11 +// This has to be defined before nsLineLayout.h is included, because
    1.12 +// nsLineLayout.h has a #include for plarena.h, which needs this defined:
    1.13 +#define PL_ARENA_CONST_ALIGN_MASK (sizeof(void*)-1)
    1.14 +#include "nsLineLayout.h"
    1.15 +
    1.16 +#include "SVGTextFrame.h"
    1.17 +#include "nsBlockFrame.h"
    1.18 +#include "nsStyleConsts.h"
    1.19 +#include "nsContainerFrame.h"
    1.20 +#include "nsFloatManager.h"
    1.21 +#include "nsStyleContext.h"
    1.22 +#include "nsPresContext.h"
    1.23 +#include "nsRenderingContext.h"
    1.24 +#include "nsGkAtoms.h"
    1.25 +#include "nsIContent.h"
    1.26 +#include "nsLayoutUtils.h"
    1.27 +#include "nsTextFrame.h"
    1.28 +#include "nsStyleStructInlines.h"
    1.29 +#include "nsBidiPresUtils.h"
    1.30 +#include <algorithm>
    1.31 +
    1.32 +#ifdef DEBUG
    1.33 +#undef  NOISY_INLINEDIR_ALIGN
    1.34 +#undef  NOISY_BLOCKDIR_ALIGN
    1.35 +#undef  REALLY_NOISY_BLOCKDIR_ALIGN
    1.36 +#undef  NOISY_REFLOW
    1.37 +#undef  REALLY_NOISY_REFLOW
    1.38 +#undef  NOISY_PUSHING
    1.39 +#undef  REALLY_NOISY_PUSHING
    1.40 +#undef  DEBUG_ADD_TEXT
    1.41 +#undef  NOISY_MAX_ELEMENT_SIZE
    1.42 +#undef  REALLY_NOISY_MAX_ELEMENT_SIZE
    1.43 +#undef  NOISY_CAN_PLACE_FRAME
    1.44 +#undef  NOISY_TRIM
    1.45 +#undef  REALLY_NOISY_TRIM
    1.46 +#endif
    1.47 +
    1.48 +using namespace mozilla;
    1.49 +
    1.50 +//----------------------------------------------------------------------
    1.51 +
    1.52 +#define FIX_BUG_50257
    1.53 +
    1.54 +nsLineLayout::nsLineLayout(nsPresContext* aPresContext,
    1.55 +                           nsFloatManager* aFloatManager,
    1.56 +                           const nsHTMLReflowState* aOuterReflowState,
    1.57 +                           const nsLineList::iterator* aLine)
    1.58 +  : mPresContext(aPresContext),
    1.59 +    mFloatManager(aFloatManager),
    1.60 +    mBlockReflowState(aOuterReflowState),
    1.61 +    mLastOptionalBreakContent(nullptr),
    1.62 +    mForceBreakContent(nullptr),
    1.63 +    mBlockRS(nullptr),/* XXX temporary */
    1.64 +    mLastOptionalBreakPriority(gfxBreakPriority::eNoBreak),
    1.65 +    mLastOptionalBreakContentOffset(-1),
    1.66 +    mForceBreakContentOffset(-1),
    1.67 +    mMinLineBSize(0),
    1.68 +    mTextIndent(0),
    1.69 +    mFirstLetterStyleOK(false),
    1.70 +    mIsTopOfPage(false),
    1.71 +    mImpactedByFloats(false),
    1.72 +    mLastFloatWasLetterFrame(false),
    1.73 +    mLineIsEmpty(false),
    1.74 +    mLineEndsInBR(false),
    1.75 +    mNeedBackup(false),
    1.76 +    mInFirstLine(false),
    1.77 +    mGotLineBox(false),
    1.78 +    mInFirstLetter(false),
    1.79 +    mHasBullet(false),
    1.80 +    mDirtyNextLine(false),
    1.81 +    mLineAtStart(false)
    1.82 +{
    1.83 +  MOZ_ASSERT(aOuterReflowState, "aOuterReflowState must not be null");
    1.84 +  NS_ASSERTION(aFloatManager || aOuterReflowState->frame->GetType() ==
    1.85 +                                  nsGkAtoms::letterFrame,
    1.86 +               "float manager should be present");
    1.87 +  MOZ_COUNT_CTOR(nsLineLayout);
    1.88 +
    1.89 +  // Stash away some style data that we need
    1.90 +  nsBlockFrame* blockFrame = do_QueryFrame(aOuterReflowState->frame);
    1.91 +  if (blockFrame)
    1.92 +    mStyleText = blockFrame->StyleTextForLineLayout();
    1.93 +  else
    1.94 +    mStyleText = aOuterReflowState->frame->StyleText();
    1.95 +
    1.96 +  mLineNumber = 0;
    1.97 +  mTotalPlacedFrames = 0;
    1.98 +  mBStartEdge = 0;
    1.99 +  mTrimmableWidth = 0;
   1.100 +
   1.101 +  mInflationMinFontSize =
   1.102 +    nsLayoutUtils::InflationMinFontSizeFor(aOuterReflowState->frame);
   1.103 +
   1.104 +  // Instead of always pre-initializing the free-lists for frames and
   1.105 +  // spans, we do it on demand so that situations that only use a few
   1.106 +  // frames and spans won't waste a lot of time in unneeded
   1.107 +  // initialization.
   1.108 +  PL_INIT_ARENA_POOL(&mArena, "nsLineLayout", 1024);
   1.109 +  mFrameFreeList = nullptr;
   1.110 +  mSpanFreeList = nullptr;
   1.111 +
   1.112 +  mCurrentSpan = mRootSpan = nullptr;
   1.113 +  mSpanDepth = 0;
   1.114 +
   1.115 +  if (aLine) {
   1.116 +    mGotLineBox = true;
   1.117 +    mLineBox = *aLine;
   1.118 +  }
   1.119 +}
   1.120 +
   1.121 +nsLineLayout::~nsLineLayout()
   1.122 +{
   1.123 +  MOZ_COUNT_DTOR(nsLineLayout);
   1.124 +
   1.125 +  NS_ASSERTION(nullptr == mRootSpan, "bad line-layout user");
   1.126 +
   1.127 +  PL_FinishArenaPool(&mArena);
   1.128 +}
   1.129 +
   1.130 +// Find out if the frame has a non-null prev-in-flow, i.e., whether it
   1.131 +// is a continuation.
   1.132 +inline bool
   1.133 +HasPrevInFlow(nsIFrame *aFrame)
   1.134 +{
   1.135 +  nsIFrame *prevInFlow = aFrame->GetPrevInFlow();
   1.136 +  return prevInFlow != nullptr;
   1.137 +}
   1.138 +
   1.139 +void
   1.140 +nsLineLayout::BeginLineReflow(nscoord aICoord, nscoord aBCoord,
   1.141 +                              nscoord aISize, nscoord aBSize,
   1.142 +                              bool aImpactedByFloats,
   1.143 +                              bool aIsTopOfPage,
   1.144 +                              WritingMode aWritingMode,
   1.145 +                              nscoord aContainerWidth)
   1.146 +{
   1.147 +  NS_ASSERTION(nullptr == mRootSpan, "bad linelayout user");
   1.148 +  NS_WARN_IF_FALSE(aISize != NS_UNCONSTRAINEDSIZE,
   1.149 +                   "have unconstrained width; this should only result from "
   1.150 +                   "very large sizes, not attempts at intrinsic width "
   1.151 +                   "calculation");
   1.152 +#ifdef DEBUG
   1.153 +  if ((aISize != NS_UNCONSTRAINEDSIZE) && CRAZY_SIZE(aISize)) {
   1.154 +    nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.155 +    printf(": Init: bad caller: width WAS %d(0x%x)\n",
   1.156 +           aISize, aISize);
   1.157 +  }
   1.158 +  if ((aBSize != NS_UNCONSTRAINEDSIZE) && CRAZY_SIZE(aBSize)) {
   1.159 +    nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.160 +    printf(": Init: bad caller: height WAS %d(0x%x)\n",
   1.161 +           aBSize, aBSize);
   1.162 +  }
   1.163 +#endif
   1.164 +#ifdef NOISY_REFLOW
   1.165 +  nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.166 +  printf(": BeginLineReflow: %d,%d,%d,%d impacted=%s %s\n",
   1.167 +         aICoord, aBCoord, aISize, aBSize,
   1.168 +         aImpactedByFloats?"true":"false",
   1.169 +         aIsTopOfPage ? "top-of-page" : "");
   1.170 +#endif
   1.171 +#ifdef DEBUG
   1.172 +  mSpansAllocated = mSpansFreed = mFramesAllocated = mFramesFreed = 0;
   1.173 +#endif
   1.174 +
   1.175 +  mFirstLetterStyleOK = false;
   1.176 +  mIsTopOfPage = aIsTopOfPage;
   1.177 +  mImpactedByFloats = aImpactedByFloats;
   1.178 +  mTotalPlacedFrames = 0;
   1.179 +  mLineIsEmpty = true;
   1.180 +  mLineAtStart = true;
   1.181 +  mLineEndsInBR = false;
   1.182 +  mSpanDepth = 0;
   1.183 +  mMaxStartBoxBSize = mMaxEndBoxBSize = 0;
   1.184 +
   1.185 +  if (mGotLineBox) {
   1.186 +    mLineBox->ClearHasBullet();
   1.187 +  }
   1.188 +
   1.189 +  PerSpanData* psd = NewPerSpanData();
   1.190 +  mCurrentSpan = mRootSpan = psd;
   1.191 +  psd->mReflowState = mBlockReflowState;
   1.192 +  psd->mIStart = aICoord;
   1.193 +  psd->mICoord = aICoord;
   1.194 +  psd->mIEnd = aICoord + aISize;
   1.195 +  mContainerWidth = aContainerWidth;
   1.196 +
   1.197 +  // If we're in a constrained height frame, then we don't allow a
   1.198 +  // max line box width to take effect.
   1.199 +  if (!(LineContainerFrame()->GetStateBits() &
   1.200 +        NS_FRAME_IN_CONSTRAINED_HEIGHT)) {
   1.201 +
   1.202 +    // If the available size is greater than the maximum line box width (if
   1.203 +    // specified), then we need to adjust the line box width to be at the max
   1.204 +    // possible width.
   1.205 +    nscoord maxLineBoxWidth =
   1.206 +      LineContainerFrame()->PresContext()->PresShell()->MaxLineBoxWidth();
   1.207 +
   1.208 +    if (maxLineBoxWidth > 0 &&
   1.209 +        psd->mIEnd - psd->mIStart > maxLineBoxWidth) {
   1.210 +      psd->mIEnd = psd->mIStart + maxLineBoxWidth;
   1.211 +    }
   1.212 +  }
   1.213 +
   1.214 +  mBStartEdge = aBCoord;
   1.215 +
   1.216 +  psd->mNoWrap =
   1.217 +    !mStyleText->WhiteSpaceCanWrapStyle() || LineContainerFrame()->IsSVGText();
   1.218 +  psd->mWritingMode = aWritingMode;
   1.219 +
   1.220 +  // If this is the first line of a block then see if the text-indent
   1.221 +  // property amounts to anything.
   1.222 +
   1.223 +  if (0 == mLineNumber && !HasPrevInFlow(mBlockReflowState->frame)) {
   1.224 +    const nsStyleCoord &textIndent = mStyleText->mTextIndent;
   1.225 +    nscoord pctBasis = 0;
   1.226 +    if (textIndent.HasPercent()) {
   1.227 +      pctBasis =
   1.228 +        nsHTMLReflowState::GetContainingBlockContentWidth(mBlockReflowState);
   1.229 +
   1.230 +      if (mGotLineBox) {
   1.231 +        mLineBox->DisableResizeReflowOptimization();
   1.232 +      }
   1.233 +    }
   1.234 +    nscoord indent = nsRuleNode::ComputeCoordPercentCalc(textIndent, pctBasis);
   1.235 +
   1.236 +    mTextIndent = indent;
   1.237 +
   1.238 +    psd->mICoord += indent;
   1.239 +  }
   1.240 +}
   1.241 +
   1.242 +void
   1.243 +nsLineLayout::EndLineReflow()
   1.244 +{
   1.245 +#ifdef NOISY_REFLOW
   1.246 +  nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.247 +  printf(": EndLineReflow: width=%d\n", mRootSpan->mICoord - mRootSpan->mIStart);
   1.248 +#endif
   1.249 +
   1.250 +  FreeSpan(mRootSpan);
   1.251 +  mCurrentSpan = mRootSpan = nullptr;
   1.252 +
   1.253 +  NS_ASSERTION(mSpansAllocated == mSpansFreed, "leak");
   1.254 +  NS_ASSERTION(mFramesAllocated == mFramesFreed, "leak");
   1.255 +
   1.256 +#if 0
   1.257 +  static int32_t maxSpansAllocated = NS_LINELAYOUT_NUM_SPANS;
   1.258 +  static int32_t maxFramesAllocated = NS_LINELAYOUT_NUM_FRAMES;
   1.259 +  if (mSpansAllocated > maxSpansAllocated) {
   1.260 +    printf("XXX: saw a line with %d spans\n", mSpansAllocated);
   1.261 +    maxSpansAllocated = mSpansAllocated;
   1.262 +  }
   1.263 +  if (mFramesAllocated > maxFramesAllocated) {
   1.264 +    printf("XXX: saw a line with %d frames\n", mFramesAllocated);
   1.265 +    maxFramesAllocated = mFramesAllocated;
   1.266 +  }
   1.267 +#endif
   1.268 +}
   1.269 +
   1.270 +// XXX swtich to a single mAvailLineWidth that we adjust as each frame
   1.271 +// on the line is placed. Each span can still have a per-span mICoord that
   1.272 +// tracks where a child frame is going in its span; they don't need a
   1.273 +// per-span mIStart?
   1.274 +
   1.275 +void
   1.276 +nsLineLayout::UpdateBand(const nsRect& aNewAvailSpace,
   1.277 +                         nsIFrame* aFloatFrame)
   1.278 +{
   1.279 +  WritingMode lineWM = mRootSpan->mWritingMode;
   1.280 +  LogicalRect availSpace(lineWM, aNewAvailSpace, mContainerWidth);
   1.281 +#ifdef REALLY_NOISY_REFLOW
   1.282 +  printf("nsLL::UpdateBand %d, %d, %d, %d, (logical %d, %d, %d, %d); frame=%p\n  will set mImpacted to true\n",
   1.283 +         aNewAvailSpace.x, aNewAvailSpace.y,
   1.284 +         aNewAvailSpace.width, aNewAvailSpace.height,
   1.285 +         availSpace.IStart(lineWM), availSpace.BStart(lineWM),
   1.286 +         availSpace.ISize(lineWM), availSpace.BSize(lineWM),
   1.287 +         aFloatFrame);
   1.288 +#endif
   1.289 +#ifdef DEBUG
   1.290 +  if ((availSpace.ISize(lineWM) != NS_UNCONSTRAINEDSIZE) &&
   1.291 +      CRAZY_SIZE(availSpace.ISize(lineWM))) {
   1.292 +    nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.293 +    printf(": UpdateBand: bad caller: ISize WAS %d(0x%x)\n",
   1.294 +           availSpace.ISize(lineWM), availSpace.ISize(lineWM));
   1.295 +  }
   1.296 +  if ((availSpace.BSize(lineWM) != NS_UNCONSTRAINEDSIZE) &&
   1.297 +      CRAZY_SIZE(availSpace.BSize(lineWM))) {
   1.298 +    nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.299 +    printf(": UpdateBand: bad caller: BSize WAS %d(0x%x)\n",
   1.300 +           availSpace.BSize(lineWM), availSpace.BSize(lineWM));
   1.301 +  }
   1.302 +#endif
   1.303 +
   1.304 +  // Compute the difference between last times width and the new width
   1.305 +  NS_WARN_IF_FALSE(mRootSpan->mIEnd != NS_UNCONSTRAINEDSIZE &&
   1.306 +                   aNewAvailSpace.width != NS_UNCONSTRAINEDSIZE,
   1.307 +                   "have unconstrained width; this should only result from "
   1.308 +                   "very large sizes, not attempts at intrinsic width "
   1.309 +                   "calculation");
   1.310 +  // The root span's mIStart moves to aICoord
   1.311 +  nscoord deltaICoord = availSpace.IStart(lineWM) - mRootSpan->mIStart;
   1.312 +  // The width of all spans changes by this much (the root span's
   1.313 +  // mIEnd moves to aICoord + aISize, its new width is aISize)
   1.314 +  nscoord deltaISize = availSpace.ISize(lineWM) -
   1.315 +                       (mRootSpan->mIEnd - mRootSpan->mIStart);
   1.316 +#ifdef NOISY_REFLOW
   1.317 +  nsFrame::ListTag(stdout, mBlockReflowState->frame);
   1.318 +  printf(": UpdateBand: %d,%d,%d,%d deltaISize=%d deltaICoord=%d\n",
   1.319 +         aNewAvailSpace.x, aNewAvailSpace.y,
   1.320 +         aNewAvailSpace.width, aNewAvailSpace.height, deltaISize, deltaICoord);
   1.321 +#endif
   1.322 +
   1.323 +  // Update the root span position
   1.324 +  mRootSpan->mIStart += deltaICoord;
   1.325 +  mRootSpan->mIEnd += deltaICoord;
   1.326 +  mRootSpan->mICoord += deltaICoord;
   1.327 +
   1.328 +  // Now update the right edges of the open spans to account for any
   1.329 +  // change in available space width
   1.330 +  for (PerSpanData* psd = mCurrentSpan; psd; psd = psd->mParent) {
   1.331 +    psd->mIEnd += deltaISize;
   1.332 +    psd->mContainsFloat = true;
   1.333 +#ifdef NOISY_REFLOW
   1.334 +    printf("  span %p: oldIEnd=%d newIEnd=%d\n",
   1.335 +           psd, psd->mIEnd - deltaISize, psd->mIEnd);
   1.336 +#endif
   1.337 +  }
   1.338 +  NS_ASSERTION(mRootSpan->mContainsFloat &&
   1.339 +               mRootSpan->mIStart == availSpace.IStart(lineWM) &&
   1.340 +               mRootSpan->mIEnd == availSpace.IEnd(lineWM),
   1.341 +               "root span was updated incorrectly?");
   1.342 +
   1.343 +  // Update frame bounds
   1.344 +  // Note: Only adjust the outermost frames (the ones that are direct
   1.345 +  // children of the block), not the ones in the child spans. The reason
   1.346 +  // is simple: the frames in the spans have coordinates local to their
   1.347 +  // parent therefore they are moved when their parent span is moved.
   1.348 +  if (deltaICoord != 0) {
   1.349 +    for (PerFrameData* pfd = mRootSpan->mFirstFrame; pfd; pfd = pfd->mNext) {
   1.350 +      pfd->mBounds.IStart(lineWM) += deltaICoord;
   1.351 +    }
   1.352 +  }
   1.353 +
   1.354 +  mBStartEdge = aNewAvailSpace.y;
   1.355 +  mImpactedByFloats = true;
   1.356 +
   1.357 +  mLastFloatWasLetterFrame = nsGkAtoms::letterFrame == aFloatFrame->GetType();
   1.358 +}
   1.359 +
   1.360 +nsLineLayout::PerSpanData*
   1.361 +nsLineLayout::NewPerSpanData()
   1.362 +{
   1.363 +  PerSpanData* psd = mSpanFreeList;
   1.364 +  if (!psd) {
   1.365 +    void *mem;
   1.366 +    PL_ARENA_ALLOCATE(mem, &mArena, sizeof(PerSpanData));
   1.367 +    if (!mem) {
   1.368 +      NS_RUNTIMEABORT("OOM");
   1.369 +    }
   1.370 +    psd = reinterpret_cast<PerSpanData*>(mem);
   1.371 +  }
   1.372 +  else {
   1.373 +    mSpanFreeList = psd->mNextFreeSpan;
   1.374 +  }
   1.375 +  psd->mParent = nullptr;
   1.376 +  psd->mFrame = nullptr;
   1.377 +  psd->mFirstFrame = nullptr;
   1.378 +  psd->mLastFrame = nullptr;
   1.379 +  psd->mContainsFloat = false;
   1.380 +  psd->mZeroEffectiveSpanBox = false;
   1.381 +  psd->mHasNonemptyContent = false;
   1.382 +
   1.383 +#ifdef DEBUG
   1.384 +  mSpansAllocated++;
   1.385 +#endif
   1.386 +  return psd;
   1.387 +}
   1.388 +
   1.389 +void
   1.390 +nsLineLayout::BeginSpan(nsIFrame* aFrame,
   1.391 +                        const nsHTMLReflowState* aSpanReflowState,
   1.392 +                        nscoord aIStart, nscoord aIEnd,
   1.393 +                        nscoord* aBaseline)
   1.394 +{
   1.395 +  NS_ASSERTION(aIEnd != NS_UNCONSTRAINEDSIZE,
   1.396 +               "should no longer be using unconstrained sizes");
   1.397 +#ifdef NOISY_REFLOW
   1.398 +  nsFrame::IndentBy(stdout, mSpanDepth+1);
   1.399 +  nsFrame::ListTag(stdout, aFrame);
   1.400 +  printf(": BeginSpan leftEdge=%d rightEdge=%d\n", aIStart, aIEnd);
   1.401 +#endif
   1.402 +
   1.403 +  PerSpanData* psd = NewPerSpanData();
   1.404 +  // Link up span frame's pfd to point to its child span data
   1.405 +  PerFrameData* pfd = mCurrentSpan->mLastFrame;
   1.406 +  NS_ASSERTION(pfd->mFrame == aFrame, "huh?");
   1.407 +  pfd->mSpan = psd;
   1.408 +
   1.409 +  // Init new span
   1.410 +  psd->mFrame = pfd;
   1.411 +  psd->mParent = mCurrentSpan;
   1.412 +  psd->mReflowState = aSpanReflowState;
   1.413 +  psd->mIStart = aIStart;
   1.414 +  psd->mICoord = aIStart;
   1.415 +  psd->mIEnd = aIEnd;
   1.416 +  psd->mBaseline = aBaseline;
   1.417 +
   1.418 +  nsIFrame* frame = aSpanReflowState->frame;
   1.419 +  psd->mNoWrap = !frame->StyleText()->WhiteSpaceCanWrap(frame);
   1.420 +  psd->mWritingMode = aSpanReflowState->GetWritingMode();
   1.421 +
   1.422 +  // Switch to new span
   1.423 +  mCurrentSpan = psd;
   1.424 +  mSpanDepth++;
   1.425 +}
   1.426 +
   1.427 +nscoord
   1.428 +nsLineLayout::EndSpan(nsIFrame* aFrame)
   1.429 +{
   1.430 +  NS_ASSERTION(mSpanDepth > 0, "end-span without begin-span");
   1.431 +#ifdef NOISY_REFLOW
   1.432 +  nsFrame::IndentBy(stdout, mSpanDepth);
   1.433 +  nsFrame::ListTag(stdout, aFrame);
   1.434 +  printf(": EndSpan width=%d\n", mCurrentSpan->mICoord - mCurrentSpan->mIStart);
   1.435 +#endif
   1.436 +  PerSpanData* psd = mCurrentSpan;
   1.437 +  nscoord iSizeResult = psd->mLastFrame ? (psd->mICoord - psd->mIStart) : 0;
   1.438 +
   1.439 +  mSpanDepth--;
   1.440 +  mCurrentSpan->mReflowState = nullptr;  // no longer valid so null it out!
   1.441 +  mCurrentSpan = mCurrentSpan->mParent;
   1.442 +  return iSizeResult;
   1.443 +}
   1.444 +
   1.445 +int32_t
   1.446 +nsLineLayout::GetCurrentSpanCount() const
   1.447 +{
   1.448 +  NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
   1.449 +  int32_t count = 0;
   1.450 +  PerFrameData* pfd = mRootSpan->mFirstFrame;
   1.451 +  while (nullptr != pfd) {
   1.452 +    count++;
   1.453 +    pfd = pfd->mNext;
   1.454 +  }
   1.455 +  return count;
   1.456 +}
   1.457 +
   1.458 +void
   1.459 +nsLineLayout::SplitLineTo(int32_t aNewCount)
   1.460 +{
   1.461 +  NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
   1.462 +
   1.463 +#ifdef REALLY_NOISY_PUSHING
   1.464 +  printf("SplitLineTo %d (current count=%d); before:\n", aNewCount,
   1.465 +         GetCurrentSpanCount());
   1.466 +  DumpPerSpanData(mRootSpan, 1);
   1.467 +#endif
   1.468 +  PerSpanData* psd = mRootSpan;
   1.469 +  PerFrameData* pfd = psd->mFirstFrame;
   1.470 +  while (nullptr != pfd) {
   1.471 +    if (--aNewCount == 0) {
   1.472 +      // Truncate list at pfd (we keep pfd, but anything following is freed)
   1.473 +      PerFrameData* next = pfd->mNext;
   1.474 +      pfd->mNext = nullptr;
   1.475 +      psd->mLastFrame = pfd;
   1.476 +
   1.477 +      // Now release all of the frames following pfd
   1.478 +      pfd = next;
   1.479 +      while (nullptr != pfd) {
   1.480 +        next = pfd->mNext;
   1.481 +        pfd->mNext = mFrameFreeList;
   1.482 +        mFrameFreeList = pfd;
   1.483 +#ifdef DEBUG
   1.484 +        mFramesFreed++;
   1.485 +#endif
   1.486 +        if (nullptr != pfd->mSpan) {
   1.487 +          FreeSpan(pfd->mSpan);
   1.488 +        }
   1.489 +        pfd = next;
   1.490 +      }
   1.491 +      break;
   1.492 +    }
   1.493 +    pfd = pfd->mNext;
   1.494 +  }
   1.495 +#ifdef NOISY_PUSHING
   1.496 +  printf("SplitLineTo %d (current count=%d); after:\n", aNewCount,
   1.497 +         GetCurrentSpanCount());
   1.498 +  DumpPerSpanData(mRootSpan, 1);
   1.499 +#endif
   1.500 +}
   1.501 +
   1.502 +void
   1.503 +nsLineLayout::PushFrame(nsIFrame* aFrame)
   1.504 +{
   1.505 +  PerSpanData* psd = mCurrentSpan;
   1.506 +  NS_ASSERTION(psd->mLastFrame->mFrame == aFrame, "pushing non-last frame");
   1.507 +
   1.508 +#ifdef REALLY_NOISY_PUSHING
   1.509 +  nsFrame::IndentBy(stdout, mSpanDepth);
   1.510 +  printf("PushFrame %p, before:\n", psd);
   1.511 +  DumpPerSpanData(psd, 1);
   1.512 +#endif
   1.513 +
   1.514 +  // Take the last frame off of the span's frame list
   1.515 +  PerFrameData* pfd = psd->mLastFrame;
   1.516 +  if (pfd == psd->mFirstFrame) {
   1.517 +    // We are pushing away the only frame...empty the list
   1.518 +    psd->mFirstFrame = nullptr;
   1.519 +    psd->mLastFrame = nullptr;
   1.520 +  }
   1.521 +  else {
   1.522 +    PerFrameData* prevFrame = pfd->mPrev;
   1.523 +    prevFrame->mNext = nullptr;
   1.524 +    psd->mLastFrame = prevFrame;
   1.525 +  }
   1.526 +
   1.527 +  // Now free it, and if it has a span, free that too
   1.528 +  pfd->mNext = mFrameFreeList;
   1.529 +  mFrameFreeList = pfd;
   1.530 +#ifdef DEBUG
   1.531 +  mFramesFreed++;
   1.532 +#endif
   1.533 +  if (nullptr != pfd->mSpan) {
   1.534 +    FreeSpan(pfd->mSpan);
   1.535 +  }
   1.536 +#ifdef NOISY_PUSHING
   1.537 +  nsFrame::IndentBy(stdout, mSpanDepth);
   1.538 +  printf("PushFrame: %p after:\n", psd);
   1.539 +  DumpPerSpanData(psd, 1);
   1.540 +#endif
   1.541 +}
   1.542 +
   1.543 +void
   1.544 +nsLineLayout::FreeSpan(PerSpanData* psd)
   1.545 +{
   1.546 +  // Free its frames
   1.547 +  PerFrameData* pfd = psd->mFirstFrame;
   1.548 +  while (nullptr != pfd) {
   1.549 +    if (nullptr != pfd->mSpan) {
   1.550 +      FreeSpan(pfd->mSpan);
   1.551 +    }
   1.552 +    PerFrameData* next = pfd->mNext;
   1.553 +    pfd->mNext = mFrameFreeList;
   1.554 +    mFrameFreeList = pfd;
   1.555 +#ifdef DEBUG
   1.556 +    mFramesFreed++;
   1.557 +#endif
   1.558 +    pfd = next;
   1.559 +  }
   1.560 +
   1.561 +  // Now put the span on the free list since it's free too
   1.562 +  psd->mNextFreeSpan = mSpanFreeList;
   1.563 +  mSpanFreeList = psd;
   1.564 +#ifdef DEBUG
   1.565 +  mSpansFreed++;
   1.566 +#endif
   1.567 +}
   1.568 +
   1.569 +bool
   1.570 +nsLineLayout::IsZeroBSize()
   1.571 +{
   1.572 +  PerSpanData* psd = mCurrentSpan;
   1.573 +  PerFrameData* pfd = psd->mFirstFrame;
   1.574 +  while (nullptr != pfd) {
   1.575 +    if (0 != pfd->mBounds.BSize(psd->mWritingMode)) {
   1.576 +      return false;
   1.577 +    }
   1.578 +    pfd = pfd->mNext;
   1.579 +  }
   1.580 +  return true;
   1.581 +}
   1.582 +
   1.583 +nsLineLayout::PerFrameData*
   1.584 +nsLineLayout::NewPerFrameData(nsIFrame* aFrame)
   1.585 +{
   1.586 +  PerFrameData* pfd = mFrameFreeList;
   1.587 +  if (!pfd) {
   1.588 +    void *mem;
   1.589 +    PL_ARENA_ALLOCATE(mem, &mArena, sizeof(PerFrameData));
   1.590 +    if (!mem) {
   1.591 +      NS_RUNTIMEABORT("OOM");
   1.592 +    }
   1.593 +    pfd = reinterpret_cast<PerFrameData*>(mem);
   1.594 +  }
   1.595 +  else {
   1.596 +    mFrameFreeList = pfd->mNext;
   1.597 +  }
   1.598 +  pfd->mSpan = nullptr;
   1.599 +  pfd->mNext = nullptr;
   1.600 +  pfd->mPrev = nullptr;
   1.601 +  pfd->mFlags = 0;  // all flags default to false
   1.602 +  pfd->mFrame = aFrame;
   1.603 +
   1.604 +  WritingMode frameWM = aFrame->GetWritingMode();
   1.605 +  WritingMode lineWM = mRootSpan->mWritingMode;
   1.606 +  pfd->mBounds = LogicalRect(lineWM);
   1.607 +  pfd->mMargin = LogicalMargin(frameWM);
   1.608 +  pfd->mBorderPadding = LogicalMargin(frameWM);
   1.609 +  pfd->mOffsets = LogicalMargin(frameWM);
   1.610 +
   1.611 +#ifdef DEBUG
   1.612 +  pfd->mBlockDirAlign = 0xFF;
   1.613 +  mFramesAllocated++;
   1.614 +#endif
   1.615 +  return pfd;
   1.616 +}
   1.617 +
   1.618 +bool
   1.619 +nsLineLayout::LineIsBreakable() const
   1.620 +{
   1.621 +  // XXX mTotalPlacedFrames should go away and we should just use
   1.622 +  // mLineIsEmpty here instead
   1.623 +  if ((0 != mTotalPlacedFrames) || mImpactedByFloats) {
   1.624 +    return true;
   1.625 +  }
   1.626 +  return false;
   1.627 +}
   1.628 +
   1.629 +// Checks all four sides for percentage units.  This means it should
   1.630 +// only be used for things (margin, padding) where percentages on top
   1.631 +// and bottom depend on the *width* just like percentages on left and
   1.632 +// right.
   1.633 +static bool
   1.634 +HasPercentageUnitSide(const nsStyleSides& aSides)
   1.635 +{
   1.636 +  NS_FOR_CSS_SIDES(side) {
   1.637 +    if (aSides.Get(side).HasPercent())
   1.638 +      return true;
   1.639 +  }
   1.640 +  return false;
   1.641 +}
   1.642 +
   1.643 +static bool
   1.644 +IsPercentageAware(const nsIFrame* aFrame)
   1.645 +{
   1.646 +  NS_ASSERTION(aFrame, "null frame is not allowed");
   1.647 +
   1.648 +  nsIAtom *fType = aFrame->GetType();
   1.649 +  if (fType == nsGkAtoms::textFrame) {
   1.650 +    // None of these things can ever be true for text frames.
   1.651 +    return false;
   1.652 +  }
   1.653 +
   1.654 +  // Some of these things don't apply to non-replaced inline frames
   1.655 +  // (that is, fType == nsGkAtoms::inlineFrame), but we won't bother making
   1.656 +  // things unnecessarily complicated, since they'll probably be set
   1.657 +  // quite rarely.
   1.658 +
   1.659 +  const nsStyleMargin* margin = aFrame->StyleMargin();
   1.660 +  if (HasPercentageUnitSide(margin->mMargin)) {
   1.661 +    return true;
   1.662 +  }
   1.663 +
   1.664 +  const nsStylePadding* padding = aFrame->StylePadding();
   1.665 +  if (HasPercentageUnitSide(padding->mPadding)) {
   1.666 +    return true;
   1.667 +  }
   1.668 +
   1.669 +  // Note that borders can't be aware of percentages
   1.670 +
   1.671 +  const nsStylePosition* pos = aFrame->StylePosition();
   1.672 +
   1.673 +  if ((pos->WidthDependsOnContainer() &&
   1.674 +       pos->mWidth.GetUnit() != eStyleUnit_Auto) ||
   1.675 +      pos->MaxWidthDependsOnContainer() ||
   1.676 +      pos->MinWidthDependsOnContainer() ||
   1.677 +      pos->OffsetHasPercent(NS_SIDE_RIGHT) ||
   1.678 +      pos->OffsetHasPercent(NS_SIDE_LEFT)) {
   1.679 +    return true;
   1.680 +  }
   1.681 +
   1.682 +  if (eStyleUnit_Auto == pos->mWidth.GetUnit()) {
   1.683 +    // We need to check for frames that shrink-wrap when they're auto
   1.684 +    // width.
   1.685 +    const nsStyleDisplay* disp = aFrame->StyleDisplay();
   1.686 +    if (disp->mDisplay == NS_STYLE_DISPLAY_INLINE_BLOCK ||
   1.687 +        disp->mDisplay == NS_STYLE_DISPLAY_INLINE_TABLE ||
   1.688 +        fType == nsGkAtoms::HTMLButtonControlFrame ||
   1.689 +        fType == nsGkAtoms::gfxButtonControlFrame ||
   1.690 +        fType == nsGkAtoms::fieldSetFrame ||
   1.691 +        fType == nsGkAtoms::comboboxDisplayFrame) {
   1.692 +      return true;
   1.693 +    }
   1.694 +
   1.695 +    // Per CSS 2.1, section 10.3.2:
   1.696 +    //   If 'height' and 'width' both have computed values of 'auto' and
   1.697 +    //   the element has an intrinsic ratio but no intrinsic height or
   1.698 +    //   width and the containing block's width does not itself depend
   1.699 +    //   on the replaced element's width, then the used value of 'width'
   1.700 +    //   is calculated from the constraint equation used for
   1.701 +    //   block-level, non-replaced elements in normal flow. 
   1.702 +    nsIFrame *f = const_cast<nsIFrame*>(aFrame);
   1.703 +    if (f->GetIntrinsicRatio() != nsSize(0, 0) &&
   1.704 +        // Some percents are treated like 'auto', so check != coord
   1.705 +        pos->mHeight.GetUnit() != eStyleUnit_Coord) {
   1.706 +      const IntrinsicSize &intrinsicSize = f->GetIntrinsicSize();
   1.707 +      if (intrinsicSize.width.GetUnit() == eStyleUnit_None &&
   1.708 +          intrinsicSize.height.GetUnit() == eStyleUnit_None) {
   1.709 +        return true;
   1.710 +      }
   1.711 +    }
   1.712 +  }
   1.713 +
   1.714 +  return false;
   1.715 +}
   1.716 +
   1.717 +nsresult
   1.718 +nsLineLayout::ReflowFrame(nsIFrame* aFrame,
   1.719 +                          nsReflowStatus& aReflowStatus,
   1.720 +                          nsHTMLReflowMetrics* aMetrics,
   1.721 +                          bool& aPushedFrame)
   1.722 +{
   1.723 +  // Initialize OUT parameter
   1.724 +  aPushedFrame = false;
   1.725 +
   1.726 +  PerFrameData* pfd = NewPerFrameData(aFrame);
   1.727 +  PerSpanData* psd = mCurrentSpan;
   1.728 +  psd->AppendFrame(pfd);
   1.729 +
   1.730 +#ifdef REALLY_NOISY_REFLOW
   1.731 +  nsFrame::IndentBy(stdout, mSpanDepth);
   1.732 +  printf("%p: Begin ReflowFrame pfd=%p ", psd, pfd);
   1.733 +  nsFrame::ListTag(stdout, aFrame);
   1.734 +  printf("\n");
   1.735 +#endif
   1.736 +
   1.737 +  mTextJustificationNumSpaces = 0;
   1.738 +  mTextJustificationNumLetters = 0;
   1.739 +
   1.740 +  // Stash copies of some of the computed state away for later
   1.741 +  // (block-direction alignment, for example)
   1.742 +  WritingMode frameWM = aFrame->GetWritingMode();
   1.743 +  WritingMode lineWM = mRootSpan->mWritingMode;
   1.744 +
   1.745 +  // NOTE: While the x coordinate remains relative to the parent span,
   1.746 +  // the y coordinate is fixed at the top edge for the line. During
   1.747 +  // BlockDirAlignFrames we will repair this so that the y coordinate
   1.748 +  // is properly set and relative to the appropriate span.
   1.749 +  pfd->mBounds.IStart(lineWM) = psd->mICoord;
   1.750 +  pfd->mBounds.BStart(lineWM) = mBStartEdge;
   1.751 +
   1.752 +  // We want to guarantee that we always make progress when
   1.753 +  // formatting. Therefore, if the object being placed on the line is
   1.754 +  // too big for the line, but it is the only thing on the line and is not
   1.755 +  // impacted by a float, then we go ahead and place it anyway. (If the line
   1.756 +  // is impacted by one or more floats, then it is safe to break because
   1.757 +  // we can move the line down below float(s).)
   1.758 +  //
   1.759 +  // Capture this state *before* we reflow the frame in case it clears
   1.760 +  // the state out. We need to know how to treat the current frame
   1.761 +  // when breaking.
   1.762 +  bool notSafeToBreak = LineIsEmpty() && !mImpactedByFloats;
   1.763 +
   1.764 +  // Figure out whether we're talking about a textframe here
   1.765 +  nsIAtom* frameType = aFrame->GetType();
   1.766 +  bool isText = frameType == nsGkAtoms::textFrame;
   1.767 +  
   1.768 +  // Compute the available size for the frame. This available width
   1.769 +  // includes room for the side margins.
   1.770 +  // For now, set the available height to unconstrained always.
   1.771 +  nsSize availSize(mBlockReflowState->ComputedWidth(), NS_UNCONSTRAINEDSIZE);
   1.772 +
   1.773 +  // Inline-ish and text-ish things don't compute their width;
   1.774 +  // everything else does.  We need to give them an available width that
   1.775 +  // reflects the space left on the line.
   1.776 +  NS_WARN_IF_FALSE(psd->mIEnd != NS_UNCONSTRAINEDSIZE,
   1.777 +                   "have unconstrained width; this should only result from "
   1.778 +                   "very large sizes, not attempts at intrinsic width "
   1.779 +                   "calculation");
   1.780 +  nscoord availableSpaceOnLine = psd->mIEnd - psd->mICoord;
   1.781 +
   1.782 +  // Setup reflow state for reflowing the frame
   1.783 +  Maybe<nsHTMLReflowState> reflowStateHolder;
   1.784 +  if (!isText) {
   1.785 +    reflowStateHolder.construct(mPresContext, *psd->mReflowState,
   1.786 +                                aFrame, availSize);
   1.787 +    nsHTMLReflowState& reflowState = reflowStateHolder.ref();
   1.788 +    reflowState.mLineLayout = this;
   1.789 +    reflowState.mFlags.mIsTopOfPage = mIsTopOfPage;
   1.790 +    if (reflowState.ComputedWidth() == NS_UNCONSTRAINEDSIZE)
   1.791 +      reflowState.AvailableWidth() = availableSpaceOnLine;
   1.792 +    WritingMode stateWM = reflowState.GetWritingMode();
   1.793 +    pfd->mMargin =
   1.794 +      reflowState.ComputedLogicalMargin().ConvertTo(frameWM, stateWM);
   1.795 +    pfd->mBorderPadding =
   1.796 +      reflowState.ComputedLogicalBorderPadding().ConvertTo(frameWM, stateWM);
   1.797 +    pfd->SetFlag(PFD_RELATIVEPOS,
   1.798 +                 reflowState.mStyleDisplay->IsRelativelyPositionedStyle());
   1.799 +    if (pfd->GetFlag(PFD_RELATIVEPOS)) {
   1.800 +      pfd->mOffsets =
   1.801 +        reflowState.ComputedLogicalOffsets().ConvertTo(frameWM, stateWM);
   1.802 +    }
   1.803 +
   1.804 +    // Calculate whether the the frame should have a start margin and
   1.805 +    // subtract the margin from the available width if necessary.
   1.806 +    // The margin will be applied to the starting inline coordinates of
   1.807 +    // the frame in CanPlaceFrame() after reflowing the frame.
   1.808 +    AllowForStartMargin(pfd, reflowState);
   1.809 +  }
   1.810 +  // if isText(), no need to propagate NS_FRAME_IS_DIRTY from the parent,
   1.811 +  // because reflow doesn't look at the dirty bits on the frame being reflowed.
   1.812 +
   1.813 +  // See if this frame depends on the width of its containing block.  If
   1.814 +  // so, disable resize reflow optimizations for the line.  (Note that,
   1.815 +  // to be conservative, we do this if we *try* to fit a frame on a
   1.816 +  // line, even if we don't succeed.)  (Note also that we can only make
   1.817 +  // this IsPercentageAware check *after* we've constructed our
   1.818 +  // nsHTMLReflowState, because that construction may be what forces aFrame
   1.819 +  // to lazily initialize its (possibly-percent-valued) intrinsic size.)
   1.820 +  if (mGotLineBox && IsPercentageAware(aFrame)) {
   1.821 +    mLineBox->DisableResizeReflowOptimization();
   1.822 +  }
   1.823 +
   1.824 +  // Let frame know that are reflowing it. Note that we don't bother
   1.825 +  // positioning the frame yet, because we're probably going to end up
   1.826 +  // moving it when we do the block-direction alignment
   1.827 +  aFrame->WillReflow(mPresContext);
   1.828 +
   1.829 +  // Adjust spacemanager coordinate system for the frame.
   1.830 +  nsHTMLReflowMetrics metrics(lineWM);
   1.831 +#ifdef DEBUG
   1.832 +  metrics.Width() = nscoord(0xdeadbeef);
   1.833 +  metrics.Height() = nscoord(0xdeadbeef);
   1.834 +#endif
   1.835 +  nsRect physicalBounds = pfd->mBounds.GetPhysicalRect(lineWM, mContainerWidth);
   1.836 +  nscoord tx = physicalBounds.x;
   1.837 +  nscoord ty = physicalBounds.y;
   1.838 +  mFloatManager->Translate(tx, ty);
   1.839 +
   1.840 +  int32_t savedOptionalBreakOffset;
   1.841 +  gfxBreakPriority savedOptionalBreakPriority;
   1.842 +  nsIContent* savedOptionalBreakContent =
   1.843 +    GetLastOptionalBreakPosition(&savedOptionalBreakOffset,
   1.844 +                                 &savedOptionalBreakPriority);
   1.845 +
   1.846 +  if (!isText) {
   1.847 +    nsresult rv = aFrame->Reflow(mPresContext, metrics, reflowStateHolder.ref(),
   1.848 +                                 aReflowStatus);
   1.849 +    if (NS_FAILED(rv)) {
   1.850 +      NS_WARNING( "Reflow of frame failed in nsLineLayout" );
   1.851 +      return rv;
   1.852 +    }
   1.853 +  } else {
   1.854 +    static_cast<nsTextFrame*>(aFrame)->
   1.855 +      ReflowText(*this, availableSpaceOnLine, psd->mReflowState->rendContext,
   1.856 +                 metrics, aReflowStatus);
   1.857 +  }
   1.858 +  
   1.859 +  pfd->mJustificationNumSpaces = mTextJustificationNumSpaces;
   1.860 +  pfd->mJustificationNumLetters = mTextJustificationNumLetters;
   1.861 +
   1.862 +  // See if the frame is a placeholderFrame and if it is process
   1.863 +  // the float. At the same time, check if the frame has any non-collapsed-away
   1.864 +  // content.
   1.865 +  bool placedFloat = false;
   1.866 +  bool isEmpty;
   1.867 +  if (!frameType) {
   1.868 +    isEmpty = pfd->mFrame->IsEmpty();
   1.869 +  } else {
   1.870 +    if (nsGkAtoms::placeholderFrame == frameType) {
   1.871 +      isEmpty = true;
   1.872 +      pfd->SetFlag(PFD_SKIPWHENTRIMMINGWHITESPACE, true);
   1.873 +      nsIFrame* outOfFlowFrame = nsLayoutUtils::GetFloatFromPlaceholder(aFrame);
   1.874 +      if (outOfFlowFrame) {
   1.875 +        // Add mTrimmableWidth to the available width since if the line ends
   1.876 +        // here, the width of the inline content will be reduced by
   1.877 +        // mTrimmableWidth.
   1.878 +        nscoord availableWidth = psd->mIEnd - (psd->mICoord - mTrimmableWidth);
   1.879 +        if (psd->mNoWrap) {
   1.880 +          // If we place floats after inline content where there's
   1.881 +          // no break opportunity, we don't know how much additional
   1.882 +          // width is required for the non-breaking content after the float,
   1.883 +          // so we can't know whether the float plus that content will fit
   1.884 +          // on the line. So for now, don't place floats after inline
   1.885 +          // content where there's no break opportunity. This is incorrect
   1.886 +          // but hopefully rare. Fixing it will require significant
   1.887 +          // restructuring of line layout.
   1.888 +          // We might as well allow zero-width floats to be placed, though.
   1.889 +          availableWidth = 0;
   1.890 +        }
   1.891 +        placedFloat = AddFloat(outOfFlowFrame, availableWidth);
   1.892 +        NS_ASSERTION(!(outOfFlowFrame->GetType() == nsGkAtoms::letterFrame &&
   1.893 +                       GetFirstLetterStyleOK()),
   1.894 +                    "FirstLetterStyle set on line with floating first letter");
   1.895 +      }
   1.896 +    }
   1.897 +    else if (isText) {
   1.898 +      // Note non-empty text-frames for inline frame compatibility hackery
   1.899 +      pfd->SetFlag(PFD_ISTEXTFRAME, true);
   1.900 +      nsTextFrame* textFrame = static_cast<nsTextFrame*>(pfd->mFrame);
   1.901 +      isEmpty = !textFrame->HasNoncollapsedCharacters();
   1.902 +      if (!isEmpty) {
   1.903 +        pfd->SetFlag(PFD_ISNONEMPTYTEXTFRAME, true);
   1.904 +        nsIContent* content = textFrame->GetContent();
   1.905 +
   1.906 +        const nsTextFragment* frag = content->GetText();
   1.907 +        if (frag) {
   1.908 +          pfd->SetFlag(PFD_ISNONWHITESPACETEXTFRAME,
   1.909 +                       !content->TextIsOnlyWhitespace());
   1.910 +        }
   1.911 +      }
   1.912 +    }
   1.913 +    else if (nsGkAtoms::brFrame == frameType) {
   1.914 +      pfd->SetFlag(PFD_SKIPWHENTRIMMINGWHITESPACE, true);
   1.915 +      isEmpty = false;
   1.916 +    } else {
   1.917 +      if (nsGkAtoms::letterFrame==frameType) {
   1.918 +        pfd->SetFlag(PFD_ISLETTERFRAME, true);
   1.919 +      }
   1.920 +      if (pfd->mSpan) {
   1.921 +        isEmpty = !pfd->mSpan->mHasNonemptyContent && pfd->mFrame->IsSelfEmpty();
   1.922 +      } else {
   1.923 +        isEmpty = pfd->mFrame->IsEmpty();
   1.924 +      }
   1.925 +    }
   1.926 +  }
   1.927 +
   1.928 +  mFloatManager->Translate(-tx, -ty);
   1.929 +
   1.930 +  NS_ASSERTION(metrics.Width() >= 0, "bad width");
   1.931 +  NS_ASSERTION(metrics.Height() >= 0,"bad height");
   1.932 +  if (metrics.Width() < 0) metrics.Width() = 0;
   1.933 +  if (metrics.Height() < 0) metrics.Height() = 0;
   1.934 +
   1.935 +#ifdef DEBUG
   1.936 +  // Note: break-before means ignore the reflow metrics since the
   1.937 +  // frame will be reflowed another time.
   1.938 +  if (!NS_INLINE_IS_BREAK_BEFORE(aReflowStatus)) {
   1.939 +    if (CRAZY_SIZE(metrics.Width()) || CRAZY_SIZE(metrics.Height())) {
   1.940 +      printf("nsLineLayout: ");
   1.941 +      nsFrame::ListTag(stdout, aFrame);
   1.942 +      printf(" metrics=%d,%d!\n", metrics.Width(), metrics.Height());
   1.943 +    }
   1.944 +    if ((metrics.Width() == nscoord(0xdeadbeef)) ||
   1.945 +        (metrics.Height() == nscoord(0xdeadbeef))) {
   1.946 +      printf("nsLineLayout: ");
   1.947 +      nsFrame::ListTag(stdout, aFrame);
   1.948 +      printf(" didn't set w/h %d,%d!\n", metrics.Width(), metrics.Height());
   1.949 +    }
   1.950 +  }
   1.951 +#endif
   1.952 +
   1.953 +  // Unlike with non-inline reflow, the overflow area here does *not*
   1.954 +  // include the accumulation of the frame's bounds and its inline
   1.955 +  // descendants' bounds. Nor does it include the outline area; it's
   1.956 +  // just the union of the bounds of any absolute children. That is
   1.957 +  // added in later by nsLineLayout::ReflowInlineFrames.
   1.958 +  pfd->mOverflowAreas = metrics.mOverflowAreas;
   1.959 +
   1.960 +  pfd->mBounds.ISize(lineWM) = metrics.ISize();
   1.961 +  pfd->mBounds.BSize(lineWM) = metrics.BSize();
   1.962 +
   1.963 +  // Size the frame, but |RelativePositionFrames| will size the view.
   1.964 +  aFrame->SetSize(nsSize(metrics.Width(), metrics.Height()));
   1.965 +
   1.966 +  // Tell the frame that we're done reflowing it
   1.967 +  aFrame->DidReflow(mPresContext,
   1.968 +                    isText ? nullptr : reflowStateHolder.addr(),
   1.969 +                    nsDidReflowStatus::FINISHED);
   1.970 +
   1.971 +  if (aMetrics) {
   1.972 +    *aMetrics = metrics;
   1.973 +  }
   1.974 +
   1.975 +  if (!NS_INLINE_IS_BREAK_BEFORE(aReflowStatus)) {
   1.976 +    // If frame is complete and has a next-in-flow, we need to delete
   1.977 +    // them now. Do not do this when a break-before is signaled because
   1.978 +    // the frame is going to get reflowed again (and may end up wanting
   1.979 +    // a next-in-flow where it ends up).
   1.980 +    if (NS_FRAME_IS_COMPLETE(aReflowStatus)) {
   1.981 +      nsIFrame* kidNextInFlow = aFrame->GetNextInFlow();
   1.982 +      if (nullptr != kidNextInFlow) {
   1.983 +        // Remove all of the childs next-in-flows. Make sure that we ask
   1.984 +        // the right parent to do the removal (it's possible that the
   1.985 +        // parent is not this because we are executing pullup code)
   1.986 +        nsContainerFrame* parent = static_cast<nsContainerFrame*>
   1.987 +                                                  (kidNextInFlow->GetParent());
   1.988 +        parent->DeleteNextInFlowChild(kidNextInFlow, true);
   1.989 +      }
   1.990 +    }
   1.991 +
   1.992 +    // Check whether this frame breaks up text runs. All frames break up text
   1.993 +    // runs (hence return false here) except for text frames and inline containers.
   1.994 +    bool continuingTextRun = aFrame->CanContinueTextRun();
   1.995 +    
   1.996 +    // Clear any residual mTrimmableWidth if this isn't a text frame
   1.997 +    if (!continuingTextRun && !pfd->GetFlag(PFD_SKIPWHENTRIMMINGWHITESPACE)) {
   1.998 +      mTrimmableWidth = 0;
   1.999 +    }
  1.1000 +
  1.1001 +    // See if we can place the frame. If we can't fit it, then we
  1.1002 +    // return now.
  1.1003 +    bool optionalBreakAfterFits;
  1.1004 +    NS_ASSERTION(isText ||
  1.1005 +                 !reflowStateHolder.ref().IsFloating(),
  1.1006 +                 "How'd we get a floated inline frame? "
  1.1007 +                 "The frame ctor should've dealt with this.");
  1.1008 +    if (CanPlaceFrame(pfd, notSafeToBreak, continuingTextRun,
  1.1009 +                      savedOptionalBreakContent != nullptr, metrics,
  1.1010 +                      aReflowStatus, &optionalBreakAfterFits)) {
  1.1011 +      if (!isEmpty) {
  1.1012 +        psd->mHasNonemptyContent = true;
  1.1013 +        mLineIsEmpty = false;
  1.1014 +        if (!pfd->mSpan) {
  1.1015 +          // nonempty leaf content has been placed
  1.1016 +          mLineAtStart = false;
  1.1017 +        }
  1.1018 +      }
  1.1019 +
  1.1020 +      // Place the frame, updating aBounds with the final size and
  1.1021 +      // location.  Then apply the bottom+right margins (as
  1.1022 +      // appropriate) to the frame.
  1.1023 +      PlaceFrame(pfd, metrics);
  1.1024 +      PerSpanData* span = pfd->mSpan;
  1.1025 +      if (span) {
  1.1026 +        // The frame we just finished reflowing is an inline
  1.1027 +        // container.  It needs its child frames aligned in the block direction,
  1.1028 +        // so do most of it now.
  1.1029 +        BlockDirAlignFrames(span);
  1.1030 +      }
  1.1031 +      
  1.1032 +      if (!continuingTextRun) {
  1.1033 +        if (!psd->mNoWrap && (!LineIsEmpty() || placedFloat)) {
  1.1034 +          // record soft break opportunity after this content that can't be
  1.1035 +          // part of a text run. This is not a text frame so we know
  1.1036 +          // that offset INT32_MAX means "after the content".
  1.1037 +          if (NotifyOptionalBreakPosition(aFrame->GetContent(), INT32_MAX, optionalBreakAfterFits, gfxBreakPriority::eNormalBreak)) {
  1.1038 +            // If this returns true then we are being told to actually break here.
  1.1039 +            aReflowStatus = NS_INLINE_LINE_BREAK_AFTER(aReflowStatus);
  1.1040 +          }
  1.1041 +        }
  1.1042 +      }
  1.1043 +    }
  1.1044 +    else {
  1.1045 +      PushFrame(aFrame);
  1.1046 +      aPushedFrame = true;
  1.1047 +      // Undo any saved break positions that the frame might have told us about,
  1.1048 +      // since we didn't end up placing it
  1.1049 +      RestoreSavedBreakPosition(savedOptionalBreakContent,
  1.1050 +                                savedOptionalBreakOffset,
  1.1051 +                                savedOptionalBreakPriority);
  1.1052 +    }
  1.1053 +  }
  1.1054 +  else {
  1.1055 +    PushFrame(aFrame);
  1.1056 +  }
  1.1057 +  
  1.1058 +#ifdef REALLY_NOISY_REFLOW
  1.1059 +  nsFrame::IndentBy(stdout, mSpanDepth);
  1.1060 +  printf("End ReflowFrame ");
  1.1061 +  nsFrame::ListTag(stdout, aFrame);
  1.1062 +  printf(" status=%x\n", aReflowStatus);
  1.1063 +#endif
  1.1064 +
  1.1065 +  return NS_OK;
  1.1066 +}
  1.1067 +
  1.1068 +void
  1.1069 +nsLineLayout::AllowForStartMargin(PerFrameData* pfd,
  1.1070 +                                  nsHTMLReflowState& aReflowState)
  1.1071 +{
  1.1072 +  NS_ASSERTION(!aReflowState.IsFloating(),
  1.1073 +               "How'd we get a floated inline frame? "
  1.1074 +               "The frame ctor should've dealt with this.");
  1.1075 +
  1.1076 +  WritingMode frameWM = pfd->mFrame->GetWritingMode();
  1.1077 +
  1.1078 +  // Only apply start-margin on the first-in flow for inline frames,
  1.1079 +  // and make sure to not apply it to any inline other than the first
  1.1080 +  // in an ib split.  Note that the ib sibling (block-in-inline
  1.1081 +  // sibling) annotations only live on the first continuation, but we
  1.1082 +  // don't want to apply the start margin for later continuations
  1.1083 +  // anyway.
  1.1084 +  if (pfd->mFrame->GetPrevContinuation() ||
  1.1085 +      pfd->mFrame->FrameIsNonFirstInIBSplit()) {
  1.1086 +    // Zero this out so that when we compute the max-element-width of
  1.1087 +    // the frame we will properly avoid adding in the starting margin.
  1.1088 +    pfd->mMargin.IStart(frameWM) = 0;
  1.1089 +  } else {
  1.1090 +    NS_WARN_IF_FALSE(NS_UNCONSTRAINEDSIZE != aReflowState.AvailableWidth(),
  1.1091 +                     "have unconstrained width; this should only result from "
  1.1092 +                     "very large sizes, not attempts at intrinsic width "
  1.1093 +                     "calculation");
  1.1094 +    if (NS_UNCONSTRAINEDSIZE == aReflowState.ComputedWidth()) {
  1.1095 +      // For inline-ish and text-ish things (which don't compute widths
  1.1096 +      // in the reflow state), adjust available width to account for the
  1.1097 +      // start margin. The end margin will be accounted for when we
  1.1098 +      // finish flowing the frame.
  1.1099 +      aReflowState.AvailableWidth() -= pfd->mMargin.IStart(frameWM);
  1.1100 +    }
  1.1101 +  }
  1.1102 +}
  1.1103 +
  1.1104 +nscoord
  1.1105 +nsLineLayout::GetCurrentFrameInlineDistanceFromBlock()
  1.1106 +{
  1.1107 +  PerSpanData* psd;
  1.1108 +  nscoord x = 0;
  1.1109 +  for (psd = mCurrentSpan; psd; psd = psd->mParent) {
  1.1110 +    x += psd->mICoord;
  1.1111 +  }
  1.1112 +  return x;
  1.1113 +}
  1.1114 +
  1.1115 +/**
  1.1116 + * See if the frame can be placed now that we know it's desired size.
  1.1117 + * We can always place the frame if the line is empty. Note that we
  1.1118 + * know that the reflow-status is not a break-before because if it was
  1.1119 + * ReflowFrame above would have returned false, preventing this method
  1.1120 + * from being called. The logic in this method assumes that.
  1.1121 + *
  1.1122 + * Note that there is no check against the Y coordinate because we
  1.1123 + * assume that the caller will take care of that.
  1.1124 + */
  1.1125 +bool
  1.1126 +nsLineLayout::CanPlaceFrame(PerFrameData* pfd,
  1.1127 +                            bool aNotSafeToBreak,
  1.1128 +                            bool aFrameCanContinueTextRun,
  1.1129 +                            bool aCanRollBackBeforeFrame,
  1.1130 +                            nsHTMLReflowMetrics& aMetrics,
  1.1131 +                            nsReflowStatus& aStatus,
  1.1132 +                            bool* aOptionalBreakAfterFits)
  1.1133 +{
  1.1134 +  NS_PRECONDITION(pfd && pfd->mFrame, "bad args, null pointers for frame data");
  1.1135 +
  1.1136 +  *aOptionalBreakAfterFits = true;
  1.1137 +
  1.1138 +  WritingMode frameWM = pfd->mFrame->GetWritingMode();
  1.1139 +  WritingMode lineWM = mRootSpan->mWritingMode;
  1.1140 +  /*
  1.1141 +   * We want to only apply the end margin if we're the last continuation and
  1.1142 +   * either not in an {ib} split or the last inline in it.  In all other
  1.1143 +   * cases we want to zero it out.  That means zeroing it out if any of these
  1.1144 +   * conditions hold:
  1.1145 +   * 1) The frame is not complete (in this case it will get a next-in-flow)
  1.1146 +   * 2) The frame is complete but has a non-fluid continuation on its
  1.1147 +   *    continuation chain.  Note that if it has a fluid continuation, that
  1.1148 +   *    continuation will get destroyed later, so we don't want to drop the
  1.1149 +   *    end-margin in that case.
  1.1150 +   * 3) The frame is in an {ib} split and is not the last part.
  1.1151 +   *
  1.1152 +   * However, none of that applies if this is a letter frame (XXXbz why?)
  1.1153 +   */
  1.1154 +  if ((NS_FRAME_IS_NOT_COMPLETE(aStatus) ||
  1.1155 +       pfd->mFrame->LastInFlow()->GetNextContinuation() ||
  1.1156 +       pfd->mFrame->FrameIsNonLastInIBSplit())
  1.1157 +      && !pfd->GetFlag(PFD_ISLETTERFRAME)) {
  1.1158 +    pfd->mMargin.IEnd(frameWM) = 0;
  1.1159 +  }
  1.1160 +
  1.1161 +  // Convert the frame's margins to the line's writing mode and apply
  1.1162 +  // the start margin to the frame bounds.
  1.1163 +  LogicalMargin usedMargins = pfd->mMargin.ConvertTo(lineWM, frameWM);
  1.1164 +  nscoord startMargin = usedMargins.IStart(lineWM);
  1.1165 +  nscoord endMargin = usedMargins.IEnd(lineWM);
  1.1166 +
  1.1167 +  pfd->mBounds.IStart(lineWM) += startMargin;
  1.1168 +
  1.1169 +  PerSpanData* psd = mCurrentSpan;
  1.1170 +  if (psd->mNoWrap) {
  1.1171 +    // When wrapping is off, everything fits.
  1.1172 +    return true;
  1.1173 +  }
  1.1174 +
  1.1175 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1176 +  if (nullptr != psd->mFrame) {
  1.1177 +    nsFrame::ListTag(stdout, psd->mFrame->mFrame);
  1.1178 +  }
  1.1179 +  else {
  1.1180 +    nsFrame::ListTag(stdout, mBlockReflowState->frame);
  1.1181 +  } 
  1.1182 +  printf(": aNotSafeToBreak=%s frame=", aNotSafeToBreak ? "true" : "false");
  1.1183 +  nsFrame::ListTag(stdout, pfd->mFrame);
  1.1184 +  printf(" frameWidth=%d, margins=%d,%d\n",
  1.1185 +         pfd->mBounds.IEnd(lineWM) + endMargin - psd->mICoord,
  1.1186 +         startMargin, endMargin);
  1.1187 +#endif
  1.1188 +
  1.1189 +  // Set outside to true if the result of the reflow leads to the
  1.1190 +  // frame sticking outside of our available area.
  1.1191 +  bool outside = pfd->mBounds.IEnd(lineWM) - mTrimmableWidth + endMargin >
  1.1192 +                 psd->mIEnd;
  1.1193 +  if (!outside) {
  1.1194 +    // If it fits, it fits
  1.1195 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1196 +    printf("   ==> inside\n");
  1.1197 +#endif
  1.1198 +    return true;
  1.1199 +  }
  1.1200 +  *aOptionalBreakAfterFits = false;
  1.1201 +
  1.1202 +  // When it doesn't fit, check for a few special conditions where we
  1.1203 +  // allow it to fit anyway.
  1.1204 +  if (0 == startMargin + pfd->mBounds.ISize(lineWM) + endMargin) {
  1.1205 +    // Empty frames always fit right where they are
  1.1206 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1207 +    printf("   ==> empty frame fits\n");
  1.1208 +#endif
  1.1209 +    return true;
  1.1210 +  }
  1.1211 +
  1.1212 +#ifdef FIX_BUG_50257
  1.1213 +  // another special case:  always place a BR
  1.1214 +  if (nsGkAtoms::brFrame == pfd->mFrame->GetType()) {
  1.1215 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1216 +    printf("   ==> BR frame fits\n");
  1.1217 +#endif
  1.1218 +    return true;
  1.1219 +  }
  1.1220 +#endif
  1.1221 +
  1.1222 +  if (aNotSafeToBreak) {
  1.1223 +    // There are no frames on the line that take up width and the line is
  1.1224 +    // not impacted by floats, so we must allow the current frame to be
  1.1225 +    // placed on the line
  1.1226 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1227 +    printf("   ==> not-safe and not-impacted fits: ");
  1.1228 +    while (nullptr != psd) {
  1.1229 +      printf("<psd=%p x=%d left=%d> ", psd, psd->mICoord, psd->mIStart);
  1.1230 +      psd = psd->mParent;
  1.1231 +    }
  1.1232 +    printf("\n");
  1.1233 +#endif
  1.1234 +    return true;
  1.1235 +  }
  1.1236 + 
  1.1237 +  // Special check for span frames
  1.1238 +  if (pfd->mSpan && pfd->mSpan->mContainsFloat) {
  1.1239 +    // If the span either directly or indirectly contains a float then
  1.1240 +    // it fits. Why? It's kind of complicated, but here goes:
  1.1241 +    //
  1.1242 +    // 1. CanPlaceFrame is used for all frame placements on a line,
  1.1243 +    // and in a span. This includes recursively placement of frames
  1.1244 +    // inside of spans, and the span itself. Because the logic always
  1.1245 +    // checks for room before proceeding (the code above here), the
  1.1246 +    // only things on a line will be those things that "fit".
  1.1247 +    //
  1.1248 +    // 2. Before a float is placed on a line, the line has to be empty
  1.1249 +    // (otherwise it's a "below current line" float and will be placed
  1.1250 +    // after the line).
  1.1251 +    //
  1.1252 +    // Therefore, if the span directly or indirectly has a float
  1.1253 +    // then it means that at the time of the placement of the float
  1.1254 +    // the line was empty. Because of #1, only the frames that fit can
  1.1255 +    // be added after that point, therefore we can assume that the
  1.1256 +    // current span being placed has fit.
  1.1257 +    //
  1.1258 +    // So how do we get here and have a span that should already fit
  1.1259 +    // and yet doesn't: Simple: span's that have the no-wrap attribute
  1.1260 +    // set on them and contain a float and are placed where they
  1.1261 +    // don't naturally fit.
  1.1262 +    return true;
  1.1263 + }
  1.1264 +
  1.1265 +  if (aFrameCanContinueTextRun) {
  1.1266 +    // Let it fit, but we reserve the right to roll back.
  1.1267 +    // Note that we usually won't get here because a text frame will break
  1.1268 +    // itself to avoid exceeding the available width.
  1.1269 +    // We'll only get here for text frames that couldn't break early enough.
  1.1270 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1271 +    printf("   ==> placing overflowing textrun, requesting backup\n");
  1.1272 +#endif
  1.1273 +
  1.1274 +    // We will want to try backup.
  1.1275 +    mNeedBackup = true;
  1.1276 +    return true;
  1.1277 +  }
  1.1278 +
  1.1279 +#ifdef NOISY_CAN_PLACE_FRAME
  1.1280 +  printf("   ==> didn't fit\n");
  1.1281 +#endif
  1.1282 +  aStatus = NS_INLINE_LINE_BREAK_BEFORE();
  1.1283 +  return false;
  1.1284 +}
  1.1285 +
  1.1286 +/**
  1.1287 + * Place the frame. Update running counters.
  1.1288 + */
  1.1289 +void
  1.1290 +nsLineLayout::PlaceFrame(PerFrameData* pfd, nsHTMLReflowMetrics& aMetrics)
  1.1291 +{
  1.1292 +  // Record ascent and update max-ascent and max-descent values
  1.1293 +  if (aMetrics.TopAscent() == nsHTMLReflowMetrics::ASK_FOR_BASELINE)
  1.1294 +    pfd->mAscent = pfd->mFrame->GetBaseline();
  1.1295 +  else
  1.1296 +    pfd->mAscent = aMetrics.TopAscent();
  1.1297 +
  1.1298 +  // Advance to next inline coordinate
  1.1299 +  WritingMode frameWM = pfd->mFrame->GetWritingMode();
  1.1300 +  WritingMode lineWM = mRootSpan->mWritingMode;
  1.1301 +  mCurrentSpan->mICoord = pfd->mBounds.IEnd(lineWM) +
  1.1302 +                          pfd->mMargin.ConvertTo(lineWM, frameWM).IEnd(lineWM);
  1.1303 +
  1.1304 +  // Count the number of non-placeholder frames on the line...
  1.1305 +  if (pfd->mFrame->GetType() == nsGkAtoms::placeholderFrame) {
  1.1306 +    NS_ASSERTION(pfd->mBounds.ISize(lineWM) == 0 &&
  1.1307 +                 pfd->mBounds.BSize(lineWM) == 0,
  1.1308 +                 "placeholders should have 0 width/height (checking "
  1.1309 +                 "placeholders were never counted by the old code in "
  1.1310 +                 "this function)");
  1.1311 +  } else {
  1.1312 +    mTotalPlacedFrames++;
  1.1313 +  }
  1.1314 +}
  1.1315 +
  1.1316 +void
  1.1317 +nsLineLayout::AddBulletFrame(nsIFrame* aFrame,
  1.1318 +                             const nsHTMLReflowMetrics& aMetrics)
  1.1319 +{
  1.1320 +  NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
  1.1321 +  NS_ASSERTION(mGotLineBox, "must have line box");
  1.1322 +
  1.1323 +  nsIFrame *blockFrame = mBlockReflowState->frame;
  1.1324 +  NS_ASSERTION(blockFrame->IsFrameOfType(nsIFrame::eBlockFrame),
  1.1325 +               "must be for block");
  1.1326 +  if (!static_cast<nsBlockFrame*>(blockFrame)->BulletIsEmpty()) {
  1.1327 +    mHasBullet = true;
  1.1328 +    mLineBox->SetHasBullet();
  1.1329 +  }
  1.1330 +
  1.1331 +  PerFrameData* pfd = NewPerFrameData(aFrame);
  1.1332 +  mRootSpan->AppendFrame(pfd);
  1.1333 +  pfd->SetFlag(PFD_ISBULLET, true);
  1.1334 +  if (aMetrics.TopAscent() == nsHTMLReflowMetrics::ASK_FOR_BASELINE)
  1.1335 +    pfd->mAscent = aFrame->GetBaseline();
  1.1336 +  else
  1.1337 +    pfd->mAscent = aMetrics.TopAscent();
  1.1338 +
  1.1339 +  // Note: block-coord value will be updated during block-direction alignment
  1.1340 +  pfd->mBounds = LogicalRect(mRootSpan->mWritingMode,
  1.1341 +                             aFrame->GetRect(), mContainerWidth);
  1.1342 +  pfd->mOverflowAreas = aMetrics.mOverflowAreas;
  1.1343 +}
  1.1344 +
  1.1345 +#ifdef DEBUG
  1.1346 +void
  1.1347 +nsLineLayout::DumpPerSpanData(PerSpanData* psd, int32_t aIndent)
  1.1348 +{
  1.1349 +  nsFrame::IndentBy(stdout, aIndent);
  1.1350 +  printf("%p: left=%d x=%d right=%d\n", static_cast<void*>(psd),
  1.1351 +         psd->mIStart, psd->mICoord, psd->mIEnd);
  1.1352 +  PerFrameData* pfd = psd->mFirstFrame;
  1.1353 +  while (nullptr != pfd) {
  1.1354 +    nsFrame::IndentBy(stdout, aIndent+1);
  1.1355 +    nsFrame::ListTag(stdout, pfd->mFrame);
  1.1356 +    nsRect rect = pfd->mBounds.GetPhysicalRect(psd->mWritingMode,
  1.1357 +                                               mContainerWidth);
  1.1358 +    printf(" %d,%d,%d,%d\n", rect.x, rect.y, rect.width, rect.height);
  1.1359 +    if (pfd->mSpan) {
  1.1360 +      DumpPerSpanData(pfd->mSpan, aIndent + 1);
  1.1361 +    }
  1.1362 +    pfd = pfd->mNext;
  1.1363 +  }
  1.1364 +}
  1.1365 +#endif
  1.1366 +
  1.1367 +#define VALIGN_OTHER  0
  1.1368 +#define VALIGN_TOP    1
  1.1369 +#define VALIGN_BOTTOM 2
  1.1370 +
  1.1371 +void
  1.1372 +nsLineLayout::BlockDirAlignLine()
  1.1373 +{
  1.1374 +  // Synthesize a PerFrameData for the block frame
  1.1375 +  PerFrameData rootPFD(mBlockReflowState->frame->GetWritingMode());
  1.1376 +  rootPFD.mFrame = mBlockReflowState->frame;
  1.1377 +  rootPFD.mAscent = 0;
  1.1378 +  mRootSpan->mFrame = &rootPFD;
  1.1379 +
  1.1380 +  // Partially place the children of the block frame. The baseline for
  1.1381 +  // this operation is set to zero so that the y coordinates for all
  1.1382 +  // of the placed children will be relative to there.
  1.1383 +  PerSpanData* psd = mRootSpan;
  1.1384 +  BlockDirAlignFrames(psd);
  1.1385 +
  1.1386 +  // *** Note that comments here still use the anachronistic term "line-height"
  1.1387 +  // when we really mean "size of the line in the block direction". This is
  1.1388 +  // partly for brevity and partly to retain the association with the CSS
  1.1389 +  // line-height property
  1.1390 +  //
  1.1391 +  // Compute the line-height. The line-height will be the larger of:
  1.1392 +  //
  1.1393 +  // [1] maxBCoord - minBCoord (the distance between the first child's
  1.1394 +  // block-start edge and the last child's block-end edge)
  1.1395 +  //
  1.1396 +  // [2] the maximum logical box block size (since not every frame may have
  1.1397 +  // participated in #1; for example: block-start/end aligned frames)
  1.1398 +  //
  1.1399 +  // [3] the minimum line height ("line-height" property set on the
  1.1400 +  // block frame)
  1.1401 +  nscoord lineBSize = psd->mMaxBCoord - psd->mMinBCoord;
  1.1402 +
  1.1403 +  // Now that the line-height is computed, we need to know where the
  1.1404 +  // baseline is in the line. Position baseline so that mMinBCoord is just
  1.1405 +  // inside the start of the line box.
  1.1406 +  nscoord baselineBCoord;
  1.1407 +  if (psd->mMinBCoord < 0) {
  1.1408 +    baselineBCoord = mBStartEdge - psd->mMinBCoord;
  1.1409 +  }
  1.1410 +  else {
  1.1411 +    baselineBCoord = mBStartEdge;
  1.1412 +  }
  1.1413 +
  1.1414 +  // It's also possible that the line block-size isn't tall enough because
  1.1415 +  // of block start/end aligned elements that were not accounted for in
  1.1416 +  // min/max BCoord.
  1.1417 +  //
  1.1418 +  // The CSS2 spec doesn't really say what happens when to the
  1.1419 +  // baseline in this situations. What we do is if the largest start
  1.1420 +  // aligned box block size is greater than the line block-size then we leave
  1.1421 +  // the baseline alone. If the largest end aligned box is greater
  1.1422 +  // than the line block-size then we slide the baseline forward by the extra
  1.1423 +  // amount.
  1.1424 +  //
  1.1425 +  // Navigator 4 gives precedence to the first top/bottom aligned
  1.1426 +  // object.  We just let block end aligned objects win.
  1.1427 +  if (lineBSize < mMaxEndBoxBSize) {
  1.1428 +    // When the line is shorter than the maximum block start aligned box
  1.1429 +    nscoord extra = mMaxEndBoxBSize - lineBSize;
  1.1430 +    baselineBCoord += extra;
  1.1431 +    lineBSize = mMaxEndBoxBSize;
  1.1432 +  }
  1.1433 +  if (lineBSize < mMaxStartBoxBSize) {
  1.1434 +    lineBSize = mMaxStartBoxBSize;
  1.1435 +  }
  1.1436 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1437 +  printf("  [line]==> lineBSize=%d baselineBCoord=%d\n", lineBSize, baselineBCoord);
  1.1438 +#endif
  1.1439 +
  1.1440 +  // Now position all of the frames in the root span. We will also
  1.1441 +  // recurse over the child spans and place any block start/end aligned
  1.1442 +  // frames we find.
  1.1443 +  // XXX PERFORMANCE: set a bit per-span to avoid the extra work
  1.1444 +  // (propagate it upward too)
  1.1445 +  WritingMode lineWM = psd->mWritingMode;
  1.1446 +  for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.1447 +    if (pfd->mBlockDirAlign == VALIGN_OTHER) {
  1.1448 +      pfd->mBounds.BStart(lineWM) += baselineBCoord;
  1.1449 +      pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.1450 +    }
  1.1451 +  }
  1.1452 +  PlaceStartEndFrames(psd, -mBStartEdge, lineBSize);
  1.1453 +
  1.1454 +  // If the frame being reflowed has text decorations, we simulate the
  1.1455 +  // propagation of those decorations to a line-level element by storing the
  1.1456 +  // offset in a frame property on any child frames that are aligned in the
  1.1457 +  // block direction somewhere other than the baseline. This property is then
  1.1458 +  // used by nsTextFrame::GetTextDecorations when the same conditions are met.
  1.1459 +  if (rootPFD.mFrame->StyleContext()->HasTextDecorationLines()) {
  1.1460 +    for (const PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.1461 +      const nsIFrame *const f = pfd->mFrame;
  1.1462 +      if (f->VerticalAlignEnum() != NS_STYLE_VERTICAL_ALIGN_BASELINE) {
  1.1463 +        const nscoord offset = baselineBCoord - pfd->mBounds.BStart(lineWM);
  1.1464 +        f->Properties().Set(nsIFrame::LineBaselineOffset(),
  1.1465 +                            NS_INT32_TO_PTR(offset));
  1.1466 +      }
  1.1467 +    }
  1.1468 +  }
  1.1469 +
  1.1470 +  // Fill in returned line-box and max-element-width data
  1.1471 +  mLineBox->SetBounds(lineWM,
  1.1472 +                      psd->mIStart, mBStartEdge,
  1.1473 +                      psd->mICoord - psd->mIStart, lineBSize,
  1.1474 +                      mContainerWidth);
  1.1475 +
  1.1476 +  mFinalLineBSize = lineBSize;
  1.1477 +  mLineBox->SetAscent(baselineBCoord - mBStartEdge);
  1.1478 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1479 +  printf(
  1.1480 +    "  [line]==> bounds{x,y,w,h}={%d,%d,%d,%d} lh=%d a=%d\n",
  1.1481 +    mLineBox->mBounds.IStart(lineWM), mLineBox->mBounds.BStart(lineWM),
  1.1482 +    mLineBox->mBounds.ISize(lineWM), mLineBox->mBounds.BSize(lineWM),
  1.1483 +    mFinalLineBSize, mLineBox->GetAscent());
  1.1484 +#endif
  1.1485 +
  1.1486 +  // Undo root-span mFrame pointer to prevent brane damage later on...
  1.1487 +  mRootSpan->mFrame = nullptr;
  1.1488 +}
  1.1489 +
  1.1490 +void
  1.1491 +nsLineLayout::PlaceStartEndFrames(PerSpanData* psd,
  1.1492 +                                  nscoord aDistanceFromStart,
  1.1493 +                                  nscoord aLineBSize)
  1.1494 +{
  1.1495 +  for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.1496 +    PerSpanData* span = pfd->mSpan;
  1.1497 +#ifdef DEBUG
  1.1498 +    NS_ASSERTION(0xFF != pfd->mBlockDirAlign, "umr");
  1.1499 +#endif
  1.1500 +    WritingMode frameWM = pfd->mFrame->GetWritingMode();
  1.1501 +    WritingMode lineWM = mRootSpan->mWritingMode;
  1.1502 +    switch (pfd->mBlockDirAlign) {
  1.1503 +      case VALIGN_TOP:
  1.1504 +        if (span) {
  1.1505 +          pfd->mBounds.BStart(lineWM) = -aDistanceFromStart - span->mMinBCoord;
  1.1506 +        }
  1.1507 +        else {
  1.1508 +          pfd->mBounds.BStart(lineWM) =
  1.1509 +            -aDistanceFromStart + pfd->mMargin.BStart(frameWM);
  1.1510 +        }
  1.1511 +        pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.1512 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1513 +        printf("    ");
  1.1514 +        nsFrame::ListTag(stdout, pfd->mFrame);
  1.1515 +        printf(": y=%d dTop=%d [bp.top=%d topLeading=%d]\n",
  1.1516 +               pfd->mBounds.BStart(lineWM), aDistanceFromStart,
  1.1517 +               span ? pfd->mBorderPadding.BStart(frameWM) : 0,
  1.1518 +               span ? span->mBStartLeading : 0);
  1.1519 +#endif
  1.1520 +        break;
  1.1521 +      case VALIGN_BOTTOM:
  1.1522 +        if (span) {
  1.1523 +          // Compute bottom leading
  1.1524 +          pfd->mBounds.BStart(lineWM) =
  1.1525 +            -aDistanceFromStart + aLineBSize - span->mMaxBCoord;
  1.1526 +        }
  1.1527 +        else {
  1.1528 +          pfd->mBounds.BStart(lineWM) = -aDistanceFromStart + aLineBSize -
  1.1529 +            pfd->mMargin.BEnd(frameWM) - pfd->mBounds.BSize(lineWM);
  1.1530 +        }
  1.1531 +        pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.1532 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1533 +        printf("    ");
  1.1534 +        nsFrame::ListTag(stdout, pfd->mFrame);
  1.1535 +        printf(": y=%d\n", pfd->mBounds.BStart(lineWM));
  1.1536 +#endif
  1.1537 +        break;
  1.1538 +    }
  1.1539 +    if (span) {
  1.1540 +      nscoord fromStart = aDistanceFromStart + pfd->mBounds.BStart(lineWM);
  1.1541 +      PlaceStartEndFrames(span, fromStart, aLineBSize);
  1.1542 +    }
  1.1543 +  }
  1.1544 +}
  1.1545 +
  1.1546 +static float
  1.1547 +GetInflationForBlockDirAlignment(nsIFrame* aFrame,
  1.1548 +                                 nscoord aInflationMinFontSize)
  1.1549 +{
  1.1550 +  if (aFrame->IsSVGText()) {
  1.1551 +    const nsIFrame* container =
  1.1552 +      nsLayoutUtils::GetClosestFrameOfType(aFrame, nsGkAtoms::svgTextFrame);
  1.1553 +    NS_ASSERTION(container, "expected to find an ancestor SVGTextFrame");
  1.1554 +    return
  1.1555 +      static_cast<const SVGTextFrame*>(container)->GetFontSizeScaleFactor();
  1.1556 +  }
  1.1557 +  return nsLayoutUtils::FontSizeInflationInner(aFrame, aInflationMinFontSize);
  1.1558 +}
  1.1559 +
  1.1560 +#define BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM nscoord_MAX
  1.1561 +#define BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM nscoord_MIN
  1.1562 +
  1.1563 +// Place frames in the block direction within a given span. Note: this doesn't
  1.1564 +// place block start/end aligned frames as those have to wait until the
  1.1565 +// entire line box block size is known. This is called after the span
  1.1566 +// frame has finished being reflowed so that we know its block size.
  1.1567 +void
  1.1568 +nsLineLayout::BlockDirAlignFrames(PerSpanData* psd)
  1.1569 +{
  1.1570 +  // Get parent frame info
  1.1571 +  PerFrameData* spanFramePFD = psd->mFrame;
  1.1572 +  nsIFrame* spanFrame = spanFramePFD->mFrame;
  1.1573 +
  1.1574 +  // Get the parent frame's font for all of the frames in this span
  1.1575 +  nsRefPtr<nsFontMetrics> fm;
  1.1576 +  float inflation =
  1.1577 +    GetInflationForBlockDirAlignment(spanFrame, mInflationMinFontSize);
  1.1578 +  nsLayoutUtils::GetFontMetricsForFrame(spanFrame, getter_AddRefs(fm),
  1.1579 +                                        inflation);
  1.1580 +  mBlockReflowState->rendContext->SetFont(fm);
  1.1581 +
  1.1582 +  bool preMode = mStyleText->WhiteSpaceIsSignificant();
  1.1583 +
  1.1584 +  // See if the span is an empty continuation. It's an empty continuation iff:
  1.1585 +  // - it has a prev-in-flow
  1.1586 +  // - it has no next in flow
  1.1587 +  // - it's zero sized
  1.1588 +  WritingMode frameWM = spanFramePFD->mFrame->GetWritingMode();
  1.1589 +  WritingMode lineWM = mRootSpan->mWritingMode;
  1.1590 +  bool emptyContinuation = psd != mRootSpan &&
  1.1591 +    spanFrame->GetPrevInFlow() && !spanFrame->GetNextInFlow() &&
  1.1592 +    spanFramePFD->mBounds.IsZeroSize();
  1.1593 +
  1.1594 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1595 +  printf("[%sSpan]", (psd == mRootSpan)?"Root":"");
  1.1596 +  nsFrame::ListTag(stdout, spanFrame);
  1.1597 +  printf(": preMode=%s strictMode=%s w/h=%d,%d emptyContinuation=%s",
  1.1598 +         preMode ? "yes" : "no",
  1.1599 +         mPresContext->CompatibilityMode() != eCompatibility_NavQuirks ? "yes" : "no",
  1.1600 +         spanFramePFD->mBounds.ISize(lineWM),
  1.1601 +         spanFramePFD->mBounds.BSize(lineWM),
  1.1602 +         emptyContinuation ? "yes" : "no");
  1.1603 +  if (psd != mRootSpan) {
  1.1604 +    WritingMode frameWM = spanFramePFD->mFrame->GetWritingMode();
  1.1605 +    printf(" bp=%d,%d,%d,%d margin=%d,%d,%d,%d",
  1.1606 +           spanFramePFD->mBorderPadding.Top(frameWM),
  1.1607 +           spanFramePFD->mBorderPadding.Right(frameWM),
  1.1608 +           spanFramePFD->mBorderPadding.Bottom(frameWM),
  1.1609 +           spanFramePFD->mBorderPadding.Left(frameWM),
  1.1610 +           spanFramePFD->mMargin.Top(frameWM),
  1.1611 +           spanFramePFD->mMargin.Right(frameWM),
  1.1612 +           spanFramePFD->mMargin.Bottom(frameWM),
  1.1613 +           spanFramePFD->mMargin.Left(frameWM));
  1.1614 +  }
  1.1615 +  printf("\n");
  1.1616 +#endif
  1.1617 +
  1.1618 +  // Compute the span's mZeroEffectiveSpanBox flag. What we are trying
  1.1619 +  // to determine is how we should treat the span: should it act
  1.1620 +  // "normally" according to css2 or should it effectively
  1.1621 +  // "disappear".
  1.1622 +  //
  1.1623 +  // In general, if the document being processed is in full standards
  1.1624 +  // mode then it should act normally (with one exception). The
  1.1625 +  // exception case is when a span is continued and yet the span is
  1.1626 +  // empty (e.g. compressed whitespace). For this kind of span we treat
  1.1627 +  // it as if it were not there so that it doesn't impact the
  1.1628 +  // line block-size.
  1.1629 +  //
  1.1630 +  // In almost standards mode or quirks mode, we should sometimes make
  1.1631 +  // it disappear. The cases that matter are those where the span
  1.1632 +  // contains no real text elements that would provide an ascent and
  1.1633 +  // descent and height. However, if css style elements have been
  1.1634 +  // applied to the span (border/padding/margin) so that it's clear the
  1.1635 +  // document author is intending css2 behavior then we act as if strict
  1.1636 +  // mode is set.
  1.1637 +  //
  1.1638 +  // This code works correctly for preMode, because a blank line
  1.1639 +  // in PRE mode is encoded as a text node with a LF in it, since
  1.1640 +  // text nodes with only whitespace are considered in preMode.
  1.1641 +  //
  1.1642 +  // Much of this logic is shared with the various implementations of
  1.1643 +  // nsIFrame::IsEmpty since they need to duplicate the way it makes
  1.1644 +  // some lines empty.  However, nsIFrame::IsEmpty can't be reused here
  1.1645 +  // since this code sets zeroEffectiveSpanBox even when there are
  1.1646 +  // non-empty children.
  1.1647 +  bool zeroEffectiveSpanBox = false;
  1.1648 +  // XXXldb If we really have empty continuations, then all these other
  1.1649 +  // checks don't make sense for them.
  1.1650 +  // XXXldb This should probably just use nsIFrame::IsSelfEmpty, assuming that
  1.1651 +  // it agrees with this code.  (If it doesn't agree, it probably should.)
  1.1652 +  if ((emptyContinuation ||
  1.1653 +       mPresContext->CompatibilityMode() != eCompatibility_FullStandards) &&
  1.1654 +      ((psd == mRootSpan) ||
  1.1655 +       (spanFramePFD->mBorderPadding.IsEmpty() &&
  1.1656 +        spanFramePFD->mMargin.IsEmpty()))) {
  1.1657 +    // This code handles an issue with compatibility with non-css
  1.1658 +    // conformant browsers. In particular, there are some cases
  1.1659 +    // where the font-size and line-height for a span must be
  1.1660 +    // ignored and instead the span must *act* as if it were zero
  1.1661 +    // sized. In general, if the span contains any non-compressed
  1.1662 +    // text then we don't use this logic.
  1.1663 +    // However, this is not propagated outwards, since (in compatibility
  1.1664 +    // mode) we don't want big line heights for things like
  1.1665 +    // <p><font size="-1">Text</font></p>
  1.1666 +
  1.1667 +    // We shouldn't include any whitespace that collapses, unless we're
  1.1668 +    // preformatted (in which case it shouldn't, but the width=0 test is
  1.1669 +    // perhaps incorrect).  This includes whitespace at the beginning of
  1.1670 +    // a line and whitespace preceded (?) by other whitespace.
  1.1671 +    // See bug 134580 and bug 155333.
  1.1672 +    zeroEffectiveSpanBox = true;
  1.1673 +    for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.1674 +      if (pfd->GetFlag(PFD_ISTEXTFRAME) &&
  1.1675 +          (pfd->GetFlag(PFD_ISNONWHITESPACETEXTFRAME) || preMode ||
  1.1676 +           pfd->mBounds.ISize(mRootSpan->mWritingMode) != 0)) {
  1.1677 +        zeroEffectiveSpanBox = false;
  1.1678 +        break;
  1.1679 +      }
  1.1680 +    }
  1.1681 +  }
  1.1682 +  psd->mZeroEffectiveSpanBox = zeroEffectiveSpanBox;
  1.1683 +
  1.1684 +  // Setup baselineBCoord, minBCoord, and maxBCoord
  1.1685 +  nscoord baselineBCoord, minBCoord, maxBCoord;
  1.1686 +  if (psd == mRootSpan) {
  1.1687 +    // Use a zero baselineBCoord since we don't yet know where the baseline
  1.1688 +    // will be (until we know how tall the line is; then we will
  1.1689 +    // know). In addition, use extreme values for the minBCoord and maxBCoord
  1.1690 +    // values so that only the child frames will impact their values
  1.1691 +    // (since these are children of the block, there is no span box to
  1.1692 +    // provide initial values).
  1.1693 +    baselineBCoord = 0;
  1.1694 +    minBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
  1.1695 +    maxBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
  1.1696 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1697 +    printf("[RootSpan]");
  1.1698 +    nsFrame::ListTag(stdout, spanFrame);
  1.1699 +    printf(": pass1 valign frames: topEdge=%d minLineBSize=%d zeroEffectiveSpanBox=%s\n",
  1.1700 +           mBStartEdge, mMinLineBSize,
  1.1701 +           zeroEffectiveSpanBox ? "yes" : "no");
  1.1702 +#endif
  1.1703 +  }
  1.1704 +  else {
  1.1705 +    // Compute the logical block size for this span. The logical block size
  1.1706 +    // is based on the "line-height" value, not the font-size. Also
  1.1707 +    // compute the top leading.
  1.1708 +    float inflation =
  1.1709 +      GetInflationForBlockDirAlignment(spanFrame, mInflationMinFontSize);
  1.1710 +    nscoord logicalBSize = nsHTMLReflowState::
  1.1711 +      CalcLineHeight(spanFrame->GetContent(), spanFrame->StyleContext(),
  1.1712 +                     mBlockReflowState->ComputedHeight(),
  1.1713 +                     inflation);
  1.1714 +    nscoord contentBSize = spanFramePFD->mBounds.BSize(lineWM) -
  1.1715 +      spanFramePFD->mBorderPadding.BStart(frameWM) -
  1.1716 +      spanFramePFD->mBorderPadding.BEnd(frameWM);
  1.1717 +
  1.1718 +    // Special-case for a ::first-letter frame, set the line height to
  1.1719 +    // the frame block size if the user has left line-height == normal
  1.1720 +    if (spanFramePFD->GetFlag(PFD_ISLETTERFRAME) &&
  1.1721 +        !spanFrame->GetPrevInFlow() &&
  1.1722 +        spanFrame->StyleText()->mLineHeight.GetUnit() == eStyleUnit_Normal) {
  1.1723 +      logicalBSize = spanFramePFD->mBounds.BSize(lineWM);
  1.1724 +    }
  1.1725 +
  1.1726 +    nscoord leading = logicalBSize - contentBSize;
  1.1727 +    psd->mBStartLeading = leading / 2;
  1.1728 +    psd->mBEndLeading = leading - psd->mBStartLeading;
  1.1729 +    psd->mLogicalBSize = logicalBSize;
  1.1730 +
  1.1731 +    if (zeroEffectiveSpanBox) {
  1.1732 +      // When the span-box is to be ignored, zero out the initial
  1.1733 +      // values so that the span doesn't impact the final line
  1.1734 +      // height. The contents of the span can impact the final line
  1.1735 +      // height.
  1.1736 +
  1.1737 +      // Note that things are readjusted for this span after its children
  1.1738 +      // are reflowed
  1.1739 +      minBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
  1.1740 +      maxBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
  1.1741 +    }
  1.1742 +    else {
  1.1743 +
  1.1744 +      // The initial values for the min and max block coord values are in the
  1.1745 +      // span's coordinate space, and cover the logical block size of the span.
  1.1746 +      // If there are child frames in this span that stick out of this area
  1.1747 +      // then the minBCoord and maxBCoord are updated by the amount of logical
  1.1748 +      // blockSize that is outside this range.
  1.1749 +      minBCoord = spanFramePFD->mBorderPadding.BStart(frameWM) -
  1.1750 +                  psd->mBStartLeading;
  1.1751 +      maxBCoord = minBCoord + psd->mLogicalBSize;
  1.1752 +    }
  1.1753 +
  1.1754 +    // This is the distance from the top edge of the parents visual
  1.1755 +    // box to the baseline. The span already computed this for us,
  1.1756 +    // so just use it.
  1.1757 +    *psd->mBaseline = baselineBCoord = spanFramePFD->mAscent;
  1.1758 +
  1.1759 +
  1.1760 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1761 +    printf("[%sSpan]", (psd == mRootSpan)?"Root":"");
  1.1762 +    nsFrame::ListTag(stdout, spanFrame);
  1.1763 +    printf(": baseLine=%d logicalBSize=%d topLeading=%d h=%d bp=%d,%d zeroEffectiveSpanBox=%s\n",
  1.1764 +           baselineBCoord, psd->mLogicalBSize, psd->mBStartLeading,
  1.1765 +           spanFramePFD->mBounds.BSize(lineWM),
  1.1766 +           spanFramePFD->mBorderPadding.Top(frameWM),
  1.1767 +           spanFramePFD->mBorderPadding.Bottom(frameWM),
  1.1768 +           zeroEffectiveSpanBox ? "yes" : "no");
  1.1769 +#endif
  1.1770 +  }
  1.1771 +
  1.1772 +  nscoord maxStartBoxBSize = 0;
  1.1773 +  nscoord maxEndBoxBSize = 0;
  1.1774 +  PerFrameData* pfd = psd->mFirstFrame;
  1.1775 +  while (nullptr != pfd) {
  1.1776 +    nsIFrame* frame = pfd->mFrame;
  1.1777 +    WritingMode frameWM = frame->GetWritingMode();
  1.1778 +
  1.1779 +    // sanity check (see bug 105168, non-reproducible crashes from null frame)
  1.1780 +    NS_ASSERTION(frame, "null frame in PerFrameData - something is very very bad");
  1.1781 +    if (!frame) {
  1.1782 +      return;
  1.1783 +    }
  1.1784 +
  1.1785 +    // Compute the logical block size of the frame
  1.1786 +    nscoord logicalBSize;
  1.1787 +    PerSpanData* frameSpan = pfd->mSpan;
  1.1788 +    if (frameSpan) {
  1.1789 +      // For span frames the logical-block-size and start-leading were
  1.1790 +      // pre-computed when the span was reflowed.
  1.1791 +      logicalBSize = frameSpan->mLogicalBSize;
  1.1792 +    }
  1.1793 +    else {
  1.1794 +      // For other elements the logical block size is the same as the
  1.1795 +      // frame's block size plus its margins.
  1.1796 +      logicalBSize = pfd->mBounds.BSize(lineWM) +
  1.1797 +                     pfd->mMargin.BStartEnd(frameWM);
  1.1798 +      if (logicalBSize < 0 &&
  1.1799 +          mPresContext->CompatibilityMode() == eCompatibility_NavQuirks) {
  1.1800 +        pfd->mAscent -= logicalBSize;
  1.1801 +        logicalBSize = 0;
  1.1802 +      }
  1.1803 +    }
  1.1804 +
  1.1805 +    // Get vertical-align property ("vertical-align" is the CSS name for
  1.1806 +    // block-direction align)
  1.1807 +    const nsStyleCoord& verticalAlign =
  1.1808 +      frame->StyleTextReset()->mVerticalAlign;
  1.1809 +    uint8_t verticalAlignEnum = frame->VerticalAlignEnum();
  1.1810 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.1811 +    printf("  [frame]");
  1.1812 +    nsFrame::ListTag(stdout, frame);
  1.1813 +    printf(": verticalAlignUnit=%d (enum == %d",
  1.1814 +           verticalAlign.GetUnit(),
  1.1815 +           ((eStyleUnit_Enumerated == verticalAlign.GetUnit())
  1.1816 +            ? verticalAlign.GetIntValue()
  1.1817 +            : -1));
  1.1818 +    if (verticalAlignEnum != nsIFrame::eInvalidVerticalAlign) {
  1.1819 +      printf(", after SVG dominant-baseline conversion == %d",
  1.1820 +             verticalAlignEnum);
  1.1821 +    }
  1.1822 +    printf(")\n");
  1.1823 +#endif
  1.1824 +
  1.1825 +    if (verticalAlignEnum != nsIFrame::eInvalidVerticalAlign) {
  1.1826 +      switch (verticalAlignEnum) {
  1.1827 +        default:
  1.1828 +        case NS_STYLE_VERTICAL_ALIGN_BASELINE:
  1.1829 +        {
  1.1830 +          // The element's baseline is aligned with the baseline of
  1.1831 +          // the parent.
  1.1832 +          pfd->mBounds.BStart(lineWM) = baselineBCoord - pfd->mAscent;
  1.1833 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1834 +          break;
  1.1835 +        }
  1.1836 +
  1.1837 +        case NS_STYLE_VERTICAL_ALIGN_SUB:
  1.1838 +        {
  1.1839 +          // Lower the baseline of the box to the subscript offset
  1.1840 +          // of the parent's box. This is identical to the baseline
  1.1841 +          // alignment except for the addition of the subscript
  1.1842 +          // offset to the baseline BCoord.
  1.1843 +          nscoord parentSubscript = fm->SubscriptOffset();
  1.1844 +          nscoord revisedBaselineBCoord = baselineBCoord + parentSubscript;
  1.1845 +          pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord - pfd->mAscent;
  1.1846 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1847 +          break;
  1.1848 +        }
  1.1849 +
  1.1850 +        case NS_STYLE_VERTICAL_ALIGN_SUPER:
  1.1851 +        {
  1.1852 +          // Raise the baseline of the box to the superscript offset
  1.1853 +          // of the parent's box. This is identical to the baseline
  1.1854 +          // alignment except for the subtraction of the superscript
  1.1855 +          // offset to the baseline BCoord.
  1.1856 +          nscoord parentSuperscript = fm->SuperscriptOffset();
  1.1857 +          nscoord revisedBaselineBCoord = baselineBCoord - parentSuperscript;
  1.1858 +          pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord - pfd->mAscent;
  1.1859 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1860 +          break;
  1.1861 +        }
  1.1862 +
  1.1863 +        case NS_STYLE_VERTICAL_ALIGN_TOP:
  1.1864 +        {
  1.1865 +          pfd->mBlockDirAlign = VALIGN_TOP;
  1.1866 +          nscoord subtreeBSize = logicalBSize;
  1.1867 +          if (frameSpan) {
  1.1868 +            subtreeBSize = frameSpan->mMaxBCoord - frameSpan->mMinBCoord;
  1.1869 +            NS_ASSERTION(subtreeBSize >= logicalBSize,
  1.1870 +                         "unexpected subtree block size");
  1.1871 +          }
  1.1872 +          if (subtreeBSize > maxStartBoxBSize) {
  1.1873 +            maxStartBoxBSize = subtreeBSize;
  1.1874 +          }
  1.1875 +          break;
  1.1876 +        }
  1.1877 +
  1.1878 +        case NS_STYLE_VERTICAL_ALIGN_BOTTOM:
  1.1879 +        {
  1.1880 +          pfd->mBlockDirAlign = VALIGN_BOTTOM;
  1.1881 +          nscoord subtreeBSize = logicalBSize;
  1.1882 +          if (frameSpan) {
  1.1883 +            subtreeBSize = frameSpan->mMaxBCoord - frameSpan->mMinBCoord;
  1.1884 +            NS_ASSERTION(subtreeBSize >= logicalBSize,
  1.1885 +                         "unexpected subtree block size");
  1.1886 +          }
  1.1887 +          if (subtreeBSize > maxEndBoxBSize) {
  1.1888 +            maxEndBoxBSize = subtreeBSize;
  1.1889 +          }
  1.1890 +          break;
  1.1891 +        }
  1.1892 +
  1.1893 +        case NS_STYLE_VERTICAL_ALIGN_MIDDLE:
  1.1894 +        {
  1.1895 +          // Align the midpoint of the frame with 1/2 the parents
  1.1896 +          // x-height above the baseline.
  1.1897 +          nscoord parentXHeight = fm->XHeight();
  1.1898 +          if (frameSpan) {
  1.1899 +            pfd->mBounds.BStart(lineWM) = baselineBCoord -
  1.1900 +              (parentXHeight + pfd->mBounds.BSize(lineWM))/2;
  1.1901 +          }
  1.1902 +          else {
  1.1903 +            pfd->mBounds.BStart(lineWM) = baselineBCoord -
  1.1904 +              (parentXHeight + logicalBSize)/2 +
  1.1905 +              pfd->mMargin.BStart(frameWM);
  1.1906 +          }
  1.1907 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1908 +          break;
  1.1909 +        }
  1.1910 +
  1.1911 +        case NS_STYLE_VERTICAL_ALIGN_TEXT_TOP:
  1.1912 +        {
  1.1913 +          // The top of the logical box is aligned with the top of
  1.1914 +          // the parent element's text.
  1.1915 +          nscoord parentAscent = fm->MaxAscent();
  1.1916 +          if (frameSpan) {
  1.1917 +            pfd->mBounds.BStart(lineWM) = baselineBCoord - parentAscent -
  1.1918 +              pfd->mBorderPadding.BStart(frameWM) + frameSpan->mBStartLeading;
  1.1919 +          }
  1.1920 +          else {
  1.1921 +            pfd->mBounds.BStart(lineWM) = baselineBCoord - parentAscent +
  1.1922 +                                          pfd->mMargin.BStart(frameWM);
  1.1923 +          }
  1.1924 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1925 +          break;
  1.1926 +        }
  1.1927 +
  1.1928 +        case NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM:
  1.1929 +        {
  1.1930 +          // The bottom of the logical box is aligned with the
  1.1931 +          // bottom of the parent elements text.
  1.1932 +          nscoord parentDescent = fm->MaxDescent();
  1.1933 +          if (frameSpan) {
  1.1934 +            pfd->mBounds.BStart(lineWM) = baselineBCoord + parentDescent -
  1.1935 +                                          pfd->mBounds.BSize(lineWM) +
  1.1936 +                                          pfd->mBorderPadding.BEnd(frameWM) -
  1.1937 +                                          frameSpan->mBEndLeading;
  1.1938 +          }
  1.1939 +          else {
  1.1940 +            pfd->mBounds.BStart(lineWM) = baselineBCoord + parentDescent -
  1.1941 +                                          pfd->mBounds.BSize(lineWM) -
  1.1942 +                                          pfd->mMargin.BEnd(frameWM);
  1.1943 +          }
  1.1944 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1945 +          break;
  1.1946 +        }
  1.1947 +
  1.1948 +        case NS_STYLE_VERTICAL_ALIGN_MIDDLE_WITH_BASELINE:
  1.1949 +        {
  1.1950 +          // Align the midpoint of the frame with the baseline of the parent.
  1.1951 +          if (frameSpan) {
  1.1952 +            pfd->mBounds.BStart(lineWM) = baselineBCoord -
  1.1953 +                                          pfd->mBounds.BSize(lineWM)/2;
  1.1954 +          }
  1.1955 +          else {
  1.1956 +            pfd->mBounds.BStart(lineWM) = baselineBCoord - logicalBSize/2 +
  1.1957 +                                          pfd->mMargin.BStart(frameWM);
  1.1958 +          }
  1.1959 +          pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1960 +          break;
  1.1961 +        }
  1.1962 +      }
  1.1963 +    } else {
  1.1964 +      // We have either a coord, a percent, or a calc().
  1.1965 +      nscoord pctBasis = 0;
  1.1966 +      if (verticalAlign.HasPercent()) {
  1.1967 +        // Percentages are like lengths, except treated as a percentage
  1.1968 +        // of the elements line block size value.
  1.1969 +        float inflation =
  1.1970 +          GetInflationForBlockDirAlignment(frame, mInflationMinFontSize);
  1.1971 +        pctBasis = nsHTMLReflowState::CalcLineHeight(frame->GetContent(),
  1.1972 +          frame->StyleContext(), mBlockReflowState->ComputedBSize(),
  1.1973 +          inflation);
  1.1974 +      }
  1.1975 +      nscoord offset =
  1.1976 +        nsRuleNode::ComputeCoordPercentCalc(verticalAlign, pctBasis);
  1.1977 +      // According to the CSS2 spec (10.8.1), a positive value
  1.1978 +      // "raises" the box by the given distance while a negative value
  1.1979 +      // "lowers" the box by the given distance (with zero being the
  1.1980 +      // baseline). Since Y coordinates increase towards the bottom of
  1.1981 +      // the screen we reverse the sign.
  1.1982 +      nscoord revisedBaselineBCoord = baselineBCoord - offset;
  1.1983 +      pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord - pfd->mAscent;
  1.1984 +      pfd->mBlockDirAlign = VALIGN_OTHER;
  1.1985 +    }
  1.1986 +
  1.1987 +    // Update minBCoord/maxBCoord for frames that we just placed. Do not factor
  1.1988 +    // text into the equation.
  1.1989 +    if (pfd->mBlockDirAlign == VALIGN_OTHER) {
  1.1990 +      // Text frames do not contribute to the min/max Y values for the
  1.1991 +      // line (instead their parent frame's font-size contributes).
  1.1992 +      // XXXrbs -- relax this restriction because it causes text frames
  1.1993 +      //           to jam together when 'font-size-adjust' is enabled
  1.1994 +      //           and layout is using dynamic font heights (bug 20394)
  1.1995 +      //        -- Note #1: With this code enabled and with the fact that we are not
  1.1996 +      //           using Em[Ascent|Descent] as nsDimensions for text metrics in
  1.1997 +      //           GFX mean that the discussion in bug 13072 cannot hold.
  1.1998 +      //        -- Note #2: We still don't want empty-text frames to interfere.
  1.1999 +      //           For example in quirks mode, avoiding empty text frames prevents
  1.2000 +      //           "tall" lines around elements like <hr> since the rules of <hr>
  1.2001 +      //           in quirks.css have pseudo text contents with LF in them.
  1.2002 +#if 0
  1.2003 +      if (!pfd->GetFlag(PFD_ISTEXTFRAME)) {
  1.2004 +#else
  1.2005 +      // Only consider non empty text frames when line-height=normal
  1.2006 +      bool canUpdate = !pfd->GetFlag(PFD_ISTEXTFRAME);
  1.2007 +      if (!canUpdate && pfd->GetFlag(PFD_ISNONWHITESPACETEXTFRAME)) {
  1.2008 +        canUpdate =
  1.2009 +          frame->StyleText()->mLineHeight.GetUnit() == eStyleUnit_Normal;
  1.2010 +      }
  1.2011 +      if (canUpdate) {
  1.2012 +#endif
  1.2013 +        nscoord blockStart, blockEnd;
  1.2014 +        if (frameSpan) {
  1.2015 +          // For spans that were are now placing, use their position
  1.2016 +          // plus their already computed min-Y and max-Y values for
  1.2017 +          // computing blockStart and blockEnd.
  1.2018 +          blockStart = pfd->mBounds.BStart(lineWM) + frameSpan->mMinBCoord;
  1.2019 +          blockEnd = pfd->mBounds.BStart(lineWM) + frameSpan->mMaxBCoord;
  1.2020 +        }
  1.2021 +        else {
  1.2022 +          blockStart = pfd->mBounds.BStart(lineWM) -
  1.2023 +                       pfd->mMargin.BStart(frameWM);
  1.2024 +          blockEnd = blockStart + logicalBSize;
  1.2025 +        }
  1.2026 +        if (!preMode &&
  1.2027 +            mPresContext->CompatibilityMode() != eCompatibility_FullStandards &&
  1.2028 +            !logicalBSize) {
  1.2029 +          // Check if it's a BR frame that is not alone on its line (it
  1.2030 +          // is given a block size of zero to indicate this), and if so reset
  1.2031 +          // blockStart and blockEnd so that BR frames don't influence the line.
  1.2032 +          if (nsGkAtoms::brFrame == frame->GetType()) {
  1.2033 +            blockStart = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
  1.2034 +            blockEnd = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
  1.2035 +          }
  1.2036 +        }
  1.2037 +        if (blockStart < minBCoord) minBCoord = blockStart;
  1.2038 +        if (blockEnd > maxBCoord) maxBCoord = blockEnd;
  1.2039 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2040 +        printf("     [frame]raw: a=%d h=%d bp=%d,%d logical: h=%d leading=%d y=%d minBCoord=%d maxBCoord=%d\n",
  1.2041 +               pfd->mAscent, pfd->mBounds.BSize(lineWM),
  1.2042 +               pfd->mBorderPadding.Top(frameWM),
  1.2043 +               pfd->mBorderPadding.Bottom(frameWM),
  1.2044 +               logicalBSize,
  1.2045 +               frameSpan ? frameSpan->mBStartLeading : 0,
  1.2046 +               pfd->mBounds.BStart(lineWM), minBCoord, maxBCoord);
  1.2047 +#endif
  1.2048 +      }
  1.2049 +      if (psd != mRootSpan) {
  1.2050 +        frame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.2051 +      }
  1.2052 +    }
  1.2053 +    pfd = pfd->mNext;
  1.2054 +  }
  1.2055 +
  1.2056 +  // Factor in the minimum line block-size when handling the root-span for
  1.2057 +  // the block.
  1.2058 +  if (psd == mRootSpan) {
  1.2059 +    // We should factor in the block element's minimum line-height (as
  1.2060 +    // defined in section 10.8.1 of the css2 spec) assuming that
  1.2061 +    // mZeroEffectiveSpanBox is not set on the root span.  This only happens
  1.2062 +    // in some cases in quirks mode:
  1.2063 +    //  (1) if the root span contains non-whitespace text directly (this
  1.2064 +    //      is handled by mZeroEffectiveSpanBox
  1.2065 +    //  (2) if this line has a bullet
  1.2066 +    //  (3) if this is the last line of an LI, DT, or DD element
  1.2067 +    //      (The last line before a block also counts, but not before a
  1.2068 +    //      BR) (NN4/IE5 quirk)
  1.2069 +
  1.2070 +    // (1) and (2) above
  1.2071 +    bool applyMinLH = !psd->mZeroEffectiveSpanBox || mHasBullet;
  1.2072 +    bool isLastLine = (!mLineBox->IsLineWrapped() && !mLineEndsInBR);
  1.2073 +    if (!applyMinLH && isLastLine) {
  1.2074 +      nsIContent* blockContent = mRootSpan->mFrame->mFrame->GetContent();
  1.2075 +      if (blockContent) {
  1.2076 +        nsIAtom *blockTagAtom = blockContent->Tag();
  1.2077 +        // (3) above, if the last line of LI, DT, or DD
  1.2078 +        if (blockTagAtom == nsGkAtoms::li ||
  1.2079 +            blockTagAtom == nsGkAtoms::dt ||
  1.2080 +            blockTagAtom == nsGkAtoms::dd) {
  1.2081 +          applyMinLH = true;
  1.2082 +        }
  1.2083 +      }
  1.2084 +    }
  1.2085 +    if (applyMinLH) {
  1.2086 +      if (psd->mHasNonemptyContent || preMode || mHasBullet) {
  1.2087 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2088 +        printf("  [span]==> adjusting min/maxBCoord: currentValues: %d,%d", minBCoord, maxBCoord);
  1.2089 +#endif
  1.2090 +        nscoord minimumLineBSize = mMinLineBSize;
  1.2091 +        nscoord blockStart =
  1.2092 +          -nsLayoutUtils::GetCenteredFontBaseline(fm, minimumLineBSize);
  1.2093 +        nscoord blockEnd = blockStart + minimumLineBSize;
  1.2094 +
  1.2095 +        if (blockStart < minBCoord) minBCoord = blockStart;
  1.2096 +        if (blockEnd > maxBCoord) maxBCoord = blockEnd;
  1.2097 +
  1.2098 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2099 +        printf(" new values: %d,%d\n", minBCoord, maxBCoord);
  1.2100 +#endif
  1.2101 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2102 +        printf("            Used mMinLineBSize: %d, blockStart: %d, blockEnd: %d\n", mMinLineBSize, blockStart, blockEnd);
  1.2103 +#endif
  1.2104 +      }
  1.2105 +      else {
  1.2106 +        // XXX issues:
  1.2107 +        // [1] BR's on empty lines stop working
  1.2108 +        // [2] May not honor css2's notion of handling empty elements
  1.2109 +        // [3] blank lines in a pre-section ("\n") (handled with preMode)
  1.2110 +
  1.2111 +        // XXX Are there other problems with this?
  1.2112 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2113 +        printf("  [span]==> zapping min/maxBCoord: currentValues: %d,%d newValues: 0,0\n",
  1.2114 +               minBCoord, maxBCoord);
  1.2115 +#endif
  1.2116 +        minBCoord = maxBCoord = 0;
  1.2117 +      }
  1.2118 +    }
  1.2119 +  }
  1.2120 +
  1.2121 +  if ((minBCoord == BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM) ||
  1.2122 +      (maxBCoord == BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM)) {
  1.2123 +    minBCoord = maxBCoord = baselineBCoord;
  1.2124 +  }
  1.2125 +
  1.2126 +  if ((psd != mRootSpan) && (psd->mZeroEffectiveSpanBox)) {
  1.2127 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2128 +    printf("   [span]adjusting for zeroEffectiveSpanBox\n");
  1.2129 +    printf("     Original: minBCoord=%d, maxBCoord=%d, bSize=%d, ascent=%d, logicalBSize=%d, topLeading=%d, bottomLeading=%d\n",
  1.2130 +           minBCoord, maxBCoord, spanFramePFD->mBounds.BSize(frameWM),
  1.2131 +           spanFramePFD->mAscent,
  1.2132 +           psd->mLogicalBSize, psd->mBStartLeading, psd->mBEndLeading);
  1.2133 +#endif
  1.2134 +    nscoord goodMinBCoord = spanFramePFD->mBorderPadding.BStart(frameWM) -
  1.2135 +                            psd->mBStartLeading;
  1.2136 +    nscoord goodMaxBCoord = goodMinBCoord + psd->mLogicalBSize;
  1.2137 +
  1.2138 +    // For cases like the one in bug 714519 (text-decoration placement
  1.2139 +    // or making nsLineLayout::IsZeroBSize() handle
  1.2140 +    // vertical-align:top/bottom on a descendant of the line that's not
  1.2141 +    // a child of it), we want to treat elements that are
  1.2142 +    // vertical-align: top or bottom somewhat like children for the
  1.2143 +    // purposes of this quirk.  To some extent, this is guessing, since
  1.2144 +    // they might end up being aligned anywhere.  However, we'll guess
  1.2145 +    // that they'll be placed aligned with the top or bottom of this
  1.2146 +    // frame (as though this frame is the only thing in the line).
  1.2147 +    // (Guessing isn't crazy, since all we're doing is reducing the
  1.2148 +    // scope of a quirk and making the behavior more standards-like.)
  1.2149 +    if (maxStartBoxBSize > maxBCoord - minBCoord) {
  1.2150 +      // Distribute maxStartBoxBSize to ascent (baselineBCoord - minBCoord), and
  1.2151 +      // then to descent (maxBCoord - baselineBCoord) by adjusting minBCoord or
  1.2152 +      // maxBCoord, but not to exceed goodMinBCoord and goodMaxBCoord.
  1.2153 +      nscoord distribute = maxStartBoxBSize - (maxBCoord - minBCoord);
  1.2154 +      nscoord ascentSpace = std::max(minBCoord - goodMinBCoord, 0);
  1.2155 +      if (distribute > ascentSpace) {
  1.2156 +        distribute -= ascentSpace;
  1.2157 +        minBCoord -= ascentSpace;
  1.2158 +        nscoord descentSpace = std::max(goodMaxBCoord - maxBCoord, 0);
  1.2159 +        if (distribute > descentSpace) {
  1.2160 +          maxBCoord += descentSpace;
  1.2161 +        } else {
  1.2162 +          maxBCoord += distribute;
  1.2163 +        }
  1.2164 +      } else {
  1.2165 +        minBCoord -= distribute;
  1.2166 +      }
  1.2167 +    }
  1.2168 +    if (maxEndBoxBSize > maxBCoord - minBCoord) {
  1.2169 +      // Likewise, but preferring descent to ascent.
  1.2170 +      nscoord distribute = maxEndBoxBSize - (maxBCoord - minBCoord);
  1.2171 +      nscoord descentSpace = std::max(goodMaxBCoord - maxBCoord, 0);
  1.2172 +      if (distribute > descentSpace) {
  1.2173 +        distribute -= descentSpace;
  1.2174 +        maxBCoord += descentSpace;
  1.2175 +        nscoord ascentSpace = std::max(minBCoord - goodMinBCoord, 0);
  1.2176 +        if (distribute > ascentSpace) {
  1.2177 +          minBCoord -= ascentSpace;
  1.2178 +        } else {
  1.2179 +          minBCoord -= distribute;
  1.2180 +        }
  1.2181 +      } else {
  1.2182 +        maxBCoord += distribute;
  1.2183 +      }
  1.2184 +    }
  1.2185 +
  1.2186 +    if (minBCoord > goodMinBCoord) {
  1.2187 +      nscoord adjust = minBCoord - goodMinBCoord; // positive
  1.2188 +
  1.2189 +      // shrink the logical extents
  1.2190 +      psd->mLogicalBSize -= adjust;
  1.2191 +      psd->mBStartLeading -= adjust;
  1.2192 +    }
  1.2193 +    if (maxBCoord < goodMaxBCoord) {
  1.2194 +      nscoord adjust = goodMaxBCoord - maxBCoord;
  1.2195 +      psd->mLogicalBSize -= adjust;
  1.2196 +      psd->mBEndLeading -= adjust;
  1.2197 +    }
  1.2198 +    if (minBCoord > 0) {
  1.2199 +
  1.2200 +      // shrink the content by moving its block start down.  This is tricky,
  1.2201 +      // since the block start is the 0 for many coordinates, so what we do is
  1.2202 +      // move everything else up.
  1.2203 +      spanFramePFD->mAscent -= minBCoord; // move the baseline up
  1.2204 +      spanFramePFD->mBounds.BSize(lineWM) -= minBCoord; // move the block end up
  1.2205 +      psd->mBStartLeading += minBCoord;
  1.2206 +      *psd->mBaseline -= minBCoord;
  1.2207 +
  1.2208 +      pfd = psd->mFirstFrame;
  1.2209 +      while (nullptr != pfd) {
  1.2210 +        pfd->mBounds.BStart(lineWM) -= minBCoord; // move all the children
  1.2211 +                                                  // back up
  1.2212 +        pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.2213 +        pfd = pfd->mNext;
  1.2214 +      }
  1.2215 +      maxBCoord -= minBCoord; // since minBCoord is in the frame's own
  1.2216 +                              // coordinate system
  1.2217 +      minBCoord = 0;
  1.2218 +    }
  1.2219 +    if (maxBCoord < spanFramePFD->mBounds.BSize(lineWM)) {
  1.2220 +      nscoord adjust = spanFramePFD->mBounds.BSize(lineWM) - maxBCoord;
  1.2221 +      spanFramePFD->mBounds.BSize(lineWM) -= adjust; // move the bottom up
  1.2222 +      psd->mBEndLeading += adjust;
  1.2223 +    }
  1.2224 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2225 +    printf("     New: minBCoord=%d, maxBCoord=%d, bSize=%d, ascent=%d, logicalBSize=%d, topLeading=%d, bottomLeading=%d\n",
  1.2226 +           minBCoord, maxBCoord, spanFramePFD->mBounds.BSize(lineWM),
  1.2227 +           spanFramePFD->mAscent,
  1.2228 +           psd->mLogicalBSize, psd->mBStartLeading, psd->mBEndLeading);
  1.2229 +#endif
  1.2230 +  }
  1.2231 +
  1.2232 +  psd->mMinBCoord = minBCoord;
  1.2233 +  psd->mMaxBCoord = maxBCoord;
  1.2234 +#ifdef NOISY_BLOCKDIR_ALIGN
  1.2235 +  printf("  [span]==> minBCoord=%d maxBCoord=%d delta=%d maxStartBoxBSize=%d maxEndBoxBSize=%d\n",
  1.2236 +         minBCoord, maxBCoord, maxBCoord - minBCoord, maxStartBoxBSize, maxEndBoxBSize);
  1.2237 +#endif
  1.2238 +  if (maxStartBoxBSize > mMaxStartBoxBSize) {
  1.2239 +    mMaxStartBoxBSize = maxStartBoxBSize;
  1.2240 +  }
  1.2241 +  if (maxEndBoxBSize > mMaxEndBoxBSize) {
  1.2242 +    mMaxEndBoxBSize = maxEndBoxBSize;
  1.2243 +  }
  1.2244 +}
  1.2245 +
  1.2246 +static void SlideSpanFrameRect(nsIFrame* aFrame, nscoord aDeltaWidth)
  1.2247 +{
  1.2248 +  // This should not use nsIFrame::MovePositionBy because it happens
  1.2249 +  // prior to relative positioning.  In particular, because
  1.2250 +  // nsBlockFrame::PlaceLine calls aLineLayout.TrimTrailingWhiteSpace()
  1.2251 +  // prior to calling aLineLayout.RelativePositionFrames().
  1.2252 +  nsPoint p = aFrame->GetPosition();
  1.2253 +  p.x -= aDeltaWidth;
  1.2254 +  aFrame->SetPosition(p);
  1.2255 +}
  1.2256 +
  1.2257 +bool
  1.2258 +nsLineLayout::TrimTrailingWhiteSpaceIn(PerSpanData* psd,
  1.2259 +                                       nscoord* aDeltaISize)
  1.2260 +{
  1.2261 +  PerFrameData* pfd = psd->mFirstFrame;
  1.2262 +  if (!pfd) {
  1.2263 +    *aDeltaISize = 0;
  1.2264 +    return false;
  1.2265 +  }
  1.2266 +  pfd = pfd->Last();
  1.2267 +  while (nullptr != pfd) {
  1.2268 +#ifdef REALLY_NOISY_TRIM
  1.2269 +    nsFrame::ListTag(stdout, (psd == mRootSpan
  1.2270 +                              ? mBlockReflowState->frame
  1.2271 +                              : psd->mFrame->mFrame));
  1.2272 +    printf(": attempting trim of ");
  1.2273 +    nsFrame::ListTag(stdout, pfd->mFrame);
  1.2274 +    printf("\n");
  1.2275 +#endif
  1.2276 +    PerSpanData* childSpan = pfd->mSpan;
  1.2277 +    WritingMode lineWM = mRootSpan->mWritingMode;
  1.2278 +    if (childSpan) {
  1.2279 +      // Maybe the child span has the trailing white-space in it?
  1.2280 +      if (TrimTrailingWhiteSpaceIn(childSpan, aDeltaISize)) {
  1.2281 +        nscoord deltaISize = *aDeltaISize;
  1.2282 +        if (deltaISize) {
  1.2283 +          // Adjust the child spans frame size
  1.2284 +          pfd->mBounds.ISize(lineWM) -= deltaISize;
  1.2285 +          if (psd != mRootSpan) {
  1.2286 +            // When the child span is not a direct child of the block
  1.2287 +            // we need to update the child spans frame rectangle
  1.2288 +            // because it most likely will not be done again. Spans
  1.2289 +            // that are direct children of the block will be updated
  1.2290 +            // later, however, because the BlockDirAlignFrames method
  1.2291 +            // will be run after this method.
  1.2292 +            nsIFrame* f = pfd->mFrame;
  1.2293 +            LogicalRect r(lineWM, f->GetRect(), mContainerWidth);
  1.2294 +            r.ISize(lineWM) -= deltaISize;
  1.2295 +            f->SetRect(lineWM, r, mContainerWidth);
  1.2296 +          }
  1.2297 +
  1.2298 +          // Adjust the inline end edge of the span that contains the child span
  1.2299 +          psd->mICoord -= deltaISize;
  1.2300 +
  1.2301 +          // Slide any frames that follow the child span over by the
  1.2302 +          // correct amount. The only thing that can follow the child
  1.2303 +          // span is empty stuff, so we are just making things
  1.2304 +          // sensible (keeping the combined area honest).
  1.2305 +          while (pfd->mNext) {
  1.2306 +            pfd = pfd->mNext;
  1.2307 +            pfd->mBounds.IStart(lineWM) -= deltaISize;
  1.2308 +            if (psd != mRootSpan) {
  1.2309 +              // When the child span is not a direct child of the block
  1.2310 +              // we need to update the child span's frame rectangle
  1.2311 +              // because it most likely will not be done again. Spans
  1.2312 +              // that are direct children of the block will be updated
  1.2313 +              // later, however, because the BlockDirAlignFrames method
  1.2314 +              // will be run after this method.
  1.2315 +              SlideSpanFrameRect(pfd->mFrame, deltaISize);
  1.2316 +            }
  1.2317 +          }
  1.2318 +        }
  1.2319 +        return true;
  1.2320 +      }
  1.2321 +    }
  1.2322 +    else if (!pfd->GetFlag(PFD_ISTEXTFRAME) &&
  1.2323 +             !pfd->GetFlag(PFD_SKIPWHENTRIMMINGWHITESPACE)) {
  1.2324 +      // If we hit a frame on the end that's not text and not a placeholder,
  1.2325 +      // then there is no trailing whitespace to trim. Stop the search.
  1.2326 +      *aDeltaISize = 0;
  1.2327 +      return true;
  1.2328 +    }
  1.2329 +    else if (pfd->GetFlag(PFD_ISTEXTFRAME)) {
  1.2330 +      // Call TrimTrailingWhiteSpace even on empty textframes because they
  1.2331 +      // might have a soft hyphen which should now appear, changing the frame's
  1.2332 +      // width
  1.2333 +      nsTextFrame::TrimOutput trimOutput = static_cast<nsTextFrame*>(pfd->mFrame)->
  1.2334 +          TrimTrailingWhiteSpace(mBlockReflowState->rendContext);
  1.2335 +#ifdef NOISY_TRIM
  1.2336 +      nsFrame::ListTag(stdout, (psd == mRootSpan
  1.2337 +                                ? mBlockReflowState->frame
  1.2338 +                                : psd->mFrame->mFrame));
  1.2339 +      printf(": trim of ");
  1.2340 +      nsFrame::ListTag(stdout, pfd->mFrame);
  1.2341 +      printf(" returned %d\n", trimOutput.mDeltaWidth);
  1.2342 +#endif
  1.2343 +      if (trimOutput.mLastCharIsJustifiable && pfd->mJustificationNumSpaces > 0) {
  1.2344 +        pfd->mJustificationNumSpaces--;
  1.2345 +      }
  1.2346 +      
  1.2347 +      if (trimOutput.mChanged) {
  1.2348 +        pfd->SetFlag(PFD_RECOMPUTEOVERFLOW, true);
  1.2349 +      }
  1.2350 +
  1.2351 +      if (trimOutput.mDeltaWidth) {
  1.2352 +        pfd->mBounds.ISize(lineWM) -= trimOutput.mDeltaWidth;
  1.2353 +
  1.2354 +        // See if the text frame has already been placed in its parent
  1.2355 +        if (psd != mRootSpan) {
  1.2356 +          // The frame was already placed during psd's
  1.2357 +          // reflow. Update the frames rectangle now.
  1.2358 +          pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.2359 +        }
  1.2360 +
  1.2361 +        // Adjust containing span's right edge
  1.2362 +        psd->mICoord -= trimOutput.mDeltaWidth;
  1.2363 +
  1.2364 +        // Slide any frames that follow the text frame over by the
  1.2365 +        // right amount. The only thing that can follow the text
  1.2366 +        // frame is empty stuff, so we are just making things
  1.2367 +        // sensible (keeping the combined area honest).
  1.2368 +        while (pfd->mNext) {
  1.2369 +          pfd = pfd->mNext;
  1.2370 +          pfd->mBounds.IStart(lineWM) -= trimOutput.mDeltaWidth;
  1.2371 +          if (psd != mRootSpan) {
  1.2372 +            // When the child span is not a direct child of the block
  1.2373 +            // we need to update the child spans frame rectangle
  1.2374 +            // because it most likely will not be done again. Spans
  1.2375 +            // that are direct children of the block will be updated
  1.2376 +            // later, however, because the BlockDirAlignFrames method
  1.2377 +            // will be run after this method.
  1.2378 +            SlideSpanFrameRect(pfd->mFrame, trimOutput.mDeltaWidth);
  1.2379 +          }
  1.2380 +        }
  1.2381 +      }
  1.2382 +
  1.2383 +      if (pfd->GetFlag(PFD_ISNONEMPTYTEXTFRAME) || trimOutput.mChanged) {
  1.2384 +        // Pass up to caller so they can shrink their span
  1.2385 +        *aDeltaISize = trimOutput.mDeltaWidth;
  1.2386 +        return true;
  1.2387 +      }
  1.2388 +    }
  1.2389 +    pfd = pfd->mPrev;
  1.2390 +  }
  1.2391 +
  1.2392 +  *aDeltaISize = 0;
  1.2393 +  return false;
  1.2394 +}
  1.2395 +
  1.2396 +bool
  1.2397 +nsLineLayout::TrimTrailingWhiteSpace()
  1.2398 +{
  1.2399 +  PerSpanData* psd = mRootSpan;
  1.2400 +  nscoord deltaISize;
  1.2401 +  TrimTrailingWhiteSpaceIn(psd, &deltaISize);
  1.2402 +  return 0 != deltaISize;
  1.2403 +}
  1.2404 +
  1.2405 +void
  1.2406 +nsLineLayout::ComputeJustificationWeights(PerSpanData* aPSD,
  1.2407 +                                          int32_t* aNumSpaces,
  1.2408 +                                          int32_t* aNumLetters)
  1.2409 +{
  1.2410 +  NS_ASSERTION(aPSD, "null arg");
  1.2411 +  NS_ASSERTION(aNumSpaces, "null arg");
  1.2412 +  NS_ASSERTION(aNumLetters, "null arg");
  1.2413 +  int32_t numSpaces = 0;
  1.2414 +  int32_t numLetters = 0;
  1.2415 +
  1.2416 +  for (PerFrameData* pfd = aPSD->mFirstFrame; pfd != nullptr; pfd = pfd->mNext) {
  1.2417 +
  1.2418 +    if (true == pfd->GetFlag(PFD_ISTEXTFRAME)) {
  1.2419 +      numSpaces += pfd->mJustificationNumSpaces;
  1.2420 +      numLetters += pfd->mJustificationNumLetters;
  1.2421 +    }
  1.2422 +    else if (pfd->mSpan != nullptr) {
  1.2423 +      int32_t spanSpaces;
  1.2424 +      int32_t spanLetters;
  1.2425 +
  1.2426 +      ComputeJustificationWeights(pfd->mSpan, &spanSpaces, &spanLetters);
  1.2427 +
  1.2428 +      numSpaces += spanSpaces;
  1.2429 +      numLetters += spanLetters;
  1.2430 +    }
  1.2431 +  }
  1.2432 +
  1.2433 +  *aNumSpaces = numSpaces;
  1.2434 +  *aNumLetters = numLetters;
  1.2435 +}
  1.2436 +
  1.2437 +nscoord 
  1.2438 +nsLineLayout::ApplyFrameJustification(PerSpanData* aPSD, FrameJustificationState* aState)
  1.2439 +{
  1.2440 +  NS_ASSERTION(aPSD, "null arg");
  1.2441 +  NS_ASSERTION(aState, "null arg");
  1.2442 +
  1.2443 +  nscoord deltaICoord = 0;
  1.2444 +  for (PerFrameData* pfd = aPSD->mFirstFrame; pfd != nullptr; pfd = pfd->mNext) {
  1.2445 +    // Don't reposition bullets (and other frames that occur out of X-order?)
  1.2446 +    if (!pfd->GetFlag(PFD_ISBULLET)) {
  1.2447 +      nscoord dw = 0;
  1.2448 +      WritingMode lineWM = mRootSpan->mWritingMode;
  1.2449 +
  1.2450 +      pfd->mBounds.IStart(lineWM) += deltaICoord;
  1.2451 +
  1.2452 +      if (true == pfd->GetFlag(PFD_ISTEXTFRAME)) {
  1.2453 +        if (aState->mTotalWidthForSpaces > 0 &&
  1.2454 +            aState->mTotalNumSpaces > 0) {
  1.2455 +          aState->mNumSpacesProcessed += pfd->mJustificationNumSpaces;
  1.2456 +
  1.2457 +          nscoord newAllocatedWidthForSpaces =
  1.2458 +            (aState->mTotalWidthForSpaces*aState->mNumSpacesProcessed)
  1.2459 +              /aState->mTotalNumSpaces;
  1.2460 +          
  1.2461 +          dw += newAllocatedWidthForSpaces - aState->mWidthForSpacesProcessed;
  1.2462 +
  1.2463 +          aState->mWidthForSpacesProcessed = newAllocatedWidthForSpaces;
  1.2464 +        }
  1.2465 +
  1.2466 +        if (aState->mTotalWidthForLetters > 0 &&
  1.2467 +            aState->mTotalNumLetters > 0) {
  1.2468 +          aState->mNumLettersProcessed += pfd->mJustificationNumLetters;
  1.2469 +
  1.2470 +          nscoord newAllocatedWidthForLetters =
  1.2471 +            (aState->mTotalWidthForLetters*aState->mNumLettersProcessed)
  1.2472 +              /aState->mTotalNumLetters;
  1.2473 +          
  1.2474 +          dw += newAllocatedWidthForLetters - aState->mWidthForLettersProcessed;
  1.2475 +
  1.2476 +          aState->mWidthForLettersProcessed = newAllocatedWidthForLetters;
  1.2477 +        }
  1.2478 +        
  1.2479 +        if (dw) {
  1.2480 +          pfd->SetFlag(PFD_RECOMPUTEOVERFLOW, true);
  1.2481 +        }
  1.2482 +      }
  1.2483 +      else {
  1.2484 +        if (nullptr != pfd->mSpan) {
  1.2485 +          dw += ApplyFrameJustification(pfd->mSpan, aState);
  1.2486 +        }
  1.2487 +      }
  1.2488 +
  1.2489 +      pfd->mBounds.ISize(lineWM) += dw;
  1.2490 +
  1.2491 +      deltaICoord += dw;
  1.2492 +      pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.2493 +    }
  1.2494 +  }
  1.2495 +  return deltaICoord;
  1.2496 +}
  1.2497 +
  1.2498 +void
  1.2499 +nsLineLayout::InlineDirAlignFrames(nsLineBox* aLine,
  1.2500 +                                   bool aIsLastLine)
  1.2501 +{
  1.2502 +  /**
  1.2503 +   * NOTE: aIsLastLine ain't necessarily so: it is correctly set by caller
  1.2504 +   * only in cases where the last line needs special handling.
  1.2505 +   */
  1.2506 +  PerSpanData* psd = mRootSpan;
  1.2507 +  WritingMode lineWM = psd->mWritingMode;
  1.2508 +  NS_WARN_IF_FALSE(psd->mIEnd != NS_UNCONSTRAINEDSIZE,
  1.2509 +                   "have unconstrained width; this should only result from "
  1.2510 +                   "very large sizes, not attempts at intrinsic width "
  1.2511 +                   "calculation");
  1.2512 +  nscoord availISize = psd->mIEnd - psd->mIStart;
  1.2513 +  nscoord remainingISize = availISize - aLine->ISize();
  1.2514 +#ifdef NOISY_INLINEDIR_ALIGN
  1.2515 +  nsFrame::ListTag(stdout, mBlockReflowState->frame);
  1.2516 +  printf(": availISize=%d lineBounds.IStart=%d lineISize=%d delta=%d\n",
  1.2517 +         availISize, aLine->IStart(), aLine->ISize(), remainingISize);
  1.2518 +#endif
  1.2519 +
  1.2520 +  // 'text-align-last: auto' is equivalent to the value of the 'text-align'
  1.2521 +  // property except when 'text-align' is set to 'justify', in which case it
  1.2522 +  // is 'justify' when 'text-justify' is 'distribute' and 'start' otherwise.
  1.2523 +  //
  1.2524 +  // XXX: the code below will have to change when we implement text-justify
  1.2525 +  //
  1.2526 +  nscoord dx = 0;
  1.2527 +  uint8_t textAlign = mStyleText->mTextAlign;
  1.2528 +  bool textAlignTrue = mStyleText->mTextAlignTrue;
  1.2529 +  if (aIsLastLine) {
  1.2530 +    textAlignTrue = mStyleText->mTextAlignLastTrue;
  1.2531 +    if (mStyleText->mTextAlignLast == NS_STYLE_TEXT_ALIGN_AUTO) {
  1.2532 +      if (textAlign == NS_STYLE_TEXT_ALIGN_JUSTIFY) {
  1.2533 +        textAlign = NS_STYLE_TEXT_ALIGN_DEFAULT;
  1.2534 +      }
  1.2535 +    } else {
  1.2536 +      textAlign = mStyleText->mTextAlignLast;
  1.2537 +    }
  1.2538 +  }
  1.2539 +
  1.2540 +  if ((remainingISize > 0 || textAlignTrue) &&
  1.2541 +      !(mBlockReflowState->frame->IsSVGText())) {
  1.2542 +
  1.2543 +    switch (textAlign) {
  1.2544 +      case NS_STYLE_TEXT_ALIGN_JUSTIFY:
  1.2545 +        int32_t numSpaces;
  1.2546 +        int32_t numLetters;
  1.2547 +
  1.2548 +        ComputeJustificationWeights(psd, &numSpaces, &numLetters);
  1.2549 +
  1.2550 +        if (numSpaces > 0) {
  1.2551 +          FrameJustificationState state =
  1.2552 +            { numSpaces, numLetters, remainingISize, 0, 0, 0, 0, 0 };
  1.2553 +
  1.2554 +          // Apply the justification, and make sure to update our linebox
  1.2555 +          // width to account for it.
  1.2556 +          aLine->ExpandBy(ApplyFrameJustification(psd, &state),
  1.2557 +                          mContainerWidth);
  1.2558 +          remainingISize = availISize - aLine->ISize();
  1.2559 +          break;
  1.2560 +        }
  1.2561 +        // Fall through to the default case if we could not justify to fill
  1.2562 +        // the space.
  1.2563 +
  1.2564 +      case NS_STYLE_TEXT_ALIGN_DEFAULT:
  1.2565 +        // default alignment is to start edge so do nothing
  1.2566 +        break;
  1.2567 +
  1.2568 +      case NS_STYLE_TEXT_ALIGN_LEFT:
  1.2569 +      case NS_STYLE_TEXT_ALIGN_MOZ_LEFT:
  1.2570 +        if (!lineWM.IsBidiLTR()) {
  1.2571 +          dx = remainingISize;
  1.2572 +        }
  1.2573 +        break;
  1.2574 +
  1.2575 +      case NS_STYLE_TEXT_ALIGN_RIGHT:
  1.2576 +      case NS_STYLE_TEXT_ALIGN_MOZ_RIGHT:
  1.2577 +        if (lineWM.IsBidiLTR()) {
  1.2578 +          dx = remainingISize;
  1.2579 +        }
  1.2580 +        break;
  1.2581 +
  1.2582 +      case NS_STYLE_TEXT_ALIGN_END:
  1.2583 +        dx = remainingISize;
  1.2584 +        break;
  1.2585 +
  1.2586 +
  1.2587 +      case NS_STYLE_TEXT_ALIGN_CENTER:
  1.2588 +      case NS_STYLE_TEXT_ALIGN_MOZ_CENTER:
  1.2589 +        dx = remainingISize / 2;
  1.2590 +        break;
  1.2591 +    }
  1.2592 +  }
  1.2593 +
  1.2594 +  if (dx) {
  1.2595 +    for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.2596 +      pfd->mBounds.IStart(lineWM) += dx;
  1.2597 +      pfd->mFrame->SetRect(lineWM, pfd->mBounds, mContainerWidth);
  1.2598 +    }
  1.2599 +    aLine->IndentBy(dx, mContainerWidth);
  1.2600 +  }
  1.2601 +
  1.2602 +  if (mPresContext->BidiEnabled() &&
  1.2603 +      (!mPresContext->IsVisualMode() || !lineWM.IsBidiLTR())) {
  1.2604 +    nsBidiPresUtils::ReorderFrames(psd->mFirstFrame->mFrame,
  1.2605 +                                   aLine->GetChildCount(),
  1.2606 +                                   lineWM, mContainerWidth);
  1.2607 +  }
  1.2608 +}
  1.2609 +
  1.2610 +void
  1.2611 +nsLineLayout::RelativePositionFrames(nsOverflowAreas& aOverflowAreas)
  1.2612 +{
  1.2613 +  RelativePositionFrames(mRootSpan, aOverflowAreas);
  1.2614 +}
  1.2615 +
  1.2616 +void
  1.2617 +nsLineLayout::RelativePositionFrames(PerSpanData* psd, nsOverflowAreas& aOverflowAreas)
  1.2618 +{
  1.2619 +  nsOverflowAreas overflowAreas;
  1.2620 +  WritingMode wm = psd->mWritingMode;
  1.2621 +  if (nullptr != psd->mFrame) {
  1.2622 +    // The span's overflow areas come in three parts:
  1.2623 +    // -- this frame's width and height
  1.2624 +    // -- pfd->mOverflowAreas, which is the area of a bullet or the union
  1.2625 +    // of a relatively positioned frame's absolute children
  1.2626 +    // -- the bounds of all inline descendants
  1.2627 +    // The former two parts are computed right here, we gather the descendants
  1.2628 +    // below.
  1.2629 +    // At this point psd->mFrame->mBounds might be out of date since
  1.2630 +    // bidi reordering can move and resize the frames. So use the frame's
  1.2631 +    // rect instead of mBounds.
  1.2632 +    nsRect adjustedBounds(nsPoint(0, 0), psd->mFrame->mFrame->GetSize());
  1.2633 +
  1.2634 +    overflowAreas.ScrollableOverflow().UnionRect(
  1.2635 +      psd->mFrame->mOverflowAreas.ScrollableOverflow(), adjustedBounds);
  1.2636 +    overflowAreas.VisualOverflow().UnionRect(
  1.2637 +      psd->mFrame->mOverflowAreas.VisualOverflow(), adjustedBounds);
  1.2638 +  }
  1.2639 +  else {
  1.2640 +    LogicalRect rect(wm, psd->mIStart, mBStartEdge,
  1.2641 +                     psd->mICoord - psd->mIStart, mFinalLineBSize);
  1.2642 +    // The minimum combined area for the frames that are direct
  1.2643 +    // children of the block starts at the upper left corner of the
  1.2644 +    // line and is sized to match the size of the line's bounding box
  1.2645 +    // (the same size as the values returned from BlockDirAlignFrames)
  1.2646 +    overflowAreas.VisualOverflow() = rect.GetPhysicalRect(wm, mContainerWidth);
  1.2647 +    overflowAreas.ScrollableOverflow() = overflowAreas.VisualOverflow();
  1.2648 +  }
  1.2649 +
  1.2650 +  for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
  1.2651 +    nsIFrame* frame = pfd->mFrame;
  1.2652 +    nsPoint origin = frame->GetPosition();
  1.2653 +
  1.2654 +    // Adjust the origin of the frame
  1.2655 +    if (pfd->GetFlag(PFD_RELATIVEPOS)) {
  1.2656 +      //XXX temporary until ApplyRelativePositioning can handle logical offsets
  1.2657 +      nsMargin physicalOffsets =
  1.2658 +        pfd->mOffsets.GetPhysicalMargin(pfd->mFrame->GetWritingMode());
  1.2659 +      // right and bottom are handled by
  1.2660 +      // nsHTMLReflowState::ComputeRelativeOffsets
  1.2661 +      nsHTMLReflowState::ApplyRelativePositioning(pfd->mFrame,
  1.2662 +                                                  physicalOffsets,
  1.2663 +                                                  &origin);
  1.2664 +      frame->SetPosition(origin);
  1.2665 +    }
  1.2666 +
  1.2667 +    // We must position the view correctly before positioning its
  1.2668 +    // descendants so that widgets are positioned properly (since only
  1.2669 +    // some views have widgets).
  1.2670 +    if (frame->HasView())
  1.2671 +      nsContainerFrame::SyncFrameViewAfterReflow(mPresContext, frame,
  1.2672 +        frame->GetView(), pfd->mOverflowAreas.VisualOverflow(),
  1.2673 +        NS_FRAME_NO_SIZE_VIEW);
  1.2674 +
  1.2675 +    // Note: the combined area of a child is in its coordinate
  1.2676 +    // system. We adjust the childs combined area into our coordinate
  1.2677 +    // system before computing the aggregated value by adding in
  1.2678 +    // <b>x</b> and <b>y</b> which were computed above.
  1.2679 +    nsOverflowAreas r;
  1.2680 +    if (pfd->mSpan) {
  1.2681 +      // Compute a new combined area for the child span before
  1.2682 +      // aggregating it into our combined area.
  1.2683 +      RelativePositionFrames(pfd->mSpan, r);
  1.2684 +    } else {
  1.2685 +      r = pfd->mOverflowAreas;
  1.2686 +      if (pfd->GetFlag(PFD_ISTEXTFRAME)) {
  1.2687 +        // We need to recompute overflow areas in two cases:
  1.2688 +        // (1) When PFD_RECOMPUTEOVERFLOW is set due to trimming
  1.2689 +        // (2) When there are text decorations, since we can't recompute the
  1.2690 +        //     overflow area until Reflow and BlockDirAlignLine have finished
  1.2691 +        if (pfd->GetFlag(PFD_RECOMPUTEOVERFLOW) ||
  1.2692 +            frame->StyleContext()->HasTextDecorationLines()) {
  1.2693 +          nsTextFrame* f = static_cast<nsTextFrame*>(frame);
  1.2694 +          r = f->RecomputeOverflow(*mBlockReflowState);
  1.2695 +        }
  1.2696 +        frame->FinishAndStoreOverflow(r, frame->GetSize());
  1.2697 +      }
  1.2698 +
  1.2699 +      // If we have something that's not an inline but with a complex frame
  1.2700 +      // hierarchy inside that contains views, they need to be
  1.2701 +      // positioned.
  1.2702 +      // All descendant views must be repositioned even if this frame
  1.2703 +      // does have a view in case this frame's view does not have a
  1.2704 +      // widget and some of the descendant views do have widgets --
  1.2705 +      // otherwise the widgets won't be repositioned.
  1.2706 +      nsContainerFrame::PositionChildViews(frame);
  1.2707 +    }
  1.2708 +
  1.2709 +    // Do this here (rather than along with setting the overflow rect
  1.2710 +    // below) so we get leaf frames as well.  No need to worry
  1.2711 +    // about the root span, since it doesn't have a frame.
  1.2712 +    if (frame->HasView())
  1.2713 +      nsContainerFrame::SyncFrameViewAfterReflow(mPresContext, frame,
  1.2714 +                                                 frame->GetView(),
  1.2715 +                                                 r.VisualOverflow(),
  1.2716 +                                                 NS_FRAME_NO_MOVE_VIEW);
  1.2717 +
  1.2718 +    overflowAreas.UnionWith(r + origin);
  1.2719 +  }
  1.2720 +
  1.2721 +  // If we just computed a spans combined area, we need to update its
  1.2722 +  // overflow rect...
  1.2723 +  if (psd->mFrame) {
  1.2724 +    PerFrameData* spanPFD = psd->mFrame;
  1.2725 +    nsIFrame* frame = spanPFD->mFrame;
  1.2726 +    frame->FinishAndStoreOverflow(overflowAreas, frame->GetSize());
  1.2727 +  }
  1.2728 +  aOverflowAreas = overflowAreas;
  1.2729 +}

mercurial