oxedyne/fe2o3/fe2o3_austenite/tests/page_setup.rs
7.0 KiB, 1 run
created by r1870400018:71964, 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 page(..)` on the reader path (`austenite` without `--eval`): `paper`, `width`, `height` and |
| 2 | //! `margin` lay the document's own geometry over the base, a one-sided margin is not mirrored, and |
| 3 | //! `numbering` sets Typst's page number or none. With no `par` set, the leading and paragraph spacing |
| 4 | //! are Typst's 0.65em and 1.2em. Before 2026-10-11 a `#set page` was refused and left the page at A4. |
| 5 | |
| 6 | use oxedyne_fe2o3_austenite::compile; |
| 7 | use oxedyne_fe2o3_austenite::compile::Rendered; |
| 8 | use oxedyne_fe2o3_austenite::fonts; |
| 9 | use oxedyne_fe2o3_austenite::ir::Sp; |
| 10 | use oxedyne_fe2o3_austenite::page::PageGeometry; |
| 11 | use oxedyne_fe2o3_austenite::page::PlacedKind; |
| 12 | use oxedyne_fe2o3_austenite::theme::Theme; |
| 13 | use oxedyne_fe2o3_austenite::vfs; |
| 14 | |
| 15 | use oxedyne_fe2o3_core::prelude::*; |
| 16 | |
| 17 | use std::collections::HashMap; |
| 18 | use std::path::PathBuf; |
| 19 | use std::sync::{ |
| 20 | Arc, |
| 21 | Mutex, |
| 22 | MutexGuard, |
| 23 | }; |
| 24 | |
| 25 | // The source map is a process global, so the tests take turns. |
| 26 | static VFS_TURN: Mutex<()> = Mutex::new(()); |
| 27 | |
| 28 | fn turn() -> MutexGuard<'static, ()> { |
| 29 | VFS_TURN.lock().unwrap_or_else(|p| p.into_inner()) |
| 30 | } |
| 31 | |
| 32 | const CM: f64 = 72.0 / 2.54; // points in a centimetre |
| 33 | const TOL: f64 = 0.05; // points, rounding slack only |
| 34 | |
| 35 | const PARA: &str = "The quick brown fox jumps over the lazy dog while the patient reader waits for the \ |
| 36 | paragraph to wrap across several lines, so that the page fills and the next one begins."; |
| 37 | |
| 38 | /// `head` followed by `paras` paragraphs, compiled through the reader path. |
| 39 | fn render(head: &str, paras: usize) -> Outcome<Rendered> { |
| 40 | let _turn = turn(); |
| 41 | let mut src = fmt!("{}\n\n", head); |
| 42 | for _ in 0..paras { |
| 43 | src.push_str(PARA); |
| 44 | src.push_str("\n\n"); |
| 45 | } |
| 46 | let mut map: HashMap<PathBuf, Vec<u8>> = HashMap::new(); |
| 47 | map.insert(PathBuf::from("/pagesetup/main.typ"), src.into_bytes()); |
| 48 | res!(vfs::install(map)); |
| 49 | let fonts = Arc::new(res!(fonts::libertinus())); |
| 50 | let run = || -> Outcome<Rendered> { |
| 51 | let assembled = res!(compile::assemble(&PathBuf::from("/pagesetup/main.typ"), || Ok(fonts.clone()))); |
| 52 | compile::author_and_run(assembled) |
| 53 | }; |
| 54 | let out = run(); |
| 55 | res!(vfs::clear()); |
| 56 | out |
| 57 | } |
| 58 | |
| 59 | fn near(a: Sp, want_pt: f64) -> bool { |
| 60 | (a.to_pt() - want_pt).abs() < TOL |
| 61 | } |
| 62 | |
| 63 | /// The texts set below the text block of each page, in page order. |
| 64 | fn foot_texts(r: &Rendered) -> Vec<Vec<String>> { |
| 65 | let bottom = r.geom.content_top() + r.geom.content_height(); |
| 66 | r.out.pages.iter().map(|page| { |
| 67 | page.frame.placed.iter().filter_map(|p| match &p.kind { |
| 68 | PlacedKind::Text(t) if p.y >= bottom => Some(t.source().to_string()), |
| 69 | _ => None, |
| 70 | }).collect() |
| 71 | }).collect() |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn margin_and_paper_change_the_geometry() -> Outcome<()> { |
| 76 | let r = res!(render("#set page(margin: 2.2cm)", 1)); |
| 77 | let g = r.geom; |
| 78 | for side in [g.inside, g.outside, g.top, g.bottom] { |
| 79 | if !near(side, 2.2 * CM) { |
| 80 | return Err(err!("A 2.2cm margin set a side of {}pt.", side.to_pt(); Test, Mismatch)); |
| 81 | } |
| 82 | } |
| 83 | if g.width != PageGeometry::a4().width { |
| 84 | return Err(err!("A margin alone moved the page width to {}pt.", g.width.to_pt(); Test, Mismatch)); |
| 85 | } |
| 86 | let r = res!(render("#set page(paper: \"a5\")", 1)); |
| 87 | if !near(r.geom.width, 148.0 / 25.4 * 72.0) || !near(r.geom.height, 210.0 / 25.4 * 72.0) { |
| 88 | return Err(err!("Paper a5 set {}pt by {}pt.", r.geom.width.to_pt(), r.geom.height.to_pt(); Test, Mismatch)); |
| 89 | } |
| 90 | let r = res!(render("#set page(paper: \"a5\", width: 100mm)", 1)); |
| 91 | if !near(r.geom.width, 100.0 / 25.4 * 72.0) || !near(r.geom.height, 210.0 / 25.4 * 72.0) { |
| 92 | return Err(err!("A width over a5 set {}pt by {}pt.", r.geom.width.to_pt(), r.geom.height.to_pt(); Test, Mismatch)); |
| 93 | } |
| 94 | Ok(()) |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn margin_dictionary_sides_beat_axes_beat_rest() -> Outcome<()> { |
| 99 | let r = res!(render("#set page(margin: (rest: 1cm, x: 2cm, left: 3cm))", 1)); |
| 100 | let g = r.geom; |
| 101 | let want = [(g.inside, 3.0), (g.outside, 2.0), (g.top, 1.0), (g.bottom, 1.0)]; |
| 102 | for (side, cm) in want { |
| 103 | if !near(side, cm * CM) { |
| 104 | return Err(err!("A margin dictionary set {}pt where {}cm was wanted.", side.to_pt(), cm; Test, Mismatch)); |
| 105 | } |
| 106 | } |
| 107 | Ok(()) |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn one_sided_margins_are_not_mirrored() -> Outcome<()> { |
| 112 | let r = res!(render("#set page(paper: \"a6\", margin: (left: 3cm, right: 1cm))", 30)); |
| 113 | if r.out.pages.len() < 2 { |
| 114 | return Err(err!("Expected at least two pages, found {}.", r.out.pages.len(); Invalid, Output)); |
| 115 | } |
| 116 | for page in r.out.pages.iter().take(2) { |
| 117 | let left = page.frame.placed.iter() |
| 118 | .filter(|p| matches!(p.kind, PlacedKind::Text(_))) |
| 119 | .map(|p| p.x) |
| 120 | .min(); |
| 121 | match left { |
| 122 | Some(x) if near(x, 3.0 * CM) => {}, |
| 123 | other => return Err(err!( |
| 124 | "Page {} sets its text from {:?}, not the 3cm left margin.", page.number, other.map(|x| x.to_pt()); |
| 125 | Test, Mismatch)), |
| 126 | } |
| 127 | } |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn numbering_pattern_numbers_every_page() -> Outcome<()> { |
| 133 | let r = res!(render("#set page(paper: \"a6\", numbering: \"1\")", 30)); |
| 134 | let feet = foot_texts(&r); |
| 135 | if feet.len() < 2 { |
| 136 | return Err(err!("Expected at least two pages, found {}.", feet.len(); Invalid, Output)); |
| 137 | } |
| 138 | for (i, foot) in feet.iter().enumerate() { |
| 139 | if foot != &vec![fmt!("{}", i + 1)] { |
| 140 | return Err(err!("Page {} carries {:?} at its foot.", i + 1, foot; Test, Mismatch)); |
| 141 | } |
| 142 | } |
| 143 | // The number's cap top stands three-tenths of the bottom margin below the text block. |
| 144 | let g = r.geom; |
| 145 | let page = &r.out.pages[0]; |
| 146 | let bottom = g.content_top() + g.content_height(); |
| 147 | let num = page.frame.placed.iter().find(|p| p.y >= bottom); |
| 148 | match num { |
| 149 | Some(p) if p.y < bottom + Sp::from_pt(0.3 * g.bottom.to_pt()) => {}, |
| 150 | other => return Err(err!("The page number stands at {:?}.", other.map(|p| p.y.to_pt()); Test, Mismatch)), |
| 151 | } |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn numbering_none_numbers_no_page() -> Outcome<()> { |
| 157 | let r = res!(render("#set page(paper: \"a6\", numbering: none)", 30)); |
| 158 | let feet = foot_texts(&r); |
| 159 | if feet.len() < 2 || feet.iter().any(|f| !f.is_empty()) { |
| 160 | return Err(err!("numbering: none left {:?} at the feet.", feet; Test, Mismatch)); |
| 161 | } |
| 162 | Ok(()) |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn no_set_page_keeps_the_geometry() -> Outcome<()> { |
| 167 | let r = res!(render("#set text(size: 11pt)", 1)); |
| 168 | if r.geom != PageGeometry::a4() { |
| 169 | return Err(err!("A file with no #set page took {:?}.", r.geom; Test, Mismatch)); |
| 170 | } |
| 171 | Ok(()) |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn defaults_are_typst_leading_and_spacing() -> Outcome<()> { |
| 176 | let t = Theme::default(); |
| 177 | let gap = res!(t.text.leading_gap.ok_or_else(|| err!("No default leading gap."; Test, Missing))); |
| 178 | if gap.em != 0.65 || gap.abs != Sp::ZERO { |
| 179 | return Err(err!("The default leading gap is {:?}, not 0.65em.", gap; Test, Mismatch)); |
| 180 | } |
| 181 | if t.par.skip_em != Some(1.2) || !near(t.par.skip, 1.2 * 11.0) { |
| 182 | return Err(err!("The default paragraph spacing is {:?} ({}pt), not 1.2em.", t.par.skip_em, t.par.skip.to_pt(); |
| 183 | Test, Mismatch)); |
| 184 | } |
| 185 | // Both follow a body size set without a `par`. |
| 186 | let mut big = Theme::default(); |
| 187 | let mut patch = oxedyne_fe2o3_austenite::theme::ThemePatch::default(); |
| 188 | patch.text.body_size = Some(Sp::from_pt(20.0)); |
| 189 | big.apply(&patch); |
| 190 | if !near(big.par.skip, 24.0) || !near(big.text.leading, (t.calibration.line_box_em + 0.65) * 20.0) { |
| 191 | return Err(err!("At 20pt the spacing is {}pt and the pitch {}pt.", big.par.skip.to_pt(), big.text.leading.to_pt(); |
| 192 | Test, Mismatch)); |
| 193 | } |
| 194 | Ok(()) |
| 195 | } |