ferri-pdf 0.1.10

HTML and CSS to PDF renderer for print, in pure Rust
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
use super::common;
use crate::dom::{DocumentTree, Selector};
use crate::fonts::{FontFamily, FontStyle, FontWeight, GenericFamily};
use crate::style::{LineHeight, Style, StyleError, Stylesheet, StylesheetSource};

fn computed(css: &str) -> Result<Style, StyleError> {
    computed_at(css, Default::default())
}

fn computed_at(css: &str, base: crate::ResourceBase) -> Result<Style, StyleError> {
    let sheet = Stylesheet::parse(&[StylesheetSource {
        text: css.into(),
        base,
    }])?;
    let tree = DocumentTree::parse("<div id=parent><p id=target>Font text</p></div>");
    let parent = tree
        .select(&Selector::parse("#parent").unwrap())
        .next()
        .unwrap();
    let target = tree
        .select(&Selector::parse("#target").unwrap())
        .next()
        .unwrap();
    let parent = sheet.computed(parent, &Style::default())?;
    sheet.computed(target, &parent)
}

#[test]
fn font_shorthand_forms_and_var_expansion() {
    for (source, size, leading, style, weight) in [
        (
            "12pt/18pt serif",
            12.0,
            Some(18.0),
            FontStyle::Normal,
            FontWeight::NORMAL,
        ),
        (
            "italic bold 18px/1.5 serif",
            13.5,
            Some(20.25),
            FontStyle::Italic,
            FontWeight::BOLD,
        ),
        (
            "700 italic 120%/140% serif",
            24.0,
            Some(33.6),
            FontStyle::Italic,
            FontWeight::BOLD,
        ),
        (
            "normal normal normal normal 14pt serif",
            14.0,
            None,
            FontStyle::Normal,
            FontWeight::NORMAL,
        ),
        (
            "calc(8pt * 2)/2 serif",
            16.0,
            Some(32.0),
            FontStyle::Normal,
            FontWeight::NORMAL,
        ),
    ] {
        let actual = computed(&format!(
            "#parent{{font-size:20pt}}#target{{--face:{source};font:var(--face)}}"
        ))
        .unwrap_or_else(|error| panic!("font {source} must compute: {error}"));
        common::assert_pt(actual.size, size);
        match (actual.line_height.resolve(actual.size), leading) {
            (Some(actual), Some(expected)) => common::assert_pt(actual, expected),
            (actual, expected) => assert_eq!(actual, expected, "font {source}"),
        }
        assert_eq!(actual.font_style, style, "font {source}");
        assert_eq!(actual.font_weight, weight, "font {source}");
        assert_eq!(
            actual.families,
            vec![FontFamily::Generic(GenericFamily::Serif)]
        );
    }
}

#[test]
fn font_shorthand_resets_only_its_defined_properties() {
    let features = |style: &Style| {
        (
            style.font_features.clone(),
            style.font_adjustments.clone(),
            style.font_variant_caps,
        )
    };
    let initial = features(&Style::default());
    for (property, value) in [
        ("font-feature-settings", "'kern' 0"),
        ("font-kerning", "none"),
        ("font-language-override", "'TRK'"),
        ("font-optical-sizing", "none"),
        ("font-size-adjust", "0.6"),
        ("font-variant-alternates", "historical-forms"),
        ("font-variant-caps", "small-caps"),
        ("font-variant-east-asian", "ruby"),
        ("font-variant-emoji", "emoji"),
        ("font-variant-ligatures", "none"),
        ("font-variant-numeric", "oldstyle-nums"),
        ("font-variant-position", "super"),
        ("font-variation-settings", "'wght' 750"),
    ] {
        let before = computed(&format!("#target{{{property}:{value}}}"))
            .unwrap_or_else(|error| panic!("{property}:{value} must compute: {error}"));
        assert_ne!(
            features(&before),
            initial,
            "the control must set a non-initial value for {property}:{value}"
        );
        let after = computed(&format!("#target{{{property}:{value};font:12pt serif}}"))
            .unwrap_or_else(|error| panic!("{property}:{value} then font must compute: {error}"));
        assert_eq!(
            features(&after),
            initial,
            "font must reset {property}:{value} to its initial value"
        );
    }
    let kept = computed(
        "#target{font-palette:light;font-synthesis:none;font-width:condensed;font:12pt serif}",
    )
    .unwrap();
    assert_eq!(kept.size, 12.0);
    assert_eq!(kept.font_width, crate::fonts::FontWidth::NORMAL);
    assert_eq!(
        kept.font_synthesis,
        crate::fonts::FontSynthesis::default(),
        "font must leave synthesis independently cascaded"
    );
    assert_eq!(
        kept.font_features.palette,
        crate::fonts::FontPalette::Light,
        "font must leave font-palette independently cascaded"
    );
}

#[test]
fn palette_mixes_equivalent_to_one_palette_compute_to_it() {
    use crate::fonts::FontPalette;
    for (value, expected) in [
        ("palette-mix(in oklab, light, light)", FontPalette::Light),
        (
            "palette-mix(in srgb, --a 100%, dark 0%)",
            FontPalette::Named("--a".into()),
        ),
        (
            "palette-mix(in hsl, normal 70%, normal)",
            FontPalette::Normal,
        ),
    ] {
        let style = computed(&format!("#target{{font-palette:{value}}}")).unwrap();
        assert_eq!(style.font_features.palette, expected, "{value}");
    }
    for value in [
        "palette-mix(in srgb, light, dark)",
        "palette-mix(in srgb, light 30%, light 30%)",
    ] {
        let style = computed(&format!("#target{{font-palette:{value}}}")).unwrap();
        assert!(
            matches!(style.font_features.palette, FontPalette::Mix(_)),
            "{value} mixes distinct palettes or keeps a shortfall's alpha: {:?}",
            style.font_features.palette
        );
    }
}

