spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
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
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
use std::borrow::Cow;

use icu_segmenter::GraphemeClusterSegmenter;

use super::super::*;
use crate::css::WhiteSpace;
use crate::document::paint::text::RenderedTextMatrix;
use crate::units::SemanticLengthExt;

pub(in crate::text) struct FontSizeAdjustmentRange {
    pub(in crate::text) range: Range<usize>,
    pub(in crate::text) font_size: f32,
}

/// One authored styled span after `@font-face unicode-range` face selection.
///
/// The shaping view borrows the authored computed style and owns only a
/// changed `unicode-range` family. The metric style always remains the
/// author's original style.
struct ResolvedStyledTextSpan<'a> {
    text: &'a str,
    shaping_style: SelectedFaceStyleView<'a>,
    metric_style: &'a ComputedStyle,
}

/// One contiguous source fragment assigned to a single shaping and metric
/// style before synthetic boundary controls are inserted.
struct StyledShapingSpan<'span, 'source> {
    text: String,
    shaping_style: &'span SelectedFaceStyleView<'source>,
    metric_style: &'source ComputedStyle,
}

/// The styled text buffer passed to Parley and its durable source provenance.
///
/// `text` may contain emoji presentation selectors and synthetic join
/// controls. `source_positions` maps its non-synthetic clusters back to the
/// authored CSS Text stream before Parley cluster ranges are converted into
/// [`ShapedGlyphRun`] provenance.
struct MappedStyledShapingText<'span, 'source> {
    text: String,
    authored_text: String,
    ranges: Vec<(Range<usize>, &'span SelectedFaceStyleView<'source>)>,
    metric_ranges: Vec<(Range<usize>, &'source ComputedStyle)>,
    shaping_contexts: ShapingContextMap,
    source_positions: Vec<StyledTextSourcePosition>,
}

/// A concrete family selected for one emoji-presentation grapheme cluster.
///
/// The range remains in shaping-input coordinates, so synthesized presentation
/// selectors stay with their preceding base through Parley cluster matching.
pub(in crate::text) struct EmojiPresentationFamilyRange {
    pub(in crate::text) range: Range<usize>,
    pub(in crate::text) source: String,
}

fn unicode_range_resolved_span(
    range: Range<usize>,
    selected_family: Option<FontFamily>,
) -> UnicodeRangeResolvedSpan {
    UnicodeRangeResolvedSpan {
        range,
        selected_family,
    }
}

impl FontSystem {
    pub(in crate::text) fn font_size_adjustment_ranges_for_line<B: parley::style::Brush>(
        &mut self,
        line: &parley::Line<'_, B>,
        text: &str,
        style: &ComputedStyle,
    ) -> Vec<FontSizeAdjustmentRange> {
        let mut ranges = Vec::new();
        for run in line.runs() {
            let fallback_character = parley_run_fallback_character(text, run.text_range());
            let Some(font_id) = self.document_font_from_parley_font_data_for_style(
                run.font(),
                style,
                fallback_character,
            ) else {
                continue;
            };
            let Some(font_size) = self.used_font_size_for_font(style, font_id) else {
                continue;
            };
            if (font_size - style.font_size).abs() > 0.01 {
                ranges.push(FontSizeAdjustmentRange {
                    range: run.text_range(),
                    font_size,
                });
            }
        }
        ranges
    }

