lichtung-plot 0.2.0

Strongly typed scientific plotting with deterministic SVG and PNG output
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
use super::*;
use crate::render::backend::{Backend, PlannedTextPlacement, Point2D, TextAnchor};
use crate::render::svg_backend::SvgBackend;
use canonical::MetricSource;

mod glyph_paint;

/// Change only generated fonts, through the existing retained-font test entry.
pub(crate) fn font(
    family: &str,
    coverage: &[char],
    typo: Option<(i16, i16)>,
    hhea: (i16, i16),
) -> Vec<u8> {
    let generated = PreparedFontEnvironment::synthetic_faces(&[(family, coverage)]);
    let mut database = fontdb::Database::new();
    generated.populate_database(&mut database).unwrap();
    let id = database.faces().next().unwrap().id;
    database
        .with_face_data(id, |bytes, index| {
            let raw = ttf_parser::RawFace::parse(bytes, index).unwrap();
            let tables = raw
                .table_records
                .into_iter()
                .filter_map(|record| {
                    let tag = record.tag.0.to_be_bytes();
                    let mut data = raw.table(record.tag).unwrap().to_vec();
                    let set = |data: &mut [u8], offset, value: i16| {
                        data[offset..offset + 2].copy_from_slice(&value.to_be_bytes());
                    };
                    match &tag {
                        b"OS/2" => {
                            let (a, d) = typo?;
                            set(&mut data, 68, a);
                            set(&mut data, 70, d);
                        }
                        b"hhea" => {
                            set(&mut data, 4, hhea.0);
                            set(&mut data, 6, hhea.1);
                        }
                        _ => {}
                    }
                    Some((tag, data))
                })
                .collect();
            super::shaping_tests::sfnt(tables)
        })
        .unwrap()
}

fn plan(
    environment: &PreparedFontEnvironment,
    source: &str,
    names: &[&str],
) -> Result<ResolvedTextPlan, TextPlanError> {
    let stack = canonical::named_stack(names);
    let mut input = super::tests::input(source, i64::MAX, PhysicalTextAlignment::Start);
    input.stack = &stack;
    environment.plan_text(input)
}

fn scene(
    environment: &PreparedFontEnvironment,
    plan: &ResolvedTextPlan,
) -> crate::scene::RenderedScene {
    let mut backend = SvgBackend::in_memory_with_fonts(160.0, 120.0, environment);
    backend.begin(160.0, 120.0).unwrap();
    backend
        .draw_planned_text(
            PlannedTextPlacement::baseline(Point2D::new(20.0, 50.0), TextAnchor::Start, 0.0),
            plan,
        )
        .unwrap();
    backend.end().unwrap();
    crate::scene::RenderedScene::new(
        crate::layout::RootSize::new(160, 120),
        backend.into_output(),
    )
    .unwrap()
}

fn alter(bytes: &[u8], mut edit: impl FnMut([u8; 4], &mut Vec<u8>)) -> Vec<u8> {
    let raw = ttf_parser::RawFace::parse(bytes, 0).unwrap();
    super::shaping_tests::sfnt(
        raw.table_records
            .into_iter()
            .map(|record| {
                let tag = record.tag.0.to_be_bytes();
                let mut data = raw.table(record.tag).unwrap().to_vec();
                edit(tag, &mut data);
                (tag, data)
            })
            .collect(),
    )
}