#[test]
fn system_keywords_and_named_family_positions_differ() {
    for keyword in [
        "caption",
        "icon",
        "menu",
        "message-box",
        "small-caption",
        "status-bar",
    ] {
        let actual = computed(&format!(
            "#parent{{font-size:25pt;font-weight:bold;font-style:italic}}#target{{font:{keyword}}}"
        ))
        .unwrap();
        let default = Style::default();
        assert_eq!(
            actual.size, default.size,
            "{keyword} uses the static UA substitute"
        );
        assert_eq!(actual.families, default.families);
        assert_eq!(actual.font_style, default.font_style);
        assert_eq!(actual.font_weight, default.font_weight);
        assert_eq!(actual.line_height, LineHeight::Normal);
    }
    let actual = computed("#target{font:18pt menu, 'serif', sans-serif}").unwrap();
    assert_eq!(
        actual.families,
        vec![
            FontFamily::Named("menu".into()),
            FontFamily::Named("serif".into()),
            FontFamily::Generic(GenericFamily::SansSerif)
        ]
    );
}

#[test]
fn font_width_stretch_aliases_share_cascade_and_variables() {
    for declarations in [
        "font-width:condensed;font-stretch:normal",
        "font-stretch:condensed;font-width:normal",
        "font-width:condensed;font:12pt serif",
        "--width:normal;font-width:condensed;font-stretch:var(--width)",
        "--width:condensed;font-stretch:var(--width);font-width:normal",
    ] {
        let actual = computed(&format!("#target{{{declarations}}}")).unwrap_or_else(|error| {
            panic!("a later canonical normal width must win for {declarations}: {error}")
        });
        assert_eq!(
            actual.font_width,
            crate::fonts::FontWidth::NORMAL,
            "{declarations}"
        );
    }
    let actual = computed("#target{font-width:condensed!important;font-stretch:normal}").unwrap();
    assert_eq!(actual.font_width, crate::fonts::FontWidth::CONDENSED);
}

#[test]
fn invalid_whole_fonts_and_invalid_computed_fonts_follow_different_cascade_rules() {
    for invalid in [
        "italic italic 12pt serif",
        "bold 700 12pt serif",
        "normal normal normal normal normal 12pt serif",
        "12pt",
        "12pt/ serif",
        "12pt serif,",
        "condensed expanded 12pt serif",
        "75% 12pt serif",
    ] {
        let actual = computed(&format!(
            "#target{{font-size:17pt;font-weight:bold;font:{invalid}}}"
        ))
        .unwrap();
        assert_eq!(actual.size, 17.0, "invalid whole shorthand {invalid}");
        assert_eq!(actual.font_weight, FontWeight::BOLD);
    }
    let actual = computed("#parent{font-size:20pt;font-weight:normal}#target{font-size:17pt;font-weight:bold;font:var(--missing)}").unwrap();
    assert_eq!(
        actual.size, 20.0,
        "invalid-at-computed-time font is unset rather than falling back to an earlier size"
    );
    assert_eq!(actual.font_weight, FontWeight::NORMAL);
}

#[test]
fn font_shorthand_inheritance_and_layer_rollback_keep_the_selected_values() {
    let actual =
        computed("#parent{font:italic bold 20pt/1.5 serif}#target{font:inherit;font-size:10pt}")
            .unwrap();
    assert_eq!(actual.size, 10.0);
    assert_eq!(actual.line_height.resolve(actual.size), Some(15.0));
    assert_eq!(actual.font_style, FontStyle::Italic);
    assert_eq!(actual.font_weight, FontWeight::BOLD);
    let actual = computed("@layer base, override;@layer base{#target{font:bold 16pt/2 serif}}@layer override{#target{font:italic 20pt monospace;font:revert-layer}}").unwrap();
    assert_eq!(actual.size, 16.0);
    assert_eq!(actual.font_weight, FontWeight::BOLD);
    assert_eq!(actual.line_height.resolve(actual.size), Some(32.0));
    let actual =
        computed("#target{font:bold 17pt/2 serif;font-synthesis:none;all:initial}").unwrap();
    let initial = Style::default();
    assert_eq!(actual.size, initial.size);
    assert_eq!(actual.families, initial.families);
    assert_eq!(actual.font_weight, initial.font_weight);
    assert_eq!(actual.line_height, initial.line_height);
    assert_eq!(
        actual.font_synthesis, initial.font_synthesis,
        "all resets synthesis even though font does not"
    );
}

#[test]
fn font_shorthand_queries_follow_actual_supported_longhands() {
    for (source, supported) in [
        ("12pt serif", true),
        ("normal normal normal normal 14pt serif", true),
        ("italic bold 16pt/1.5 serif", true),
        ("menu", true),
        ("small-caps 12pt serif", true),
        ("condensed 12pt serif", true),
        ("oblique 25deg 12pt serif", true),
        ("12pt", false),
    ] {
        let actual = computed(&format!(
            "#target{{font-size:9pt}}@supports(font:{source}){{#target{{font-size:18pt}}}}"
        ))
        .unwrap();
        assert_eq!(
            actual.size,
            if supported { 18.0 } else { 9.0 },
            "support query for font:{source}"
        );
    }
}

#[test]
fn default_font_shorthand_reaches_shaping_and_pdf_serialization() {
    let html = format!(
        "<style>{}p{{font:italic bold 18pt/1.5 serif}}</style><p>Shared font shorthand</p>",
        common::PROFILE
    );
    let output = crate::render_html(&html, &common::local_options()).unwrap();
    let trace = output.layout.unwrap();
    assert_eq!(trace.pages.len(), 1);
    let line = &trace.pages[0][0];
    assert_eq!(line.text, "Shared font shorthand");
    assert_eq!(line.font_size, 18.0);
    assert!(!line.runs.is_empty());
}

const VARIABLE_FAMILY: &str = "Variable Fixture";
const VARIABLE_SIZE_PT: f32 = 20.0;

