Skip to main content

pdf_interpret/
pattern.rs

1//! PDF patterns.
2
3use crate::WarningSinkFn;
4use crate::cache::Cache;
5use crate::color::{Color, ColorSpace};
6use crate::context::Context;
7use crate::device::Device;
8use crate::font::Glyph;
9use crate::interpret::state::{ActiveTransferFunction, State};
10use crate::shading::Shading;
11use crate::soft_mask::SoftMask;
12use crate::util::{Float32Ext, RectExt, decode_or_warn, hash128};
13use crate::{BlendMode, CacheKey, ClipPath, GlyphDrawMode, Image, PathDrawMode};
14use crate::{FillRule, InterpreterSettings, Paint, interpret};
15use kurbo::{Affine, BezPath, Rect, Shape};
16use log::warn;
17use pdf_syntax::content::TypedIter;
18use pdf_syntax::object::Dict;
19use pdf_syntax::object::Stream;
20use pdf_syntax::object::dict::keys::{
21    BBOX, EXT_G_STATE, MATRIX, PAINT_TYPE, RESOURCES, SHADING, X_STEP, Y_STEP,
22};
23use pdf_syntax::object::{Object, dict_or_stream};
24use pdf_syntax::page::Resources;
25use pdf_syntax::xref::XRef;
26use std::fmt::{Debug, Formatter};
27use std::sync::Arc;
28
29/// A PDF pattern.
30#[derive(Debug, Clone)]
31pub enum Pattern<'a> {
32    /// A shading pattern.
33    Shading(ShadingPattern),
34    /// A tiling pattern.
35    Tiling(Box<TilingPattern<'a>>),
36}
37
38impl<'a> Pattern<'a> {
39    pub(crate) fn new(
40        object: Object<'a>,
41        ctx: &Context<'a>,
42        resources: &Resources<'a>,
43    ) -> Option<Self> {
44        if let Some(dict) = object.clone().into_dict() {
45            Some(Self::Shading(ShadingPattern::new(
46                &dict,
47                &ctx.object_cache,
48                ctx.get().graphics_state.non_stroke_alpha,
49                &ctx.settings.warning_sink,
50            )?))
51        } else if let Some(stream) = object.clone().into_stream() {
52            Some(Self::Tiling(Box::new(TilingPattern::new(
53                stream, ctx, resources,
54            )?)))
55        } else {
56            None
57        }
58    }
59
60    pub(crate) fn pre_concat_transform(&mut self, transform: Affine) {
61        match self {
62            Self::Shading(p) => {
63                p.matrix = transform * p.matrix;
64                let transformed_clip_path = p.shading.clip_path.clone().map(|r| p.matrix * r);
65                Arc::make_mut(&mut p.shading).clip_path = transformed_clip_path;
66            }
67            Self::Tiling(p) => p.matrix = transform * p.matrix,
68        }
69    }
70
71    pub(crate) fn set_transfer_function(&mut self, tf: ActiveTransferFunction) {
72        if let Self::Shading(p) = self {
73            p.transfer_function = Some(tf);
74        }
75    }
76}
77
78impl CacheKey for Pattern<'_> {
79    fn cache_key(&self) -> u128 {
80        match self {
81            Self::Shading(p) => p.cache_key(),
82            Self::Tiling(p) => p.cache_key(),
83        }
84    }
85}
86
87/// A shading pattern.
88#[derive(Clone, Debug)]
89pub struct ShadingPattern {
90    /// The underlying shading of the pattern.
91    pub shading: Arc<Shading>,
92    /// A transformation matrix to apply prior to rendering.
93    pub matrix: Affine,
94    /// An additional opacity to apply to the shading pattern.
95    pub opacity: f32,
96    /// An optional transfer function to apply to the shading's output colors.
97    pub transfer_function: Option<ActiveTransferFunction>,
98}
99
100impl ShadingPattern {
101    pub(crate) fn new(
102        dict: &Dict<'_>,
103        cache: &Cache,
104        opacity: f32,
105        warning_sink: &WarningSinkFn,
106    ) -> Option<Self> {
107        let shading = dict.get::<Object<'_>>(SHADING).and_then(|o| {
108            let (dict, stream) = dict_or_stream(&o)?;
109
110            Shading::new(&dict, stream.as_ref(), cache, warning_sink)
111        })?;
112        let matrix = dict
113            .get::<[f64; 6]>(MATRIX)
114            .map(Affine::new)
115            .unwrap_or_default();
116
117        if dict.contains_key(EXT_G_STATE) {
118            warn!("shading patterns with ext_g_state are not supported yet");
119        }
120
121        Some(Self {
122            shading: Arc::new(shading),
123            opacity,
124            matrix,
125            transfer_function: None,
126        })
127    }
128}
129
130impl CacheKey for ShadingPattern {
131    fn cache_key(&self) -> u128 {
132        hash128(&(self.shading.cache_key(), self.matrix.cache_key()))
133    }
134}
135
136/// A tiling pattern.
137#[derive(Clone)]
138pub struct TilingPattern<'a> {
139    cache_key: u128,
140    ctx_bbox: Rect,
141    /// The bbox of the tiling pattern.
142    pub bbox: Rect,
143    /// The step in the x direction.
144    pub x_step: f32,
145    /// The step in the y direction.
146    pub y_step: f32,
147    /// A transformation to apply prior to rendering.
148    pub matrix: Affine,
149    stream: Stream<'a>,
150    /// Whether this is a colored (`PaintType 1`) tiling pattern.
151    pub is_color: bool,
152    /// For colored tiling patterns: the non-stroking opacity from the parent
153    /// graphics state at the time the pattern was set. PDF ยง8.7.3.1 states
154    /// that the current graphics state (including opacity) applies when a
155    /// tiling pattern is painted, so we capture it here for later use.
156    pub opacity: f32,
157    pub(crate) stroke_paint: Color,
158    pub(crate) non_stroking_paint: Color,
159    pub(crate) parent_resources: Resources<'a>,
160    pub(crate) cache: Cache,
161    pub(crate) settings: InterpreterSettings,
162    pub(crate) xref: &'a XRef,
163}
164
165impl Debug for TilingPattern<'_> {
166    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
167        f.write_str("TilingPattern")
168    }
169}
170
171impl<'a> TilingPattern<'a> {
172    pub(crate) fn new(
173        stream: Stream<'a>,
174        ctx: &Context<'a>,
175        resources: &Resources<'a>,
176    ) -> Option<Self> {
177        let cache_key = stream.cache_key();
178        let dict = stream.dict();
179
180        let bbox = dict.get::<pdf_syntax::object::Rect>(BBOX)?.to_kurbo();
181        let x_step = dict.get::<f32>(X_STEP)?;
182        let y_step = dict.get::<f32>(Y_STEP)?;
183
184        if x_step.is_nearly_zero() || y_step.is_nearly_zero() || bbox.is_zero_area() {
185            return None;
186        }
187
188        let is_color = dict.get::<u8>(PAINT_TYPE)? == 1;
189        let matrix = dict
190            .get::<[f64; 6]>(MATRIX)
191            .map(Affine::new)
192            .unwrap_or_default();
193
194        let state = ctx.get().clone();
195        let ctx_bbox = ctx.bbox();
196
197        let fill_cs = state
198            .graphics_state
199            .none_stroke_cs
200            .pattern_cs()
201            .unwrap_or(ColorSpace::device_gray());
202        let stroke_cs = state
203            .graphics_state
204            .stroke_cs
205            .pattern_cs()
206            .unwrap_or(ColorSpace::device_gray());
207
208        let non_stroke_alpha = state.graphics_state.non_stroke_alpha;
209        let stroke_alpha = state.graphics_state.stroke_alpha;
210
211        let non_stroking_paint = Color::new(
212            fill_cs,
213            state.graphics_state.non_stroke_color.clone(),
214            non_stroke_alpha,
215        );
216        let stroke_paint = Color::new(
217            stroke_cs,
218            state.graphics_state.stroke_color.clone(),
219            stroke_alpha,
220        );
221
222        // For colored tiling patterns the fill opacity controls transparency
223        // of the whole tile; for uncolored patterns it is already embedded in
224        // the paint colors above, so we record it but use it conditionally.
225        let opacity = non_stroke_alpha;
226
227        Some(Self {
228            cache_key,
229            bbox,
230            x_step,
231            y_step,
232            matrix,
233            ctx_bbox,
234            is_color,
235            stream,
236            opacity,
237            stroke_paint,
238            non_stroking_paint,
239            settings: ctx.settings.clone(),
240            parent_resources: resources.clone(),
241            cache: ctx.object_cache.clone(),
242            xref: ctx.xref,
243        })
244    }
245
246    /// Interpret the contents of the pattern into the given device.
247    pub fn interpret(
248        &self,
249        device: &mut impl Device<'a>,
250        initial_transform: Affine,
251        is_stroke: bool,
252    ) -> Option<()> {
253        let state = State::new(initial_transform);
254
255        let mut context = Context::new_with(
256            state.ctm,
257            // TODO: bbox?
258            (initial_transform * self.ctx_bbox.to_path(0.1)).bounding_box(),
259            self.cache.clone(),
260            self.xref,
261            self.settings.clone(),
262            state,
263        );
264
265        let decoded = decode_or_warn(&self.stream, &self.settings.warning_sink)?;
266        let resources = Resources::from_parent(
267            self.stream.dict().get(RESOURCES).unwrap_or_default(),
268            self.parent_resources.clone(),
269        );
270        let iter = TypedIter::new(decoded.as_ref());
271
272        let clip_path = ClipPath {
273            path: initial_transform * self.bbox.to_path(0.1),
274            fill: FillRule::NonZero,
275        };
276        device.push_clip_path(&clip_path);
277
278        if self.is_color {
279            interpret(iter, &resources, &mut context, device);
280        } else {
281            let paint = if !is_stroke {
282                Paint::Color(self.non_stroking_paint.clone())
283            } else {
284                Paint::Color(self.stroke_paint.clone())
285            };
286
287            let mut device = StencilPatternDevice::new(device, paint.clone());
288            interpret(iter, &resources, &mut context, &mut device);
289        }
290
291        device.pop_clip_path();
292
293        Some(())
294    }
295}
296
297impl CacheKey for TilingPattern<'_> {
298    fn cache_key(&self) -> u128 {
299        self.cache_key
300    }
301}
302
303struct StencilPatternDevice<'a, 'b, T: Device<'a>> {
304    inner: &'b mut T,
305    paint: Paint<'a>,
306}
307
308impl<'a, 'b, T: Device<'a>> StencilPatternDevice<'a, 'b, T> {
309    pub(crate) fn new(device: &'b mut T, paint: Paint<'a>) -> Self {
310        Self {
311            inner: device,
312            paint,
313        }
314    }
315}
316
317// Only filling, stroking of paths and stencil masks are allowed.
318impl<'a, T: Device<'a>> Device<'a> for StencilPatternDevice<'a, '_, T> {
319    fn draw_path(
320        &mut self,
321        path: &BezPath,
322        transform: Affine,
323        _: &Paint<'_>,
324        draw_mode: &PathDrawMode,
325    ) {
326        self.inner
327            .draw_path(path, transform, &self.paint, draw_mode);
328    }
329
330    fn set_soft_mask(&mut self, _: Option<SoftMask<'_>>) {}
331
332    fn push_clip_path(&mut self, clip_path: &ClipPath) {
333        self.inner.push_clip_path(clip_path);
334    }
335
336    fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'_>>, _: BlendMode) {}
337
338    fn draw_glyph(
339        &mut self,
340        g: &Glyph<'a>,
341        transform: Affine,
342        glyph_transform: Affine,
343        p: &Paint<'a>,
344        draw_mode: &GlyphDrawMode,
345    ) {
346        self.inner
347            .draw_glyph(g, transform, glyph_transform, p, draw_mode);
348    }
349
350    fn draw_image(&mut self, image: Image<'a, '_>, transform: Affine) {
351        if let Image::Stencil(mut s) = image {
352            s.paint = self.paint.clone();
353            self.inner.draw_image(Image::Stencil(s), transform);
354        }
355    }
356
357    fn pop_clip_path(&mut self) {
358        self.inner.pop_clip_path();
359    }
360
361    fn pop_transparency_group(&mut self) {}
362
363    fn set_blend_mode(&mut self, _: BlendMode) {}
364}