fn check_output(environment: &PreparedFontEnvironment, measured: &ResolvedTextPlan) {
    let before = canonical::planning_counts();
    let scene = scene(environment, measured);
    let outlined = scene.svg_with_text_as_paths().unwrap();
    let png = scene
        .png_bytes(&crate::output::PngOptions {
            sizing: crate::output::PngSizing::Source,
            ..crate::output::PngOptions::default()
        })
        .unwrap();
    let actual = resvg::tiny_skia::Pixmap::decode_png(&png).unwrap();
    // The same frozen paths, not an independently shaped text scene, must
    // determine the raster's geometry. The independent 8-bit compositor may
    // differ by one sample when overlapping glyph coverage rounds separately.
    let tree = resvg::usvg::Tree::from_data(&outlined, &resvg::usvg::Options::default()).unwrap();
    let mut expected = resvg::tiny_skia::Pixmap::new(160, 120).unwrap();
    resvg::render(
        &tree,
        resvg::tiny_skia::Transform::identity(),
        &mut expected.as_mut(),
    );
    assert_eq!(
        (actual.width(), actual.height()),
        (expected.width(), expected.height())
    );
    let largest_sample_difference = actual
        .data()
        .iter()
        .zip(expected.data())
        .map(|(&actual, &expected)| actual.abs_diff(expected))
        .max()
        .unwrap();
    assert!(
        largest_sample_difference <= 1,
        "PNG samples differ beyond device rounding"
    );
    assert!(
        actual
            .pixels()
            .iter()
            .zip(expected.pixels())
            .all(|(actual, expected)| {
                (actual.alpha() == 0) == (expected.alpha() == 0)
                    && (actual.alpha() == 255) == (expected.alpha() == 255)
            }),
        "PNG must preserve the frozen outlined scene's painted and fully covered pixels"
    );
    assert_eq!(canonical::planning_counts(), before);
}

#[test]
fn double_zero_nominal_metrics_can_render_from_actual_ink() {
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![font(
        "Zero",
        &[' ', 'A'],
        Some((0, 0)),
        (0, 0),
    )]);
    let measured = plan(&environment, "A", &["Zero"]).unwrap();
    let run = &measured.lines[0].runs[0];
    assert_eq!(run.metric_source, MetricSource::InkOnly);
    assert_eq!((run.ascender_quanta, run.descender_quanta), (0, 0));
    assert_eq!(measured.height_quanta, 7 * 1024);
    assert_eq!(measured.first_baseline_quanta, Some(7 * 1024));
    let scene = scene(&environment, &measured);
    let outlined = scene.svg_with_text_as_paths().unwrap();
    assert!(!outlined.windows(5).any(|bytes| bytes == b"<text"));
    assert!(outlined.windows(5).any(|bytes| bytes == b"<path"));
    let png = scene
        .png_bytes(&crate::output::PngOptions::default())
        .unwrap();
    let pixels = resvg::tiny_skia::Pixmap::decode_png(&png).unwrap();
    assert!(pixels.pixels().iter().any(|pixel| pixel.alpha() != 0));
    check_output(&environment, &measured);
    for source in [" A ", "A\nA", "A\u{200d}", "A\n", "A\n\nA"] {
        let measured = plan(&environment, source, &["Zero"]).unwrap();
        assert!(measured.height_quanta > 0);
        if source == "A\n\nA" {
            assert_eq!(measured.height_quanta, 31 * 1024);
            assert_eq!(measured.first_baseline_quanta, Some(7 * 1024));
            assert_eq!(measured.last_baseline_quanta, Some(31 * 1024));
        }
        check_output(&environment, &measured);
    }
    for source in ["", " ", "\u{200d}"] {
        assert!(
            matches!(
                plan(&environment, source, &["Zero"]),
                Err(TextPlanError::NoTextHeight { .. })
            ),
            "{source:?}"
        );
    }
}