fn variable_options() -> crate::RenderOptions {
    let mut options = common::local_options();
    options.base = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/fonts")
        .into();
    options
}

fn variable_font(descriptors: &str, declarations: &str) -> crate::RenderOutput {
    let html = format!(
        "<style>@font-face{{font-family:'{VARIABLE_FAMILY}';src:url(RobotoFlex.ttf) format('truetype');{descriptors}}}@page{{size:300pt 200pt;margin:20pt}}p{{margin:0;font-family:'{VARIABLE_FAMILY}';font-size:{VARIABLE_SIZE_PT}pt;line-height:24pt;font-optical-sizing:none;{declarations}}}</style><p>H</p>"
    );
    crate::render_html(&html, &variable_options()).unwrap_or_else(|error| {
        panic!("variable face {descriptors:?}, request {declarations:?}: {error}")
    })
}

fn independent_variable_advance(sample: usize) -> f32 {
    let metrics: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/RobotoFlex-metrics.json"
    ))
    .unwrap();
    let units = metrics["samples"][sample]["advance"].as_f64().unwrap();
    let upem = metrics["units_per_em"].as_f64().unwrap();
    // The shaper reports nearest whole font units; FontTools retains fractional advances.
    (units.round() / upem) as f32 * VARIABLE_SIZE_PT
}

#[test]
fn variable_weight_width_and_slant_use_independently_measured_instances() {
    let metrics: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/RobotoFlex-metrics.json"
    ))
    .unwrap();
    for (sample, measured) in metrics["samples"].as_array().unwrap().iter().enumerate() {
        let location = &measured["location"];
        let weight = location["wght"].as_f64().unwrap();
        let width = location["wdth"].as_f64().unwrap();
        let angle = -location["slnt"].as_f64().unwrap();
        let request =
            format!("font-weight:{weight};font-width:{width}%;font-style:oblique {angle}deg");
        let output = variable_font("", &request);
        let trace = output.layout.unwrap();
        let line = &trace.pages[0][0];
        assert_eq!(line.text, "H", "{request}");
        common::assert_pt(line.width, independent_variable_advance(sample));
        for run in &line.runs {
            assert_eq!(trace.fonts[run.font_id.index()].family, VARIABLE_FAMILY);
            assert!(
                !run.synthesis.bold && !run.synthesis.oblique,
                "native axes must supply this instance, without synthetic styling: {request}"
            );
        }
    }
}

#[test]
fn descriptor_ranges_clamp_instances_and_reversed_endpoints_are_swapped() {
    for descriptors in [
        "font-weight:300 600;font-width:75% 125%;font-style:oblique 0deg 10deg",
        "font-weight:600 300;font-width:125% 75%;font-style:oblique 10deg 0deg",
    ] {
        for (request, sample) in [
            (
                "font-weight:900;font-width:200%;font-style:oblique 25deg",
                9,
            ),
            ("font-weight:100;font-width:50%;font-style:normal", 10),
        ] {
            let trace = variable_font(descriptors, request).layout.unwrap();
            common::assert_pt(
                trace.pages[0][0].width,
                independent_variable_advance(sample),
            );
        }
    }
}

#[test]
fn face_matching_narrows_width_before_style_and_weight() {
    let faces: String = [
        (75, "normal", 400),
        (100, "italic", 400),
        (100, "normal", 700),
        (125, "normal", 400),
    ]
    .into_iter()
    .map(|(width, style, weight)| {
        format!("@font-face{{font-family:'{VARIABLE_FAMILY}';src:url(RobotoFlex.ttf);font-width:{width}%;font-style:{style};font-weight:{weight}}}")
    })
    .collect();
    for (request, expected_units) in [
        ("font-width:100%;font-style:normal;font-weight:400", 1508.0),
        ("font-width:100%;font-style:italic;font-weight:700", 1458.0),
        ("font-width:90%;font-style:italic;font-weight:700", 1406.0),
        ("font-width:110%;font-style:normal;font-weight:400", 1536.0),
    ] {
        let html = format!(
            "<style>{faces}p{{font-family:'{VARIABLE_FAMILY}';font-size:{VARIABLE_SIZE_PT}pt;line-height:24pt;font-optical-sizing:none;{request}}}</style><p>H</p>"
        );
        let output = crate::render_html(&html, &variable_options())
            .unwrap_or_else(|error| panic!("face matching for {request}: {error}"));
        common::assert_pt(
            output.layout.unwrap().pages[0][0].width,
            expected_units / 2048.0 * VARIABLE_SIZE_PT,
        );
    }
}

#[test]
fn modern_font_source_hints_skip_unsupported_technology_and_report_policy_failures() {
    const FONT_BYTE_LIMIT: usize = 4;
    let html = format!(
        "<style>@font-face{{font-family:'{VARIABLE_FAMILY}';src:url('../../../assets/fonts/DejaVuSans.ttf') format(truetype) tech(features-graphite),url(RobotoFlex.ttf) format(truetype) tech(variations)}}p{{font-family:'{VARIABLE_FAMILY}';font-size:{VARIABLE_SIZE_PT}pt;line-height:24pt;font-optical-sizing:none}}</style><p>H</p>"
    );
    let options = variable_options();
    let output = crate::render_html(&html, &options)
        .expect("a supported modern format/technology entry must survive an unsupported one");
    let trace = output.layout.unwrap();
    assert_eq!(
        trace.fonts[trace.pages[0][0].runs[0].font_id.index()].family,
        VARIABLE_FAMILY,
        "unsupported source hints must not cause the entire face to disappear"
    );
    common::assert_pt(trace.pages[0][0].width, independent_variable_advance(1));
    assert_eq!(
        computed("#target{font-size:10pt}@supports font-tech(variations){#target{font-size:20pt}}")
            .unwrap()
            .size,
        VARIABLE_SIZE_PT,
        "variation support queries must agree with the real source and rendering path"
    );
    let resolver = crate::FileResolver {
        max_resource_bytes: FONT_BYTE_LIMIT,
        ..Default::default()
    };
    let output = crate::render_with_resolver(&html, &options, &resolver)
        .expect("a font over the resource limit is unavailable, not fatal");
    assert!(
        matches!(
            output.diagnostics.as_slice(),
            [crate::RenderDiagnostic::UnavailableFont(failure)]
                if failure.reference == "RobotoFlex.ttf"
                    && matches!(failure.error, crate::fonts::FontError::Resource(
                        crate::ResourceError::TooLarge { limit: FONT_BYTE_LIMIT, .. }
                    ))
        ),
        "font technology fallback must still report the resource policy failure: {:?}",
        output.diagnostics
    );
    let trace = output.layout.unwrap();
    assert_ne!(
        trace.fonts[trace.pages[0][0].runs[0].font_id.index()].family,
        VARIABLE_FAMILY,
        "text falls back past the unavailable face"
    );
}

