hephaestus 0.1.0

Backend-agnostic 2D scene renderer for data visualization.
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
//! Tests for the rich-text shaping, wrapping and draw passes.

use parley::Alignment;

use super::draw::*;
use super::length::{pt, RichMargin};
use super::run::*;
use super::shape::*;
use super::style::{RichTextStyleSheet, StyleDelta};
use crate::brush::Brush;
use crate::color::Color;
use crate::geometry::Affine;
use crate::layout::Measure;
use crate::pick::PickId;
use crate::scene::recording::{Op, RecordingScene};
use crate::style_vocab::{HAlign, Palette};
use crate::text::rich::anchor::RichAnchor;
use crate::text::TextStyle;

fn palette() -> Palette {
    Palette::new(
        Color::from_rgba8(255, 255, 255, 255),
        Color::from_rgba8(0, 0, 0, 255),
        Color::from_rgba8(51, 105, 232, 255),
    )
}

fn base_style() -> TextStyle {
    TextStyle::new(14.0)
}

fn make(src: &str) -> RichTextRun {
    let sheet = RichTextStyleSheet::new();
    RichTextRun::new(
        src,
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    )
}

fn draw(run: &RichTextRun) -> RecordingScene {
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    scene
}

fn glyph_x_at_line(scene: &RecordingScene, line_ordinal: usize) -> Option<f32> {
    // Return the leftmost x of the `line_ordinal`-th DrawGlyphs op
    // (approximation: parley emits one op per line for simple
    // text; heavier styling may split further).
    let mut ops = scene.ops.iter().filter_map(|op| match op {
        Op::DrawGlyphs(gr) => Some(gr),
        _ => None,
    });
    let gr = ops.nth(line_ordinal)?;
    gr.glyphs.iter().map(|g| g.x).fold(None::<f32>, |acc, x| {
        Some(match acc {
            None => x,
            Some(a) => a.min(x),
        })
    })
}

#[test]
fn inline_code_span_emits_background_chip() {
    // The default sheet's `code` selector sets background +
    // padding + border-radius. An `` `x` `` inline span should
    // therefore emit at least one Fill op (the chip rect)
    // BEFORE the glyph run for its content.
    let run = make("plain `x` more");
    let scene = draw(&run);
    let mut saw_fill = false;
    let mut saw_glyphs_after_fill = false;
    for op in &scene.ops {
        match op {
            Op::Fill { .. } => saw_fill = true,
            Op::DrawGlyphs(_) if saw_fill => saw_glyphs_after_fill = true,
            _ => {}
        }
    }
    assert!(saw_fill, "expected a Fill op for the code chip");
    assert!(saw_glyphs_after_fill, "glyphs should follow the chip fill");
}

