From 799e53c5936edd2e7d4d9450398bbce50bfd63ff Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:40:56 -0800 Subject: [PATCH 1/4] hayro-interpret: a soft mask set outside a form draws that form The self-draw check added with the PdfCraft port (patch 89) compared each form against every form being drawn on the page when it was drawn. A device interprets a soft mask, a tiling pattern or a Type 3 glyph when it applies it, which can be inside a form that the mask's content draws again: a soft mask set on the page whose group draws the same shared form as the page then draws. The mask's copy of the form was skipped, and the page reported ContentTruncated. Soft masks, tiling patterns and Type 3 glyphs now record the forms being drawn where the content set or showed them, and their content is checked against that record. A form that draws itself, directly, through another form, or through a soft mask, tiling pattern or Type 3 glyph set inside it, is still skipped and reported. --- vendor/README.craft-reader.md | 11 + vendor/hayro-interpret/SOURCE.json | 16 +- vendor/hayro-interpret/src/context.rs | 44 ++- vendor/hayro-interpret/src/font/mod.rs | 13 +- vendor/hayro-interpret/src/lib.rs | 2 + vendor/hayro-interpret/src/pattern.rs | 17 +- vendor/hayro-interpret/src/self_draw_tests.rs | 282 ++++++++++++++++++ vendor/hayro-interpret/src/soft_mask.rs | 16 +- 8 files changed, 388 insertions(+), 13 deletions(-) create mode 100644 vendor/hayro-interpret/src/self_draw_tests.rs diff --git a/vendor/README.craft-reader.md b/vendor/README.craft-reader.md index 405799ceb6..82e9c29302 100644 --- a/vendor/README.craft-reader.md +++ b/vendor/README.craft-reader.md @@ -88,6 +88,17 @@ colorants paint through their alternate space (`Plate::spots_as_process`) and no overprints. Masks in other color spaces are unchanged. A test in `crates/pdf/tests/plates.rs` checks process, spot, RGB and backdrop colors. +A craft-libs patch, labeled `Craft patch (self-draw scope)` in code comments, scopes the +self-draw check of hayro-interpret patch 89 for soft masks, tiling patterns and Type 3 glyphs. +Each of them records the forms being drawn where the content set or showed it +(`context::FormsDrawing`), and `SoftMask::interpret`, `SoftMask::interpret_plate`, +`TilingPattern::interpret` and `Type3Glyph::interpret` check the forms they draw against that +record instead of the forms the device is drawing when it calls them. A soft mask set on the page +and interpreted by a device while it draws a form now draws that form; before, the mask's copy of +the form was skipped and the page reported `ContentTruncated`. A form that draws itself, directly, +through another form, or through a soft mask, tiling pattern or Type 3 glyph set inside it, is +still skipped. Tests: `self_draw_tests.rs` in hayro-interpret. + The output-intent conversion of patch 36 goes through a `color::CmykConversion` trait, so that callers can convert with their own color management. `CmykSimulation::new` keeps #2770's moxcms conversion as the default, and `CmykSimulation::with_conversion` takes a diff --git a/vendor/hayro-interpret/SOURCE.json b/vendor/hayro-interpret/SOURCE.json index bffa37b555..71924358dd 100644 --- a/vendor/hayro-interpret/SOURCE.json +++ b/vendor/hayro-interpret/SOURCE.json @@ -188,7 +188,7 @@ "path": "src/context.rs", "source_sha256": "dc2be553958ee3f5e5e644a77a0301723bae9ca027a352c13b9caaf549cc0e56", "source_git_blob": "d6772baa15ebb95d3ba2d1f54ce5c250ad1e8137", - "sha256": "221174b2b65824e1489e8d0f61ac5c81b828caf7f5c7053e27c84a6657857f4c" + "sha256": "cf87f1889acca28e7d0e19b08e25ce9db3654de8f612b29f9a111bd06e01affd" }, { "path": "src/convert.rs", @@ -290,7 +290,7 @@ "path": "src/font/mod.rs", "source_sha256": "7e6411927d91205ca76c6b59f9d737cd1104d066f8cfcc0a87f481016ef30313", "source_git_blob": "7c8c8d086d59139df791cdb6952506473ec4b5a7", - "sha256": "c0c903cb8a33ab863412c407ad10181adec0d22b81c899fc5225de14d8878091" + "sha256": "903b2426fbbc1bc0ffbd3f1f2d1a3ce7fdd28aad424f38ba8592b866982de9f9" }, { "path": "src/font/outline.rs", @@ -386,7 +386,7 @@ "path": "src/lib.rs", "source_sha256": "ea3fa23b74f99b5382798f90b7e9f0e3edca354ee26ea92fe26a91ee5c0195c9", "source_git_blob": "24c3be295783d7a53ac6bc39d93e539883480cd4", - "sha256": "ea3fa23b74f99b5382798f90b7e9f0e3edca354ee26ea92fe26a91ee5c0195c9" + "sha256": "777f78daa1c2dbcb945bc2061bfd5748f3ee07a0efbe53de7fa7de29d2880f5f" }, { "path": "src/log.rs", @@ -404,7 +404,13 @@ "path": "src/pattern.rs", "source_sha256": "17b49a465275c20e6645dea9443f2cb3a64c594925fd860e74b80b44d5344e37", "source_git_blob": "9295f51709f8915951d7b9afca9411651d65a1f8", - "sha256": "e57d4de60126eafa0da2113af12bbbde1aabd15554f1ee26f6551b8219f4ede5" + "sha256": "7b89b41219c11eac781b64ec0584ccaebbd76aa9c570ba13a00e0cb06c59b4c5" + }, + { + "path": "src/self_draw_tests.rs", + "source_sha256": null, + "source_git_blob": null, + "sha256": "58930501e624b86fd45ddc82e464a959d989d6ef94eb7e62f8f8ad24c78ec8ec" }, { "path": "src/shading.rs", @@ -416,7 +422,7 @@ "path": "src/soft_mask.rs", "source_sha256": "6cd9c59c0cff580879c1f06ff40f3537c04b719bdb3ea59008fa1ab7559db5dd", "source_git_blob": "4d97d565b53c820fa927fbba8c3dda6acead7f34", - "sha256": "fe3da157602bf5b16a452ecbe1d8d554af992a5b9dfaa771dd23c2e2e66d730b" + "sha256": "54cb20d6ba5ab8e290e420833acfa9ce2e8e7274b74680e4b9481a093b5d1db9" }, { "path": "src/types.rs", diff --git a/vendor/hayro-interpret/src/context.rs b/vendor/hayro-interpret/src/context.rs index 7997e9c0de..66ae11e1cb 100644 --- a/vendor/hayro-interpret/src/context.rs +++ b/vendor/hayro-interpret/src/context.rs @@ -40,7 +40,9 @@ std::thread_local! { // PdfCraft patch (89): the form XObjects being drawn on this page, outermost first, across // the contexts of the tiling patterns, Type 3 glyphs, soft masks and groups painted while // they draw. A form already on it draws itself and is skipped. Its length is bounded by the - // nesting limits: every form on it is nested in the ones before it. + // nesting limits: every form on it is nested in the ones before it. While a device + // interprets a soft mask, tiling pattern or Type 3 glyph, it holds the forms that one was + // set in (`with_forms_drawing`). static FORMS_DRAWING: RefCell> = const { RefCell::new(Vec::new()) }; } @@ -159,6 +161,42 @@ pub(crate) fn take_nested_paint(depth: u32) -> bool { }) } +/// Craft patch (self-draw scope): the forms being drawn where a soft mask, a tiling pattern or a +/// Type 3 glyph was set in the content. A device may interpret any of them later, while it draws +/// another form: a soft mask set on the page is interpreted for each fill it applies to, inside +/// whatever form that fill is in. Its content is nested in the forms it was set in, so the +/// self-draw check of patch (89) runs against those (see [`with_forms_drawing`]). +#[derive(Clone, Default)] +pub(crate) struct FormsDrawing(Vec); + +impl FormsDrawing { + /// The forms being drawn now. + pub(crate) fn current() -> Self { + FORMS_DRAWING.with(|forms| Self(forms.try_borrow().map(|forms| forms.clone()).unwrap_or_default())) + } + + /// The forms, outermost first. + pub(crate) fn ids(&self) -> &[hayro_syntax::object::ObjectIdentifier] { + &self.0 + } +} + +/// Craft patch (self-draw scope): run `draw` with `forms` as the forms being drawn, and put +/// back the ones being drawn before. A form `draw` reaches is skipped only when it is among +/// `forms` or nested in itself within `draw`. +pub(crate) fn with_forms_drawing(forms: &FormsDrawing, draw: impl FnOnce() -> R) -> R { + let outer = FORMS_DRAWING.with(|stack| stack.try_borrow_mut().ok().map(|mut stack| core::mem::replace(&mut *stack, forms.0.clone()))); + let result = draw(); + if let Some(outer) = outer { + FORMS_DRAWING.with(|stack| { + if let Ok(mut stack) = stack.try_borrow_mut() { + *stack = outer; + } + }); + } + result +} + /// A cache used by the interpreter. /// /// Ideally, such a cache should be constructed once per PDF and then reused across @@ -363,7 +401,9 @@ impl<'a> Context<'a> { /// The forms being drawn are those of this page's context and of every context painting /// inside it (a tiling pattern, a Type 3 glyph, a soft mask or a group), so a form that /// reaches itself through one of those is skipped too, while a form drawn again after it - /// has finished draws as usual. + /// has finished draws as usual. A soft mask, tiling pattern or Type 3 glyph draws inside + /// the forms it was set in, wherever the device interprets it (Craft patch (self-draw + /// scope), [`FormsDrawing`]). pub(crate) fn enter_form( &mut self, id: Option, diff --git a/vendor/hayro-interpret/src/font/mod.rs b/vendor/hayro-interpret/src/font/mod.rs index e87382d976..a9ce0fe360 100644 --- a/vendor/hayro-interpret/src/font/mod.rs +++ b/vendor/hayro-interpret/src/font/mod.rs @@ -1,6 +1,7 @@ //! Interacting with the different kinds of PDF fonts. use crate::context::Context; +use crate::context::FormsDrawing; use crate::context::InterpreterCache; use crate::device::Device; use crate::font::cid::Type0Font; @@ -222,11 +223,16 @@ pub struct Type3Glyph<'a> { pub(crate) settings: InterpreterSettings, pub(crate) nesting_depth: u32, pub(crate) char_code: u32, + /// Craft patch (self-draw scope): the forms being drawn where the glyph was shown. + pub(crate) forms: FormsDrawing, } /// A glyph defined by PDF drawing instructions. impl<'a> Type3Glyph<'a> { /// Draw the type3 glyph to the given device. + /// + /// Craft patch (self-draw scope): the glyph procedure draws inside the forms the glyph was + /// shown in, wherever the device interprets it (see [`crate::SoftMask::interpret`]). pub fn interpret( &self, device: &mut impl Device<'a>, @@ -234,8 +240,10 @@ impl<'a> Type3Glyph<'a> { glyph_transform: Affine, paint: &Paint<'a>, ) { - self.font - .render_glyph(self, transform, glyph_transform, paint, device); + crate::context::with_forms_drawing(&self.forms, || { + self.font + .render_glyph(self, transform, glyph_transform, paint, device) + }); } /// Returns the Unicode code point for this glyph, if available: the `ToUnicode` cmap, then @@ -386,6 +394,7 @@ impl<'a> Font<'a> { settings: ctx.settings.clone(), nesting_depth, char_code, + forms: FormsDrawing::current(), }; Glyph::Type3(Box::new(shape_glyph)) diff --git a/vendor/hayro-interpret/src/lib.rs b/vendor/hayro-interpret/src/lib.rs index 03a0f44811..565fbc1c5d 100644 --- a/vendor/hayro-interpret/src/lib.rs +++ b/vendor/hayro-interpret/src/lib.rs @@ -40,6 +40,8 @@ mod device; mod function; mod interpret; mod ocg; +#[cfg(test)] +mod self_draw_tests; mod soft_mask; mod types; mod x_object; diff --git a/vendor/hayro-interpret/src/pattern.rs b/vendor/hayro-interpret/src/pattern.rs index bc3b99c943..288df7f54a 100644 --- a/vendor/hayro-interpret/src/pattern.rs +++ b/vendor/hayro-interpret/src/pattern.rs @@ -2,7 +2,7 @@ use crate::cache::Cache; use crate::color::{Color, ColorSpace, DefaultColorSpaces, Plate}; -use crate::context::{Context, InterpreterCache}; +use crate::context::{Context, FormsDrawing, InterpreterCache, with_forms_drawing}; use crate::device::Device; use crate::font::Glyph; use crate::interpret::state::{ActiveTransferFunction, State}; @@ -182,6 +182,8 @@ pub struct TilingPattern<'a> { pub(crate) settings: InterpreterSettings, pub(crate) xref: &'a XRef, nesting_depth: u32, + /// Craft patch (self-draw scope): the forms being drawn where the pattern was set. + forms: FormsDrawing, } impl TilingPattern<'_> { @@ -280,15 +282,28 @@ impl<'a> TilingPattern<'a> { cache: ctx.interpreter_cache.clone(), xref: ctx.xref, nesting_depth, + forms: FormsDrawing::current(), }) } /// Interpret the contents of the pattern into the given device. + /// + /// Craft patch (self-draw scope): the cell draws inside the forms the pattern was set in, + /// wherever the device interprets it (see [`SoftMask::interpret`]). pub fn interpret( &self, device: &mut impl Device<'a>, initial_transform: Affine, is_stroke: bool, + ) -> Option<()> { + with_forms_drawing(&self.forms, || self.interpret_cell(device, initial_transform, is_stroke)) + } + + fn interpret_cell( + &self, + device: &mut impl Device<'a>, + initial_transform: Affine, + is_stroke: bool, ) -> Option<()> { // PdfCraft patch: a tiling pattern can paint itself (directly, or through resources it // inherits when its own are missing), which recursed until the stack overflowed. Bound diff --git a/vendor/hayro-interpret/src/self_draw_tests.rs b/vendor/hayro-interpret/src/self_draw_tests.rs new file mode 100644 index 0000000000..e28b481f06 --- /dev/null +++ b/vendor/hayro-interpret/src/self_draw_tests.rs @@ -0,0 +1,282 @@ +//! Craft patch (self-draw scope) tests: a form that draws itself, directly or through another +//! form, a soft mask, a tiling pattern or a Type 3 glyph set inside it, is skipped and the page +//! reported truncated (patch (89)). A soft mask, tiling pattern or Type 3 glyph set outside a +//! form draws that form in full when the device interprets it while drawing the form. + +use crate::context::content_was_truncated; +use crate::font::{Glyph, Type3Glyph}; +use crate::pattern::Pattern; +use crate::{ + BlendMode, ClipPath, Context, Device, GlyphDrawMode, Image, InterpreterCache, + InterpreterSettings, InterpreterWarning, Paint, PathDrawMode, SoftMask, interpret_page, +}; +use hayro_syntax::Pdf; +use kurbo::{Affine, BezPath, Rect}; +use std::sync::{Arc, Mutex}; + +/// A PDF with one 100 by 100 page whose resources are `resources` and whose content is +/// `content`, followed by `objects` as objects 5, 6 and so on. +fn pdf(resources: &str, content: &str, objects: &[String]) -> Pdf { + let mut bodies = vec![ + "<< /Type /Catalog /Pages 2 0 R >>".to_string(), + "<< /Type /Pages /Count 1 /Kids [3 0 R] >>".to_string(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R /Resources {resources} >>"), + stream("", content), + ]; + bodies.extend_from_slice(objects); + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, body) in bodies.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n{body}\nendobj\n", i + 1).as_bytes()); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes()); + for offset in offsets { + out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice( + format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes(), + ); + Pdf::new(out).expect("synthetic pdf") +} + +fn stream(entries: &str, content: &str) -> String { + format!("<< /Length {} {entries} >>\nstream\n{content}\nendstream", content.len()) +} + +/// A form 10 by 10 that draws `content` with the resources of whatever draws it. +fn form(content: &str) -> String { + stream("/Type /XObject /Subtype /Form /BBox [0 0 10 10]", content) +} + +/// A soft mask group that draws `content` with `resources`. +fn mask_group(resources: &str, content: &str) -> String { + stream( + &format!("/Type /XObject /Subtype /Form /BBox [0 0 100 100] /Group << /S /Transparency >> /Resources {resources}"), + content, + ) +} + +/// A coloured tiling pattern whose cell draws `content` with `resources`. +fn tiling(resources: &str, content: &str) -> String { + stream( + &format!( + "/Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 10 10] /XStep 10 /YStep 10 /Resources {resources}" + ), + content, + ) +} + +/// A Type 3 font whose glyph `a` is drawn by the procedure in object `proc`, with `resources`. +fn type3(resources: &str, proc: usize) -> String { + format!( + "<< /Type /Font /Subtype /Type3 /FontBBox [0 0 1000 1000] /FontMatrix [0.001 0 0 0.001 0 0] /CharProcs << /a {proc} 0 R >> /Encoding << /Type /Encoding /Differences [97 /a] >> /FirstChar 97 /LastChar 97 /Widths [1000] /Resources {resources} >>" + ) +} + +/// The paths a device was given, by where they were drawn. +#[derive(Default, Debug, PartialEq)] +struct Drawn { + paths: usize, + in_masks: usize, + in_patterns: usize, + in_glyphs: usize, +} + +/// A device that interprets every soft mask it is given, every tiling pattern it fills with and +/// every Type 3 glyph it draws, each into a device of its own, at the time it is given them. With +/// `defer_glyphs`, it keeps the first Type 3 glyph and interprets it at the next path instead. +#[derive(Default)] +struct Recorder<'a> { + drawn: Drawn, + defer_glyphs: bool, + deferred: Option>>, +} + +impl Recorder<'_> { + fn absorb(&mut self, inner: &Recorder<'_>) -> usize { + let d = &inner.drawn; + self.drawn.in_masks += d.in_masks; + self.drawn.in_patterns += d.in_patterns; + self.drawn.in_glyphs += d.in_glyphs; + d.paths + } +} + +impl<'a> Device<'a> for Recorder<'a> { + fn set_soft_mask(&mut self, mask: Option>) { + if let Some(mask) = mask { + let mut inner = Recorder::default(); + mask.interpret(&mut inner); + self.drawn.in_masks += self.absorb(&inner); + } + } + + fn set_blend_mode(&mut self, _: BlendMode) {} + + fn draw_path(&mut self, _: &BezPath, transform: Affine, paint: &Paint<'a>, _: &PathDrawMode) { + self.drawn.paths += 1; + if let Paint::Pattern(pattern) = paint + && let Pattern::Tiling(tiling) = &**pattern + { + let mut inner = Recorder::default(); + tiling.interpret(&mut inner, transform, false); + self.drawn.in_patterns += self.absorb(&inner); + } + if let Some(glyph) = self.deferred.take() { + let mut inner = Recorder::default(); + glyph.interpret(&mut inner, Affine::IDENTITY, Affine::IDENTITY, paint); + self.drawn.in_glyphs += self.absorb(&inner); + } + } + + fn push_clip_path(&mut self, _: &ClipPath) {} + + fn push_transparency_group(&mut self, _: f32, _: Option>, _: BlendMode) {} + + fn draw_glyph(&mut self, glyph: &Glyph<'a>, transform: Affine, glyph_transform: Affine, paint: &Paint<'a>, _: &GlyphDrawMode) { + let Glyph::Type3(glyph) = glyph else { + return; + }; + if self.defer_glyphs { + self.deferred = Some(glyph.clone()); + return; + } + let mut inner = Recorder::default(); + glyph.interpret(&mut inner, transform, glyph_transform, paint); + self.drawn.in_glyphs += self.absorb(&inner); + } + + fn draw_image(&mut self, _: Image<'a, '_>, _: Affine) {} + + fn pop_clip_path(&mut self) {} + + fn pop_transparency_group(&mut self) {} +} + +/// Draws the page of `pdf` and returns what was drawn and whether the page was reported +/// truncated. +fn draw(pdf: &Pdf, defer_glyphs: bool) -> (Drawn, bool) { + let cache = InterpreterCache::new(); + let warnings = Arc::new(Mutex::new(Vec::new())); + let sink = warnings.clone(); + let settings = InterpreterSettings { + warning_sink: Arc::new(move |warning| sink.lock().expect("warnings").push(warning)), + ..InterpreterSettings::default() + }; + let mut ctx = Context::new(Affine::IDENTITY, Rect::new(0.0, 0.0, 100.0, 100.0), &cache, pdf.xref(), settings); + let mut device = Recorder { defer_glyphs, ..Recorder::default() }; + let page = &pdf.pages()[0]; + interpret_page(page, &mut ctx, &mut device); + let warnings = warnings.lock().expect("warnings"); + let truncated = warnings.iter().any(|w| matches!(w, InterpreterWarning::ContentTruncated)); + assert_eq!(truncated, content_was_truncated()); + (device.drawn, truncated) +} + +const SQUARE: &str = "0 0 10 10 re f"; + +// A form that names itself in its own resources is a reference cycle, which hayro-syntax reads +// as a missing object. The forms below have no resources of their own and reach themselves +// through the page's. + +#[test] +fn a_form_that_draws_itself_is_skipped() { + let pdf = pdf("<< /XObject << /F 5 0 R >> >>", "/F Do", &[form("0 0 10 10 re f /F Do")]); + let (drawn, truncated) = draw(&pdf, false); + assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert!(truncated); +} + +#[test] +fn a_form_that_draws_itself_through_another_form_is_skipped() { + let pdf = pdf( + "<< /XObject << /F 5 0 R /H 6 0 R >> >>", + "/F Do", + &[form("0 0 10 10 re f /H Do"), form("/F Do")], + ); + let (drawn, truncated) = draw(&pdf, false); + assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert!(truncated); +} + +#[test] +fn a_soft_mask_set_outside_a_form_draws_that_form() { + // The mask is set on the page and draws the form the page then draws; the device interprets + // it while it draws that form. + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /ExtGState << /GS << /SMask << /S /Alpha /G 6 0 R >> >> >> >>", + "/GS gs /F Do", + &[form(SQUARE), mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do")], + ); + let (drawn, truncated) = draw(&pdf, false); + assert!(drawn.in_masks > 0, "{drawn:?}"); + assert_eq!(drawn.paths, 1); + assert!(!truncated); +} + +#[test] +fn a_soft_mask_set_inside_a_form_that_it_draws_skips_that_form() { + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /ExtGState << /GS << /SMask << /S /Alpha /G 6 0 R >> >> >> >>", + "/F Do", + &[form("/GS gs 0 0 10 10 re f"), mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do")], + ); + let (drawn, truncated) = draw(&pdf, false); + assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert!(truncated); +} + +#[test] +fn a_tiling_pattern_set_outside_a_form_draws_that_form() { + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /Pattern << /P 6 0 R >> >>", + "/Pattern cs /P scn /F Do", + &[form(SQUARE), tiling("<< /XObject << /F 5 0 R >> >>", "/F Do")], + ); + let (drawn, truncated) = draw(&pdf, false); + assert!(drawn.in_patterns > 0, "{drawn:?}"); + assert!(!truncated); +} + +#[test] +fn a_tiling_pattern_set_inside_a_form_that_it_draws_skips_that_form() { + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /Pattern << /P 6 0 R >> >>", + "/F Do", + &[form("/Pattern cs /P scn 0 0 10 10 re f"), tiling("<< /XObject << /F 5 0 R >> >>", "/F Do")], + ); + let (drawn, truncated) = draw(&pdf, false); + assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert!(truncated); +} + +#[test] +fn a_type3_glyph_shown_outside_a_form_draws_that_form() { + // The device keeps the glyph shown on the page and interprets it while it draws the form. + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /Font << /T 6 0 R >> >>", + "BT /T 10 Tf (a) Tj ET /F Do", + &[form(SQUARE), type3("<< /XObject << /F 5 0 R >> >>", 7), stream("", "1000 0 d0 /F Do")], + ); + let (drawn, truncated) = draw(&pdf, true); + assert!(drawn.in_glyphs > 0, "{drawn:?}"); + assert!(!truncated); +} + +#[test] +fn a_type3_glyph_shown_inside_a_form_that_it_draws_skips_that_form() { + let pdf = pdf( + "<< /XObject << /F 5 0 R >> /Font << /T 6 0 R >> >>", + "/F Do", + &[ + form("BT /T 10 Tf (a) Tj ET 0 0 10 10 re f"), + type3("<< /XObject << /F 5 0 R >> >>", 7), + stream("", "1000 0 d0 /F Do"), + ], + ); + let (drawn, truncated) = draw(&pdf, false); + assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert!(truncated); +} diff --git a/vendor/hayro-interpret/src/soft_mask.rs b/vendor/hayro-interpret/src/soft_mask.rs index 6ecd81dbfd..1ddee0c9e2 100644 --- a/vendor/hayro-interpret/src/soft_mask.rs +++ b/vendor/hayro-interpret/src/soft_mask.rs @@ -1,5 +1,5 @@ use crate::color::{CmykSimulation, Color, ColorComponents, ColorSpace, DeviceFamily, Ink, Plate, device_cmyk_to_gray}; -use crate::context::{Context, InterpreterCache}; +use crate::context::{Context, FormsDrawing, InterpreterCache, with_forms_drawing}; use crate::device::Device; use crate::function::Function; use crate::interpret::state::State; @@ -83,6 +83,8 @@ struct Repr<'a> { device_cmyk: bool, /// Craft patch (CMYK masks): the page plate's printing condition, for [`SoftMask::plate`]. simulation: Option, + /// Craft patch (self-draw scope): the forms being drawn where the mask was set. + forms: FormsDrawing, } impl Hash for Repr<'_> { @@ -184,6 +186,7 @@ impl<'a> SoftMask<'a> { .plate .as_ref() .and_then(|p| p.simulation.clone()); + let forms = FormsDrawing::current(); let cache_key = hash128(&( simulation.as_ref().map(|s| s.digest()), dict.cache_key(), @@ -191,6 +194,8 @@ impl<'a> SoftMask<'a> { [bbox.x0.to_bits(), bbox.y0.to_bits(), bbox.x1.to_bits(), bbox.y1.to_bits()], resources_key, nesting_depth, + // Craft patch (self-draw scope): the mask skips the forms it was set in. + forms.ids(), )); Some(Self(Rc::new(Repr { @@ -216,6 +221,7 @@ impl<'a> SoftMask<'a> { nesting_depth, device_cmyk, simulation, + forms, }))) } @@ -256,10 +262,14 @@ impl<'a> SoftMask<'a> { state, self.0.nesting_depth, ); - draw_form_xobject(&self.0.parent_resources, &self.0.group, &mut ctx, device); + with_forms_drawing(&self.0.forms, || draw_form_xobject(&self.0.parent_resources, &self.0.group, &mut ctx, device)); } /// Interpret the contents of the mask into the given device. + /// + /// Craft patch (self-draw scope): the contents draw inside the forms the mask was set in, + /// so a device may interpret the mask while it draws any form: a mask that draws that form + /// draws it, and one that draws a form it was set in skips that form. pub fn interpret(&self, device: &mut impl Device<'a>) { let state = State::new(self.0.root_transform); let mut ctx = Context::new_with( @@ -272,7 +282,7 @@ impl<'a> SoftMask<'a> { self.0.nesting_depth, ); ctx.luminosity_in_device_cmyk = self.0.luminosity_in_device_cmyk; - draw_form_xobject(&self.0.parent_resources, &self.0.group, &mut ctx, device); + with_forms_drawing(&self.0.forms, || draw_form_xobject(&self.0.parent_resources, &self.0.group, &mut ctx, device)); } /// Return the object identifier of the mask. From b7b02ef2cddfddb851b178fb02ca99cfb77e43f8 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:52:30 -0800 Subject: [PATCH 2/4] hayro-interpret: American spelling in the self-draw test helper's comment --- vendor/hayro-interpret/SOURCE.json | 2 +- vendor/hayro-interpret/src/self_draw_tests.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/hayro-interpret/SOURCE.json b/vendor/hayro-interpret/SOURCE.json index 71924358dd..31ea9d136f 100644 --- a/vendor/hayro-interpret/SOURCE.json +++ b/vendor/hayro-interpret/SOURCE.json @@ -410,7 +410,7 @@ "path": "src/self_draw_tests.rs", "source_sha256": null, "source_git_blob": null, - "sha256": "58930501e624b86fd45ddc82e464a959d989d6ef94eb7e62f8f8ad24c78ec8ec" + "sha256": "84263a0741b19879104fa57fad43cc80c33ba902ba25fa2b9356fffed2e46202" }, { "path": "src/shading.rs", diff --git a/vendor/hayro-interpret/src/self_draw_tests.rs b/vendor/hayro-interpret/src/self_draw_tests.rs index e28b481f06..6cc6c2cb26 100644 --- a/vendor/hayro-interpret/src/self_draw_tests.rs +++ b/vendor/hayro-interpret/src/self_draw_tests.rs @@ -58,7 +58,7 @@ fn mask_group(resources: &str, content: &str) -> String { ) } -/// A coloured tiling pattern whose cell draws `content` with `resources`. +/// A colored tiling pattern whose cell draws `content` with `resources`. fn tiling(resources: &str, content: &str) -> String { stream( &format!( From 992edb605bfd0369ef4186a4f296e00878d794c4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 11:51:55 -0800 Subject: [PATCH 3/4] hayro-interpret: rustfmt the patch 89 tiling pattern call and self-draw tests Formats the new lines in pattern.rs and self_draw_tests.rs with rustfmt's default settings, which the vendored hayro files follow. Lines that came from main are left as they are. --- vendor/hayro-interpret/src/pattern.rs | 4 +- vendor/hayro-interpret/src/self_draw_tests.rs | 99 +++++++++++++++---- 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/vendor/hayro-interpret/src/pattern.rs b/vendor/hayro-interpret/src/pattern.rs index c6e0e70d4e..1dceae2dd3 100644 --- a/vendor/hayro-interpret/src/pattern.rs +++ b/vendor/hayro-interpret/src/pattern.rs @@ -298,7 +298,9 @@ impl<'a> TilingPattern<'a> { initial_transform: Affine, is_stroke: bool, ) -> Option<()> { - drawing_within(&self.forms_drawing, || self.interpret_cell(device, initial_transform, is_stroke)) + drawing_within(&self.forms_drawing, || { + self.interpret_cell(device, initial_transform, is_stroke) + }) } fn interpret_cell( diff --git a/vendor/hayro-interpret/src/self_draw_tests.rs b/vendor/hayro-interpret/src/self_draw_tests.rs index cf63ff7131..c3b2b5ce17 100644 --- a/vendor/hayro-interpret/src/self_draw_tests.rs +++ b/vendor/hayro-interpret/src/self_draw_tests.rs @@ -21,7 +21,9 @@ fn pdf(resources: &str, content: &str, objects: &[String]) -> Pdf { let mut bodies = vec![ "<< /Type /Catalog /Pages 2 0 R >>".to_string(), "<< /Type /Pages /Count 1 /Kids [3 0 R] >>".to_string(), - format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R /Resources {resources} >>"), + format!( + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R /Resources {resources} >>" + ), stream("", content), ]; bodies.extend_from_slice(objects); @@ -32,18 +34,27 @@ fn pdf(resources: &str, content: &str, objects: &[String]) -> Pdf { out.extend_from_slice(format!("{} 0 obj\n{body}\nendobj\n", i + 1).as_bytes()); } let xref = out.len(); - out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes()); + out.extend_from_slice( + format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes(), + ); for offset in offsets { out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); } out.extend_from_slice( - format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes(), + format!( + "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", + bodies.len() + 1 + ) + .as_bytes(), ); Pdf::new(out).expect("synthetic pdf") } fn stream(entries: &str, content: &str) -> String { - format!("<< /Length {} {entries} >>\nstream\n{content}\nendstream", content.len()) + format!( + "<< /Length {} {entries} >>\nstream\n{content}\nendstream", + content.len() + ) } /// A form 10 by 10 that draws `content` with the resources of whatever draws it. @@ -54,7 +65,9 @@ fn form(content: &str) -> String { /// A soft mask group that draws `content` with `resources`. fn mask_group(resources: &str, content: &str) -> String { stream( - &format!("/Type /XObject /Subtype /Form /BBox [0 0 100 100] /Group << /S /Transparency >> /Resources {resources}"), + &format!( + "/Type /XObject /Subtype /Form /BBox [0 0 100 100] /Group << /S /Transparency >> /Resources {resources}" + ), content, ) } @@ -136,7 +149,14 @@ impl<'a> Device<'a> for Recorder<'a> { fn push_transparency_group(&mut self, _: f32, _: Option>, _: BlendMode) {} - fn draw_glyph(&mut self, glyph: &Glyph<'a>, transform: Affine, glyph_transform: Affine, paint: &Paint<'a>, _: &GlyphDrawMode) { + fn draw_glyph( + &mut self, + glyph: &Glyph<'a>, + transform: Affine, + glyph_transform: Affine, + paint: &Paint<'a>, + _: &GlyphDrawMode, + ) { let Glyph::Type3(glyph) = glyph else { return; }; @@ -166,12 +186,23 @@ fn draw(pdf: &Pdf, defer_glyphs: bool) -> (Drawn, bool) { warning_sink: Arc::new(move |warning| sink.lock().expect("warnings").push(warning)), ..InterpreterSettings::default() }; - let mut ctx = Context::new(Affine::IDENTITY, Rect::new(0.0, 0.0, 100.0, 100.0), &cache, pdf.xref(), settings); - let mut device = Recorder { defer_glyphs, ..Recorder::default() }; + let mut ctx = Context::new( + Affine::IDENTITY, + Rect::new(0.0, 0.0, 100.0, 100.0), + &cache, + pdf.xref(), + settings, + ); + let mut device = Recorder { + defer_glyphs, + ..Recorder::default() + }; let page = &pdf.pages()[0]; interpret_page(page, &mut ctx, &mut device); let warnings = warnings.lock().expect("warnings"); - let truncated = warnings.iter().any(|w| matches!(w, InterpreterWarning::ContentTruncated)); + let truncated = warnings + .iter() + .any(|w| matches!(w, InterpreterWarning::ContentTruncated)); assert_eq!(truncated, content_was_truncated()); (device.drawn, truncated) } @@ -189,7 +220,10 @@ fn a_soft_mask_set_outside_a_form_draws_that_form() { let pdf = pdf( "<< /XObject << /F 5 0 R >> /ExtGState << /GS << /SMask << /S /Alpha /G 6 0 R >> >> >> >>", "/GS gs /F Do", - &[form(SQUARE), mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do")], + &[ + form(SQUARE), + mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do"), + ], ); let (drawn, truncated) = draw(&pdf, false); assert!(drawn.in_masks > 0, "{drawn:?}"); @@ -202,10 +236,19 @@ fn a_soft_mask_set_inside_a_form_that_it_draws_skips_that_form() { let pdf = pdf( "<< /XObject << /F 5 0 R >> /ExtGState << /GS << /SMask << /S /Alpha /G 6 0 R >> >> >> >>", "/F Do", - &[form("/GS gs 0 0 10 10 re f"), mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do")], + &[ + form("/GS gs 0 0 10 10 re f"), + mask_group("<< /XObject << /F 5 0 R >> >>", "/F Do"), + ], ); let (drawn, truncated) = draw(&pdf, false); - assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert_eq!( + drawn, + Drawn { + paths: 1, + ..Drawn::default() + } + ); assert!(!truncated); } @@ -214,7 +257,10 @@ fn a_tiling_pattern_set_outside_a_form_draws_that_form() { let pdf = pdf( "<< /XObject << /F 5 0 R >> /Pattern << /P 6 0 R >> >>", "/Pattern cs /P scn /F Do", - &[form(SQUARE), tiling("<< /XObject << /F 5 0 R >> >>", "/F Do")], + &[ + form(SQUARE), + tiling("<< /XObject << /F 5 0 R >> >>", "/F Do"), + ], ); let (drawn, truncated) = draw(&pdf, false); assert!(drawn.in_patterns > 0, "{drawn:?}"); @@ -226,10 +272,19 @@ fn a_tiling_pattern_set_inside_a_form_that_it_draws_skips_that_form() { let pdf = pdf( "<< /XObject << /F 5 0 R >> /Pattern << /P 6 0 R >> >>", "/F Do", - &[form("/Pattern cs /P scn 0 0 10 10 re f"), tiling("<< /XObject << /F 5 0 R >> >>", "/F Do")], + &[ + form("/Pattern cs /P scn 0 0 10 10 re f"), + tiling("<< /XObject << /F 5 0 R >> >>", "/F Do"), + ], ); let (drawn, truncated) = draw(&pdf, false); - assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert_eq!( + drawn, + Drawn { + paths: 1, + ..Drawn::default() + } + ); assert!(!truncated); } @@ -239,7 +294,11 @@ fn a_type3_glyph_shown_outside_a_form_draws_that_form() { let pdf = pdf( "<< /XObject << /F 5 0 R >> /Font << /T 6 0 R >> >>", "BT /T 10 Tf (a) Tj ET /F Do", - &[form(SQUARE), type3("<< /XObject << /F 5 0 R >> >>", 7), stream("", "1000 0 d0 /F Do")], + &[ + form(SQUARE), + type3("<< /XObject << /F 5 0 R >> >>", 7), + stream("", "1000 0 d0 /F Do"), + ], ); let (drawn, truncated) = draw(&pdf, true); assert!(drawn.in_glyphs > 0, "{drawn:?}"); @@ -258,6 +317,12 @@ fn a_type3_glyph_shown_inside_a_form_that_it_draws_skips_that_form() { ], ); let (drawn, truncated) = draw(&pdf, false); - assert_eq!(drawn, Drawn { paths: 1, ..Drawn::default() }); + assert_eq!( + drawn, + Drawn { + paths: 1, + ..Drawn::default() + } + ); assert!(!truncated); } From 7dd943f93abbef3d37b9f926064aa9a482fb1f42 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 30 Nov 2026 09:38:33 +0700 Subject: [PATCH 4/4] vendor/hayro-interpret: record the rustfmt'd hashes of pattern.rs and self_draw_tests.rs --- vendor/hayro-interpret/SOURCE.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/hayro-interpret/SOURCE.json b/vendor/hayro-interpret/SOURCE.json index dd0936abb9..8ad75195aa 100644 --- a/vendor/hayro-interpret/SOURCE.json +++ b/vendor/hayro-interpret/SOURCE.json @@ -404,13 +404,13 @@ "path": "src/pattern.rs", "source_sha256": "17b49a465275c20e6645dea9443f2cb3a64c594925fd860e74b80b44d5344e37", "source_git_blob": "9295f51709f8915951d7b9afca9411651d65a1f8", - "sha256": "dccd845f4d6f72c43f78fdeca0ed12064fc1e49f8991c1682bc079e2d0e9e9e8" + "sha256": "4621fe8b85f472e731e2c01d0f92ec1662c7d9d0f0e20f520fd9c926201af8d5" }, { "path": "src/self_draw_tests.rs", "source_sha256": null, "source_git_blob": null, - "sha256": "9d7c7af03161eb4ad00aaa6db8a4df3c11fa340281b2fc27c22ca83b5aa11d23" + "sha256": "1f0a93fb387d3ba51c1348ed69fe567fdb2be88ee57506400cd66644dadadb85" }, { "path": "src/shading.rs",