1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* state and methods used while laying out a single line of a block frame */
8 
9 #include "nsLineLayout.h"
10 
11 #include "LayoutLogging.h"
12 #include "SVGTextFrame.h"
13 #include "nsBlockFrame.h"
14 #include "nsFontMetrics.h"
15 #include "nsStyleConsts.h"
16 #include "nsContainerFrame.h"
17 #include "nsFloatManager.h"
18 #include "nsStyleContext.h"
19 #include "nsPresContext.h"
20 #include "nsGkAtoms.h"
21 #include "nsIContent.h"
22 #include "nsLayoutUtils.h"
23 #include "nsTextFrame.h"
24 #include "nsStyleStructInlines.h"
25 #include "nsBidiPresUtils.h"
26 #include "nsRubyFrame.h"
27 #include "nsRubyTextFrame.h"
28 #include "RubyUtils.h"
29 #include <algorithm>
30 
31 #ifdef DEBUG
32 #undef NOISY_INLINEDIR_ALIGN
33 #undef NOISY_BLOCKDIR_ALIGN
34 #undef NOISY_REFLOW
35 #undef REALLY_NOISY_REFLOW
36 #undef NOISY_PUSHING
37 #undef REALLY_NOISY_PUSHING
38 #undef NOISY_CAN_PLACE_FRAME
39 #undef NOISY_TRIM
40 #undef REALLY_NOISY_TRIM
41 #endif
42 
43 using namespace mozilla;
44 
45 //----------------------------------------------------------------------
46 
47 #define FIX_BUG_50257
48 
nsLineLayout(nsPresContext * aPresContext,nsFloatManager * aFloatManager,const ReflowInput * aOuterReflowInput,const nsLineList::iterator * aLine,nsLineLayout * aBaseLineLayout)49 nsLineLayout::nsLineLayout(nsPresContext* aPresContext,
50                            nsFloatManager* aFloatManager,
51                            const ReflowInput* aOuterReflowInput,
52                            const nsLineList::iterator* aLine,
53                            nsLineLayout* aBaseLineLayout)
54     : mPresContext(aPresContext),
55       mFloatManager(aFloatManager),
56       mBlockReflowInput(aOuterReflowInput),
57       mBaseLineLayout(aBaseLineLayout),
58       mLastOptionalBreakFrame(nullptr),
59       mForceBreakFrame(nullptr),
60       mBlockRI(nullptr), /* XXX temporary */
61       mLastOptionalBreakPriority(gfxBreakPriority::eNoBreak),
62       mLastOptionalBreakFrameOffset(-1),
63       mForceBreakFrameOffset(-1),
64       mMinLineBSize(0),
65       mTextIndent(0),
66       mFirstLetterStyleOK(false),
67       mIsTopOfPage(false),
68       mImpactedByFloats(false),
69       mLastFloatWasLetterFrame(false),
70       mLineIsEmpty(false),
71       mLineEndsInBR(false),
72       mNeedBackup(false),
73       mInFirstLine(false),
74       mGotLineBox(false),
75       mInFirstLetter(false),
76       mHasBullet(false),
77       mDirtyNextLine(false),
78       mLineAtStart(false),
79       mHasRuby(false),
80       mSuppressLineWrap(
81           nsSVGUtils::IsInSVGTextSubtree(aOuterReflowInput->mFrame)) {
82   MOZ_ASSERT(aOuterReflowInput, "aOuterReflowInput must not be null");
83   NS_ASSERTION(aFloatManager || aOuterReflowInput->mFrame->IsLetterFrame(),
84                "float manager should be present");
85   MOZ_ASSERT((!!mBaseLineLayout) ==
86                  aOuterReflowInput->mFrame->IsRubyTextContainerFrame(),
87              "Only ruby text container frames have "
88              "a different base line layout");
89   MOZ_COUNT_CTOR(nsLineLayout);
90 
91   // Stash away some style data that we need
92   nsBlockFrame* blockFrame = do_QueryFrame(aOuterReflowInput->mFrame);
93   if (blockFrame)
94     mStyleText = blockFrame->StyleTextForLineLayout();
95   else
96     mStyleText = aOuterReflowInput->mFrame->StyleText();
97 
98   mLineNumber = 0;
99   mTotalPlacedFrames = 0;
100   mBStartEdge = 0;
101   mTrimmableISize = 0;
102 
103   mInflationMinFontSize =
104       nsLayoutUtils::InflationMinFontSizeFor(aOuterReflowInput->mFrame);
105 
106   // Instead of always pre-initializing the free-lists for frames and
107   // spans, we do it on demand so that situations that only use a few
108   // frames and spans won't waste a lot of time in unneeded
109   // initialization.
110   mFrameFreeList = nullptr;
111   mSpanFreeList = nullptr;
112 
113   mCurrentSpan = mRootSpan = nullptr;
114   mSpanDepth = 0;
115 
116   if (aLine) {
117     mGotLineBox = true;
118     mLineBox = *aLine;
119   }
120 }
121 
~nsLineLayout()122 nsLineLayout::~nsLineLayout() {
123   MOZ_COUNT_DTOR(nsLineLayout);
124 
125   NS_ASSERTION(nullptr == mRootSpan, "bad line-layout user");
126 }
127 
128 // Find out if the frame has a non-null prev-in-flow, i.e., whether it
129 // is a continuation.
HasPrevInFlow(nsIFrame * aFrame)130 inline bool HasPrevInFlow(nsIFrame* aFrame) {
131   nsIFrame* prevInFlow = aFrame->GetPrevInFlow();
132   return prevInFlow != nullptr;
133 }
134 
BeginLineReflow(nscoord aICoord,nscoord aBCoord,nscoord aISize,nscoord aBSize,bool aImpactedByFloats,bool aIsTopOfPage,WritingMode aWritingMode,const nsSize & aContainerSize)135 void nsLineLayout::BeginLineReflow(nscoord aICoord, nscoord aBCoord,
136                                    nscoord aISize, nscoord aBSize,
137                                    bool aImpactedByFloats, bool aIsTopOfPage,
138                                    WritingMode aWritingMode,
139                                    const nsSize& aContainerSize) {
140   NS_ASSERTION(nullptr == mRootSpan, "bad linelayout user");
141   LAYOUT_WARN_IF_FALSE(aISize != NS_UNCONSTRAINEDSIZE,
142                        "have unconstrained width; this should only result from "
143                        "very large sizes, not attempts at intrinsic width "
144                        "calculation");
145 #ifdef DEBUG
146   if ((aISize != NS_UNCONSTRAINEDSIZE) && CRAZY_SIZE(aISize) &&
147       !LineContainerFrame()->GetParent()->IsCrazySizeAssertSuppressed()) {
148     nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
149     printf(": Init: bad caller: width WAS %d(0x%x)\n", aISize, aISize);
150   }
151   if ((aBSize != NS_UNCONSTRAINEDSIZE) && CRAZY_SIZE(aBSize) &&
152       !LineContainerFrame()->GetParent()->IsCrazySizeAssertSuppressed()) {
153     nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
154     printf(": Init: bad caller: height WAS %d(0x%x)\n", aBSize, aBSize);
155   }
156 #endif
157 #ifdef NOISY_REFLOW
158   nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
159   printf(": BeginLineReflow: %d,%d,%d,%d impacted=%s %s\n", aICoord, aBCoord,
160          aISize, aBSize, aImpactedByFloats ? "true" : "false",
161          aIsTopOfPage ? "top-of-page" : "");
162 #endif
163 #ifdef DEBUG
164   mSpansAllocated = mSpansFreed = mFramesAllocated = mFramesFreed = 0;
165 #endif
166 
167   mFirstLetterStyleOK = false;
168   mIsTopOfPage = aIsTopOfPage;
169   mImpactedByFloats = aImpactedByFloats;
170   mTotalPlacedFrames = 0;
171   if (!mBaseLineLayout) {
172     mLineIsEmpty = true;
173     mLineAtStart = true;
174   } else {
175     mLineIsEmpty = false;
176     mLineAtStart = false;
177   }
178   mLineEndsInBR = false;
179   mSpanDepth = 0;
180   mMaxStartBoxBSize = mMaxEndBoxBSize = 0;
181 
182   if (mGotLineBox) {
183     mLineBox->ClearHasBullet();
184   }
185 
186   PerSpanData* psd = NewPerSpanData();
187   mCurrentSpan = mRootSpan = psd;
188   psd->mReflowInput = mBlockReflowInput;
189   psd->mIStart = aICoord;
190   psd->mICoord = aICoord;
191   psd->mIEnd = aICoord + aISize;
192   mContainerSize = aContainerSize;
193 
194   mBStartEdge = aBCoord;
195 
196   psd->mNoWrap = !mStyleText->WhiteSpaceCanWrapStyle() || mSuppressLineWrap;
197   psd->mWritingMode = aWritingMode;
198 
199   // If this is the first line of a block then see if the text-indent
200   // property amounts to anything.
201 
202   if (0 == mLineNumber && !HasPrevInFlow(mBlockReflowInput->mFrame)) {
203     const nsStyleCoord& textIndent = mStyleText->mTextIndent;
204     nscoord pctBasis = 0;
205     if (textIndent.HasPercent()) {
206       pctBasis =
207           mBlockReflowInput->GetContainingBlockContentISize(aWritingMode);
208     }
209     nscoord indent = textIndent.ComputeCoordPercentCalc(pctBasis);
210 
211     mTextIndent = indent;
212 
213     psd->mICoord += indent;
214   }
215 
216   PerFrameData* pfd = NewPerFrameData(mBlockReflowInput->mFrame);
217   pfd->mAscent = 0;
218   pfd->mSpan = psd;
219   psd->mFrame = pfd;
220   nsIFrame* frame = mBlockReflowInput->mFrame;
221   if (frame->IsRubyTextContainerFrame()) {
222     // Ruby text container won't be reflowed via ReflowFrame, hence the
223     // relative positioning information should be recorded here.
224     MOZ_ASSERT(mBaseLineLayout != this);
225     pfd->mRelativePos =
226         mBlockReflowInput->mStyleDisplay->IsRelativelyPositionedStyle();
227     if (pfd->mRelativePos) {
228       MOZ_ASSERT(mBlockReflowInput->GetWritingMode() == pfd->mWritingMode,
229                  "mBlockReflowInput->frame == frame, "
230                  "hence they should have identical writing mode");
231       pfd->mOffsets = mBlockReflowInput->ComputedLogicalOffsets();
232     }
233   }
234 }
235 
EndLineReflow()236 void nsLineLayout::EndLineReflow() {
237 #ifdef NOISY_REFLOW
238   nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
239   printf(": EndLineReflow: width=%d\n",
240          mRootSpan->mICoord - mRootSpan->mIStart);
241 #endif
242 
243   NS_ASSERTION(!mBaseLineLayout ||
244                    (!mSpansAllocated && !mSpansFreed && !mSpanFreeList &&
245                     !mFramesAllocated && !mFramesFreed && !mFrameFreeList),
246                "Allocated frames or spans on non-base line layout?");
247 
248   UnlinkFrame(mRootSpan->mFrame);
249   mCurrentSpan = mRootSpan = nullptr;
250 
251   NS_ASSERTION(mSpansAllocated == mSpansFreed, "leak");
252   NS_ASSERTION(mFramesAllocated == mFramesFreed, "leak");
253 
254 #if 0
255   static int32_t maxSpansAllocated = NS_LINELAYOUT_NUM_SPANS;
256   static int32_t maxFramesAllocated = NS_LINELAYOUT_NUM_FRAMES;
257   if (mSpansAllocated > maxSpansAllocated) {
258     printf("XXX: saw a line with %d spans\n", mSpansAllocated);
259     maxSpansAllocated = mSpansAllocated;
260   }
261   if (mFramesAllocated > maxFramesAllocated) {
262     printf("XXX: saw a line with %d frames\n", mFramesAllocated);
263     maxFramesAllocated = mFramesAllocated;
264   }
265 #endif
266 }
267 
268 // XXX swtich to a single mAvailLineWidth that we adjust as each frame
269 // on the line is placed. Each span can still have a per-span mICoord that
270 // tracks where a child frame is going in its span; they don't need a
271 // per-span mIStart?
272 
UpdateBand(WritingMode aWM,const LogicalRect & aNewAvailSpace,nsIFrame * aFloatFrame)273 void nsLineLayout::UpdateBand(WritingMode aWM,
274                               const LogicalRect& aNewAvailSpace,
275                               nsIFrame* aFloatFrame) {
276   WritingMode lineWM = mRootSpan->mWritingMode;
277   // need to convert to our writing mode, because we might have a different
278   // mode from the caller due to dir: auto
279   LogicalRect availSpace =
280       aNewAvailSpace.ConvertTo(lineWM, aWM, ContainerSize());
281 #ifdef REALLY_NOISY_REFLOW
282   printf(
283       "nsLL::UpdateBand %d, %d, %d, %d, (converted to %d, %d, %d, %d); "
284       "frame=%p\n  will set mImpacted to true\n",
285       aNewAvailSpace.IStart(aWM), aNewAvailSpace.BStart(aWM),
286       aNewAvailSpace.ISize(aWM), aNewAvailSpace.BSize(aWM),
287       availSpace.IStart(lineWM), availSpace.BStart(lineWM),
288       availSpace.ISize(lineWM), availSpace.BSize(lineWM), aFloatFrame);
289 #endif
290 #ifdef DEBUG
291   if ((availSpace.ISize(lineWM) != NS_UNCONSTRAINEDSIZE) &&
292       CRAZY_SIZE(availSpace.ISize(lineWM)) &&
293       !LineContainerFrame()->GetParent()->IsCrazySizeAssertSuppressed()) {
294     nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
295     printf(": UpdateBand: bad caller: ISize WAS %d(0x%x)\n",
296            availSpace.ISize(lineWM), availSpace.ISize(lineWM));
297   }
298   if ((availSpace.BSize(lineWM) != NS_UNCONSTRAINEDSIZE) &&
299       CRAZY_SIZE(availSpace.BSize(lineWM)) &&
300       !LineContainerFrame()->GetParent()->IsCrazySizeAssertSuppressed()) {
301     nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
302     printf(": UpdateBand: bad caller: BSize WAS %d(0x%x)\n",
303            availSpace.BSize(lineWM), availSpace.BSize(lineWM));
304   }
305 #endif
306 
307   // Compute the difference between last times width and the new width
308   NS_WARNING_ASSERTION(
309       mRootSpan->mIEnd != NS_UNCONSTRAINEDSIZE &&
310           availSpace.ISize(lineWM) != NS_UNCONSTRAINEDSIZE,
311       "have unconstrained inline size; this should only result from very large "
312       "sizes, not attempts at intrinsic width calculation");
313   // The root span's mIStart moves to aICoord
314   nscoord deltaICoord = availSpace.IStart(lineWM) - mRootSpan->mIStart;
315   // The inline size of all spans changes by this much (the root span's
316   // mIEnd moves to aICoord + aISize, its new inline size is aISize)
317   nscoord deltaISize =
318       availSpace.ISize(lineWM) - (mRootSpan->mIEnd - mRootSpan->mIStart);
319 #ifdef NOISY_REFLOW
320   nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
321   printf(": UpdateBand: %d,%d,%d,%d deltaISize=%d deltaICoord=%d\n",
322          availSpace.IStart(lineWM), availSpace.BStart(lineWM),
323          availSpace.ISize(lineWM), availSpace.BSize(lineWM), deltaISize,
324          deltaICoord);
325 #endif
326 
327   // Update the root span position
328   mRootSpan->mIStart += deltaICoord;
329   mRootSpan->mIEnd += deltaICoord;
330   mRootSpan->mICoord += deltaICoord;
331 
332   // Now update the right edges of the open spans to account for any
333   // change in available space width
334   for (PerSpanData* psd = mCurrentSpan; psd; psd = psd->mParent) {
335     psd->mIEnd += deltaISize;
336     psd->mContainsFloat = true;
337 #ifdef NOISY_REFLOW
338     printf("  span %p: oldIEnd=%d newIEnd=%d\n", psd, psd->mIEnd - deltaISize,
339            psd->mIEnd);
340 #endif
341   }
342   NS_ASSERTION(mRootSpan->mContainsFloat &&
343                    mRootSpan->mIStart == availSpace.IStart(lineWM) &&
344                    mRootSpan->mIEnd == availSpace.IEnd(lineWM),
345                "root span was updated incorrectly?");
346 
347   // Update frame bounds
348   // Note: Only adjust the outermost frames (the ones that are direct
349   // children of the block), not the ones in the child spans. The reason
350   // is simple: the frames in the spans have coordinates local to their
351   // parent therefore they are moved when their parent span is moved.
352   if (deltaICoord != 0) {
353     for (PerFrameData* pfd = mRootSpan->mFirstFrame; pfd; pfd = pfd->mNext) {
354       pfd->mBounds.IStart(lineWM) += deltaICoord;
355     }
356   }
357 
358   mBStartEdge = availSpace.BStart(lineWM);
359   mImpactedByFloats = true;
360 
361   mLastFloatWasLetterFrame = aFloatFrame->IsLetterFrame();
362 }
363 
NewPerSpanData()364 nsLineLayout::PerSpanData* nsLineLayout::NewPerSpanData() {
365   nsLineLayout* outerLineLayout = GetOutermostLineLayout();
366   PerSpanData* psd = outerLineLayout->mSpanFreeList;
367   if (!psd) {
368     void* mem = outerLineLayout->mArena.Allocate(sizeof(PerSpanData));
369     psd = reinterpret_cast<PerSpanData*>(mem);
370   } else {
371     outerLineLayout->mSpanFreeList = psd->mNextFreeSpan;
372   }
373   psd->mParent = nullptr;
374   psd->mFrame = nullptr;
375   psd->mFirstFrame = nullptr;
376   psd->mLastFrame = nullptr;
377   psd->mContainsFloat = false;
378   psd->mHasNonemptyContent = false;
379 
380 #ifdef DEBUG
381   outerLineLayout->mSpansAllocated++;
382 #endif
383   return psd;
384 }
385 
BeginSpan(nsIFrame * aFrame,const ReflowInput * aSpanReflowInput,nscoord aIStart,nscoord aIEnd,nscoord * aBaseline)386 void nsLineLayout::BeginSpan(nsIFrame* aFrame,
387                              const ReflowInput* aSpanReflowInput,
388                              nscoord aIStart, nscoord aIEnd,
389                              nscoord* aBaseline) {
390   NS_ASSERTION(aIEnd != NS_UNCONSTRAINEDSIZE,
391                "should no longer be using unconstrained sizes");
392 #ifdef NOISY_REFLOW
393   nsFrame::IndentBy(stdout, mSpanDepth + 1);
394   nsFrame::ListTag(stdout, aFrame);
395   printf(": BeginSpan leftEdge=%d rightEdge=%d\n", aIStart, aIEnd);
396 #endif
397 
398   PerSpanData* psd = NewPerSpanData();
399   // Link up span frame's pfd to point to its child span data
400   PerFrameData* pfd = mCurrentSpan->mLastFrame;
401   NS_ASSERTION(pfd->mFrame == aFrame, "huh?");
402   pfd->mSpan = psd;
403 
404   // Init new span
405   psd->mFrame = pfd;
406   psd->mParent = mCurrentSpan;
407   psd->mReflowInput = aSpanReflowInput;
408   psd->mIStart = aIStart;
409   psd->mICoord = aIStart;
410   psd->mIEnd = aIEnd;
411   psd->mBaseline = aBaseline;
412 
413   nsIFrame* frame = aSpanReflowInput->mFrame;
414   psd->mNoWrap = !frame->StyleText()->WhiteSpaceCanWrap(frame) ||
415                  mSuppressLineWrap ||
416                  frame->StyleContext()->ShouldSuppressLineBreak();
417   psd->mWritingMode = aSpanReflowInput->GetWritingMode();
418 
419   // Switch to new span
420   mCurrentSpan = psd;
421   mSpanDepth++;
422 }
423 
EndSpan(nsIFrame * aFrame)424 nscoord nsLineLayout::EndSpan(nsIFrame* aFrame) {
425   NS_ASSERTION(mSpanDepth > 0, "end-span without begin-span");
426 #ifdef NOISY_REFLOW
427   nsFrame::IndentBy(stdout, mSpanDepth);
428   nsFrame::ListTag(stdout, aFrame);
429   printf(": EndSpan width=%d\n", mCurrentSpan->mICoord - mCurrentSpan->mIStart);
430 #endif
431   PerSpanData* psd = mCurrentSpan;
432   nscoord iSizeResult = psd->mLastFrame ? (psd->mICoord - psd->mIStart) : 0;
433 
434   mSpanDepth--;
435   mCurrentSpan->mReflowInput = nullptr;  // no longer valid so null it out!
436   mCurrentSpan = mCurrentSpan->mParent;
437   return iSizeResult;
438 }
439 
AttachFrameToBaseLineLayout(PerFrameData * aFrame)440 void nsLineLayout::AttachFrameToBaseLineLayout(PerFrameData* aFrame) {
441   NS_PRECONDITION(mBaseLineLayout,
442                   "This method must not be called in a base line layout.");
443 
444   PerFrameData* baseFrame = mBaseLineLayout->LastFrame();
445   MOZ_ASSERT(aFrame && baseFrame);
446   MOZ_ASSERT(!aFrame->mIsLinkedToBase,
447              "The frame must not have been linked with the base");
448 #ifdef DEBUG
449   LayoutFrameType baseType = baseFrame->mFrame->Type();
450   LayoutFrameType annotationType = aFrame->mFrame->Type();
451   MOZ_ASSERT((baseType == LayoutFrameType::RubyBaseContainer &&
452               annotationType == LayoutFrameType::RubyTextContainer) ||
453              (baseType == LayoutFrameType::RubyBase &&
454               annotationType == LayoutFrameType::RubyText));
455 #endif
456 
457   aFrame->mNextAnnotation = baseFrame->mNextAnnotation;
458   baseFrame->mNextAnnotation = aFrame;
459   aFrame->mIsLinkedToBase = true;
460 }
461 
GetCurrentSpanCount() const462 int32_t nsLineLayout::GetCurrentSpanCount() const {
463   NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
464   int32_t count = 0;
465   PerFrameData* pfd = mRootSpan->mFirstFrame;
466   while (nullptr != pfd) {
467     count++;
468     pfd = pfd->mNext;
469   }
470   return count;
471 }
472 
SplitLineTo(int32_t aNewCount)473 void nsLineLayout::SplitLineTo(int32_t aNewCount) {
474   NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
475 
476 #ifdef REALLY_NOISY_PUSHING
477   printf("SplitLineTo %d (current count=%d); before:\n", aNewCount,
478          GetCurrentSpanCount());
479   DumpPerSpanData(mRootSpan, 1);
480 #endif
481   PerSpanData* psd = mRootSpan;
482   PerFrameData* pfd = psd->mFirstFrame;
483   while (nullptr != pfd) {
484     if (--aNewCount == 0) {
485       // Truncate list at pfd (we keep pfd, but anything following is freed)
486       PerFrameData* next = pfd->mNext;
487       pfd->mNext = nullptr;
488       psd->mLastFrame = pfd;
489 
490       // Now unlink all of the frames following pfd
491       UnlinkFrame(next);
492       break;
493     }
494     pfd = pfd->mNext;
495   }
496 #ifdef NOISY_PUSHING
497   printf("SplitLineTo %d (current count=%d); after:\n", aNewCount,
498          GetCurrentSpanCount());
499   DumpPerSpanData(mRootSpan, 1);
500 #endif
501 }
502 
PushFrame(nsIFrame * aFrame)503 void nsLineLayout::PushFrame(nsIFrame* aFrame) {
504   PerSpanData* psd = mCurrentSpan;
505   NS_ASSERTION(psd->mLastFrame->mFrame == aFrame, "pushing non-last frame");
506 
507 #ifdef REALLY_NOISY_PUSHING
508   nsFrame::IndentBy(stdout, mSpanDepth);
509   printf("PushFrame %p, before:\n", psd);
510   DumpPerSpanData(psd, 1);
511 #endif
512 
513   // Take the last frame off of the span's frame list
514   PerFrameData* pfd = psd->mLastFrame;
515   if (pfd == psd->mFirstFrame) {
516     // We are pushing away the only frame...empty the list
517     psd->mFirstFrame = nullptr;
518     psd->mLastFrame = nullptr;
519   } else {
520     PerFrameData* prevFrame = pfd->mPrev;
521     prevFrame->mNext = nullptr;
522     psd->mLastFrame = prevFrame;
523   }
524 
525   // Now unlink the frame
526   MOZ_ASSERT(!pfd->mNext);
527   UnlinkFrame(pfd);
528 #ifdef NOISY_PUSHING
529   nsFrame::IndentBy(stdout, mSpanDepth);
530   printf("PushFrame: %p after:\n", psd);
531   DumpPerSpanData(psd, 1);
532 #endif
533 }
534 
UnlinkFrame(PerFrameData * pfd)535 void nsLineLayout::UnlinkFrame(PerFrameData* pfd) {
536   while (nullptr != pfd) {
537     PerFrameData* next = pfd->mNext;
538     if (pfd->mIsLinkedToBase) {
539       // This frame is linked to a ruby base, and should not be freed
540       // now. Just unlink it from the span. It will be freed when its
541       // base frame gets unlinked.
542       pfd->mNext = pfd->mPrev = nullptr;
543       pfd = next;
544       continue;
545     }
546 
547     // It is a ruby base frame. If there are any annotations
548     // linked to this frame, free them first.
549     PerFrameData* annotationPFD = pfd->mNextAnnotation;
550     while (annotationPFD) {
551       PerFrameData* nextAnnotation = annotationPFD->mNextAnnotation;
552       MOZ_ASSERT(
553           annotationPFD->mNext == nullptr && annotationPFD->mPrev == nullptr,
554           "PFD in annotations should have been unlinked.");
555       FreeFrame(annotationPFD);
556       annotationPFD = nextAnnotation;
557     }
558 
559     FreeFrame(pfd);
560     pfd = next;
561   }
562 }
563 
FreeFrame(PerFrameData * pfd)564 void nsLineLayout::FreeFrame(PerFrameData* pfd) {
565   if (nullptr != pfd->mSpan) {
566     FreeSpan(pfd->mSpan);
567   }
568   nsLineLayout* outerLineLayout = GetOutermostLineLayout();
569   pfd->mNext = outerLineLayout->mFrameFreeList;
570   outerLineLayout->mFrameFreeList = pfd;
571 #ifdef DEBUG
572   outerLineLayout->mFramesFreed++;
573 #endif
574 }
575 
FreeSpan(PerSpanData * psd)576 void nsLineLayout::FreeSpan(PerSpanData* psd) {
577   // Unlink its frames
578   UnlinkFrame(psd->mFirstFrame);
579 
580   nsLineLayout* outerLineLayout = GetOutermostLineLayout();
581   // Now put the span on the free list since it's free too
582   psd->mNextFreeSpan = outerLineLayout->mSpanFreeList;
583   outerLineLayout->mSpanFreeList = psd;
584 #ifdef DEBUG
585   outerLineLayout->mSpansFreed++;
586 #endif
587 }
588 
IsZeroBSize()589 bool nsLineLayout::IsZeroBSize() {
590   PerSpanData* psd = mCurrentSpan;
591   PerFrameData* pfd = psd->mFirstFrame;
592   while (nullptr != pfd) {
593     if (0 != pfd->mBounds.BSize(psd->mWritingMode)) {
594       return false;
595     }
596     pfd = pfd->mNext;
597   }
598   return true;
599 }
600 
NewPerFrameData(nsIFrame * aFrame)601 nsLineLayout::PerFrameData* nsLineLayout::NewPerFrameData(nsIFrame* aFrame) {
602   nsLineLayout* outerLineLayout = GetOutermostLineLayout();
603   PerFrameData* pfd = outerLineLayout->mFrameFreeList;
604   if (!pfd) {
605     void* mem = outerLineLayout->mArena.Allocate(sizeof(PerFrameData));
606     pfd = reinterpret_cast<PerFrameData*>(mem);
607   } else {
608     outerLineLayout->mFrameFreeList = pfd->mNext;
609   }
610   pfd->mSpan = nullptr;
611   pfd->mNext = nullptr;
612   pfd->mPrev = nullptr;
613   pfd->mNextAnnotation = nullptr;
614   pfd->mFrame = aFrame;
615 
616   // all flags default to false
617   pfd->mRelativePos = false;
618   pfd->mIsTextFrame = false;
619   pfd->mIsNonEmptyTextFrame = false;
620   pfd->mIsNonWhitespaceTextFrame = false;
621   pfd->mIsLetterFrame = false;
622   pfd->mRecomputeOverflow = false;
623   pfd->mIsBullet = false;
624   pfd->mSkipWhenTrimmingWhitespace = false;
625   pfd->mIsEmpty = false;
626   pfd->mIsPlaceholder = false;
627   pfd->mIsLinkedToBase = false;
628 
629   pfd->mWritingMode = aFrame->GetWritingMode();
630   WritingMode lineWM = mRootSpan->mWritingMode;
631   pfd->mBounds = LogicalRect(lineWM);
632   pfd->mOverflowAreas.Clear();
633   pfd->mMargin = LogicalMargin(lineWM);
634   pfd->mBorderPadding = LogicalMargin(lineWM);
635   pfd->mOffsets = LogicalMargin(pfd->mWritingMode);
636 
637   pfd->mJustificationInfo = JustificationInfo();
638   pfd->mJustificationAssignment = JustificationAssignment();
639 
640 #ifdef DEBUG
641   pfd->mBlockDirAlign = 0xFF;
642   outerLineLayout->mFramesAllocated++;
643 #endif
644   return pfd;
645 }
646 
LineIsBreakable() const647 bool nsLineLayout::LineIsBreakable() const {
648   // XXX mTotalPlacedFrames should go away and we should just use
649   // mLineIsEmpty here instead
650   if ((0 != mTotalPlacedFrames) || mImpactedByFloats) {
651     return true;
652   }
653   return false;
654 }
655 
656 // Checks all four sides for percentage units.  This means it should
657 // only be used for things (margin, padding) where percentages on top
658 // and bottom depend on the *width* just like percentages on left and
659 // right.
HasPercentageUnitSide(const nsStyleSides & aSides)660 static bool HasPercentageUnitSide(const nsStyleSides& aSides) {
661   NS_FOR_CSS_SIDES(side) {
662     if (aSides.Get(side).HasPercent()) return true;
663   }
664   return false;
665 }
666 
IsPercentageAware(const nsIFrame * aFrame)667 static bool IsPercentageAware(const nsIFrame* aFrame) {
668   NS_ASSERTION(aFrame, "null frame is not allowed");
669 
670   LayoutFrameType fType = aFrame->Type();
671   if (fType == LayoutFrameType::Text) {
672     // None of these things can ever be true for text frames.
673     return false;
674   }
675 
676   // Some of these things don't apply to non-replaced inline frames
677   // (that is, fType == LayoutFrameType::Inline), but we won't bother making
678   // things unnecessarily complicated, since they'll probably be set
679   // quite rarely.
680 
681   const nsStyleMargin* margin = aFrame->StyleMargin();
682   if (HasPercentageUnitSide(margin->mMargin)) {
683     return true;
684   }
685 
686   const nsStylePadding* padding = aFrame->StylePadding();
687   if (HasPercentageUnitSide(padding->mPadding)) {
688     return true;
689   }
690 
691   // Note that borders can't be aware of percentages
692 
693   const nsStylePosition* pos = aFrame->StylePosition();
694 
695   if ((pos->WidthDependsOnContainer() &&
696        pos->mWidth.GetUnit() != eStyleUnit_Auto) ||
697       pos->MaxWidthDependsOnContainer() || pos->MinWidthDependsOnContainer() ||
698       pos->OffsetHasPercent(eSideRight) || pos->OffsetHasPercent(eSideLeft)) {
699     return true;
700   }
701 
702   if (eStyleUnit_Auto == pos->mWidth.GetUnit()) {
703     // We need to check for frames that shrink-wrap when they're auto
704     // width.
705     const nsStyleDisplay* disp = aFrame->StyleDisplay();
706     if (disp->mDisplay == StyleDisplay::InlineBlock ||
707         disp->mDisplay == StyleDisplay::InlineTable ||
708         fType == LayoutFrameType::HTMLButtonControl ||
709         fType == LayoutFrameType::GfxButtonControl ||
710         fType == LayoutFrameType::FieldSet ||
711         fType == LayoutFrameType::ComboboxDisplay) {
712       return true;
713     }
714 
715     // Per CSS 2.1, section 10.3.2:
716     //   If 'height' and 'width' both have computed values of 'auto' and
717     //   the element has an intrinsic ratio but no intrinsic height or
718     //   width and the containing block's width does not itself depend
719     //   on the replaced element's width, then the used value of 'width'
720     //   is calculated from the constraint equation used for
721     //   block-level, non-replaced elements in normal flow.
722     nsIFrame* f = const_cast<nsIFrame*>(aFrame);
723     if (f->GetIntrinsicRatio() != nsSize(0, 0) &&
724         // Some percents are treated like 'auto', so check != coord
725         pos->mHeight.GetUnit() != eStyleUnit_Coord) {
726       const IntrinsicSize& intrinsicSize = f->GetIntrinsicSize();
727       if (intrinsicSize.width.GetUnit() == eStyleUnit_None &&
728           intrinsicSize.height.GetUnit() == eStyleUnit_None) {
729         return true;
730       }
731     }
732   }
733 
734   return false;
735 }
736 
ReflowFrame(nsIFrame * aFrame,nsReflowStatus & aReflowStatus,ReflowOutput * aMetrics,bool & aPushedFrame)737 void nsLineLayout::ReflowFrame(nsIFrame* aFrame, nsReflowStatus& aReflowStatus,
738                                ReflowOutput* aMetrics, bool& aPushedFrame) {
739   // Initialize OUT parameter
740   aPushedFrame = false;
741 
742   PerFrameData* pfd = NewPerFrameData(aFrame);
743   PerSpanData* psd = mCurrentSpan;
744   psd->AppendFrame(pfd);
745 
746 #ifdef REALLY_NOISY_REFLOW
747   nsFrame::IndentBy(stdout, mSpanDepth);
748   printf("%p: Begin ReflowFrame pfd=%p ", psd, pfd);
749   nsFrame::ListTag(stdout, aFrame);
750   printf("\n");
751 #endif
752 
753   if (mCurrentSpan == mRootSpan) {
754     pfd->mFrame->RemoveProperty(nsIFrame::LineBaselineOffset());
755   } else {
756 #ifdef DEBUG
757     bool hasLineOffset;
758     pfd->mFrame->GetProperty(nsIFrame::LineBaselineOffset(), &hasLineOffset);
759     NS_ASSERTION(!hasLineOffset,
760                  "LineBaselineOffset was set but was not expected");
761 #endif
762   }
763 
764   mJustificationInfo = JustificationInfo();
765 
766   // Stash copies of some of the computed state away for later
767   // (block-direction alignment, for example)
768   WritingMode frameWM = pfd->mWritingMode;
769   WritingMode lineWM = mRootSpan->mWritingMode;
770 
771   // NOTE: While the inline direction coordinate remains relative to the
772   // parent span, the block direction coordinate is fixed at the top
773   // edge for the line. During VerticalAlignFrames we will repair this
774   // so that the block direction coordinate is properly set and relative
775   // to the appropriate span.
776   pfd->mBounds.IStart(lineWM) = psd->mICoord;
777   pfd->mBounds.BStart(lineWM) = mBStartEdge;
778 
779   // We want to guarantee that we always make progress when
780   // formatting. Therefore, if the object being placed on the line is
781   // too big for the line, but it is the only thing on the line and is not
782   // impacted by a float, then we go ahead and place it anyway. (If the line
783   // is impacted by one or more floats, then it is safe to break because
784   // we can move the line down below float(s).)
785   //
786   // Capture this state *before* we reflow the frame in case it clears
787   // the state out. We need to know how to treat the current frame
788   // when breaking.
789   bool notSafeToBreak = LineIsEmpty() && !mImpactedByFloats;
790 
791   // Figure out whether we're talking about a textframe here
792   LayoutFrameType frameType = aFrame->Type();
793   const bool isText = frameType == LayoutFrameType::Text;
794 
795   // Inline-ish and text-ish things don't compute their width;
796   // everything else does.  We need to give them an available width that
797   // reflects the space left on the line.
798   LAYOUT_WARN_IF_FALSE(psd->mIEnd != NS_UNCONSTRAINEDSIZE,
799                        "have unconstrained width; this should only result from "
800                        "very large sizes, not attempts at intrinsic width "
801                        "calculation");
802   nscoord availableSpaceOnLine = psd->mIEnd - psd->mICoord;
803 
804   // Setup reflow state for reflowing the frame
805   Maybe<ReflowInput> reflowInputHolder;
806   if (!isText) {
807     // Compute the available size for the frame. This available width
808     // includes room for the side margins.
809     // For now, set the available block-size to unconstrained always.
810     LogicalSize availSize = mBlockReflowInput->ComputedSize(frameWM);
811     availSize.BSize(frameWM) = NS_UNCONSTRAINEDSIZE;
812     reflowInputHolder.emplace(mPresContext, *psd->mReflowInput, aFrame,
813                               availSize);
814     ReflowInput& reflowInput = *reflowInputHolder;
815     reflowInput.mLineLayout = this;
816     reflowInput.mFlags.mIsTopOfPage = mIsTopOfPage;
817     if (reflowInput.ComputedISize() == NS_UNCONSTRAINEDSIZE) {
818       reflowInput.AvailableISize() = availableSpaceOnLine;
819     }
820     WritingMode stateWM = reflowInput.GetWritingMode();
821     pfd->mMargin =
822         reflowInput.ComputedLogicalMargin().ConvertTo(lineWM, stateWM);
823     pfd->mBorderPadding =
824         reflowInput.ComputedLogicalBorderPadding().ConvertTo(lineWM, stateWM);
825     pfd->mRelativePos =
826         reflowInput.mStyleDisplay->IsRelativelyPositionedStyle();
827     if (pfd->mRelativePos) {
828       pfd->mOffsets =
829           reflowInput.ComputedLogicalOffsets().ConvertTo(frameWM, stateWM);
830     }
831 
832     // Calculate whether the the frame should have a start margin and
833     // subtract the margin from the available width if necessary.
834     // The margin will be applied to the starting inline coordinates of
835     // the frame in CanPlaceFrame() after reflowing the frame.
836     AllowForStartMargin(pfd, reflowInput);
837   }
838   // if isText(), no need to propagate NS_FRAME_IS_DIRTY from the parent,
839   // because reflow doesn't look at the dirty bits on the frame being reflowed.
840 
841   // See if this frame depends on the width of its containing block.  If
842   // so, disable resize reflow optimizations for the line.  (Note that,
843   // to be conservative, we do this if we *try* to fit a frame on a
844   // line, even if we don't succeed.)  (Note also that we can only make
845   // this IsPercentageAware check *after* we've constructed our
846   // ReflowInput, because that construction may be what forces aFrame
847   // to lazily initialize its (possibly-percent-valued) intrinsic size.)
848   if (mGotLineBox && IsPercentageAware(aFrame)) {
849     mLineBox->DisableResizeReflowOptimization();
850   }
851 
852   // Note that we don't bother positioning the frame yet, because we're probably
853   // going to end up moving it when we do the block-direction alignment.
854 
855   // Adjust spacemanager coordinate system for the frame.
856   ReflowOutput reflowOutput(lineWM);
857 #ifdef DEBUG
858   reflowOutput.ISize(lineWM) = nscoord(0xdeadbeef);
859   reflowOutput.BSize(lineWM) = nscoord(0xdeadbeef);
860 #endif
861   nscoord tI = pfd->mBounds.LineLeft(lineWM, ContainerSize());
862   nscoord tB = pfd->mBounds.BStart(lineWM);
863   mFloatManager->Translate(tI, tB);
864 
865   int32_t savedOptionalBreakOffset;
866   gfxBreakPriority savedOptionalBreakPriority;
867   nsIFrame* savedOptionalBreakFrame = GetLastOptionalBreakPosition(
868       &savedOptionalBreakOffset, &savedOptionalBreakPriority);
869 
870   if (!isText) {
871     aFrame->Reflow(mPresContext, reflowOutput, *reflowInputHolder,
872                    aReflowStatus);
873   } else {
874     static_cast<nsTextFrame*>(aFrame)->ReflowText(
875         *this, availableSpaceOnLine,
876         psd->mReflowInput->mRenderingContext->GetDrawTarget(), reflowOutput,
877         aReflowStatus);
878   }
879 
880   pfd->mJustificationInfo = mJustificationInfo;
881   mJustificationInfo = JustificationInfo();
882 
883   // See if the frame is a placeholderFrame and if it is process
884   // the float. At the same time, check if the frame has any non-collapsed-away
885   // content.
886   bool placedFloat = false;
887   bool isEmpty;
888   if (frameType == LayoutFrameType::None) {
889     isEmpty = pfd->mFrame->IsEmpty();
890   } else {
891     if (LayoutFrameType::Placeholder == frameType) {
892       isEmpty = true;
893       pfd->mIsPlaceholder = true;
894       pfd->mSkipWhenTrimmingWhitespace = true;
895       nsIFrame* outOfFlowFrame = nsLayoutUtils::GetFloatFromPlaceholder(aFrame);
896       if (outOfFlowFrame) {
897         // Add mTrimmableISize to the available width since if the line ends
898         // here, the width of the inline content will be reduced by
899         // mTrimmableISize.
900         nscoord availableISize = psd->mIEnd - (psd->mICoord - mTrimmableISize);
901         if (psd->mNoWrap) {
902           // If we place floats after inline content where there's
903           // no break opportunity, we don't know how much additional
904           // width is required for the non-breaking content after the float,
905           // so we can't know whether the float plus that content will fit
906           // on the line. So for now, don't place floats after inline
907           // content where there's no break opportunity. This is incorrect
908           // but hopefully rare. Fixing it will require significant
909           // restructuring of line layout.
910           // We might as well allow zero-width floats to be placed, though.
911           availableISize = 0;
912         }
913         placedFloat =
914             GetOutermostLineLayout()->AddFloat(outOfFlowFrame, availableISize);
915         NS_ASSERTION(
916             !(outOfFlowFrame->IsLetterFrame() && GetFirstLetterStyleOK()),
917             "FirstLetterStyle set on line with floating first letter");
918       }
919     } else if (isText) {
920       // Note non-empty text-frames for inline frame compatibility hackery
921       pfd->mIsTextFrame = true;
922       nsTextFrame* textFrame = static_cast<nsTextFrame*>(pfd->mFrame);
923       isEmpty = !textFrame->HasNoncollapsedCharacters();
924       if (!isEmpty) {
925         pfd->mIsNonEmptyTextFrame = true;
926         nsIContent* content = textFrame->GetContent();
927 
928         const nsTextFragment* frag = content->GetText();
929         if (frag) {
930           pfd->mIsNonWhitespaceTextFrame = !content->TextIsOnlyWhitespace();
931         }
932       }
933     } else if (LayoutFrameType::Br == frameType) {
934       pfd->mSkipWhenTrimmingWhitespace = true;
935       isEmpty = false;
936     } else {
937       if (LayoutFrameType::Letter == frameType) {
938         pfd->mIsLetterFrame = true;
939       }
940       if (pfd->mSpan) {
941         isEmpty =
942             !pfd->mSpan->mHasNonemptyContent && pfd->mFrame->IsSelfEmpty();
943       } else {
944         isEmpty = pfd->mFrame->IsEmpty();
945       }
946     }
947   }
948   pfd->mIsEmpty = isEmpty;
949 
950   mFloatManager->Translate(-tI, -tB);
951 
952   NS_ASSERTION(reflowOutput.ISize(lineWM) >= 0, "bad inline size");
953   NS_ASSERTION(reflowOutput.BSize(lineWM) >= 0, "bad block size");
954   if (reflowOutput.ISize(lineWM) < 0) {
955     reflowOutput.ISize(lineWM) = 0;
956   }
957   if (reflowOutput.BSize(lineWM) < 0) {
958     reflowOutput.BSize(lineWM) = 0;
959   }
960 
961 #ifdef DEBUG
962   // Note: break-before means ignore the reflow metrics since the
963   // frame will be reflowed another time.
964   if (!aReflowStatus.IsInlineBreakBefore()) {
965     if ((CRAZY_SIZE(reflowOutput.ISize(lineWM)) ||
966          CRAZY_SIZE(reflowOutput.BSize(lineWM))) &&
967         !LineContainerFrame()->GetParent()->IsCrazySizeAssertSuppressed()) {
968       printf("nsLineLayout: ");
969       nsFrame::ListTag(stdout, aFrame);
970       printf(" metrics=%d,%d!\n", reflowOutput.Width(), reflowOutput.Height());
971     }
972     if ((reflowOutput.Width() == nscoord(0xdeadbeef)) ||
973         (reflowOutput.Height() == nscoord(0xdeadbeef))) {
974       printf("nsLineLayout: ");
975       nsFrame::ListTag(stdout, aFrame);
976       printf(" didn't set w/h %d,%d!\n", reflowOutput.Width(),
977              reflowOutput.Height());
978     }
979   }
980 #endif
981 
982   // Unlike with non-inline reflow, the overflow area here does *not*
983   // include the accumulation of the frame's bounds and its inline
984   // descendants' bounds. Nor does it include the outline area; it's
985   // just the union of the bounds of any absolute children. That is
986   // added in later by nsLineLayout::ReflowInlineFrames.
987   pfd->mOverflowAreas = reflowOutput.mOverflowAreas;
988 
989   pfd->mBounds.ISize(lineWM) = reflowOutput.ISize(lineWM);
990   pfd->mBounds.BSize(lineWM) = reflowOutput.BSize(lineWM);
991 
992   // Size the frame, but |RelativePositionFrames| will size the view.
993   aFrame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(psd));
994 
995   // Tell the frame that we're done reflowing it
996   aFrame->DidReflow(mPresContext, isText ? nullptr : reflowInputHolder.ptr());
997 
998   if (aMetrics) {
999     *aMetrics = reflowOutput;
1000   }
1001 
1002   if (!aReflowStatus.IsInlineBreakBefore()) {
1003     // If frame is complete and has a next-in-flow, we need to delete
1004     // them now. Do not do this when a break-before is signaled because
1005     // the frame is going to get reflowed again (and may end up wanting
1006     // a next-in-flow where it ends up).
1007     if (aReflowStatus.IsComplete()) {
1008       nsIFrame* kidNextInFlow = aFrame->GetNextInFlow();
1009       if (nullptr != kidNextInFlow) {
1010         // Remove all of the childs next-in-flows. Make sure that we ask
1011         // the right parent to do the removal (it's possible that the
1012         // parent is not this because we are executing pullup code)
1013         kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
1014       }
1015     }
1016 
1017     // Check whether this frame breaks up text runs. All frames break up text
1018     // runs (hence return false here) except for text frames and inline
1019     // containers.
1020     bool continuingTextRun = aFrame->CanContinueTextRun();
1021 
1022     // Clear any residual mTrimmableISize if this isn't a text frame
1023     if (!continuingTextRun && !pfd->mSkipWhenTrimmingWhitespace) {
1024       mTrimmableISize = 0;
1025     }
1026 
1027     // See if we can place the frame. If we can't fit it, then we
1028     // return now.
1029     bool optionalBreakAfterFits;
1030     NS_ASSERTION(isText || !reflowInputHolder->IsFloating(),
1031                  "How'd we get a floated inline frame? "
1032                  "The frame ctor should've dealt with this.");
1033     if (CanPlaceFrame(pfd, notSafeToBreak, continuingTextRun,
1034                       savedOptionalBreakFrame != nullptr, reflowOutput,
1035                       aReflowStatus, &optionalBreakAfterFits)) {
1036       if (!isEmpty) {
1037         psd->mHasNonemptyContent = true;
1038         mLineIsEmpty = false;
1039         if (!pfd->mSpan) {
1040           // nonempty leaf content has been placed
1041           mLineAtStart = false;
1042         }
1043         if (LayoutFrameType::Ruby == frameType) {
1044           mHasRuby = true;
1045           SyncAnnotationBounds(pfd);
1046         }
1047       }
1048 
1049       // Place the frame, updating aBounds with the final size and
1050       // location.  Then apply the bottom+right margins (as
1051       // appropriate) to the frame.
1052       PlaceFrame(pfd, reflowOutput);
1053       PerSpanData* span = pfd->mSpan;
1054       if (span) {
1055         // The frame we just finished reflowing is an inline
1056         // container.  It needs its child frames aligned in the block direction,
1057         // so do most of it now.
1058         VerticalAlignFrames(span);
1059       }
1060 
1061       if (!continuingTextRun) {
1062         if (!psd->mNoWrap && (!LineIsEmpty() || placedFloat)) {
1063           // record soft break opportunity after this content that can't be
1064           // part of a text run. This is not a text frame so we know
1065           // that offset INT32_MAX means "after the content".
1066           if (NotifyOptionalBreakPosition(aFrame, INT32_MAX,
1067                                           optionalBreakAfterFits,
1068                                           gfxBreakPriority::eNormalBreak)) {
1069             // If this returns true then we are being told to actually break
1070             // here.
1071             aReflowStatus.SetInlineLineBreakAfter();
1072           }
1073         }
1074       }
1075     } else {
1076       PushFrame(aFrame);
1077       aPushedFrame = true;
1078       // Undo any saved break positions that the frame might have told us about,
1079       // since we didn't end up placing it
1080       RestoreSavedBreakPosition(savedOptionalBreakFrame,
1081                                 savedOptionalBreakOffset,
1082                                 savedOptionalBreakPriority);
1083     }
1084   } else {
1085     PushFrame(aFrame);
1086     aPushedFrame = true;
1087   }
1088 
1089 #ifdef REALLY_NOISY_REFLOW
1090   nsFrame::IndentBy(stdout, mSpanDepth);
1091   printf("End ReflowFrame ");
1092   nsFrame::ListTag(stdout, aFrame);
1093   printf(" status=%x\n", aReflowStatus);
1094 #endif
1095 }
1096 
AllowForStartMargin(PerFrameData * pfd,ReflowInput & aReflowInput)1097 void nsLineLayout::AllowForStartMargin(PerFrameData* pfd,
1098                                        ReflowInput& aReflowInput) {
1099   NS_ASSERTION(!aReflowInput.IsFloating(),
1100                "How'd we get a floated inline frame? "
1101                "The frame ctor should've dealt with this.");
1102 
1103   WritingMode lineWM = mRootSpan->mWritingMode;
1104 
1105   // Only apply start-margin on the first-in flow for inline frames,
1106   // and make sure to not apply it to any inline other than the first
1107   // in an ib split.  Note that the ib sibling (block-in-inline
1108   // sibling) annotations only live on the first continuation, but we
1109   // don't want to apply the start margin for later continuations
1110   // anyway.  For box-decoration-break:clone we apply the start-margin
1111   // on all continuations.
1112   if ((pfd->mFrame->GetPrevContinuation() ||
1113        pfd->mFrame->FrameIsNonFirstInIBSplit()) &&
1114       aReflowInput.mStyleBorder->mBoxDecorationBreak ==
1115           StyleBoxDecorationBreak::Slice) {
1116     // Zero this out so that when we compute the max-element-width of
1117     // the frame we will properly avoid adding in the starting margin.
1118     pfd->mMargin.IStart(lineWM) = 0;
1119   } else if (NS_UNCONSTRAINEDSIZE == aReflowInput.ComputedISize()) {
1120     NS_WARNING_ASSERTION(
1121         NS_UNCONSTRAINEDSIZE != aReflowInput.AvailableISize(),
1122         "have unconstrained inline-size; this should only result from very "
1123         "large sizes, not attempts at intrinsic inline-size calculation");
1124     // For inline-ish and text-ish things (which don't compute widths
1125     // in the reflow state), adjust available inline-size to account
1126     // for the start margin. The end margin will be accounted for when
1127     // we finish flowing the frame.
1128     WritingMode wm = aReflowInput.GetWritingMode();
1129     aReflowInput.AvailableISize() -=
1130         pfd->mMargin.ConvertTo(wm, lineWM).IStart(wm);
1131   }
1132 }
1133 
GetCurrentFrameInlineDistanceFromBlock()1134 nscoord nsLineLayout::GetCurrentFrameInlineDistanceFromBlock() {
1135   PerSpanData* psd;
1136   nscoord x = 0;
1137   for (psd = mCurrentSpan; psd; psd = psd->mParent) {
1138     x += psd->mICoord;
1139   }
1140   return x;
1141 }
1142 
1143 /**
1144  * This method syncs bounds of ruby annotations and ruby annotation
1145  * containers from their rect. It is necessary because:
1146  * Containers are not part of the line in their levels, which means
1147  * their bounds are not set properly before.
1148  * Ruby annotations' position may have been changed when reflowing
1149  * their containers.
1150  */
SyncAnnotationBounds(PerFrameData * aRubyFrame)1151 void nsLineLayout::SyncAnnotationBounds(PerFrameData* aRubyFrame) {
1152   MOZ_ASSERT(aRubyFrame->mFrame->IsRubyFrame());
1153   MOZ_ASSERT(aRubyFrame->mSpan);
1154 
1155   PerSpanData* span = aRubyFrame->mSpan;
1156   WritingMode lineWM = mRootSpan->mWritingMode;
1157   for (PerFrameData* pfd = span->mFirstFrame; pfd; pfd = pfd->mNext) {
1158     for (PerFrameData* rtc = pfd->mNextAnnotation; rtc;
1159          rtc = rtc->mNextAnnotation) {
1160       if (lineWM.IsOrthogonalTo(rtc->mFrame->GetWritingMode())) {
1161         // Inter-character case: don't attempt to sync annotation bounds.
1162         continue;
1163       }
1164       // When the annotation container is reflowed, the width of the
1165       // ruby container is unknown so we use a dummy container size;
1166       // in the case of RTL block direction, the final position will be
1167       // fixed up later.
1168       const nsSize dummyContainerSize;
1169       LogicalRect rtcBounds(lineWM, rtc->mFrame->GetRect(), dummyContainerSize);
1170       rtc->mBounds = rtcBounds;
1171       nsSize rtcSize = rtcBounds.Size(lineWM).GetPhysicalSize(lineWM);
1172       for (PerFrameData* rt = rtc->mSpan->mFirstFrame; rt; rt = rt->mNext) {
1173         LogicalRect rtBounds = rt->mFrame->GetLogicalRect(lineWM, rtcSize);
1174         MOZ_ASSERT(rt->mBounds.Size(lineWM) == rtBounds.Size(lineWM),
1175                    "Size of the annotation should not have been changed");
1176         rt->mBounds.SetOrigin(lineWM, rtBounds.Origin(lineWM));
1177       }
1178     }
1179   }
1180 }
1181 
1182 /**
1183  * See if the frame can be placed now that we know it's desired size.
1184  * We can always place the frame if the line is empty. Note that we
1185  * know that the reflow-status is not a break-before because if it was
1186  * ReflowFrame above would have returned false, preventing this method
1187  * from being called. The logic in this method assumes that.
1188  *
1189  * Note that there is no check against the Y coordinate because we
1190  * assume that the caller will take care of that.
1191  */
CanPlaceFrame(PerFrameData * pfd,bool aNotSafeToBreak,bool aFrameCanContinueTextRun,bool aCanRollBackBeforeFrame,ReflowOutput & aMetrics,nsReflowStatus & aStatus,bool * aOptionalBreakAfterFits)1192 bool nsLineLayout::CanPlaceFrame(PerFrameData* pfd, bool aNotSafeToBreak,
1193                                  bool aFrameCanContinueTextRun,
1194                                  bool aCanRollBackBeforeFrame,
1195                                  ReflowOutput& aMetrics,
1196                                  nsReflowStatus& aStatus,
1197                                  bool* aOptionalBreakAfterFits) {
1198   NS_PRECONDITION(pfd && pfd->mFrame, "bad args, null pointers for frame data");
1199 
1200   *aOptionalBreakAfterFits = true;
1201 
1202   WritingMode lineWM = mRootSpan->mWritingMode;
1203   /*
1204    * We want to only apply the end margin if we're the last continuation and
1205    * either not in an {ib} split or the last inline in it.  In all other
1206    * cases we want to zero it out.  That means zeroing it out if any of these
1207    * conditions hold:
1208    * 1) The frame is not complete (in this case it will get a next-in-flow)
1209    * 2) The frame is complete but has a non-fluid continuation on its
1210    *    continuation chain.  Note that if it has a fluid continuation, that
1211    *    continuation will get destroyed later, so we don't want to drop the
1212    *    end-margin in that case.
1213    * 3) The frame is in an {ib} split and is not the last part.
1214    *
1215    * However, none of that applies if this is a letter frame (XXXbz why?)
1216    *
1217    * For box-decoration-break:clone we apply the end margin on all
1218    * continuations (that are not letter frames).
1219    */
1220   if ((aStatus.IsIncomplete() ||
1221        pfd->mFrame->LastInFlow()->GetNextContinuation() ||
1222        pfd->mFrame->FrameIsNonLastInIBSplit()) &&
1223       !pfd->mIsLetterFrame &&
1224       pfd->mFrame->StyleBorder()->mBoxDecorationBreak ==
1225           StyleBoxDecorationBreak::Slice) {
1226     pfd->mMargin.IEnd(lineWM) = 0;
1227   }
1228 
1229   // Apply the start margin to the frame bounds.
1230   nscoord startMargin = pfd->mMargin.IStart(lineWM);
1231   nscoord endMargin = pfd->mMargin.IEnd(lineWM);
1232 
1233   pfd->mBounds.IStart(lineWM) += startMargin;
1234 
1235   PerSpanData* psd = mCurrentSpan;
1236   if (psd->mNoWrap) {
1237     // When wrapping is off, everything fits.
1238     return true;
1239   }
1240 
1241 #ifdef NOISY_CAN_PLACE_FRAME
1242   if (nullptr != psd->mFrame) {
1243     nsFrame::ListTag(stdout, psd->mFrame->mFrame);
1244   }
1245   printf(": aNotSafeToBreak=%s frame=", aNotSafeToBreak ? "true" : "false");
1246   nsFrame::ListTag(stdout, pfd->mFrame);
1247   printf(" frameWidth=%d, margins=%d,%d\n",
1248          pfd->mBounds.IEnd(lineWM) + endMargin - psd->mICoord, startMargin,
1249          endMargin);
1250 #endif
1251 
1252   // Set outside to true if the result of the reflow leads to the
1253   // frame sticking outside of our available area.
1254   bool outside =
1255       pfd->mBounds.IEnd(lineWM) - mTrimmableISize + endMargin > psd->mIEnd;
1256   if (!outside) {
1257   // If it fits, it fits
1258 #ifdef NOISY_CAN_PLACE_FRAME
1259     printf("   ==> inside\n");
1260 #endif
1261     return true;
1262   }
1263   *aOptionalBreakAfterFits = false;
1264 
1265   // When it doesn't fit, check for a few special conditions where we
1266   // allow it to fit anyway.
1267   if (0 == startMargin + pfd->mBounds.ISize(lineWM) + endMargin) {
1268   // Empty frames always fit right where they are
1269 #ifdef NOISY_CAN_PLACE_FRAME
1270     printf("   ==> empty frame fits\n");
1271 #endif
1272     return true;
1273   }
1274 
1275 #ifdef FIX_BUG_50257
1276   // another special case:  always place a BR
1277   if (pfd->mFrame->IsBrFrame()) {
1278 #ifdef NOISY_CAN_PLACE_FRAME
1279     printf("   ==> BR frame fits\n");
1280 #endif
1281     return true;
1282   }
1283 #endif
1284 
1285   if (aNotSafeToBreak) {
1286   // There are no frames on the line that take up width and the line is
1287   // not impacted by floats, so we must allow the current frame to be
1288   // placed on the line
1289 #ifdef NOISY_CAN_PLACE_FRAME
1290     printf("   ==> not-safe and not-impacted fits: ");
1291     while (nullptr != psd) {
1292       printf("<psd=%p x=%d left=%d> ", psd, psd->mICoord, psd->mIStart);
1293       psd = psd->mParent;
1294     }
1295     printf("\n");
1296 #endif
1297     return true;
1298   }
1299 
1300   // Special check for span frames
1301   if (pfd->mSpan && pfd->mSpan->mContainsFloat) {
1302     // If the span either directly or indirectly contains a float then
1303     // it fits. Why? It's kind of complicated, but here goes:
1304     //
1305     // 1. CanPlaceFrame is used for all frame placements on a line,
1306     // and in a span. This includes recursively placement of frames
1307     // inside of spans, and the span itself. Because the logic always
1308     // checks for room before proceeding (the code above here), the
1309     // only things on a line will be those things that "fit".
1310     //
1311     // 2. Before a float is placed on a line, the line has to be empty
1312     // (otherwise it's a "below current line" float and will be placed
1313     // after the line).
1314     //
1315     // Therefore, if the span directly or indirectly has a float
1316     // then it means that at the time of the placement of the float
1317     // the line was empty. Because of #1, only the frames that fit can
1318     // be added after that point, therefore we can assume that the
1319     // current span being placed has fit.
1320     //
1321     // So how do we get here and have a span that should already fit
1322     // and yet doesn't: Simple: span's that have the no-wrap attribute
1323     // set on them and contain a float and are placed where they
1324     // don't naturally fit.
1325     return true;
1326   }
1327 
1328   if (aFrameCanContinueTextRun) {
1329   // Let it fit, but we reserve the right to roll back.
1330   // Note that we usually won't get here because a text frame will break
1331   // itself to avoid exceeding the available width.
1332   // We'll only get here for text frames that couldn't break early enough.
1333 #ifdef NOISY_CAN_PLACE_FRAME
1334     printf("   ==> placing overflowing textrun, requesting backup\n");
1335 #endif
1336 
1337     // We will want to try backup.
1338     mNeedBackup = true;
1339     return true;
1340   }
1341 
1342 #ifdef NOISY_CAN_PLACE_FRAME
1343   printf("   ==> didn't fit\n");
1344 #endif
1345   aStatus.SetInlineLineBreakBeforeAndReset();
1346   return false;
1347 }
1348 
1349 /**
1350  * Place the frame. Update running counters.
1351  */
PlaceFrame(PerFrameData * pfd,ReflowOutput & aMetrics)1352 void nsLineLayout::PlaceFrame(PerFrameData* pfd, ReflowOutput& aMetrics) {
1353   WritingMode lineWM = mRootSpan->mWritingMode;
1354 
1355   // If the frame's block direction does not match the line's, we can't use
1356   // its ascent; instead, treat it as a block with baseline at the block-end
1357   // edge (or block-begin in the case of an "inverted" line).
1358   if (pfd->mWritingMode.GetBlockDir() != lineWM.GetBlockDir()) {
1359     pfd->mAscent = lineWM.IsLineInverted() ? 0 : aMetrics.BSize(lineWM);
1360   } else {
1361     if (aMetrics.BlockStartAscent() == ReflowOutput::ASK_FOR_BASELINE) {
1362       pfd->mAscent = pfd->mFrame->GetLogicalBaseline(lineWM);
1363     } else {
1364       pfd->mAscent = aMetrics.BlockStartAscent();
1365     }
1366   }
1367 
1368   // Advance to next inline coordinate
1369   mCurrentSpan->mICoord = pfd->mBounds.IEnd(lineWM) + pfd->mMargin.IEnd(lineWM);
1370 
1371   // Count the number of non-placeholder frames on the line...
1372   if (pfd->mFrame->IsPlaceholderFrame()) {
1373     NS_ASSERTION(
1374         pfd->mBounds.ISize(lineWM) == 0 && pfd->mBounds.BSize(lineWM) == 0,
1375         "placeholders should have 0 width/height (checking "
1376         "placeholders were never counted by the old code in "
1377         "this function)");
1378   } else {
1379     mTotalPlacedFrames++;
1380   }
1381 }
1382 
AddBulletFrame(nsIFrame * aFrame,const ReflowOutput & aMetrics)1383 void nsLineLayout::AddBulletFrame(nsIFrame* aFrame,
1384                                   const ReflowOutput& aMetrics) {
1385   NS_ASSERTION(mCurrentSpan == mRootSpan, "bad linelayout user");
1386   NS_ASSERTION(mGotLineBox, "must have line box");
1387 
1388   nsIFrame* blockFrame = mBlockReflowInput->mFrame;
1389   NS_ASSERTION(blockFrame->IsFrameOfType(nsIFrame::eBlockFrame),
1390                "must be for block");
1391   if (!static_cast<nsBlockFrame*>(blockFrame)->BulletIsEmpty()) {
1392     mHasBullet = true;
1393     mLineBox->SetHasBullet();
1394   }
1395 
1396   WritingMode lineWM = mRootSpan->mWritingMode;
1397   PerFrameData* pfd = NewPerFrameData(aFrame);
1398   mRootSpan->AppendFrame(pfd);
1399   pfd->mIsBullet = true;
1400   if (aMetrics.BlockStartAscent() == ReflowOutput::ASK_FOR_BASELINE) {
1401     pfd->mAscent = aFrame->GetLogicalBaseline(lineWM);
1402   } else {
1403     pfd->mAscent = aMetrics.BlockStartAscent();
1404   }
1405 
1406   // Note: block-coord value will be updated during block-direction alignment
1407   pfd->mBounds = LogicalRect(lineWM, aFrame->GetRect(), ContainerSize());
1408   pfd->mOverflowAreas = aMetrics.mOverflowAreas;
1409 }
1410 
1411 #ifdef DEBUG
DumpPerSpanData(PerSpanData * psd,int32_t aIndent)1412 void nsLineLayout::DumpPerSpanData(PerSpanData* psd, int32_t aIndent) {
1413   nsFrame::IndentBy(stdout, aIndent);
1414   printf("%p: left=%d x=%d right=%d\n", static_cast<void*>(psd), psd->mIStart,
1415          psd->mICoord, psd->mIEnd);
1416   PerFrameData* pfd = psd->mFirstFrame;
1417   while (nullptr != pfd) {
1418     nsFrame::IndentBy(stdout, aIndent + 1);
1419     nsFrame::ListTag(stdout, pfd->mFrame);
1420     nsRect rect =
1421         pfd->mBounds.GetPhysicalRect(psd->mWritingMode, ContainerSize());
1422     printf(" %d,%d,%d,%d\n", rect.x, rect.y, rect.width, rect.height);
1423     if (pfd->mSpan) {
1424       DumpPerSpanData(pfd->mSpan, aIndent + 1);
1425     }
1426     pfd = pfd->mNext;
1427   }
1428 }
1429 #endif
1430 
1431 #define VALIGN_OTHER 0
1432 #define VALIGN_TOP 1
1433 #define VALIGN_BOTTOM 2
1434 
VerticalAlignLine()1435 void nsLineLayout::VerticalAlignLine() {
1436   // Partially place the children of the block frame. The baseline for
1437   // this operation is set to zero so that the y coordinates for all
1438   // of the placed children will be relative to there.
1439   PerSpanData* psd = mRootSpan;
1440   VerticalAlignFrames(psd);
1441 
1442   // *** Note that comments here still use the anachronistic term
1443   // "line-height" when we really mean "size of the line in the block
1444   // direction", "vertical-align" when we really mean "alignment in
1445   // the block direction", and "top" and "bottom" when we really mean
1446   // "block start" and "block end". This is partly for brevity and
1447   // partly to retain the association with the CSS line-height and
1448   // vertical-align properties.
1449   //
1450   // Compute the line-height. The line-height will be the larger of:
1451   //
1452   // [1] maxBCoord - minBCoord (the distance between the first child's
1453   // block-start edge and the last child's block-end edge)
1454   //
1455   // [2] the maximum logical box block size (since not every frame may have
1456   // participated in #1; for example: "top" and "botttom" aligned frames)
1457   //
1458   // [3] the minimum line height ("line-height" property set on the
1459   // block frame)
1460   nscoord lineBSize = psd->mMaxBCoord - psd->mMinBCoord;
1461 
1462   // Now that the line-height is computed, we need to know where the
1463   // baseline is in the line. Position baseline so that mMinBCoord is just
1464   // inside the start of the line box.
1465   nscoord baselineBCoord;
1466   if (psd->mMinBCoord < 0) {
1467     baselineBCoord = mBStartEdge - psd->mMinBCoord;
1468   } else {
1469     baselineBCoord = mBStartEdge;
1470   }
1471 
1472   // It's also possible that the line block-size isn't tall enough because
1473   // of "top" and "bottom" aligned elements that were not accounted for in
1474   // min/max BCoord.
1475   //
1476   // The CSS2 spec doesn't really say what happens when to the
1477   // baseline in this situations. What we do is if the largest start
1478   // aligned box block size is greater than the line block-size then we leave
1479   // the baseline alone. If the largest end aligned box is greater
1480   // than the line block-size then we slide the baseline forward by the extra
1481   // amount.
1482   //
1483   // Navigator 4 gives precedence to the first top/bottom aligned
1484   // object.  We just let block end aligned objects win.
1485   if (lineBSize < mMaxEndBoxBSize) {
1486     // When the line is shorter than the maximum block start aligned box
1487     nscoord extra = mMaxEndBoxBSize - lineBSize;
1488     baselineBCoord += extra;
1489     lineBSize = mMaxEndBoxBSize;
1490   }
1491   if (lineBSize < mMaxStartBoxBSize) {
1492     lineBSize = mMaxStartBoxBSize;
1493   }
1494 #ifdef NOISY_BLOCKDIR_ALIGN
1495   printf("  [line]==> lineBSize=%d baselineBCoord=%d\n", lineBSize,
1496          baselineBCoord);
1497 #endif
1498 
1499   // Now position all of the frames in the root span. We will also
1500   // recurse over the child spans and place any frames we find with
1501   // vertical-align: top or bottom.
1502   // XXX PERFORMANCE: set a bit per-span to avoid the extra work
1503   // (propagate it upward too)
1504   WritingMode lineWM = psd->mWritingMode;
1505   for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
1506     if (pfd->mBlockDirAlign == VALIGN_OTHER) {
1507       pfd->mBounds.BStart(lineWM) += baselineBCoord;
1508       pfd->mFrame->SetRect(lineWM, pfd->mBounds, ContainerSize());
1509     }
1510   }
1511   PlaceTopBottomFrames(psd, -mBStartEdge, lineBSize);
1512 
1513   mFinalLineBSize = lineBSize;
1514   if (mGotLineBox) {
1515     // Fill in returned line-box and max-element-width data
1516     mLineBox->SetBounds(lineWM, psd->mIStart, mBStartEdge,
1517                         psd->mICoord - psd->mIStart, lineBSize,
1518                         ContainerSize());
1519 
1520     mLineBox->SetLogicalAscent(baselineBCoord - mBStartEdge);
1521 #ifdef NOISY_BLOCKDIR_ALIGN
1522     printf("  [line]==> bounds{x,y,w,h}={%d,%d,%d,%d} lh=%d a=%d\n",
1523            mLineBox->GetBounds().IStart(lineWM),
1524            mLineBox->GetBounds().BStart(lineWM),
1525            mLineBox->GetBounds().ISize(lineWM),
1526            mLineBox->GetBounds().BSize(lineWM), mFinalLineBSize,
1527            mLineBox->GetLogicalAscent());
1528 #endif
1529   }
1530 }
1531 
1532 // Place frames with CSS property vertical-align: top or bottom.
PlaceTopBottomFrames(PerSpanData * psd,nscoord aDistanceFromStart,nscoord aLineBSize)1533 void nsLineLayout::PlaceTopBottomFrames(PerSpanData* psd,
1534                                         nscoord aDistanceFromStart,
1535                                         nscoord aLineBSize) {
1536   for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
1537     PerSpanData* span = pfd->mSpan;
1538 #ifdef DEBUG
1539     NS_ASSERTION(0xFF != pfd->mBlockDirAlign, "umr");
1540 #endif
1541     WritingMode lineWM = mRootSpan->mWritingMode;
1542     nsSize containerSize = ContainerSizeForSpan(psd);
1543     switch (pfd->mBlockDirAlign) {
1544       case VALIGN_TOP:
1545         if (span) {
1546           pfd->mBounds.BStart(lineWM) = -aDistanceFromStart - span->mMinBCoord;
1547         } else {
1548           pfd->mBounds.BStart(lineWM) =
1549               -aDistanceFromStart + pfd->mMargin.BStart(lineWM);
1550         }
1551         pfd->mFrame->SetRect(lineWM, pfd->mBounds, containerSize);
1552 #ifdef NOISY_BLOCKDIR_ALIGN
1553         printf("    ");
1554         nsFrame::ListTag(stdout, pfd->mFrame);
1555         printf(": y=%d dTop=%d [bp.top=%d topLeading=%d]\n",
1556                pfd->mBounds.BStart(lineWM), aDistanceFromStart,
1557                span ? pfd->mBorderPadding.BStart(lineWM) : 0,
1558                span ? span->mBStartLeading : 0);
1559 #endif
1560         break;
1561       case VALIGN_BOTTOM:
1562         if (span) {
1563           // Compute bottom leading
1564           pfd->mBounds.BStart(lineWM) =
1565               -aDistanceFromStart + aLineBSize - span->mMaxBCoord;
1566         } else {
1567           pfd->mBounds.BStart(lineWM) = -aDistanceFromStart + aLineBSize -
1568                                         pfd->mMargin.BEnd(lineWM) -
1569                                         pfd->mBounds.BSize(lineWM);
1570         }
1571         pfd->mFrame->SetRect(lineWM, pfd->mBounds, containerSize);
1572 #ifdef NOISY_BLOCKDIR_ALIGN
1573         printf("    ");
1574         nsFrame::ListTag(stdout, pfd->mFrame);
1575         printf(": y=%d\n", pfd->mBounds.BStart(lineWM));
1576 #endif
1577         break;
1578     }
1579     if (span) {
1580       nscoord fromStart = aDistanceFromStart + pfd->mBounds.BStart(lineWM);
1581       PlaceTopBottomFrames(span, fromStart, aLineBSize);
1582     }
1583   }
1584 }
1585 
GetBSizeOfEmphasisMarks(nsIFrame * aSpanFrame,float aInflation)1586 static nscoord GetBSizeOfEmphasisMarks(nsIFrame* aSpanFrame, float aInflation) {
1587   RefPtr<nsFontMetrics> fm = nsLayoutUtils::GetFontMetricsOfEmphasisMarks(
1588       aSpanFrame->StyleContext(), aInflation);
1589   return fm->MaxHeight();
1590 }
1591 
AdjustLeadings(nsIFrame * spanFrame,PerSpanData * psd,const nsStyleText * aStyleText,float aInflation,bool * aZeroEffectiveSpanBox)1592 void nsLineLayout::AdjustLeadings(nsIFrame* spanFrame, PerSpanData* psd,
1593                                   const nsStyleText* aStyleText,
1594                                   float aInflation,
1595                                   bool* aZeroEffectiveSpanBox) {
1596   MOZ_ASSERT(spanFrame == psd->mFrame->mFrame);
1597   nscoord requiredStartLeading = 0;
1598   nscoord requiredEndLeading = 0;
1599   if (spanFrame->IsRubyFrame()) {
1600     // We may need to extend leadings here for ruby annotations as
1601     // required by section Line Spacing in the CSS Ruby spec.
1602     // See http://dev.w3.org/csswg/css-ruby/#line-height
1603     auto rubyFrame = static_cast<nsRubyFrame*>(spanFrame);
1604     RubyBlockLeadings leadings = rubyFrame->GetBlockLeadings();
1605     requiredStartLeading += leadings.mStart;
1606     requiredEndLeading += leadings.mEnd;
1607   }
1608   if (aStyleText->HasTextEmphasis()) {
1609     nscoord bsize = GetBSizeOfEmphasisMarks(spanFrame, aInflation);
1610     LogicalSide side = aStyleText->TextEmphasisSide(mRootSpan->mWritingMode);
1611     if (side == eLogicalSideBStart) {
1612       requiredStartLeading += bsize;
1613     } else {
1614       MOZ_ASSERT(side == eLogicalSideBEnd,
1615                  "emphasis marks must be in block axis");
1616       requiredEndLeading += bsize;
1617     }
1618   }
1619 
1620   nscoord requiredLeading = requiredStartLeading + requiredEndLeading;
1621   // If we do not require any additional leadings, don't touch anything
1622   // here even if it is greater than the original leading, because the
1623   // latter could be negative.
1624   if (requiredLeading != 0) {
1625     nscoord leading = psd->mBStartLeading + psd->mBEndLeading;
1626     nscoord deltaLeading = requiredLeading - leading;
1627     if (deltaLeading > 0) {
1628       // If the total leading is not wide enough for ruby annotations
1629       // and/or emphasis marks, extend the side which is not enough. If
1630       // both sides are not wide enough, replace the leadings with the
1631       // requested values.
1632       if (requiredStartLeading < psd->mBStartLeading) {
1633         psd->mBEndLeading += deltaLeading;
1634       } else if (requiredEndLeading < psd->mBEndLeading) {
1635         psd->mBStartLeading += deltaLeading;
1636       } else {
1637         psd->mBStartLeading = requiredStartLeading;
1638         psd->mBEndLeading = requiredEndLeading;
1639       }
1640       psd->mLogicalBSize += deltaLeading;
1641       // We have adjusted the leadings, it is no longer a zero
1642       // effective span box.
1643       *aZeroEffectiveSpanBox = false;
1644     }
1645   }
1646 }
1647 
GetInflationForBlockDirAlignment(nsIFrame * aFrame,nscoord aInflationMinFontSize)1648 static float GetInflationForBlockDirAlignment(nsIFrame* aFrame,
1649                                               nscoord aInflationMinFontSize) {
1650   if (nsSVGUtils::IsInSVGTextSubtree(aFrame)) {
1651     const nsIFrame* container =
1652         nsLayoutUtils::GetClosestFrameOfType(aFrame, LayoutFrameType::SVGText);
1653     NS_ASSERTION(container, "expected to find an ancestor SVGTextFrame");
1654     return static_cast<const SVGTextFrame*>(container)
1655         ->GetFontSizeScaleFactor();
1656   }
1657   return nsLayoutUtils::FontSizeInflationInner(aFrame, aInflationMinFontSize);
1658 }
1659 
1660 #define BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM nscoord_MAX
1661 #define BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM nscoord_MIN
1662 
1663 // Place frames in the block direction within a given span (CSS property
1664 // vertical-align) Note: this doesn't place frames with vertical-align:
1665 // top or bottom as those have to wait until the entire line box block
1666 // size is known. This is called after the span frame has finished being
1667 // reflowed so that we know its block size.
VerticalAlignFrames(PerSpanData * psd)1668 void nsLineLayout::VerticalAlignFrames(PerSpanData* psd) {
1669   // Get parent frame info
1670   PerFrameData* spanFramePFD = psd->mFrame;
1671   nsIFrame* spanFrame = spanFramePFD->mFrame;
1672 
1673   // Get the parent frame's font for all of the frames in this span
1674   float inflation =
1675       GetInflationForBlockDirAlignment(spanFrame, mInflationMinFontSize);
1676   RefPtr<nsFontMetrics> fm =
1677       nsLayoutUtils::GetFontMetricsForFrame(spanFrame, inflation);
1678 
1679   bool preMode = mStyleText->WhiteSpaceIsSignificant();
1680 
1681   // See if the span is an empty continuation. It's an empty continuation iff:
1682   // - it has a prev-in-flow
1683   // - it has no next in flow
1684   // - it's zero sized
1685   WritingMode lineWM = mRootSpan->mWritingMode;
1686   bool emptyContinuation = psd != mRootSpan && spanFrame->GetPrevInFlow() &&
1687                            !spanFrame->GetNextInFlow() &&
1688                            spanFramePFD->mBounds.IsZeroSize();
1689 
1690 #ifdef NOISY_BLOCKDIR_ALIGN
1691   printf("[%sSpan]", (psd == mRootSpan) ? "Root" : "");
1692   nsFrame::ListTag(stdout, spanFrame);
1693   printf(": preMode=%s strictMode=%s w/h=%d,%d emptyContinuation=%s",
1694          preMode ? "yes" : "no",
1695          mPresContext->CompatibilityMode() != eCompatibility_NavQuirks ? "yes"
1696                                                                        : "no",
1697          spanFramePFD->mBounds.ISize(lineWM),
1698          spanFramePFD->mBounds.BSize(lineWM), emptyContinuation ? "yes" : "no");
1699   if (psd != mRootSpan) {
1700     printf(" bp=%d,%d,%d,%d margin=%d,%d,%d,%d",
1701            spanFramePFD->mBorderPadding.Top(lineWM),
1702            spanFramePFD->mBorderPadding.Right(lineWM),
1703            spanFramePFD->mBorderPadding.Bottom(lineWM),
1704            spanFramePFD->mBorderPadding.Left(lineWM),
1705            spanFramePFD->mMargin.Top(lineWM),
1706            spanFramePFD->mMargin.Right(lineWM),
1707            spanFramePFD->mMargin.Bottom(lineWM),
1708            spanFramePFD->mMargin.Left(lineWM));
1709   }
1710   printf("\n");
1711 #endif
1712 
1713   // Compute the span's zeroEffectiveSpanBox flag. What we are trying
1714   // to determine is how we should treat the span: should it act
1715   // "normally" according to css2 or should it effectively
1716   // "disappear".
1717   //
1718   // In general, if the document being processed is in full standards
1719   // mode then it should act normally (with one exception). The
1720   // exception case is when a span is continued and yet the span is
1721   // empty (e.g. compressed whitespace). For this kind of span we treat
1722   // it as if it were not there so that it doesn't impact the
1723   // line block-size.
1724   //
1725   // In almost standards mode or quirks mode, we should sometimes make
1726   // it disappear. The cases that matter are those where the span
1727   // contains no real text elements that would provide an ascent and
1728   // descent and height. However, if css style elements have been
1729   // applied to the span (border/padding/margin) so that it's clear the
1730   // document author is intending css2 behavior then we act as if strict
1731   // mode is set.
1732   //
1733   // This code works correctly for preMode, because a blank line
1734   // in PRE mode is encoded as a text node with a LF in it, since
1735   // text nodes with only whitespace are considered in preMode.
1736   //
1737   // Much of this logic is shared with the various implementations of
1738   // nsIFrame::IsEmpty since they need to duplicate the way it makes
1739   // some lines empty.  However, nsIFrame::IsEmpty can't be reused here
1740   // since this code sets zeroEffectiveSpanBox even when there are
1741   // non-empty children.
1742   bool zeroEffectiveSpanBox = false;
1743   // XXXldb If we really have empty continuations, then all these other
1744   // checks don't make sense for them.
1745   // XXXldb This should probably just use nsIFrame::IsSelfEmpty, assuming that
1746   // it agrees with this code.  (If it doesn't agree, it probably should.)
1747   if ((emptyContinuation ||
1748        mPresContext->CompatibilityMode() != eCompatibility_FullStandards) &&
1749       ((psd == mRootSpan) || (spanFramePFD->mBorderPadding.IsAllZero() &&
1750                               spanFramePFD->mMargin.IsAllZero()))) {
1751     // This code handles an issue with compatibility with non-css
1752     // conformant browsers. In particular, there are some cases
1753     // where the font-size and line-height for a span must be
1754     // ignored and instead the span must *act* as if it were zero
1755     // sized. In general, if the span contains any non-compressed
1756     // text then we don't use this logic.
1757     // However, this is not propagated outwards, since (in compatibility
1758     // mode) we don't want big line heights for things like
1759     // <p><font size="-1">Text</font></p>
1760 
1761     // We shouldn't include any whitespace that collapses, unless we're
1762     // preformatted (in which case it shouldn't, but the width=0 test is
1763     // perhaps incorrect).  This includes whitespace at the beginning of
1764     // a line and whitespace preceded (?) by other whitespace.
1765     // See bug 134580 and bug 155333.
1766     zeroEffectiveSpanBox = true;
1767     for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
1768       if (pfd->mIsTextFrame &&
1769           (pfd->mIsNonWhitespaceTextFrame || preMode ||
1770            pfd->mBounds.ISize(mRootSpan->mWritingMode) != 0)) {
1771         zeroEffectiveSpanBox = false;
1772         break;
1773       }
1774     }
1775   }
1776 
1777   // Setup baselineBCoord, minBCoord, and maxBCoord
1778   nscoord baselineBCoord, minBCoord, maxBCoord;
1779   if (psd == mRootSpan) {
1780     // Use a zero baselineBCoord since we don't yet know where the baseline
1781     // will be (until we know how tall the line is; then we will
1782     // know). In addition, use extreme values for the minBCoord and maxBCoord
1783     // values so that only the child frames will impact their values
1784     // (since these are children of the block, there is no span box to
1785     // provide initial values).
1786     baselineBCoord = 0;
1787     minBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
1788     maxBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
1789 #ifdef NOISY_BLOCKDIR_ALIGN
1790     printf("[RootSpan]");
1791     nsFrame::ListTag(stdout, spanFrame);
1792     printf(
1793         ": pass1 valign frames: topEdge=%d minLineBSize=%d "
1794         "zeroEffectiveSpanBox=%s\n",
1795         mBStartEdge, mMinLineBSize, zeroEffectiveSpanBox ? "yes" : "no");
1796 #endif
1797   } else {
1798     // Compute the logical block size for this span. The logical block size
1799     // is based on the "line-height" value, not the font-size. Also
1800     // compute the top leading.
1801     float inflation =
1802         GetInflationForBlockDirAlignment(spanFrame, mInflationMinFontSize);
1803     nscoord logicalBSize = ReflowInput::CalcLineHeight(
1804         spanFrame->GetContent(), spanFrame->StyleContext(),
1805         mBlockReflowInput->ComputedHeight(), inflation);
1806     nscoord contentBSize = spanFramePFD->mBounds.BSize(lineWM) -
1807                            spanFramePFD->mBorderPadding.BStartEnd(lineWM);
1808 
1809     // Special-case for a ::first-letter frame, set the line height to
1810     // the frame block size if the user has left line-height == normal
1811     const nsStyleText* styleText = spanFrame->StyleText();
1812     if (spanFramePFD->mIsLetterFrame && !spanFrame->GetPrevInFlow() &&
1813         styleText->mLineHeight.GetUnit() == eStyleUnit_Normal) {
1814       logicalBSize = spanFramePFD->mBounds.BSize(lineWM);
1815     }
1816 
1817     nscoord leading = logicalBSize - contentBSize;
1818     psd->mBStartLeading = leading / 2;
1819     psd->mBEndLeading = leading - psd->mBStartLeading;
1820     psd->mLogicalBSize = logicalBSize;
1821     AdjustLeadings(spanFrame, psd, styleText, inflation, &zeroEffectiveSpanBox);
1822 
1823     if (zeroEffectiveSpanBox) {
1824       // When the span-box is to be ignored, zero out the initial
1825       // values so that the span doesn't impact the final line
1826       // height. The contents of the span can impact the final line
1827       // height.
1828 
1829       // Note that things are readjusted for this span after its children
1830       // are reflowed
1831       minBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
1832       maxBCoord = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
1833     } else {
1834       // The initial values for the min and max block coord values are in the
1835       // span's coordinate space, and cover the logical block size of the span.
1836       // If there are child frames in this span that stick out of this area
1837       // then the minBCoord and maxBCoord are updated by the amount of logical
1838       // blockSize that is outside this range.
1839       minBCoord =
1840           spanFramePFD->mBorderPadding.BStart(lineWM) - psd->mBStartLeading;
1841       maxBCoord = minBCoord + psd->mLogicalBSize;
1842     }
1843 
1844     // This is the distance from the top edge of the parents visual
1845     // box to the baseline. The span already computed this for us,
1846     // so just use it.
1847     *psd->mBaseline = baselineBCoord = spanFramePFD->mAscent;
1848 
1849 #ifdef NOISY_BLOCKDIR_ALIGN
1850     printf("[%sSpan]", (psd == mRootSpan) ? "Root" : "");
1851     nsFrame::ListTag(stdout, spanFrame);
1852     printf(
1853         ": baseLine=%d logicalBSize=%d topLeading=%d h=%d bp=%d,%d "
1854         "zeroEffectiveSpanBox=%s\n",
1855         baselineBCoord, psd->mLogicalBSize, psd->mBStartLeading,
1856         spanFramePFD->mBounds.BSize(lineWM),
1857         spanFramePFD->mBorderPadding.Top(lineWM),
1858         spanFramePFD->mBorderPadding.Bottom(lineWM),
1859         zeroEffectiveSpanBox ? "yes" : "no");
1860 #endif
1861   }
1862 
1863   nscoord maxStartBoxBSize = 0;
1864   nscoord maxEndBoxBSize = 0;
1865   PerFrameData* pfd = psd->mFirstFrame;
1866   while (nullptr != pfd) {
1867     nsIFrame* frame = pfd->mFrame;
1868 
1869     // sanity check (see bug 105168, non-reproducible crashes from null frame)
1870     NS_ASSERTION(frame,
1871                  "null frame in PerFrameData - something is very very bad");
1872     if (!frame) {
1873       return;
1874     }
1875 
1876     // Compute the logical block size of the frame
1877     nscoord logicalBSize;
1878     PerSpanData* frameSpan = pfd->mSpan;
1879     if (frameSpan) {
1880       // For span frames the logical-block-size and start-leading were
1881       // pre-computed when the span was reflowed.
1882       logicalBSize = frameSpan->mLogicalBSize;
1883     } else {
1884       // For other elements the logical block size is the same as the
1885       // frame's block size plus its margins.
1886       logicalBSize =
1887           pfd->mBounds.BSize(lineWM) + pfd->mMargin.BStartEnd(lineWM);
1888       if (logicalBSize < 0 &&
1889           mPresContext->CompatibilityMode() == eCompatibility_NavQuirks) {
1890         pfd->mAscent -= logicalBSize;
1891         logicalBSize = 0;
1892       }
1893     }
1894 
1895     // Get vertical-align property ("vertical-align" is the CSS name for
1896     // block-direction align)
1897     const nsStyleCoord& verticalAlign = frame->StyleDisplay()->mVerticalAlign;
1898     uint8_t verticalAlignEnum = frame->VerticalAlignEnum();
1899 #ifdef NOISY_BLOCKDIR_ALIGN
1900     printf("  [frame]");
1901     nsFrame::ListTag(stdout, frame);
1902     printf(": verticalAlignUnit=%d (enum == %d", verticalAlign.GetUnit(),
1903            ((eStyleUnit_Enumerated == verticalAlign.GetUnit())
1904                 ? verticalAlign.GetIntValue()
1905                 : -1));
1906     if (verticalAlignEnum != nsIFrame::eInvalidVerticalAlign) {
1907       printf(", after SVG dominant-baseline conversion == %d",
1908              verticalAlignEnum);
1909     }
1910     printf(")\n");
1911 #endif
1912 
1913     if (verticalAlignEnum != nsIFrame::eInvalidVerticalAlign) {
1914       if (lineWM.IsVertical()) {
1915         if (verticalAlignEnum == NS_STYLE_VERTICAL_ALIGN_MIDDLE) {
1916           // For vertical writing mode where the dominant baseline is centered
1917           // (i.e. text-orientation is not sideways-*), we remap 'middle' to
1918           // 'middle-with-baseline' so that images align sensibly with the
1919           // center-baseline-aligned text.
1920           if (!lineWM.IsSideways()) {
1921             verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_MIDDLE_WITH_BASELINE;
1922           }
1923         } else if (lineWM.IsLineInverted()) {
1924           // Swap the meanings of top and bottom when line is inverted
1925           // relative to block direction.
1926           switch (verticalAlignEnum) {
1927             case NS_STYLE_VERTICAL_ALIGN_TOP:
1928               verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_BOTTOM;
1929               break;
1930             case NS_STYLE_VERTICAL_ALIGN_BOTTOM:
1931               verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_TOP;
1932               break;
1933             case NS_STYLE_VERTICAL_ALIGN_TEXT_TOP:
1934               verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM;
1935               break;
1936             case NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM:
1937               verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_TEXT_TOP;
1938               break;
1939           }
1940         }
1941       }
1942 
1943       // baseline coord that may be adjusted for script offset
1944       nscoord revisedBaselineBCoord = baselineBCoord;
1945 
1946       // For superscript and subscript, raise or lower the baseline of the box
1947       // to the proper offset of the parent's box, then proceed as for BASELINE
1948       if (verticalAlignEnum == NS_STYLE_VERTICAL_ALIGN_SUB ||
1949           verticalAlignEnum == NS_STYLE_VERTICAL_ALIGN_SUPER) {
1950         revisedBaselineBCoord +=
1951             lineWM.FlowRelativeToLineRelativeFactor() *
1952             (verticalAlignEnum == NS_STYLE_VERTICAL_ALIGN_SUB
1953                  ? fm->SubscriptOffset()
1954                  : -fm->SuperscriptOffset());
1955         verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_BASELINE;
1956       }
1957 
1958       switch (verticalAlignEnum) {
1959         default:
1960         case NS_STYLE_VERTICAL_ALIGN_BASELINE:
1961           if (lineWM.IsVertical() && !lineWM.IsSideways()) {
1962             if (frameSpan) {
1963               pfd->mBounds.BStart(lineWM) =
1964                   revisedBaselineBCoord - pfd->mBounds.BSize(lineWM) / 2;
1965             } else {
1966               pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord -
1967                                             logicalBSize / 2 +
1968                                             pfd->mMargin.BStart(lineWM);
1969             }
1970           } else {
1971             pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord - pfd->mAscent;
1972           }
1973           pfd->mBlockDirAlign = VALIGN_OTHER;
1974           break;
1975 
1976         case NS_STYLE_VERTICAL_ALIGN_TOP: {
1977           pfd->mBlockDirAlign = VALIGN_TOP;
1978           nscoord subtreeBSize = logicalBSize;
1979           if (frameSpan) {
1980             subtreeBSize = frameSpan->mMaxBCoord - frameSpan->mMinBCoord;
1981             NS_ASSERTION(subtreeBSize >= logicalBSize,
1982                          "unexpected subtree block size");
1983           }
1984           if (subtreeBSize > maxStartBoxBSize) {
1985             maxStartBoxBSize = subtreeBSize;
1986           }
1987           break;
1988         }
1989 
1990         case NS_STYLE_VERTICAL_ALIGN_BOTTOM: {
1991           pfd->mBlockDirAlign = VALIGN_BOTTOM;
1992           nscoord subtreeBSize = logicalBSize;
1993           if (frameSpan) {
1994             subtreeBSize = frameSpan->mMaxBCoord - frameSpan->mMinBCoord;
1995             NS_ASSERTION(subtreeBSize >= logicalBSize,
1996                          "unexpected subtree block size");
1997           }
1998           if (subtreeBSize > maxEndBoxBSize) {
1999             maxEndBoxBSize = subtreeBSize;
2000           }
2001           break;
2002         }
2003 
2004         case NS_STYLE_VERTICAL_ALIGN_MIDDLE: {
2005           // Align the midpoint of the frame with 1/2 the parents
2006           // x-height above the baseline.
2007           nscoord parentXHeight =
2008               lineWM.FlowRelativeToLineRelativeFactor() * fm->XHeight();
2009           if (frameSpan) {
2010             pfd->mBounds.BStart(lineWM) =
2011                 baselineBCoord -
2012                 (parentXHeight + pfd->mBounds.BSize(lineWM)) / 2;
2013           } else {
2014             pfd->mBounds.BStart(lineWM) = baselineBCoord -
2015                                           (parentXHeight + logicalBSize) / 2 +
2016                                           pfd->mMargin.BStart(lineWM);
2017           }
2018           pfd->mBlockDirAlign = VALIGN_OTHER;
2019           break;
2020         }
2021 
2022         case NS_STYLE_VERTICAL_ALIGN_TEXT_TOP: {
2023           // The top of the logical box is aligned with the top of
2024           // the parent element's text.
2025           // XXX For vertical text we will need a new API to get the logical
2026           //     max-ascent here
2027           nscoord parentAscent =
2028               lineWM.IsLineInverted() ? fm->MaxDescent() : fm->MaxAscent();
2029           if (frameSpan) {
2030             pfd->mBounds.BStart(lineWM) = baselineBCoord - parentAscent -
2031                                           pfd->mBorderPadding.BStart(lineWM) +
2032                                           frameSpan->mBStartLeading;
2033           } else {
2034             pfd->mBounds.BStart(lineWM) =
2035                 baselineBCoord - parentAscent + pfd->mMargin.BStart(lineWM);
2036           }
2037           pfd->mBlockDirAlign = VALIGN_OTHER;
2038           break;
2039         }
2040 
2041         case NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM: {
2042           // The bottom of the logical box is aligned with the
2043           // bottom of the parent elements text.
2044           nscoord parentDescent =
2045               lineWM.IsLineInverted() ? fm->MaxAscent() : fm->MaxDescent();
2046           if (frameSpan) {
2047             pfd->mBounds.BStart(lineWM) =
2048                 baselineBCoord + parentDescent - pfd->mBounds.BSize(lineWM) +
2049                 pfd->mBorderPadding.BEnd(lineWM) - frameSpan->mBEndLeading;
2050           } else {
2051             pfd->mBounds.BStart(lineWM) = baselineBCoord + parentDescent -
2052                                           pfd->mBounds.BSize(lineWM) -
2053                                           pfd->mMargin.BEnd(lineWM);
2054           }
2055           pfd->mBlockDirAlign = VALIGN_OTHER;
2056           break;
2057         }
2058 
2059         case NS_STYLE_VERTICAL_ALIGN_MIDDLE_WITH_BASELINE: {
2060           // Align the midpoint of the frame with the baseline of the parent.
2061           if (frameSpan) {
2062             pfd->mBounds.BStart(lineWM) =
2063                 baselineBCoord - pfd->mBounds.BSize(lineWM) / 2;
2064           } else {
2065             pfd->mBounds.BStart(lineWM) =
2066                 baselineBCoord - logicalBSize / 2 + pfd->mMargin.BStart(lineWM);
2067           }
2068           pfd->mBlockDirAlign = VALIGN_OTHER;
2069           break;
2070         }
2071       }
2072     } else {
2073       // We have either a coord, a percent, or a calc().
2074       nscoord pctBasis = 0;
2075       if (verticalAlign.HasPercent()) {
2076         // Percentages are like lengths, except treated as a percentage
2077         // of the elements line block size value.
2078         float inflation =
2079             GetInflationForBlockDirAlignment(frame, mInflationMinFontSize);
2080         pctBasis = ReflowInput::CalcLineHeight(
2081             frame->GetContent(), frame->StyleContext(),
2082             mBlockReflowInput->ComputedBSize(), inflation);
2083       }
2084       nscoord offset = verticalAlign.ComputeCoordPercentCalc(pctBasis);
2085       // According to the CSS2 spec (10.8.1), a positive value
2086       // "raises" the box by the given distance while a negative value
2087       // "lowers" the box by the given distance (with zero being the
2088       // baseline). Since Y coordinates increase towards the bottom of
2089       // the screen we reverse the sign, unless the line orientation is
2090       // inverted relative to block direction.
2091       nscoord revisedBaselineBCoord =
2092           baselineBCoord - offset * lineWM.FlowRelativeToLineRelativeFactor();
2093       if (lineWM.IsVertical() && !lineWM.IsSideways()) {
2094         // If we're using a dominant center baseline, we align with the center
2095         // of the frame being placed (bug 1133945).
2096         pfd->mBounds.BStart(lineWM) =
2097             revisedBaselineBCoord - pfd->mBounds.BSize(lineWM) / 2;
2098       } else {
2099         pfd->mBounds.BStart(lineWM) = revisedBaselineBCoord - pfd->mAscent;
2100       }
2101       pfd->mBlockDirAlign = VALIGN_OTHER;
2102     }
2103 
2104     // Update minBCoord/maxBCoord for frames that we just placed. Do not factor
2105     // text into the equation.
2106     if (pfd->mBlockDirAlign == VALIGN_OTHER) {
2107       // Text frames do not contribute to the min/max Y values for the
2108       // line (instead their parent frame's font-size contributes).
2109       // XXXrbs -- relax this restriction because it causes text frames
2110       //           to jam together when 'font-size-adjust' is enabled
2111       //           and layout is using dynamic font heights (bug 20394)
2112       //        -- Note #1: With this code enabled and with the fact that we are
2113       //        not
2114       //           using Em[Ascent|Descent] as nsDimensions for text metrics in
2115       //           GFX mean that the discussion in bug 13072 cannot hold.
2116       //        -- Note #2: We still don't want empty-text frames to interfere.
2117       //           For example in quirks mode, avoiding empty text frames
2118       //           prevents "tall" lines around elements like <hr> since the
2119       //           rules of <hr> in quirks.css have pseudo text contents with LF
2120       //           in them.
2121       bool canUpdate;
2122       if (pfd->mIsTextFrame) {
2123         // Only consider text frames if they're not empty and
2124         // line-height=normal.
2125         canUpdate =
2126             pfd->mIsNonWhitespaceTextFrame &&
2127             frame->StyleText()->mLineHeight.GetUnit() == eStyleUnit_Normal;
2128       } else {
2129         canUpdate = !pfd->mIsPlaceholder;
2130       }
2131 
2132       if (canUpdate) {
2133         nscoord blockStart, blockEnd;
2134         if (frameSpan) {
2135           // For spans that were are now placing, use their position
2136           // plus their already computed min-Y and max-Y values for
2137           // computing blockStart and blockEnd.
2138           blockStart = pfd->mBounds.BStart(lineWM) + frameSpan->mMinBCoord;
2139           blockEnd = pfd->mBounds.BStart(lineWM) + frameSpan->mMaxBCoord;
2140         } else {
2141           blockStart =
2142               pfd->mBounds.BStart(lineWM) - pfd->mMargin.BStart(lineWM);
2143           blockEnd = blockStart + logicalBSize;
2144         }
2145         if (!preMode &&
2146             mPresContext->CompatibilityMode() != eCompatibility_FullStandards &&
2147             !logicalBSize) {
2148           // Check if it's a BR frame that is not alone on its line (it
2149           // is given a block size of zero to indicate this), and if so reset
2150           // blockStart and blockEnd so that BR frames don't influence the line.
2151           if (frame->IsBrFrame()) {
2152             blockStart = BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM;
2153             blockEnd = BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM;
2154           }
2155         }
2156         if (blockStart < minBCoord) minBCoord = blockStart;
2157         if (blockEnd > maxBCoord) maxBCoord = blockEnd;
2158 #ifdef NOISY_BLOCKDIR_ALIGN
2159         printf(
2160             "     [frame]raw: a=%d h=%d bp=%d,%d logical: h=%d leading=%d y=%d "
2161             "minBCoord=%d maxBCoord=%d\n",
2162             pfd->mAscent, pfd->mBounds.BSize(lineWM),
2163             pfd->mBorderPadding.Top(lineWM), pfd->mBorderPadding.Bottom(lineWM),
2164             logicalBSize, frameSpan ? frameSpan->mBStartLeading : 0,
2165             pfd->mBounds.BStart(lineWM), minBCoord, maxBCoord);
2166 #endif
2167       }
2168       if (psd != mRootSpan) {
2169         frame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(psd));
2170       }
2171     }
2172     pfd = pfd->mNext;
2173   }
2174 
2175   // Factor in the minimum line block-size when handling the root-span for
2176   // the block.
2177   if (psd == mRootSpan) {
2178     // We should factor in the block element's minimum line-height (as
2179     // defined in section 10.8.1 of the css2 spec) assuming that
2180     // zeroEffectiveSpanBox is not set on the root span.  This only happens
2181     // in some cases in quirks mode:
2182     //  (1) if the root span contains non-whitespace text directly (this
2183     //      is handled by zeroEffectiveSpanBox
2184     //  (2) if this line has a bullet
2185     //  (3) if this is the last line of an LI, DT, or DD element
2186     //      (The last line before a block also counts, but not before a
2187     //      BR) (NN4/IE5 quirk)
2188 
2189     // (1) and (2) above
2190     bool applyMinLH = !zeroEffectiveSpanBox || mHasBullet;
2191     bool isLastLine =
2192         !mGotLineBox || (!mLineBox->IsLineWrapped() && !mLineEndsInBR);
2193     if (!applyMinLH && isLastLine) {
2194       nsIContent* blockContent = mRootSpan->mFrame->mFrame->GetContent();
2195       if (blockContent) {
2196         // (3) above, if the last line of LI, DT, or DD
2197         if (blockContent->IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dt,
2198                                               nsGkAtoms::dd)) {
2199           applyMinLH = true;
2200         }
2201       }
2202     }
2203     if (applyMinLH) {
2204       if (psd->mHasNonemptyContent || preMode || mHasBullet) {
2205 #ifdef NOISY_BLOCKDIR_ALIGN
2206         printf("  [span]==> adjusting min/maxBCoord: currentValues: %d,%d",
2207                minBCoord, maxBCoord);
2208 #endif
2209         nscoord minimumLineBSize = mMinLineBSize;
2210         nscoord blockStart = -nsLayoutUtils::GetCenteredFontBaseline(
2211             fm, minimumLineBSize, lineWM.IsLineInverted());
2212         nscoord blockEnd = blockStart + minimumLineBSize;
2213 
2214         if (mStyleText->HasTextEmphasis()) {
2215           nscoord fontMaxHeight = fm->MaxHeight();
2216           nscoord emphasisHeight =
2217               GetBSizeOfEmphasisMarks(spanFrame, inflation);
2218           nscoord delta = fontMaxHeight + emphasisHeight - minimumLineBSize;
2219           if (delta > 0) {
2220             if (minimumLineBSize < fontMaxHeight) {
2221               // If the leadings are negative, fill them first.
2222               nscoord ascent = fm->MaxAscent();
2223               nscoord descent = fm->MaxDescent();
2224               if (lineWM.IsLineInverted()) {
2225                 Swap(ascent, descent);
2226               }
2227               blockStart = -ascent;
2228               blockEnd = descent;
2229               delta = emphasisHeight;
2230             }
2231             LogicalSide side = mStyleText->TextEmphasisSide(lineWM);
2232             if (side == eLogicalSideBStart) {
2233               blockStart -= delta;
2234             } else {
2235               blockEnd += delta;
2236             }
2237           }
2238         }
2239 
2240         if (blockStart < minBCoord) minBCoord = blockStart;
2241         if (blockEnd > maxBCoord) maxBCoord = blockEnd;
2242 
2243 #ifdef NOISY_BLOCKDIR_ALIGN
2244         printf(" new values: %d,%d\n", minBCoord, maxBCoord);
2245 #endif
2246 #ifdef NOISY_BLOCKDIR_ALIGN
2247         printf(
2248             "            Used mMinLineBSize: %d, blockStart: %d, blockEnd: "
2249             "%d\n",
2250             mMinLineBSize, blockStart, blockEnd);
2251 #endif
2252       } else {
2253       // XXX issues:
2254       // [1] BR's on empty lines stop working
2255       // [2] May not honor css2's notion of handling empty elements
2256       // [3] blank lines in a pre-section ("\n") (handled with preMode)
2257 
2258       // XXX Are there other problems with this?
2259 #ifdef NOISY_BLOCKDIR_ALIGN
2260         printf(
2261             "  [span]==> zapping min/maxBCoord: currentValues: %d,%d "
2262             "newValues: 0,0\n",
2263             minBCoord, maxBCoord);
2264 #endif
2265         minBCoord = maxBCoord = 0;
2266       }
2267     }
2268   }
2269 
2270   if ((minBCoord == BLOCKDIR_ALIGN_FRAMES_NO_MINIMUM) ||
2271       (maxBCoord == BLOCKDIR_ALIGN_FRAMES_NO_MAXIMUM)) {
2272     minBCoord = maxBCoord = baselineBCoord;
2273   }
2274 
2275   if (psd != mRootSpan && zeroEffectiveSpanBox) {
2276 #ifdef NOISY_BLOCKDIR_ALIGN
2277     printf("   [span]adjusting for zeroEffectiveSpanBox\n");
2278     printf(
2279         "     Original: minBCoord=%d, maxBCoord=%d, bSize=%d, ascent=%d, "
2280         "logicalBSize=%d, topLeading=%d, bottomLeading=%d\n",
2281         minBCoord, maxBCoord, spanFramePFD->mBounds.BSize(lineWM),
2282         spanFramePFD->mAscent, psd->mLogicalBSize, psd->mBStartLeading,
2283         psd->mBEndLeading);
2284 #endif
2285     nscoord goodMinBCoord =
2286         spanFramePFD->mBorderPadding.BStart(lineWM) - psd->mBStartLeading;
2287     nscoord goodMaxBCoord = goodMinBCoord + psd->mLogicalBSize;
2288 
2289     // For cases like the one in bug 714519 (text-decoration placement
2290     // or making nsLineLayout::IsZeroBSize() handle
2291     // vertical-align:top/bottom on a descendant of the line that's not
2292     // a child of it), we want to treat elements that are
2293     // vertical-align: top or bottom somewhat like children for the
2294     // purposes of this quirk.  To some extent, this is guessing, since
2295     // they might end up being aligned anywhere.  However, we'll guess
2296     // that they'll be placed aligned with the top or bottom of this
2297     // frame (as though this frame is the only thing in the line).
2298     // (Guessing isn't crazy, since all we're doing is reducing the
2299     // scope of a quirk and making the behavior more standards-like.)
2300     if (maxStartBoxBSize > maxBCoord - minBCoord) {
2301       // Distribute maxStartBoxBSize to ascent (baselineBCoord - minBCoord), and
2302       // then to descent (maxBCoord - baselineBCoord) by adjusting minBCoord or
2303       // maxBCoord, but not to exceed goodMinBCoord and goodMaxBCoord.
2304       nscoord distribute = maxStartBoxBSize - (maxBCoord - minBCoord);
2305       nscoord ascentSpace = std::max(minBCoord - goodMinBCoord, 0);
2306       if (distribute > ascentSpace) {
2307         distribute -= ascentSpace;
2308         minBCoord -= ascentSpace;
2309         nscoord descentSpace = std::max(goodMaxBCoord - maxBCoord, 0);
2310         if (distribute > descentSpace) {
2311           maxBCoord += descentSpace;
2312         } else {
2313           maxBCoord += distribute;
2314         }
2315       } else {
2316         minBCoord -= distribute;
2317       }
2318     }
2319     if (maxEndBoxBSize > maxBCoord - minBCoord) {
2320       // Likewise, but preferring descent to ascent.
2321       nscoord distribute = maxEndBoxBSize - (maxBCoord - minBCoord);
2322       nscoord descentSpace = std::max(goodMaxBCoord - maxBCoord, 0);
2323       if (distribute > descentSpace) {
2324         distribute -= descentSpace;
2325         maxBCoord += descentSpace;
2326         nscoord ascentSpace = std::max(minBCoord - goodMinBCoord, 0);
2327         if (distribute > ascentSpace) {
2328           minBCoord -= ascentSpace;
2329         } else {
2330           minBCoord -= distribute;
2331         }
2332       } else {
2333         maxBCoord += distribute;
2334       }
2335     }
2336 
2337     if (minBCoord > goodMinBCoord) {
2338       nscoord adjust = minBCoord - goodMinBCoord;  // positive
2339 
2340       // shrink the logical extents
2341       psd->mLogicalBSize -= adjust;
2342       psd->mBStartLeading -= adjust;
2343     }
2344     if (maxBCoord < goodMaxBCoord) {
2345       nscoord adjust = goodMaxBCoord - maxBCoord;
2346       psd->mLogicalBSize -= adjust;
2347       psd->mBEndLeading -= adjust;
2348     }
2349     if (minBCoord > 0) {
2350       // shrink the content by moving its block start down.  This is tricky,
2351       // since the block start is the 0 for many coordinates, so what we do is
2352       // move everything else up.
2353       spanFramePFD->mAscent -= minBCoord;  // move the baseline up
2354       spanFramePFD->mBounds.BSize(lineWM) -=
2355           minBCoord;  // move the block end up
2356       psd->mBStartLeading += minBCoord;
2357       *psd->mBaseline -= minBCoord;
2358 
2359       pfd = psd->mFirstFrame;
2360       while (nullptr != pfd) {
2361         pfd->mBounds.BStart(lineWM) -= minBCoord;  // move all the children
2362                                                    // back up
2363         pfd->mFrame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(psd));
2364         pfd = pfd->mNext;
2365       }
2366       maxBCoord -= minBCoord;  // since minBCoord is in the frame's own
2367                                // coordinate system
2368       minBCoord = 0;
2369     }
2370     if (maxBCoord < spanFramePFD->mBounds.BSize(lineWM)) {
2371       nscoord adjust = spanFramePFD->mBounds.BSize(lineWM) - maxBCoord;
2372       spanFramePFD->mBounds.BSize(lineWM) -= adjust;  // move the bottom up
2373       psd->mBEndLeading += adjust;
2374     }
2375 #ifdef NOISY_BLOCKDIR_ALIGN
2376     printf(
2377         "     New: minBCoord=%d, maxBCoord=%d, bSize=%d, ascent=%d, "
2378         "logicalBSize=%d, topLeading=%d, bottomLeading=%d\n",
2379         minBCoord, maxBCoord, spanFramePFD->mBounds.BSize(lineWM),
2380         spanFramePFD->mAscent, psd->mLogicalBSize, psd->mBStartLeading,
2381         psd->mBEndLeading);
2382 #endif
2383   }
2384 
2385   psd->mMinBCoord = minBCoord;
2386   psd->mMaxBCoord = maxBCoord;
2387 #ifdef NOISY_BLOCKDIR_ALIGN
2388   printf(
2389       "  [span]==> minBCoord=%d maxBCoord=%d delta=%d maxStartBoxBSize=%d "
2390       "maxEndBoxBSize=%d\n",
2391       minBCoord, maxBCoord, maxBCoord - minBCoord, maxStartBoxBSize,
2392       maxEndBoxBSize);
2393 #endif
2394   if (maxStartBoxBSize > mMaxStartBoxBSize) {
2395     mMaxStartBoxBSize = maxStartBoxBSize;
2396   }
2397   if (maxEndBoxBSize > mMaxEndBoxBSize) {
2398     mMaxEndBoxBSize = maxEndBoxBSize;
2399   }
2400 }
2401 
SlideSpanFrameRect(nsIFrame * aFrame,nscoord aDeltaWidth)2402 static void SlideSpanFrameRect(nsIFrame* aFrame, nscoord aDeltaWidth) {
2403   // This should not use nsIFrame::MovePositionBy because it happens
2404   // prior to relative positioning.  In particular, because
2405   // nsBlockFrame::PlaceLine calls aLineLayout.TrimTrailingWhiteSpace()
2406   // prior to calling aLineLayout.RelativePositionFrames().
2407   nsPoint p = aFrame->GetPosition();
2408   p.x -= aDeltaWidth;
2409   aFrame->SetPosition(p);
2410 }
2411 
TrimTrailingWhiteSpaceIn(PerSpanData * psd,nscoord * aDeltaISize)2412 bool nsLineLayout::TrimTrailingWhiteSpaceIn(PerSpanData* psd,
2413                                             nscoord* aDeltaISize) {
2414   PerFrameData* pfd = psd->mFirstFrame;
2415   if (!pfd) {
2416     *aDeltaISize = 0;
2417     return false;
2418   }
2419   pfd = pfd->Last();
2420   while (nullptr != pfd) {
2421 #ifdef REALLY_NOISY_TRIM
2422     nsFrame::ListTag(stdout, psd->mFrame->mFrame);
2423     printf(": attempting trim of ");
2424     nsFrame::ListTag(stdout, pfd->mFrame);
2425     printf("\n");
2426 #endif
2427     PerSpanData* childSpan = pfd->mSpan;
2428     WritingMode lineWM = mRootSpan->mWritingMode;
2429     if (childSpan) {
2430       // Maybe the child span has the trailing white-space in it?
2431       if (TrimTrailingWhiteSpaceIn(childSpan, aDeltaISize)) {
2432         nscoord deltaISize = *aDeltaISize;
2433         if (deltaISize) {
2434           // Adjust the child spans frame size
2435           pfd->mBounds.ISize(lineWM) -= deltaISize;
2436           if (psd != mRootSpan) {
2437             // When the child span is not a direct child of the block
2438             // we need to update the child spans frame rectangle
2439             // because it most likely will not be done again. Spans
2440             // that are direct children of the block will be updated
2441             // later, however, because the VerticalAlignFrames method
2442             // will be run after this method.
2443             nsSize containerSize = ContainerSizeForSpan(childSpan);
2444             nsIFrame* f = pfd->mFrame;
2445             LogicalRect r(lineWM, f->GetRect(), containerSize);
2446             r.ISize(lineWM) -= deltaISize;
2447             f->SetRect(lineWM, r, containerSize);
2448           }
2449 
2450           // Adjust the inline end edge of the span that contains the child span
2451           psd->mICoord -= deltaISize;
2452 
2453           // Slide any frames that follow the child span over by the
2454           // correct amount. The only thing that can follow the child
2455           // span is empty stuff, so we are just making things
2456           // sensible (keeping the combined area honest).
2457           while (pfd->mNext) {
2458             pfd = pfd->mNext;
2459             pfd->mBounds.IStart(lineWM) -= deltaISize;
2460             if (psd != mRootSpan) {
2461               // When the child span is not a direct child of the block
2462               // we need to update the child span's frame rectangle
2463               // because it most likely will not be done again. Spans
2464               // that are direct children of the block will be updated
2465               // later, however, because the VerticalAlignFrames method
2466               // will be run after this method.
2467               SlideSpanFrameRect(pfd->mFrame, deltaISize);
2468             }
2469           }
2470         }
2471         return true;
2472       }
2473     } else if (!pfd->mIsTextFrame && !pfd->mSkipWhenTrimmingWhitespace) {
2474       // If we hit a frame on the end that's not text and not a placeholder,
2475       // then there is no trailing whitespace to trim. Stop the search.
2476       *aDeltaISize = 0;
2477       return true;
2478     } else if (pfd->mIsTextFrame) {
2479       // Call TrimTrailingWhiteSpace even on empty textframes because they
2480       // might have a soft hyphen which should now appear, changing the frame's
2481       // width
2482       nsTextFrame::TrimOutput trimOutput =
2483           static_cast<nsTextFrame*>(pfd->mFrame)
2484               ->TrimTrailingWhiteSpace(
2485                   mBlockReflowInput->mRenderingContext->GetDrawTarget());
2486 #ifdef NOISY_TRIM
2487       nsFrame::ListTag(stdout, psd->mFrame->mFrame);
2488       printf(": trim of ");
2489       nsFrame::ListTag(stdout, pfd->mFrame);
2490       printf(" returned %d\n", trimOutput.mDeltaWidth);
2491 #endif
2492 
2493       if (trimOutput.mChanged) {
2494         pfd->mRecomputeOverflow = true;
2495       }
2496 
2497       // Delta width not being zero means that
2498       // there is trimmed space in the frame.
2499       if (trimOutput.mDeltaWidth) {
2500         pfd->mBounds.ISize(lineWM) -= trimOutput.mDeltaWidth;
2501 
2502         // If any trailing space is trimmed, the justification opportunity
2503         // generated by the space should be removed as well.
2504         pfd->mJustificationInfo.CancelOpportunityForTrimmedSpace();
2505 
2506         // See if the text frame has already been placed in its parent
2507         if (psd != mRootSpan) {
2508           // The frame was already placed during psd's
2509           // reflow. Update the frames rectangle now.
2510           pfd->mFrame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(psd));
2511         }
2512 
2513         // Adjust containing span's right edge
2514         psd->mICoord -= trimOutput.mDeltaWidth;
2515 
2516         // Slide any frames that follow the text frame over by the
2517         // right amount. The only thing that can follow the text
2518         // frame is empty stuff, so we are just making things
2519         // sensible (keeping the combined area honest).
2520         while (pfd->mNext) {
2521           pfd = pfd->mNext;
2522           pfd->mBounds.IStart(lineWM) -= trimOutput.mDeltaWidth;
2523           if (psd != mRootSpan) {
2524             // When the child span is not a direct child of the block
2525             // we need to update the child spans frame rectangle
2526             // because it most likely will not be done again. Spans
2527             // that are direct children of the block will be updated
2528             // later, however, because the VerticalAlignFrames method
2529             // will be run after this method.
2530             SlideSpanFrameRect(pfd->mFrame, trimOutput.mDeltaWidth);
2531           }
2532         }
2533       }
2534 
2535       if (pfd->mIsNonEmptyTextFrame || trimOutput.mChanged) {
2536         // Pass up to caller so they can shrink their span
2537         *aDeltaISize = trimOutput.mDeltaWidth;
2538         return true;
2539       }
2540     }
2541     pfd = pfd->mPrev;
2542   }
2543 
2544   *aDeltaISize = 0;
2545   return false;
2546 }
2547 
TrimTrailingWhiteSpace()2548 bool nsLineLayout::TrimTrailingWhiteSpace() {
2549   PerSpanData* psd = mRootSpan;
2550   nscoord deltaISize;
2551   TrimTrailingWhiteSpaceIn(psd, &deltaISize);
2552   return 0 != deltaISize;
2553 }
2554 
ParticipatesInJustification() const2555 bool nsLineLayout::PerFrameData::ParticipatesInJustification() const {
2556   if (mIsBullet || mIsEmpty || mSkipWhenTrimmingWhitespace) {
2557     // Skip bullets, empty frames, and placeholders
2558     return false;
2559   }
2560   if (mIsTextFrame && !mIsNonWhitespaceTextFrame &&
2561       static_cast<nsTextFrame*>(mFrame)->IsAtEndOfLine()) {
2562     // Skip trimmed whitespaces
2563     return false;
2564   }
2565   return true;
2566 }
2567 
2568 struct nsLineLayout::JustificationComputationState {
2569   PerFrameData* mFirstParticipant;
2570   PerFrameData* mLastParticipant;
2571   // When we are going across a boundary of ruby base, i.e. entering
2572   // one, leaving one, or both, the following fields will be set to
2573   // the corresponding ruby base frame for handling ruby-align.
2574   PerFrameData* mLastExitedRubyBase;
2575   PerFrameData* mLastEnteredRubyBase;
2576 
JustificationComputationStatensLineLayout::JustificationComputationState2577   JustificationComputationState()
2578       : mFirstParticipant(nullptr),
2579         mLastParticipant(nullptr),
2580         mLastExitedRubyBase(nullptr),
2581         mLastEnteredRubyBase(nullptr) {}
2582 };
2583 
IsRubyAlignSpaceAround(nsIFrame * aRubyBase)2584 static bool IsRubyAlignSpaceAround(nsIFrame* aRubyBase) {
2585   return aRubyBase->StyleText()->mRubyAlign == NS_STYLE_RUBY_ALIGN_SPACE_AROUND;
2586 }
2587 
2588 /**
2589  * Assign justification gaps for justification
2590  * opportunities across two frames.
2591  */
AssignInterframeJustificationGaps(PerFrameData * aFrame,JustificationComputationState & aState)2592 /* static */ int nsLineLayout::AssignInterframeJustificationGaps(
2593     PerFrameData* aFrame, JustificationComputationState& aState) {
2594   PerFrameData* prev = aState.mLastParticipant;
2595   MOZ_ASSERT(prev);
2596 
2597   auto& assign = aFrame->mJustificationAssignment;
2598   auto& prevAssign = prev->mJustificationAssignment;
2599 
2600   if (aState.mLastExitedRubyBase || aState.mLastEnteredRubyBase) {
2601     PerFrameData* exitedRubyBase = aState.mLastExitedRubyBase;
2602     if (!exitedRubyBase || IsRubyAlignSpaceAround(exitedRubyBase->mFrame)) {
2603       prevAssign.mGapsAtEnd = 1;
2604     } else {
2605       exitedRubyBase->mJustificationAssignment.mGapsAtEnd = 1;
2606     }
2607 
2608     PerFrameData* enteredRubyBase = aState.mLastEnteredRubyBase;
2609     if (!enteredRubyBase || IsRubyAlignSpaceAround(enteredRubyBase->mFrame)) {
2610       assign.mGapsAtStart = 1;
2611     } else {
2612       enteredRubyBase->mJustificationAssignment.mGapsAtStart = 1;
2613     }
2614 
2615     // We are no longer going across a ruby base boundary.
2616     aState.mLastExitedRubyBase = nullptr;
2617     aState.mLastEnteredRubyBase = nullptr;
2618     return 1;
2619   }
2620 
2621   const auto& info = aFrame->mJustificationInfo;
2622   const auto& prevInfo = prev->mJustificationInfo;
2623   if (!info.mIsStartJustifiable && !prevInfo.mIsEndJustifiable) {
2624     return 0;
2625   }
2626 
2627   if (!info.mIsStartJustifiable) {
2628     prevAssign.mGapsAtEnd = 2;
2629     assign.mGapsAtStart = 0;
2630   } else if (!prevInfo.mIsEndJustifiable) {
2631     prevAssign.mGapsAtEnd = 0;
2632     assign.mGapsAtStart = 2;
2633   } else {
2634     prevAssign.mGapsAtEnd = 1;
2635     assign.mGapsAtStart = 1;
2636   }
2637   return 1;
2638 }
2639 
2640 /**
2641  * Compute the justification info of the given span, and store the
2642  * number of inner opportunities into the frame's justification info.
2643  * It returns the number of non-inner opportunities it detects.
2644  */
ComputeFrameJustification(PerSpanData * aPSD,JustificationComputationState & aState)2645 int32_t nsLineLayout::ComputeFrameJustification(
2646     PerSpanData* aPSD, JustificationComputationState& aState) {
2647   NS_ASSERTION(aPSD, "null arg");
2648   NS_ASSERTION(!aState.mLastParticipant || !aState.mLastParticipant->mSpan,
2649                "Last participant shall always be a leaf frame");
2650   bool firstChild = true;
2651   int32_t& innerOpportunities =
2652       aPSD->mFrame->mJustificationInfo.mInnerOpportunities;
2653   MOZ_ASSERT(innerOpportunities == 0,
2654              "Justification info should not have been set yet.");
2655   int32_t outerOpportunities = 0;
2656 
2657   for (PerFrameData* pfd = aPSD->mFirstFrame; pfd; pfd = pfd->mNext) {
2658     if (!pfd->ParticipatesInJustification()) {
2659       continue;
2660     }
2661 
2662     bool isRubyBase = pfd->mFrame->IsRubyBaseFrame();
2663     PerFrameData* outerRubyBase = aState.mLastEnteredRubyBase;
2664     if (isRubyBase) {
2665       aState.mLastEnteredRubyBase = pfd;
2666     }
2667 
2668     int extraOpportunities = 0;
2669     if (pfd->mSpan) {
2670       PerSpanData* span = pfd->mSpan;
2671       extraOpportunities = ComputeFrameJustification(span, aState);
2672       innerOpportunities += pfd->mJustificationInfo.mInnerOpportunities;
2673     } else {
2674       if (pfd->mIsTextFrame) {
2675         innerOpportunities += pfd->mJustificationInfo.mInnerOpportunities;
2676       }
2677 
2678       if (!aState.mLastParticipant) {
2679         aState.mFirstParticipant = pfd;
2680         // It is not an empty ruby base, but we are not assigning gaps
2681         // to the content for now. Clear the last entered ruby base so
2682         // that we can correctly set the last exited ruby base.
2683         aState.mLastEnteredRubyBase = nullptr;
2684       } else {
2685         extraOpportunities = AssignInterframeJustificationGaps(pfd, aState);
2686       }
2687 
2688       aState.mLastParticipant = pfd;
2689     }
2690 
2691     if (isRubyBase) {
2692       if (aState.mLastEnteredRubyBase == pfd) {
2693         // There is no justification participant inside this ruby base.
2694         // Ignore this ruby base completely and restore the outer ruby
2695         // base here.
2696         aState.mLastEnteredRubyBase = outerRubyBase;
2697       } else {
2698         aState.mLastExitedRubyBase = pfd;
2699       }
2700     }
2701 
2702     if (firstChild) {
2703       outerOpportunities = extraOpportunities;
2704       firstChild = false;
2705     } else {
2706       innerOpportunities += extraOpportunities;
2707     }
2708   }
2709 
2710   return outerOpportunities;
2711 }
2712 
AdvanceAnnotationInlineBounds(PerFrameData * aPFD,const nsSize & aContainerSize,nscoord aDeltaICoord,nscoord aDeltaISize)2713 void nsLineLayout::AdvanceAnnotationInlineBounds(PerFrameData* aPFD,
2714                                                  const nsSize& aContainerSize,
2715                                                  nscoord aDeltaICoord,
2716                                                  nscoord aDeltaISize) {
2717   nsIFrame* frame = aPFD->mFrame;
2718   LayoutFrameType frameType = frame->Type();
2719   MOZ_ASSERT(frameType == LayoutFrameType::RubyText ||
2720              frameType == LayoutFrameType::RubyTextContainer);
2721   MOZ_ASSERT(aPFD->mSpan, "rt and rtc should have span.");
2722 
2723   PerSpanData* psd = aPFD->mSpan;
2724   WritingMode lineWM = mRootSpan->mWritingMode;
2725   aPFD->mBounds.IStart(lineWM) += aDeltaICoord;
2726 
2727   // Check whether this expansion should be counted into the reserved
2728   // isize or not. When it is a ruby text container, and it has some
2729   // children linked to the base, it must not have reserved isize,
2730   // or its children won't align with their bases.  Otherwise, this
2731   // expansion should be reserved.  There are two cases a ruby text
2732   // container does not have children linked to the base:
2733   // 1. it is a container for span; 2. its children are collapsed.
2734   // See bug 1055674 for the second case.
2735   if (frameType == LayoutFrameType::RubyText ||
2736       // This ruby text container is a span.
2737       (psd->mFirstFrame == psd->mLastFrame && psd->mFirstFrame &&
2738        !psd->mFirstFrame->mIsLinkedToBase)) {
2739     // For ruby text frames, only increase frames
2740     // which are not auto-hidden.
2741     if (frameType != LayoutFrameType::RubyText ||
2742         !static_cast<nsRubyTextFrame*>(frame)->IsAutoHidden()) {
2743       nscoord reservedISize = RubyUtils::GetReservedISize(frame);
2744       RubyUtils::SetReservedISize(frame, reservedISize + aDeltaISize);
2745     }
2746   } else {
2747     // It is a normal ruby text container. Its children will expand
2748     // themselves properly. We only need to expand its own size here.
2749     aPFD->mBounds.ISize(lineWM) += aDeltaISize;
2750   }
2751   aPFD->mFrame->SetRect(lineWM, aPFD->mBounds, aContainerSize);
2752 }
2753 
2754 /**
2755  * This function applies the changes of icoord and isize caused by
2756  * justification to annotations of the given frame.
2757  */
ApplyLineJustificationToAnnotations(PerFrameData * aPFD,nscoord aDeltaICoord,nscoord aDeltaISize)2758 void nsLineLayout::ApplyLineJustificationToAnnotations(PerFrameData* aPFD,
2759                                                        nscoord aDeltaICoord,
2760                                                        nscoord aDeltaISize) {
2761   PerFrameData* pfd = aPFD->mNextAnnotation;
2762   while (pfd) {
2763     nsSize containerSize = pfd->mFrame->GetParent()->GetSize();
2764     AdvanceAnnotationInlineBounds(pfd, containerSize, aDeltaICoord,
2765                                   aDeltaISize);
2766 
2767     // There are two cases where an annotation frame has siblings which
2768     // do not attached to a ruby base-level frame:
2769     // 1. there's an intra-annotation whitespace which has no intra-base
2770     //    white-space to pair with;
2771     // 2. there are not enough ruby bases to be paired with annotations.
2772     // In these cases, their size should not be affected, but we still
2773     // need to move them so that they won't overlap other frames.
2774     PerFrameData* sibling = pfd->mNext;
2775     while (sibling && !sibling->mIsLinkedToBase) {
2776       AdvanceAnnotationInlineBounds(sibling, containerSize,
2777                                     aDeltaICoord + aDeltaISize, 0);
2778       sibling = sibling->mNext;
2779     }
2780 
2781     pfd = pfd->mNextAnnotation;
2782   }
2783 }
2784 
ApplyFrameJustification(PerSpanData * aPSD,JustificationApplicationState & aState)2785 nscoord nsLineLayout::ApplyFrameJustification(
2786     PerSpanData* aPSD, JustificationApplicationState& aState) {
2787   NS_ASSERTION(aPSD, "null arg");
2788 
2789   nscoord deltaICoord = 0;
2790   for (PerFrameData* pfd = aPSD->mFirstFrame; pfd != nullptr;
2791        pfd = pfd->mNext) {
2792     // Don't reposition bullets (and other frames that occur out of X-order?)
2793     if (!pfd->mIsBullet) {
2794       nscoord dw = 0;
2795       WritingMode lineWM = mRootSpan->mWritingMode;
2796       const auto& assign = pfd->mJustificationAssignment;
2797       bool isInlineText =
2798           pfd->mIsTextFrame && !pfd->mWritingMode.IsOrthogonalTo(lineWM);
2799 
2800       if (isInlineText) {
2801         if (aState.IsJustifiable()) {
2802           // Set corresponding justification gaps here, so that the
2803           // text frame knows how it should add gaps at its sides.
2804           const auto& info = pfd->mJustificationInfo;
2805           auto textFrame = static_cast<nsTextFrame*>(pfd->mFrame);
2806           textFrame->AssignJustificationGaps(assign);
2807           dw = aState.Consume(JustificationUtils::CountGaps(info, assign));
2808         }
2809 
2810         if (dw) {
2811           pfd->mRecomputeOverflow = true;
2812         }
2813       } else {
2814         if (nullptr != pfd->mSpan) {
2815           dw = ApplyFrameJustification(pfd->mSpan, aState);
2816         }
2817       }
2818 
2819       pfd->mBounds.ISize(lineWM) += dw;
2820       nscoord gapsAtEnd = 0;
2821       if (!isInlineText && assign.TotalGaps()) {
2822         // It is possible that we assign gaps to non-text frame or an
2823         // orthogonal text frame. Apply the gaps as margin for them.
2824         deltaICoord += aState.Consume(assign.mGapsAtStart);
2825         gapsAtEnd = aState.Consume(assign.mGapsAtEnd);
2826         dw += gapsAtEnd;
2827       }
2828       pfd->mBounds.IStart(lineWM) += deltaICoord;
2829 
2830       // The gaps added to the end of the frame should also be
2831       // excluded from the isize added to the annotation.
2832       ApplyLineJustificationToAnnotations(pfd, deltaICoord, dw - gapsAtEnd);
2833       deltaICoord += dw;
2834       pfd->mFrame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(aPSD));
2835     }
2836   }
2837   return deltaICoord;
2838 }
2839 
FindNearestRubyBaseAncestor(nsIFrame * aFrame)2840 static nsIFrame* FindNearestRubyBaseAncestor(nsIFrame* aFrame) {
2841   MOZ_ASSERT(aFrame->StyleContext()->ShouldSuppressLineBreak());
2842   while (aFrame && !aFrame->IsRubyBaseFrame()) {
2843     aFrame = aFrame->GetParent();
2844   }
2845   // XXX It is possible that no ruby base ancestor is found because of
2846   // some edge cases like form control or canvas inside ruby text.
2847   // See bug 1138092 comment 4.
2848   NS_WARNING_ASSERTION(aFrame, "no ruby base ancestor?");
2849   return aFrame;
2850 }
2851 
2852 /**
2853  * This method expands the given frame by the given reserved isize.
2854  */
ExpandRubyBox(PerFrameData * aFrame,nscoord aReservedISize,const nsSize & aContainerSize)2855 void nsLineLayout::ExpandRubyBox(PerFrameData* aFrame, nscoord aReservedISize,
2856                                  const nsSize& aContainerSize) {
2857   WritingMode lineWM = mRootSpan->mWritingMode;
2858   auto rubyAlign = aFrame->mFrame->StyleText()->mRubyAlign;
2859   switch (rubyAlign) {
2860     case NS_STYLE_RUBY_ALIGN_START:
2861       // do nothing for start
2862       break;
2863     case NS_STYLE_RUBY_ALIGN_SPACE_BETWEEN:
2864     case NS_STYLE_RUBY_ALIGN_SPACE_AROUND: {
2865       int32_t opportunities = aFrame->mJustificationInfo.mInnerOpportunities;
2866       int32_t gaps = opportunities * 2;
2867       if (rubyAlign == NS_STYLE_RUBY_ALIGN_SPACE_AROUND) {
2868         // Each expandable ruby box with ruby-align space-around has a
2869         // gap at each of its sides. For rb/rbc, see comment in
2870         // AssignInterframeJustificationGaps; for rt/rtc, see comment
2871         // in ExpandRubyBoxWithAnnotations.
2872         gaps += 2;
2873       }
2874       if (gaps > 0) {
2875         JustificationApplicationState state(gaps, aReservedISize);
2876         ApplyFrameJustification(aFrame->mSpan, state);
2877         break;
2878       }
2879       // If there are no justification opportunities for space-between,
2880       // fall-through to center per spec.
2881       MOZ_FALLTHROUGH;
2882     }
2883     case NS_STYLE_RUBY_ALIGN_CENTER:
2884       // Indent all children by half of the reserved inline size.
2885       for (PerFrameData* child = aFrame->mSpan->mFirstFrame; child;
2886            child = child->mNext) {
2887         child->mBounds.IStart(lineWM) += aReservedISize / 2;
2888         child->mFrame->SetRect(lineWM, child->mBounds, aContainerSize);
2889       }
2890       break;
2891     default:
2892       MOZ_ASSERT_UNREACHABLE("Unknown ruby-align value");
2893   }
2894 
2895   aFrame->mBounds.ISize(lineWM) += aReservedISize;
2896   aFrame->mFrame->SetRect(lineWM, aFrame->mBounds, aContainerSize);
2897 }
2898 
2899 /**
2900  * This method expands the given frame by the reserved inline size.
2901  * It also expands its annotations if they are expandable and have
2902  * reserved isize larger than zero.
2903  */
ExpandRubyBoxWithAnnotations(PerFrameData * aFrame,const nsSize & aContainerSize)2904 void nsLineLayout::ExpandRubyBoxWithAnnotations(PerFrameData* aFrame,
2905                                                 const nsSize& aContainerSize) {
2906   nscoord reservedISize = RubyUtils::GetReservedISize(aFrame->mFrame);
2907   if (reservedISize) {
2908     ExpandRubyBox(aFrame, reservedISize, aContainerSize);
2909   }
2910 
2911   WritingMode lineWM = mRootSpan->mWritingMode;
2912   bool isLevelContainer = aFrame->mFrame->IsRubyBaseContainerFrame();
2913   for (PerFrameData* annotation = aFrame->mNextAnnotation; annotation;
2914        annotation = annotation->mNextAnnotation) {
2915     if (lineWM.IsOrthogonalTo(annotation->mFrame->GetWritingMode())) {
2916       // Inter-character case: don't attempt to expand ruby annotations.
2917       continue;
2918     }
2919     if (isLevelContainer) {
2920       nsIFrame* rtcFrame = annotation->mFrame;
2921       MOZ_ASSERT(rtcFrame->IsRubyTextContainerFrame());
2922       // It is necessary to set the rect again because the container
2923       // width was unknown, and zero was used instead when we reflow
2924       // them. The corresponding base containers were repositioned in
2925       // VerticalAlignFrames and PlaceTopBottomFrames.
2926       MOZ_ASSERT(rtcFrame->GetLogicalSize(lineWM) ==
2927                  annotation->mBounds.Size(lineWM));
2928       rtcFrame->SetPosition(lineWM, annotation->mBounds.Origin(lineWM),
2929                             aContainerSize);
2930     }
2931 
2932     nscoord reservedISize = RubyUtils::GetReservedISize(annotation->mFrame);
2933     if (!reservedISize) {
2934       continue;
2935     }
2936 
2937     MOZ_ASSERT(annotation->mSpan);
2938     JustificationComputationState computeState;
2939     ComputeFrameJustification(annotation->mSpan, computeState);
2940     if (!computeState.mFirstParticipant) {
2941       continue;
2942     }
2943     if (IsRubyAlignSpaceAround(annotation->mFrame)) {
2944       // Add one gap at each side of this annotation.
2945       computeState.mFirstParticipant->mJustificationAssignment.mGapsAtStart = 1;
2946       computeState.mLastParticipant->mJustificationAssignment.mGapsAtEnd = 1;
2947     }
2948     nsIFrame* parentFrame = annotation->mFrame->GetParent();
2949     nsSize containerSize = parentFrame->GetSize();
2950     MOZ_ASSERT(containerSize == aContainerSize ||
2951                    parentFrame->IsRubyTextContainerFrame(),
2952                "Container width should only be different when the current "
2953                "annotation is a ruby text frame, whose parent is not same "
2954                "as its base frame.");
2955     ExpandRubyBox(annotation, reservedISize, containerSize);
2956     ExpandInlineRubyBoxes(annotation->mSpan);
2957   }
2958 }
2959 
2960 /**
2961  * This method looks for all expandable ruby box in the given span, and
2962  * calls ExpandRubyBox to expand them in depth-first preorder.
2963  */
ExpandInlineRubyBoxes(PerSpanData * aSpan)2964 void nsLineLayout::ExpandInlineRubyBoxes(PerSpanData* aSpan) {
2965   nsSize containerSize = ContainerSizeForSpan(aSpan);
2966   for (PerFrameData* pfd = aSpan->mFirstFrame; pfd; pfd = pfd->mNext) {
2967     if (RubyUtils::IsExpandableRubyBox(pfd->mFrame)) {
2968       ExpandRubyBoxWithAnnotations(pfd, containerSize);
2969     }
2970     if (pfd->mSpan) {
2971       ExpandInlineRubyBoxes(pfd->mSpan);
2972     }
2973   }
2974 }
2975 
2976 // Align inline frames within the line according to the CSS text-align
2977 // property.
TextAlignLine(nsLineBox * aLine,bool aIsLastLine)2978 void nsLineLayout::TextAlignLine(nsLineBox* aLine, bool aIsLastLine) {
2979   /**
2980    * NOTE: aIsLastLine ain't necessarily so: it is correctly set by caller
2981    * only in cases where the last line needs special handling.
2982    */
2983   PerSpanData* psd = mRootSpan;
2984   WritingMode lineWM = psd->mWritingMode;
2985   LAYOUT_WARN_IF_FALSE(psd->mIEnd != NS_UNCONSTRAINEDSIZE,
2986                        "have unconstrained width; this should only result from "
2987                        "very large sizes, not attempts at intrinsic width "
2988                        "calculation");
2989   nscoord availISize = psd->mIEnd - psd->mIStart;
2990   nscoord remainingISize = availISize - aLine->ISize();
2991 #ifdef NOISY_INLINEDIR_ALIGN
2992   nsFrame::ListTag(stdout, mBlockReflowInput->mFrame);
2993   printf(": availISize=%d lineBounds.IStart=%d lineISize=%d delta=%d\n",
2994          availISize, aLine->IStart(), aLine->ISize(), remainingISize);
2995 #endif
2996 
2997   // 'text-align-last: auto' is equivalent to the value of the 'text-align'
2998   // property except when 'text-align' is set to 'justify', in which case it
2999   // is 'justify' when 'text-justify' is 'distribute' and 'start' otherwise.
3000   //
3001   // XXX: the code below will have to change when we implement text-justify
3002   //
3003   nscoord dx = 0;
3004   uint8_t textAlign = mStyleText->mTextAlign;
3005   bool textAlignTrue = mStyleText->mTextAlignTrue;
3006   if (aIsLastLine) {
3007     textAlignTrue = mStyleText->mTextAlignLastTrue;
3008     if (mStyleText->mTextAlignLast == NS_STYLE_TEXT_ALIGN_AUTO) {
3009       if (textAlign == NS_STYLE_TEXT_ALIGN_JUSTIFY) {
3010         textAlign = NS_STYLE_TEXT_ALIGN_START;
3011       }
3012     } else {
3013       textAlign = mStyleText->mTextAlignLast;
3014     }
3015   }
3016 
3017   bool isSVG = nsSVGUtils::IsInSVGTextSubtree(mBlockReflowInput->mFrame);
3018   bool doTextAlign = remainingISize > 0 || textAlignTrue;
3019 
3020   int32_t additionalGaps = 0;
3021   if (!isSVG &&
3022       (mHasRuby || (doTextAlign && textAlign == NS_STYLE_TEXT_ALIGN_JUSTIFY))) {
3023     JustificationComputationState computeState;
3024     ComputeFrameJustification(psd, computeState);
3025     if (mHasRuby && computeState.mFirstParticipant) {
3026       PerFrameData* firstFrame = computeState.mFirstParticipant;
3027       if (firstFrame->mFrame->StyleContext()->ShouldSuppressLineBreak()) {
3028         MOZ_ASSERT(!firstFrame->mJustificationAssignment.mGapsAtStart);
3029         nsIFrame* rubyBase = FindNearestRubyBaseAncestor(firstFrame->mFrame);
3030         if (rubyBase && IsRubyAlignSpaceAround(rubyBase)) {
3031           firstFrame->mJustificationAssignment.mGapsAtStart = 1;
3032           additionalGaps++;
3033         }
3034       }
3035       PerFrameData* lastFrame = computeState.mLastParticipant;
3036       if (lastFrame->mFrame->StyleContext()->ShouldSuppressLineBreak()) {
3037         MOZ_ASSERT(!lastFrame->mJustificationAssignment.mGapsAtEnd);
3038         nsIFrame* rubyBase = FindNearestRubyBaseAncestor(lastFrame->mFrame);
3039         if (rubyBase && IsRubyAlignSpaceAround(rubyBase)) {
3040           lastFrame->mJustificationAssignment.mGapsAtEnd = 1;
3041           additionalGaps++;
3042         }
3043       }
3044     }
3045   }
3046 
3047   if (!isSVG && doTextAlign) {
3048     switch (textAlign) {
3049       case NS_STYLE_TEXT_ALIGN_JUSTIFY: {
3050         int32_t opportunities =
3051             psd->mFrame->mJustificationInfo.mInnerOpportunities;
3052         if (opportunities > 0) {
3053           int32_t gaps = opportunities * 2 + additionalGaps;
3054           JustificationApplicationState applyState(gaps, remainingISize);
3055 
3056           // Apply the justification, and make sure to update our linebox
3057           // width to account for it.
3058           aLine->ExpandBy(ApplyFrameJustification(psd, applyState),
3059                           ContainerSizeForSpan(psd));
3060 
3061           MOZ_ASSERT(applyState.mGaps.mHandled == applyState.mGaps.mCount,
3062                      "Unprocessed justification gaps");
3063           MOZ_ASSERT(
3064               applyState.mWidth.mConsumed == applyState.mWidth.mAvailable,
3065               "Unprocessed justification width");
3066           break;
3067         }
3068         // Fall through to the default case if we could not justify to fill
3069         // the space.
3070         MOZ_FALLTHROUGH;
3071       }
3072 
3073       case NS_STYLE_TEXT_ALIGN_START:
3074         // default alignment is to start edge so do nothing
3075         break;
3076 
3077       case NS_STYLE_TEXT_ALIGN_LEFT:
3078       case NS_STYLE_TEXT_ALIGN_MOZ_LEFT:
3079         if (!lineWM.IsBidiLTR()) {
3080           dx = remainingISize;
3081         }
3082         break;
3083 
3084       case NS_STYLE_TEXT_ALIGN_RIGHT:
3085       case NS_STYLE_TEXT_ALIGN_MOZ_RIGHT:
3086         if (lineWM.IsBidiLTR()) {
3087           dx = remainingISize;
3088         }
3089         break;
3090 
3091       case NS_STYLE_TEXT_ALIGN_END:
3092         dx = remainingISize;
3093         break;
3094 
3095       case NS_STYLE_TEXT_ALIGN_CENTER:
3096       case NS_STYLE_TEXT_ALIGN_MOZ_CENTER:
3097         dx = remainingISize / 2;
3098         break;
3099     }
3100   }
3101 
3102   if (mHasRuby) {
3103     ExpandInlineRubyBoxes(mRootSpan);
3104   }
3105 
3106   if (mPresContext->BidiEnabled() &&
3107       (!mPresContext->IsVisualMode() || !lineWM.IsBidiLTR())) {
3108     nsBidiPresUtils::ReorderFrames(
3109         psd->mFirstFrame->mFrame, aLine->GetChildCount(), lineWM,
3110         mContainerSize, psd->mIStart + mTextIndent + dx);
3111     if (dx) {
3112       aLine->IndentBy(dx, ContainerSize());
3113     }
3114   } else if (dx) {
3115     for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
3116       pfd->mBounds.IStart(lineWM) += dx;
3117       pfd->mFrame->SetRect(lineWM, pfd->mBounds, ContainerSizeForSpan(psd));
3118     }
3119     aLine->IndentBy(dx, ContainerSize());
3120   }
3121 }
3122 
3123 // This method applies any relative positioning to the given frame.
ApplyRelativePositioning(PerFrameData * aPFD)3124 void nsLineLayout::ApplyRelativePositioning(PerFrameData* aPFD) {
3125   if (!aPFD->mRelativePos) {
3126     return;
3127   }
3128 
3129   nsIFrame* frame = aPFD->mFrame;
3130   WritingMode frameWM = aPFD->mWritingMode;
3131   LogicalPoint origin = frame->GetLogicalPosition(ContainerSize());
3132   // right and bottom are handled by
3133   // ReflowInput::ComputeRelativeOffsets
3134   ReflowInput::ApplyRelativePositioning(frame, frameWM, aPFD->mOffsets, &origin,
3135                                         ContainerSize());
3136   frame->SetPosition(frameWM, origin, ContainerSize());
3137 }
3138 
3139 // This method do relative positioning for ruby annotations.
RelativePositionAnnotations(PerSpanData * aRubyPSD,nsOverflowAreas & aOverflowAreas)3140 void nsLineLayout::RelativePositionAnnotations(
3141     PerSpanData* aRubyPSD, nsOverflowAreas& aOverflowAreas) {
3142   MOZ_ASSERT(aRubyPSD->mFrame->mFrame->IsRubyFrame());
3143   for (PerFrameData* pfd = aRubyPSD->mFirstFrame; pfd; pfd = pfd->mNext) {
3144     MOZ_ASSERT(pfd->mFrame->IsRubyBaseContainerFrame());
3145     for (PerFrameData* rtc = pfd->mNextAnnotation; rtc;
3146          rtc = rtc->mNextAnnotation) {
3147       nsIFrame* rtcFrame = rtc->mFrame;
3148       MOZ_ASSERT(rtcFrame->IsRubyTextContainerFrame());
3149       ApplyRelativePositioning(rtc);
3150       nsOverflowAreas rtcOverflowAreas;
3151       RelativePositionFrames(rtc->mSpan, rtcOverflowAreas);
3152       aOverflowAreas.UnionWith(rtcOverflowAreas + rtcFrame->GetPosition());
3153     }
3154   }
3155 }
3156 
RelativePositionFrames(PerSpanData * psd,nsOverflowAreas & aOverflowAreas)3157 void nsLineLayout::RelativePositionFrames(PerSpanData* psd,
3158                                           nsOverflowAreas& aOverflowAreas) {
3159   nsOverflowAreas overflowAreas;
3160   WritingMode wm = psd->mWritingMode;
3161   if (psd != mRootSpan) {
3162     // The span's overflow areas come in three parts:
3163     // -- this frame's width and height
3164     // -- pfd->mOverflowAreas, which is the area of a bullet or the union
3165     // of a relatively positioned frame's absolute children
3166     // -- the bounds of all inline descendants
3167     // The former two parts are computed right here, we gather the descendants
3168     // below.
3169     // At this point psd->mFrame->mBounds might be out of date since
3170     // bidi reordering can move and resize the frames. So use the frame's
3171     // rect instead of mBounds.
3172     nsRect adjustedBounds(nsPoint(0, 0), psd->mFrame->mFrame->GetSize());
3173 
3174     overflowAreas.ScrollableOverflow().UnionRect(
3175         psd->mFrame->mOverflowAreas.ScrollableOverflow(), adjustedBounds);
3176     overflowAreas.VisualOverflow().UnionRect(
3177         psd->mFrame->mOverflowAreas.VisualOverflow(), adjustedBounds);
3178   } else {
3179     LogicalRect rect(wm, psd->mIStart, mBStartEdge, psd->mICoord - psd->mIStart,
3180                      mFinalLineBSize);
3181     // The minimum combined area for the frames that are direct
3182     // children of the block starts at the upper left corner of the
3183     // line and is sized to match the size of the line's bounding box
3184     // (the same size as the values returned from VerticalAlignFrames)
3185     overflowAreas.VisualOverflow() = rect.GetPhysicalRect(wm, ContainerSize());
3186     overflowAreas.ScrollableOverflow() = overflowAreas.VisualOverflow();
3187   }
3188 
3189   for (PerFrameData* pfd = psd->mFirstFrame; pfd; pfd = pfd->mNext) {
3190     nsIFrame* frame = pfd->mFrame;
3191 
3192     // Adjust the origin of the frame
3193     ApplyRelativePositioning(pfd);
3194 
3195     // We must position the view correctly before positioning its
3196     // descendants so that widgets are positioned properly (since only
3197     // some views have widgets).
3198     if (frame->HasView())
3199       nsContainerFrame::SyncFrameViewAfterReflow(
3200           mPresContext, frame, frame->GetView(),
3201           pfd->mOverflowAreas.VisualOverflow(), NS_FRAME_NO_SIZE_VIEW);
3202 
3203     // Note: the combined area of a child is in its coordinate
3204     // system. We adjust the childs combined area into our coordinate
3205     // system before computing the aggregated value by adding in
3206     // <b>x</b> and <b>y</b> which were computed above.
3207     nsOverflowAreas r;
3208     if (pfd->mSpan) {
3209       // Compute a new combined area for the child span before
3210       // aggregating it into our combined area.
3211       RelativePositionFrames(pfd->mSpan, r);
3212     } else {
3213       r = pfd->mOverflowAreas;
3214       if (pfd->mIsTextFrame) {
3215         // We need to recompute overflow areas in four cases:
3216         // (1) When PFD_RECOMPUTEOVERFLOW is set due to trimming
3217         // (2) When there are text decorations, since we can't recompute the
3218         //     overflow area until Reflow and VerticalAlignLine have finished
3219         // (3) When there are text emphasis marks, since the marks may be
3220         //     put further away if the text is inside ruby.
3221         // (4) When there are text strokes
3222         if (pfd->mRecomputeOverflow ||
3223             frame->StyleContext()->HasTextDecorationLines() ||
3224             frame->StyleText()->HasTextEmphasis() ||
3225             frame->StyleText()->HasWebkitTextStroke()) {
3226           nsTextFrame* f = static_cast<nsTextFrame*>(frame);
3227           r = f->RecomputeOverflow(mBlockReflowInput->mFrame);
3228         }
3229         frame->FinishAndStoreOverflow(r, frame->GetSize());
3230       }
3231 
3232       // If we have something that's not an inline but with a complex frame
3233       // hierarchy inside that contains views, they need to be
3234       // positioned.
3235       // All descendant views must be repositioned even if this frame
3236       // does have a view in case this frame's view does not have a
3237       // widget and some of the descendant views do have widgets --
3238       // otherwise the widgets won't be repositioned.
3239       nsContainerFrame::PositionChildViews(frame);
3240     }
3241 
3242     // Do this here (rather than along with setting the overflow rect
3243     // below) so we get leaf frames as well.  No need to worry
3244     // about the root span, since it doesn't have a frame.
3245     if (frame->HasView())
3246       nsContainerFrame::SyncFrameViewAfterReflow(
3247           mPresContext, frame, frame->GetView(), r.VisualOverflow(),
3248           NS_FRAME_NO_MOVE_VIEW);
3249 
3250     overflowAreas.UnionWith(r + frame->GetPosition());
3251   }
3252 
3253   // Also compute relative position in the annotations.
3254   if (psd->mFrame->mFrame->IsRubyFrame()) {
3255     RelativePositionAnnotations(psd, overflowAreas);
3256   }
3257 
3258   // If we just computed a spans combined area, we need to update its
3259   // overflow rect...
3260   if (psd != mRootSpan) {
3261     PerFrameData* spanPFD = psd->mFrame;
3262     nsIFrame* frame = spanPFD->mFrame;
3263     frame->FinishAndStoreOverflow(overflowAreas, frame->GetSize());
3264   }
3265   aOverflowAreas = overflowAreas;
3266 }
3267