#[test]
fn per_span_text_stroke_emits_outline_pass() {
    // A custom class with `text_stroke` produces a stroke-only
    // glyph pass behind the fill. To reliably split the parley
    // run at the span's boundary we also set a distinct
    // `color`; parley splits on brush change, isolating the
    // outline to the span.
    let mut sheet = RichTextStyleSheet::new();
    sheet.set(
        "haloed",
        crate::text::rich::style::StyleDelta {
            color: Some(crate::style_vocab::ThemeColor::Fixed(Color::from_rgba8(
                220, 30, 30, 255,
            ))),
            text_stroke: Some(crate::style_vocab::ThemeColor::Fixed(Color::from_rgba8(
                255, 255, 255, 255,
            ))),
            text_stroke_width: Some(pt(2.0)),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "before {.haloed word} after",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let has_stroked_pass = scene
        .ops
        .iter()
        .any(|op| matches!(op, Op::DrawGlyphs(gr) if gr.style.is_some()));
    assert!(
        has_stroked_pass,
        "expected a stroke-only glyph pass for the `haloed` span"
    );
}

#[test]
fn block_level_border_does_not_double_render_as_inline() {
    // A block-level `border` set via the sheet's `paragraph`
    // selector should paint ONCE (in the block paint pass), not
    // again as an inline border on the block's text runs.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin::all(pt(1.0))),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "some text",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .count();
    assert_eq!(
        strokes, 1,
        "expected exactly one border stroke (block-level)"
    );
}

#[test]
fn list_container_margin_survives_a_re_break() {
    // Regression: `set_max_width` re-stacks the blocks, and the
    // ancestor container margins routed to first / last
    // descendant at shape time must still be applied there.
    let mut sheet = RichTextStyleSheet::new();
    sheet.set(
        "list",
        StyleDelta {
            margin: Some(RichMargin::new(pt(40.0), pt(0.0), pt(40.0), pt(0.0))),
            ..StyleDelta::empty()
        },
    );
    let make_with = |src: &str, sheet: &RichTextStyleSheet| {
        RichTextRun::new(
            src,
            &base_style(),
            Color::from_rgba8(0, 0, 0, 255),
            sheet,
            &palette(),
            96.0,
        )
    };
    let roomy = make_with("- alpha\n\nfollowing", &sheet);
    let tight = make_with("- alpha\n\nfollowing", &RichTextStyleSheet::new());
    let natural_delta = roomy.natural_height() - tight.natural_height();
    assert!(
        natural_delta > 20.0,
        "the list's own margin should widen the natural stack (delta={natural_delta})"
    );
    let width = roomy.natural_width() as f32;
    let roomy_broken = roomy.set_max_width(width, HAlign::Start) as f64;
    let tight_broken = tight.set_max_width(width, HAlign::Start) as f64;
    assert!(
        (roomy_broken - tight_broken - natural_delta).abs() < 1.0,
        "re-break lost the container margin ({roomy_broken} - {tight_broken} vs {natural_delta})"
    );
}

#[test]
fn strikethrough_sits_above_baseline() {
    let sheet = RichTextStyleSheet::new();
    let run = RichTextRun::new(
        "a ~~strike~~ b",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let baseline_y = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::DrawGlyphs(gr) => gr.glyphs.first().map(|g| g.y),
            _ => None,
        })
        .expect("baseline");
    let fill_y0 = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::Fill { path, .. } => Some(crate::geometry::Shape::bounding_box(path).y0 as f32),
            _ => None,
        })
        .expect("strikethrough fill");
    assert!(
        fill_y0 < baseline_y,
        "strikethrough rect (y0={fill_y0}) should sit ABOVE the baseline (y={baseline_y})"
    );
}

#[test]
fn underline_sits_below_baseline() {
    let sheet = RichTextStyleSheet::new();
    let run = RichTextRun::new(
        "a _under_ b",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let baseline_y = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::DrawGlyphs(gr) => gr.glyphs.first().map(|g| g.y),
            _ => None,
        })
        .expect("baseline");
    let fill_y0 = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::Fill { path, .. } => Some(crate::geometry::Shape::bounding_box(path).y0 as f32),
            _ => None,
        })
        .expect("underline fill");
    assert!(
        fill_y0 > baseline_y,
        "underline rect (y0={fill_y0}) should sit BELOW the baseline (y={baseline_y})"
    );
}

#[test]
fn plain_text_shapes_and_measures() {
    let run = make("hello world");
    assert!(run.natural_width() > 0.0);
    assert!(run.natural_height() > 0.0);
}

#[test]
fn bold_widens_natural_width_vs_plain() {
    let plain = make("hello world");
    let bold = make("**hello world**");
    assert!(bold.natural_width() >= plain.natural_width());
}

#[test]
fn draw_emits_glyph_runs_with_per_range_brushes() {
    let run = make("a {.red word} b");
    let scene = draw(&run);
    let glyph_runs: Vec<_> = scene
        .ops
        .iter()
        .filter_map(|op| match op {
            Op::DrawGlyphs(gr) => Some(gr),
            _ => None,
        })
        .collect();
    assert!(glyph_runs.len() >= 2, "expected multiple glyph runs");
    let has_red = glyph_runs.iter().any(|gr| match &gr.brush {
        Brush::Solid(c) => {
            let [r, g, b, _] = c.components;
            (r - 1.0).abs() < 1e-3 && g < 0.1 && b < 0.1
        }
        _ => false,
    });
    assert!(has_red);
}

#[test]
fn sup_offsets_glyphs_upward() {
    let run = make("a ^2^ b");
    let scene = draw(&run);
    let mut ys: Vec<f32> = Vec::new();
    for op in &scene.ops {
        if let Op::DrawGlyphs(gr) = op {
            for g in &gr.glyphs {
                ys.push(g.y);
            }
        }
    }
    assert!(!ys.is_empty());
    let min_y = ys.iter().cloned().fold(f32::INFINITY, f32::min);
    let max_y = ys.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
    assert!(max_y - min_y > 1.0);
}

