Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / pdf / src / main / rust / src / reference_diff_tests_part4.rs

reference_diff_tests_part4.rs357 lines · 14.0 KiBRaw
1/// A `/Decode` array that inverts a grayscale image (§8.9.5.2, Table 89).
2#[test]
3#[ignore]
4fn refdiff_image_decode_array_inversion() {
5 let mut doc = Document::with_version("1.7");
6 let gray = vec![0u8, 64, 128, 255];
7 let img = doc.add_object(Stream::new(
8 dictionary! {
9 "Type" => "XObject", "Subtype" => "Image",
10 "Width" => 4, "Height" => 1,
11 "ColorSpace" => Object::Name(b"DeviceGray".to_vec()),
12 "BitsPerComponent" => 8,
13 "Decode" => vec![Object::Real(1.0), Object::Real(0.0)],
14 },
15 gray,
16 ));
17 let ops = vec![
18 op("q", vec![]),
19 op("cm", vec![n(160.0), n(0.0), n(0.0), n(120.0), n(20.0), n(40.0)]),
20 op("Do", vec![Object::Name(b"Im".to_vec())]),
21 op("Q", vec![]),
22 ];
23 let res = dictionary! { "XObject" => dictionary! { "Im" => Object::Reference(img) } };
24 let content = Content { operations: ops }.encode().expect("encode");
25 let _ = assemble(&mut doc, content, res, dictionary! {});
26 let mut bytes = Vec::new();
27 doc.save_to(&mut bytes).expect("save");
28
29 let r = compare_page("image_decode", &bytes);
30 for (i, want) in [255.0f64, 191.0, 127.0, 0.0].iter().enumerate() {
31 let x = 25.0 + i as f64 * 40.0;
32 let rect = [x, 50.0, x + 30.0, 150.0];
33 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
34 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
35 println!(" sample {i}: ours={:.1} theirs={:.1} spec≈{want:.0}", a[0], b[0]);
36 assert!((a[0] - b[0]).abs() <= 4.0, "sample {i}: ours {:.1} vs hayro {:.1}", a[0], b[0]);
37 }
38}
39
40/// Form XObject with a non-identity `/Matrix` and a `/BBox` that must clip
41/// (§8.10.2, Table 95). Getting the Matrix/BBox composition order wrong is the
42/// canonical self-consistent mistake here: the content still appears, just in
43/// the wrong place or unclipped.
44#[test]
45#[ignore]
46fn refdiff_form_xobject_matrix_and_bbox_clip() {
47 let mut doc = Document::with_version("1.7");
48 let inner = Content {
49 operations: vec![
50 op("rg", vec![n(0.8), n(0.2), n(0.6)]),
51 op("re", vec![n(-50.0), n(-50.0), n(200.0), n(200.0)]),
52 op("f", vec![]),
53 ],
54 }
55 .encode()
56 .expect("encode inner");
57 let form = doc.add_object(Stream::new(
58 dictionary! {
59 "Type" => "XObject", "Subtype" => "Form",
60 // BBox must clip the oversized rect to 0..100 in form space.
61 "BBox" => vec![Object::Real(0.0), Object::Real(0.0), Object::Real(100.0), Object::Real(100.0)],
62 // Scale by 1.5 and translate: applied BEFORE the CTM.
63 "Matrix" => vec![
64 Object::Real(1.5), Object::Real(0.0), Object::Real(0.0),
65 Object::Real(1.5), Object::Real(10.0), Object::Real(20.0),
66 ],
67 "Resources" => dictionary! {},
68 },
69 inner,
70 ));
71 let ops = vec![
72 op("q", vec![]),
73 op("cm", vec![n(1.0), n(0.0), n(0.0), n(1.0), n(15.0), n(10.0)]),
74 op("Do", vec![Object::Name(b"Fm".to_vec())]),
75 op("Q", vec![]),
76 ];
77 let res = dictionary! { "XObject" => dictionary! { "Fm" => Object::Reference(form) } };
78 let content = Content { operations: ops }.encode().expect("encode");
79 let _ = assemble(&mut doc, content, res, dictionary! {});
80 let mut bytes = Vec::new();
81 doc.save_to(&mut bytes).expect("save");
82 let _ = compare_page("form_matrix_bbox", &bytes);
83}
84
85/// Nested clipping with both winding rules, and the `W n` deferral rule
86/// (§8.5.4: the clip takes effect only after the path-painting operator).
87#[test]
88#[ignore]
89fn refdiff_nested_and_evenodd_clipping() {
90 let ops = vec![
91 op("q", vec![]),
92 op("re", vec![n(20.0), n(20.0), n(120.0), n(120.0)]),
93 op("W", vec![]),
94 op("n", vec![]),
95 op("re", vec![n(60.0), n(60.0), n(120.0), n(120.0)]),
96 op("W", vec![]),
97 op("n", vec![]),
98 op("rg", vec![n(0.1), n(0.5), n(0.9)]),
99 op("re", vec![n(0.0), n(0.0), n(200.0), n(200.0)]),
100 op("f", vec![]),
101 op("Q", vec![]),
102 // Even-odd clip: an annulus made of two nested rects.
103 op("q", vec![]),
104 op("re", vec![n(150.0), n(10.0), n(45.0), n(45.0)]),
105 op("re", vec![n(160.0), n(20.0), n(25.0), n(25.0)]),
106 op("W*", vec![]),
107 op("n", vec![]),
108 op("g", vec![n(0.0)]),
109 op("re", vec![n(0.0), n(0.0), n(200.0), n(200.0)]),
110 op("f", vec![]),
111 op("Q", vec![]),
112 ];
113 let _ = compare_page("clipping", &pdf_bytes(ops, dictionary! {}));
114}
115
116/// Type 3 font text: the only way to compare TEXT POSITIONING pixel-for-pixel
117/// between two renderers without shipping a font file, because the glyphs are
118/// content streams both must draw identically. Exercises `/FontMatrix`,
119/// `/Widths`, `Tf`, `Td`, `TL`/`T*`, `Tc`, `Tz` and `TJ` kerning (§9.4.4).
120///
121/// **FIXED.** See [`refdiff_root_cause_d0_mangles_the_next_operator`] for the
122/// isolated repro. In short: every Type 3 glyph description must begin
123/// with `d0` or `d1` (§9.6.5), lopdf mis-tokenises those, and the damage lands
124/// on the glyph's first painting operator. The square glyph in this fixture
125/// disappeared entirely; the triangle glyph was drawn from the wrong start point.
126#[test]
127#[ignore]
128fn refdiff_type3_font_text_positioning() {
129 let mut doc = Document::with_version("1.7");
130 // Glyph "a": a filled square in the left 700/1000 of the em.
131 let ga = doc.add_object(Stream::new(
132 dictionary! {},
133 Content {
134 operations: vec![
135 op("d0", vec![n(700.0), n(0.0)]),
136 op("re", vec![n(50.0), n(0.0), n(600.0), n(600.0)]),
137 op("f", vec![]),
138 ],
139 }
140 .encode()
141 .expect("glyph a"),
142 ));
143 // Glyph "b": a filled triangle, narrower.
144 let gb = doc.add_object(Stream::new(
145 dictionary! {},
146 Content {
147 operations: vec![
148 op("d0", vec![n(400.0), n(0.0)]),
149 op("m", vec![n(30.0), n(0.0)]),
150 op("l", vec![n(370.0), n(0.0)]),
151 op("l", vec![n(200.0), n(700.0)]),
152 op("h", vec![]),
153 op("f", vec![]),
154 ],
155 }
156 .encode()
157 .expect("glyph b"),
158 ));
159 let font = doc.add_object(dictionary! {
160 "Type" => "Font",
161 "Subtype" => "Type3",
162 "FontBBox" => vec![0.into(), 0.into(), 750.into(), 750.into()],
163 "FontMatrix" => vec![
164 Object::Real(0.001), Object::Real(0.0), Object::Real(0.0),
165 Object::Real(0.001), Object::Real(0.0), Object::Real(0.0),
166 ],
167 "CharProcs" => dictionary! { "ga" => Object::Reference(ga), "gb" => Object::Reference(gb) },
168 "Encoding" => dictionary! {
169 "Type" => "Encoding",
170 "Differences" => vec![
171 Object::Integer(97),
172 Object::Name(b"ga".to_vec()),
173 Object::Name(b"gb".to_vec()),
174 ],
175 },
176 "FirstChar" => 97,
177 "LastChar" => 98,
178 "Widths" => vec![Object::Integer(700), Object::Integer(400)],
179 "Resources" => dictionary! {},
180 });
181 let ops = vec![
182 op("BT", vec![]),
183 op("rg", vec![n(0.0), n(0.0), n(0.0)]),
184 op("Tf", vec![Object::Name(b"T3".to_vec()), n(24.0)]),
185 op("TL", vec![n(34.0)]),
186 op("Td", vec![n(15.0), n(160.0)]),
187 op("Tj", vec![Object::string_literal("abab")]),
188 op("T*", vec![]),
189 op("Tc", vec![n(6.0)]),
190 op("Tj", vec![Object::string_literal("abab")]),
191 op("T*", vec![]),
192 op("Tc", vec![n(0.0)]),
193 op("Tz", vec![n(160.0)]),
194 op("Tj", vec![Object::string_literal("aab")]),
195 op("T*", vec![]),
196 op("Tz", vec![n(100.0)]),
197 op(
198 "TJ",
199 vec![Object::Array(vec![
200 Object::string_literal("ab"),
201 Object::Integer(-500),
202 Object::string_literal("ab"),
203 ])],
204 ),
205 op("ET", vec![]),
206 ];
207 let res = dictionary! { "Font" => dictionary! { "T3" => Object::Reference(font) } };
208 let content = Content { operations: ops }.encode().expect("encode");
209 let _ = assemble(&mut doc, content, res, dictionary! {});
210 let mut bytes = Vec::new();
211 doc.save_to(&mut bytes).expect("save");
212 let _ = compare_page("type3_text", &bytes);
213}
214
215/// Constant alpha (`/ca`) with overlapping fills — §11.6.4.4. The compositing
216/// arithmetic is a place where "close enough" hides a systematic error.
217#[test]
218#[ignore]
219fn refdiff_constant_alpha_compositing() {
220 let mut doc = Document::with_version("1.7");
221 let gs = doc.add_object(dictionary! { "Type" => "ExtGState", "ca" => Object::Real(0.5) });
222 let ops = vec![
223 op("rg", vec![n(1.0), n(0.0), n(0.0)]),
224 op("re", vec![n(20.0), n(60.0), n(100.0), n(80.0)]),
225 op("f", vec![]),
226 op("gs", vec![Object::Name(b"G".to_vec())]),
227 op("rg", vec![n(0.0), n(0.0), n(1.0)]),
228 op("re", vec![n(80.0), n(60.0), n(100.0), n(80.0)]),
229 op("f", vec![]),
230 ];
231 let res = dictionary! { "ExtGState" => dictionary! { "G" => Object::Reference(gs) } };
232 let content = Content { operations: ops }.encode().expect("encode");
233 let _ = assemble(&mut doc, content, res, dictionary! {});
234 let mut bytes = Vec::new();
235 doc.save_to(&mut bytes).expect("save");
236
237 let r = compare_page("constant_alpha", &bytes);
238 for (label, rect) in [
239 ("red_only", [30.0, 70.0, 70.0, 130.0]),
240 ("blue_over_red", [90.0, 70.0, 110.0, 130.0]),
241 ("blue_over_white", [140.0, 70.0, 170.0, 130.0]),
242 ] {
243 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
244 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
245 println!(" {label}: ours={a:?} theirs={b:?}");
246 for c in 0..3 {
247 assert!((a[c] - b[c]).abs() <= 5.0, "{label} channel {c}: {} vs {}", a[c], b[c]);
248 }
249 }
250}
251
252/// `/Rotate` and a `/CropBox` offset from the `/MediaBox` origin (§7.7.3.3,
253/// §14.11.2). Both renderers must independently agree on where the ink lands.
254#[test]
255#[ignore]
256fn refdiff_rotate_and_cropbox() {
257 for rotate in [0i64, 90, 180, 270] {
258 let ops = vec![
259 op("rg", vec![n(0.9), n(0.3), n(0.0)]),
260 // An L shape, so every rotation is distinguishable.
261 op("re", vec![n(60.0), n(60.0), n(80.0), n(20.0)]),
262 op("f", vec![]),
263 op("re", vec![n(60.0), n(60.0), n(20.0), n(80.0)]),
264 op("f", vec![]),
265 ];
266 let page = dictionary! {
267 "MediaBox" => vec![0.into(), 0.into(), 240.into(), 240.into()],
268 "CropBox" => vec![20.into(), 20.into(), 220.into(), 200.into()],
269 "Rotate" => Object::Integer(rotate),
270 };
271 let _ = compare_page(
272 &format!("rotate_{rotate}"),
273 &pdf_bytes_page(ops, dictionary! {}, page),
274 );
275 }
276}
277
278/// Dash patterns (§8.4.3.6): phase handling and the odd-length-array rule are
279/// both easy to read two ways.
280#[test]
281#[ignore]
282fn refdiff_dash_patterns() {
283 let ops = vec![
284 op("w", vec![n(6.0)]),
285 op("J", vec![Object::Integer(0)]),
286 op("g", vec![n(0.0)]),
287 op("d", vec![Object::Array(vec![n(12.0), n(8.0)]), n(0.0)]),
288 op("m", vec![n(10.0), n(170.0)]),
289 op("l", vec![n(190.0), n(170.0)]),
290 op("S", vec![]),
291 op("d", vec![Object::Array(vec![n(12.0), n(8.0)]), n(6.0)]),
292 op("m", vec![n(10.0), n(130.0)]),
293 op("l", vec![n(190.0), n(130.0)]),
294 op("S", vec![]),
295 // Odd-length array: §8.4.3.6 says it is used cyclically, so the phases
296 // alternate on/off across repetitions.
297 op("d", vec![Object::Array(vec![n(15.0)]), n(0.0)]),
298 op("m", vec![n(10.0), n(90.0)]),
299 op("l", vec![n(190.0), n(90.0)]),
300 op("S", vec![]),
301 // Empty array = solid.
302 op("d", vec![Object::Array(vec![]), n(0.0)]),
303 op("m", vec![n(10.0), n(50.0)]),
304 op("l", vec![n(190.0), n(50.0)]),
305 op("S", vec![]),
306 ];
307 let _ = compare_page("dashes", &pdf_bytes(ops, dictionary! {}));
308}
309
310/// Luminosity soft mask (§11.6.5.2) driven by an axial shading.
311#[test]
312#[ignore]
313fn refdiff_luminosity_soft_mask() {
314 let mut doc = Document::with_version("1.7");
315 let f = doc.add_object(Stream::new(
316 dictionary! {
317 "FunctionType" => 2, "Domain" => vec![0.into(), 1.into()],
318 "C0" => vec![Object::Real(0.0)], "C1" => vec![Object::Real(1.0)], "N" => 1,
319 },
320 Vec::new(),
321 ));
322 let sh = doc.add_object(dictionary! {
323 "ShadingType" => 2,
324 "ColorSpace" => Object::Name(b"DeviceGray".to_vec()),
325 "Coords" => vec![Object::Real(20.0), Object::Real(0.0), Object::Real(180.0), Object::Real(0.0)],
326 "Function" => Object::Reference(f),
327 "Extend" => vec![Object::Boolean(true), Object::Boolean(true)],
328 });
329 let group = doc.add_object(Stream::new(
330 dictionary! {
331 "Type" => "XObject", "Subtype" => "Form",
332 "BBox" => vec![Object::Real(0.0), Object::Real(0.0), Object::Real(200.0), Object::Real(200.0)],
333 "Group" => dictionary! { "S" => "Transparency", "CS" => Object::Name(b"DeviceGray".to_vec()) },
334 "Resources" => dictionary! { "Shading" => dictionary! { "S0" => Object::Reference(sh) } },
335 },
336 Content { operations: vec![op("sh", vec![Object::Name(b"S0".to_vec())])] }
337 .encode()
338 .expect("mask content"),
339 ));
340 let gs = doc.add_object(dictionary! {
341 "Type" => "ExtGState",
342 "SMask" => dictionary! { "S" => "Luminosity", "G" => Object::Reference(group) },
343 });
344 let ops = vec![
345 op("gs", vec![Object::Name(b"G".to_vec())]),
346 op("rg", vec![n(0.8), n(0.0), n(0.0)]),
347 op("re", vec![n(10.0), n(50.0), n(180.0), n(100.0)]),
348 op("f", vec![]),
349 ];
350 let res = dictionary! { "ExtGState" => dictionary! { "G" => Object::Reference(gs) } };
351 let content = Content { operations: ops }.encode().expect("encode");
352 let _ = assemble(&mut doc, content, res, dictionary! {});
353 let mut bytes = Vec::new();
354 doc.save_to(&mut bytes).expect("save");
355 let _ = compare_page("soft_mask", &bytes);
356}
357