#[test]
fn pure_blank_lines_use_leading_for_total_height_and_frozen_output() {
    for (source, count, height) in [("\n", 2, 12 * 1024), ("\n\n", 3, 24 * 1024)] {
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![font(
            "Zero",
            &[' '],
            Some((0, 0)),
            (0, 0),
        )]);
        let stack = canonical::named_stack(&["Zero"]);
        let mut input = super::tests::input(source, i64::MAX, PhysicalTextAlignment::Start);
        input.stack = &stack;
        let measured = environment.plan_text(input.clone()).unwrap();
        assert_eq!(measured.lines.len(), count);
        assert_eq!(measured.height_quanta, height);
        assert_eq!(measured.line_pitch_quanta, 12 * 1024);
        assert_eq!(measured.first_baseline_quanta, Some(0));
        assert_eq!(measured.last_baseline_quanta, Some(height));
        for (index, line) in measured.lines.iter().enumerate() {
            assert_eq!(
                line.translation_y_quanta,
                i64::try_from(index).unwrap() * 12 * 1024
            );
            assert!(line.runs.is_empty() && line.ink_quanta.is_none());
        }
        let direct = environment
            .text_intrinsic_requirements(input.clone(), 1)
            .unwrap();
        let cached = measured.intrinsic_from_analysis(1).unwrap();
        assert_eq!(
            (
                direct.hard_width_quanta,
                direct.natural_width_quanta,
                direct.preferred_width_quanta
            ),
            (0, 0, 0)
        );
        assert_eq!(
            (
                cached.hard_width_quanta,
                cached.natural_width_quanta,
                cached.preferred_width_quanta
            ),
            (0, 0, 0)
        );
        input.maximum_width_quanta = 0;
        let reflowed = measured.reflow(input.clone()).unwrap();
        assert_eq!(reflowed.height_quanta, height);
        input.line_height = TextLineHeight::Multiplier(2.4);
        let taller = measured.reflow(input).unwrap();
        assert_eq!(taller.height_quanta, 2 * height);
        assert_eq!(taller.first_baseline_quanta, Some(0));
        assert_eq!(taller.last_baseline_quanta, Some(2 * height));
        let region = crate::layout::PixelRect::new(0.0, 160.0, 0.0, 120.0);
        let placement = super::placement::resolve(
            PlannedTextPlacement::baseline(Point2D::new(20.0, 50.0), TextAnchor::Start, 0.0),
            &measured,
            Some(region),
        )
        .unwrap();
        super::placement::contain(
            measured.clone(),
            placement,
            region,
            &crate::layout::fit::FitScope::root(),
        )
        .unwrap();
        environment.operation().unwrap().seal_fonts().unwrap();
        for plan in [&measured, &reflowed, &taller] {
            check_output(&environment, plan);
        }
        let frozen = scene(&environment, &measured);
        let outlined = frozen.svg_with_text_as_paths().unwrap();
        assert!(!outlined.windows(5).any(|bytes| bytes == b"<path"));
        let png = frozen
            .png_bytes(&crate::output::PngOptions {
                sizing: crate::output::PngSizing::Source,
                ..crate::output::PngOptions::default()
            })
            .unwrap();
        let pixels = resvg::tiny_skia::Pixmap::decode_png(&png).unwrap();
        assert!(pixels.pixels().iter().all(|pixel| pixel.alpha() == 0));
    }
}

#[test]
fn blank_line_ellipsis_and_fit_reuse_the_same_total_height_rule() {
    use crate::layout::fit::{FitOutcome, FitScope};
    use crate::text::{TextAppearance, TextRequestContext, TextService};
    for (source, height) in [("\n", 12 * 1024), ("\n\n", 24 * 1024)] {
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![font(
            "Zero",
            &[' '],
            Some((0, 0)),
            (0, 0),
        )]);
        let appearance = crate::Theme::publication().resolve().unwrap();
        let text = TextService::default();
        text.fonts.set(environment.clone()).unwrap();
        let context = TextRequestContext::new(&appearance, &text);
        let stack = canonical::named_stack(&["Zero"]);
        let style = context
            .typography
            .root
            .caption
            .clone()
            .stack(stack.clone())
            .unwrap()
            .font_size(10.0)
            .unwrap()
            .line_height(1.2)
            .unwrap();
        let layout =
            crate::text::TextLayout::default().overflow(crate::text::TextOverflow::ellipsis(
                crate::text::TextEllipsis::new(crate::text::TextElision::End).max_lines(None),
                crate::text::TextFailure::Error,
            ));
        for request in [
            layout.clone(),
            layout.fit(crate::text::TextFit::shrink_to(10.0).unwrap()),
        ] {
            let FitOutcome::Measured(measured) = super::request::measure_quanta(
                &context,
                source,
                TextAppearance::from(&style).layout(Some(&request)),
                0,
                PhysicalTextAlignment::Start,
                "blank line reuse",
                &FitScope::root(),
            )
            .unwrap() else {
                panic!("complete blank-line content fits at zero width")
            };
            assert_eq!(measured.source, source);
            assert_eq!(measured.height_quanta, height);
            assert_eq!(measured.first_baseline_quanta, Some(0));
            assert_eq!(measured.last_baseline_quanta, Some(height));
            check_output(&environment, &measured);
        }
        let pair = environment
            .prepared_pair(&stack, RequestedWeight::Normal)
            .unwrap()
            .unwrap();
        assert!(
            pair.selection_for_unit("\u{2026}", '\u{2026}').is_err(),
            "an unused marker is not needed to measure blank-line height"
        );
    }
}