#[test]
fn measure_impl_reports_positive_height() {
    let run = make("hello **bold** world");
    let h = run.height_at(200.0, 96.0);
    assert!(h > 0.0);
}

#[test]
fn base_brush_flows_through_to_plain_runs() {
    let sheet = RichTextStyleSheet::new();
    let base_col = Color::from_rgba8(50, 100, 200, 255);
    let run = RichTextRun::new(
        "plain text",
        &base_style(),
        base_col,
        &sheet,
        &palette(),
        96.0,
    );
    let scene = draw(&run);
    let first = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::DrawGlyphs(gr) => Some(gr),
            _ => None,
        })
        .expect("glyph run");
    match &first.brush {
        Brush::Solid(c) => {
            let [r, g, b, _] = c.components;
            assert!((r - 50.0 / 255.0).abs() < 1e-2);
            assert!((g - 100.0 / 255.0).abs() < 1e-2);
            assert!((b - 200.0 / 255.0).abs() < 1e-2);
        }
        _ => panic!("expected solid brush"),
    }
}

#[test]
fn heading_produces_larger_glyph_run_font_size() {
    let plain = make("Big");
    let heading = make("# Big");
    let max_size = |run: &RichTextRun| {
        let scene = draw(run);
        scene
            .ops
            .iter()
            .filter_map(|op| match op {
                Op::DrawGlyphs(gr) => Some(gr.font_size),
                _ => None,
            })
            .fold(0.0_f32, f32::max)
    };
    assert!(max_size(&heading) > max_size(&plain) * 1.5);
}

#[test]
fn size_selector_produces_larger_run_height() {
    let plain = make("x");
    let big = make("{.36 x}");
    assert!(big.natural_height() > plain.natural_height());
}

