oxedyne/fe2o3/fe2o3_austenite/tests/vspace.rs
4.5 KiB, 1 run
created by r1870400018:71670, 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 | //! `#v(..)` on the reader path (`austenite` without `--eval`, so `--explain` too) keeps Typst's meaning: |
| 2 | //! a strong space adds its amount to the block spacing, an `em` resolving against the text size in force, |
| 3 | //! and a weak one displaces the block spacing and collapses at the start of the flow. Before 2026-10-11 an |
| 4 | //! `em` or weak `#v` was refused and skipped. |
| 5 | |
| 6 | use oxedyne_fe2o3_austenite::compile; |
| 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::HashMap; |
| 14 | use std::path::PathBuf; |
| 15 | use std::sync::{ |
| 16 | Arc, |
| 17 | Mutex, |
| 18 | MutexGuard, |
| 19 | }; |
| 20 | |
| 21 | // The source map is a process global, so the tests take turns. |
| 22 | static VFS_TURN: Mutex<()> = Mutex::new(()); |
| 23 | |
| 24 | fn turn() -> MutexGuard<'static, ()> { |
| 25 | VFS_TURN.lock().unwrap_or_else(|p| p.into_inner()) |
| 26 | } |
| 27 | |
| 28 | const TOL: f64 = 0.05; // points, rounding slack only |
| 29 | const HEAD: &str = "#set text(size: 11pt)\n#set par(spacing: 1.2em)\n"; |
| 30 | const HEAD_14: &str = "#set text(size: 14pt)\n#set par(spacing: 1.2em)\n"; |
| 31 | const SKIP: f64 = 13.2; // the 1.2em block spacing at 11pt |
| 32 | |
| 33 | /// The distinct baselines, in points, on the first page of [`HEAD`] + `body` compiled through the reader. |
| 34 | fn baselines(body: &str) -> Outcome<Vec<f64>> { |
| 35 | baselines_under(HEAD, body) |
| 36 | } |
| 37 | |
| 38 | fn baselines_under(head: &str, body: &str) -> Outcome<Vec<f64>> { |
| 39 | let _turn = turn(); |
| 40 | let src = fmt!("{}\n{}\n", head, body); |
| 41 | let mut map: HashMap<PathBuf, Vec<u8>> = HashMap::new(); |
| 42 | map.insert(PathBuf::from("/vspace/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("/vspace/main.typ"), || Ok(fonts.clone()))); |
| 47 | if let Some(line) = assembled.refusals.report() { |
| 48 | return Err(err!("The reader refused part of the source: {}", line; Invalid, Input)); |
| 49 | } |
| 50 | let rendered = res!(compile::author_and_run(assembled)); |
| 51 | let mut bases: Vec<i32> = Vec::new(); |
| 52 | for page in rendered.out.pages.iter().take(1) { |
| 53 | for placed in &page.frame.placed { |
| 54 | if let PlacedKind::Text(_) = &placed.kind { |
| 55 | let base = placed.y.raw() + placed.dims.height.raw(); |
| 56 | if !bases.contains(&base) { |
| 57 | bases.push(base); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | Ok(bases) |
| 63 | }; |
| 64 | let out = run(); |
| 65 | res!(vfs::clear()); |
| 66 | let mut bases = res!(out); |
| 67 | bases.sort(); |
| 68 | Ok(bases.into_iter().map(|b| b as f64 / 65536.0).collect()) |
| 69 | } |
| 70 | |
| 71 | /// The baseline gap between two one-line paragraphs with `between` set on its own line between them. |
| 72 | fn gap(between: &str) -> Outcome<f64> { |
| 73 | gap_under(HEAD, between) |
| 74 | } |
| 75 | |
| 76 | fn gap_under(head: &str, between: &str) -> Outcome<f64> { |
| 77 | let bases = res!(baselines_under(head, &fmt!("First paragraph.\n\n{}\n\nSecond paragraph.", between))); |
| 78 | // A folio may follow at the page foot; the two paragraphs are the first two baselines. |
| 79 | if bases.len() < 2 { |
| 80 | return Err(err!("Expected two body baselines, found {:?}.", bases; Invalid, Output)); |
| 81 | } |
| 82 | Ok(bases[1] - bases[0]) |
| 83 | } |
| 84 | |
| 85 | fn near(got: f64, want: f64, what: &str) -> Outcome<()> { |
| 86 | if (got - want).abs() > TOL { |
| 87 | return Err(err!("{}: {:.3}pt, expected {:.3}pt.", what, got, want; Invalid, Output)); |
| 88 | } |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn an_em_space_adds_one_em_of_the_text_size() -> Outcome<()> { |
| 94 | let plain = res!(gap("")); |
| 95 | let spaced = res!(gap("#v(1em)")); |
| 96 | near(spaced - plain, 11.0, "#v(1em) at 11pt") |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn a_point_space_adds_its_points() -> Outcome<()> { |
| 101 | let plain = res!(gap("")); |
| 102 | let spaced = res!(gap("#v(12pt)")); |
| 103 | near(spaced - plain, 12.0, "#v(12pt)") |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn an_em_space_follows_the_text_size() -> Outcome<()> { |
| 108 | let plain = res!(gap_under(HEAD_14, "")); |
| 109 | let spaced = res!(gap_under(HEAD_14, "#v(1.5em)")); |
| 110 | near(spaced - plain, 21.0, "#v(1.5em) at 14pt") |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn a_weak_space_displaces_the_block_spacing() -> Outcome<()> { |
| 115 | let plain = res!(gap("")); |
| 116 | let weak = res!(gap("#v(30pt, weak: true)")); |
| 117 | res!(near(weak - plain, 30.0 - SKIP, "#v(30pt, weak: true)")); |
| 118 | // Two weak spaces with nothing between collapse to the larger. |
| 119 | let both = res!(gap("#v(20pt, weak: true)\n#v(30pt, weak: true)")); |
| 120 | near(both - plain, 30.0 - SKIP, "two weak spaces") |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn a_weak_space_collapses_at_the_start_of_the_flow() -> Outcome<()> { |
| 125 | let plain = res!(baselines("First paragraph.")); |
| 126 | let weak = res!(baselines("#v(30pt, weak: true)\n\nFirst paragraph.")); |
| 127 | if plain.is_empty() || weak.is_empty() { |
| 128 | return Err(err!("A first baseline is missing."; Invalid, Output)); |
| 129 | } |
| 130 | near(weak[0], plain[0], "a weak space at the flow's start") |
| 131 | } |