#[test]
fn invalid_font_faces_do_not_abort_document_rendering() {
    for face in [
        "@font-face{font-family:Missing}",
        "@font-face{src:url(missing.ttf)}",
        "@font-face{font-family:Missing;src:url(missing.ttf) format('unknown-format')}",
    ] {
        let html = format!("<style>{face}p{{font-family:Missing,serif}}</style><p>Fallback</p>");
        let output = crate::render_html(&html, &variable_options())
            .unwrap_or_else(|error| panic!("invalid face must be ignored: {face}: {error}"));
        assert_eq!(output.layout.unwrap().pages[0][0].text, "Fallback");
    }
}

#[test]
fn font_source_lists_recover_invalid_items_without_losing_valid_ones() {
    for sources in [
        "url(RobotoFlex.ttf) format(unknown),url(RobotoFlex.ttf) format(truetype)",
        "url(RobotoFlex.ttf) tech(unknown-tech),url(RobotoFlex.ttf)",
        "local(),url(RobotoFlex.ttf)",
        "url(RobotoFlex.ttf) format(truetype,woff2),url(RobotoFlex.ttf)",
        "url(RobotoFlex.ttf) format(unknown),url(RobotoFlex.ttf) format('truetype-variations')",
    ] {
        let html = format!(
            "<style>@font-face{{font-family:'{VARIABLE_FAMILY}';src:{sources}}}p{{font-family:'{VARIABLE_FAMILY}';font-size:{VARIABLE_SIZE_PT}pt;line-height:24pt;font-optical-sizing:none}}</style><p>H</p>"
        );
        let output = crate::render_html(&html, &variable_options())
            .unwrap_or_else(|error| panic!("source list {sources}: {error}"));
        let trace = output.layout.unwrap();
        let run = &trace.pages[0][0].runs[0];
        assert_eq!(
            trace.fonts[run.font_id.index()].family,
            VARIABLE_FAMILY,
            "invalid items must not discard a usable source: {sources}"
        );
        common::assert_pt(trace.pages[0][0].width, independent_variable_advance(1));
    }
}

#[test]
fn oblique_angle_bounds_compare_canonical_degrees_before_cascade() {
    for (source, expected) in [
        ("0.25turn", Some(90.0)),
        ("-0.25turn", Some(-90.0)),
        ("100grad", Some(90.0)),
        ("0.5rad", Some(0.5_f32.to_degrees())),
        ("0.5turn", None),
        ("101grad", None),
        ("-91deg", None),
    ] {
        let actual = computed(&format!(
            "#target{{font-style:italic;font-style:oblique {source}}}"
        ))
        .unwrap_or_else(|error| panic!("oblique {source}: {error}"));
        if let Some(degrees) = expected {
            assert_eq!(actual.font_style, FontStyle::Oblique);
            common::assert_pt(actual.font_slant.value(), degrees);
        } else {
            assert_eq!(
                actual.font_style,
                FontStyle::Italic,
                "invalid oblique {source} must leave the earlier declaration intact"
            );
        }
    }
}

#[test]
fn font_relative_units_measure_the_same_variable_instance_as_shaping() {
    let metrics: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/RobotoFlex-metrics.json"
    ))
    .unwrap();
    let parent = metrics["samples"][5]["zero_advance"].as_f64().unwrap();
    let child = metrics["samples"][1]["zero_advance"].as_f64().unwrap();
    let upem = metrics["units_per_em"].as_f64().unwrap();
    let parent_ch = parent.round() as f32 / upem as f32;
    let child_ch = child.round() as f32 / upem as f32;
    assert_ne!(
        parent_ch, child_ch,
        "the fixture must distinguish the parent and child width instances"
    );
    let actual = computed_at(
        &format!("@font-face{{font-family:'{VARIABLE_FAMILY}';src:url(RobotoFlex.ttf)}}#parent{{font-family:'{VARIABLE_FAMILY}';font-size:{VARIABLE_SIZE_PT}pt;font-width:75%;font-optical-sizing:none}}#target{{font-size:1ch;font-width:100%;width:1ch}}"),
        variable_options().base,
    ).unwrap();
    common::assert_pt(actual.size, parent_ch * VARIABLE_SIZE_PT);
    common::assert_pt(
        actual.width.resolve_definite(None).unwrap(),
        child_ch * actual.size,
    );
}

fn caps_render(text: &str, declarations: &str, language: &str) -> crate::RenderOutput {
    let html = format!(
        "<style>@font-face{{font-family:CapsFixture;src:url(SourceSerif4-Regular.ttf)}}@font-face{{font-family:Limited;src:url(FontStyleTest-Ital.ttf)}}@page{{size:300pt 200pt;margin:20pt}}p{{margin:0;font:20pt/28pt serif;{declarations}}}</style><p lang='{language}'>{text}</p>"
    );
    crate::render_html(&html, &variable_options()).unwrap_or_else(|error| {
        panic!("caps {text:?}, {declarations:?}, language {language:?}: {error}")
    })
}

