Skip to main content

pdf_interpret/
util.rs

1//! A number of utility methods.
2
3use crate::{InterpreterWarning, WarningSinkFn};
4use kurbo::{Affine, BezPath, PathEl, Rect};
5use log::warn;
6use pdf_syntax::object::Stream;
7use pdf_syntax::object::stream::DecodeFailure;
8use pdf_syntax::page::{Page, Rotation};
9use siphasher::sip128::{Hasher128, SipHasher13};
10use std::hash::Hash;
11use std::ops::Sub;
12
13pub(crate) fn decode_or_warn(stream: &Stream<'_>, sink: &WarningSinkFn) -> Option<Vec<u8>> {
14    match stream.decoded() {
15        Ok(data) => Some(data),
16        Err(DecodeFailure::StreamTooLarge { observed, limit }) => {
17            sink(InterpreterWarning::StreamTooLarge { observed, limit });
18            None
19        }
20        Err(_) => None,
21    }
22}
23
24pub(crate) trait OptionLog {
25    fn warn_none(self, f: &str) -> Self;
26}
27
28impl<T> OptionLog for Option<T> {
29    #[inline]
30    fn warn_none(self, f: &str) -> Self {
31        self.or_else(|| {
32            warn!("{f}");
33
34            None
35        })
36    }
37}
38
39const SCALAR_NEARLY_ZERO: f32 = 1.0 / (1 << 8) as f32;
40
41/// A number of useful methods for f32 numbers.
42pub trait Float32Ext: Sized + Sub<f32, Output = f32> + Copy + PartialOrd<f32> {
43    /// Whether the number is approximately 0.
44    fn is_nearly_zero(&self) -> bool {
45        self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO)
46    }
47
48    /// Whether the number is nearly equal to another number.
49    fn is_nearly_equal(&self, other: f32) -> bool {
50        (*self - other).is_nearly_zero()
51    }
52
53    /// Whether the number is nearly equal to another number.
54    fn is_nearly_less_or_equal(&self, other: f32) -> bool {
55        (*self - other).is_nearly_zero() || *self < other
56    }
57
58    /// Whether the number is nearly equal to another number.
59    fn is_nearly_greater_or_equal(&self, other: f32) -> bool {
60        (*self - other).is_nearly_zero() || *self > other
61    }
62
63    /// Whether the number is approximately 0, with a given tolerance.
64    fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool;
65}
66
67impl Float32Ext for f32 {
68    fn is_nearly_zero_within_tolerance(&self, tolerance: f32) -> bool {
69        debug_assert!(tolerance >= 0.0, "tolerance must be non-negative");
70
71        self.abs() <= tolerance
72    }
73}
74
75/// A number of useful methods for f64 numbers.
76pub trait Float64Ext: Sized + Sub<f64, Output = f64> + Copy + PartialOrd<f64> {
77    /// Whether the number is approximately 0.
78    fn is_nearly_zero(&self) -> bool {
79        self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO as f64)
80    }
81
82    /// Whether the number is nearly equal to another number.
83    fn is_nearly_equal(&self, other: f64) -> bool {
84        (*self - other).is_nearly_zero()
85    }
86
87    /// Whether the number is nearly equal to another number.
88    fn is_nearly_less_or_equal(&self, other: f64) -> bool {
89        (*self - other).is_nearly_zero() || *self < other
90    }
91
92    /// Whether the number is nearly equal to another number.
93    fn is_nearly_greater_or_equal(&self, other: f64) -> bool {
94        (*self - other).is_nearly_zero() || *self > other
95    }
96
97    /// Whether the number is approximately 0, with a given tolerance.
98    fn is_nearly_zero_within_tolerance(&self, tolerance: f64) -> bool;
99}
100
101impl Float64Ext for f64 {
102    fn is_nearly_zero_within_tolerance(&self, tolerance: f64) -> bool {
103        debug_assert!(tolerance >= 0.0, "tolerance must be non-negative");
104
105        self.abs() <= tolerance
106    }
107}
108
109pub(crate) trait PointExt: Sized {
110    fn x(&self) -> f32;
111    fn y(&self) -> f32;
112
113    fn nearly_same(&self, other: Self) -> bool {
114        self.x().is_nearly_equal(other.x()) && self.y().is_nearly_equal(other.y())
115    }
116}
117
118impl PointExt for kurbo::Point {
119    fn x(&self) -> f32 {
120        self.x as f32
121    }
122
123    fn y(&self) -> f32 {
124        self.y as f32
125    }
126}
127
128/// Calculate a 128-bit siphash of a value.
129pub(crate) fn hash128<T: Hash + ?Sized>(value: &T) -> u128 {
130    let mut state = SipHasher13::new();
131    value.hash(&mut state);
132    state.finish128().as_u128()
133}
134
135pub(crate) trait BezPathExt {
136    fn fast_bounding_box(&self) -> Rect;
137}
138
139impl BezPathExt for BezPath {
140    fn fast_bounding_box(&self) -> Rect {
141        let mut min_x = f64::INFINITY;
142        let mut min_y = f64::INFINITY;
143        let mut max_x = f64::NEG_INFINITY;
144        let mut max_y = f64::NEG_INFINITY;
145
146        let mut include = |x: f64, y: f64| {
147            min_x = min_x.min(x);
148            min_y = min_y.min(y);
149            max_x = max_x.max(x);
150            max_y = max_y.max(y);
151        };
152
153        for el in self.elements() {
154            match *el {
155                PathEl::MoveTo(p) | PathEl::LineTo(p) => include(p.x, p.y),
156                PathEl::QuadTo(p1, p2) => {
157                    include(p1.x, p1.y);
158                    include(p2.x, p2.y);
159                }
160                PathEl::CurveTo(p1, p2, p3) => {
161                    include(p1.x, p1.y);
162                    include(p2.x, p2.y);
163                    include(p3.x, p3.y);
164                }
165                PathEl::ClosePath => {}
166            }
167        }
168
169        if min_x > max_x {
170            Rect::ZERO
171        } else {
172            Rect::new(min_x, min_y, max_x, max_y)
173        }
174    }
175}
176
177/// Extension methods for rectangles.
178pub trait RectExt {
179    /// Convert the rectangle to a `kurbo` rectangle.
180    fn to_kurbo(&self) -> Rect;
181}
182
183impl RectExt for pdf_syntax::object::Rect {
184    fn to_kurbo(&self) -> Rect {
185        Rect::new(self.x0, self.y0, self.x1, self.y1)
186    }
187}
188
189// Note: Keep in sync with `pdf-interpret-write`.
190/// Extension methods for PDF pages.
191pub trait PageExt {
192    /// Return the initial transform that should be applied when rendering. This accounts for a
193    /// number of factors, such as the mismatch between PDF's y-up and most renderers' y-down
194    /// coordinate system, the rotation of the page and the offset of the crop box.
195    fn initial_transform(&self, invert_y: bool) -> Affine;
196}
197
198impl PageExt for Page<'_> {
199    fn initial_transform(&self, invert_y: bool) -> Affine {
200        // Use the raw CropBox origin for the coordinate-system translation.
201        // MuPDF maps (CropBox.x0, CropBox.y0) → canvas (0, 0).  For normal
202        // PDFs (CropBox ⊆ MediaBox) intersected_crop_box and crop_box share
203        // the same origin, so there is no change.  For unusual documents where
204        // CropBox extends beyond MediaBox (e.g. gen-802: CropBox=[0,0,684,864]
205        // but MediaBox=[36,36,648,828]) the intersected origin was (36,36),
206        // producing a 75-pixel content offset vs MuPDF. (#544 follow-up)
207        let crop_box = self.crop_box();
208        let (_, base_height) = self.base_dimensions();
209        let (width, height) = self.render_dimensions();
210
211        let horizontal_t =
212            Affine::rotate(90.0_f64.to_radians()) * Affine::translate((0.0, -width as f64));
213        let flipped_horizontal_t =
214            Affine::translate((0.0, height as f64)) * Affine::rotate(-90.0_f64.to_radians());
215
216        let rotation_transform = match self.rotation() {
217            Rotation::None => Affine::IDENTITY,
218            Rotation::Horizontal => {
219                if invert_y {
220                    horizontal_t
221                } else {
222                    flipped_horizontal_t
223                }
224            }
225            Rotation::Flipped => {
226                Affine::scale(-1.0) * Affine::translate((-width as f64, -height as f64))
227            }
228            Rotation::FlippedHorizontal => {
229                if invert_y {
230                    flipped_horizontal_t
231                } else {
232                    horizontal_t
233                }
234            }
235        };
236
237        let inversion_transform = if invert_y {
238            Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, base_height as f64])
239        } else {
240            Affine::IDENTITY
241        };
242
243        rotation_transform * inversion_transform * Affine::translate((-crop_box.x0, -crop_box.y0))
244    }
245}