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