#[test]
fn small_caps_uses_available_features_or_defined_synthesis() {
    let measured: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/SmallCaps-metrics.json"
    ))
    .unwrap();
    for (sample, text, language) in [(0, "Aaß fi", "und"), (1, "iıIİ", "tr")] {
        let output = caps_render(
            text,
            "font-family:CapsFixture;font-variant:small-caps;font-synthesis:none",
            language,
        );
        let trace = output.layout.unwrap();
        let line = &trace.pages[0][0];
        let expected = &measured["samples"][sample];
        let glyphs = expected["glyphs"].as_array().unwrap();
        let actual: Vec<_> = line
            .runs
            .iter()
            .flat_map(|r| r.glyphs().iter().map(|g| g.id))
            .collect();
        assert_eq!(
            actual,
            glyphs
                .iter()
                .map(|g| g["g"].as_u64().unwrap() as u32)
                .collect::<Vec<_>>(),
            "native smcp for {text:?}"
        );
        common::assert_pt(
            line.width,
            glyphs
                .iter()
                .map(|g| g["ax"].as_f64().unwrap() as f32)
                .sum::<f32>()
                / expected["units_per_em"].as_f64().unwrap() as f32
                * 20.0,
        );
        assert_eq!(line.text, text);
        assert!(line.runs.iter().all(|r| r.font_size == 20.0));
    }
    let trace = caps_render("Aaß", "font-variant-caps:small-caps", "und")
        .layout
        .unwrap();
    let runs = &trace.pages[0][0].runs;
    assert_eq!(
        runs.iter().map(|r| r.text.as_str()).collect::<String>(),
        "Aaß"
    );
    assert_eq!(
        runs.iter().map(|r| r.font_size).collect::<Vec<_>>(),
        vec![20.0, 16.0]
    );
    let lower = &runs[1];
    assert_eq!(
        lower.glyphs().iter().map(|g| g.id).collect::<Vec<_>>(),
        vec![36, 54, 54]
    );
    assert_eq!(
        lower
            .glyphs()
            .iter()
            .map(|g| g.text_range.clone())
            .collect::<Vec<_>>(),
        vec![0..1, 1..3, 1..3]
    );
    common::assert_pt(lower.width, (1479.0 + 1139.0 * 2.0) / 2048.0 * 16.0);
    let trace = caps_render("iı", "font-variant-caps:small-caps", "tr")
        .layout
        .unwrap();
    let expected = &measured["samples"][3];
    common::assert_pt(
        trace.pages[0][0].width,
        expected["glyphs"]
            .as_array()
            .unwrap()
            .iter()
            .map(|g| g["ax"].as_f64().unwrap() as f32)
            .sum::<f32>()
            / expected["units_per_em"].as_f64().unwrap() as f32
            * 16.0,
    );
    assert_eq!(
        trace.pages[0][0]
            .runs
            .iter()
            .flat_map(|r| r.glyphs().iter().map(|g| g.id))
            .collect::<Vec<_>>(),
        expected["glyphs"]
            .as_array()
            .unwrap()
            .iter()
            .map(|g| g["g"].as_u64().unwrap() as u32)
            .collect::<Vec<_>>()
    );
}

#[test]
fn small_caps_preserves_text_clusters_fallback_and_line_breaks() {
    let normal = caps_render("Aaß 123", "", "und").layout.unwrap();
    let disabled = caps_render(
        "Aaß 123",
        "font-variant:small-caps;font-synthesis-small-caps:none",
        "und",
    )
    .layout
    .unwrap();
    common::assert_pt(disabled.pages[0][0].width, normal.pages[0][0].width);
    assert!(
        disabled.pages[0][0]
            .runs
            .iter()
            .all(|r| r.font_size == 20.0)
    );
    let fallback = caps_render(
        "aßЖ",
        "font-family:Limited,serif;font-variant:small-caps",
        "und",
    )
    .layout
    .unwrap();
    let runs = &fallback.pages[0][0].runs;
    assert_eq!(
        runs.iter().map(|r| r.text.as_str()).collect::<String>(),
        "aßЖ"
    );
    assert_eq!(fallback.fonts[runs[0].font_id.index()].family, "Limited");
    assert!(
        runs.iter()
            .any(|r| fallback.fonts[r.font_id.index()].family == "Liberation Serif")
    );
    let text = "Aaß test iı words many times wrapped across narrow lines ".repeat(4);
    let trace = caps_render(
        &text,
        "font:small-caps 20pt/28pt serif;width:85pt;letter-spacing:1pt;text-decoration:underline",
        "tr",
    )
    .layout
    .unwrap();
    let lines: Vec<_> = trace.pages.iter().flatten().collect();
    assert!(
        trace.pages.len() > 1,
        "the caps case must exercise page fragmentation"
    );
    assert!(lines.len() > 3);
    assert_eq!(
        lines
            .iter()
            .flat_map(|l| l.text.split_whitespace())
            .collect::<Vec<_>>(),
        text.split_whitespace().collect::<Vec<_>>()
    );
    assert!(lines.iter().all(|l| l.width <= 85.001));
    assert!(
        lines
            .iter()
            .flat_map(|l| &l.runs)
            .any(|r| r.font_size == 16.0)
    );
}

#[test]
fn small_caps_keep_nominal_line_and_link_metrics() {
    let normal = caps_render(
        "<a href='#end'>aաբ</a><span id=end></span>",
        "line-height:normal",
        "und",
    )
    .layout
    .unwrap();
    let caps = caps_render(
        "<a href='#end'>aաբ</a><span id=end></span>",
        "line-height:normal;font-variant:small-caps",
        "und",
    )
    .layout
    .unwrap();
    common::assert_pt(caps.pages[0][0].height, normal.pages[0][0].height);
    common::assert_pt(caps.pages[0][0].baseline, normal.pages[0][0].baseline);
    assert_eq!(caps.links.len(), normal.links.len());
    let (crate::LinkShape::Rect(normal), crate::LinkShape::Rect(caps)) =
        (&normal.links[0][0].shape, &caps.links[0][0].shape)
    else {
        panic!("untransformed text links must have rectangular activation areas");
    };
    common::assert_pt(caps.y, normal.y);
    common::assert_pt(caps.height, normal.height);
}