    pub(in crate::text) fn styled_font_size_adjustment_ranges_for_line<B: parley::style::Brush>(
        &mut self,
        line: &parley::Line<'_, B>,
        text: &str,
        shaping_ranges: &[(Range<usize>, &SelectedFaceStyleView<'_>)],
        metric_ranges: &[(Range<usize>, &ComputedStyle)],
        default_style: &SelectedFaceStyleView<'_>,
    ) -> Vec<FontSizeAdjustmentRange> {
        let mut adjustments = Vec::new();
        for run in line.runs() {
            let run_range = run.text_range();
            let run_style =
                style_for_text_range(shaping_ranges, run_range.clone()).unwrap_or(default_style);
            let metric_style = style_for_text_range(metric_ranges, run_range.clone())
                .unwrap_or(default_style.authored());
            let fallback_character = parley_run_fallback_character(text, run_range.clone());
            let Some(font_id) = self.document_font_from_parley_font_data_for_family(
                run.font(),
                run_style.authored(),
                run_style.font_family(),
                fallback_character,
            ) else {
                continue;
            };
            let Some(font_size) = self.used_font_size_for_font(metric_style, font_id) else {
                continue;
            };
            if (font_size - metric_style.font_size).abs() > 0.01 {
                adjustments.push(FontSizeAdjustmentRange {
                    range: run_range,
                    font_size,
                });
            }
        }
        adjustments
    }

    /// Re-shape a provably simple visible fallback fragment with the CSS face
    /// selected for that scalar, retaining its control-bearing source text for
    /// PDF extraction and reporting the omitted fallback advance separately.
    pub(in crate::text) fn rehome_control_fallback_run(
        &mut self,
        style: &ComputedStyle,
        request: ControlFallbackRehomeRequest,
        letter_spacing: ShapingLetterSpacing,
    ) -> Option<(ShapedGlyphRun, ShaperInlineAdvance)> {
        self.rehome_control_fallback_run_for_family(
            style,
            &style.font_family,
            request,
            letter_spacing,
        )
    }

    fn rehome_control_fallback_run_for_selected_face(
        &mut self,
        style: &SelectedFaceStyleView<'_>,
        request: ControlFallbackRehomeRequest,
        letter_spacing: ShapingLetterSpacing,
    ) -> Option<(ShapedGlyphRun, ShaperInlineAdvance)> {
        self.rehome_control_fallback_run_for_family(
            style.authored(),
            style.font_family(),
            request,
            letter_spacing,
        )
    }

    fn rehome_control_fallback_run_for_family(
        &mut self,
        style: &ComputedStyle,
        family: &FontFamily,
        request: ControlFallbackRehomeRequest,
        letter_spacing: ShapingLetterSpacing,
    ) -> Option<(ShapedGlyphRun, ShaperInlineAdvance)> {
        let selected_font_id =
            self.font_for_character_in_family(style, family, request.character)?;
        (selected_font_id != request.fallback_font_id).then_some(())?;
        let selected_font = self.document_fonts.get(selected_font_id)?;
        let mut glyphs = shape_text_with_document_font(
            selected_font,
            &request.character.to_string(),
            request.font_size,
            0.0,
            0.0,
        )?;
        if glyphs.len() != 1 {
            return None;
        }
        glyphs[0].x_advance += letter_spacing.requested_for(style);
        let dropped_advance = ShaperInlineAdvance::from_parley(
            (request.shaper_advance.points() - glyphs[0].x_advance).max(0.0),
        );
        Some((
            ShapedGlyphRun {
                text: request.text,
                x_offset: request.visual_position.points(),
                y_offset: 0.0,
                text_matrix: RenderedTextMatrix::IDENTITY,
                font_size: request.font_size,
                font_id: Some(selected_font_id),
                font_palette: style.font_palette.clone(),
                glyphs,
                glyph_source_ranges: vec![request.source_range],
            },
            dropped_advance,
        ))
    }

    pub(in crate::text) fn rendered_text_runs_for_parley_line<B: parley::style::Brush>(
        &mut self,
        text: &str,
        line: parley::Line<'_, B>,
        style: &ComputedStyle,
        letter_spacing: ShapingLetterSpacing,
    ) -> Vec<ShapedGlyphRun> {
        let run_count = line.runs().size_hint().0;
        let mut rendered_runs = Vec::with_capacity(run_count);
        let mut tab_contexts = Vec::with_capacity(run_count);
        let mut dropped_default_ignorable_runs = Vec::new();
        let mut rehomed_control_fallback_runs = Vec::new();
        for run in line.runs() {
            let run_range = run.text_range();
            let raw_run_text = text.get(run_range.clone()).unwrap_or_default();
            let control_fallback_cluster = classify_control_fallback_cluster(
                raw_run_text,
                run.visual_clusters()
                    .flat_map(|cluster| cluster.glyphs())
                    .any(|glyph| glyph.x != 0.0 || glyph.y != 0.0),
            );
            let run_text = text
                .get(run_range.clone())
                .map(text_without_variation_selectors)
                .unwrap_or_else(|| Cow::Borrowed(""));
            // CSS `font-size: 0` gives text no glyph ink or advance. Do not
            // serialize a zero-scale PDF text operation: PDF consumers may
            // handle a zero `Tf` state inconsistently, and the run has no
            // paint or extraction contribution.
            if run.font_size() <= 0.01 {
                continue;
            }
            let x_offset = run
                .visual_clusters()
                .next()
                .and_then(|cluster| cluster.visual_offset())
                .unwrap_or(0.0);
            if control_fallback_cluster == ControlFallbackCluster::DropControlOnly {
                dropped_default_ignorable_runs.push(SourceOnlyRun {
                    visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                    shaper_advance: ShaperInlineAdvance::from_parley(run.advance()),
                    text: run_text.clone().into_owned().into(),
                });
                continue;
            }
            let fallback_character = parley_run_fallback_character(text, run_range.clone());
            let Some(font_id) = self.document_font_from_parley_font_data_for_style(
                run.font(),
                style,
                fallback_character,
            ) else {
                continue;
            };
            if let ControlFallbackCluster::RehomeSimpleVisibleFragment { character } =
                control_fallback_cluster
                && let Some((rehomed_run, dropped_advance)) = self.rehome_control_fallback_run(
                    style,
                    ControlFallbackRehomeRequest {
                        character,
                        fallback_font_id: font_id,
                        text: run_text.clone().into_owned().into(),
                        font_size: run.font_size(),
                        visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                        shaper_advance: ShaperInlineAdvance::from_parley(run.advance()),
                        source_range: Some(run_range.clone()),
                    },
                    letter_spacing,
                )
            {
                if !dropped_advance.is_zero() {
                    dropped_default_ignorable_runs.push(SourceOnlyRun {
                        visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                        shaper_advance: dropped_advance,
                        text: Rc::from(""),
                    });
                }
                rehomed_control_fallback_runs.push(rendered_runs.len());
                rendered_runs.push(rehomed_run);
                tab_contexts.push(RenderedRunTabContext {
                    style,
                    metric_style: style,
                });
                continue;
            }
            if !run_text.contains('\t')
                && self
                    .document_fonts
                    .support_kind_for_run(font_id, run_text.as_ref())
                    == FontSupportKind::ColorOrEmojiOnlyFallback
                && !self
                    .document_fonts
                    .run_has_emoji_presentation_glyph(font_id, run_text.as_ref())
                && let Some(fallback_font_id) =
                    self.visible_text_fallback_for_run(run_text.as_ref(), style, font_id)
                && let Some(fallback_font) = self.document_fonts.get(fallback_font_id)
                && let Some(glyphs) = shape_text_with_document_font(
                    fallback_font,
                    run_text.as_ref(),
                    run.font_size(),
                    letter_spacing.requested_for(style),
                    style.used_word_spacing().points(),
                )
                && !glyphs.is_empty()
            {
                let glyph_source_ranges = vec![None; glyphs.len()];
                rendered_runs.push(ShapedGlyphRun {
                    text: run_text.into_owned().into(),
                    x_offset,
                    y_offset: 0.0,
                    text_matrix: RenderedTextMatrix::IDENTITY,
                    font_size: run.font_size(),
                    font_id: Some(fallback_font_id),
                    font_palette: style.font_palette.clone(),
                    glyphs,
                    glyph_source_ranges,
                });
                tab_contexts.push(RenderedRunTabContext {
                    style,
                    metric_style: style,
                });
                continue;
            }
            let Some(font) = self.document_fonts.get(font_id) else {
                continue;
            };
            let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
                continue;
            };
            let units_per_em = font.units_per_em.max(1) as f32;
            let scale = run.font_size() / units_per_em;
            let mut glyphs = Vec::new();
            let mut glyph_source_ranges = Vec::new();
            let mut emitted_source_ranges = Vec::<Range<usize>>::new();
            for cluster in run.visual_clusters() {
                let cluster_range = cluster.text_range();
                let cluster_text = text.get(cluster_range.clone()).unwrap_or_default();
                let emitted_cluster_text = text_without_glyph_output_controls(cluster_text);
                let default_ignorable_only =
                    cluster_is_default_ignorable_only(cluster_text, emitted_cluster_text.as_ref());
                if default_ignorable_only
                    && !default_ignorable_cluster_has_shaping_glyph(
                        &face,
                        cluster_text,
                        emitted_cluster_text.as_ref(),
                        cluster.glyphs().filter_map(|glyph| {
                            u16::try_from(glyph.id)
                                .ok()
                                .map(|glyph_id| (glyph_id, glyph.advance))
                        }),
                    )
                {
                    continue;
                }
                if emitted_cluster_text.as_ref() == "\t" {
                    let provisional_advance = cluster.glyphs().map(|glyph| glyph.advance).sum();
                    glyphs.push(synthesized_tab_glyph(provisional_advance));
                    glyph_source_ranges.push(Some(cluster_range));
                    continue;
                }
                let mut first_cluster_glyph = true;
                for glyph in cluster.glyphs() {
                    let Ok(glyph_id) = u16::try_from(glyph.id) else {
                        continue;
                    };
                    let unicode = if first_cluster_glyph {
                        if default_ignorable_only
                            || emitted_source_ranges
                                .iter()
                                .any(|range| range == &cluster_range)
                        {
                            String::new()
                        } else {
                            emitted_source_ranges.push(cluster_range.clone());
                            emitted_cluster_text.as_ref().to_owned()
                        }
                    } else {
                        String::new()
                    };
                    if glyph_is_join_control_artifact(&face, glyph_id, &unicode, cluster_text) {
                        first_cluster_glyph = false;
                        continue;
                    }
                    if glyph_is_non_painting_shaping_artifact(
                        &face,
                        glyph_id,
                        glyph.advance,
                        &unicode,
                    ) {
                        first_cluster_glyph = false;
                        continue;
                    }
                    first_cluster_glyph = false;
                    glyphs.push(RenderedGlyph {
                        kind: RenderedGlyphKind::Paint(glyph_id),
                        x_advance: glyph.advance,
                        nominal_x_advance: face
                            .glyph_hor_advance(ttf_parser::GlyphId(glyph_id))
                            .map(|advance| advance as f32 * scale)
                            .unwrap_or(glyph.advance),
                        x_offset: glyph.x,
                        y_offset: -glyph.y,
                        unicode,
                    });
                    glyph_source_ranges.push(Some(cluster_range.clone()));
                }
            }
            if glyphs.is_empty() {
                continue;
            }
            let mut font_size = run.font_size();
            apply_synthetic_position_fallback(
                &mut glyphs,
                &mut font_size,
                style,
                &face,
                run_text.as_ref(),
            );
            rendered_runs.push(ShapedGlyphRun {
                text: run_text.into_owned().into(),
                x_offset,
                y_offset: 0.0,
                text_matrix: RenderedTextMatrix::IDENTITY,
                font_size,
                font_id: Some(font_id),
                font_palette: style.font_palette.clone(),
                glyphs,
                glyph_source_ranges,
            });
            tab_contexts.push(RenderedRunTabContext {
                style,
                metric_style: style,
            });
        }
        for run in &mut rendered_runs {
            run.x_offset = corrected_visual_inline_position(
                ShaperVisualInlinePosition::from_parley(run.x_offset),
                &dropped_default_ignorable_runs,
            )
            .points();
        }
        if style.white_space == WhiteSpace::BreakSpaces {
            // `break-spaces` retains each preserved separator as its own CSS text
            // processing unit.  Do not let a glyph-placement adjustment on the
            // first following text glyph pull that glyph back into the retained
            // separator's physical advance; the same source split at an inline
            // boundary must keep the identical visible edge.
            // <https://www.w3.org/TR/css-text-3/#white-space-phase-2>
            let mut previous_was_spacer = false;
            for run in &mut rendered_runs {
                for glyph in &mut run.glyphs {
                    let is_spacer = !glyph.unicode.is_empty()
                        && glyph
                            .unicode
                            .chars()
                            .all(character_is_text_decoration_spacer);
                    if previous_was_spacer && !is_spacer {
                        glyph.x_offset = 0.0;
                    }
                    previous_was_spacer = is_spacer;
                }
            }
        }
        stitch_dropped_join_control_runs(&mut rendered_runs, &dropped_default_ignorable_runs);
        for index in rehomed_control_fallback_runs.into_iter().rev() {
            if stitch_rehomed_control_fallback_run(&mut rendered_runs, index) {
                tab_contexts.remove(index + 1);
            }
        }
        self.apply_css_tab_stops(&mut rendered_runs, &tab_contexts, 0.0, letter_spacing);
        rendered_runs
    }

    /// Split a named font stack into range-limited shaping spans.
    ///
    /// CSS Fonts applies `@font-face unicode-range` during font matching, while
    /// CSS Text still requires one shaping context across join controls and
    /// cursive-script neighbors. Parley/fontique does not expose the descriptor
    /// on registration, so Spindrift resolves range-limited named families before
    /// shaping and passes the result back to Parley as styled ranges:
    /// <https://www.w3.org/TR/css-fonts-4/#unicode-range-desc>,
    /// <https://www.w3.org/TR/css-text-3/#text-processing-order>, and
    /// <https://www.w3.org/TR/alreq/#h_joining_enforcement>.
    pub(in crate::text) fn unicode_range_resolved_text_spans(
        &mut self,
        text: &str,
        style: &ComputedStyle,
    ) -> Option<Vec<UnicodeRangeResolvedSpan>> {
        let names = named_font_families(&style.font_family);
        if names.is_empty()
            || !names.iter().any(|name| {
                self.resolve_single_family(
                    name,
                    style.font_weight,
                    style.font_style,
                    style.font_width,
                )
                .is_some_and(|font_id| self.document_fonts.font_has_unicode_range(font_id))
            })
        {
            return None;
        }

        let mut previous_family = None::<FontFamily>;
        let mut selected_family = None::<FontFamily>;
        let mut selected_span_start = 0usize;
        let mut has_selected_character = false;
        let mut spans = Vec::<UnicodeRangeResolvedSpan>::new();
        for (start, character) in text.char_indices() {
            let end = start + character.len_utf8();
            let family = if character_is_join_control(character) {
                previous_family
                    .clone()
                    .or_else(|| self.next_unicode_range_family_for_text(text, end, style))
            } else {
                self.unicode_range_family_for_character(character, style)
            };
            if let Some(family) = &family {
                previous_family = Some(family.clone());
            }
            if !has_selected_character {
                selected_family = family;
                selected_span_start = start;
                has_selected_character = true;
            } else if selected_family != family {
                spans.push(unicode_range_resolved_span(
                    selected_span_start..start,
                    selected_family,
                ));
                selected_family = family;
                selected_span_start = start;
            }
        }
        if !has_selected_character || spans.is_empty() && selected_family.is_none() {
            return None;
        }
        spans.push(unicode_range_resolved_span(
            selected_span_start..text.len(),
            selected_family,
        ));

        spans
            .iter()
            .any(|span| span.selected_family.as_ref() != Some(&style.font_family))
            .then_some(spans)
    }

    pub(in crate::text) fn next_unicode_range_family_for_text(
        &mut self,
        text: &str,
        start: usize,
        style: &ComputedStyle,
    ) -> Option<FontFamily> {
        text.get(start..)?.chars().find_map(|character| {
            (!character_is_join_control(character))
                .then(|| self.unicode_range_family_for_character(character, style))
                .flatten()
        })
    }

    pub(in crate::text) fn unicode_range_family_for_character(
        &mut self,
        character: char,
        style: &ComputedStyle,
    ) -> Option<FontFamily> {
        let families = match &style.font_family {
            FontFamily::List(families) => families.as_slice(),
            family => std::slice::from_ref(family),
        };
        for family in families {
            match family {
                FontFamily::Named(name) => {
                    let Some(font_id) = self.resolve_single_family(
                        name.as_str(),
                        style.font_weight,
                        style.font_style,
                        style.font_width,
                    ) else {
                        continue;
                    };
                    if self.document_fonts.font_has_character(font_id, character) {
                        return Some(FontFamily::Named(name.clone()));
                    }
                }
                generic => {
                    // A generic family is resolved by the platform after all
                    // explicitly range-limited named faces have declined the
                    // character. Keep it as the backend family source rather
                    // than retaining a rejected preceding face in the stack.
                    return Some(generic.clone());
                }
            }
        }
        None
    }
}

impl FontSystem {
    pub(in crate::text) fn apply_upright_vertical_metrics(
        &self,
        runs: &mut [ShapedInlineRun],
        typesetting_plan: &TextTypesettingPlan,
    ) {
        // Shaping spans need not be stored in visual order: neutral punctuation
        // can split an LTR sequence into runs that Parley returns in source
        // order. Their offsets, however, are visual-inline positions. Apply
        // each preceding metric delta in that coordinate order.
        let mut visual_run_order = (0..runs.len()).collect::<Vec<_>>();
        visual_run_order.sort_by(|&left, &right| {
            runs[left]
                .x_offset
                .total_cmp(&runs[right].x_offset)
                .then_with(|| left.cmp(&right))
        });
        let mut preceding_advance_delta = 0.0;
        for run_index in visual_run_order {
            let run = &mut runs[run_index];
            // Parley records every visual run origin in the horizontal
            // shaping coordinate system. Rebase a later run after converting
            // an earlier upright unit to its vertical advance, otherwise a
            // font/style boundary retains the old horizontal pen position.
            // <https://www.w3.org/TR/css-writing-modes-4/#vertical-font-features>
            run.x_offset += preceding_advance_delta;
            let original_advance = run
                .glyphs
                .iter()
                .map(|glyph| glyph.rendered.x_advance)
                .sum::<f32>();
            let Some(font_id) = run.font_id else {
                continue;
            };
            let Some(font) = self.document_fonts.get(font_id) else {
                continue;
            };
            let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
                continue;
            };
            let units_per_em = font.units_per_em.max(1) as f32;
            let scale = run.font_size / units_per_em;
            for glyph in &mut run.glyphs {
                let Some(VerticalUnitTypesetting::UprightVertical) = glyph
                    .source_range
                    .as_ref()
                    .and_then(|range| typesetting_plan.unanimous_typesetting_for_range(range))
                else {
                    continue;
                };
                if glyph.rendered.unicode == "\t" {
                    continue;
                }
                let Some(glyph_id) = glyph.rendered.painted_id() else {
                    continue;
                };
                let glyph_id = ttf_parser::GlyphId(glyph_id);
                let synthesized_vertical_advance =
                    (i32::from(face.ascender()) - i32::from(face.descender())).max(1) as f32;
                let vertical_advance = face
                    .glyph_ver_advance(glyph_id)
                    .map(|advance| advance as f32)
                    .filter(|advance| *advance > 0.0)
                    .unwrap_or(synthesized_vertical_advance)
                    * scale;
                // Parley shapes this stream on a horizontal baseline. CSS
                // Writing Modes instead positions upright glyphs from their
                // OpenType vertical origin. Convert the shaped baseline
                // offset here, while the selected face and glyph metrics are
                // still available; text painting turns it into a run origin
                // because PDF text positioning has no per-glyph y advance.
                //
                // OpenType defines the vertical origin with VORG when
                // present, otherwise from the glyph top side bearing. A font
                // that supplies neither vertical metric has no glyph-specific
                // vertical origin. Its used vertical advance is therefore the
                // stable typographic-unit origin; deriving one from horizontal
                // ink bounds would make the same one-em unit start at a
                // different position for every glyph.
                // <https://learn.microsoft.com/en-us/typography/opentype/spec/vorg>
                // <https://www.w3.org/TR/css-writing-modes-4/#text-orientation>
                let glyph_vertical_origin = face
                    .glyph_y_origin(glyph_id)
                    .map(f32::from)
                    .or_else(|| {
                        face.glyph_bounding_box(glyph_id)
                            .zip(face.glyph_ver_side_bearing(glyph_id))
                            .map(|(bounds, top_side_bearing)| {
                                f32::from(bounds.y_max) + f32::from(top_side_bearing)
                            })
                    })
                    .map(|origin| origin * scale);
                // Only an OpenType-provided glyph-specific origin alters the
                // horizontal shaping baseline. When neither VORG nor a
                // vertical side bearing is available, the synthesized
                // vertical advance determines the CSS typographic-unit
                // origin; translating the glyph by that same advance would
                // apply the unit placement twice.
                if let Some(vertical_origin) = glyph_vertical_origin {
                    glyph.rendered.y_offset -= vertical_origin;
                }
                let extra_spacing =
                    (glyph.rendered.x_advance - glyph.rendered.nominal_x_advance).max(0.0);
                glyph.rendered.nominal_x_advance = vertical_advance;
                glyph.rendered.x_advance = vertical_advance + extra_spacing;
            }
            let used_advance = run
                .glyphs
                .iter()
                .map(|glyph| glyph.rendered.x_advance)
                .sum::<f32>();
            preceding_advance_delta += used_advance - original_advance;
        }
    }

