1use 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
41pub trait Float32Ext: Sized + Sub<f32, Output = f32> + Copy + PartialOrd<f32> {
43 fn is_nearly_zero(&self) -> bool {
45 self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO)
46 }
47
48 fn is_nearly_equal(&self, other: f32) -> bool {
50 (*self - other).is_nearly_zero()
51 }
52
53 fn is_nearly_less_or_equal(&self, other: f32) -> bool {
55 (*self - other).is_nearly_zero() || *self < other
56 }
57
58 fn is_nearly_greater_or_equal(&self, other: f32) -> bool {
60 (*self - other).is_nearly_zero() || *self > other
61 }
62
63 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
75pub trait Float64Ext: Sized + Sub<f64, Output = f64> + Copy + PartialOrd<f64> {
77 fn is_nearly_zero(&self) -> bool {
79 self.is_nearly_zero_within_tolerance(SCALAR_NEARLY_ZERO as f64)
80 }
81
82 fn is_nearly_equal(&self, other: f64) -> bool {
84 (*self - other).is_nearly_zero()
85 }
86
87 fn is_nearly_less_or_equal(&self, other: f64) -> bool {
89 (*self - other).is_nearly_zero() || *self < other
90 }
91
92 fn is_nearly_greater_or_equal(&self, other: f64) -> bool {
94 (*self - other).is_nearly_zero() || *self > other
95 }
96
97 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
128pub(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
177pub trait RectExt {
179 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
189pub trait PageExt {
192 fn initial_transform(&self, invert_y: bool) -> Affine;
196}
197
198impl PageExt for Page<'_> {
199 fn initial_transform(&self, invert_y: bool) -> Affine {
200 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}