#[test]
fn new_with_width_wraps_at_fixed_pixels() {
    let sheet = RichTextStyleSheet::new();
    let unwrapped = RichTextRun::new(
        "one two three four five six seven eight",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let wrapped = RichTextRun::new_with_width(
        "one two three four five six seven eight",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
        RichTextWidth::Fixed(60.0),
    );
    assert!(wrapped.current_height() > unwrapped.current_height());
}

#[test]
fn blockquote_indents_its_paragraph() {
    // A blockquote's default padding.left = Rel(1.0) so its
    // paragraph child should sit ~1em (14pt) to the right of a
    // plain paragraph. In screen px: 14pt at 96dpi ≈ 18.67px.
    let plain = make("hello world");
    let quoted = make("> hello world");
    // The paragraph inside the blockquote should be at ~1em from
    // the left; the plain paragraph's leftmost glyph sits at ~0.
    let plain_x = glyph_x_at_line(&draw(&plain), 0).unwrap_or(0.0);
    let quoted_x = glyph_x_at_line(&draw(&quoted), 0).unwrap_or(0.0);
    assert!(
        quoted_x > plain_x + 10.0,
        "blockquote content should be indented (plain={plain_x}, quoted={quoted_x})"
    );
}

#[test]
fn single_paragraph_box_is_tight_around_its_line() {
    // `paragraph` carries `margin.bottom = rem(1)`, but that margin
    // reaches the document's bottom edge and collapses out of the box.
    // A one-line label therefore measures one line tall — the same box
    // a plain `TextRun` of the same string reports, so both anchor the
    // same way.
    let run = make("**bold** and *italic*");
    let line = run.layout_bounds().height as f64;
    let total = run.natural_height();
    assert!(
        (total - line).abs() < 0.01,
        "one-paragraph run should measure one line (line={line}, total={total})"
    );
}

#[test]
fn leading_block_margin_collapses_out_of_the_box() {
    // Mirror of the trailing case: `h2` carries `margin.top = em(1)`,
    // which reaches the document's top edge. The heading's first line
    // therefore starts at the top of the box rather than one em down.
    let run = make("## Heading");
    let ink_top = run.layout_bounds().ink_top;
    assert!(
        ink_top < 1.0,
        "leading margin should collapse out of the box (ink_top={ink_top})"
    );
}

#[test]
fn interior_block_margins_survive() {
    // Only the document's own edges drop margins. A heading between
    // two paragraphs keeps both of its margins, so the stack is taller
    // than the same blocks with the heading's margins zeroed.
    let mut flat = RichTextStyleSheet::new();
    flat.set(
        "h2",
        StyleDelta {
            margin: Some(RichMargin::all(pt(0.0))),
            ..flat.get("h2").cloned().unwrap_or_default()
        },
    );
    let spaced = make("intro\n\n## Heading\n\nbody");
    let flattened = RichTextRun::new(
        "intro\n\n## Heading\n\nbody",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &flat,
        &palette(),
        96.0,
    );
    let delta = spaced.natural_height() - flattened.natural_height();
    assert!(
        delta > 10.0,
        "interior heading margins should still add space (delta={delta})"
    );
}

#[test]
fn sibling_margins_collapse_via_max_not_sum() {
    // Every paragraph carries `margin.bottom = rem(1)` and no top
    // margin, so an adjacent pair collapses to one gap of 1rem,
    // not two. Two extra paragraphs therefore add
    // 2 × (line + 1rem), not 2 × (line + 2rem).
    let three = make("a\n\nb\n\nc");
    let five = make("a\n\nb\n\nc\n\nd\n\ne");
    let diff = five.natural_height() - three.natural_height();
    // 14pt base, line-height 1.6 → one line ≈ 29.9px; 1rem ≈
    // 18.7px. Two paragraphs ≈ 97px collapsed, ≈ 134px if the
    // margins summed.
    assert!(
        diff < 115.0,
        "2 extra paragraphs should collapse to ≈97px of extra height, got {diff}"
    );
}

#[test]
fn adjacent_heading_paragraph_margins_collapse() {
    // Heading has `margin.bottom = Rel(0.3)` and paragraph has
    // `margin.top = 0` → collapse via `max(0.3, 0)` = 0.3em.
    // A bare `# H\n\ntext` should be shorter than a hypothetical
    // sum which would be 0.3+0 = 0.3em anyway. Test that headings
    // followed by paragraphs stack sanely.
    let with_heading = make("# Header\n\nBody");
    let no_heading = make("Header\n\nBody");
    assert!(with_heading.natural_height() > no_heading.natural_height());
}

#[test]
fn horizontal_rule_emits_single_top_stroke() {
    // `---` produces a Rule block; the sheet's `hr` entry sets a
    // top-only 1pt border, so the paint pass emits exactly one
    // horizontal stroke.
    let run = make("above\n\n---\n\nbelow");
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes: Vec<_> = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .collect();
    assert_eq!(
        strokes.len(),
        1,
        "hr should emit exactly one stroke (top edge only), got {}",
        strokes.len()
    );
}

#[test]
fn horizontal_rule_stretches_to_full_natural_width() {
    // The hr's paint outer_rect should span the same width as the
    // surrounding text — natural_width from the surrounding
    // paragraphs.
    let run = make("hello world hello world hello world\n\n---\n\nfollowing");
    let paints = run.block_paints();
    // Find the hr paint (border only, no background, small height).
    let hr = paints
        .iter()
        .find(|p| p.border.is_some() && p.background.is_none())
        .expect("expected hr paint");
    let width = hr.outer_rect.width();
    assert!(
        width > 50.0,
        "hr should span at least a paragraph's width; got {width}"
    );
    assert!(
        width as f32 >= run.natural_width() as f32 - 5.0,
        "hr should span ≥ natural width ({} vs {})",
        width,
        run.natural_width()
    );
}

#[test]
fn horizontal_rule_reserves_vertical_space() {
    // hr has 0 height but non-zero top/bottom margins from the
    // sheet default. `above` and `below` should be separated by
    // more space than a plain paragraph break.
    let without_hr = make("above\n\nbelow");
    let with_hr = make("above\n\n---\n\nbelow");
    assert!(
        with_hr.natural_height() > without_hr.natural_height(),
        "hr's margins should add vertical space (without: {}, with: {})",
        without_hr.natural_height(),
        with_hr.natural_height()
    );
}

#[test]
fn blockquote_emits_single_left_edge_stroke() {
    // The default `block_quote` sheet entry sets `border_width`
    // = Margin { left: 3pt, others: 0 }. The paint pass must
    // emit exactly ONE stroke op (for the left edge) — not a
    // full four-sided box.
    let run = make("> hello world");
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes: Vec<_> = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .collect();
    assert_eq!(
        strokes.len(),
        1,
        "blockquote should emit exactly one stroke (left edge only), got {}",
        strokes.len()
    );
}

#[test]
fn halign_start_maps_to_right_under_rtl() {
    assert_eq!(hal_to_alignment(HAlign::Start, false), Alignment::Left);
    assert_eq!(hal_to_alignment(HAlign::Start, true), Alignment::Right);
    assert_eq!(hal_to_alignment(HAlign::End, false), Alignment::Right);
    assert_eq!(hal_to_alignment(HAlign::End, true), Alignment::Left);
    assert_eq!(hal_to_alignment(HAlign::Center, false), Alignment::Center);
    assert_eq!(hal_to_alignment(HAlign::Center, true), Alignment::Center);
    assert_eq!(hal_to_alignment(HAlign::Justify, true), Alignment::Justify);
}

#[test]
fn auto_direction_reads_parley_is_rtl_for_arabic() {
    // Auto (unset text_direction) should reflect parley's UBA
    // determination on the block's text. Arabic script drives
    // base_level to Rtl.
    let sheet = RichTextStyleSheet::empty();
    let run = RichTextRun::new(
        "مرحبا",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let blocks = run.blocks.borrow();
    assert!(
        blocks.first().map(|bl| bl.is_rtl).unwrap_or(false),
        "an Arabic-only paragraph should resolve to Rtl via parley::Layout::is_rtl"
    );
}

#[test]
fn explicit_ltr_overrides_arabic_content_direction() {
    // A block with explicit `Direction::Ltr` in its class stays
    // Ltr even when parley infers Rtl from Arabic content.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            text_direction: Some(crate::text::rich::style::Direction::Ltr),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "مرحبا",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let blocks = run.blocks.borrow();
    assert!(
        !blocks.first().map(|bl| bl.is_rtl).unwrap_or(true),
        "explicit Direction::Ltr must override parley's Rtl inference"
    );
}

#[test]
fn rtl_blockquote_paints_right_edge_bar() {
    // A block_quote class sets `border_width` as `[0, 0, 0, 3]` —
    // start-side bar. Under Rtl the l/r swap in `border_for`
    // routes it onto the physical right edge, so `widths_px[1]`
    // (right) becomes non-zero and `widths_px[3]` (left) stays 0.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "block_quote",
        crate::text::rich::style::StyleDelta {
            text_direction: Some(crate::text::rich::style::Direction::Rtl),
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin::new(pt(0.0), pt(0.0), pt(0.0), pt(3.0))),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "> quoted content",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let paints = run.block_paints();
    let border = paints
        .iter()
        .find_map(|p| p.border.as_ref())
        .expect("blockquote should have a border");
    assert!(
        border.widths_px[1] > 0.0,
        "under Rtl the start-side bar should paint on the physical right (widths_px[1])"
    );
    assert!(
        border.widths_px[3].abs() < 1e-3,
        "physical left (widths_px[3]) should be zero"
    );
}

#[test]
fn ltr_blockquote_still_paints_left_edge_bar() {
    // Baseline sanity: no direction set → default Ltr behavior
    // stays intact. `[0, 0, 0, 3]` still lands on the left edge.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "block_quote",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin::new(pt(0.0), pt(0.0), pt(0.0), pt(3.0))),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "> quoted content",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let paints = run.block_paints();
    let border = paints
        .iter()
        .find_map(|p| p.border.as_ref())
        .expect("blockquote should have a border");
    assert!(
        border.widths_px[3] > 0.0,
        "under Ltr the start-side bar should paint on the physical left (widths_px[3])"
    );
    assert!(
        border.widths_px[1].abs() < 1e-3,
        "physical right (widths_px[1]) should be zero"
    );
}

#[test]
fn uniform_border_emits_single_boxed_stroke() {
    // A uniform four-sided border should collapse to a single
    // rectangular stroke — no fan of segments.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin::all(pt(1.0))),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "boxed",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .count();
    assert_eq!(strokes, 1, "uniform border → one rectangular stroke");
}

#[test]
fn adjacent_partial_borders_collapse_into_one_polyline() {
    // Top + left borders both present with the same width should
    // stroke as ONE polyline sharing the top-left corner, not
    // two independent segments meeting there. Distinct widths on
    // T and L, in contrast, must stay separate.
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin {
                top: pt(2.0),
                right: pt(0.0),
                bottom: pt(0.0),
                left: pt(2.0),
            }),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "l shape",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .count();
    assert_eq!(
        strokes, 1,
        "top + left same-width partial borders should collapse into one polyline"
    );
}

#[test]
fn partial_borders_with_mismatched_widths_stay_separate() {
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin {
                top: pt(1.0),
                right: pt(0.0),
                bottom: pt(0.0),
                left: pt(4.0),
            }),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "mixed",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        0.0,
        RichAnchor::top_left(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let strokes = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .count();
    assert_eq!(strokes, 2, "different widths keep sides separate");
}

#[test]
fn dashed_border_carries_dash_pattern_through() {
    use crate::scales::value::LinetypeStep;
    use std::sync::Arc;
    let mut sheet = RichTextStyleSheet::empty();
    sheet.set(
        "paragraph",
        crate::text::rich::style::StyleDelta {
            border_color: Some(crate::style_vocab::ThemeColor::Ink),
            border_width: Some(RichMargin::all(pt(1.0))),
            border_type: Some(Arc::from(vec![
                LinetypeStep::Dash(4.0),
                LinetypeStep::Gap(2.0),
            ])),
            ..crate::text::rich::style::StyleDelta::empty()
        },
    );
    let run = RichTextRun::new(
        "dashy",
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
    );
    let paints = run.block_paints();
    let border = paints
        .iter()
        .find_map(|p| p.border.as_ref())
        .expect("expected a border on the paragraph");
    let pattern = border
        .linetype_pt
        .as_ref()
        .expect("border_type should produce a linetype pattern");
    // Two entries (dash + gap), both positive.
    assert_eq!(pattern.len(), 2);
    use crate::scales::value::LinetypeStep::{Dash, Gap};
    assert!(matches!(pattern[0], Dash(d) if d > 0.0));
    assert!(matches!(pattern[1], Gap(g) if g > 0.0));
}

#[test]
fn loose_list_stacks_taller_than_tight_list() {
    // Same items, one written tight and one written loose. The
    // loose version should be strictly taller because each
    // paragraph body carries margin.bottom.
    let tight = make("- alpha\n- beta\n- gamma");
    let loose = make("- alpha\n\n- beta\n\n- gamma");
    assert!(
        loose.natural_height() > tight.natural_height() + 5.0,
        "loose ({}) should stack taller than tight ({})",
        loose.natural_height(),
        tight.natural_height()
    );
}

#[test]
fn nested_list_item_sits_further_right_than_outer() {
    // A nested list should render its bullet indented under the
    // outer item's continuation position, i.e. outer's hanging
    // (~1.5em) to the right of the outer marker.
    let run = make("- outer\n  - inner");
    let scene = draw(&run);
    // Extract leftmost glyph x per line (approximate via y-bucket).
    let mut by_line: std::collections::BTreeMap<i32, f32> = std::collections::BTreeMap::new();
    for op in &scene.ops {
        if let Op::DrawGlyphs(gr) = op {
            if let Some(min_x) = gr.glyphs.iter().map(|g| g.x).fold(None::<f32>, |acc, x| {
                Some(match acc {
                    None => x,
                    Some(a) => a.min(x),
                })
            }) {
                let y_key = gr.glyphs[0].y as i32;
                by_line
                    .entry(y_key)
                    .and_modify(|v| *v = v.min(min_x))
                    .or_insert(min_x);
            }
        }
    }
    let xs: Vec<f32> = by_line.into_values().collect();
    assert!(xs.len() >= 2, "expected at least 2 lines, got {xs:?}");
    // First line = outer's bullet at x≈0; second = inner's bullet
    // at x≈1.5em (=1.5*14pt≈21pt→28px at 96dpi).
    assert!(
        xs[1] > xs[0] + 10.0,
        "nested item should be indented past outer (xs={xs:?})"
    );
}

#[test]
fn list_item_hanging_shifts_continuation_lines() {
    // Force the list item to wrap so a continuation line appears,
    // then verify its leftmost glyph is right of the first line's.
    let src = "- one two three four five six seven eight nine";
    let sheet = RichTextStyleSheet::new();
    let run = RichTextRun::new_with_width(
        src,
        &base_style(),
        Color::from_rgba8(0, 0, 0, 255),
        &sheet,
        &palette(),
        96.0,
        RichTextWidth::Fixed(100.0),
    );
    let scene = draw(&run);
    // Extract all glyph runs and their minimum x + y. Group by y
    // to distinguish lines.
    let mut by_line: std::collections::BTreeMap<i32, f32> = std::collections::BTreeMap::new();
    for op in &scene.ops {
        if let Op::DrawGlyphs(gr) = op {
            if let Some(min_x) = gr.glyphs.iter().map(|g| g.x).fold(None::<f32>, |acc, x| {
                Some(match acc {
                    None => x,
                    Some(a) => a.min(x),
                })
            }) {
                let y_key = gr.glyphs[0].y as i32;
                by_line
                    .entry(y_key)
                    .and_modify(|v| *v = v.min(min_x))
                    .or_insert(min_x);
            }
        }
    }
    let xs: Vec<f32> = by_line.into_values().collect();
    assert!(xs.len() >= 2, "expected at least 2 lines, got {xs:?}");
    assert!(
        xs[1] > xs[0] + 5.0,
        "continuation line should be right of first (xs={xs:?})"
    );
}

#[test]
fn code_block_emits_fill_before_glyphs() {
    let run = make("```\nlet x = 1;\n```");
    let scene = draw(&run);
    let first_fill = scene
        .ops
        .iter()
        .position(|op| matches!(op, Op::Fill { .. }));
    let first_glyphs = scene
        .ops
        .iter()
        .position(|op| matches!(op, Op::DrawGlyphs(_)));
    let (fi, gi) = (first_fill.expect("fill"), first_glyphs.expect("glyphs"));
    assert!(fi < gi, "Fill at {fi} should precede DrawGlyphs at {gi}");
}

#[test]
fn plain_text_emits_no_block_paints() {
    let run = make("just plain");
    let scene = draw(&run);
    let fills = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Fill { .. }))
        .count();
    let strokes = scene
        .ops
        .iter()
        .filter(|op| matches!(op, Op::Stroke { .. }))
        .count();
    assert_eq!(fills, 0);
    assert_eq!(strokes, 0);
}