#[test]
fn small_caps_cascade_resets_and_synthesis_permissions_reach_shaping() {
    for (control, enabled) in [
        ("", true),
        ("font-synthesis:none", false),
        ("font-synthesis:weight style", false),
        ("font-synthesis:small-caps", true),
        ("font-synthesis-small-caps:none", false),
    ] {
        let trace = caps_render(
            "a 123",
            &format!("{control};font:small-caps 20pt/28pt serif"),
            "und",
        )
        .layout
        .unwrap();
        let runs = &trace.pages[0][0].runs;
        assert_eq!(
            runs[0].font_size,
            if enabled { 16.0 } else { 20.0 },
            "{control}"
        );
        assert_eq!(
            runs.last().unwrap().font_size,
            20.0,
            "caseless spaces and digits must retain full size: {control}"
        );
    }
    let inherited = computed("#parent{font-variant-caps:small-caps;font-synthesis:none}#target{font-variant-caps:inherit}").unwrap();
    assert_eq!(
        inherited.font_variant_caps,
        crate::style::FontVariantCaps::SmallCaps
    );
    assert_eq!(inherited.font_synthesis, Default::default());
    let reset =
        computed("#parent{font-variant:small-caps;font-synthesis:none}#target{font:20pt serif}")
            .unwrap();
    assert_eq!(
        reset.font_variant_caps,
        crate::style::FontVariantCaps::Normal
    );
    assert_eq!(reset.font_synthesis, Default::default());
    for (variant, caps, historical, styleset) in [
        (
            "all-small-caps",
            crate::style::FontVariantCaps::AllSmallCaps,
            false,
            &[][..],
        ),
        (
            "small-caps historical-forms",
            crate::style::FontVariantCaps::SmallCaps,
            true,
            &[],
        ),
        (
            "small-caps styleset(foo)",
            crate::style::FontVariantCaps::SmallCaps,
            false,
            &["foo"],
        ),
    ] {
        let style = computed(&format!("#target{{font-variant:{variant}}}"))
            .unwrap_or_else(|error| panic!("font-variant:{variant}: {error}"));
        assert_eq!(style.font_variant_caps, caps, "{variant}");
        let alternates = &style.font_features.alternates;
        assert_eq!(alternates.historical_forms, historical, "{variant}");
        assert_eq!(
            alternates
                .styleset
                .iter()
                .map(AsRef::as_ref)
                .collect::<Vec<&str>>(),
            styleset,
            "{variant}"
        );
    }
    for (text, language, upper) in [("i\u{307}", "lt", "I"), ("άι", "el", "ΑΪ")] {
        let caps = caps_render(text, "font-variant-caps:small-caps", language)
            .layout
            .unwrap();
        let expected = caps_render(upper, "font-size:16pt", language)
            .layout
            .unwrap();
        let actual = &caps.pages[0][0];
        assert_eq!(actual.text, text);
        common::assert_pt(actual.width, expected.pages[0][0].width);
        assert_eq!(
            actual
                .runs
                .iter()
                .flat_map(|r| r.glyphs().iter().map(|g| g.id))
                .collect::<Vec<_>>(),
            expected.pages[0][0]
                .runs
                .iter()
                .flat_map(|r| r.glyphs().iter().map(|g| g.id))
                .collect::<Vec<_>>(),
            "contextual {language} capitals"
        );
        for run in &actual.runs {
            for glyph in run.glyphs() {
                assert!(
                    run.text.is_char_boundary(glyph.text_range.start)
                        && run.text.is_char_boundary(glyph.text_range.end)
                );
            }
        }
    }
}

fn character_policy_trace(families: &str, text: &str, declarations: &str) -> crate::LayoutTrace {
    crate::render_html(
        &format!("<!doctype html><style>html,body,p{{margin:0;padding:0;font:20pt/24pt {families}}}p{{{declarations}}}</style><p>{text}</p>"),
        &common::local_options(),
    ).unwrap_or_else(|error|panic!("character policy {families}/{text:?}: {error}")).layout.unwrap()
}

#[test]
fn exhausted_glyph_fallback_renders_visible_symbols_and_retains_source() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/character-policy-reference.json"
    ))
    .unwrap();
    let id = reference["characters"]["replacement"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    let advance = reference["characters"]["replacement"]["advance"]
        .as_f64()
        .unwrap()
        / reference["units_per_em"].as_f64().unwrap()
        * 20.0;
    let text = "\u{0378}\u{10ffff}";
    let trace = character_policy_trace("serif", text, "");
    let line = &trace.pages[0][0];
    assert_eq!(line.text, text);
    let glyphs = line
        .runs
        .iter()
        .flat_map(crate::GlyphRun::glyphs)
        .collect::<Vec<_>>();
    assert_eq!(
        glyphs.len(),
        2,
        "each missing scalar needs a visible indication"
    );
    for glyph in glyphs {
        assert_eq!(glyph.id, id);
        common::assert_pt(glyph.advance(20.0), advance as f32);
    }
    assert!(
        line.runs
            .iter()
            .all(|run| trace.fonts[run.font_id.index()].family == "DejaVu Sans")
    );
}

