oxedyne/fe2o3/fe2o3_austenite/tests/align_text.rs
5.9 KiB, 1 run
created by r1870400018:72069, 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 | //! `#align(alignment)[body]`, an own-line `#text(args)[body]` and `#set heading(numbering: none)` on the |
| 2 | //! reader path (`austenite` without `--eval`). Before 2026-10-11 the first two were skipped, their words |
| 3 | //! dropped, and a chapter still drew its giant number under `numbering: none`. |
| 4 | |
| 5 | use oxedyne_fe2o3_austenite::compile; |
| 6 | use oxedyne_fe2o3_austenite::compile::Rendered; |
| 7 | use oxedyne_fe2o3_austenite::fonts; |
| 8 | use oxedyne_fe2o3_austenite::page::PlacedKind; |
| 9 | use oxedyne_fe2o3_austenite::vfs; |
| 10 | |
| 11 | use oxedyne_fe2o3_core::prelude::*; |
| 12 | |
| 13 | use std::collections::BTreeMap; |
| 14 | use std::collections::HashMap; |
| 15 | use std::path::PathBuf; |
| 16 | use std::sync::{ |
| 17 | Arc, |
| 18 | Mutex, |
| 19 | MutexGuard, |
| 20 | }; |
| 21 | |
| 22 | // The source map is a process global, so the tests take turns. |
| 23 | static VFS_TURN: Mutex<()> = Mutex::new(()); |
| 24 | |
| 25 | fn turn() -> MutexGuard<'static, ()> { |
| 26 | VFS_TURN.lock().unwrap_or_else(|p| p.into_inner()) |
| 27 | } |
| 28 | |
| 29 | const TOL: f64 = 0.05; // points, rounding slack only |
| 30 | |
| 31 | const PARA: &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 page fills and the last line falls short."; |
| 33 | |
| 34 | fn render(src: &str) -> Outcome<Rendered> { |
| 35 | let _turn = turn(); |
| 36 | let mut map: HashMap<PathBuf, Vec<u8>> = HashMap::new(); |
| 37 | map.insert(PathBuf::from("/aligntext/main.typ"), src.as_bytes().to_vec()); |
| 38 | res!(vfs::install(map)); |
| 39 | let fonts = Arc::new(res!(fonts::libertinus())); |
| 40 | let run = || -> Outcome<Rendered> { |
| 41 | let assembled = res!(compile::assemble(&PathBuf::from("/aligntext/main.typ"), || Ok(fonts.clone()))); |
| 42 | compile::author_and_run(assembled) |
| 43 | }; |
| 44 | let out = run(); |
| 45 | res!(vfs::clear()); |
| 46 | out |
| 47 | } |
| 48 | |
| 49 | /// The first page's body lines, top to bottom, each as (left edge, right edge, its words), measured |
| 50 | /// from the content block's left edge. |
| 51 | fn lines(r: &Rendered) -> Vec<(f64, f64, String)> { |
| 52 | let left = r.geom.content_left().to_pt(); |
| 53 | let bottom = r.geom.content_top() + r.geom.content_height(); |
| 54 | let mut by_y: BTreeMap<i32, (f64, f64, String)> = BTreeMap::new(); |
| 55 | if let Some(page) = r.out.pages.first() { |
| 56 | for p in &page.frame.placed { |
| 57 | if let PlacedKind::Text(t) = &p.kind { |
| 58 | if p.y >= bottom || p.y + p.dims.height < r.geom.content_top() { |
| 59 | continue; |
| 60 | } |
| 61 | let (x0, x1) = (p.x.to_pt() - left, (p.x + p.dims.width).to_pt() - left); |
| 62 | let e = by_y.entry(p.y.raw()).or_insert((f64::MAX, f64::MIN, String::new())); |
| 63 | if x0 < e.0 { e.0 = x0; } |
| 64 | if x1 > e.1 { e.1 = x1; } |
| 65 | e.2.push_str(t.source()); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | by_y.into_values().collect() |
| 70 | } |
| 71 | |
| 72 | fn measure(r: &Rendered) -> f64 { r.geom.content_width().to_pt() } |
| 73 | |
| 74 | #[test] |
| 75 | fn a_centred_block_centres_its_short_lines_and_not_its_justified_ones() -> Outcome<()> { |
| 76 | let r = res!(render(&fmt!("#set par(justify: true)\n\n#align(center)[\n {}\n]\n", PARA))); |
| 77 | let ls = lines(&r); |
| 78 | let w = measure(&r); |
| 79 | if ls.len() < 2 { |
| 80 | return Err(err!("The centred paragraph set {} lines, wanting at least two.", ls.len(); Test, Mismatch)); |
| 81 | } |
| 82 | for (i, (x0, x1, words)) in ls[..ls.len() - 1].iter().enumerate() { |
| 83 | if x0.abs() > TOL || (x1 - w).abs() > TOL { |
| 84 | return Err(err!("Justified line {} ({:?}) runs {}..{}pt of a {}pt measure.", i, words, x0, x1, w; Test, Mismatch)); |
| 85 | } |
| 86 | } |
| 87 | let (x0, x1, words) = &ls[ls.len() - 1]; |
| 88 | if *x0 < 1.0 || ((w - x1) - x0).abs() > TOL { |
| 89 | return Err(err!("The short last line ({:?}) runs {}..{}pt of a {}pt measure, not centred.", words, x0, x1, w; Test, Mismatch)); |
| 90 | } |
| 91 | Ok(()) |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn a_right_aligned_block_sets_every_ragged_line_at_the_right_edge() -> Outcome<()> { |
| 96 | let r = res!(render(&fmt!("#set par(justify: false)\n\n#align(right)[\n {}\n]\n", PARA))); |
| 97 | let ls = lines(&r); |
| 98 | let w = measure(&r); |
| 99 | if ls.len() < 2 { |
| 100 | return Err(err!("The right-aligned paragraph set {} lines, wanting at least two.", ls.len(); Test, Mismatch)); |
| 101 | } |
| 102 | for (x0, x1, words) in &ls { |
| 103 | if (x1 - w).abs() > TOL || *x0 < 1.0 { |
| 104 | return Err(err!("Line {:?} runs {}..{}pt of a {}pt measure, not flush right.", words, x0, x1, w; Test, Mismatch)); |
| 105 | } |
| 106 | } |
| 107 | Ok(()) |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn an_own_line_text_call_sets_its_body_bold_at_its_size() -> Outcome<()> { |
| 112 | let r = res!(render("#text(size: 26pt, weight: \"bold\")[Xylophone]\n\nAfter.\n")); |
| 113 | let page = res!(r.out.pages.first().ok_or_else(|| err!("No page was set."; Test, Missing))); |
| 114 | let mut found = false; |
| 115 | for p in &page.frame.placed { |
| 116 | if let PlacedKind::Text(t) = &p.kind { |
| 117 | if t.source().contains("Xylophone") { |
| 118 | found = true; |
| 119 | let info = res!(t.face_info()); |
| 120 | if (t.size() as f64 - 26.0).abs() > TOL || info.weight < 600 { |
| 121 | return Err(err!("Xylophone was set at {}pt in weight {}, not bold at 26pt.", t.size(), info.weight; Test, Mismatch)); |
| 122 | } |
| 123 | } |
| 124 | if t.source().contains("After") && (t.size() as f64 - 26.0).abs() < 1.0 { |
| 125 | return Err(err!("The text call's size leaked into the next paragraph."; Test, Mismatch)); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if !found { |
| 130 | return Err(err!("The text call's body was not set."; Test, Missing)); |
| 131 | } |
| 132 | Ok(()) |
| 133 | } |
| 134 | |
| 135 | /// How many marks the first page draws above its first heading, a chapter's display numeral among them. |
| 136 | fn marks_above_heading(r: &Rendered) -> usize { |
| 137 | let Some(page) = r.out.pages.first() else { return 0; }; |
| 138 | let head = page.frame.placed.iter().find_map(|p| match &p.kind { |
| 139 | PlacedKind::Text(t) if t.source() == "Alpha" => Some(p.y), |
| 140 | _ => None, |
| 141 | }); |
| 142 | match head { |
| 143 | Some(y) => page.frame.placed.iter().filter(|p| p.y < y && p.y >= r.geom.content_top()).count(), |
| 144 | None => 0, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn heading_numbering_none_draws_no_number() -> Outcome<()> { |
| 150 | let r = res!(render(&fmt!("#set heading(numbering: none)\n\n= Alpha\n\n{}\n\n== Beta\n\n{}\n", PARA, PARA))); |
| 151 | let n = marks_above_heading(&r); |
| 152 | if n != 0 { |
| 153 | return Err(err!("Under numbering: none the page still drew {} marks above the heading.", n; Test, Mismatch)); |
| 154 | } |
| 155 | Ok(()) |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn without_the_set_a_chapter_keeps_its_number() -> Outcome<()> { |
| 160 | let r = res!(render(&fmt!("= Alpha\n\n{}\n", PARA))); |
| 161 | if marks_above_heading(&r) == 0 { |
| 162 | return Err(err!("With no heading set the chapter drew no number."; Test, Missing)); |
| 163 | } |
| 164 | Ok(()) |
| 165 | } |
| 166 |