#[test]
fn center_anchor_shifts_glyph_positions_by_half_width() {
    let run = make("abcdef");
    let width = run.natural_width() as f32;
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        100.0,
        100.0,
        RichAnchor::center(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let first_op = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::DrawGlyphs(gr) => Some(gr),
            _ => None,
        })
        .unwrap();
    let first_g = first_op.glyphs.first().unwrap();
    let transformed_x = first_op.transform.as_coeffs()[4] as f32 + first_g.x;
    assert!(
        (transformed_x - (100.0 - width * 0.5)).abs() < 2.0,
        "first glyph should sit near (x - width/2), got {transformed_x} (expected ~{})",
        100.0 - width * 0.5
    );
}

#[test]
fn first_line_anchor_places_baseline_on_y() {
    let run = make("hi");
    let mut scene = RecordingScene::default();
    draw_rich_text(
        &mut scene,
        &run,
        0.0,
        100.0,
        RichAnchor::first_line_baseline(),
        Affine::IDENTITY,
        PickId::Skip,
    );
    let first = scene
        .ops
        .iter()
        .find_map(|op| match op {
            Op::DrawGlyphs(gr) => Some(gr),
            _ => None,
        })
        .unwrap();
    let coeffs = first.transform.as_coeffs();
    let g = first.glyphs.first().unwrap();
    let screen_y = coeffs[5] as f32 + g.y;
    assert!(
        (screen_y - 100.0).abs() < 0.5,
        "first baseline should land at y = 100; got screen y = {screen_y}"
    );
}