    /// Shape adjacent styled text spans as one CSS Text shaping context.
    ///
    /// CSS Text requires shaping to see across inline element boundaries unless
    /// a boundary condition explicitly interrupts shaping. Parley accepts
    /// ranged styles, allowing the renderer to preserve font/style runs for
    /// PDF emission while still giving OpenType shaping adjacent cursive-script
    /// context across those ranges:
    /// <https://www.w3.org/TR/css-text-3/#boundary-shaping> and
    /// <https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm>.
    #[cfg(test)]
    pub(crate) fn shape_styled_text_runs_with_parley(
        &mut self,
        spans: &[StyledTextSpan<'_>],
    ) -> Vec<ShapedGlyphRun> {
        self.shape_styled_text_runs_with_parley_with_letter_spacing(
            spans,
            ShapingLetterSpacing::Computed,
        )
    }

    pub(in crate::text) fn shape_styled_text_runs_with_parley_with_letter_spacing(
        &mut self,
        spans: &[StyledTextSpan<'_>],
        letter_spacing: ShapingLetterSpacing,
    ) -> Vec<ShapedGlyphRun> {
        let Some(tab_metric_style) = spans.first().map(|span| span.style) else {
            return Vec::new();
        };
        self.shape_styled_text_runs_with_parley_at_tab_origin_with_letter_spacing(
            spans,
            0.0,
            tab_metric_style,
            letter_spacing,
        )
    }

    /// Shape styled text with tabs measured from a CSS line's content edge.
    ///
    /// Preserved tabs advance to stops measured from the block content edge,
    /// not from text alignment, indentation, or a fragment boundary:
    /// <https://drafts.csswg.org/css-text-3/#white-space-phase-2>.
    #[allow(dead_code)] // Computed mode remains available for legacy artifacts.
    pub(crate) fn shape_styled_text_runs_with_parley_at_tab_origin(
        &mut self,
        spans: &[StyledTextSpan<'_>],
        tab_origin: f32,
        tab_metric_style: &ComputedStyle,
    ) -> Vec<ShapedGlyphRun> {
        self.shape_styled_text_runs_with_parley_at_tab_origin_with_letter_spacing(
            spans,
            tab_origin,
            tab_metric_style,
            ShapingLetterSpacing::Computed,
        )
    }

    pub(in crate::text) fn shape_styled_text_runs_with_parley_at_tab_origin_with_letter_spacing(
        &mut self,
        spans: &[StyledTextSpan<'_>],
        tab_origin: f32,
        tab_metric_style: &ComputedStyle,
        letter_spacing: ShapingLetterSpacing,
    ) -> Vec<ShapedGlyphRun> {
        let resolved_spans = self.resolved_styled_text_spans(spans);
        let Some(MappedStyledShapingText {
            text,
            authored_text,
            ranges,
            metric_ranges,
            shaping_contexts,
            source_positions,
        }) = Self::mapped_styled_shaping_text(&resolved_spans)
        else {
            return Vec::new();
        };
        // The normalized text is only the input to Parley's glyph selection.
        // `source_positions` maps the font-selectable buffer back to its
        // original CSS Text byte ranges.
        let shaping_text = text_with_shaping_compatibility_normalization(&text);
        let shaping_text = shaping_text.as_ref();
        let default_style = ranges[0].1;
        let default_authored_style = default_style.authored();
        let has_cursive_face_transition = text.chars().any(character_has_cursive_shaping_behavior)
            && ranges.windows(2).any(|pair| {
                matches!(
                    pair[0].1.boundary_effect(pair[1].1),
                    InlineBoundaryEffect::ShapingInputChange
                )
            });
        self.with_reusable_parley_layout(|this, layout| {
            let parley_styles = ranges
                .iter()
                .map(|(_, style)| this.shaping_style_for_selected_face_view(style))
                .collect::<Vec<_>>();
            let default_parley_style = &parley_styles[0];
            let default_feature_context =
                this.font_feature_context_for_selected_face(default_style);
            let feature_contexts = ranges
                .iter()
                .map(|(_, style)| this.font_feature_context_for_selected_face(style))
                .collect::<Vec<_>>();
            let mut font_family_sources = Vec::<String>::with_capacity(ranges.len());
            for (range, style) in &ranges {
                let authored = style.authored();
                let selected = this.resolved_parley_font_family_source_for_family(
                    style.font_family(),
                    authored.font_weight,
                    authored.font_style,
                    authored.font_width,
                );
                // A closing bidi formatting control has no glyph or family
                // choice of its own. Keeping the preceding selected source
                // prevents it from merging the final visible cluster into an
                // enclosing element's unselected authored stack.
                // <https://www.w3.org/TR/css-writing-modes-4/#bidi-algo>
                if text[range.clone()]
                    .chars()
                    .all(character_is_bidi_format_control)
                    && let Some(previous) = font_family_sources.last()
                {
                    font_family_sources.push(previous.clone());
                } else {
                    font_family_sources.push(selected);
                }
            }
            let emoji_family_ranges = ranges
                .iter()
                .flat_map(|(style_range, style)| {
                    this.emoji_presentation_family_ranges(
                        &text[style_range.clone()],
                        style.authored(),
                        style.font_family(),
                    )
                    .into_iter()
                    .map(|mut emoji_range| {
                        emoji_range.range.start += style_range.start;
                        emoji_range.range.end += style_range.start;
                        emoji_range
                    })
                })
                .collect::<Vec<_>>();
            // RangedBuilder uses the default properties at the zero-length
            // boundary before applying the first explicit range. Keep the
            // default family source in sync with the first selected range so
            // a presentation-selected emoji at byte zero does not retain the
            // authored stack's first face.
            let default_font_family_source = font_family_sources[0].clone();
            let mut builder: parley::RangedBuilder<'_, FontPalette> = this
                .parley_layout_context
                .ranged_builder(&mut this.parley_font_context, shaping_text, 1.0, false);
            push_parley_default_style(
                &mut builder,
                default_parley_style,
                &default_font_family_source,
            );
            push_parley_text_spacing_default_with_context(
                &mut builder,
                shaping_text,
                default_authored_style,
                letter_spacing.requested_for(default_authored_style),
                default_feature_context.as_ref(),
            );
            for ((((range, style), parley_style), feature_context), font_family_source) in ranges
                .iter()
                .zip(&parley_styles)
                .zip(&feature_contexts)
                .zip(&font_family_sources)
            {
                push_parley_style_range(
                    &mut builder,
                    parley_style,
                    font_family_source,
                    range.clone(),
                );
                push_parley_text_spacing_range_with_context(
                    &mut builder,
                    &shaping_text[range.clone()],
                    style.authored(),
                    range.clone(),
                    letter_spacing.requested_for(style.authored()),
                    feature_context.as_ref(),
                );
            }
            for emoji_range in &emoji_family_ranges {
                builder.push(
                    StyleProperty::FontFamily(ParleyFontFamily::from(emoji_range.source.as_str())),
                    emoji_range.range.clone(),
                );
            }
            builder.build_into(layout, shaping_text);
            layout.break_all_lines(None);
            let Some(line) = layout.lines().next() else {
                return Vec::new();
            };
            let adjustment_ranges = this.styled_font_size_adjustment_ranges_for_line(
                &line,
                shaping_text,
                &ranges,
                &metric_ranges,
                default_style,
            );
            if !adjustment_ranges.is_empty() {
                let mut builder: parley::RangedBuilder<'_, FontPalette> = this
                    .parley_layout_context
                    .ranged_builder(&mut this.parley_font_context, shaping_text, 1.0, false);
                push_parley_default_style(
                    &mut builder,
                    default_parley_style,
                    &default_font_family_source,
                );
                push_parley_text_spacing_default_with_context(
                    &mut builder,
                    shaping_text,
                    default_authored_style,
                    letter_spacing.requested_for(default_authored_style),
                    default_feature_context.as_ref(),
                );
                for ((((range, style), parley_style), feature_context), font_family_source) in
                    ranges
                        .iter()
                        .zip(&parley_styles)
                        .zip(&feature_contexts)
                        .zip(&font_family_sources)
                {
                    push_parley_style_range(
                        &mut builder,
                        parley_style,
                        font_family_source,
                        range.clone(),
                    );
                    push_parley_text_spacing_range_with_context(
                        &mut builder,
                        &shaping_text[range.clone()],
                        style.authored(),
                        range.clone(),
                        letter_spacing.requested_for(style.authored()),
                        feature_context.as_ref(),
                    );
                }
                for emoji_range in &emoji_family_ranges {
                    builder.push(
                        StyleProperty::FontFamily(ParleyFontFamily::from(
                            emoji_range.source.as_str(),
                        )),
                        emoji_range.range.clone(),
                    );
                }
                for adjustment in &adjustment_ranges {
                    builder.push(
                        StyleProperty::FontSize(adjustment.font_size),
                        adjustment.range.clone(),
                    );
                }
                builder.build_into(layout, shaping_text);
                layout.break_all_lines(None);
            }
            let Some(line) = layout.lines().next() else {
                return Vec::new();
            };
            let run_count = line.runs().size_hint().0;
            let mut rendered_runs = Vec::with_capacity(run_count);
            let mut tab_contexts = Vec::with_capacity(run_count);
            let mut dropped_default_ignorable_runs = Vec::new();
            let mut rehomed_control_fallback_runs = Vec::new();
            for run in line.runs() {
                let run_range = run.text_range();
                let raw_run_text = text.get(run_range.clone()).unwrap_or_default();
                let control_fallback_cluster = classify_control_fallback_cluster(
                    raw_run_text,
                    run.visual_clusters()
                        .flat_map(|cluster| cluster.glyphs())
                        .any(|glyph| glyph.x != 0.0 || glyph.y != 0.0),
                );
                let run_text_without_synthetic = text_without_synthetic_join_controls(
                    &text,
                    run_range.clone(),
                    &shaping_contexts,
                );
                let run_text = match text_without_variation_selectors(&run_text_without_synthetic) {
                    Cow::Borrowed(_) => run_text_without_synthetic,
                    Cow::Owned(text) => text,
                };
                let authored_run_text = styled_authored_text_for_internal_range(
                    run_range.clone(),
                    &source_positions,
                    &authored_text,
                )
                .unwrap_or(run_text.as_ref());
                if run.font_size() <= 0.01 {
                    continue;
                }
                let run_style =
                    style_for_text_range(&ranges, run_range.clone()).unwrap_or(default_style);
                let x_offset = run
                    .visual_clusters()
                    .next()
                    .and_then(|cluster| cluster.visual_offset())
                    .unwrap_or(0.0);
                if control_fallback_cluster == ControlFallbackCluster::DropControlOnly {
                    dropped_default_ignorable_runs.push(SourceOnlyRun {
                        visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                        shaper_advance: ShaperInlineAdvance::from_parley(run.advance()),
                        text: authored_run_text.into(),
                    });
                    continue;
                }
                let fallback_character =
                    parley_run_fallback_character(shaping_text, run_range.clone());
                let Some(font_id) = this.document_font_from_parley_font_data_for_family(
                    run.font(),
                    run_style.authored(),
                    run_style.font_family(),
                    fallback_character,
                ) else {
                    continue;
                };
                let contextual_glyphs = this.selected_face_contextual_glyphs(
                    &text,
                    run_range.clone(),
                    run_style,
                    has_cursive_face_transition,
                    letter_spacing,
                );
                if let ControlFallbackCluster::RehomeSimpleVisibleFragment { character } =
                    control_fallback_cluster
                    && let Some((rehomed_run, dropped_advance)) = this
                        .rehome_control_fallback_run_for_selected_face(
                            run_style,
                            ControlFallbackRehomeRequest {
                                character,
                                fallback_font_id: font_id,
                                text: run_text.clone().into(),
                                font_size: run.font_size(),
                                visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                                shaper_advance: ShaperInlineAdvance::from_parley(run.advance()),
                                source_range: styled_cluster_source_range(
                                    run_range.clone(),
                                    &source_positions,
                                ),
                            },
                            letter_spacing,
                        )
                {
                    if !dropped_advance.is_zero() {
                        dropped_default_ignorable_runs.push(SourceOnlyRun {
                            visual_position: ShaperVisualInlinePosition::from_parley(x_offset),
                            shaper_advance: dropped_advance,
                            text: Rc::from(""),
                        });
                    }
                    rehomed_control_fallback_runs.push(rendered_runs.len());
                    rendered_runs.push(rehomed_run);
                    tab_contexts.push(RenderedRunTabContext {
                        style: run_style.authored(),
                        metric_style: tab_metric_style,
                    });
                    continue;
                }
                if !raw_run_text.contains('\t')
                    && this.document_fonts.support_kind_for_run(font_id, &run_text)
                        == FontSupportKind::ColorOrEmojiOnlyFallback
                    && !this
                        .document_fonts
                        .run_has_emoji_presentation_glyph(font_id, raw_run_text)
                    && let Some(fallback_font_id) = this
                        .visible_text_fallback_for_run_for_selected_face(
                            &run_text, run_style, font_id,
                        )
                    && let Some(fallback_font) = this.document_fonts.get(fallback_font_id)
                    && let Some(glyphs) = shape_text_with_document_font(
                        fallback_font,
                        raw_run_text,
                        run.font_size(),
                        letter_spacing.requested_for(run_style.authored()),
                        run_style.authored().used_word_spacing().points(),
                    )
                    && !glyphs.is_empty()
                {
                    let glyphs = glyphs_without_synthetic_join_controls(
                        glyphs,
                        raw_run_text,
                        run_range.start,
                        &shaping_contexts,
                    );
                    let glyph_source_ranges = vec![None; glyphs.len()];
                    rendered_runs.push(ShapedGlyphRun {
                        text: run_text.into(),
                        x_offset,
                        y_offset: 0.0,
                        text_matrix: RenderedTextMatrix::IDENTITY,
                        font_size: run.font_size(),
                        font_id: Some(fallback_font_id),
                        font_palette: run_style.authored().font_palette.clone(),
                        glyphs,
                        glyph_source_ranges,
                    });
                    tab_contexts.push(RenderedRunTabContext {
                        style: run_style.authored(),
                        metric_style: tab_metric_style,
                    });
                    continue;
                }
                let Some(font) = this.document_fonts.get(font_id) else {
                    continue;
                };
                let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
                    continue;
                };
                let units_per_em = font.units_per_em.max(1) as f32;
                let scale = run.font_size() / units_per_em;
                // A styled Parley run may include authored default-ignorable
                // controls which deliberately emit no Unicode on any glyph.
                // Remember the output boundary so that an unsplit paint run
                // can retain its complete logical source text below.
                let rendered_run_start = rendered_runs.len();
                let mut glyphs = Vec::new();
                let mut glyph_source_ranges = Vec::new();
                // Parley may retain a single shaping run across authored
                // inline boundaries when the changed property does not affect
                // glyph shaping. Keep the style associated with every emitted
                // glyph so a preserved tab can retain its own `tab-size`.
                let mut glyph_tab_styles = Vec::new();
                // Parley keeps paint-only style transitions on glyphs rather
                // than splitting the underlying shaping run. Retain the
                // palette selected by each cluster so CSS `font-palette`
                // remains independent for adjacent inline elements.
                let mut glyph_palettes = Vec::new();
                for cluster in run.visual_clusters() {
                    let cluster_range = cluster.text_range();
                    let cluster_style = style_for_text_range(&ranges, cluster_range.clone())
                        .unwrap_or(run_style)
                        .authored();
                    let cluster_palette = cluster.first_style().brush.clone();
                    let raw_cluster_text = text.get(cluster_range.clone()).unwrap_or_default();
                    let source_range = if raw_cluster_text.chars().all(character_is_join_control) {
                        styled_join_control_cluster_source_range(
                            cluster_range.clone(),
                            &source_positions,
                            &authored_text,
                        )
                    } else {
                        styled_cluster_source_range(cluster_range.clone(), &source_positions)
                    };
                    let cluster_text_without_synthetic = text_without_synthetic_join_controls(
                        &text,
                        cluster_range.clone(),
                        &shaping_contexts,
                    );
                    let cleaned_cluster_text =
                        text_without_glyph_output_controls(&cluster_text_without_synthetic);
                    let default_ignorable_only = cluster_is_default_ignorable_only(
                        raw_cluster_text,
                        cleaned_cluster_text.as_ref(),
                    );
                    // A visible contextual glyph may be clustered with an
                    // authored or virtual join control. Emit the adjacent
                    // authored typographic unit as its Unicode payload; the
                    // control itself remains source text only.
                    let contextual_source_text = default_ignorable_only
                        .then(|| {
                            source_range
                                .as_ref()
                                .and_then(|range| authored_text.get(range.clone()))
                        })
                        .flatten();
                    if default_ignorable_only
                        && !default_ignorable_cluster_has_shaping_glyph(
                            &face,
                            raw_cluster_text,
                            cleaned_cluster_text.as_ref(),
                            cluster.glyphs().filter_map(|glyph| {
                                u16::try_from(glyph.id)
                                    .ok()
                                    .map(|glyph_id| (glyph_id, glyph.advance))
                            }),
                        )
                    {
                        continue;
                    }
                    if cleaned_cluster_text.as_ref() == "\t" {
                        let provisional_advance = cluster.glyphs().map(|glyph| glyph.advance).sum();
                        glyphs.push(synthesized_tab_glyph(provisional_advance));
                        glyph_source_ranges.push(source_range);
                        glyph_palettes.push(cluster_palette);
                        glyph_tab_styles.push(cluster_style);
                        continue;
                    }
                    let mut first_cluster_glyph = true;
                    for glyph in cluster.glyphs() {
                        let Ok(glyph_id) = u16::try_from(glyph.id) else {
                            continue;
                        };
                        let candidate_unicode = if first_cluster_glyph {
                            if default_ignorable_only {
                                String::new()
                            } else {
                                cleaned_cluster_text.as_ref().to_owned()
                            }
                        } else {
                            String::new()
                        };
                        if glyph_is_join_control_artifact(
                            &face,
                            glyph_id,
                            &candidate_unicode,
                            raw_cluster_text,
                        ) {
                            continue;
                        }
                        if glyph_is_non_painting_shaping_artifact(
                            &face,
                            glyph_id,
                            glyph.advance,
                            &candidate_unicode,
                        ) {
                            continue;
                        }
                        let unicode = if first_cluster_glyph && default_ignorable_only {
                            contextual_source_text.unwrap_or_default().to_owned()
                        } else {
                            candidate_unicode
                        };
                        first_cluster_glyph = false;
                        glyphs.push(RenderedGlyph {
                            kind: RenderedGlyphKind::Paint(glyph_id),
                            x_advance: glyph.advance,
                            nominal_x_advance: face
                                .glyph_hor_advance(ttf_parser::GlyphId(glyph_id))
                                .map(|advance| advance as f32 * scale)
                                .unwrap_or(glyph.advance),
                            x_offset: glyph.x,
                            y_offset: -glyph.y,
                            unicode,
                        });
                        glyph_source_ranges.push(source_range.clone());
                        glyph_palettes.push(cluster_palette.clone());
                        glyph_tab_styles.push(cluster_style);
                    }
                }
                if glyphs.is_empty() {
                    continue;
                }
                if let Some(contextual_glyphs) = contextual_glyphs
                    && contextual_glyphs.len() == glyphs.len()
                {
                    // Preserve the ranged backend's paint origin and authored
                    // cluster ownership, but replace its isolated glyph forms
                    // with forms selected in the complete logical context.
                    for (glyph, contextual) in glyphs.iter_mut().zip(contextual_glyphs) {
                        let unicode = std::mem::take(&mut glyph.unicode);
                        *glyph = contextual;
                        glyph.unicode = unicode;
                    }
                }
                let mut font_size = run.font_size();
                apply_synthetic_position_fallback(
                    &mut glyphs,
                    &mut font_size,
                    run_style.authored(),
                    &face,
                    &run_text,
                );
                debug_assert_eq!(glyphs.len(), glyph_source_ranges.len());
                debug_assert_eq!(glyphs.len(), glyph_palettes.len());
                debug_assert_eq!(glyphs.len(), glyph_tab_styles.len());
                let mut group_palette = glyph_palettes[0].clone();
                let mut group_style = glyph_tab_styles[0];
                let mut group_glyphs = Vec::new();
                let mut group_source_ranges = Vec::new();
                let mut group_text = String::new();
                let mut group_x_offset = x_offset;
                let mut next_x_offset = x_offset;
                let mut group_has_tab = false;
                for (((glyph, source_range), palette), glyph_style) in glyphs
                    .into_iter()
                    .zip(glyph_source_ranges)
                    .zip(glyph_palettes)
                    .zip(glyph_tab_styles)
                {
                    // A tab's `tab-size` belongs to the style which owns the
                    // tab, but ordinary paint-equivalent style boundaries do
                    // not split the durable shaping run. In particular, a
                    // join control may have a distinct authored font family
                    // while remaining part of its neighbours' one shaping
                    // context.
                    if palette != group_palette
                        || (!std::ptr::eq(glyph_style, group_style)
                            && (glyph.unicode == "\t" || group_has_tab))
                    {
                        rendered_runs.push(ShapedGlyphRun {
                            text: std::mem::take(&mut group_text).into(),
                            x_offset: group_x_offset,
                            y_offset: 0.0,
                            text_matrix: RenderedTextMatrix::IDENTITY,
                            font_size,
                            font_id: Some(font_id),
                            font_palette: group_palette.clone(),
                            glyphs: std::mem::take(&mut group_glyphs),
                            glyph_source_ranges: std::mem::take(&mut group_source_ranges),
                        });
                        tab_contexts.push(RenderedRunTabContext {
                            style: group_style,
                            metric_style: tab_metric_style,
                        });
                        group_palette = palette.clone();
                        group_style = glyph_style;
                        group_x_offset = next_x_offset;
                        group_has_tab = false;
                    }
                    next_x_offset += glyph.x_advance;
                    group_text.push_str(&glyph.unicode);
                    group_has_tab |= glyph.unicode == "\t";
                    group_glyphs.push(glyph);
                    group_source_ranges.push(source_range);
                }
                rendered_runs.push(ShapedGlyphRun {
                    text: group_text.into(),
                    x_offset: group_x_offset,
                    y_offset: 0.0,
                    text_matrix: RenderedTextMatrix::IDENTITY,
                    font_size,
                    font_id: Some(font_id),
                    font_palette: group_palette,
                    glyphs: group_glyphs,
                    glyph_source_ranges: group_source_ranges,
                });
                tab_contexts.push(RenderedRunTabContext {
                    style: group_style,
                    metric_style: tab_metric_style,
                });
                // Glyph Unicode is a PDF ToUnicode summary, not the complete
                // CSS source stream. A ligature can be attached only to its
                // first source cluster, and U+200C/U+200D affect shaping
                // without producing a standalone glyph. When palette
                // grouping leaves this Parley run intact, retain its authored
                // (synthetic controls already removed) text so the PDF paint
                // boundary can detect and preserve any missing source text
                // with `/ActualText`.
                // ISO 32000-2:2020, 14.9.4.4, "ActualText".
                if rendered_runs.len() == rendered_run_start + 1 {
                    let grouped_glyph_text = rendered_runs[rendered_run_start]
                        .glyphs
                        .iter()
                        .map(|glyph| glyph.unicode.as_str())
                        .collect::<String>();
                    if grouped_glyph_text != authored_run_text {
                        rendered_runs[rendered_run_start].text = authored_run_text.into();
                    }
                }
            }
            for run in &mut rendered_runs {
                run.x_offset = corrected_visual_inline_position(
                    ShaperVisualInlinePosition::from_parley(run.x_offset),
                    &dropped_default_ignorable_runs,
                )
                .points();
            }
            stitch_dropped_join_control_runs(&mut rendered_runs, &dropped_default_ignorable_runs);
            for index in rehomed_control_fallback_runs.into_iter().rev() {
                if stitch_rehomed_control_fallback_run(&mut rendered_runs, index) {
                    tab_contexts.remove(index + 1);
                }
            }
            this.apply_css_tab_stops(
                &mut rendered_runs,
                &tab_contexts,
                tab_origin,
                letter_spacing,
            );
            rendered_runs
        })
    }

    /// Resolve authored styled spans through `unicode-range` face selection.
    ///
    /// The selected face is a shaping input, while the authored style remains
    /// the source of line metrics such as `font-size-adjust: from-font`.
    /// <https://www.w3.org/TR/css-fonts-4/#unicode-range-desc>
    fn resolved_styled_text_spans<'a>(
        &mut self,
        spans: &[StyledTextSpan<'a>],
    ) -> Vec<ResolvedStyledTextSpan<'a>> {
        // The common case emits exactly one resolved span for each authored
        // span. Reserve that shape up front, then reserve a known expansion
        // before appending a `unicode-range` split so this durable result does
        // not repeatedly grow while building Parley's input.
        let mut resolved_spans = Vec::with_capacity(spans.len());
        for span in spans.iter().filter(|span| !span.text.is_empty()) {
            if let Some(unicode_range_resolved) =
                self.unicode_range_resolved_text_spans(span.text, span.style)
            {
                resolved_spans.reserve(unicode_range_resolved.len());
                for resolved in unicode_range_resolved {
                    if let Some(text) = span.text.get(resolved.range) {
                        resolved_spans.push(ResolvedStyledTextSpan {
                            text,
                            shaping_style: SelectedFaceStyleView::new(
                                span.style,
                                resolved.selected_family,
                            ),
                            metric_style: span.style,
                        });
                    }
                }
            } else {
                resolved_spans.push(ResolvedStyledTextSpan {
                    text: span.text,
                    shaping_style: SelectedFaceStyleView::new(span.style, None),
                    metric_style: span.style,
                });
            }
        }
        resolved_spans
    }

    /// Build Parley's styled input and retain a map to authored CSS text.
    ///
    /// Join controls and emoji presentation selectors influence font and
    /// OpenType selection without becoming independently selectable source
    /// text. This method makes that internal/authored coordinate boundary
    /// explicit before any Parley cluster is converted to a durable glyph run.
    /// <https://www.w3.org/TR/css-text-3/#boundary-shaping> and
    /// <https://www.w3.org/TR/css-text-3/#text-processing-order>
    fn mapped_styled_shaping_text<'span, 'source>(
        resolved_spans: &'span [ResolvedStyledTextSpan<'source>],
    ) -> Option<MappedStyledShapingText<'span, 'source>> {
        let mut ranges = Vec::with_capacity(resolved_spans.len());
        let mut metric_ranges = Vec::with_capacity(resolved_spans.len());
        let mut shaping_contexts = ShapingContextMap::default();
        let authored_text = resolved_spans
            .iter()
            .map(|span| span.text)
            .collect::<String>();

        // Join controls are default-ignorable shaping instructions, not
        // visible font content. Their own inline style must therefore not
        // create a separate font-selection range: an author may isolate a
        // U+200C/U+200D in a span with a deliberately unrelated fallback
        // face, while the adjacent Arabic text still shapes as one context.
        // <https://www.w3.org/TR/css-text-3/#boundary-shaping> and
        // <https://www.w3.org/TR/alreq/#h_joining-enforcement>
        let mut shaping_spans = Vec::<StyledShapingSpan<'_, '_>>::new();
        let mut pending_join_controls = String::new();
        for span in resolved_spans {
            // Default-ignorables are font-neutral, but not source-neutral.
            // Excluding them from Parley's font-selection buffer avoids
            // giving it a second line-breaking authority. They remain in
            // `authored_text`, and `styled_text_source_positions` restores
            // their authored byte coordinates when clusters are selected.
            // In particular, a standalone U+200B must never turn into an
            // empty Parley style range.
            let span_text = text_without_font_neutral_default_ignorables(span.text);
            let span_text = span_text.as_ref();
            if span_text.is_empty() {
                continue;
            }
            if span_text.chars().all(character_is_join_control) {
                if let Some(previous) = shaping_spans.last_mut() {
                    previous.text.push_str(span_text);
                } else {
                    pending_join_controls.push_str(span_text);
                }
                continue;
            }
            let mut text = std::mem::take(&mut pending_join_controls);
            text.push_str(span_text);
            if let Some(previous) = shaping_spans.last_mut()
                && previous
                    .shaping_style
                    .has_same_effective_style(&span.shaping_style)
                && previous.metric_style == span.metric_style
                // Equal shaping inputs do not imply that there was no CSS
                // inline boundary. Keep independently authored styles as
                // separate shaping spans so boundary shaping can add its
                // synthetic context before the backend sees a flat string.
                // Unicode-range resolution may split one authored style; its
                // pieces retain the same style identity and may still merge.
                // <https://drafts.csswg.org/css-text-3/#boundary-shaping>
                && std::ptr::eq(previous.shaping_style.authored(), span.shaping_style.authored())
                && std::ptr::eq(previous.metric_style, span.metric_style)
            {
                previous.text.push_str(&text);
            } else {
                shaping_spans.push(StyledShapingSpan {
                    text,
                    shaping_style: &span.shaping_style,
                    metric_style: span.metric_style,
                });
            }
        }
        if !pending_join_controls.is_empty()
            && let Some(previous) = shaping_spans.last_mut()
        {
            previous.text.push_str(&pending_join_controls);
        }

        let mut text = String::new();
        for (index, span) in shaping_spans.iter().enumerate() {
            let start = text.len();
            if index > 0
                && shaping_spans.get(index - 1).is_some_and(|previous| {
                    matches!(
                        previous.shaping_style.boundary_effect(span.shaping_style),
                        InlineBoundaryEffect::ShapingInputChange
                    ) && span_boundary_needs_join_control(&previous.text, &span.text)
                })
            {
                push_synthetic_join_control(&mut text, &mut shaping_contexts);
            }
            push_text_with_font_variant_emoji(&mut text, &span.text, span.shaping_style.authored());
            if shaping_spans.get(index + 1).is_some_and(|next| {
                matches!(
                    next.shaping_style.boundary_effect(span.shaping_style),
                    InlineBoundaryEffect::ShapingInputChange
                ) && span_boundary_needs_join_control(&span.text, &next.text)
            }) {
                push_synthetic_join_control(&mut text, &mut shaping_contexts);
            }
            ranges.push((start..text.len(), span.shaping_style));
            metric_ranges.push((start..text.len(), span.metric_style));
        }
        if text.is_empty() || ranges.is_empty() {
            return None;
        }
        shaping_contexts.add_authored_join_controls(&text);
        debug_assert!(ranges.iter().all(|(range, _)| {
            range.start < range.end
                && range.end <= text.len()
                && text.is_char_boundary(range.start)
                && text.is_char_boundary(range.end)
        }));
        let source_positions =
            styled_text_source_positions(&text, &authored_text, &shaping_contexts);
        Some(MappedStyledShapingText {
            text,
            authored_text,
            ranges,
            metric_ranges,
            shaping_contexts,
            source_positions,
        })
    }

    /// Shape a selected font-face range with its complete cursive context.
    ///
    /// CSS Fonts limits the face that paints a character, not the neighboring
    /// characters that OpenType sees while selecting its contextual form.
    /// A ranged backend style otherwise shapes every face run in isolation,
    /// which makes an Arabic letter lose its joining form when its neighbors
    /// are selected from a different `unicode-range` face.  The returned
    /// glyphs retain only the selected source range; callers keep the
    /// backend's range placement and authored source provenance.
    /// <https://www.w3.org/TR/css-fonts-4/#unicode-range-desc>
    /// <https://www.w3.org/TR/css-text-3/#boundary-shaping>
    fn selected_face_contextual_glyphs(
        &mut self,
        text: &str,
        range: Range<usize>,
        style: &SelectedFaceStyleView<'_>,
        has_cursive_face_transition: bool,
        letter_spacing: ShapingLetterSpacing,
    ) -> Option<Vec<RenderedGlyph>> {
        has_cursive_face_transition.then_some(())?;
        let range_text = text.get(range.clone())?;
        range_text
            .chars()
            .any(character_has_cursive_shaping_behavior)
            .then_some(())?;

        let mut contextual_style = style.authored().clone();
        contextual_style.font_family = style.font_family().clone();
        // This cannot borrow the reusable layout scratch: callers are
        // converting a borrowed Parley line from the primary ranged pass.
        // A small local layout keeps the contextual probe independent.
        let parley_style = self.shaping_style_for_selected_face_view(style);
        let font_family_source = self.resolved_parley_font_family_source_for_family(
            style.font_family(),
            contextual_style.font_weight,
            contextual_style.font_style,
            contextual_style.font_width,
        );
        let feature_context = self.font_feature_context_for_selected_face(style);
        let mut layout = ParleyLayout::default();
        let mut builder: parley::RangedBuilder<'_, FontPalette> = self
            .parley_layout_context
            .ranged_builder(&mut self.parley_font_context, text, 1.0, false);
        push_parley_default_style(&mut builder, &parley_style, &font_family_source);
        push_parley_text_spacing_default_with_context(
            &mut builder,
            text,
            &contextual_style,
            letter_spacing.requested_for(&contextual_style),
            feature_context.as_ref(),
        );
        builder.build_into(&mut layout, text);
        layout.break_all_lines(None);
        let line = layout.lines().next()?;
        let glyphs = self
            .rendered_text_runs_for_parley_line(text, line, &contextual_style, letter_spacing)
            .into_iter()
            .flat_map(|run| run.glyphs.into_iter().zip(run.glyph_source_ranges))
            .filter_map(|(glyph, source_range)| {
                source_range
                    .is_some_and(|source_range| {
                        source_range.start < range.end && range.start < source_range.end
                    })
                    .then_some(glyph)
            })
            .collect::<Vec<_>>();
        (!glyphs.is_empty()).then_some(glyphs)
    }

    /// Select concrete families for presentation-sensitive grapheme clusters.
    ///
    /// CSS Fonts matches a variation selector with its preceding base, rather
    /// than selecting one face for an entire text run. This is observable when
    /// a keycap or emoji occurs after ordinary text in the same run.
    /// <https://www.w3.org/TR/css-fonts-4/#font-variant-emoji-prop>
    /// <https://www.w3.org/TR/css-fonts-4/#cluster-matching>
    pub(in crate::text) fn emoji_presentation_family_ranges(
        &mut self,
        text: &str,
        style: &ComputedStyle,
        family: &FontFamily,
    ) -> Vec<EmojiPresentationFamilyRange> {
        let boundaries = GraphemeClusterSegmenter::new()
            .segment_str(text)
            .collect::<Vec<_>>();
        let mut ranges = Vec::new();
        for range in boundaries.windows(2).map(|pair| pair[0]..pair[1]) {
            let cluster = &text[range.clone()];
            let characters = cluster
                .chars()
                .filter(|character| !character_is_bidi_format_control(*character))
                .collect::<Vec<_>>();
            let Some((base_index, base)) =
                characters
                    .iter()
                    .copied()
                    .enumerate()
                    .find(|(_, character)| {
                        emoji_presentation_participating_code_point(*character)
                            || character_is_emoji(*character)
                    })
            else {
                continue;
            };
            let selector = characters[base_index + 1..]
                .iter()
                .copied()
                .find(|character| matches!(character, '\u{fe0e}' | '\u{fe0f}'));
            let requested = match selector {
                Some('\u{fe0e}') => EmojiPresentationCapability::Text,
                Some('\u{fe0f}') => EmojiPresentationCapability::Emoji,
                None if style.font_variant_emoji == FontVariantEmoji::Unicode => {
                    if character_has_emoji_presentation(base) {
                        EmojiPresentationCapability::Emoji
                    } else {
                        EmojiPresentationCapability::Text
                    }
                }
                None => {
                    // `normal` deliberately leaves presentation to ordinary
                    // platform matching when no author selector is present.
                    continue;
                }
                Some(_) => unreachable!("only emoji presentation selectors are selected"),
            };
            let selected = self
                .emoji_presentation_font_candidates_for_family(style, family, base)
                .into_iter()
                .find(|font_id| {
                    self.document_fonts
                        .emoji_presentation_capability(*font_id, base, selector)
                        == Some(requested)
                })
                .or_else(|| {
                    // CSS Fonts falls back to the base glyph when no face
                    // supports the selector. Do not force a presentation face
                    // in that case.
                    self.emoji_presentation_font_candidates_for_family(style, family, base)
                        .into_iter()
                        .find(|font_id| self.document_fonts.font_has_character(*font_id, base))
                });
            if let Some(font_id) = selected
                && let Some(font) = self.document_fonts.get(font_id)
            {
                ranges.push(EmojiPresentationFamilyRange {
                    range,
                    source: parley_font_family_source(&FontFamily::named(font.family.clone())),
                });
            }
        }
        ranges
    }

    /// Return the CSS stack's usable faces in precedence order, then the
    /// platform fallback. This is deliberately kept at the font-selection
    /// boundary so that shaping, metric lookup, and PDF embedding refer to
    /// the same concrete document font.
    fn emoji_presentation_font_candidates_for_family(
        &mut self,
        style: &ComputedStyle,
        family: &FontFamily,
        character: char,
    ) -> Vec<usize> {
        let families = match family {
            FontFamily::List(families) => families.clone(),
            family => vec![family.clone()],
        };
        let mut candidates = Vec::new();
        for family in families {
            match family {
                FontFamily::Named(name) => {
                    if let Some(font_id) = self.resolve_single_family(
                        name.as_str(),
                        style.font_weight,
                        style.font_style,
                        style.font_width,
                    ) {
                        candidates.push(font_id);
                    }
                }
                generic => {
                    if let Some(font_id) = self.resolve_generic_family(
                        &generic,
                        style.font_weight,
                        style.font_style,
                        style.font_width,
                    ) {
                        candidates.push(font_id);
                    }
                }
            }
        }
        if let Some(font_id) = self.resolve_system_fallback_for_style_character(style, character) {
            candidates.push(font_id);
        }
        // Keep CSS fallback order: it is observable when two faces offer the
        // requested presentation. `dedup` after sorting would incorrectly
        // reorder the author-provided stack.
        candidates.dedup();
        candidates
    }

    pub(in crate::text) fn visible_text_fallback_for_run(
        &mut self,
        text: &str,
        style: &ComputedStyle,
        current_font_id: usize,
    ) -> Option<usize> {
        self.visible_text_fallback_for_run_in_family(
            text,
            style,
            &style.font_family,
            current_font_id,
        )
    }

    fn visible_text_fallback_for_run_for_selected_face(
        &mut self,
        text: &str,
        style: &SelectedFaceStyleView<'_>,
        current_font_id: usize,
    ) -> Option<usize> {
        self.visible_text_fallback_for_run_in_family(
            text,
            style.authored(),
            style.font_family(),
            current_font_id,
        )
    }

    fn visible_text_fallback_for_run_in_family(
        &mut self,
        text: &str,
        style: &ComputedStyle,
        family: &FontFamily,
        current_font_id: usize,
    ) -> Option<usize> {
        let mut fallback_font_id = None;
        for character in text
            .chars()
            .filter(|character| !character_is_default_ignorable_code_point(*character))
        {
            let candidate =
                self.resolve_family_fallback_for_character_in_family(style, family, character)?;
            if candidate == current_font_id {
                return None;
            }
            match fallback_font_id {
                Some(existing) if existing != candidate => return None,
                Some(_) => {}
                None => fallback_font_id = Some(candidate),
            }
        }
        fallback_font_id
    }
}

#[derive(Debug, Clone)]
struct StyledTextSourcePosition {
    internal: Range<usize>,
    authored: Range<usize>,
}

/// Map Parley's augmented styled-shaping buffer back to authored text.
///
/// Styled shaping inserts join controls and edge context solely for OpenType
/// shaping. Their byte positions are not positions in the CSS text stream,
/// which is retained separately for line selection and PDF extraction. Keep
/// cluster provenance in that authored coordinate system so selected source
/// slices never use an offset into the synthetic buffer:
/// <https://www.w3.org/TR/css-text-3/#boundary-shaping> and
/// <https://www.w3.org/TR/css-text-3/#text-processing-order>.
fn styled_text_source_positions(
    internal_text: &str,
    authored_text: &str,
    shaping_contexts: &ShapingContextMap,
) -> Vec<StyledTextSourcePosition> {
    let mut positions = Vec::new();
    let mut authored = authored_text.char_indices().peekable();
    for (start, character) in internal_text.char_indices() {
        let end = start + character.len_utf8();
        if shaping_contexts.is_synthetic_at(start)
            && shaping_contexts.is_synthetic_at(end.saturating_sub(1))
        {
            continue;
        }
        loop {
            let Some((authored_start, authored_character)) = authored.next() else {
                return Vec::new();
            };
            let authored_end = authored
                .peek()
                .map_or(authored_text.len(), |(next, _)| *next);
            if character_is_font_neutral_default_ignorable(authored_character) {
                continue;
            }
            if character != authored_character {
                return Vec::new();
            }
            positions.push(StyledTextSourcePosition {
                internal: start..end,
                authored: authored_start..authored_end,
            });
            break;
        }
    }
    if authored.all(|(_, character)| character_is_font_neutral_default_ignorable(character)) {
        positions
    } else {
        Vec::new()
    }
}

fn styled_authored_text_for_internal_range<'a>(
    internal: Range<usize>,
    positions: &[StyledTextSourcePosition],
    authored_text: &'a str,
) -> Option<&'a str> {
    let authored = styled_cluster_source_range(internal, positions)?;
    authored_text.get(authored)
}

