oxedyne/fe2o3/fe2o3_austenite/tests/pdf_outline_eval.rs
3.2 KiB, 1 run
created by r1870400018:66216, 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 | //! The bookmarks of a PDF written through the evaluator are Typst's: a numbered heading is titled with the |
| 2 | //! number it shows, a space and its body, as Typst titles it, and an unnumbered one with its body alone. The |
| 3 | //! expectation is Typst's own PDF of the same source, read back with the same reader. |
| 4 | |
| 5 | #![allow(dead_code)] |
| 6 | |
| 7 | #[path = "eval_oracle/mod.rs"] |
| 8 | mod harness; |
| 9 | |
| 10 | use harness::oracle::Oracle; |
| 11 | use harness::pdf::{ |
| 12 | austenite_pdf, |
| 13 | typst_pdf, |
| 14 | work_dir, |
| 15 | }; |
| 16 | |
| 17 | use oxedyne_fe2o3_core::prelude::*; |
| 18 | |
| 19 | use std::path::Path; |
| 20 | use std::process::Command; |
| 21 | |
| 22 | // Every number pattern and function a heading can show: a pattern whose last piece repeats, a numbering of |
| 23 | // no text, an empty one, one that is only a space, a heading left out of the outline, one left out of the |
| 24 | // bookmarks only, and a counter that is moved. |
| 25 | const SOURCE: &str = r#"#set page(width: 200pt, height: 80pt, margin: 10pt) |
| 26 | #set heading(numbering: "1.1") |
| 27 | = One |
| 28 | == Under *bold* one |
| 29 | #heading(numbering: none)[Plain] |
| 30 | #heading(level: 2, numbering: "A)")[Lettered] |
| 31 | #set heading(numbering: "I.") |
| 32 | = Roman |
| 33 | == Sub |
| 34 | #set heading(numbering: (..n) => [Part #n.pos().map(str).join("-")]) |
| 35 | = Func |
| 36 | == Func sub |
| 37 | #set heading(numbering: "1.") |
| 38 | #counter(heading).update(10) |
| 39 | = Ten |
| 40 | #heading(outlined: false)[Hidden] |
| 41 | #heading(bookmarked: false)[NoMark] |
| 42 | #heading(numbering: " 1 ")[Spaced] |
| 43 | #heading(numbering: "(1)", supplement: [Sec])[Paren] |
| 44 | #heading(numbering: (..n) => none)[NoneFn] |
| 45 | #heading(numbering: (..n) => [])[EmptyFn] |
| 46 | #heading(numbering: (..n) => [#h(1em)x])[SpaceFn] |
| 47 | "#; |
| 48 | |
| 49 | // The outline as a JSON list of [level, title], read by pikepdf; None where the tool is not here. |
| 50 | fn titles(pdf: &Path) -> Option<String> { |
| 51 | let script = "import json, sys, pikepdf\n\ |
| 52 | pdf = pikepdf.open(sys.argv[1])\n\ |
| 53 | out = []\n\ |
| 54 | def walk(items, level):\n\ |
| 55 | \tfor i in items:\n\ |
| 56 | \t\tout.append([level, str(i.title)])\n\ |
| 57 | \t\twalk(i.children, level + 1)\n\ |
| 58 | walk(pdf.open_outline().root, 1)\n\ |
| 59 | print(json.dumps(out))\n"; |
| 60 | match Command::new("python3").arg("-c").arg(script).arg(pdf).output() { |
| 61 | Ok(o) if o.status.success() => Some(String::from_utf8_lossy(&o.stdout).trim().to_string()), |
| 62 | _ => None, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn bookmarks_are_titled_with_the_number_a_heading_shows_as_typst_titles_them() -> Outcome<()> { |
| 68 | if res!(Oracle::find()).is_none() { |
| 69 | return Ok(()); |
| 70 | } |
| 71 | let dir = res!(work_dir("pdf-outline-eval")); |
| 72 | let src = dir.join("main.typ"); |
| 73 | res!(std::fs::write(&src, SOURCE)); |
| 74 | let typst_out = dir.join("typst.pdf"); |
| 75 | res!(typst_pdf(&src, &typst_out)); |
| 76 | let made = res!(austenite_pdf(&src)); |
| 77 | let ours = dir.join("austenite.pdf"); |
| 78 | res!(std::fs::write(&ours, &made.bytes)); |
| 79 | let (want, got) = match (titles(&typst_out), titles(&ours)) { |
| 80 | (Some(w), Some(g)) => (w, g), |
| 81 | _ => { |
| 82 | println!("[pdf-outline] SKIPPED: python3 with pikepdf is not here"); |
| 83 | return Ok(()); |
| 84 | } |
| 85 | }; |
| 86 | // Fixed by construction, so a comparison of two empty outlines cannot pass. |
| 87 | assert!(want.starts_with(r#"[[1, "1 One"], [2, "1.1 Under bold one"], [1, "Plain"]"#), "Typst's outline: {}", want); |
| 88 | assert!(want.contains(r#"[1, "11. Ten"]"#) && want.contains(r#"[1, " NoneFn"]"#), "Typst's outline: {}", want); |
| 89 | assert!(!want.contains("Hidden") && !want.contains("NoMark"), "Typst's outline: {}", want); |
| 90 | assert_eq!(got, want, "the bookmarks differ from Typst's"); |
| 91 | Ok(()) |
| 92 | } |