forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegacyLineLayout.cpp
More file actions
2354 lines (2070 loc) · 115 KB
/
LegacyLineLayout.cpp
File metadata and controls
2354 lines (2070 loc) · 115 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2000 Lars Knoll (knoll@kde.org)
* Copyright (C) 2003-2019 Apple Inc. All right reserved.
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2013 ChangSeok Oh <shivamidow@gmail.com>
* Copyright (C) 2013 Adobe Systems Inc. All right reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "LegacyLineLayout.h"
#include "AXObjectCache.h"
#include "BidiResolver.h"
#include "BreakingContext.h"
#include "DocumentInlines.h"
#include "FloatingObjects.h"
#include "HTMLParserIdioms.h"
#include "InlineIteratorBox.h"
#include "InlineIteratorTextBox.h"
#include "InlineTextBoxStyle.h"
#include "InlineWalker.h"
#include "LegacyInlineElementBox.h"
#include "LegacyInlineIterator.h"
#include "LegacyInlineTextBox.h"
#include "LineLayoutState.h"
#include "Logging.h"
#include "RenderBlockFlow.h"
#include "RenderFragmentContainer.h"
#include "RenderFragmentedFlow.h"
#include "RenderLayoutState.h"
#include "RenderLineBreak.h"
#include "RenderRubyBase.h"
#include "RenderRubyText.h"
#include "RenderSVGText.h"
#include "RenderView.h"
#include "SVGElementTypeHelpers.h"
#include "SVGRootInlineBox.h"
#include "Settings.h"
#include "VerticalPositionCache.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
LegacyLineLayout::LegacyLineLayout(RenderBlockFlow& flow)
: m_flow(flow)
{
}
LegacyLineLayout::~LegacyLineLayout()
{
if (m_flow.containsFloats())
m_flow.floatingObjects()->clearLineBoxTreePointers();
lineBoxes().deleteLineBoxTree();
};
static void determineDirectionality(TextDirection& dir, LegacyInlineIterator iter)
{
while (!iter.atEnd()) {
if (iter.atParagraphSeparator())
return;
if (UChar current = iter.current()) {
UCharDirection charDirection = u_charDirection(current);
if (charDirection == U_LEFT_TO_RIGHT) {
dir = TextDirection::LTR;
return;
}
if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
dir = TextDirection::RTL;
return;
}
}
iter.increment();
}
}
inline std::unique_ptr<BidiRun> createRun(int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
{
return makeUnique<BidiRun>(start, end, obj, resolver.context(), resolver.dir());
}
void LegacyLineLayout::appendRunsForObject(BidiRunList<BidiRun>* runs, int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
{
if (start > end || RenderBlock::shouldSkipCreatingRunsForObject(obj))
return;
LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
bool haveNextTransition = (lineWhitespaceCollapsingState.currentTransition() < lineWhitespaceCollapsingState.numTransitions());
LegacyInlineIterator nextTransition;
if (haveNextTransition)
nextTransition = lineWhitespaceCollapsingState.transitions()[lineWhitespaceCollapsingState.currentTransition()];
if (lineWhitespaceCollapsingState.betweenTransitions()) {
if (!haveNextTransition || (&obj != nextTransition.renderer()))
return;
// This is a new start point. Stop ignoring objects and
// adjust our start.
start = nextTransition.offset();
lineWhitespaceCollapsingState.incrementCurrentTransition();
if (start < end) {
appendRunsForObject(runs, start, end, obj, resolver);
return;
}
} else {
if (!haveNextTransition || (&obj != nextTransition.renderer())) {
if (runs)
runs->appendRun(createRun(start, end, obj, resolver));
return;
}
// An end transition has been encountered within our object. We need to append a run with our endpoint.
if (static_cast<int>(nextTransition.offset() + 1) <= end) {
lineWhitespaceCollapsingState.incrementCurrentTransition();
// The end of the line is before the object we're inspecting. Skip everything and return
if (nextTransition.refersToEndOfPreviousNode())
return;
if (static_cast<int>(nextTransition.offset() + 1) > start && runs)
runs->appendRun(createRun(start, nextTransition.offset() + 1, obj, resolver));
appendRunsForObject(runs, nextTransition.offset() + 1, end, obj, resolver);
} else if (runs)
runs->appendRun(createRun(start, end, obj, resolver));
}
}
std::unique_ptr<LegacyRootInlineBox> LegacyLineLayout::createRootInlineBox()
{
if (is<RenderSVGText>(m_flow)) {
auto box = makeUnique<SVGRootInlineBox>(downcast<RenderSVGText>(m_flow));
box->setHasVirtualLogicalHeight();
return box;
}
return makeUnique<LegacyRootInlineBox>(m_flow);
}
LegacyRootInlineBox* LegacyLineLayout::createAndAppendRootInlineBox()
{
auto newRootBox = createRootInlineBox();
LegacyRootInlineBox* rootBox = newRootBox.get();
m_lineBoxes.appendLineBox(WTFMove(newRootBox));
if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && firstRootBox() == rootBox) {
if (AXObjectCache* cache = m_flow.document().existingAXObjectCache())
cache->deferRecomputeIsIgnored(m_flow.element());
}
return rootBox;
}
LegacyInlineBox* LegacyLineLayout::createInlineBoxForRenderer(RenderObject* renderer, bool isOnlyRun)
{
if (renderer == &m_flow)
return createAndAppendRootInlineBox();
if (is<RenderText>(*renderer))
return downcast<RenderText>(*renderer).createInlineTextBox();
if (is<RenderBox>(*renderer)) {
// FIXME: This is terrible. This branch returns an *owned* pointer!
return downcast<RenderBox>(*renderer).createInlineBox().release();
}
if (is<RenderLineBreak>(*renderer)) {
// FIXME: This is terrible. This branch returns an *owned* pointer!
auto inlineBox = downcast<RenderLineBreak>(*renderer).createInlineBox().release();
// We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
// (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
inlineBox->setBehavesLikeText(isOnlyRun || renderer->document().inNoQuirksMode() || renderer->isLineBreakOpportunity());
return inlineBox;
}
return downcast<RenderInline>(*renderer).createAndAppendInlineFlowBox();
}
static inline void dirtyLineBoxesForRenderer(RenderObject& renderer, bool fullLayout)
{
if (is<RenderText>(renderer)) {
RenderText& renderText = downcast<RenderText>(renderer);
updateCounterIfNeeded(renderText);
renderText.dirtyLineBoxes(fullLayout);
} else if (is<RenderLineBreak>(renderer))
downcast<RenderLineBreak>(renderer).dirtyLineBoxes(fullLayout);
else
downcast<RenderInline>(renderer).dirtyLineBoxes(fullLayout);
}
static bool parentIsConstructedOrHaveNext(LegacyInlineFlowBox* parentBox)
{
do {
if (parentBox->isConstructed() || parentBox->nextOnLine())
return true;
parentBox = parentBox->parent();
} while (parentBox);
return false;
}
LegacyInlineFlowBox* LegacyLineLayout::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, LegacyInlineBox* childBox)
{
// See if we have an unconstructed line box for this object that is also
// the last item on the line.
unsigned lineDepth = 1;
LegacyInlineFlowBox* parentBox = nullptr;
LegacyInlineFlowBox* result = nullptr;
bool hasDefaultLineBoxContain = style().lineBoxContain() == RenderStyle::initialLineBoxContain();
do {
RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(is<RenderInline>(*obj) || obj == &m_flow);
RenderInline* inlineFlow = obj != &m_flow ? downcast<RenderInline>(obj) : nullptr;
// Get the last box we made for this render object.
parentBox = inlineFlow ? inlineFlow->lastLineBox() : downcast<RenderBlockFlow>(*obj).lastRootBox();
// If this box or its ancestor is constructed then it is from a previous line, and we need
// to make a new box for our line. If this box or its ancestor is unconstructed but it has
// something following it on the line, then we know we have to make a new box
// as well. In this situation our inline has actually been split in two on
// the same line (this can happen with very fancy language mixtures).
bool constructedNewBox = false;
bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
if (!canUseExistingParentBox) {
// We need to make a new box for this render object. Once
// made, we need to place it at the end of the current line.
LegacyInlineBox* newBox = createInlineBoxForRenderer(obj);
parentBox = downcast<LegacyInlineFlowBox>(newBox);
parentBox->setIsFirstLine(lineInfo.isFirstLine());
parentBox->setIsHorizontal(m_flow.isHorizontalWritingMode());
if (!hasDefaultLineBoxContain)
parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
constructedNewBox = true;
}
if (constructedNewBox || canUseExistingParentBox) {
if (!result)
result = parentBox;
// If we have hit the block itself, then |box| represents the root
// inline box for the line, and it doesn't have to be appended to any parent
// inline.
if (childBox)
parentBox->addToLine(childBox);
if (!constructedNewBox || obj == &m_flow)
break;
childBox = parentBox;
}
// If we've exceeded our line depth, then jump straight to the root and skip all the remaining
// intermediate inline flows.
obj = (++lineDepth >= cMaxLineDepth) ? &m_flow : obj->parent();
} while (true);
return result;
}
template<typename CharacterType> static inline bool endsWithHTMLSpaces(const CharacterType* characters, unsigned position, unsigned end)
{
for (unsigned i = position; i < end; ++i) {
if (!isHTMLSpace(characters[i]))
return false;
}
return true;
}
static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
{
BidiRun* run = bidiRuns.logicallyLastRun();
if (!run)
return true;
if (!is<RenderText>(run->renderer()))
return false;
auto& text = downcast<RenderText>(run->renderer()).text();
unsigned position = run->stop();
unsigned length = text.length();
if (text.is8Bit())
return endsWithHTMLSpaces(text.characters8(), position, length);
return endsWithHTMLSpaces(text.characters16(), position, length);
}
LegacyRootInlineBox* LegacyLineLayout::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
{
ASSERT(bidiRuns.firstRun());
LegacyInlineFlowBox* parentBox = 0;
int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
// Create a box for our object.
bool isOnlyRun = (runCount == 1);
if (runCount == 2 && !r->renderer().isListMarker())
isOnlyRun = (!style().isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->renderer().isListMarker();
if (lineInfo.isEmpty())
continue;
LegacyInlineBox* box = createInlineBoxForRenderer(&r->renderer(), isOnlyRun);
r->setBox(box);
// If we have no parent box yet, or if the run is not simply a sibling,
// then we need to construct inline boxes as necessary to properly enclose the
// run's inline box. Segments can only be siblings at the root level, as
// they are positioned separately.
if (!parentBox || &parentBox->renderer() != r->renderer().parent()) {
// Create new inline boxes all the way back to the appropriate insertion point.
RenderObject* parentToUse = r->renderer().parent();
parentBox = createLineBoxes(parentToUse, lineInfo, box);
} else {
// Append the inline box to this line.
parentBox->addToLine(box);
}
box->setBidiLevel(r->level());
if (is<LegacyInlineTextBox>(*box)) {
auto& textBox = downcast<LegacyInlineTextBox>(*box);
textBox.setStart(r->m_start);
textBox.setLen(r->m_stop - r->m_start);
if (r->m_hasHyphen)
textBox.setHasHyphen(true);
}
}
// We should have a root inline box. It should be unconstructed and
// be the last continuation of our line list.
ASSERT(lastRootBox() && !lastRootBox()->isConstructed());
// Set bits on our inline flow boxes that indicate which sides should
// paint borders/margins/padding. This knowledge will ultimately be used when
// we determine the horizontal positions and widths of all the inline boxes on
// the line.
bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->renderer().isText() ? !reachedEndOfTextRenderer(bidiRuns) : !is<RenderInline>(bidiRuns.logicallyLastRun()->renderer());
lastRootBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, &bidiRuns.logicallyLastRun()->renderer());
// Now mark the line boxes as being constructed.
lastRootBox()->setConstructed();
// Return the last line.
return lastRootBox();
}
TextAlignMode LegacyLineLayout::textAlignmentForLine(bool endsWithSoftBreak) const
{
if (auto overrideAlignment = m_flow.overrideTextAlignmentForLine(endsWithSoftBreak))
return *overrideAlignment;
TextAlignMode alignment = style().textAlign();
if (endsWithSoftBreak)
return alignment;
TextAlignLast alignmentLast = style().textAlignLast();
switch (alignmentLast) {
case TextAlignLast::Start:
return TextAlignMode::Start;
case TextAlignLast::End:
return TextAlignMode::End;
case TextAlignLast::Left:
return TextAlignMode::Left;
case TextAlignLast::Right:
return TextAlignMode::Right;
case TextAlignLast::Center:
return TextAlignMode::Center;
case TextAlignLast::Justify:
return TextAlignMode::Justify;
case TextAlignLast::Auto:
if (alignment == TextAlignMode::Justify)
return TextAlignMode::Start;
return alignment;
}
ASSERT_NOT_REACHED();
return TextAlignMode::Start;
}
static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
// The direction of the block should determine what happens with wide lines.
// In particular with RTL blocks, wide lines should still spill out to the left.
if (isLeftToRightDirection) {
if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
return;
}
if (trailingSpaceRun)
trailingSpaceRun->box()->setLogicalWidth(0);
else if (totalLogicalWidth > availableLogicalWidth)
logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
}
static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
// Wide lines spill out of the block based off direction.
// So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
// side of the block.
if (isLeftToRightDirection) {
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
trailingSpaceRun->box()->setLogicalWidth(0);
}
logicalLeft += std::max(0.f, availableLogicalWidth - totalLogicalWidth);
return;
}
if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
} else
logicalLeft += availableLogicalWidth - totalLogicalWidth;
}
static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
{
float trailingSpaceWidth = 0;
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
trailingSpaceWidth = std::min(trailingSpaceRun->box()->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceWidth));
}
if (isLeftToRightDirection)
logicalLeft += std::max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
else
logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
}
void LegacyLineLayout::setMarginsForRubyRun(BidiRun* run, RenderRubyRun& renderer, RenderObject* previousObject, const LineInfo& lineInfo)
{
float startOverhang;
float endOverhang;
RenderObject* nextObject = 0;
for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
if (!runWithNextObject->renderer().isOutOfFlowPositioned() && !runWithNextObject->box()->isLineBreak()) {
nextObject = &runWithNextObject->renderer();
break;
}
}
renderer.getOverhang(lineInfo.isFirstLine(), renderer.style().isLeftToRightDirection() ? previousObject : nextObject, renderer.style().isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
m_flow.setMarginStartForChild(renderer, LayoutUnit(-startOverhang));
m_flow.setMarginEndForChild(renderer, LayoutUnit(-endOverhang));
}
static inline void setLogicalWidthForTextRun(LegacyRootInlineBox* lineBox, BidiRun* run, RenderText& renderer, float xPos, const LineInfo& lineInfo,
GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
{
HashSet<const Font*> fallbackFonts;
GlyphOverflow glyphOverflow;
const FontCascade& font = lineStyle(*renderer.parent(), lineInfo).fontCascade();
// Always compute glyph overflow if the block's line-box-contain value is "glyphs".
if (lineBox->fitsToGlyphs()) {
// If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
// will keep us from computing glyph bounds in nearly all cases.
bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
int baselineShift = lineBox->verticalPositionForBox(run->box(), verticalPositionCache);
int rootDescent = includeRootLine ? font.metricsOfPrimaryFont().descent() : 0;
int rootAscent = includeRootLine ? font.metricsOfPrimaryFont().ascent() : 0;
int boxAscent = font.metricsOfPrimaryFont().ascent() - baselineShift;
int boxDescent = font.metricsOfPrimaryFont().descent() + baselineShift;
if (boxAscent > rootDescent || boxDescent > rootAscent)
glyphOverflow.computeBounds = true;
}
LayoutUnit hyphenWidth;
if (downcast<LegacyInlineTextBox>(*run->box()).hasHyphen())
hyphenWidth = measureHyphenWidth(renderer, font, &fallbackFonts);
float measuredWidth = 0;
bool kerningIsEnabled = font.enableKerning();
bool canUseSimpleFontCodePath = renderer.canUseSimpleFontCodePath();
// Since we don't cache glyph overflows, we need to re-measure the run if
// the style is linebox-contain: glyph.
if (!lineBox->fitsToGlyphs() && canUseSimpleFontCodePath) {
unsigned lastEndOffset = run->m_start;
bool atFirstWordMeasurement = true;
for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
WordMeasurement& wordMeasurement = wordMeasurements[i];
if (wordMeasurement.width <= 0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
continue;
if (wordMeasurement.renderer != &renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
continue;
lastEndOffset = wordMeasurement.endOffset;
if (kerningIsEnabled && lastEndOffset == run->m_stop) {
int wordLength = lastEndOffset - wordMeasurement.startOffset;
GlyphOverflow overflow;
measuredWidth += renderer.width(wordMeasurement.startOffset, wordLength, xPos + measuredWidth, lineInfo.isFirstLine(),
&wordMeasurement.fallbackFonts, &overflow);
UChar c = renderer.characterAt(wordMeasurement.startOffset);
// renderer.width() omits word-spacing value for leading whitespace, so let's just add it back here.
if (!atFirstWordMeasurement && FontCascade::treatAsSpace(c))
measuredWidth += renderer.style().fontCascade().wordSpacing();
} else
measuredWidth += wordMeasurement.width;
atFirstWordMeasurement = false;
for (auto& font : wordMeasurement.fallbackFonts)
fallbackFonts.add(font);
}
if (measuredWidth && lastEndOffset != run->m_stop) {
// If we don't have enough cached data, we'll measure the run again.
measuredWidth = 0;
fallbackFonts.clear();
}
}
if (!measuredWidth)
measuredWidth = renderer.width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
ASSERT(measuredWidth >= 0);
ASSERT(hyphenWidth >= 0);
run->box()->setLogicalWidth(measuredWidth + hyphenWidth);
if (!fallbackFonts.isEmpty()) {
ASSERT(run->box()->behavesLikeText());
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<LegacyInlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
ASSERT(it->value.first.isEmpty());
it->value.first = copyToVector(fallbackFonts);
run->box()->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
}
// Include text decoration visual overflow as part of the glyph overflow.
if (!renderer.style().textDecorationsInEffect().isEmpty())
glyphOverflow.extendTo(visualOverflowForDecorations(run->box()->lineStyle(), InlineIterator::textBoxFor(downcast<LegacyInlineTextBox>(run->box()))));
if (!glyphOverflow.isEmpty()) {
ASSERT(run->box()->behavesLikeText());
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<LegacyInlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
it->value.second = glyphOverflow;
run->box()->clearKnownToHaveNoOverflow();
}
}
void LegacyLineLayout::updateRubyForJustifiedText(RenderRubyRun& rubyRun, BidiRun& r, const Vector<unsigned, 16>& expansionOpportunities, unsigned& expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth, size_t& i)
{
if (!rubyRun.rubyBase() || !rubyRun.rubyBase()->firstRootBox() || rubyRun.rubyBase()->firstRootBox()->nextRootBox() || !r.renderer().style().collapseWhiteSpace())
return;
auto& rubyBase = *rubyRun.rubyBase();
auto& rootBox = *rubyBase.firstRootBox();
float totalExpansion = 0;
unsigned totalOpportunitiesInRun = 0;
for (auto* leafChild = rootBox.firstLeafDescendant(); leafChild; leafChild = leafChild->nextLeafOnLine()) {
if (!leafChild->isInlineTextBox())
continue;
unsigned opportunitiesInRun = expansionOpportunities[i++];
ASSERT(opportunitiesInRun <= expansionOpportunityCount);
auto expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
totalExpansion += expansion;
totalOpportunitiesInRun += opportunitiesInRun;
}
ASSERT(!rubyRun.hasOverridingLogicalWidth());
float newBaseWidth = rubyRun.logicalWidth() + totalExpansion + m_flow.marginStartForChild(rubyRun) + m_flow.marginEndForChild(rubyRun);
float newRubyRunWidth = rubyRun.logicalWidth() + totalExpansion;
rubyBase.setInitialOffset((newRubyRunWidth - newBaseWidth) / 2);
rubyRun.setOverridingLogicalWidth(LayoutUnit(newRubyRunWidth));
rubyRun.setNeedsLayout(MarkOnlyThis);
rootBox.markDirty();
if (RenderRubyText* rubyText = rubyRun.rubyText()) {
if (LegacyRootInlineBox* textRootBox = rubyText->firstRootBox())
textRootBox->markDirty();
}
rubyRun.layoutBlock(true);
rubyRun.clearOverridingLogicalWidth();
r.box()->setExpansion(newRubyRunWidth - r.box()->logicalWidth());
totalLogicalWidth += totalExpansion;
expansionOpportunityCount -= totalOpportunitiesInRun;
}
void LegacyLineLayout::computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, const Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float totalLogicalWidth, float availableLogicalWidth)
{
if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
return;
size_t i = 0;
for (BidiRun* run = firstRun; run; run = run->next()) {
if (!run->box() || run == trailingSpaceRun)
continue;
// Positioned objects are only participating to figure out their correct static x position.
// They have no affect on the width. Similarly, line break boxes have no affect on the width.
if (run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak())
continue;
if (is<RenderText>(run->renderer())) {
unsigned opportunitiesInRun = expansionOpportunities[i++];
ASSERT(opportunitiesInRun <= expansionOpportunityCount);
// Only justify text if whitespace is collapsed.
if (run->renderer().style().collapseWhiteSpace()) {
LegacyInlineTextBox& textBox = downcast<LegacyInlineTextBox>(*run->box());
float expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
textBox.setExpansion(expansion);
totalLogicalWidth += expansion;
}
expansionOpportunityCount -= opportunitiesInRun;
} else if (is<RenderRubyRun>(run->renderer()))
updateRubyForJustifiedText(downcast<RenderRubyRun>(run->renderer()), *run, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth, i);
if (!expansionOpportunityCount)
break;
}
}
void LegacyLineLayout::updateLogicalWidthForAlignment(RenderBlockFlow& flow, const TextAlignMode& textAlign, const LegacyRootInlineBox* rootInlineBox, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
{
TextDirection direction;
if (rootInlineBox && flow.style().unicodeBidi() == UnicodeBidi::Plaintext)
direction = rootInlineBox->direction();
else
direction = flow.style().direction();
bool isLeftToRightDirection = flow.style().isLeftToRightDirection();
// Armed with the total width of the line (without justification),
// we now examine our text-align property in order to determine where to position the
// objects horizontally. The total width of the line can be increased if we end up
// justifying text.
switch (textAlign) {
case TextAlignMode::Left:
case TextAlignMode::WebKitLeft:
updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TextAlignMode::Right:
case TextAlignMode::WebKitRight:
updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TextAlignMode::Center:
case TextAlignMode::WebKitCenter:
updateLogicalWidthForCenterAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TextAlignMode::Justify:
flow.adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
if (expansionOpportunityCount) {
if (trailingSpaceRun) {
totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
trailingSpaceRun->box()->setLogicalWidth(0);
}
break;
}
FALLTHROUGH;
case TextAlignMode::Start:
if (direction == TextDirection::LTR)
updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
else
updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
case TextAlignMode::End:
if (direction == TextDirection::LTR)
updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
else
updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
break;
}
}
static void updateLogicalInlinePositions(RenderBlockFlow& block, float& lineLogicalLeft, float& lineLogicalRight, float& availableLogicalWidth, bool firstLine,
IndentTextOrNot shouldIndentText, LayoutUnit boxLogicalHeight)
{
LayoutUnit lineLogicalHeight = block.minLineHeightForReplacedRenderer(firstLine, boxLogicalHeight);
lineLogicalLeft = block.logicalLeftOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
lineLogicalRight = block.logicalRightOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
availableLogicalWidth = lineLogicalRight - lineLogicalLeft;
}
void LegacyLineLayout::computeInlineDirectionPositionsForLine(LegacyRootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
{
TextAlignMode textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
// CSS 2.1: "'Text-indent' only affects a line if it is the first formatted line of an element. For example, the first line of an anonymous block
// box is only affected if it is the first child of its parent element."
// CSS3 "text-indent", "each-line" affects the first line of the block container as well as each line after a forced line break,
// but does not affect lines after a soft wrap break.
bool isFirstLine = lineInfo.isFirstLine() && !(m_flow.isAnonymousBlock() && m_flow.parent()->firstChild() != &m_flow);
bool isAfterHardLineBreak = lineBox->prevRootBox() && lineBox->prevRootBox()->endsWithBreak();
IndentTextOrNot shouldIndentText = requiresIndent(isFirstLine, isAfterHardLineBreak, style());
float lineLogicalLeft;
float lineLogicalRight;
float availableLogicalWidth;
updateLogicalInlinePositions(m_flow, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, 0);
bool needsWordSpacing;
if (firstRun && firstRun->renderer().isReplacedOrInlineBlock()) {
RenderBox& renderBox = downcast<RenderBox>(firstRun->renderer());
updateLogicalInlinePositions(m_flow, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, renderBox.logicalHeight());
}
computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, lineLogicalLeft, availableLogicalWidth, firstRun, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
// The widths of all runs are now known. We can now place every inline box (and
// compute accurate widths for the inline flow boxes).
needsWordSpacing = false;
lineBox->placeBoxesInInlineDirection(lineLogicalLeft, needsWordSpacing);
}
static inline ExpansionBehavior expansionBehaviorForInlineTextBox(RenderBlockFlow& block, LegacyInlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, TextAlignMode textAlign, bool isAfterExpansion)
{
// Tatechuyoko is modeled as the Object Replacement Character (U+FFFC), which can never have expansion opportunities inside nor intrinsically adjacent to it.
if (textBox.renderer().style().textCombine() == TextCombine::All)
return ExpansionBehavior::forbidAll();
auto result = ExpansionBehavior::forbidAll();
bool setLeftExpansion = false;
bool setRightExpansion = false;
if (textAlign == TextAlignMode::Justify) {
// If the next box is ruby, and we're justifying, and the first box in the ruby base has a leading expansion, and we are a text box, then force a trailing expansion.
if (nextRun && is<RenderRubyRun>(nextRun->renderer()) && downcast<RenderRubyRun>(nextRun->renderer()).rubyBase() && nextRun->renderer().style().collapseWhiteSpace()) {
auto& rubyBase = *downcast<RenderRubyRun>(nextRun->renderer()).rubyBase();
if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
if (auto* leafChild = rubyBase.firstRootBox()->firstLeafDescendant()) {
if (is<LegacyInlineTextBox>(*leafChild)) {
// FIXME: This leftExpansionOpportunity doesn't actually work because it doesn't perform the UBA
if (FontCascade::leftExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
setRightExpansion = true;
result.right = ExpansionBehavior::Behavior::Force;
}
}
}
}
}
// Same thing, except if we're following a ruby
if (previousRun && is<RenderRubyRun>(previousRun->renderer()) && downcast<RenderRubyRun>(previousRun->renderer()).rubyBase() && previousRun->renderer().style().collapseWhiteSpace()) {
auto& rubyBase = *downcast<RenderRubyRun>(previousRun->renderer()).rubyBase();
if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
if (auto* leafChild = rubyBase.firstRootBox()->lastLeafDescendant()) {
if (is<LegacyInlineTextBox>(*leafChild)) {
// FIXME: This leftExpansionOpportunity doesn't actually work because it doesn't perform the UBA
if (FontCascade::rightExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
setLeftExpansion = true;
result.left = ExpansionBehavior::Behavior::Force;
}
}
}
}
}
// If we're the first box inside a ruby base, forbid a leading expansion, and vice-versa
if (is<RenderRubyBase>(block)) {
RenderRubyBase& rubyBase = downcast<RenderRubyBase>(block);
if (&textBox == rubyBase.firstRootBox()->firstLeafDescendant()) {
setLeftExpansion = true;
result.left = ExpansionBehavior::Behavior::Forbid;
} if (&textBox == rubyBase.firstRootBox()->lastLeafDescendant()) {
setRightExpansion = true;
result.right = ExpansionBehavior::Behavior::Forbid;
}
}
}
if (!setLeftExpansion)
result.left = isAfterExpansion ? ExpansionBehavior::Behavior::Forbid : ExpansionBehavior::Behavior::Allow;
if (!setRightExpansion)
result.right = ExpansionBehavior::Behavior::Allow;
return result;
}
static inline void applyExpansionBehavior(LegacyInlineTextBox& textBox, ExpansionBehavior expansionBehavior)
{
switch (expansionBehavior.left) {
case ExpansionBehavior::Behavior::Force:
textBox.setForceLeftExpansion();
break;
case ExpansionBehavior::Behavior::Forbid:
textBox.setCanHaveLeftExpansion(false);
break;
case ExpansionBehavior::Behavior::Allow:
textBox.setCanHaveLeftExpansion(true);
break;
default:
ASSERT_NOT_REACHED();
break;
};
switch (expansionBehavior.right) {
case ExpansionBehavior::Behavior::Force:
textBox.setForceRightExpansion();
break;
case ExpansionBehavior::Behavior::Forbid:
textBox.setCanHaveRightExpansion(false);
break;
case ExpansionBehavior::Behavior::Allow:
textBox.setCanHaveRightExpansion(true);
break;
default:
ASSERT_NOT_REACHED();
break;
}
}
static bool inlineAncestorHasStartBorderPaddingOrMargin(const RenderBlockFlow& block, const LegacyInlineBox& box)
{
bool isLTR = block.style().isLeftToRightDirection();
for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
if ((isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0)
|| (!isLTR && currentBox->marginBorderPaddingLogicalRight() > 0))
return true;
}
return false;
}
static bool inlineAncestorHasEndBorderPaddingOrMargin(const RenderBlockFlow& block, const LegacyInlineBox& box)
{
bool isLTR = block.style().isLeftToRightDirection();
for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
if ((isLTR && currentBox->marginBorderPaddingLogicalRight() > 0)
|| (!isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0))
return true;
}
return false;
}
static bool isLastInFlowRun(BidiRun& runToCheck)
{
for (auto* run = runToCheck.next(); run; run = run->next()) {
if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak())
continue;
return false;
}
return true;
}
BidiRun* LegacyLineLayout::computeInlineDirectionPositionsForSegment(LegacyRootInlineBox* lineBox, const LineInfo& lineInfo, TextAlignMode textAlign, float& lineLogicalLeft,
float& availableLogicalWidth, BidiRun* firstRun, BidiRun* trailingSpaceRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache,
WordMeasurements& wordMeasurements)
{
bool needsWordSpacing = false;
bool canHangPunctuationAtStart = style().hangingPunctuation().contains(HangingPunctuation::First);
bool canHangPunctuationAtEnd = style().hangingPunctuation().contains(HangingPunctuation::Last);
bool isLTR = style().isLeftToRightDirection();
float contentWidth = 0;
unsigned expansionOpportunityCount = 0;
bool isAfterExpansion = is<RenderRubyBase>(m_flow) ? downcast<RenderRubyBase>(m_flow).isAfterExpansion() : true;
Vector<unsigned, 16> expansionOpportunities;
HashMap<LegacyInlineTextBox*, LayoutUnit> logicalSpacingForInlineTextBoxes;
auto collectSpacingLogicalWidths = [&] () {
auto totalSpacingWidth = LayoutUnit { };
// Collect the spacing positions (margin, border padding) for the textboxes by traversing the inline tree of the current line.
Vector<LegacyInlineBox*> queue;
queue.append(lineBox);
// 1. Visit each inline box in a preorder fashion
// 2. Accumulate the spacing when we find an LegacyInlineFlowBox (inline container e.g. span)
// 3. Add the LegacyInlineTextBoxes to the hashmap
while (!queue.isEmpty()) {
while (true) {
auto* inlineBox = queue.last();
if (is<LegacyInlineFlowBox>(inlineBox)) {
auto& inlineFlowBox = downcast<LegacyInlineFlowBox>(*inlineBox);
totalSpacingWidth += inlineFlowBox.marginBorderPaddingLogicalLeft();
if (auto* child = inlineFlowBox.firstChild()) {
queue.append(child);
continue;
}
break;
}
if (is<LegacyInlineTextBox>(inlineBox))
logicalSpacingForInlineTextBoxes.add(downcast<LegacyInlineTextBox>(inlineBox), totalSpacingWidth);
break;
}
while (!queue.isEmpty()) {
auto& inlineBox = *queue.takeLast();
if (is<LegacyInlineFlowBox>(inlineBox))
totalSpacingWidth += downcast<LegacyInlineFlowBox>(inlineBox).marginBorderPaddingLogicalRight();
if (auto* nextSibling = inlineBox.nextOnLine()) {
queue.append(nextSibling);
break;
}
}
}
};
collectSpacingLogicalWidths();
BidiRun* run = firstRun;
BidiRun* previousRun = nullptr;
for (; run; run = run->next()) {
auto computeExpansionOpportunities = [&expansionOpportunities, &expansionOpportunityCount, textAlign, &isAfterExpansion] (RenderBlockFlow& block,
LegacyInlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, StringView stringView, TextDirection direction)
{
if (stringView.isEmpty()) {
// Empty runs should still produce an entry in expansionOpportunities list so that the number of items matches the number of runs.
expansionOpportunities.append(0);
return;
}
ExpansionBehavior expansionBehavior = expansionBehaviorForInlineTextBox(block, textBox, previousRun, nextRun, textAlign, isAfterExpansion);
applyExpansionBehavior(textBox, expansionBehavior);
unsigned opportunitiesInRun;
std::tie(opportunitiesInRun, isAfterExpansion) = FontCascade::expansionOpportunityCount(stringView, direction, expansionBehavior);
expansionOpportunities.append(opportunitiesInRun);
expansionOpportunityCount += opportunitiesInRun;
};
if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak()) {
// Positioned objects are only participating to figure out their correct static x position.
// They have no effect on the width. Similarly, line break boxes have no effect on the width.
continue;
}
if (is<RenderText>(run->renderer())) {
auto& renderText = downcast<RenderText>(run->renderer());
auto& textBox = downcast<LegacyInlineTextBox>(*run->box());
if (canHangPunctuationAtStart && lineInfo.isFirstLine() && (isLTR || isLastInFlowRun(*run))
&& !inlineAncestorHasStartBorderPaddingOrMargin(m_flow, *run->box())) {
float hangStartWidth = renderText.hangablePunctuationStartWidth(run->m_start);
availableLogicalWidth += hangStartWidth;
if (style().isLeftToRightDirection())
lineLogicalLeft -= hangStartWidth;
canHangPunctuationAtStart = false;
}
if (canHangPunctuationAtEnd && lineInfo.isLastLine() && run->m_stop > 0 && (!isLTR || isLastInFlowRun(*run))
&& !inlineAncestorHasEndBorderPaddingOrMargin(m_flow, *run->box())) {
float hangEndWidth = renderText.hangablePunctuationEndWidth(run->m_stop - 1);
availableLogicalWidth += hangEndWidth;
if (!style().isLeftToRightDirection())
lineLogicalLeft -= hangEndWidth;
canHangPunctuationAtEnd = false;
}
if (textAlign == TextAlignMode::Justify && run != trailingSpaceRun)
computeExpansionOpportunities(m_flow, textBox, previousRun, run->next(), renderText.stringView(run->m_start, run->m_stop), run->box()->direction());
if (unsigned length = renderText.text().length()) {
if (!run->m_start && needsWordSpacing && isSpaceOrNewline(renderText.characterAt(run->m_start)))
contentWidth += lineStyle(*renderText.parent(), lineInfo).fontCascade().wordSpacing();
// run->m_start == run->m_stop should only be true iff the run is a replaced run for bidi: isolate.
ASSERT(run->m_stop > 0 || run->m_start == run->m_stop);
needsWordSpacing = run->m_stop == length && !isSpaceOrNewline(renderText.characterAt(run->m_stop - 1));
}
auto currentLogicalLeftPosition = logicalSpacingForInlineTextBoxes.get(&textBox) + contentWidth;
setLogicalWidthForTextRun(lineBox, run, renderText, currentLogicalLeftPosition, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
} else {
canHangPunctuationAtStart = false;
bool encounteredJustifiedRuby = false;
if (is<RenderRubyRun>(run->renderer()) && textAlign == TextAlignMode::Justify && run != trailingSpaceRun && downcast<RenderRubyRun>(run->renderer()).rubyBase()) {
auto* rubyBase = downcast<RenderRubyRun>(run->renderer()).rubyBase();
if (rubyBase->firstRootBox() && !rubyBase->firstRootBox()->nextRootBox() && run->renderer().style().collapseWhiteSpace()) {
rubyBase->setIsAfterExpansion(isAfterExpansion);
for (auto* leafChild = rubyBase->firstRootBox()->firstLeafDescendant(); leafChild; leafChild = leafChild->nextLeafOnLine()) {
if (!is<LegacyInlineTextBox>(*leafChild))
continue;
encounteredJustifiedRuby = true;
computeExpansionOpportunities(*rubyBase, downcast<LegacyInlineTextBox>(*leafChild), nullptr, nullptr,
downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction());
}
}
}
if (!encounteredJustifiedRuby)
isAfterExpansion = false;
if (!is<RenderInline>(run->renderer())) {
auto& renderBox = downcast<RenderBox>(run->renderer());
if (is<RenderRubyRun>(renderBox))
setMarginsForRubyRun(run, downcast<RenderRubyRun>(renderBox), previousRun ? &previousRun->renderer() : nullptr, lineInfo);
run->box()->setLogicalWidth(m_flow.logicalWidthForChild(renderBox));
contentWidth += m_flow.marginStartForChild(renderBox) + m_flow.marginEndForChild(renderBox);
}
}
contentWidth += run->box()->logicalWidth();
previousRun = run;
}
if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
// FIXME: see <webkit.org/b/139393#c11>
int lastValidExpansionOpportunitiesIndex = expansionOpportunities.size() - 1;
while (lastValidExpansionOpportunitiesIndex >= 0 && !expansionOpportunities.at(lastValidExpansionOpportunitiesIndex))
--lastValidExpansionOpportunitiesIndex;
if (lastValidExpansionOpportunitiesIndex >= 0) {
ASSERT(expansionOpportunities.at(lastValidExpansionOpportunitiesIndex));
expansionOpportunities.at(lastValidExpansionOpportunitiesIndex)--;
expansionOpportunityCount--;
}
}
if (is<RenderRubyBase>(m_flow) && !expansionOpportunityCount)
textAlign = TextAlignMode::Center;
auto totalLogicalWidth = contentWidth + lineBox->getFlowSpacingLogicalWidth();
updateLogicalWidthForAlignment(m_flow, textAlign, lineBox, trailingSpaceRun, lineLogicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
return run;
}