#[test]
fn private_use_characters_require_an_explicit_non_generic_family() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/character-policy-reference.json"
    ))
    .unwrap();
    let replacement = reference["characters"]["replacement"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    let covered = reference["characters"]["private_use"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    for (families, id) in [
        ("sans-serif", replacement),
        ("'Liberation Serif', sans-serif", replacement),
        ("'DejaVu Sans'", covered),
        ("serif, 'DejaVu Sans'", covered),
    ] {
        let trace = character_policy_trace(families, "\u{ef00}", "");
        let line = &trace.pages[0][0];
        assert_eq!(line.text, "\u{ef00}");
        let ids = line
            .runs
            .iter()
            .flat_map(|run| run.glyphs().iter().map(|glyph| glyph.id))
            .collect::<Vec<_>>();
        assert_eq!(
            ids,
            [id],
            "{families}: generic coverage cannot define a private-use meaning"
        );
    }
}

#[test]
fn private_use_named_matching_keeps_its_font_namespace() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/character-policy-reference.json"
    ))
    .unwrap();
    let replacement = reference["characters"]["replacement"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    let font = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("assets/fonts/LiberationSerif-Regular.ttf");
    let trace=crate::render_html(
        &format!("<!doctype html><style>html,body,p{{margin:0;padding:0;font:20pt/24pt serif}}</style><div><template shadowrootmode=open><style>@font-face{{font-family:'DejaVu Sans';src:url('{}')}}p{{font-family:'DejaVu Sans',sans-serif}}</style><p>\u{ef00}</p></template></div>",font.display()),
        &common::local_options(),
    ).unwrap().layout.unwrap();
    let ids = trace.pages[0][0]
        .runs
        .iter()
        .flat_map(|run| run.glyphs().iter().map(|glyph| glyph.id))
        .collect::<Vec<_>>();
    assert_eq!(
        ids,
        [replacement],
        "the same family name in implicit root fallback is not an explicitly selected shadow face"
    );
}

#[test]
fn missing_symbols_keep_source_clusters_and_default_ignorables() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/character-policy-reference.json"
    ))
    .unwrap();
    let replacement = reference["characters"]["replacement"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    let text = "a\u{0378}\u{200d}\u{10ffff}";
    for declarations in ["font-variant-caps:small-caps", "font-size:0pt"] {
        let trace = character_policy_trace("serif", text, declarations);
        let line = &trace.pages[0][0];
        assert_eq!(line.text, text);
        if declarations.contains("0pt") {
            common::assert_pt(line.width, 0.0);
        } else {
            let symbols = line
                .runs
                .iter()
                .filter(|run| trace.fonts[run.font_id.index()].family == "DejaVu Sans")
                .flat_map(crate::GlyphRun::glyphs)
                .filter(|glyph| glyph.id == replacement)
                .count();
            assert_eq!(
                symbols, 2,
                "the joiner must remain invisible while missing characters get symbols"
            );
        }
        for run in &line.runs {
            assert!(
                text.is_char_boundary(run.source_range.start)
                    && text.is_char_boundary(run.source_range.end)
            );
            for glyph in run.glyphs() {
                assert!(
                    run.text.is_char_boundary(glyph.text_range.start)
                        && run.text.is_char_boundary(glyph.text_range.end)
                );
            }
        }
    }
}

#[test]
fn incomplete_combining_clusters_keep_the_longest_supported_prefix() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/character-policy-reference.json"
    ))
    .unwrap();
    let replacement = reference["characters"]["replacement"]["glyph_id"]
        .as_u64()
        .unwrap() as u32;
    for (families, base, key, family) in [
        ("'DejaVu Sans'", '\u{ef00}', "private_use", "DejaVu Sans"),
        ("serif", 'Ω', "greek_prefix", "Liberation Serif"),
    ] {
        let text = format!("{base}\u{1ab0}");
        let trace = character_policy_trace(families, &text, "");
        let line = &trace.pages[0][0];
        assert_eq!(line.text, text);
        let ids = line
            .runs
            .iter()
            .flat_map(|run| run.glyphs().iter().map(|glyph| glyph.id))
            .collect::<Vec<_>>();
        assert_eq!(
            ids,
            [
                reference["characters"][key]["glyph_id"].as_u64().unwrap() as u32,
                replacement
            ],
            "{families}: supported prefix must survive missing mark fallback"
        );
        assert_eq!(trace.fonts[line.runs[0].font_id.index()].family, family);
    }
}

#[test]
fn variation_sequences_search_later_faces_before_discarding_the_selector() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/VariationSequenceTest-source.json"
    ))
    .unwrap();
    let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/fonts/VariationSequenceTest.ttf");
    for sample in reference["samples"].as_array().unwrap() {
        let text = sample["text"].as_str().unwrap();
        let html = format!(
            "<!doctype html><style>@font-face{{font-family:'Variation Fixture';src:url('{}')}}html,body,p{{margin:0;padding:0;font:20pt/24pt 'Liberation Serif','Variation Fixture'}}</style><p>{text}</p>",
            fixture.display()
        );
        let trace = crate::render_html(&html, &common::local_options())
            .unwrap()
            .layout
            .unwrap();
        let line = &trace.pages[0][0];
        assert_eq!(line.text, text);
        let runs = line
            .runs
            .iter()
            .filter(|run| !run.glyphs().is_empty())
            .collect::<Vec<_>>();
        assert_eq!(
            runs.len(),
            1,
            "{text:?}: selectors must stay with their base glyph"
        );
        let run = runs[0];
        let expected_family = if text == "A\u{fe01}" {
            "Liberation Serif"
        } else {
            "Variation Fixture"
        };
        assert_eq!(
            trace.fonts[run.font_id.index()].family,
            expected_family,
            "{text:?}: full sequence coverage must precede base-only fallback"
        );
        assert_eq!(
            run.glyphs().len(),
            1,
            "{text:?}: selectors have no separate visible glyph"
        );
        assert_eq!(
            run.glyphs()[0].id,
            sample["glyph_id"].as_u64().unwrap() as u32,
            "{text:?}: selected glyph must match independent HarfBuzz shaping"
        );
        common::assert_pt(
            run.width,
            (sample["advance"].as_f64().unwrap() / reference["units_per_em"].as_f64().unwrap()
                * 20.0) as f32,
        );
        assert_eq!(
            &run.text[run.glyphs()[0].text_range.clone()],
            text,
            "{text:?}: invisible selectors still identify the serialized variant"
        );
    }
}