fn styled_cluster_source_range(
    cluster: Range<usize>,
    positions: &[StyledTextSourcePosition],
) -> Option<Range<usize>> {
    let mut authored_start = None;
    let mut authored_end = None;
    for position in positions {
        if position.internal.end <= cluster.start || cluster.end <= position.internal.start {
            continue;
        }
        if position.internal.start < cluster.start || cluster.end < position.internal.end {
            return None;
        }
        authored_start.get_or_insert(position.authored.start);
        authored_end = Some(position.authored.end);
    }
    authored_start
        .zip(authored_end)
        .map(|(start, end)| start..end)
}

/// Attribute a control-only Parley cluster to the typographic unit whose
/// contextual form it carries.
///
/// HarfBuzz is permitted to associate an Arabic/N'Ko/Mongolian glyph with the
/// following U+200C/U+200D cluster rather than the preceding letter. The
/// control has no layout ownership of its own; for source slicing it belongs
/// to the preceding authored typographic unit, or to the following unit at a
/// stream start. This is input-range provenance, not a glyph-ID heuristic, so
/// it remains valid after OpenType substitution.
/// <https://drafts.csswg.org/css-text-3/#boundary-shaping>
fn styled_join_control_cluster_source_range(
    cluster: Range<usize>,
    positions: &[StyledTextSourcePosition],
    authored_text: &str,
) -> Option<Range<usize>> {
    positions
        .iter()
        .rev()
        .filter(|position| {
            authored_text
                .get(position.authored.clone())
                .is_some_and(|text| !text.chars().all(character_is_join_control))
        })
        .find(|position| position.internal.end <= cluster.start)
        .or_else(|| {
            positions
                .iter()
                .filter(|position| {
                    authored_text
                        .get(position.authored.clone())
                        .is_some_and(|text| !text.chars().all(character_is_join_control))
                })
                .find(|position| cluster.end <= position.internal.start)
        })
        .map(|position| position.authored.clone())
}

