Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/par_leading.rs

4.4 KiB, 1 run

created by r1870400018:71647, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `#set par(leading: ..)` on the reader path (`austenite` without `--eval`) keeps Typst's meaning: the
2//! gap added between line boxes, in `em` or `pt`, so the baseline pitch is the calibrated line box plus
3//! that gap. Before 2026-10-11 an `em` leading was refused and a `pt` one was taken as the pitch itself.
4
5use oxedyne_fe2o3_austenite::compile;
6use oxedyne_fe2o3_austenite::fonts;
7use oxedyne_fe2o3_austenite::page::PlacedKind;
8use oxedyne_fe2o3_austenite::theme::Theme;
9use oxedyne_fe2o3_austenite::vfs;
10
11use oxedyne_fe2o3_core::prelude::*;
12
13use std::collections::HashMap;
14use std::path::PathBuf;
15use std::sync::{
16 Arc,
17 Mutex,
18 MutexGuard,
19};
20
21// The source map is a process global, so the tests take turns.
22static VFS_TURN: Mutex<()> = Mutex::new(());
23
24fn turn() -> MutexGuard<'static, ()> {
25 VFS_TURN.lock().unwrap_or_else(|p| p.into_inner())
26}
27
28// A quarter point, in scaled points: slack for rounding only.
29const TOL: f64 = 0.25;
30
31const BODY: &str = "The quick brown fox jumps over the lazy dog while the patient reader waits for the \
32 paragraph to wrap across several lines, so that the distance between one baseline and the next can be \
33 measured as many times as a reader might wish, without a heading or a list to disturb the count. \
34 Another sentence follows to make quite sure that the paragraph runs to five or six lines at the least.";
35
36/// The most common gap, in points, between consecutive distinct baselines of `head` + [`BODY`] compiled
37/// through the reader path. The body is one paragraph, so every gap is a line pitch.
38fn body_pitch(head: &str) -> Outcome<f64> {
39 let _turn = turn();
40 let src = fmt!("{}\n\n{}\n", head, BODY);
41 let mut map: HashMap<PathBuf, Vec<u8>> = HashMap::new();
42 map.insert(PathBuf::from("/leading/main.typ"), src.into_bytes());
43 res!(vfs::install(map));
44 let fonts = Arc::new(res!(fonts::libertinus()));
45 let run = || -> Outcome<Vec<i32>> {
46 let assembled = res!(compile::assemble(&PathBuf::from("/leading/main.typ"), || Ok(fonts.clone())));
47 let rendered = res!(compile::author_and_run(assembled));
48 let mut bases: Vec<i32> = Vec::new();
49 for page in rendered.out.pages.iter().take(1) {
50 for placed in &page.frame.placed {
51 if let PlacedKind::Text(_) = &placed.kind {
52 let base = placed.y.raw() + placed.dims.height.raw();
53 if !bases.contains(&base) {
54 bases.push(base);
55 }
56 }
57 }
58 }
59 Ok(bases)
60 };
61 let out = run();
62 res!(vfs::clear());
63 let mut bases = res!(out);
64 bases.sort();
65 if bases.len() < 4 {
66 return Err(err!("Expected at least four body lines, found {}.", bases.len(); Invalid, Output));
67 }
68 let mut gaps: Vec<i32> = bases.windows(2).map(|w| w[1] - w[0]).collect();
69 gaps.sort();
70 let mut best = (gaps[0], 0usize);
71 for g in &gaps {
72 let n = gaps.iter().filter(|x| *x == g).count();
73 if n > best.1 {
74 best = (*g, n);
75 }
76 }
77 Ok(best.0 as f64 / 65536.0)
78}
79
80fn line_box_pt(body_pt: f64) -> f64 {
81 Theme::default().calibration.line_box_em * body_pt
82}
83
84#[test]
85fn unset_leading_keeps_the_theme_default_pitch() -> Outcome<()> {
86 let pitch = res!(body_pitch(""));
87 let want = Theme::default().text.leading.to_pt();
88 if (pitch - want).abs() > TOL {
89 return Err(err!("Unset leading set a {:.3}pt pitch, the theme default is {:.3}pt.", pitch, want; Invalid, Output));
90 }
91 Ok(())
92}
93
94#[test]
95fn em_leading_is_the_gap_between_line_boxes() -> Outcome<()> {
96 let pitch = res!(body_pitch("#set par(leading: 0.9em)"));
97 let want = line_box_pt(11.0) + 0.9 * 11.0;
98 if (pitch - want).abs() > TOL {
99 return Err(err!("A 0.9em leading set a {:.3}pt pitch, want {:.3}pt.", pitch, want; Invalid, Output));
100 }
101 // The em follows the text size in force, wherever the `#set text` sits in the batch.
102 let pitch = res!(body_pitch("#set par(leading: 0.65em)\n#set text(size: 14pt)"));
103 let want = line_box_pt(14.0) + 0.65 * 14.0;
104 if (pitch - want).abs() > TOL {
105 return Err(err!("A 0.65em leading at 14pt set a {:.3}pt pitch, want {:.3}pt.", pitch, want; Invalid, Output));
106 }
107 Ok(())
108}
109
110#[test]
111fn pt_leading_is_the_gap_between_line_boxes() -> Outcome<()> {
112 // Above the reader's floor: a pitch never drops below the face's nominal ascender + descender (~1.14em),
113 // so a gap under ~0.48em does not tighten further there, unlike Typst.
114 let pitch = res!(body_pitch("#set par(leading: 8pt)"));
115 let want = line_box_pt(11.0) + 8.0;
116 if (pitch - want).abs() > TOL {
117 return Err(err!("An 8pt leading set a {:.3}pt pitch, want {:.3}pt.", pitch, want; Invalid, Output));
118 }
119 Ok(())
120}