#[test]
fn ignored_variation_selectors_do_not_remove_word_spacing() {
    for declarations in [
        "word-spacing:7pt",
        "word-spacing:7pt;width:100pt;text-align:justify;text-align-last:justify",
    ] {
        let plain = character_policy_trace("serif", "a b", declarations);
        let marked = character_policy_trace("serif", "a \u{fe01}b", declarations);
        common::assert_pt(marked.pages[0][0].width, plain.pages[0][0].width);
        assert_eq!(marked.pages[0][0].text, "a \u{fe01}b");
    }
}

#[test]
fn font_names_use_full_default_unicode_folding_without_normalization() {
    let reference: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/font-name-matching.json"
    ))
    .unwrap();
    let fixture = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("assets/fonts/LiberationMono-Regular.ttf");
    for case in reference["cases"].as_array().unwrap() {
        let declared = case["declared"].as_str().unwrap();
        let requested = case["requested"].as_str().unwrap();
        let html = format!(
            "<!doctype html><html lang=tr><style>@font-face{{font-family:'{declared}';src:url('{}')}}p{{font:20pt/24pt '{requested}',serif}}</style><p>H</p></html>",
            fixture.display()
        );
        let trace = crate::render_html(&html, &common::local_options())
            .unwrap()
            .layout
            .unwrap();
        let run = &trace.pages[0][0].runs[0];
        let expected = if case["matches"].as_bool().unwrap() {
            declared
        } else {
            "Liberation Serif"
        };
        assert_eq!(
            trace.fonts[run.font_id.index()].family,
            expected,
            "{declared:?} / {requested:?}: default C/F folding must ignore the document language and retain canonical distinctions"
        );
    }
}

#[test]
fn shadow_font_names_fold_before_selecting_their_namespace() {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/fonts");
    let html = format!(
        "<!doctype html><style>@font-face{{font-family:STRASSE;src:url('{}')}}p{{font:20pt/24pt STRASSE}}</style><div><template shadowrootmode=open><style>@font-face{{font-family:'Straße';src:url('{}')}}p{{font:20pt/24pt STRASSE}}</style><p>H</p></template></div><p>H</p>",
        root.join("LiberationSerif-Regular.ttf").display(),
        root.join("LiberationMono-Regular.ttf").display()
    );
    let trace = crate::render_html(&html, &common::local_options())
        .unwrap()
        .layout
        .unwrap();
    let families = trace.pages[0]
        .iter()
        .map(|line| trace.fonts[line.runs[0].font_id.index()].family.as_str())
        .collect::<Vec<_>>();
    assert_eq!(
        families,
        ["Straße", "STRASSE"],
        "the folded shadow alias must hide the root face only inside its font namespace"
    );
    assert_ne!(trace.pages[0][0].width, trace.pages[0][1].width);
}

#[test]
fn local_font_full_and_postscript_names_use_the_same_unicode_fold() {
    for local in ["DejaVu ſans", "DejaVuſans"] {
        let html = format!(
            "<!doctype html><style>@font-face{{font-family:LocalAlias;src:local('{local}')}}p{{font:20pt/24pt LocalAlias}}</style><p>H</p>"
        );
        let trace = crate::render_html(&html, &common::local_options())
            .unwrap()
            .layout
            .unwrap();
        assert_eq!(
            trace.fonts[trace.pages[0][0].runs[0].font_id.index()].family,
            "LocalAlias",
            "{local:?}: a Unicode-folded local name must resolve bundled data"
        );
    }
}

#[test]
fn font_size_adjust_scales_metric_units_but_not_em_or_numeric_line_heights() {
    let metrics: serde_json::Value = serde_json::from_str(include_str!(
        "../../tests/fixtures/fonts/FontUnit-metrics.json"
    ))
    .unwrap();
    let serif = &metrics["faces"]["InitialSerif"];
    let em = |name: &str| {
        serif[name].as_f64().unwrap() as f32 / serif["units_per_em"].as_f64().unwrap() as f32
    };
    let size = 20.0;
    let used = size * 0.5 / em("x_height");
    let style = computed(
        "#target{font-size:20pt;font-size-adjust:0.5;line-height:normal;margin:1ex 1em 1ch 1cap;width:1lh}",
    )
    .unwrap();
    let length = |value: &crate::style::Size| value.resolve_definite(None).unwrap();
    common::assert_labelled_pt(
        "1ex keeps the specified aspect",
        length(&style.margin[0]),
        size * 0.5,
    );
    common::assert_labelled_pt("1em is the computed size", length(&style.margin[1]), size);
    common::assert_labelled_pt("1ch", length(&style.margin[2]), em("zero_advance") * used);
    common::assert_labelled_pt("1cap", length(&style.margin[3]), em("cap_height") * used);
    common::assert_labelled_pt(
        "normal line height uses the used size",
        length(&style.width),
        (em("ascender") - em("descender") + em("line_gap")) * used,
    );
    let numeric =
        computed("#target{font-size:20pt;font-size-adjust:0.5;line-height:1.5;width:1lh}").unwrap();
    common::assert_labelled_pt("numeric lh", length(&numeric.width), size * 1.5);
    let from_font = computed("#parent{font-size-adjust:ch-width from-font}").unwrap();
    assert_eq!(
        from_font.font_adjustments.size_adjust,
        Some(crate::fonts::SizeAdjust {
            metric: crate::fonts::SizeMetric::ChWidth,
            value: em("zero_advance"),
        }),
        "from-font computes to the first available font's metric, which the child inherits"
    );
}