#[test]
fn frozen_metric_source_mismatch_is_an_output_error_not_font_fallback() {
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![
        font("Frozen", &['A'], Some((800, 0)), (900, -300)),
        font("Fallback", &['A'], Some((800, -200)), (900, -300)),
    ]);
    let mut measured = plan(&environment, "A", &["Frozen", "Fallback"]).unwrap();
    measured.payload_mut().lines[0].runs[0].metric_source = MetricSource::Hhea;
    let scene = scene(&environment, &measured);
    let error = scene.svg_with_text_as_paths().unwrap_err();
    assert!(error.to_string().contains("metrics do not match"));
    assert!(scene
        .png_bytes(&crate::output::PngOptions::default())
        .is_err());
}

#[test]
fn zero_height_error_identifies_used_faces_not_unused_primary_or_fallback() {
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![
        font("Primary", &['X'], Some((0, 0)), (0, 0)),
        font("Used", &[' '], Some((0, 0)), (0, 0)),
        font("Unused", &[' '], Some((800, -200)), (900, -300)),
    ]);
    let stack = canonical::named_stack(&["Primary", "Used", "Unused"]);
    let error = plan(&environment, " ", &["Primary", "Used", "Unused"]).unwrap_err();
    let pair = environment
        .prepared_pair(&stack, RequestedWeight::Normal)
        .unwrap()
        .unwrap();
    let TextPlanError::NoTextHeight { faces, .. } = error else {
        panic!("selected blank is not replaced by another font")
    };
    assert_eq!(faces, [pair.provenance.candidates[1]]);
    let error = plan(&environment, "", &["Primary", "Used", "Unused"]).unwrap_err();
    let TextPlanError::NoTextHeight { faces, .. } = error else {
        panic!("empty block uses primary metrics")
    };
    assert_eq!(faces, [pair.primary]);
}

#[test]
fn metric_pairs_preserve_zero_endpoints_and_use_one_same_face_source() {
    for (typo, hhea, expected, values) in [
        (
            Some((800, 0)),
            (900, -300),
            MetricSource::Typographic,
            (800, 0),
        ),
        (
            Some((0, -200)),
            (900, -300),
            MetricSource::Typographic,
            (0, -200),
        ),
        (Some((0, 0)), (900, -300), MetricSource::Hhea, (900, -300)),
        (
            Some((100, 200)),
            (900, -300),
            MetricSource::Hhea,
            (900, -300),
        ),
        (None, (900, -300), MetricSource::Hhea, (900, -300)),
    ] {
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![font(
            "Metric",
            &[' ', 'A'],
            typo,
            hhea,
        )]);
        let measured = plan(&environment, " ", &["Metric"]).unwrap();
        let run = &measured.lines[0].runs[0];
        assert_eq!(run.metric_source, expected);
        assert_eq!((run.design_ascender, run.design_descender), values);
        assert_eq!(
            measured.first_baseline_quanta,
            Some(i64::from(values.0.max(0)) * 1024 / 100)
        );
        check_output(&environment, &measured); // includes baseline=0
    }
    let generated = font("Metric", &['A'], Some((800, -200)), (900, -300));
    for (version, flags, truncate) in [
        (4_u16, 0_u16, false),
        (4, 0x80, false),
        (0, 0x80, false),
        (0, 0, true),
    ] {
        let bytes = alter(&generated, |tag, data| {
            if tag == *b"OS/2" {
                if version == 4 {
                    data.resize(96, 0);
                }
                data[..2].copy_from_slice(&version.to_be_bytes());
                data[62..64].copy_from_slice(&flags.to_be_bytes());
                if truncate {
                    data.truncate(68);
                }
            }
        });
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![bytes]);
        let measured = plan(&environment, "A", &["Metric"]).unwrap();
        let run = &measured.lines[0].runs[0];
        assert_eq!(
            run.metric_source,
            if truncate {
                MetricSource::Hhea
            } else {
                MetricSource::Typographic
            }
        );
        assert_eq!(run.design_ascender, if truncate { 900 } else { 800 });
    }
}

