Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / pdf / src / main / rust / src / tests_part1.rs
tests_part1.rs355 lines · 16.4 KiBRaw
| 1 | /// §8.7.4.1: an unclipped `sh` paints the whole page, so `interpret_page` must |
| 2 | /// seed the clip extent with the page box rather than leaving the shading to fall |
| 3 | /// back to a small guessed square (item 1). |
| 4 | #[test] |
| 5 | fn unclipped_sh_covers_page() { |
| 6 | let mut doc = Document::with_version("1.5"); |
| 7 | let func_id = doc.add_object(dictionary! { |
| 8 | "FunctionType" => 2, |
| 9 | "Domain" => vec![0.into(), 1.into()], |
| 10 | "C0" => vec![1.0.into(), 0.0.into(), 0.0.into()], |
| 11 | "C1" => vec![0.0.into(), 0.0.into(), 1.0.into()], |
| 12 | "N" => 1, |
| 13 | }); |
| 14 | // Axial shading, deliberately with no /BBox. |
| 15 | let sh_id = doc.add_object(dictionary! { |
| 16 | "ShadingType" => 2, |
| 17 | "ColorSpace" => "DeviceRGB", |
| 18 | "Coords" => vec![0.into(), 0.into(), 400.into(), 0.into()], |
| 19 | "Extend" => vec![Object::Boolean(true), Object::Boolean(true)], |
| 20 | "Function" => func_id, |
| 21 | }); |
| 22 | let content = Content { |
| 23 | operations: vec![Operation::new("sh", vec![Object::Name(b"Sh0".to_vec())])], |
| 24 | }; |
| 25 | let content_id = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap())); |
| 26 | let pages_id = doc.new_object_id(); |
| 27 | let page_id = doc.add_object(dictionary! { |
| 28 | "Type" => "Page", |
| 29 | "Parent" => pages_id, |
| 30 | "MediaBox" => vec![0.into(), 0.into(), 400.into(), 500.into()], |
| 31 | "Contents" => content_id, |
| 32 | "Resources" => dictionary! { "Shading" => dictionary! { "Sh0" => sh_id } }, |
| 33 | }); |
| 34 | doc.objects.insert( |
| 35 | pages_id, |
| 36 | Object::Dictionary(dictionary! { |
| 37 | "Type" => "Pages", |
| 38 | "Kids" => vec![page_id.into()], |
| 39 | "Count" => 1, |
| 40 | }), |
| 41 | ); |
| 42 | let catalog_id = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 43 | doc.trailer.set("Root", catalog_id); |
| 44 | |
| 45 | let page = interpret_page(&doc, page_id).expect("page should interpret"); |
| 46 | let ctm = page |
| 47 | .prims |
| 48 | .iter() |
| 49 | .find_map(|p| match p { |
| 50 | Prim::Image { ctm, .. } => Some(*ctm), |
| 51 | _ => None, |
| 52 | }) |
| 53 | .expect("unclipped sh must emit an Image prim"); |
| 54 | // The image unit square maps through `ctm`; its device width/height must span |
| 55 | // the page, not a ~100x100 patch near the origin. |
| 56 | let w = (ctm[0].abs() + ctm[2].abs()) as f64; |
| 57 | let h = (ctm[1].abs() + ctm[3].abs()) as f64; |
| 58 | assert!(w >= 399.0, "shading device width {w} should cover the 400pt page"); |
| 59 | assert!(h >= 499.0, "shading device height {h} should cover the 500pt page"); |
| 60 | } |
| 61 | |
| 62 | /// Round-trip a full open -> count -> render -> close cycle via the byte API. |
| 63 | #[test] |
| 64 | fn open_render_close_roundtrip() { |
| 65 | let mut doc = Document::with_version("1.5"); |
| 66 | let content = Content { |
| 67 | operations: vec![ |
| 68 | Operation::new("re", vec![0.into(), 0.into(), 10.into(), 10.into()]), |
| 69 | Operation::new("f", vec![]), |
| 70 | ], |
| 71 | }; |
| 72 | let content_data = content.encode().unwrap(); |
| 73 | let content_id = doc.add_object(Stream::new(dictionary! {}, content_data)); |
| 74 | let pages_id = doc.new_object_id(); |
| 75 | let page_id = doc.add_object(dictionary! { |
| 76 | "Type" => "Page", |
| 77 | "Parent" => pages_id, |
| 78 | "MediaBox" => vec![0.into(), 0.into(), 200.into(), 300.into()], |
| 79 | "Contents" => content_id, |
| 80 | "Resources" => dictionary! {}, |
| 81 | }); |
| 82 | doc.objects.insert( |
| 83 | pages_id, |
| 84 | Object::Dictionary(dictionary! { |
| 85 | "Type" => "Pages", |
| 86 | "Kids" => vec![page_id.into()], |
| 87 | "Count" => 1, |
| 88 | }), |
| 89 | ); |
| 90 | let catalog_id = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 91 | doc.trailer.set("Root", catalog_id); |
| 92 | |
| 93 | let mut bytes = Vec::new(); |
| 94 | doc.save_to(&mut bytes).unwrap(); |
| 95 | |
| 96 | let handle = open_document(&bytes); |
| 97 | assert_ne!(handle, 0); |
| 98 | assert_eq!(page_count(handle), 1); |
| 99 | let buf = render_page(handle, 0).expect("render should succeed"); |
| 100 | // Header v2: MAGIC, VERSION, width, height, count. |
| 101 | let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap()); |
| 102 | assert_eq!(magic, 0x50444657); |
| 103 | let width = f32::from_le_bytes(buf[8..12].try_into().unwrap()); |
| 104 | let height = f32::from_le_bytes(buf[12..16].try_into().unwrap()); |
| 105 | assert_eq!(width, 200.0); |
| 106 | assert_eq!(height, 300.0); |
| 107 | close_document(handle); |
| 108 | assert_eq!(page_count(handle), 0); |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod edit_render_tests { |
| 113 | use crate::*; |
| 114 | use lopdf::{dictionary, Stream}; |
| 115 | use lopdf::content::{Content, Operation}; |
| 116 | |
| 117 | fn one_page_pdf() -> Vec<u8> { |
| 118 | let mut doc = Document::with_version("1.5"); |
| 119 | let content = lopdf::content::Content { |
| 120 | operations: vec![lopdf::content::Operation::new("re", vec![0.into(), 0.into(), 10.into(), 10.into()])], |
| 121 | }; |
| 122 | let cid = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap())); |
| 123 | let pages_id = doc.new_object_id(); |
| 124 | let page_id = doc.add_object(dictionary! { |
| 125 | "Type" => "Page", "Parent" => pages_id, |
| 126 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 127 | "Contents" => cid, "Resources" => dictionary! {}, |
| 128 | }); |
| 129 | doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 130 | "Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1, |
| 131 | })); |
| 132 | let cat = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 133 | doc.trailer.set("Root", cat); |
| 134 | let mut bytes = Vec::new(); |
| 135 | doc.save_to(&mut bytes).unwrap(); |
| 136 | bytes |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn added_rect_annotation_renders() { |
| 141 | let bytes = one_page_pdf(); |
| 142 | let handle = open_document(&bytes); |
| 143 | assert_ne!(handle, 0); |
| 144 | let id = add_square(handle, 0, [100.0, 100.0, 300.0, 250.0], 0xFFFF0000, 2.0, false); |
| 145 | assert!(id.is_some() && id != Some(0), "add_square failed: {id:?}"); |
| 146 | |
| 147 | let buf = render_page(handle, 0).expect("render"); |
| 148 | // The JNI-facing path must produce a non-empty page. |
| 149 | let count = u32::from_le_bytes(buf[16..20].try_into().unwrap()); |
| 150 | assert!(count >= 1, "expected primitives on the page, got {count}"); |
| 151 | |
| 152 | // Assert on the primitives themselves rather than hand-decoding the wire |
| 153 | // buffer. The previous inline decoder duplicated the v10 layout and had |
| 154 | // drifted out of date (it omitted Stroke's v5 blend byte, Image's v9 alpha |
| 155 | // and v10 blend, Fill's v6 multi-contour count and ClipPush's v4 path ops), |
| 156 | // so it desynced and panicked with a bogus "bad tag" on any page whose prim |
| 157 | // mix changed. Wire layout is covered by wire::tests::round_trips_all_primitives. |
| 158 | let strokes = { |
| 159 | let reg = registry() |
| 160 | .lock() |
| 161 | .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 162 | let doc = reg.get(&handle).expect("document still open"); |
| 163 | let page_id = *doc.get_pages().get(&1).expect("one page"); |
| 164 | let page = interpret_page(doc, page_id).expect("interpret"); |
| 165 | page.prims |
| 166 | .iter() |
| 167 | .filter(|p| matches!(p, Prim::Stroke { .. })) |
| 168 | .count() |
| 169 | }; |
| 170 | assert!(strokes >= 1, "expected the annotation stroke to render, got {strokes}"); |
| 171 | close_document(handle); |
| 172 | } |
| 173 | |
| 174 | /// A luminosity soft mask set via ExtGState must bracket a subsequent fill |
| 175 | /// with SoftMaskPush ... Fill ... SoftMaskContent ... SoftMaskPop. |
| 176 | #[test] |
| 177 | fn soft_mask_brackets_a_fill() { |
| 178 | let mut doc = Document::with_version("1.7"); |
| 179 | // Soft-mask group form XObject: fills a white rectangle (full luminance). |
| 180 | let mask_content = Content { operations: vec![ |
| 181 | Operation::new("rg", vec![1.0.into(), 1.0.into(), 1.0.into()]), |
| 182 | Operation::new("re", vec![0.into(), 0.into(), 200.into(), 200.into()]), |
| 183 | Operation::new("f", vec![]), |
| 184 | ]}; |
| 185 | let mask_id = doc.add_object(Stream::new(dictionary! { |
| 186 | "Type" => "XObject", |
| 187 | "Subtype" => "Form", |
| 188 | "BBox" => vec![0.into(), 0.into(), 200.into(), 200.into()], |
| 189 | "Group" => dictionary! { "S" => "Transparency" }, |
| 190 | }, mask_content.encode().unwrap())); |
| 191 | let gs_id = doc.add_object(dictionary! { |
| 192 | "SMask" => dictionary! { |
| 193 | "S" => "Luminosity", |
| 194 | "G" => Object::Reference(mask_id), |
| 195 | }, |
| 196 | }); |
| 197 | let content = Content { operations: vec![ |
| 198 | Operation::new("gs", vec![Object::Name(b"GS1".to_vec())]), |
| 199 | Operation::new("rg", vec![1.0.into(), 0.0.into(), 0.0.into()]), |
| 200 | Operation::new("re", vec![10.into(), 10.into(), 50.into(), 50.into()]), |
| 201 | Operation::new("f", vec![]), |
| 202 | ]}; |
| 203 | let content_id = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap())); |
| 204 | let resources = dictionary! { |
| 205 | "ExtGState" => dictionary! { "GS1" => gs_id }, |
| 206 | }; |
| 207 | let pages_id = doc.new_object_id(); |
| 208 | let page_id = doc.add_object(dictionary! { |
| 209 | "Type" => "Page", "Parent" => pages_id, |
| 210 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 211 | "Contents" => content_id, "Resources" => resources, |
| 212 | }); |
| 213 | doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 214 | "Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1, |
| 215 | })); |
| 216 | let catalog_id = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 217 | doc.trailer.set("Root", catalog_id); |
| 218 | |
| 219 | let page = interpret_page(&doc, page_id).expect("interpret"); |
| 220 | let kinds: Vec<u8> = page.prims.iter().map(|p| match p { |
| 221 | Prim::SoftMaskPush { .. } => 10, |
| 222 | Prim::SoftMaskContent => 11, |
| 223 | Prim::SoftMaskPop => 12, |
| 224 | Prim::Fill { .. } => 2, |
| 225 | _ => 0, |
| 226 | }).collect(); |
| 227 | let push = kinds.iter().position(|&k| k == 10).expect("SoftMaskPush"); |
| 228 | let content_marker = kinds.iter().position(|&k| k == 11).expect("SoftMaskContent"); |
| 229 | let pop = kinds.iter().position(|&k| k == 12).expect("SoftMaskPop"); |
| 230 | let masked_fill = kinds[push..content_marker].contains(&2); |
| 231 | assert!(push < content_marker && content_marker < pop, "bracket order"); |
| 232 | assert!(masked_fill, "the red fill must sit inside the mask bracket"); |
| 233 | } |
| 234 | |
| 235 | /// Overprint (`/op true`) must NOT change how a fill is composited. Per ISO |
| 236 | /// 32000-1 8.6.7 overprint control governs how ink is applied to individual |
| 237 | /// colorants and has no effect on a device with one colorant or an additive |
| 238 | /// (RGB) device — which this rasterizer is. The previous Multiply |
| 239 | /// approximation actively broke pages, because `white MULTIPLY dst == dst` |
| 240 | /// turns the white knockout rectangles that editors emit to cover content |
| 241 | /// into no-ops, so the content underneath reappears. Do not "restore" this. |
| 242 | #[test] |
| 243 | fn overprint_fill_ignored_on_rgb_device() { |
| 244 | let mut doc = Document::with_version("1.7"); |
| 245 | let gs_id = doc.add_object(dictionary! { "op" => true }); |
| 246 | let content = Content { operations: vec![ |
| 247 | Operation::new("gs", vec![Object::Name(b"GS1".to_vec())]), |
| 248 | Operation::new("rg", vec![0.0.into(), 0.0.into(), 1.0.into()]), |
| 249 | Operation::new("re", vec![10.into(), 10.into(), 50.into(), 50.into()]), |
| 250 | Operation::new("f", vec![]), |
| 251 | ]}; |
| 252 | let content_id = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap())); |
| 253 | let resources = dictionary! { "ExtGState" => dictionary! { "GS1" => gs_id } }; |
| 254 | let pages_id = doc.new_object_id(); |
| 255 | let page_id = doc.add_object(dictionary! { |
| 256 | "Type" => "Page", "Parent" => pages_id, |
| 257 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 258 | "Contents" => content_id, "Resources" => resources, |
| 259 | }); |
| 260 | doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 261 | "Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1, |
| 262 | })); |
| 263 | let catalog_id = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 264 | doc.trailer.set("Root", catalog_id); |
| 265 | |
| 266 | let page = interpret_page(&doc, page_id).expect("interpret"); |
| 267 | let fill = page.prims.iter().find_map(|p| match p { |
| 268 | Prim::Fill { blend, .. } => Some(*blend), |
| 269 | _ => None, |
| 270 | }).expect("a fill"); |
| 271 | assert!( |
| 272 | fill == BlendMode::Normal, |
| 273 | "overprint has no effect on an additive RGB device (8.6.7); got {:?}", |
| 274 | fill as u8 |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /// Turning one radio-button widget on must clear its siblings' `/AS` to Off |
| 279 | /// and record the chosen export value on the parent field's `/V`. |
| 280 | #[test] |
| 281 | fn radio_group_clears_siblings() { |
| 282 | let mut doc = Document::with_version("1.7"); |
| 283 | let parent_id = doc.new_object_id(); |
| 284 | let ap_a = dictionary! { "N" => dictionary! { "A" => dictionary!{}, "Off" => dictionary!{} } }; |
| 285 | let ap_b = dictionary! { "N" => dictionary! { "B" => dictionary!{}, "Off" => dictionary!{} } }; |
| 286 | let w1 = doc.add_object(dictionary! { |
| 287 | "Type" => "Annot", "Subtype" => "Widget", "Parent" => parent_id, |
| 288 | "AS" => Object::Name(b"Off".to_vec()), "AP" => ap_a, |
| 289 | "Rect" => vec![0.into(), 0.into(), 10.into(), 10.into()], |
| 290 | }); |
| 291 | let w2 = doc.add_object(dictionary! { |
| 292 | "Type" => "Annot", "Subtype" => "Widget", "Parent" => parent_id, |
| 293 | "AS" => Object::Name(b"Off".to_vec()), "AP" => ap_b, |
| 294 | "Rect" => vec![0.into(), 0.into(), 10.into(), 10.into()], |
| 295 | }); |
| 296 | doc.objects.insert(parent_id, Object::Dictionary(dictionary! { |
| 297 | "FT" => "Btn", "Ff" => (1i64 << 15), // radio flag |
| 298 | "Kids" => vec![w1.into(), w2.into()], |
| 299 | "V" => Object::Name(b"Off".to_vec()), |
| 300 | })); |
| 301 | let handle = crate::registry::next_handle(); |
| 302 | crate::registry::registry().lock().unwrap().insert(handle, doc); |
| 303 | |
| 304 | assert!(crate::forms::set_checkbox(handle, crate::annotations::encode_id(w1), true)); |
| 305 | { |
| 306 | let reg = crate::registry::registry().lock().unwrap(); |
| 307 | let d = reg.get(&handle).unwrap(); |
| 308 | assert_eq!(d.get_dictionary(w1).unwrap().get(b"AS").unwrap().as_name().unwrap(), b"A"); |
| 309 | assert_eq!(d.get_dictionary(w2).unwrap().get(b"AS").unwrap().as_name().unwrap(), b"Off"); |
| 310 | assert_eq!(d.get_dictionary(parent_id).unwrap().get(b"V").unwrap().as_name().unwrap(), b"A"); |
| 311 | } |
| 312 | // Selecting the second widget flips exclusivity. |
| 313 | assert!(crate::forms::set_checkbox(handle, crate::annotations::encode_id(w2), true)); |
| 314 | { |
| 315 | let reg = crate::registry::registry().lock().unwrap(); |
| 316 | let d = reg.get(&handle).unwrap(); |
| 317 | assert_eq!(d.get_dictionary(w1).unwrap().get(b"AS").unwrap().as_name().unwrap(), b"Off"); |
| 318 | assert_eq!(d.get_dictionary(w2).unwrap().get(b"AS").unwrap().as_name().unwrap(), b"B"); |
| 319 | assert_eq!(d.get_dictionary(parent_id).unwrap().get(b"V").unwrap().as_name().unwrap(), b"B"); |
| 320 | } |
| 321 | crate::registry::close_document(handle); |
| 322 | } |
| 323 | |
| 324 | /// A Square annotation with no /AP stream must still render (synthesized |
| 325 | /// appearance) — here an interior-colored fill. |
| 326 | #[test] |
| 327 | fn annotation_without_ap_is_synthesized() { |
| 328 | let mut doc = Document::with_version("1.7"); |
| 329 | let content_id = doc.add_object(Stream::new(dictionary! {}, Vec::new())); |
| 330 | let square = doc.add_object(dictionary! { |
| 331 | "Type" => "Annot", |
| 332 | "Subtype" => "Square", |
| 333 | "Rect" => vec![100.into(), 100.into(), 200.into(), 160.into()], |
| 334 | "IC" => vec![1.0.into(), 0.0.into(), 0.0.into()], // red interior |
| 335 | "C" => vec![0.0.into(), 0.0.into(), 0.0.into()], |
| 336 | }); |
| 337 | let pages_id = doc.new_object_id(); |
| 338 | let page_id = doc.add_object(dictionary! { |
| 339 | "Type" => "Page", "Parent" => pages_id, |
| 340 | "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 341 | "Contents" => content_id, |
| 342 | "Resources" => dictionary! {}, |
| 343 | "Annots" => vec![square.into()], |
| 344 | }); |
| 345 | doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 346 | "Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1, |
| 347 | })); |
| 348 | let catalog_id = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages_id }); |
| 349 | doc.trailer.set("Root", catalog_id); |
| 350 | |
| 351 | let page = interpret_page(&doc, page_id).expect("interpret"); |
| 352 | let has_red_fill = page.prims.iter().any(|p| matches!(p, Prim::Fill { argb, .. } if (*argb & 0x00FF_FFFF) == 0x00FF_0000)); |
| 353 | assert!(has_red_fill, "square annotation without /AP should synthesize a red fill"); |
| 354 | } |
| 355 | } |