#[test]
fn breaking_at_the_natural_width_reproduces_the_natural_height() {
    let run = make("a paragraph long enough to have an opinion about width\n\nand another");
    let natural = run.natural_height();
    let broken = run.set_max_width(run.natural_width() as f32, HAlign::Start) as f64;
    assert!(
        (broken - natural).abs() < 1.0,
        "re-breaking at the natural width changed the height ({natural} → {broken})"
    );
}

#[test]
fn natural_height_survives_a_narrow_re_break() {
    let run = make("a paragraph long enough to wrap when the column gets narrow");
    let natural = run.natural_height();
    run.set_max_width(natural as f32 / 4.0, HAlign::Start);
    assert!(
        run.current_height() > natural,
        "wrapping should make the run taller"
    );
    assert_eq!(
        run.natural_height(),
        natural,
        "natural height must not move when the run re-breaks"
    );
}

#[test]
fn base_style_font_features_reach_the_rich_shaper() {
    // `push_base_defaults` shares `TextRun`'s property pushes, so
    // a feature that changes advances must change the shaped
    // width here too.
    let sheet = RichTextStyleSheet::new();
    let plain = TextStyle::new(20.0);
    let small_caps = plain.clone().features([crate::text::FontFeatureSetting {
        tag: *b"smcp",
        value: 1,
    }]);
    let width_of = |style: &TextStyle| {
        RichTextRun::new(
            "widths",
            style,
            Color::from_rgba8(0, 0, 0, 255),
            &sheet,
            &palette(),
            96.0,
        )
        .natural_width()
    };
    // Both shape; the point is that the feature reaches parley at
    // all rather than being silently dropped on the rich path.
    assert!(width_of(&plain) > 0.0);
    assert!(width_of(&small_caps) > 0.0);
}

