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

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

golden_tests_part5.rs432 lines · 18.3 KiBRaw
1/// The same orientation invariant for the TYPE 4 MESH producer
2/// (`shading::rasterize_shading_mesh`), which is a different code path from the
3/// axial one and was flipped independently. Mesh vertex colours are black along
4/// `y = 0` and white along `y = 100`, so the row placed at the lower page y must
5/// be the darker one — the same document-level fact, no convention assumed.
6#[test]
7fn type4_mesh_raster_is_not_vertically_flipped() {
8 let mut doc = Document::with_version("1.5");
9 // flag(1) x(1) y(1) r(1) g(1) b(1) per vertex; two triangles covering a square.
10 // Decode maps x,y from 0..255 onto 0..100, so y=0 is black and y=100 is white.
11 let v = |x: u8, y: u8, l: u8| -> [u8; 6] { [0, x, y, l, l, l] };
12 let mut mesh: Vec<u8> = Vec::new();
13 for b in [v(0, 0, 0), v(255, 0, 0), v(0, 255, 255)] {
14 mesh.extend_from_slice(&b);
15 }
16 for b in [v(255, 0, 0), v(0, 255, 255), v(255, 255, 255)] {
17 mesh.extend_from_slice(&b);
18 }
19 let sh_id = doc.add_object(Stream::new(
20 dictionary! {
21 "ShadingType" => 4,
22 "ColorSpace" => "DeviceRGB",
23 "BitsPerCoordinate" => 8,
24 "BitsPerComponent" => 8,
25 "BitsPerFlag" => 8,
26 "Decode" => vec![
27 0.into(), 100.into(), 0.into(), 100.into(),
28 0.into(), 1.into(), 0.into(), 1.into(), 0.into(), 1.into(),
29 ],
30 },
31 mesh,
32 ));
33 let bytes = Content {
34 operations: vec![Operation::new("sh", vec![Object::Name(b"Sh0".to_vec())])],
35 }
36 .encode()
37 .unwrap();
38 let page_id = assemble(
39 &mut doc,
40 bytes,
41 dictionary! { "Shading" => dictionary! { "Sh0" => sh_id } },
42 dictionary! { "MediaBox" => vec![0.into(), 0.into(), 100.into(), 100.into()] },
43 dictionary! {},
44 );
45
46 let page = interpret_page(&doc, page_id).expect("interpret");
47 let (ctm, w, h, data) = page
48 .prims
49 .iter()
50 .find_map(|p| match p {
51 Prim::Image { ctm, w, h, data, format: 0, .. } => Some((*ctm, *w, *h, data.clone())),
52 _ => None,
53 })
54 .expect("a type 4 mesh must emit a decoded raster");
55 assert!(w > 0 && h > 1, "need at least two rows to talk about orientation");
56
57 // Mean luminance over OPAQUE pixels only: a mesh does not cover its whole bbox,
58 // and averaging in the transparent margin would wash the gradient out.
59 let row_luma = |row: u32| -> Option<f64> {
60 let (mut sum, mut n) = (0f64, 0f64);
61 for x in 0..w {
62 let i = ((row * w + x) * 4) as usize;
63 if i + 3 < data.len() && data[i + 3] > 0 {
64 sum += (data[i] as f64 + data[i + 1] as f64 + data[i + 2] as f64) / 3.0;
65 n += 1.0;
66 }
67 }
68 if n == 0.0 { None } else { Some(sum / n) }
69 };
70 // First and last rows that actually contain mesh.
71 let first = (0..h).find(|&r| row_luma(r).is_some()).expect("some row must be covered");
72 let last = (0..h).rev().find(|&r| row_luma(r).is_some()).expect("some row must be covered");
73 assert!(last > first, "the mesh must cover more than one row");
74 let (luma_first, luma_last) = (row_luma(first).unwrap(), row_luma(last).unwrap());
75 assert!
76 (
77 (luma_first - luma_last).abs() > 16.0,
78 "the mesh gradient must vary between its first and last covered row, else \
79 this test cannot detect an inversion (row{first}={luma_first:.0}, \
80 row{last}={luma_last:.0})"
81 );
82
83 // Lower row index is nearer v=1 (§8.9.5.2), so it lands wherever v=1 lands.
84 let y_at_v0 = transform(&ctm, 0.5, 0.0).1;
85 let y_at_v1 = transform(&ctm, 0.5, 1.0).1;
86 let (low_y_luma, high_y_luma) = if y_at_v1 > y_at_v0 {
87 (luma_last, luma_first)
88 } else {
89 (luma_first, luma_last)
90 };
91 assert!(
92 low_y_luma < high_y_luma,
93 "the type 4 mesh raster is vertically FLIPPED. Vertex colours are black along \
94 page y=0 and white along y=100, so the row placed at the lower page y must be \
95 darker. Got low-y luma {low_y_luma:.0}, high-y luma {high_y_luma:.0} \
96 (row{first}={luma_first:.0}, row{last}={luma_last:.0}; v=0 at y={y_at_v0:.0}, \
97 v=1 at y={y_at_v1:.0}). This producer is separate from the axial one and was \
98 flipped independently, so fixing one does not fix the other."
99 );
100}
101
102/// The same orientation invariant for the TYPE 1 FUNCTION-BASED producer
103/// (`images::rasterize_shading_function_based`), the fourth and last of the
104/// synthetic rasters that were mirrored. Its `/Function` returns the gray level
105/// directly from the y coordinate, so black sits at page y=0 and white at y=100
106/// by construction — the same document-level fact as the other three.
107#[test]
108fn type1_function_based_raster_is_not_vertically_flipped() {
109 let mut doc = Document::with_version("1.5");
110 // PostScript calculator over (x, y): drop x, scale y into 0..1 as the gray level.
111 let func_id = doc.add_object(Stream::new(
112 dictionary! {
113 "FunctionType" => 4,
114 "Domain" => vec![0.into(), 100.into(), 0.into(), 100.into()],
115 "Range" => vec![0.into(), 1.into()],
116 },
117 b"{ exch pop 100 div }".to_vec(),
118 ));
119 let sh_id = doc.add_object(dictionary! {
120 "ShadingType" => 1,
121 "ColorSpace" => "DeviceGray",
122 "Domain" => vec![0.into(), 100.into(), 0.into(), 100.into()],
123 "Function" => func_id,
124 });
125 let bytes = Content {
126 operations: vec![Operation::new("sh", vec![Object::Name(b"Sh0".to_vec())])],
127 }
128 .encode()
129 .unwrap();
130 let page_id = assemble(
131 &mut doc,
132 bytes,
133 dictionary! { "Shading" => dictionary! { "Sh0" => sh_id } },
134 dictionary! { "MediaBox" => vec![0.into(), 0.into(), 100.into(), 100.into()] },
135 dictionary! {},
136 );
137
138 let page = interpret_page(&doc, page_id).expect("interpret");
139 let (ctm, w, h, data) = page
140 .prims
141 .iter()
142 .find_map(|p| match p {
143 Prim::Image { ctm, w, h, data, format: 0, .. } => Some((*ctm, *w, *h, data.clone())),
144 _ => None,
145 })
146 .expect("a type 1 function-based shading must emit a decoded raster");
147 assert!(w > 0 && h > 1, "need at least two rows to talk about orientation");
148
149 let row_luma = |row: u32| -> Option<f64> {
150 let (mut sum, mut n) = (0f64, 0f64);
151 for x in 0..w {
152 let i = ((row * w + x) * 4) as usize;
153 if i + 3 < data.len() && data[i + 3] > 0 {
154 sum += (data[i] as f64 + data[i + 1] as f64 + data[i + 2] as f64) / 3.0;
155 n += 1.0;
156 }
157 }
158 if n == 0.0 { None } else { Some(sum / n) }
159 };
160 let first = (0..h).find(|&r| row_luma(r).is_some()).expect("some row must be covered");
161 let last = (0..h).rev().find(|&r| row_luma(r).is_some()).expect("some row must be covered");
162 assert!(last > first, "the shading must cover more than one row");
163 let (luma_first, luma_last) = (row_luma(first).unwrap(), row_luma(last).unwrap());
164 assert!(
165 (luma_first - luma_last).abs() > 16.0,
166 "the ramp must vary between the first and last covered row, else this test \
167 cannot detect an inversion (row{first}={luma_first:.0}, row{last}={luma_last:.0})"
168 );
169
170 let y_at_v0 = transform(&ctm, 0.5, 0.0).1;
171 let y_at_v1 = transform(&ctm, 0.5, 1.0).1;
172 let (low_y_luma, high_y_luma) = if y_at_v1 > y_at_v0 {
173 (luma_last, luma_first)
174 } else {
175 (luma_first, luma_last)
176 };
177 assert!(
178 low_y_luma < high_y_luma,
179 "the type 1 function-based raster is vertically FLIPPED. Its /Function returns \
180 gray = y/100, so the row placed at the lower page y must be darker. Got low-y \
181 luma {low_y_luma:.0}, high-y luma {high_y_luma:.0} (row{first}={luma_first:.0}, \
182 row{last}={luma_last:.0}; v=0 at y={y_at_v0:.0}, v=1 at y={y_at_v1:.0})."
183 );
184}
185
186/// The same orientation invariant for a RADIAL (type 3) shading. Radial shares the
187/// axial pixel loop, so this is a cheap guard against a future divergence rather
188/// than a separate producer — the concentric case has no vertical asymmetry, so the
189/// circles are offset along y to give the raster a known top and bottom.
190#[test]
191fn radial_shading_raster_is_not_vertically_flipped() {
192 let mut doc = Document::with_version("1.5");
193 let func_id = doc.add_object(dictionary! {
194 "FunctionType" => 2,
195 "Domain" => vec![0.into(), 1.into()],
196 "C0" => vec![0.0.into(), 0.0.into(), 0.0.into()], // black at the small circle
197 "C1" => vec![1.0.into(), 1.0.into(), 1.0.into()], // white at the large one
198 "N" => 1,
199 });
200 // Small circle low on the page, large circle high: dark at the bottom.
201 let sh_id = doc.add_object(dictionary! {
202 "ShadingType" => 3,
203 "ColorSpace" => "DeviceRGB",
204 "Coords" => vec![50.into(), 5.into(), 1.into(), 50.into(), 95.into(), 90.into()],
205 "Extend" => vec![Object::Boolean(true), Object::Boolean(true)],
206 "Function" => func_id,
207 });
208 let bytes = Content {
209 operations: vec![Operation::new("sh", vec![Object::Name(b"Sh0".to_vec())])],
210 }
211 .encode()
212 .unwrap();
213 let page_id = assemble(
214 &mut doc,
215 bytes,
216 dictionary! { "Shading" => dictionary! { "Sh0" => sh_id } },
217 dictionary! { "MediaBox" => vec![0.into(), 0.into(), 100.into(), 100.into()] },
218 dictionary! {},
219 );
220
221 let page = interpret_page(&doc, page_id).expect("interpret");
222 let (ctm, w, h, data) = page
223 .prims
224 .iter()
225 .find_map(|p| match p {
226 Prim::Image { ctm, w, h, data, format: 0, .. } => Some((*ctm, *w, *h, data.clone())),
227 _ => None,
228 })
229 .expect("a radial shading must emit a decoded raster");
230 let row_luma = |row: u32| -> Option<f64> {
231 let (mut sum, mut n) = (0f64, 0f64);
232 for x in 0..w {
233 let i = ((row * w + x) * 4) as usize;
234 if i + 3 < data.len() && data[i + 3] > 0 {
235 sum += (data[i] as f64 + data[i + 1] as f64 + data[i + 2] as f64) / 3.0;
236 n += 1.0;
237 }
238 }
239 if n == 0.0 { None } else { Some(sum / n) }
240 };
241 let first = (0..h).find(|&r| row_luma(r).is_some()).expect("some row must be covered");
242 let last = (0..h).rev().find(|&r| row_luma(r).is_some()).expect("some row must be covered");
243 let (luma_first, luma_last) = (row_luma(first).unwrap(), row_luma(last).unwrap());
244 assert!(
245 (luma_first - luma_last).abs() > 16.0,
246 "the radial ramp must vary vertically for this test to mean anything \
247 (row{first}={luma_first:.0}, row{last}={luma_last:.0})"
248 );
249 let y_at_v0 = transform(&ctm, 0.5, 0.0).1;
250 let y_at_v1 = transform(&ctm, 0.5, 1.0).1;
251 let (low_y_luma, high_y_luma) = if y_at_v1 > y_at_v0 {
252 (luma_last, luma_first)
253 } else {
254 (luma_first, luma_last)
255 };
256 assert!(
257 low_y_luma < high_y_luma,
258 "the radial raster is vertically FLIPPED. Its small (black) circle sits low on \
259 the page and its large (white) circle high, so the row at the lower page y \
260 must be darker. Got low-y {low_y_luma:.0}, high-y {high_y_luma:.0}."
261 );
262}
263
264/// only in its LOWER half must come back as a raster whose opaque rows are the
265/// ones that land at the lower page y — not the upper.
266#[test]
267fn tiling_pattern_cell_raster_is_not_vertically_flipped() {
268 let mut doc = Document::with_version("1.5");
269 // Cell is 10x10; paint only y in [0,5), i.e. the bottom half.
270 let cell = Content {
271 operations: vec![
272 Operation::new("rg", vec![0.0.into(), 0.0.into(), 0.0.into()]),
273 Operation::new("re", vec![0.into(), 0.into(), 10.into(), 5.into()]),
274 Operation::new("f", vec![]),
275 ],
276 };
277 let pid = doc.add_object(Stream::new(
278 dictionary! {
279 "Type" => "Pattern", "PatternType" => 1, "PaintType" => 1, "TilingType" => 1,
280 "BBox" => vec![0.into(), 0.into(), 10.into(), 10.into()],
281 "XStep" => 10, "YStep" => 10,
282 "Resources" => dictionary! {},
283 },
284 cell.encode().unwrap(),
285 ));
286 let region = fill_region(0.0, 0.0, 100.0, 100.0);
287 let mut prims = Vec::new();
288 paint_pattern_fill(
289 &doc, pid, &region, false, &IDENTITY, 0xFF00_0000, 1.0, BlendMode::Normal,
290 &HashMap::new(), &mut prims, 0, 0,
291 );
292
293 // The periodic path emits one ImageTiled cell raster; the per-tile path emits
294 // Fills. Both are valid representations, so assert whichever appeared.
295 let tiled = prims.iter().find_map(|p| match p {
296 Prim::ImageTiled { ctm, w, h, data, .. } => Some((*ctm, *w, *h, data.clone())),
297 _ => None,
298 });
299 if let Some((ctm, w, h, data)) = tiled {
300 println!("pattern cell: RASTER path, {w}x{h} cell");
301 assert!(h > 1, "need at least two rows to talk about orientation");
302 let row_opaque = |row: u32| -> f64 {
303 let (mut op, mut n) = (0f64, 0f64);
304 for x in 0..w {
305 let i = ((row * w + x) * 4) as usize;
306 if i + 3 < data.len() {
307 if data[i + 3] > 0 { op += 1.0; }
308 n += 1.0;
309 }
310 }
311 if n == 0.0 { 0.0 } else { op / n }
312 };
313 let (first, last) = (row_opaque(0), row_opaque(h - 1));
314 assert!(
315 (first - last).abs() > 0.5,
316 "the cell must be opaque in one half and clear in the other, else this \
317 test cannot detect an inversion (row0={first:.2}, row{}={last:.2})",
318 h - 1
319 );
320 let y_at_v0 = transform(&ctm, 0.5, 0.0).1;
321 let y_at_v1 = transform(&ctm, 0.5, 1.0).1;
322 let (low_y_opaque, high_y_opaque) = if y_at_v1 > y_at_v0 { (last, first) } else { (first, last) };
323 assert!(
324 low_y_opaque > high_y_opaque,
325 "the pattern cell raster is vertically FLIPPED. The cell paints only its \
326 lower half, so the raster row landing at the lower page y must be the \
327 opaque one. Got low-y opacity {low_y_opaque:.2}, high-y {high_y_opaque:.2} \
328 (row0={first:.2}, row{}={last:.2}; v=0 at y={y_at_v0:.0}, v=1 at \
329 y={y_at_v1:.0}). A flipped cell still tiles seamlessly, so this is \
330 invisible to a spot check.",
331 h - 1
332 );
333 } else {
334 // Per-tile path: the painted rects must sit in the lower half of each cell.
335 println!("pattern cell: PER-TILE path (no ImageTiled emitted)");
336 let ys: Vec<f32> = prims
337 .iter()
338 .filter_map(|p| match p {
339 Prim::Fill { contours, .. } => contours.first().map(|c| bbox_of(c)[1]),
340 _ => None,
341 })
342 .collect();
343 assert!(!ys.is_empty(), "the pattern must paint something");
344 assert!(
345 ys.iter().any(|&y| (y % 10.0).abs() < 1.0),
346 "each tile's painted rect must start at the cell's low y, got {ys:?}"
347 );
348 }
349}
350
351/// `tests::unclipped_sh_covers_page` by checking the emitted raster genuinely
352/// varies across the page rather than being one flat colour in a corner.
353#[test]
354fn unclipped_sh_raster_spans_the_page_and_varies_across_it() {
355 let mut doc = Document::with_version("1.5");
356 let sh_id = axial_shading(&mut doc);
357 let bytes = Content {
358 operations: vec![Operation::new("sh", vec![Object::Name(b"Sh0".to_vec())])],
359 }
360 .encode()
361 .unwrap();
362 let page_id = assemble(
363 &mut doc,
364 bytes,
365 dictionary! { "Shading" => dictionary! { "Sh0" => sh_id } },
366 dictionary! { "MediaBox" => vec![0.into(), 0.into(), 400.into(), 500.into()] },
367 dictionary! {},
368 );
369 let page = interpret_page(&doc, page_id).expect("interpret");
370 let (ctm, w, h, data) = page
371 .prims
372 .iter()
373 .find_map(|p| match p {
374 Prim::Image { ctm, w, h, data, format: 0, .. } => Some((*ctm, *w, *h, data.clone())),
375 _ => None,
376 })
377 .expect("an unclipped sh must emit a raster");
378 let dev_w = ctm[0].abs() + ctm[2].abs();
379 let dev_h = ctm[1].abs() + ctm[3].abs();
380 assert!(dev_w >= 399.0 && dev_h >= 499.0, "raster must span the page, got {dev_w:.0}x{dev_h:.0}");
381 // The axial ramp runs left to right, so the left and right edges must differ.
382 let px = |x: u32, y: u32| {
383 let i = ((y * w + x) * 4) as usize;
384 (data[i], data[i + 1], data[i + 2])
385 };
386 let (l, r) = (px(0, h / 2), px(w - 1, h / 2));
387 assert_ne!(l, r, "the axial ramp must vary across the page, not paint one flat colour");
388}
389
390/// §14.6.2 + the MAX_OC_STACK cap: BMC/BDC pushes beyond the nesting cap are
391/// dropped, so the matching EMCs must be dropped too. If an EMC past the cap
392/// pops a frame it does not own, the hidden region un-hides from there on.
393#[test]
394fn marked_content_nested_past_the_stack_cap_still_stays_hidden() {
395 let mut doc = Document::with_version("1.6");
396 let ocg_id = doc.add_object(dictionary! { "Type" => "OCG", "Name" => Object::string_literal("L") });
397 let mut ops = vec![Operation::new(
398 "BDC",
399 vec![Object::Name(b"OC".to_vec()), Object::Name(b"OC0".to_vec())],
400 )];
401 // Nest far past the cap, paint, then unwind by exactly as many EMCs.
402 const DEEP: usize = 80;
403 for _ in 0..DEEP {
404 ops.push(Operation::new("BMC", vec![Object::Name(b"Tx".to_vec())]));
405 }
406 ops.extend(rect_ops(10, 10, 40, 40)); // deep inside, hidden
407 for _ in 0..DEEP {
408 ops.push(Operation::new("EMC", vec![]));
409 }
410 ops.extend(rect_ops(60, 10, 40, 40)); // back at OCG level, still hidden
411 ops.push(Operation::new("EMC", vec![]));
412 ops.extend(rect_ops(300, 300, 40, 40)); // outside the OCG, visible
413 let bytes = Content { operations: ops }.encode().unwrap();
414 let catalog = dictionary! {
415 "OCProperties" => dictionary! {
416 "OCGs" => vec![ocg_id.into()],
417 "D" => dictionary! { "OFF" => vec![ocg_id.into()] },
418 },
419 };
420 let resources = dictionary! { "Properties" => dictionary! { "OC0" => ocg_id } };
421 let page_id = assemble(&mut doc, bytes, resources, dictionary! {}, catalog);
422
423 let page = interpret_page(&doc, page_id).expect("interpret");
424 assert!(
425 !ink_in_region(&page.prims, 0.0, 0.0, 150.0, 100.0),
426 "nesting past the marked-content cap must not un-hide the region on the way out"
427 );
428 assert!(
429 ink_in_region(&page.prims, 300.0, 300.0, 341.0, 341.0),
430 "and content genuinely outside the OCG must still paint"
431 );
432}