1use strict_num::ApproxEqUlps;
5use svgtypes::{Align, AspectRatio};
6pub use tiny_skia_path::{NonZeroRect, Rect, Size, Transform};
7
8pub trait ApproxZeroUlps: ApproxEqUlps {
10 fn approx_zero_ulps(&self, ulps: <Self::Flt as strict_num::Ulps>::U) -> bool;
12}
13
14impl ApproxZeroUlps for f32 {
15 fn approx_zero_ulps(&self, ulps: i32) -> bool {
16 self.approx_eq_ulps(&0.0, ulps)
17 }
18}
19
20impl ApproxZeroUlps for f64 {
21 fn approx_zero_ulps(&self, ulps: i64) -> bool {
22 self.approx_eq_ulps(&0.0, ulps)
23 }
24}
25
26pub(crate) trait IsValidLength {
28 fn is_valid_length(&self) -> bool;
30}
31
32impl IsValidLength for f32 {
33 #[inline]
34 fn is_valid_length(&self) -> bool {
35 *self > 0.0 && self.is_finite()
36 }
37}
38
39impl IsValidLength for f64 {
40 #[inline]
41 fn is_valid_length(&self) -> bool {
42 *self > 0.0 && self.is_finite()
43 }
44}
45
46#[derive(Clone, Copy, Debug)]
48pub(crate) struct ViewBox {
49 pub rect: NonZeroRect,
51
52 pub aspect: AspectRatio,
54}
55
56impl ViewBox {
57 pub fn to_transform(&self, img_size: Size) -> Transform {
59 let vr = self.rect;
60
61 let sx = img_size.width() / vr.width();
62 let sy = img_size.height() / vr.height();
63
64 let (sx, sy) = if self.aspect.align == Align::None {
65 (sx, sy)
66 } else {
67 let s = if self.aspect.slice {
68 if sx < sy { sy } else { sx }
69 } else {
70 if sx > sy { sy } else { sx }
71 };
72
73 (s, s)
74 };
75
76 let x = -vr.x() * sx;
77 let y = -vr.y() * sy;
78 let w = img_size.width() - vr.width() * sx;
79 let h = img_size.height() - vr.height() * sy;
80
81 let (tx, ty) = aligned_pos(self.aspect.align, x, y, w, h);
82 Transform::from_row(sx, 0.0, 0.0, sy, tx, ty)
83 }
84}
85
86#[derive(Clone, Copy, Debug)]
88pub(crate) struct BBox {
89 left: f32,
90 top: f32,
91 right: f32,
92 bottom: f32,
93}
94
95impl From<Rect> for BBox {
96 fn from(r: Rect) -> Self {
97 Self {
98 left: r.left(),
99 top: r.top(),
100 right: r.right(),
101 bottom: r.bottom(),
102 }
103 }
104}
105
106impl From<NonZeroRect> for BBox {
107 fn from(r: NonZeroRect) -> Self {
108 Self {
109 left: r.left(),
110 top: r.top(),
111 right: r.right(),
112 bottom: r.bottom(),
113 }
114 }
115}
116
117impl Default for BBox {
118 fn default() -> Self {
119 Self {
120 left: f32::MAX,
121 top: f32::MAX,
122 right: f32::MIN,
123 bottom: f32::MIN,
124 }
125 }
126}
127
128impl BBox {
129 pub fn is_default(&self) -> bool {
131 self.left == f32::MAX
132 && self.top == f32::MAX
133 && self.right == f32::MIN
134 && self.bottom == f32::MIN
135 }
136
137 #[must_use]
139 pub fn expand(&self, r: impl Into<Self>) -> Self {
140 self.expand_impl(r.into())
141 }
142
143 fn expand_impl(&self, r: Self) -> Self {
144 Self {
145 left: self.left.min(r.left),
146 top: self.top.min(r.top),
147 right: self.right.max(r.right),
148 bottom: self.bottom.max(r.bottom),
149 }
150 }
151
152 pub fn to_rect(&self) -> Option<Rect> {
154 if !self.is_default() {
155 Rect::from_ltrb(self.left, self.top, self.right, self.bottom)
156 } else {
157 None
158 }
159 }
160
161 pub fn to_non_zero_rect(&self) -> Option<NonZeroRect> {
163 if !self.is_default() {
164 NonZeroRect::from_ltrb(self.left, self.top, self.right, self.bottom)
165 } else {
166 None
167 }
168 }
169}
170
171pub(crate) fn aligned_pos(align: Align, x: f32, y: f32, w: f32, h: f32) -> (f32, f32) {
173 match align {
174 Align::None => (x, y),
175 Align::XMinYMin => (x, y),
176 Align::XMidYMin => (x + w / 2.0, y),
177 Align::XMaxYMin => (x + w, y),
178 Align::XMinYMid => (x, y + h / 2.0),
179 Align::XMidYMid => (x + w / 2.0, y + h / 2.0),
180 Align::XMaxYMid => (x + w, y + h / 2.0),
181 Align::XMinYMax => (x, y + h),
182 Align::XMidYMax => (x + w / 2.0, y + h),
183 Align::XMaxYMax => (x + w, y + h),
184 }
185}