summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/line/inline_flow_box.cc
blob: f98ae0fa8823bd6201f551c6d58a3b517712d7e9 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
/*
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc.
 *               All rights 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 "third_party/blink/renderer/core/layout/line/inline_flow_box.h"

#include <math.h>
#include <algorithm>
#include "third_party/blink/renderer/core/css/css_property_names.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_api_shim.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_box.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_inline.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_list_marker.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_ruby_base.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_ruby_run.h"
#include "third_party/blink/renderer/core/layout/api/line_layout_ruby_text.h"
#include "third_party/blink/renderer/core/layout/hit_test_result.h"
#include "third_party/blink/renderer/core/layout/line/glyph_overflow.h"
#include "third_party/blink/renderer/core/layout/line/inline_text_box.h"
#include "third_party/blink/renderer/core/layout/line/line_orientation_utils.h"
#include "third_party/blink/renderer/core/layout/line/root_inline_box.h"
#include "third_party/blink/renderer/core/layout/ng/ng_ink_overflow.h"
#include "third_party/blink/renderer/core/paint/box_painter.h"
#include "third_party/blink/renderer/core/paint/inline_flow_box_painter.h"
#include "third_party/blink/renderer/core/paint/rounded_border_geometry.h"
#include "third_party/blink/renderer/core/style/shadow_list.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/wtf/size_assertions.h"

namespace blink {

struct SameSizeAsInlineFlowBox : public InlineBox {
  void* pointers[5];
  uint32_t bitfields : 23;
};

ASSERT_SIZE(InlineFlowBox, SameSizeAsInlineFlowBox);

#if DCHECK_IS_ON()
InlineFlowBox::~InlineFlowBox() {
  if (!has_bad_child_list_)
    for (InlineBox* child = FirstChild(); child; child = child->NextOnLine())
      child->SetHasBadParent();
}
#endif

LayoutUnit InlineFlowBox::GetFlowSpacingLogicalWidth() {
  LayoutUnit tot_width =
      MarginBorderPaddingLogicalLeft() + MarginBorderPaddingLogicalRight();
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->IsInlineFlowBox())
      tot_width += To<InlineFlowBox>(curr)->GetFlowSpacingLogicalWidth();
  }
  return tot_width;
}

LayoutRect InlineFlowBox::FrameRect() const {
  return LayoutRect(Location(), Size());
}

static void SetHasTextDescendantsOnAncestors(InlineFlowBox* box) {
  while (box && !box->HasTextDescendants()) {
    box->SetHasTextDescendants();
    box = box->Parent();
  }
}

static inline bool HasIdenticalLineHeightProperties(
    const ComputedStyle& parent_style,
    const ComputedStyle& child_style,
    bool is_root) {
  return parent_style.HasIdenticalAscentDescentAndLineGap(child_style) &&
         parent_style.LineHeight() == child_style.LineHeight() &&
         (parent_style.VerticalAlign() == EVerticalAlign::kBaseline ||
          is_root) &&
         child_style.VerticalAlign() == EVerticalAlign::kBaseline;
}

inline bool InlineFlowBox::HasEmphasisMarkBefore(
    const InlineTextBox* text_box) const {
  TextEmphasisPosition emphasis_mark_position;
  const auto& style =
      text_box->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
  if (!text_box->GetEmphasisMarkPosition(style, emphasis_mark_position))
    return false;
  LineLogicalSide side = style.GetTextEmphasisLineLogicalSide();
  if (IsHorizontal() || !style.IsFlippedLinesWritingMode())
    return side == LineLogicalSide::kOver;
  return side == LineLogicalSide::kUnder;
}

inline bool InlineFlowBox::HasEmphasisMarkOver(
    const InlineTextBox* text_box) const {
  const auto& style =
      text_box->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
  TextEmphasisPosition emphasis_mark_position;
  return text_box->GetEmphasisMarkPosition(style, emphasis_mark_position) &&
         style.GetTextEmphasisLineLogicalSide() == LineLogicalSide::kOver;
}

inline bool InlineFlowBox::HasEmphasisMarkUnder(
    const InlineTextBox* text_box) const {
  const auto& style =
      text_box->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
  TextEmphasisPosition emphasis_mark_position;
  return text_box->GetEmphasisMarkPosition(style, emphasis_mark_position) &&
         style.GetTextEmphasisLineLogicalSide() == LineLogicalSide::kUnder;
}

void InlineFlowBox::AddToLine(InlineBox* child) {
  DCHECK(!child->Parent());
  DCHECK(!child->NextOnLine());
  DCHECK(!child->PrevOnLine());

  child->SetParent(this);
  if (!first_child_) {
    first_child_ = child;
    last_child_ = child;
  } else {
    last_child_->SetNextOnLine(child);
    child->SetPrevOnLine(last_child_);
    last_child_ = child;
  }
  child->SetFirstLineStyleBit(IsFirstLineStyle());
  child->SetIsHorizontal(IsHorizontal());
  if (child->IsText()) {
    if (child->GetLineLayoutItem().Parent() == GetLineLayoutItem())
      has_text_children_ = true;
    SetHasTextDescendantsOnAncestors(this);
  } else if (child->IsInlineFlowBox()) {
    if (To<InlineFlowBox>(child)->HasTextDescendants())
      SetHasTextDescendantsOnAncestors(this);
  }

  if (DescendantsHaveSameLineHeightAndBaseline() &&
      !child->GetLineLayoutItem().IsOutOfFlowPositioned()) {
    const ComputedStyle& parent_style =
        GetLineLayoutItem().StyleRef(IsFirstLineStyle());
    const ComputedStyle& child_style =
        child->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
    bool root = IsRootInlineBox();
    bool should_clear_descendants_have_same_line_height_and_baseline = false;
    if (child->GetLineLayoutItem().IsAtomicInlineLevel()) {
      should_clear_descendants_have_same_line_height_and_baseline = true;
    } else if (child->IsText()) {
      if (child->GetLineLayoutItem().IsBR() ||
          (child->GetLineLayoutItem().Parent() != GetLineLayoutItem())) {
        if (!HasIdenticalLineHeightProperties(parent_style, child_style, root))
          should_clear_descendants_have_same_line_height_and_baseline = true;
      }
      if (child_style.HasTextCombine() ||
          child_style.GetTextEmphasisMark() != TextEmphasisMark::kNone)
        should_clear_descendants_have_same_line_height_and_baseline = true;
    } else {
      if (child->GetLineLayoutItem().IsBR()) {
        // FIXME: This is dumb. We only turn off because current web test
        // results expect the <br> to be 0-height on the baseline.
        // Other than making a zillion tests have to regenerate results, there's
        // no reason to ditch the optimization here.
        should_clear_descendants_have_same_line_height_and_baseline = true;
      } else {
        DCHECK(IsInlineFlowBox());
        auto* child_flow_box = To<InlineFlowBox>(child);
        // Check the child's bit, and then also check for differences in font,
        // line-height, vertical-align
        if (!child_flow_box->DescendantsHaveSameLineHeightAndBaseline() ||
            !HasIdenticalLineHeightProperties(parent_style, child_style,
                                              root) ||
            child_style.HasBorder() || child_style.MayHavePadding() ||
            child_style.HasTextCombine()) {
          should_clear_descendants_have_same_line_height_and_baseline = true;
        }
      }
    }

    if (should_clear_descendants_have_same_line_height_and_baseline)
      ClearDescendantsHaveSameLineHeightAndBaseline();
  }

  if (!child->GetLineLayoutItem().IsOutOfFlowPositioned()) {
    if (child->IsText()) {
      const ComputedStyle& child_style =
          child->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
      if (child_style.LetterSpacing() < 0 || child_style.TextShadow() ||
          child_style.GetTextEmphasisMark() != TextEmphasisMark::kNone ||
          child_style.TextStrokeWidth() || child->IsLineBreak())
        child->ClearKnownToHaveNoOverflow();
    } else if (child->GetLineLayoutItem().IsAtomicInlineLevel()) {
      LineLayoutBox box = LineLayoutBox(child->GetLineLayoutItem());
#if DCHECK_IS_ON()
      // We're reading the previous overflow state. Read as no overflow if it
      // was not computed yet.
      NGInkOverflow::ReadUnsetAsNoneScope read_unset_as_none;
#endif
      if (box.HasLayoutOverflow() || box.HasVisualOverflow() ||
          box.HasSelfPaintingLayer())
        child->ClearKnownToHaveNoOverflow();
    } else if (!child->GetLineLayoutItem().IsBR() &&
               (child->GetLineLayoutItem()
                    .Style(IsFirstLineStyle())
                    ->BoxShadow() ||
                child->BoxModelObject().HasSelfPaintingLayer() ||
                (child->GetLineLayoutItem().IsListMarker() &&
                 !LineLayoutListMarker(child->GetLineLayoutItem())
                      .IsInside()) ||
                child->GetLineLayoutItem()
                    .Style(IsFirstLineStyle())
                    ->HasBorderImageOutsets() ||
                child->GetLineLayoutItem()
                    .Style(IsFirstLineStyle())
                    ->HasOutline())) {
      child->ClearKnownToHaveNoOverflow();
    }

    if (KnownToHaveNoOverflow() && child->IsInlineFlowBox() &&
        !To<InlineFlowBox>(child)->KnownToHaveNoOverflow())
      ClearKnownToHaveNoOverflow();
  }
}

void InlineFlowBox::RemoveChild(InlineBox* child, MarkLineBoxes mark_dirty) {
  if (mark_dirty == kMarkLineBoxesDirty && !IsDirty())
    DirtyLineBoxes();

  Root().ChildRemoved(child);

  if (child == first_child_)
    first_child_ = child->NextOnLine();
  if (child == last_child_)
    last_child_ = child->PrevOnLine();
  if (child->NextOnLine())
    child->NextOnLine()->SetPrevOnLine(child->PrevOnLine());
  if (child->PrevOnLine())
    child->PrevOnLine()->SetNextOnLine(child->NextOnLine());

  child->SetParent(nullptr);
}

void InlineFlowBox::DeleteLine() {
  InlineBox* child = FirstChild();
  InlineBox* next = nullptr;
  while (child) {
    DCHECK_EQ(this, child->Parent());
    next = child->NextOnLine();
#if DCHECK_IS_ON()
    child->SetParent(nullptr);
#endif
    child->DeleteLine();
    child = next;
  }
#if DCHECK_IS_ON()
  first_child_ = nullptr;
  last_child_ = nullptr;
#endif

  RemoveLineBoxFromLayoutObject();
  Destroy();
}

void InlineFlowBox::RemoveLineBoxFromLayoutObject() {
  LineBoxes()->RemoveLineBox(this);
}

void InlineFlowBox::ExtractLine() {
  if (!Extracted())
    ExtractLineBoxFromLayoutObject();
  for (InlineBox* child = FirstChild(); child; child = child->NextOnLine())
    child->ExtractLine();
}

void InlineFlowBox::ExtractLineBoxFromLayoutObject() {
  LineBoxes()->ExtractLineBox(this);
}

void InlineFlowBox::AttachLine() {
  if (Extracted())
    AttachLineBoxToLayoutObject();
  for (InlineBox* child = FirstChild(); child; child = child->NextOnLine())
    child->AttachLine();
}

void InlineFlowBox::AttachLineBoxToLayoutObject() {
  LineBoxes()->AttachLineBox(this);
}

void InlineFlowBox::Move(const LayoutSize& delta) {
  InlineBox::Move(delta);
  for (InlineBox* child = FirstChild(); child; child = child->NextOnLine()) {
    if (child->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;
    child->Move(delta);
  }
  // FIXME: Rounding error here since overflow was pixel snapped, but nobody
  // other than list markers passes non-integral values here.
  if (LayoutOverflowIsSet())
    overflow_->layout_overflow->Move(delta.Width(), delta.Height());
  if (VisualOverflowIsSet())
    overflow_->visual_overflow->Move(delta.Width(), delta.Height());
}

LineBoxList* InlineFlowBox::LineBoxes() const {
  return LineLayoutInline(GetLineLayoutItem()).LineBoxes();
}

static inline bool IsLastChildForLayoutObject(LineLayoutItem ancestor,
                                              LineLayoutItem child) {
  if (!child)
    return false;

  if (child == ancestor)
    return true;

  LineLayoutItem curr = child;
  LineLayoutItem parent = curr.Parent();
  while (parent && (!parent.IsLayoutBlock() || parent.IsInline())) {
    if (parent.SlowLastChild() != curr)
      return false;
    if (parent == ancestor)
      return true;

    curr = parent;
    parent = curr.Parent();
  }

  return true;
}

static bool IsAncestorAndWithinBlock(LineLayoutItem ancestor,
                                     LineLayoutItem child) {
  LineLayoutItem item = child;
  while (item && (!item.IsLayoutBlock() || item.IsInline())) {
    if (item == ancestor)
      return true;
    item = item.Parent();
  }
  return false;
}

void InlineFlowBox::DetermineSpacingForFlowBoxes(
    bool last_line,
    bool is_logically_last_run_wrapped,
    LineLayoutItem logically_last_run_layout_object) {
  // All boxes start off open.  They will not apply any margins/border/padding
  // on any side.
  bool include_left_edge = false;
  bool include_right_edge = false;

  // The root inline box never has borders/margins/padding.
  if (Parent()) {
    bool ltr = GetLineLayoutItem().StyleRef().IsLeftToRightDirection();

    // Check to see if all initial lines are unconstructed.  If so, then
    // we know the inline began on this line (unless we are a continuation).
    LineBoxList* line_box_list = LineBoxes();
    if (!line_box_list->First()->IsConstructed() &&
        !GetLineLayoutItem().IsInlineElementContinuation()) {
      if (GetLineLayoutItem().StyleRef().BoxDecorationBreak() ==
          EBoxDecorationBreak::kClone)
        include_left_edge = include_right_edge = true;
      else if (ltr && line_box_list->First() == this)
        include_left_edge = true;
      else if (!ltr && line_box_list->Last() == this)
        include_right_edge = true;
    }

    if (!line_box_list->Last()->IsConstructed()) {
      LineLayoutInline inline_flow = LineLayoutInline(GetLineLayoutItem());
      LineLayoutItem logically_last_run_layout_item(
          logically_last_run_layout_object);
      bool is_last_object_on_line =
          !IsAncestorAndWithinBlock(GetLineLayoutItem(),
                                    logically_last_run_layout_item) ||
          (IsLastChildForLayoutObject(GetLineLayoutItem(),
                                      logically_last_run_layout_item) &&
           !is_logically_last_run_wrapped);

      // We include the border under these conditions:
      // (1) The next line was not created, or it is constructed. We check the
      //     previous line for rtl.
      // (2) The logicallyLastRun is not a descendant of this layout object.
      // (3) The logicallyLastRun is a descendant of this layout object, but it
      //     is the last child of this layout object and it does not wrap to the
      //     next line.
      // (4) The decoration break is set to clone therefore there will be
      //     borders on every sides.
      if (GetLineLayoutItem().StyleRef().BoxDecorationBreak() ==
          EBoxDecorationBreak::kClone) {
        include_left_edge = include_right_edge = true;
      } else if (ltr) {
        if (!NextForSameLayoutObject() &&
            ((last_line || is_last_object_on_line) &&
             !inline_flow.Continuation()))
          include_right_edge = true;
      } else {
        if ((!PrevForSameLayoutObject() ||
             PrevForSameLayoutObject()->IsConstructed()) &&
            ((last_line || is_last_object_on_line) &&
             !inline_flow.Continuation()))
          include_left_edge = true;
      }
    }
  }

  SetEdges(include_left_edge, include_right_edge);

  // Recur into our children.
  for (InlineBox* curr_child = FirstChild(); curr_child;
       curr_child = curr_child->NextOnLine()) {
    if (curr_child->IsInlineFlowBox()) {
      auto* curr_flow = To<InlineFlowBox>(curr_child);
      curr_flow->DetermineSpacingForFlowBoxes(last_line,
                                              is_logically_last_run_wrapped,
                                              logically_last_run_layout_object);
    }
  }
}

LayoutUnit InlineFlowBox::PlaceBoxesInInlineDirection(
    LayoutUnit logical_left,
    bool& needs_word_spacing) {
  // Set our x position.
  BeginPlacingBoxRangesInInlineDirection(logical_left);

  LayoutUnit start_logical_left = logical_left;
  logical_left += BorderLogicalLeft() + PaddingLogicalLeft();

  LayoutUnit min_logical_left = start_logical_left;
  LayoutUnit max_logical_right = logical_left;

  PlaceBoxRangeInInlineDirection(FirstChild(), nullptr, logical_left,
                                 min_logical_left, max_logical_right,
                                 needs_word_spacing);

  logical_left += BorderLogicalRight() + PaddingLogicalRight();
  EndPlacingBoxRangesInInlineDirection(start_logical_left, logical_left,
                                       min_logical_left, max_logical_right);
  return logical_left;
}

// TODO(wkorman): needsWordSpacing may not need to be a reference in the below.
// Seek a test case.
void InlineFlowBox::PlaceBoxRangeInInlineDirection(
    InlineBox* first_child,
    InlineBox* last_child,
    LayoutUnit& logical_left,
    LayoutUnit& min_logical_left,
    LayoutUnit& max_logical_right,
    bool& needs_word_spacing) {
  for (InlineBox* curr = first_child; curr && curr != last_child;
       curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsText()) {
      auto* text = To<InlineTextBox>(curr);
      LineLayoutText rt = text->GetLineLayoutItem();
      LayoutUnit space;
      if (rt.TextLength()) {
        if (needs_word_spacing &&
            IsSpaceOrNewline(rt.CharacterAt(text->Start())))
          space = LayoutUnit(rt.Style(IsFirstLineStyle())
                                 ->GetFont()
                                 .GetFontDescription()
                                 .WordSpacing());
        needs_word_spacing = !IsSpaceOrNewline(rt.CharacterAt(text->end()));
      }
      if (IsLeftToRightDirection()) {
        logical_left += space;
        text->SetLogicalLeft(logical_left);
      } else {
        text->SetLogicalLeft(logical_left);
        logical_left += space;
      }
      if (KnownToHaveNoOverflow())
        min_logical_left = std::min(logical_left, min_logical_left);
      logical_left += text->LogicalWidth();
      if (KnownToHaveNoOverflow())
        max_logical_right = std::max(logical_left, max_logical_right);
    } else {
      if (curr->GetLineLayoutItem().IsOutOfFlowPositioned()) {
        if (curr->GetLineLayoutItem()
                .Parent()
                .StyleRef()
                .IsLeftToRightDirection()) {
          curr->SetLogicalLeft(logical_left);
        } else {
          // Our offset that we cache needs to be from the edge of the right
          // border box and not the left border box. We have to subtract |x|
          // from the width of the block (which can be obtained from the root
          // line box).
          curr->SetLogicalLeft(Root().Block().LogicalWidth() - logical_left);
        }
        continue;  // The positioned object has no effect on the width.
      }
      if (curr->GetLineLayoutItem().IsLayoutInline()) {
        auto* flow = To<InlineFlowBox>(curr);
        logical_left += flow->MarginLogicalLeft();
        if (KnownToHaveNoOverflow())
          min_logical_left = std::min(logical_left, min_logical_left);
        logical_left =
            flow->PlaceBoxesInInlineDirection(logical_left, needs_word_spacing);
        if (KnownToHaveNoOverflow())
          max_logical_right = std::max(logical_left, max_logical_right);
        logical_left += flow->MarginLogicalRight();
      } else if (!curr->GetLineLayoutItem().IsListMarker() ||
                 LineLayoutListMarker(curr->GetLineLayoutItem()).IsInside()) {
        // The box can have a different writing-mode than the overall line, so
        // this is a bit complicated. Just get all the physical margin and
        // overflow values by hand based off |isHorizontal|.
        LineLayoutBoxModel box = curr->BoxModelObject();
        LayoutUnit logical_left_margin;
        LayoutUnit logical_right_margin;
        if (IsHorizontal()) {
          logical_left_margin = box.MarginLeft();
          logical_right_margin = box.MarginRight();
        } else {
          logical_left_margin = box.MarginTop();
          logical_right_margin = box.MarginBottom();
        }

        logical_left += logical_left_margin;
        curr->SetLogicalLeft(logical_left);
        if (KnownToHaveNoOverflow())
          min_logical_left = std::min(logical_left, min_logical_left);
        logical_left += curr->LogicalWidth();
        if (KnownToHaveNoOverflow())
          max_logical_right = std::max(logical_left, max_logical_right);
        logical_left += logical_right_margin;
        // If we encounter any space after this inline block then ensure it is
        // treated as the space between two words.
        needs_word_spacing = true;
      }
    }
  }
}

FontBaseline InlineFlowBox::DominantBaseline() const {
  // Use "central" (Ideographic) baseline if writing-mode is vertical-* and
  // text-orientation is not sideways-*.
  // https://drafts.csswg.org/css-writing-modes/#text-baselines
  if (!IsHorizontal() && GetLineLayoutItem()
                             .Style(IsFirstLineStyle())
                             ->GetFontDescription()
                             .IsVerticalAnyUpright())
    return kIdeographicBaseline;
  return kAlphabeticBaseline;
}

void InlineFlowBox::AdjustMaxAscentAndDescent(LayoutUnit& max_ascent,
                                              LayoutUnit& max_descent,
                                              int max_position_top,
                                              int max_position_bottom) {
  LayoutUnit original_max_ascent(max_ascent);
  LayoutUnit original_max_descent(max_descent);
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    // The computed lineheight needs to be extended for the
    // positioned elements
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.
    if (curr->VerticalAlign() == EVerticalAlign::kTop ||
        curr->VerticalAlign() == EVerticalAlign::kBottom) {
      int line_height = curr->LineHeight().Round();
      if (curr->VerticalAlign() == EVerticalAlign::kTop) {
        if (max_ascent + max_descent < line_height)
          max_descent = line_height - max_ascent;
      } else {
        if (max_ascent + max_descent < line_height)
          max_ascent = line_height - max_descent;
      }

      if (max_ascent + max_descent >=
          std::max(max_position_top, max_position_bottom))
        break;
      max_ascent = original_max_ascent;
      max_descent = original_max_descent;
    }

    if (curr->IsInlineFlowBox())
      To<InlineFlowBox>(curr)->AdjustMaxAscentAndDescent(
          max_ascent, max_descent, max_position_top, max_position_bottom);
  }
}

void InlineFlowBox::ComputeLogicalBoxHeights(
    RootInlineBox* root_box,
    LayoutUnit& max_position_top,
    LayoutUnit& max_position_bottom,
    LayoutUnit& max_ascent,
    LayoutUnit& max_descent,
    bool& set_max_ascent,
    bool& set_max_descent,
    bool no_quirks_mode,
    GlyphOverflowAndFallbackFontsMap& text_box_data_map,
    FontBaseline baseline_type,
    VerticalPositionCache& vertical_position_cache) {
  // The primary purpose of this function is to compute the maximal ascent and
  // descent values for a line.
  //
  // The maxAscent value represents the distance of the highest point of any box
  // (typically including line-height) from the root box's baseline. The
  // maxDescent value represents the distance of the lowest point of any box
  // (also typically including line-height) from the root box baseline. These
  // values can be negative.
  //
  // A secondary purpose of this function is to store the offset of every box's
  // baseline from the root box's baseline. This information is cached in the
  // logicalTop() of every box. We're effectively just using the logicalTop() as
  // scratch space.
  //
  // Because a box can be positioned such that it ends up fully above or fully
  // below the root line box, we only consider it to affect the maxAscent and
  // maxDescent values if some part of the box (EXCLUDING leading) is above (for
  // ascent) or below (for descent) the root box's baseline.
  bool affects_ascent = false;
  bool affects_descent = false;
  bool check_children = !DescendantsHaveSameLineHeightAndBaseline();

  DCHECK(root_box);
  if (!root_box)
    return;

  if (IsRootInlineBox()) {
    // Examine our root box.
    LayoutUnit ascent;
    LayoutUnit descent;
    root_box->AscentAndDescentForBox(root_box, text_box_data_map, ascent,
                                     descent, affects_ascent, affects_descent);
    if (no_quirks_mode || HasTextChildren() ||
        (!check_children && HasTextDescendants())) {
      if (max_ascent < ascent || !set_max_ascent) {
        max_ascent = ascent;
        set_max_ascent = true;
      }
      if (max_descent < descent || !set_max_descent) {
        max_descent = descent;
        set_max_descent = true;
      }
    }
  }

  if (!check_children)
    return;

  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    auto* inline_flow_box = DynamicTo<InlineFlowBox>(curr);

    bool child_affects_ascent = false;
    bool child_affects_descent = false;

    // The verticalPositionForBox function returns the distance between the
    // child box's baseline and the root box's baseline. The value is negative
    // if the child box's baseline is above the root box's baseline, and it is
    // positive if the child box's baseline is below the root box's baseline.
    DCHECK(root_box);
    curr->SetLogicalTop(
        root_box->VerticalPositionForBox(curr, vertical_position_cache));

    LayoutUnit ascent;
    LayoutUnit descent;
    root_box->AscentAndDescentForBox(curr, text_box_data_map, ascent, descent,
                                     child_affects_ascent,
                                     child_affects_descent);

    LayoutUnit box_height(ascent + descent);
    if (curr->VerticalAlign() == EVerticalAlign::kTop) {
      if (max_position_top < box_height)
        max_position_top = box_height;
    } else if (curr->VerticalAlign() == EVerticalAlign::kBottom) {
      if (max_position_bottom < box_height)
        max_position_bottom = box_height;
    } else if (!inline_flow_box || no_quirks_mode ||
               inline_flow_box->HasTextChildren() ||
               (inline_flow_box->DescendantsHaveSameLineHeightAndBaseline() &&
                inline_flow_box->HasTextDescendants()) ||
               inline_flow_box->BoxModelObject()
                   .HasInlineDirectionBordersOrPadding()) {
      // Note that these values can be negative. Even though we only affect the
      // maxAscent and maxDescent values if our box (excluding line-height) was
      // above (for ascent) or below (for descent) the root baseline, once you
      // factor in line-height the final box can end up being fully above or
      // fully below the root box's baseline! This is ok, but what it means is
      // that ascent and descent (including leading), can end up being negative.
      // The setMaxAscent and setMaxDescent booleans are used to ensure that
      // we're willing to initially set maxAscent/Descent to negative values.
      ascent -= curr->LogicalTop();
      descent += curr->LogicalTop();
      if (child_affects_ascent && (max_ascent < ascent || !set_max_ascent)) {
        max_ascent = ascent;
        set_max_ascent = true;
      }

      if (child_affects_descent &&
          (max_descent < descent || !set_max_descent)) {
        max_descent = descent;
        set_max_descent = true;
      }
    }

    if (inline_flow_box)
      inline_flow_box->ComputeLogicalBoxHeights(
          root_box, max_position_top, max_position_bottom, max_ascent,
          max_descent, set_max_ascent, set_max_descent, no_quirks_mode,
          text_box_data_map, baseline_type, vertical_position_cache);
  }
}

void InlineFlowBox::PlaceBoxesInBlockDirection(
    LayoutUnit top,
    LayoutUnit max_height,
    LayoutUnit max_ascent,
    bool no_quirks_mode,
    LayoutUnit& line_top,
    LayoutUnit& line_bottom,
    LayoutUnit& selection_bottom,
    bool& set_line_top,
    LayoutUnit& line_top_including_margins,
    LayoutUnit& line_bottom_including_margins,
    bool& has_annotations_before,
    bool& has_annotations_after,
    FontBaseline baseline_type) {
  bool is_root_box = IsRootInlineBox();
  if (is_root_box) {
    const SimpleFontData* font_data =
        GetLineLayoutItem().Style(IsFirstLineStyle())->GetFont().PrimaryFont();
    DCHECK(font_data);
    if (!font_data)
      return;
    const FontMetrics& font_metrics = font_data->GetFontMetrics();
    // RootInlineBoxes are always placed at pixel boundaries in their logical y
    // direction. Not doing so results in incorrect layout of text decorations,
    // most notably underlines.
    SetLogicalTop(top + max_ascent - font_metrics.Ascent(baseline_type));
  }

  LayoutUnit adjustment_for_children_with_same_line_height_and_baseline;
  if (DescendantsHaveSameLineHeightAndBaseline()) {
    adjustment_for_children_with_same_line_height_and_baseline = LogicalTop();
    if (Parent())
      adjustment_for_children_with_same_line_height_and_baseline +=
          BoxModelObject().BorderAndPaddingOver();
  }

  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    if (DescendantsHaveSameLineHeightAndBaseline()) {
      curr->MoveInBlockDirection(
          adjustment_for_children_with_same_line_height_and_baseline);
      continue;
    }

    auto* inline_flow_box = DynamicTo<InlineFlowBox>(curr);
    bool child_affects_top_bottom_pos = true;
    if (curr->VerticalAlign() == EVerticalAlign::kTop) {
      curr->SetLogicalTop(top);
    } else if (curr->VerticalAlign() == EVerticalAlign::kBottom) {
      curr->SetLogicalTop((top + max_height - curr->LineHeight()));
    } else {
      if (!no_quirks_mode && inline_flow_box &&
          !inline_flow_box->HasTextChildren() &&
          !curr->BoxModelObject().HasInlineDirectionBordersOrPadding() &&
          !(inline_flow_box->DescendantsHaveSameLineHeightAndBaseline() &&
            inline_flow_box->HasTextDescendants()))
        child_affects_top_bottom_pos = false;
      LayoutUnit pos_adjust =
          max_ascent - curr->BaselinePosition(baseline_type);
      curr->SetLogicalTop(curr->LogicalTop() + top + pos_adjust);
    }

    LayoutUnit new_logical_top = curr->LogicalTop();
    LayoutUnit new_logical_top_including_margins = new_logical_top;
    LayoutUnit box_height = curr->LogicalHeight();
    LayoutUnit box_height_including_margins = box_height;
    LayoutUnit border_padding_height;
    if (curr->IsText() || curr->IsInlineFlowBox()) {
      const SimpleFontData* font_data = curr->GetLineLayoutItem()
                                            .Style(IsFirstLineStyle())
                                            ->GetFont()
                                            .PrimaryFont();
      DCHECK(font_data);
      if (!font_data)
        continue;

      const FontMetrics& font_metrics = font_data->GetFontMetrics();
      new_logical_top += curr->BaselinePosition(baseline_type) -
                         font_metrics.Ascent(baseline_type);
      if (curr->IsInlineFlowBox()) {
        LineLayoutBoxModel box_object =
            LineLayoutBoxModel(curr->GetLineLayoutItem());
        new_logical_top -= box_object.BorderAndPaddingOver();
        border_padding_height = box_object.BorderAndPaddingLogicalHeight();
      }
      new_logical_top_including_margins = new_logical_top;
    } else if (!curr->GetLineLayoutItem().IsBR()) {
      LineLayoutBox box = LineLayoutBox(curr->GetLineLayoutItem());
      new_logical_top_including_margins = new_logical_top;
      // TODO(kojii): isHorizontal() does not match to
      // m_layoutObject.isHorizontalWritingMode(). crbug.com/552954
      // DCHECK_EQ(curr->isHorizontal(),
      // curr->getLineLayoutItem().style()->isHorizontalWritingMode());
      // We may flip lines in case of verticalLR mode, so we can
      // assume verticalRL for now.
      LayoutUnit over_side_margin =
          curr->IsHorizontal() ? box.MarginTop() : box.MarginRight();
      LayoutUnit under_side_margin =
          curr->IsHorizontal() ? box.MarginBottom() : box.MarginLeft();
      new_logical_top += over_side_margin;
      box_height_including_margins += over_side_margin + under_side_margin;
    }

    curr->SetLogicalTop(new_logical_top);

    if (child_affects_top_bottom_pos) {
      if (curr->GetLineLayoutItem().IsRubyRun()) {
        // Treat the leading on the first and last lines of ruby runs as not
        // being part of the overall lineTop/lineBottom.
        // Really this is a workaround hack for the fact that ruby should have
        // been done as line layout and not done using inline-block.
        RubyPosition block_start_position =
            GetLineLayoutItem().StyleRef().IsFlippedLinesWritingMode()
                ? RubyPosition::kAfter
                : RubyPosition::kBefore;
        if (curr->GetLineLayoutItem().StyleRef().GetRubyPosition() ==
            block_start_position)
          has_annotations_before = true;
        else
          has_annotations_after = true;

        LineLayoutRubyRun ruby_run =
            LineLayoutRubyRun(curr->GetLineLayoutItem());
        if (LineLayoutRubyBase ruby_base = ruby_run.RubyBase()) {
          LayoutUnit bottom_ruby_base_leading =
              (curr->LogicalHeight() - ruby_base.LogicalBottom()) +
              ruby_base.LogicalHeight() -
              (ruby_base.LastRootBox() ? ruby_base.LastRootBox()->LineBottom()
                                       : LayoutUnit());
          LayoutUnit top_ruby_base_leading =
              ruby_base.LogicalTop() +
              (ruby_base.FirstRootBox() ? ruby_base.FirstRootBox()->LineTop()
                                        : LayoutUnit());
          new_logical_top +=
              !GetLineLayoutItem().StyleRef().IsFlippedLinesWritingMode()
                  ? top_ruby_base_leading
                  : bottom_ruby_base_leading;
          box_height -= (top_ruby_base_leading + bottom_ruby_base_leading);
        }
      }
      if (curr->IsInlineTextBox()) {
        TextEmphasisPosition emphasis_mark_position;
        if (To<InlineTextBox>(curr)->GetEmphasisMarkPosition(
                curr->GetLineLayoutItem().StyleRef(IsFirstLineStyle()),
                emphasis_mark_position)) {
          if (HasEmphasisMarkBefore(To<InlineTextBox>(curr)))
            has_annotations_before = true;
          else
            has_annotations_after = true;
        }
      }

      if (!set_line_top) {
        set_line_top = true;
        line_top = new_logical_top;
        line_top_including_margins =
            std::min(line_top, new_logical_top_including_margins);
      } else {
        line_top = std::min(line_top, new_logical_top);
        line_top_including_margins =
            std::min(line_top, std::min(line_top_including_margins,
                                        new_logical_top_including_margins));
      }
      selection_bottom =
          std::max(selection_bottom,
                   new_logical_top + box_height - border_padding_height);
      line_bottom = std::max(line_bottom, new_logical_top + box_height);
      line_bottom_including_margins =
          std::max(line_bottom, std::max(line_bottom_including_margins,
                                         new_logical_top_including_margins +
                                             box_height_including_margins));
    }

    // Adjust boxes to use their real box y/height and not the logical height
    // (as dictated by line-height).
    if (inline_flow_box)
      inline_flow_box->PlaceBoxesInBlockDirection(
          top, max_height, max_ascent, no_quirks_mode, line_top, line_bottom,
          selection_bottom, set_line_top, line_top_including_margins,
          line_bottom_including_margins, has_annotations_before,
          has_annotations_after, baseline_type);
  }

  if (is_root_box) {
    if (no_quirks_mode || HasTextChildren() ||
        (DescendantsHaveSameLineHeightAndBaseline() && HasTextDescendants())) {
      if (!set_line_top) {
        set_line_top = true;
        line_top = LogicalTop();
        line_top_including_margins = line_top;
      } else {
        line_top = std::min(line_top, LogicalTop());
        line_top_including_margins =
            std::min(line_top, line_top_including_margins);
      }
      selection_bottom = std::max(selection_bottom, LogicalBottom());
      line_bottom = std::max(line_bottom, LogicalBottom());
      line_bottom_including_margins =
          std::max(line_bottom, line_bottom_including_margins);
    }

    if (GetLineLayoutItem().StyleRef().IsFlippedLinesWritingMode())
      FlipLinesInBlockDirection(line_top_including_margins,
                                line_bottom_including_margins);
  }
}

bool InlineFlowBox::OverrideVisualOverflowFromLogicalRect(
    const LayoutRect& logical_visual_overflow,
    LayoutUnit line_top,
    LayoutUnit line_bottom) {
  // If we are setting an overflow, then we can't pretend not to have an
  // overflow.
  ClearKnownToHaveNoOverflow();
  LayoutRect old_visual_overflow_rect =
      VisualOverflowRect(line_top, line_bottom);
  SetVisualOverflowFromLogicalRect(logical_visual_overflow, line_top,
                                   line_bottom);
  return VisualOverflowRect(line_top, line_bottom) != old_visual_overflow_rect;
}

void InlineFlowBox::OverrideLayoutOverflowFromLogicalRect(
    const LayoutRect& logical_layout_overflow,
    LayoutUnit line_top,
    LayoutUnit line_bottom) {
  // If we are setting an overflow, then we can't pretend not to have an
  // overflow.
  ClearKnownToHaveNoOverflow();
  SetLayoutOverflowFromLogicalRect(logical_layout_overflow, line_top,
                                   line_bottom);
}

LayoutUnit InlineFlowBox::FarthestPositionForUnderline(
    LineLayoutItem decorating_box,
    FontVerticalPositionType position_type,
    FontBaseline baseline_type,
    LayoutUnit farthest) const {
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    // If the text decoration isn't in effect on the child, it must be outside
    // of |decorationObject|.
    if (!EnumHasFlags(curr->LineStyleRef().TextDecorationsInEffect(),
                      TextDecoration::kUnderline))
      continue;

    if (decorating_box && decorating_box.IsLayoutInline() &&
        !IsAncestorAndWithinBlock(decorating_box, curr->GetLineLayoutItem()))
      continue;

    if (curr->IsInlineFlowBox()) {
      farthest = To<InlineFlowBox>(curr)->FarthestPositionForUnderline(
          decorating_box, position_type, baseline_type, farthest);
    } else if (curr->IsInlineTextBox()) {
      LayoutUnit position = To<InlineTextBox>(curr)->VerticalPosition(
          position_type, baseline_type);
      if (IsLineOverSide(position_type))
        farthest = std::min(farthest, position);
      else
        farthest = std::max(farthest, position);
    }
  }
  return farthest;
}

void InlineFlowBox::FlipLinesInBlockDirection(LayoutUnit line_top,
                                              LayoutUnit line_bottom) {
  // Flip the box on the line such that the top is now relative to the
  // lineBottom instead of the lineTop.
  SetLogicalTop(line_bottom - (LogicalTop() - line_top) - LogicalHeight());

  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders aren't affected here.

    if (curr->IsInlineFlowBox())
      To<InlineFlowBox>(curr)->FlipLinesInBlockDirection(line_top, line_bottom);
    else
      curr->SetLogicalTop(line_bottom - (curr->LogicalTop() - line_top) -
                          curr->LogicalHeight());
  }
}

inline void InlineFlowBox::AddBoxShadowVisualOverflow(
    LayoutRect& logical_visual_overflow) {
  const ComputedStyle& style = GetLineLayoutItem().StyleRef(IsFirstLineStyle());

  // box-shadow on the block element applies to the block and not to the lines,
  // unless it is modified by :first-line pseudo element.
  if (!Parent() &&
      (!IsFirstLineStyle() || &style == GetLineLayoutItem().Style()))
    return;

  WritingMode writing_mode = style.GetWritingMode();
  ShadowList* box_shadow = style.BoxShadow();
  if (!box_shadow)
    return;

  LayoutRectOutsets outsets(box_shadow->RectOutsetsIncludingOriginal());
  // Similar to how glyph overflow works, if our lines are flipped, then it's
  // actually the opposite shadow that applies, since the line is "upside down"
  // in terms of block coordinates.
  LayoutRectOutsets logical_outsets =
      LineOrientationLayoutRectOutsetsWithFlippedLines(outsets, writing_mode);

  LayoutRect shadow_bounds(LogicalFrameRect());
  shadow_bounds.Expand(logical_outsets);
  logical_visual_overflow.Unite(shadow_bounds);
}

inline void InlineFlowBox::AddBorderOutsetVisualOverflow(
    LayoutRect& logical_visual_overflow) {
  const ComputedStyle& style = GetLineLayoutItem().StyleRef(IsFirstLineStyle());

  // border-image-outset on the block element applies to the block and not to
  // the lines, unless it is modified by :first-line pseudo element.
  if (!Parent() &&
      (!IsFirstLineStyle() || &style == GetLineLayoutItem().Style()))
    return;

  if (!style.HasBorderImageOutsets())
    return;

  // Similar to how glyph overflow works, if our lines are flipped, then it's
  // actually the opposite border that applies, since the line is "upside down"
  // in terms of block coordinates. vertical-rl is the flipped line mode.
  LayoutRectOutsets logical_outsets =
      LineOrientationLayoutRectOutsetsWithFlippedLines(
          style.BorderImageOutsets(), style.GetWritingMode());

  if (!IncludeLogicalLeftEdge())
    logical_outsets.SetLeft(LayoutUnit());
  if (!IncludeLogicalRightEdge())
    logical_outsets.SetRight(LayoutUnit());

  LayoutRect border_outset_bounds(LogicalFrameRect());
  border_outset_bounds.Expand(logical_outsets);
  logical_visual_overflow.Unite(border_outset_bounds);
}

inline void InlineFlowBox::AddOutlineVisualOverflow(
    LayoutRect& logical_visual_overflow) {
  // Outline on root line boxes is applied to the block and not to the lines.
  if (!Parent())
    return;

  const ComputedStyle& style = GetLineLayoutItem().StyleRef(IsFirstLineStyle());
  if (!style.HasOutline())
    return;

  logical_visual_overflow.Inflate(style.OutlineOutsetExtent());
}

inline void InlineFlowBox::AddTextBoxVisualOverflow(
    InlineTextBox* text_box,
    GlyphOverflowAndFallbackFontsMap& text_box_data_map,
    LayoutRect& logical_visual_overflow) {
  if (text_box->KnownToHaveNoOverflow())
    return;

  LayoutRectOutsets visual_rect_outsets;
  const ComputedStyle& style =
      text_box->GetLineLayoutItem().StyleRef(IsFirstLineStyle());

  GlyphOverflowAndFallbackFontsMap::iterator it =
      text_box_data_map.find(text_box);
  if (it != text_box_data_map.end()) {
    const GlyphOverflow& glyph_overflow = it->value.second;
    bool is_flipped_line = style.IsFlippedLinesWritingMode();
    visual_rect_outsets = EnclosingLayoutRectOutsets(FloatRectOutsets(
        is_flipped_line ? glyph_overflow.bottom : glyph_overflow.top,
        glyph_overflow.right,
        is_flipped_line ? glyph_overflow.top : glyph_overflow.bottom,
        glyph_overflow.left));
  }

  if (float stroke_width = style.TextStrokeWidth()) {
    visual_rect_outsets += LayoutUnit::FromFloatCeil(stroke_width / 2.0f);
  }

  TextEmphasisPosition emphasis_mark_position;
  if (style.GetTextEmphasisMark() != TextEmphasisMark::kNone &&
      text_box->GetEmphasisMarkPosition(style, emphasis_mark_position)) {
    LayoutUnit emphasis_mark_height = LayoutUnit(
        style.GetFont().EmphasisMarkHeight(style.TextEmphasisMarkString()));
    if (HasEmphasisMarkBefore(text_box)) {
      visual_rect_outsets.SetTop(
          std::max(visual_rect_outsets.Top(), emphasis_mark_height));
    } else {
      visual_rect_outsets.SetBottom(
          std::max(visual_rect_outsets.Bottom(), emphasis_mark_height));
    }
  }

  if (ShadowList* text_shadow = style.TextShadow()) {
    LayoutRectOutsets text_shadow_logical_outsets =
        LineOrientationLayoutRectOutsets(
            LayoutRectOutsets(text_shadow->RectOutsetsIncludingOriginal()),
            style.GetWritingMode());
    text_shadow_logical_outsets.ClampNegativeToZero();
    visual_rect_outsets += text_shadow_logical_outsets;
  }

  LayoutRect frame_rect = text_box->LogicalFrameRect();
  frame_rect.Expand(visual_rect_outsets);
  frame_rect = LayoutRect(EnclosingIntRect(frame_rect));
  logical_visual_overflow.Unite(frame_rect);

  if (logical_visual_overflow != text_box->LogicalFrameRect())
    text_box->SetLogicalOverflowRect(logical_visual_overflow);
}

inline void InlineFlowBox::AddReplacedChildLayoutOverflow(
    const InlineBox* inline_box,
    LayoutRect& logical_layout_overflow) {
  LineLayoutBox box = LineLayoutBox(inline_box->GetLineLayoutItem());

  // Layout overflow internal to the child box only propagates if the child box
  // doesn't have overflow clip set. Otherwise the child border box propagates
  // as layout overflow. This rectangle must include transforms and relative
  // positioning and be adjusted for writing-mode differences.
  LayoutRect child_logical_layout_overflow =
      box.LogicalLayoutOverflowRectForPropagation();
  child_logical_layout_overflow.Move(inline_box->LogicalLeft(),
                                     inline_box->LogicalTop());
  logical_layout_overflow.Unite(child_logical_layout_overflow);
}

void InlineFlowBox::AddReplacedChildrenVisualOverflow(LayoutUnit line_top,
                                                      LayoutUnit line_bottom) {
  LayoutRect logical_visual_overflow =
      LogicalVisualOverflowRect(line_top, line_bottom);
  bool visual_overflow_may_have_changed = false;
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    const LineLayoutItem& item = curr->GetLineLayoutItem();
    if (item.IsOutOfFlowPositioned() || item.IsText())
      continue;

    if (item.IsLayoutInline()) {
      auto* flow = To<InlineFlowBox>(curr);
      flow->AddReplacedChildrenVisualOverflow(line_top, line_bottom);
      // Propagate visual overflow only if it may be present.
      if (!KnownToHaveNoOverflow()) {
        if (!flow->BoxModelObject().HasSelfPaintingLayer()) {
          logical_visual_overflow.Unite(
              flow->LogicalVisualOverflowRect(line_top, line_bottom));
          visual_overflow_may_have_changed = true;
        }
      }

      continue;
    }

    LineLayoutBox box = LineLayoutBox(curr->GetLineLayoutItem());

    // Visual overflow only propagates if the box doesn't have a self-painting
    // layer. This rectangle does not include transforms or relative positioning
    // (since those objects always have self-painting layers), but it does need
    // to be adjusted for writing-mode differences.
    if (!box.HasSelfPaintingLayer()) {
      LayoutRect child_logical_visual_overflow =
          box.LogicalVisualOverflowRectForPropagation();
      child_logical_visual_overflow.Move(curr->LogicalLeft(),
                                         curr->LogicalTop());
      logical_visual_overflow.Unite(child_logical_visual_overflow);
      ClearKnownToHaveNoOverflow();
      visual_overflow_may_have_changed = true;
    }
  }
  if (visual_overflow_may_have_changed) {
    SetVisualOverflowFromLogicalRect(logical_visual_overflow, line_top,
                                     line_bottom);
  }
}

static void ComputeGlyphOverflow(
    InlineTextBox* text,
    const LineLayoutText& layout_text,
    GlyphOverflowAndFallbackFontsMap& text_box_data_map) {
  HashSet<const SimpleFontData*> fallback_fonts;
  FloatRect glyph_bounds;
  GlyphOverflow glyph_overflow;
  float measured_width = layout_text.Width(
      text->Start(), text->Len(), LayoutUnit(), text->Direction(), false,
      &fallback_fonts, &glyph_bounds);
  const Font& font = layout_text.StyleRef().GetFont();
  glyph_overflow.SetFromBounds(glyph_bounds, font, measured_width);
  if (!fallback_fonts.IsEmpty()) {
    GlyphOverflowAndFallbackFontsMap::ValueType* it =
        text_box_data_map
            .insert(text, std::make_pair(Vector<const SimpleFontData*>(),
                                         GlyphOverflow()))
            .stored_value;
    DCHECK(it->value.first.IsEmpty());
    CopyToVector(fallback_fonts, it->value.first);
  }
  if (!glyph_overflow.IsApproximatelyZero()) {
    GlyphOverflowAndFallbackFontsMap::ValueType* it =
        text_box_data_map
            .insert(text, std::make_pair(Vector<const SimpleFontData*>(),
                                         GlyphOverflow()))
            .stored_value;
    it->value.second = glyph_overflow;
  }
}

void InlineFlowBox::ComputeOverflow(
    LayoutUnit line_top,
    LayoutUnit line_bottom,
    GlyphOverflowAndFallbackFontsMap& text_box_data_map) {
  // If we know we have no overflow, we can just bail.
  if (KnownToHaveNoOverflow()) {
    DCHECK(!LayoutOverflowIsSet() && !VisualOverflowIsSet());
    return;
  }

  overflow_.reset();

  // Visual overflow just includes overflow for stuff we need to issues paint
  // invalidations for ourselves. Self-painting layers are ignored.
  // Layout overflow is used to determine scrolling extent, so it still includes
  // child layers and also factors in transforms, relative positioning, etc.
  LayoutRect logical_layout_overflow;
  LayoutRect logical_visual_overflow(
      LogicalFrameRectIncludingLineHeight(line_top, line_bottom));

  AddBoxShadowVisualOverflow(logical_visual_overflow);
  AddBorderOutsetVisualOverflow(logical_visual_overflow);
  AddOutlineVisualOverflow(logical_visual_overflow);

  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    if (curr->GetLineLayoutItem().IsText()) {
      auto* text = To<InlineTextBox>(curr);
      LayoutRect text_box_overflow(text->LogicalFrameRect());
      if (text->IsLineBreak()) {
        text_box_overflow.SetWidth(
            LayoutUnit(text_box_overflow.Width() + text->NewlineSpaceWidth()));
      } else {
        if (text_box_data_map.IsEmpty()) {
          // An empty glyph map means that we're computing overflow without
          // a layout, so calculate the glyph overflow on the fly.
          GlyphOverflowAndFallbackFontsMap glyph_overflow_for_text;
          ComputeGlyphOverflow(text, text->GetLineLayoutItem(),
                               glyph_overflow_for_text);
          AddTextBoxVisualOverflow(text, glyph_overflow_for_text,
                                   text_box_overflow);
        } else {
          AddTextBoxVisualOverflow(text, text_box_data_map, text_box_overflow);
        }
      }
      logical_visual_overflow.Unite(text_box_overflow);
    } else if (curr->GetLineLayoutItem().IsLayoutInline()) {
      auto* flow = To<InlineFlowBox>(curr);
      flow->ComputeOverflow(line_top, line_bottom, text_box_data_map);
      if (!flow->BoxModelObject().HasSelfPaintingLayer())
        logical_visual_overflow.Unite(
            flow->LogicalVisualOverflowRect(line_top, line_bottom));
      LayoutRect child_layout_overflow =
          flow->LogicalLayoutOverflowRect(line_top, line_bottom);
      child_layout_overflow.Unite(
          LogicalFrameRectIncludingLineHeight(line_top, line_bottom));
      if (flow->BoxModelObject().IsRelPositioned()) {
        child_layout_overflow.Move(
            flow->BoxModelObject().RelativePositionLogicalOffset());
      }
      logical_layout_overflow.Unite(child_layout_overflow);
    } else {
      if (logical_layout_overflow.IsEmpty()) {
        logical_layout_overflow =
            LogicalFrameRectIncludingLineHeight(line_top, line_bottom);
      }
      AddReplacedChildLayoutOverflow(curr, logical_layout_overflow);
    }
  }

  SetLayoutOverflowFromLogicalRect(logical_layout_overflow, line_top,
                                   line_bottom);
  SetVisualOverflowFromLogicalRect(logical_visual_overflow, line_top,
                                   line_bottom);
}

void InlineFlowBox::SetLayoutOverflow(const LayoutRect& rect,
                                      const LayoutRect& frame_box) {
  DCHECK(!KnownToHaveNoOverflow());
  if (frame_box.Contains(rect) || rect.IsEmpty())
    return;

  if (!LayoutOverflowIsSet()) {
    if (!overflow_)
      overflow_ = std::make_unique<SimpleOverflowModel>();
    overflow_->layout_overflow.emplace(frame_box);
  }

  overflow_->layout_overflow->SetLayoutOverflow(rect);
}

void InlineFlowBox::SetVisualOverflow(const LayoutRect& rect,
                                      const LayoutRect& frame_box) {
  DCHECK(!KnownToHaveNoOverflow());
  if (frame_box.Contains(rect) || rect.IsEmpty())
    return;

  if (!VisualOverflowIsSet()) {
    if (!overflow_)
      overflow_ = std::make_unique<SimpleOverflowModel>();
    overflow_->visual_overflow.emplace(frame_box);
  }

  overflow_->visual_overflow->SetVisualOverflow(rect);
}

void InlineFlowBox::SetVisualOverflowFromLogicalRect(
    const LayoutRect& logical_visual_overflow,
    LayoutUnit line_top,
    LayoutUnit line_bottom) {
  DCHECK(!KnownToHaveNoOverflow());
  LayoutRect frame_box = FrameRectIncludingLineHeight(line_top, line_bottom);
  LayoutRect visual_overflow(IsHorizontal()
                                 ? logical_visual_overflow
                                 : logical_visual_overflow.TransposedRect());
  SetVisualOverflow(visual_overflow, frame_box);
}

void InlineFlowBox::SetLayoutOverflowFromLogicalRect(
    const LayoutRect& logical_layout_overflow,
    LayoutUnit line_top,
    LayoutUnit line_bottom) {
  DCHECK(!KnownToHaveNoOverflow());
  LayoutRect frame_box = FrameRectIncludingLineHeight(line_top, line_bottom);

  LayoutRect layout_overflow(IsHorizontal()
                                 ? logical_layout_overflow
                                 : logical_layout_overflow.TransposedRect());
  SetLayoutOverflow(layout_overflow, frame_box);
}

bool InlineFlowBox::NodeAtPoint(HitTestResult& result,
                                const HitTestLocation& hit_test_location,
                                const PhysicalOffset& accumulated_offset,
                                LayoutUnit line_top,
                                LayoutUnit line_bottom) {
  PhysicalRect overflow_rect =
      PhysicalVisualOverflowRect(line_top, line_bottom);
  overflow_rect.Move(accumulated_offset);
  if (!hit_test_location.Intersects(overflow_rect))
    return false;

  // We need to hit test both our inline children (Inline Boxes) and culled
  // inlines (LayoutObjects). We check our inlines in the same order as line
  // layout but for each inline we additionally need to hit test its culled
  // inline parents. While hit testing culled inline parents, we can stop once
  // we reach a non-inline parent or a culled inline associated with a different
  // inline box.
  InlineBox* prev;
  for (InlineBox* curr = LastChild(); curr; curr = prev) {
    prev = curr->PrevOnLine();

    // Layers will handle hit testing themselves.
    if (!curr->BoxModelObject() ||
        !curr->BoxModelObject().HasSelfPaintingLayer()) {
      if (curr->NodeAtPoint(result, hit_test_location, accumulated_offset,
                            line_top, line_bottom)) {
        GetLineLayoutItem().UpdateHitTestResult(
            result, hit_test_location.Point() - accumulated_offset);
        return true;
      }
    }

    // If the current inline box's layout object and the previous inline box's
    // layout object are same, we should yield the hit-test to the previous
    // inline box.
    if (prev && curr->GetLineLayoutItem() == prev->GetLineLayoutItem())
      continue;

    // Hit test the culled inline if necessary.
    LineLayoutItem curr_layout_item = curr->GetLineLayoutItem();
    while (true) {
      // If the previous inline box is not a descendant of a current inline's
      // parent, the parent is a culled inline and we hit test it.
      // Otherwise, move to the previous inline box because we hit test first
      // all candidate inline boxes under the parent to take a pre-order tree
      // traversal in reverse.
      bool has_sibling =
          curr_layout_item.PreviousSibling() || curr_layout_item.NextSibling();
      LineLayoutItem culled_parent = curr_layout_item.Parent();
      DCHECK(culled_parent);

      if (culled_parent == GetLineLayoutItem() ||
          (has_sibling && prev &&
           prev->GetLineLayoutItem().IsDescendantOf(culled_parent)))
        break;

      if (culled_parent.IsLayoutInline() &&
          LineLayoutInline(culled_parent)
              .HitTestCulledInline(result, hit_test_location,
                                   accumulated_offset))
        return true;

      curr_layout_item = culled_parent;
    }
  }

  if (GetLineLayoutItem().IsBox() &&
      To<LayoutBox>(LineLayoutAPIShim::LayoutObjectFrom(GetLineLayoutItem()))
          ->HitTestClippedOutByBorder(hit_test_location, overflow_rect.offset))
    return false;

  if (GetLineLayoutItem().StyleRef().HasBorderRadius()) {
    // TODO(layout-dev): LogicalFrameRect() seems incorrect.
    PhysicalRect border_rect = PhysicalRectToBeNoop(LogicalFrameRect());
    border_rect.Move(accumulated_offset);
    FloatRoundedRect border = RoundedBorderGeometry::PixelSnappedRoundedBorder(
        GetLineLayoutItem().StyleRef(), border_rect, SidesToInclude());
    if (!hit_test_location.Intersects(border))
      return false;
  }

  // Now check ourselves.
  LayoutRect layout_rect =
      InlineFlowBoxPainter(*this).FrameRectClampedToLineTopAndBottomIfNeeded();
  FlipForWritingMode(layout_rect);
  PhysicalRect rect(layout_rect);
  rect.Move(accumulated_offset);

  // Pixel snap hit testing.
  rect = PhysicalRect(PixelSnappedIntRect(rect));
  if (VisibleToHitTestRequest(result.GetHitTestRequest()) &&
      hit_test_location.Intersects(rect)) {
    // Don't add in m_topLeft here, we want coords in the containing block's
    // coordinate space.
    GetLineLayoutItem().UpdateHitTestResult(
        result, hit_test_location.Point() - accumulated_offset);
    if (result.AddNodeToListBasedTestResult(GetLineLayoutItem().GetNode(),
                                            hit_test_location,
                                            rect) == kStopHitTesting)
      return true;
  }

  return false;
}

void InlineFlowBox::Paint(const PaintInfo& paint_info,
                          const PhysicalOffset& paint_offset,
                          LayoutUnit line_top,
                          LayoutUnit line_bottom) const {
  InlineFlowBoxPainter(*this).Paint(paint_info, paint_offset, line_top,
                                    line_bottom);
}

bool InlineFlowBox::BoxShadowCanBeAppliedToBackground(
    const FillLayer& last_background_layer) const {
  // The checks here match how paintFillLayer() decides whether to clip (if it
  // does, the shadow
  // would be clipped out, so it has to be drawn separately).
  StyleImage* image = last_background_layer.GetImage();
  bool has_fill_image = image && image->CanRender();
  return (!has_fill_image &&
          !GetLineLayoutItem().StyleRef().HasBorderRadius()) ||
         (!PrevForSameLayoutObject() && !NextForSameLayoutObject()) ||
         !Parent();
}

InlineBox* InlineFlowBox::FirstLeafChild() const {
  InlineBox* leaf = nullptr;
  for (InlineBox* child = FirstChild(); child && !leaf;
       child = child->NextOnLine())
    leaf = child->IsLeaf() ? child : To<InlineFlowBox>(child)->FirstLeafChild();
  return leaf;
}

InlineBox* InlineFlowBox::LastLeafChild() const {
  InlineBox* leaf = nullptr;
  for (InlineBox* child = LastChild(); child && !leaf;
       child = child->PrevOnLine())
    leaf = child->IsLeaf() ? child : To<InlineFlowBox>(child)->LastLeafChild();
  return leaf;
}

bool InlineFlowBox::CanAccommodateEllipsis(bool ltr,
                                           LayoutUnit block_edge,
                                           LayoutUnit ellipsis_width) const {
  for (InlineBox* box = FirstChild(); box; box = box->NextOnLine()) {
    if (!box->CanAccommodateEllipsis(ltr, block_edge, ellipsis_width))
      return false;
  }
  return true;
}

LayoutUnit InlineFlowBox::PlaceEllipsisBox(bool ltr,
                                           LayoutUnit block_left_edge,
                                           LayoutUnit block_right_edge,
                                           LayoutUnit ellipsis_width,
                                           LayoutUnit& truncated_width,
                                           InlineBox** found_box,
                                           LayoutUnit logical_left_offset) {
  LayoutUnit result(-1);
  // We iterate over all children, the foundBox variable tells us when we've
  // found the box containing the ellipsis.  All boxes after that one in the
  // flow are hidden.
  // If our flow is ltr then iterate over the boxes from left to right,
  // otherwise iterate from right to left. Varying the order allows us to
  // correctly hide the boxes following the ellipsis.
  InlineBox* box = ltr ? FirstChild() : LastChild();

  // NOTE: these will cross after foundBox = true.
  LayoutUnit visible_left_edge = block_left_edge;
  LayoutUnit visible_right_edge = block_right_edge;

  while (box) {
    bool had_found_box = *found_box;
    LayoutUnit curr_result = box->PlaceEllipsisBox(
        ltr, visible_left_edge, visible_right_edge, ellipsis_width,
        truncated_width, found_box, logical_left_offset);
    if (IsRootInlineBox() && *found_box && !had_found_box)
      *found_box = box;
    if (curr_result != -1 && result == -1)
      result = curr_result;

    // List markers will sit outside the box so don't let them contribute
    // width.
    LayoutUnit box_width = box->GetLineLayoutItem().IsListMarker()
                               ? LayoutUnit()
                               : box->LogicalWidth();
    if (ltr) {
      visible_left_edge += box_width;
      box = box->NextOnLine();
    } else {
      visible_right_edge -= box_width;
      box = box->PrevOnLine();
    }
  }
  return result;
}

void InlineFlowBox::ClearTruncation() {
  for (InlineBox* box = FirstChild(); box; box = box->NextOnLine())
    box->ClearTruncation();
}

LayoutUnit InlineFlowBox::ComputeOverAnnotationAdjustment(
    LayoutUnit allowed_position) const {
  LayoutUnit result;
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    if (curr->IsInlineFlowBox()) {
      result = std::max(
          result, To<InlineFlowBox>(curr)->ComputeOverAnnotationAdjustment(
                      allowed_position));
    }

    if (curr->GetLineLayoutItem().IsAtomicInlineLevel() &&
        curr->GetLineLayoutItem().IsRubyRun() &&
        curr->GetLineLayoutItem().StyleRef().GetRubyPosition() ==
            RubyPosition::kBefore) {
      LineLayoutRubyRun ruby_run = LineLayoutRubyRun(curr->GetLineLayoutItem());
      LineLayoutRubyText ruby_text = ruby_run.RubyText();
      if (!ruby_text)
        continue;

      if (!ruby_run.StyleRef().IsFlippedLinesWritingMode()) {
        LayoutUnit top_of_first_ruby_text_line =
            ruby_text.LogicalTop() + (ruby_text.FirstRootBox()
                                          ? ruby_text.FirstRootBox()->LineTop()
                                          : LayoutUnit());
        if (top_of_first_ruby_text_line >= 0)
          continue;
        top_of_first_ruby_text_line += curr->LogicalTop();
        result =
            std::max(result, allowed_position - top_of_first_ruby_text_line);
      } else {
        LayoutUnit bottom_of_last_ruby_text_line =
            ruby_text.LogicalTop() +
            (ruby_text.LastRootBox() ? ruby_text.LastRootBox()->LineBottom()
                                     : ruby_text.LogicalHeight());
        if (bottom_of_last_ruby_text_line <= curr->LogicalHeight())
          continue;
        bottom_of_last_ruby_text_line += curr->LogicalTop();
        result =
            std::max(result, bottom_of_last_ruby_text_line - allowed_position);
      }
    }

    if (curr->IsInlineTextBox()) {
      const ComputedStyle& style =
          curr->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
      TextEmphasisPosition emphasis_mark_position;
      if (style.GetTextEmphasisMark() != TextEmphasisMark::kNone &&
          To<InlineTextBox>(curr)->GetEmphasisMarkPosition(
              style, emphasis_mark_position) &&
          HasEmphasisMarkOver(To<InlineTextBox>(curr))) {
        if (!style.IsFlippedLinesWritingMode()) {
          int top_of_emphasis_mark =
              (curr->LogicalTop() - style.GetFont().EmphasisMarkHeight(
                                        style.TextEmphasisMarkString()))
                  .ToInt();
          result = std::max(result, allowed_position - top_of_emphasis_mark);
        } else {
          int bottom_of_emphasis_mark =
              (curr->LogicalBottom() + style.GetFont().EmphasisMarkHeight(
                                           style.TextEmphasisMarkString()))
                  .ToInt();
          result = std::max(result, bottom_of_emphasis_mark - allowed_position);
        }
      }
    }
  }
  return result;
}

LayoutUnit InlineFlowBox::ComputeUnderAnnotationAdjustment(
    LayoutUnit allowed_position) const {
  LayoutUnit result;
  for (InlineBox* curr = FirstChild(); curr; curr = curr->NextOnLine()) {
    if (curr->GetLineLayoutItem().IsOutOfFlowPositioned())
      continue;  // Positioned placeholders don't affect calculations.

    if (curr->IsInlineFlowBox()) {
      result = std::max(
          result, To<InlineFlowBox>(curr)->ComputeUnderAnnotationAdjustment(
                      allowed_position));
    }

    if (curr->GetLineLayoutItem().IsAtomicInlineLevel() &&
        curr->GetLineLayoutItem().IsRubyRun() &&
        curr->GetLineLayoutItem().StyleRef().GetRubyPosition() ==
            RubyPosition::kAfter) {
      LineLayoutRubyRun ruby_run = LineLayoutRubyRun(curr->GetLineLayoutItem());
      LineLayoutRubyText ruby_text = ruby_run.RubyText();
      if (!ruby_text)
        continue;

      if (ruby_run.StyleRef().IsFlippedLinesWritingMode()) {
        LayoutUnit top_of_first_ruby_text_line =
            ruby_text.LogicalTop() + (ruby_text.FirstRootBox()
                                          ? ruby_text.FirstRootBox()->LineTop()
                                          : LayoutUnit());
        if (top_of_first_ruby_text_line >= 0)
          continue;
        top_of_first_ruby_text_line += curr->LogicalTop();
        result =
            std::max(result, allowed_position - top_of_first_ruby_text_line);
      } else {
        LayoutUnit bottom_of_last_ruby_text_line =
            ruby_text.LogicalTop() +
            (ruby_text.LastRootBox() ? ruby_text.LastRootBox()->LineBottom()
                                     : ruby_text.LogicalHeight());
        if (bottom_of_last_ruby_text_line <= curr->LogicalHeight())
          continue;
        bottom_of_last_ruby_text_line += curr->LogicalTop();
        result =
            std::max(result, bottom_of_last_ruby_text_line - allowed_position);
      }
    }

    if (curr->IsInlineTextBox()) {
      const ComputedStyle& style =
          curr->GetLineLayoutItem().StyleRef(IsFirstLineStyle());
      if (style.GetTextEmphasisMark() != TextEmphasisMark::kNone &&
          HasEmphasisMarkUnder(To<InlineTextBox>(curr))) {
        if (!style.IsFlippedLinesWritingMode()) {
          LayoutUnit bottom_of_emphasis_mark =
              curr->LogicalBottom() + style.GetFont().EmphasisMarkHeight(
                                          style.TextEmphasisMarkString());
          result = std::max(result, bottom_of_emphasis_mark - allowed_position);
        } else {
          LayoutUnit top_of_emphasis_mark =
              curr->LogicalTop() - style.GetFont().EmphasisMarkHeight(
                                       style.TextEmphasisMarkString());
          result = std::max(result, allowed_position - top_of_emphasis_mark);
        }
      }
    }
  }
  return result;
}

const char* InlineFlowBox::BoxName() const {
  return "InlineFlowBox";
}

#if DCHECK_IS_ON()

void InlineFlowBox::DumpLineTreeAndMark(StringBuilder& string_builder,
                                        const InlineBox* marked_box1,
                                        const char* marked_label1,
                                        const InlineBox* marked_box2,
                                        const char* marked_label2,
                                        const LayoutObject* obj,
                                        int depth) const {
  InlineBox::DumpLineTreeAndMark(string_builder, marked_box1, marked_label1,
                                 marked_box2, marked_label2, obj, depth);
  for (const InlineBox* box = FirstChild(); box; box = box->NextOnLine()) {
    box->DumpLineTreeAndMark(string_builder, marked_box1, marked_label1,
                             marked_box2, marked_label2, obj, depth + 1);
  }
}

#endif

}  // namespace blink