Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_ink_native.rs

5.2 KiB, 3 runs

created by r1870400018:67943, 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//! A page drawn through the evaluator reaches the PDF writer with each colour in the space it was written
2//! in. In [`ColourOut::Native`] the file says so: `g` and `G` for a `luma`, `k` and `K` for a `cmyk`, `rg`
3//! and `RG` for the rest, in the text, a fill and a stroke alike. In [`ColourOut::Rgb`], the mode every
4//! writer has by default and the bytes a file carried before an ink knew its space, there is no operator but
5//! `rg` and `RG`.
6
7use oxedyne_fe2o3_austenite::compile;
8use oxedyne_fe2o3_austenite::emit::pdf::render_page;
9use oxedyne_fe2o3_austenite::eval::fixpoint::PageSink;
10use oxedyne_fe2o3_austenite::eval::intro::Introspector;
11use oxedyne_fe2o3_austenite::eval::Engine;
12use oxedyne_fe2o3_austenite::flow::text::FontStore;
13use oxedyne_fe2o3_austenite::page::Page;
14
15use oxedyne_fe2o3_core::prelude::*;
16use oxedyne_fe2o3_graphics::pdf::{
17 ColourOut,
18 PdfPage,
19 PdfWriter,
20};
21
22use std::path::{
23 Path,
24 PathBuf,
25};
26
27// Every surface has its own value in each space, so a colour found in the file is known by where it came from.
28const SRC: &str = "\
29#set page(width: 240pt, height: 240pt, margin: 12pt)
30Plain default text.
31
32#text(fill: luma(25%))[grey text] #text(fill: cmyk(100%, 0%, 0%, 0%))[cmyk text] #text(fill: rgb(\"#00ff00\"))[rgb text]
33
34#rect(width: 40pt, height: 12pt, fill: luma(50%))
35#rect(width: 40pt, height: 12pt, fill: cmyk(0%, 100%, 100%, 0%))
36#rect(width: 40pt, height: 12pt, fill: rgb(\"#ff0000\"))
37
38#grid(columns: (30pt,), stroke: 1pt + luma(75%), [grey ruled])
39#grid(columns: (30pt,), stroke: 1pt + cmyk(0%, 0%, 100%, 0%), [cmyk ruled])
40#grid(columns: (30pt,), stroke: 1pt + rgb(\"#0000ff\"), [rgb ruled])
41";
42
43/// Collects the pages and writes them with the colour mode under test, uncompressed.
44struct ModeSink {
45 pages: Vec<PdfPage>,
46 mode: ColourOut,
47 out: Option<Vec<u8>>,
48}
49
50impl PageSink for ModeSink {
51 fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> {
52 self.pages.push(res!(render_page(&page)));
53 Ok(())
54 }
55
56 fn discard_pass(&mut self) -> Outcome<()> {
57 self.pages.clear();
58 self.out = None;
59 Ok(())
60 }
61
62 fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> {
63 let mut w = PdfWriter::new().with_colour_out(self.mode.clone());
64 for p in self.pages.drain(..) {
65 w.add_page(p);
66 }
67 self.out = Some(res!(w.to_bytes()));
68 Ok(())
69 }
70}
71
72/// The content streams of a file written uncompressed, joined: every stream that is text, which leaves out
73/// the binary font programmes.
74fn content_of(pdf: &[u8]) -> String {
75 let (open, close) = (&b"stream\n"[..], &b"\nendstream"[..]);
76 let mut text = String::new();
77 let mut at = 0;
78 while let Some(i) = pdf[at..].windows(open.len()).position(|w| w == open) {
79 let from = at + i + open.len();
80 let len = match pdf[from..].windows(close.len()).position(|w| w == close) {
81 Some(n) => n,
82 None => break,
83 };
84 if let Ok(s) = std::str::from_utf8(&pdf[from..from + len]) {
85 text.push_str(s);
86 text.push('\n');
87 }
88 at = from + len + close.len();
89 }
90 text
91}
92
93fn written(name: &str, mode: ColourOut) -> Outcome<String> {
94 let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("eval-ink-native").join(name);
95 res!(std::fs::create_dir_all(&dir));
96 res!(std::fs::write(dir.join("main.typ"), SRC));
97 let main: PathBuf = dir.join("main.typ");
98 let mut sink = ModeSink { pages: Vec::new(), mode, out: None };
99 let done = res!(compile::assemble_eval(&main, &dir, FontStore::default(), &mut sink));
100 res!(done.laid.as_ref().map_err(|e| err!("{} did not lay out: {}", name, e.plain(); Test)));
101 let pdf = res!(sink.out.ok_or_else(|| err!("{}: no PDF", name; Test)));
102 Ok(content_of(&pdf))
103}
104
105fn expect(content: &str, want: &[&str], absent: &[&str], mode: &str) -> Outcome<()> {
106 for w in want {
107 if !content.contains(w) {
108 return Err(err!("{} mode: the file lacks {:?}:\n{}", mode, w, content; Test));
109 }
110 }
111 for a in absent {
112 if content.contains(a) {
113 return Err(err!("{} mode: the file holds {:?}", mode, a; Test));
114 }
115 }
116 Ok(())
117}
118
119#[test]
120fn native_mode_writes_each_surface_in_its_own_space() -> Outcome<()> {
121 let c = res!(written("native", ColourOut::Native));
122 expect(&c, &[
123 // Default text is Typst's black, a grey.
124 "0 g\n",
125 // Text colour, one space each.
126 "0.25 g\n", "1 0 0 0 k\n", "0 1 0 rg\n",
127 // Fills.
128 "0.5 g\n", "0 1 1 0 k\n", "1 0 0 rg\n",
129 // Strokes.
130 "0.75 G\n", "0 0 1 0 K\n", "0 0 1 RG\n",
131 ], &[], "native")
132}
133
134#[test]
135fn rgb_mode_lowers_every_surface_to_rg() -> Outcome<()> {
136 let c = res!(written("rgb", ColourOut::Rgb));
137 expect(&c, &[
138 "0 0 0 rg\n", "0 1 0 rg\n", "1 0 0 rg\n", "0 0 1 RG\n",
139 ], &[" g\n", " k\n", " G\n", " K\n"], "rgb")
140}
141
142/// The two modes draw one page: they differ in the colour operators and in nothing else.
143#[test]
144fn the_two_modes_differ_only_in_their_colour_operators() -> Outcome<()> {
145 let native = res!(written("native-twin", ColourOut::Native));
146 let rgb = res!(written("rgb-twin", ColourOut::Rgb));
147 let strip = |s: &str| -> Vec<String> {
148 s.lines().filter(|l| {
149 let t = l.trim_end();
150 !(t.ends_with(" g") || t.ends_with(" k") || t.ends_with(" G") || t.ends_with(" K")
151 || t.ends_with(" rg") || t.ends_with(" RG"))
152 }).map(|l| l.to_string()).collect()
153 };
154 if strip(&native) != strip(&rgb) {
155 return Err(err!("the modes differ beyond their colour operators"; Test));
156 }
157 Ok(())
158}