mod tests {
    #[cfg(test)]
    use super::{
        InlineBoundaryEffect, SelectedFaceStyleView, ShapingContextMap,
        styled_cluster_source_range, styled_join_control_cluster_source_range,
        styled_text_source_positions,
    };
    #[cfg(test)]
    use crate::CssColor;
    #[cfg(test)]
    use crate::css::{ComputedStyle, FontFamily, FontStyle};

    #[test]
    fn selected_face_view_borrows_the_authored_style_when_unmodified() {
        let style = ComputedStyle::initial();
        let borrowed = SelectedFaceStyleView::new(&style, None);

        assert!(std::ptr::eq(borrowed.authored(), &style));
        assert!(std::ptr::eq(borrowed.font_family(), &style.font_family));

        let selected_family = FontFamily::Serif;
        let selected = SelectedFaceStyleView::new(&style, Some(selected_family.clone()));
        assert_eq!(selected.font_family(), &selected_family);
        assert!(!borrowed.has_same_effective_style(&selected));
    }

    #[test]
    fn boundary_effect_ignores_paint_only_changes_but_tracks_font_style() {
        let base = ComputedStyle::initial();
        let mut color = base.clone();
        color.color = CssColor::new(255, 0, 0);
        let mut italic = base.clone();
        italic.font_style = FontStyle::DEFAULT_OBLIQUE;

        let base_view = SelectedFaceStyleView::new(&base, None);
        let color_view = SelectedFaceStyleView::new(&color, None);
        let italic_view = SelectedFaceStyleView::new(&italic, None);

        assert_eq!(
            base_view.boundary_effect(&color_view),
            InlineBoundaryEffect::PaintOnly
        );
        assert_eq!(
            base_view.boundary_effect(&italic_view),
            InlineBoundaryEffect::ShapingInputChange
        );
    }