#[test]
fn metric_denominators_extremes_and_outward_quantization_remain_checked() {
    let generated = font(
        "Metric",
        &[' ', 'A'],
        Some((i16::MAX, i16::MIN)),
        (800, -200),
    );
    for em in [16_u16, 1001, 16384] {
        let bytes = alter(&generated, |tag, data| {
            if tag == *b"head" {
                data[18..20].copy_from_slice(&em.to_be_bytes());
            }
        });
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![bytes]);
        let stack = canonical::named_stack(&["Metric"]);
        for size in [f64::from_bits(1), 0.0001, 10.0] {
            let mut input = super::tests::input(" ", i64::MAX, PhysicalTextAlignment::Start);
            input.stack = &stack;
            input.font_size = size;
            let measured = environment.plan_text(input).unwrap();
            assert!(measured.height_quanta > 0 && measured.line_pitch_quanta > 0);
            let run = &measured.lines[0].runs[0];
            assert!(run.ascender_quanta >= 0 && run.descender_quanta <= 0);
        }
        let mut input = super::tests::input("A", i64::MAX, PhysicalTextAlignment::Start);
        input.stack = &stack;
        input.font_size = f64::MAX;
        input.line_height = TextLineHeight::ResolvedQuanta(1);
        let error = environment.plan_text(input).unwrap_err();
        assert!(
            matches!(
                error,
                TextPlanError::Arithmetic(_) | TextPlanError::InvalidLineHeight
            ),
            "{error:?}"
        );
    }
    for em in [0_u16, 15, 16385] {
        let bytes = alter(&generated, |tag, data| {
            if tag == *b"head" {
                data[18..20].copy_from_slice(&em.to_be_bytes());
            }
        });
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![bytes]);
        assert!(matches!(
            plan(&environment, "A", &["Metric"]),
            Err(TextPlanError::Font(FontError::Admission { .. }))
        ));
    }
    for pair in [(800, 200), (-200, -800)] {
        let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![font(
            "Metric",
            &[' '],
            Some(pair),
            (0, 0),
        )]);
        let measured = plan(&environment, " ", &["Metric"]).unwrap();
        assert_eq!(measured.height_quanta, 8 * 1024);
        assert_eq!(
            measured.first_baseline_quanta,
            Some(if pair.0 > 0 { 8 * 1024 } else { 0 })
        );
        check_output(&environment, &measured);
    }
}