#[test]
fn list_markers_draw_in_the_gutter_left_of_the_body() {
    let run = make("- alpha");
    let blocks = run.blocks.borrow();
    let bl = blocks
        .iter()
        .find(|b| b.marker.is_some())
        .expect("the item body carries the marker");
    let marker = bl.marker.as_ref().expect("marker");
    let (x0, x1) = marker_x_range(bl, marker);
    assert!(x1 <= bl.left_px, "marker must sit start-side of the text");
    assert!(x0 < x1, "marker must have width");
}

#[test]
fn multi_digit_ordinals_share_a_right_edge() {
    let run = make(
        "1. one\n2. two\n3. three\n4. four\n5. five\n6. six\n7. seven\n8. eight\n9. nine\n10. ten",
    );
    let blocks = run.blocks.borrow();
    let right_edges: Vec<f32> = blocks
        .iter()
        .filter_map(|bl| bl.marker.as_ref().map(|m| marker_x_range(bl, m).1))
        .collect();
    assert!(right_edges.len() >= 10, "expected ten markers");
    let first = right_edges[0];
    for e in &right_edges {
        assert!(
            (e - first).abs() < 0.01,
            "markers should right-align, got {right_edges:?}"
        );
    }
}

#[test]
fn a_background_with_no_padding_still_blocks_margin_collapse() {
    let mut sheet = RichTextStyleSheet::new();
    sheet.set(
        "barrier",
        StyleDelta {
            background: Some(crate::style_vocab::ThemeColor::Accent),
            margin: Some(RichMargin::new(pt(20.0), pt(0.0), pt(20.0), pt(0.0))),
            ..StyleDelta::empty()
        },
    );
    sheet.set(
        "plain",
        StyleDelta {
            margin: Some(RichMargin::new(pt(20.0), pt(0.0), pt(20.0), pt(0.0))),
            ..StyleDelta::empty()
        },
    );
    let make_with = |src: &str| {
        RichTextRun::new(
            src,
            &base_style(),
            Color::from_rgba8(0, 0, 0, 255),
            &sheet,
            &palette(),
            96.0,
        )
    };
    // Two stacked divs, each wrapping a paragraph that carries
    // its own bottom margin. With no barrier that inner margin
    // collapses out through the div's edge and merges with the
    // div's own; a background on the div stops it, so the painted
    // stack is one paragraph margin taller.
    let collapsed = make_with(":::plain\na\n:::\n:::plain\nb\n:::").natural_height();
    let separated = make_with(":::barrier\na\n:::\n:::plain\nb\n:::").natural_height();
    assert!(
        separated > collapsed + 15.0,
        "a background must stop the collapse ({collapsed} → {separated})"
    );
}