    #[test]
    fn styled_cluster_ranges_exclude_synthetic_join_context() {
        let internal = "ا\u{200d}ل\u{0640}سلا\u{200d}م";
        let synthetic = ShapingContextMap::from_synthetic_ranges(&[2..5, 7..9, 15..18]);
        let positions = styled_text_source_positions(internal, "السلام", &synthetic);

        assert_eq!(styled_cluster_source_range(5..7, &positions), Some(2..4),);
        assert_eq!(styled_cluster_source_range(9..13, &positions), Some(4..8),);
        assert_eq!(styled_cluster_source_range(2..5, &positions), None,);
    }

    #[test]
    fn join_control_cluster_borrows_the_preceding_authored_unit() {
        let positions = styled_text_source_positions(
            "ع\u{200d}\u{200d}ع\u{200d}\u{200d}ع",
            "ع\u{200d}\u{200d}ع\u{200d}\u{200d}ع",
            &ShapingContextMap::default(),
        );

        assert_eq!(
            styled_join_control_cluster_source_range(
                13..16,
                &positions,
                "ع\u{200d}\u{200d}ع\u{200d}\u{200d}ع"
            ),
            Some(8..10)
        );
        assert_eq!(
            styled_join_control_cluster_source_range(
                5..8,
                &positions,
                "ع\u{200d}\u{200d}ع\u{200d}\u{200d}ع"
            ),
            Some(0..2)
        );
    }
}