oxedyne/fe2o3/fe2o3_austenite/tests/eval_math_fonts.rs
6.9 KiB, 1 run
created by r1870400018:63602, 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 | //! Maths set in a face the host supplies. An equation's face is found through the compilation's font store, the |
| 2 | //! way the text around it is, so a family given by `--font-path` or `add_bytes` is found by |
| 3 | //! `show math.equation: set text(font: ..)`, and the embedded New Computer Modern Math is the fallback. Each |
| 4 | //! synthetic document is compiled through the evaluator to its own PDF and by Typst 0.15.1 with the same |
| 5 | //! `--font-path`; the lines of the two files (text without its spaces, box bottom, first word) and the fonts |
| 6 | //! they embed are compared. The faces are two the font crate carries for its own tests: DejaVu Sans, which has |
| 7 | //! a MATH table, and Noto Sans, which has none. |
| 8 | |
| 9 | #![allow(dead_code)] |
| 10 | |
| 11 | #[path = "eval_oracle/mod.rs"] |
| 12 | mod harness; |
| 13 | |
| 14 | use harness::oracle::Oracle; |
| 15 | use harness::pdf::{ |
| 16 | pdf_compare, |
| 17 | work_dir, |
| 18 | }; |
| 19 | |
| 20 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 21 | use oxedyne_fe2o3_austenite::fonts::FaceVariant; |
| 22 | use oxedyne_fe2o3_austenite::math::font; |
| 23 | use oxedyne_fe2o3_core::prelude::*; |
| 24 | |
| 25 | use std::path::{ |
| 26 | Path, |
| 27 | PathBuf, |
| 28 | }; |
| 29 | |
| 30 | const DOCUMENTS: [(&str, &str); 5] = [ |
| 31 | // The equation's own show-set names the family. |
| 32 | ("show_set", r#" |
| 33 | #show math.equation: set text(font: "DejaVu Sans") |
| 34 | Euler: $e^(i pi) + 1 = 0$, and a block: |
| 35 | $ sum_(k=1)^n k = (n (n + 1)) / 2 $ |
| 36 | $ sqrt(x^2 + y^2) quad lr(( a / b )) quad lim_(x -> 0) $ |
| 37 | $ integral_0^1 f(x) dif x = lim_(n -> oo) sum_(i=1)^n f(i / n) 1 / n $ |
| 38 | "#), |
| 39 | // A list whose first family is nowhere: the second is found. |
| 40 | ("family_list", r#" |
| 41 | #show math.equation: set text(font: ("No Such Math", "DejaVu Sans")) |
| 42 | $ x^2 + y^2 = z^2 $ |
| 43 | $ mat(1, 2; 3, 4) vec(a, b) $ |
| 44 | "#), |
| 45 | // Only block equations take the host face; an inline one stays in the embedded face. |
| 46 | ("block_only", r#" |
| 47 | #show math.equation.where(block: true): set text(font: "DejaVu Sans") |
| 48 | An inline $a^2 + b^2$ in the default face. |
| 49 | $ a^2 + b^2 = c^2 $ |
| 50 | "#), |
| 51 | // A face that is nowhere: the embedded maths face, the head of Typst's maths fallback list. |
| 52 | ("absent_family", r#" |
| 53 | #show math.equation: set text(font: "No Such Math") |
| 54 | $ a^2 + sqrt(b) $ |
| 55 | $ c^2 + d / e $ |
| 56 | "#), |
| 57 | // A face switched inside one equation. |
| 58 | ("inner_text", r#" |
| 59 | $ a + text(font: "DejaVu Sans", b^2) + c $ |
| 60 | "#), |
| 61 | ]; |
| 62 | |
| 63 | // The font crate's own faces: DejaVu Sans carries a (small) MATH table, Noto Sans none. |
| 64 | fn crate_font(name: &str) -> PathBuf { |
| 65 | Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("fe2o3_font").join("fonts").join(name) |
| 66 | } |
| 67 | |
| 68 | // Does the face name the host maths face, DejaVu Sans, not the mono sibling the crate embeds? |
| 69 | fn is_host(name: &str) -> bool { |
| 70 | name.starts_with("DejaVuSans") && !name.contains("Mono") |
| 71 | } |
| 72 | |
| 73 | // A directory holding just `file`, so `--font-path` offers Typst exactly the face the store is given. |
| 74 | fn only(dir: &Path, name: &str, file: &Path) -> Outcome<PathBuf> { |
| 75 | let d = dir.join(name); |
| 76 | res!(std::fs::create_dir_all(&d)); |
| 77 | let target = d.join(file.file_name().unwrap_or_default()); |
| 78 | res!(std::fs::copy(file, &target)); |
| 79 | Ok(d) |
| 80 | } |
| 81 | |
| 82 | fn written(dir: &Path, name: &str, src: &str) -> Outcome<PathBuf> { |
| 83 | let p = dir.join(fmt!("{}.typ", name)); |
| 84 | res!(std::fs::write(&p, src)); |
| 85 | Ok(p) |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn a_family_the_host_supplies_sets_the_equation() -> Outcome<()> { |
| 90 | if res!(Oracle::find()).is_none() { |
| 91 | return Ok(()); |
| 92 | } |
| 93 | let root = res!(work_dir("eval-math-fonts")); |
| 94 | let fonts = res!(only(&root, "host_math", &crate_font("DejaVuSans.ttf"))); |
| 95 | let mut failures = Vec::new(); |
| 96 | for (name, src) in DOCUMENTS { |
| 97 | let typ = res!(written(&root, name, src)); |
| 98 | let found = res!(pdf_compare(&typ, &root.join(name), Some(&fonts), true)); |
| 99 | let both = |names: &[String]| names.iter().any(|n| is_host(n)); |
| 100 | // Only the documents that name the host face set it; the others fall back to the embedded one. |
| 101 | let host = !matches!(name, "absent_family"); |
| 102 | if !found.differences.is_empty() { |
| 103 | failures.push(fmt!("{}: {}", name, found.differences.join("; "))); |
| 104 | } |
| 105 | if host && !(both(&found.theirs) && both(&found.ours)) { |
| 106 | failures.push(fmt!("{}: the host face is not in both files: typst {:?}, austenite {:?}", name, found.theirs, found.ours)); |
| 107 | } |
| 108 | if !host && both(&found.ours) { |
| 109 | failures.push(fmt!("{}: austenite embeds the host face for a document that does not name it: {:?}", name, found.ours)); |
| 110 | } |
| 111 | println!("{:14} typst {:?} / austenite {:?}", name, found.theirs, found.ours); |
| 112 | } |
| 113 | for f in &failures { |
| 114 | println!("FAIL {}", f); |
| 115 | } |
| 116 | assert!(failures.is_empty(), "{} document(s) differ from typst", failures.len()); |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn a_text_face_with_no_maths_table_sets_the_equation_with_a_warning() -> Outcome<()> { |
| 122 | if res!(Oracle::find()).is_none() { |
| 123 | return Ok(()); |
| 124 | } |
| 125 | let root = res!(work_dir("eval-math-fonts")); |
| 126 | let fonts = res!(only(&root, "host_text", &crate_font("NotoSans-Regular.ttf"))); |
| 127 | let typ = res!(written(&root, "no_math_table", r#" |
| 128 | #show math.equation: set text(font: "Noto Sans") |
| 129 | $ x^2 + 1 / (y + 1) = sqrt(z) $ |
| 130 | "#)); |
| 131 | let found = res!(pdf_compare(&typ, &root.join("no_math_table"), Some(&fonts), true)); |
| 132 | println!("typst {:?} / austenite {:?}", found.theirs, found.ours); |
| 133 | assert!(found.differences.is_empty(), "differs from typst: {}", found.differences.join("; ")); |
| 134 | let warned = found.report.diagnostics.iter().any(|d| d.message.contains("not designed for math") |
| 135 | && d.kind == oxedyne_fe2o3_austenite::diag::DiagnosticKind::Lint); |
| 136 | assert!(warned, "no `current font is not designed for math` lint: {:?}", found.report.diagnostics); |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn the_store_supplies_the_maths_face_and_the_embedded_one_is_the_fallback() -> Outcome<()> { |
| 142 | let file = crate_font("DejaVuSans.ttf"); |
| 143 | let bytes = res!(std::fs::read(&file)); |
| 144 | let family = res!(oxedyne_fe2o3_austenite::fonts::declared_family(&bytes)); |
| 145 | let key = family.to_lowercase(); |
| 146 | let want = FaceVariant::default(); |
| 147 | let names = vec![key.clone()]; |
| 148 | |
| 149 | // Without the file the store holds no such family, so the embedded face answers. |
| 150 | let mut bare = FontStore::default(); |
| 151 | let book = res!(bare.book()); |
| 152 | let got = res!(font::resolve(&book, &names, true, want)); |
| 153 | assert_eq!(got.family, "New Computer Modern Math", "the embedded face is the fallback"); |
| 154 | assert!(res!(font::select(&book, &key, want)).is_none(), "{} is not in a bare store", family); |
| 155 | |
| 156 | // With the file added as bytes, the family is found, and its own MATH table drives the layout. |
| 157 | let mut host = FontStore::default(); |
| 158 | host.add_bytes(bytes); |
| 159 | let book = res!(host.book()); |
| 160 | let got = res!(font::resolve(&book, &names, true, want)); |
| 161 | assert_eq!(got.family, family, "the host's family is found through the store"); |
| 162 | assert!(got.has_math, "{} carries a MATH table", family); |
| 163 | assert_eq!(res!(font::chain(&book, &names, true, want)).len() > 1, true, "the fallback faces follow the host's"); |
| 164 | let again = res!(font::select(&book, &key, want)); |
| 165 | assert!(again.map(|a| std::sync::Arc::ptr_eq(&a, &got)).unwrap_or(false), "a face is parsed once and shared"); |
| 166 | |
| 167 | // A family the store lacks falls through the list to the one it has. |
| 168 | let list = vec!["no such math".to_string(), key]; |
| 169 | assert_eq!(res!(font::resolve(&book, &list, true, want)).family, family); |
| 170 | Ok(()) |
| 171 | } |