#[test]
fn only_actual_fallback_and_blank_lines_require_their_own_metrics() {
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![
        font("Empty nominal", &[' '], Some((0, 0)), (0, 0)),
        font("Fallback", &[' ', 'A'], Some((800, 0)), (900, -300)),
    ]);
    canonical::FONT_METRIC_QUERIES.with(|queries| queries.borrow_mut().clear());
    let measured = plan(&environment, "A", &["Empty nominal", "Fallback"]).unwrap();
    canonical::FONT_METRIC_QUERIES
        .with(|queries| assert!(queries.borrow().is_empty(), "no eager primary metric query"));
    let primary = measured.pair_provenance.candidates[0];
    assert_eq!(
        measured.pair_provenance.candidates.len(),
        2,
        "do not filter the primary"
    );
    assert_eq!(
        measured.lines[0].runs[0].face,
        measured.pair_provenance.candidates[1]
    );
    check_output(&environment, &measured);
    for source in ["A\n", "A\n\nA"] {
        canonical::FONT_METRIC_QUERIES.with(|queries| queries.borrow_mut().clear());
        let measured = plan(&environment, source, &["Empty nominal", "Fallback"]).unwrap();
        canonical::FONT_METRIC_QUERIES
            .with(|queries| assert_eq!(queries.borrow().as_slice(), &[primary]));
        assert!(measured.height_quanta > 0);
        check_output(&environment, &measured);
    }
    for source in ["", " "] {
        assert!(
            matches!(
                plan(&environment, source, &["Empty nominal", "Fallback"]),
                Err(TextPlanError::NoTextHeight { .. })
            ),
            "{source:?}"
        );
    }
    // The primary owns real blank lines; zero ascent is valid and baseline 0
    // remains accepted by placement and the concrete backend.
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![
        font("Blank", &[' '], Some((0, -200)), (900, -300)),
        font("Fallback", &['A'], Some((800, 0)), (900, -300)),
    ]);
    let measured = plan(&environment, "A\n\n \n", &["Blank", "Fallback"]).unwrap();
    assert_eq!(measured.lines.len(), 4);
    assert_eq!(measured.first_baseline_quanta, Some(8 * 1024));
    assert_eq!(measured.last_baseline_quanta, Some(44 * 1024));
    assert_eq!(measured.height_quanta, 46 * 1024);
    assert_eq!(measured.line_pitch_quanta, 12 * 1024);
    for (index, line) in measured.lines.iter().enumerate() {
        assert_eq!(
            line.translation_y_quanta,
            8 * 1024 + i64::try_from(index).unwrap() * 12 * 1024
        );
    }
    for source in ["", " ", "\n", "\u{200d}"] {
        let blank = plan(&environment, source, &["Blank", "Fallback"]).unwrap();
        assert_eq!(blank.first_baseline_quanta, Some(0));
        assert!(blank.height_quanta > 0);
        check_output(&environment, &blank);
    }
    check_output(&environment, &measured);
}

#[test]
fn real_positioned_ink_expands_zero_nominal_metrics_before_outline_and_png() {
    let bytes = super::shaping_tests::contextual_fonts().remove(0);
    let bytes = alter(&bytes, |tag, data| match &tag {
        b"OS/2" => data[68..72].fill(0),
        b"hhea" => data[4..8].fill(0),
        // Give all contextual glyphs the generated rectangle's real outline.
        b"glyf" => *data = data.repeat(8),
        b"loca" => {
            *data = [0_u16, 0, 17, 34, 51, 68, 85, 102, 119, 136]
                .into_iter()
                .flat_map(u16::to_be_bytes)
                .collect();
        }
        _ => {}
    });
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![bytes]);
    let measured = plan(&environment, "A\u{301}", &["First"]).unwrap();
    let glyph = &measured.lines[0].runs[0].glyphs[0];
    assert_eq!(
        (glyph.glyph_id, glyph.design_x_offset, glyph.design_y_offset),
        (4, -17, 23)
    );
    let ink = measured.lines[0].ink_quanta.unwrap();
    assert_eq!(measured.first_baseline_quanta, Some(ink.top));
    assert_eq!(ink.top, (723 * 1024 + 99) / 100);
    check_output(&environment, &measured);
    // Below-baseline ink must likewise expand a zero nominal descent.
    let bytes = alter(
        &font("Below", &['A'], Some((800, 0)), (900, -300)),
        |tag, data| {
            if tag == *b"glyf" {
                data[4..6].copy_from_slice(&(-200_i16).to_be_bytes());
                data[8..10].copy_from_slice(&500_i16.to_be_bytes());
                data[26..28].copy_from_slice(&(-200_i16).to_be_bytes());
            } else if tag == *b"head" {
                data[38..40].copy_from_slice(&(-200_i16).to_be_bytes());
                data[42..44].copy_from_slice(&500_i16.to_be_bytes());
            }
        },
    );
    let environment = PreparedFontEnvironment::synthetic_shaping_fonts(vec![bytes]);
    let measured = plan(&environment, "A", &["Below"]).unwrap();
    assert_eq!(measured.height_quanta, 10 * 1024);
    assert_eq!(measured.lines[0].ink_quanta.unwrap().bottom, -2 * 1024);
    check_output(&environment, &measured);
}