1 | // Copyright 2019 the Kurbo Authors |
2 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
3 | |
4 | //! A 2D size. |
5 | |
6 | use core::fmt; |
7 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; |
8 | |
9 | use crate::common::FloatExt; |
10 | use crate::{Rect, RoundedRect, RoundedRectRadii, Vec2}; |
11 | |
12 | #[cfg (not(feature = "std" ))] |
13 | use crate::common::FloatFuncs; |
14 | |
15 | /// A 2D size. |
16 | #[derive (Clone, Copy, Default, PartialEq)] |
17 | #[cfg_attr (feature = "schemars" , derive(schemars::JsonSchema))] |
18 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
19 | pub struct Size { |
20 | /// The width. |
21 | pub width: f64, |
22 | /// The height. |
23 | pub height: f64, |
24 | } |
25 | |
26 | impl Size { |
27 | /// A size with zero width or height. |
28 | pub const ZERO: Size = Size::new(0., 0.); |
29 | |
30 | /// Create a new `Size` with the provided `width` and `height`. |
31 | #[inline ] |
32 | pub const fn new(width: f64, height: f64) -> Self { |
33 | Size { width, height } |
34 | } |
35 | |
36 | /// Returns the max of `width` and `height`. |
37 | /// |
38 | /// # Examples |
39 | /// |
40 | /// ``` |
41 | /// use kurbo::Size; |
42 | /// let size = Size::new(-10.5, 42.0); |
43 | /// assert_eq!(size.max_side(), 42.0); |
44 | /// ``` |
45 | pub fn max_side(self) -> f64 { |
46 | self.width.max(self.height) |
47 | } |
48 | |
49 | /// Returns the min of `width` and `height`. |
50 | /// |
51 | /// # Examples |
52 | /// |
53 | /// ``` |
54 | /// use kurbo::Size; |
55 | /// let size = Size::new(-10.5, 42.0); |
56 | /// assert_eq!(size.min_side(), -10.5); |
57 | /// ``` |
58 | pub fn min_side(self) -> f64 { |
59 | self.width.min(self.height) |
60 | } |
61 | |
62 | /// The area covered by this size. |
63 | #[inline ] |
64 | pub fn area(self) -> f64 { |
65 | self.width * self.height |
66 | } |
67 | |
68 | /// Whether this size has zero area. |
69 | #[doc (alias = "is_empty" )] |
70 | #[inline ] |
71 | pub fn is_zero_area(self) -> bool { |
72 | self.area() == 0.0 |
73 | } |
74 | |
75 | /// Whether this size has zero area. |
76 | /// |
77 | /// Note: a size with negative area is not considered empty. |
78 | #[inline ] |
79 | #[deprecated (since = "0.11.1" , note = "use is_zero_area instead" )] |
80 | pub fn is_empty(self) -> bool { |
81 | self.is_zero_area() |
82 | } |
83 | |
84 | /// Returns a new size bounded by `min` and `max.` |
85 | /// |
86 | /// # Examples |
87 | /// |
88 | /// ``` |
89 | /// use kurbo::Size; |
90 | /// |
91 | /// let this = Size::new(0., 100.); |
92 | /// let min = Size::new(10., 10.,); |
93 | /// let max = Size::new(50., 50.); |
94 | /// assert_eq!(this.clamp(min, max), Size::new(10., 50.)) |
95 | /// ``` |
96 | pub fn clamp(self, min: Size, max: Size) -> Self { |
97 | let width = self.width.max(min.width).min(max.width); |
98 | let height = self.height.max(min.height).min(max.height); |
99 | Size { width, height } |
100 | } |
101 | |
102 | /// Convert this size into a [`Vec2`], with `width` mapped to `x` and `height` |
103 | /// mapped to `y`. |
104 | #[inline ] |
105 | pub const fn to_vec2(self) -> Vec2 { |
106 | Vec2::new(self.width, self.height) |
107 | } |
108 | |
109 | /// Returns a new `Size`, |
110 | /// with `width` and `height` [rounded] to the nearest integer. |
111 | /// |
112 | /// # Examples |
113 | /// |
114 | /// ``` |
115 | /// use kurbo::Size; |
116 | /// let size_pos = Size::new(3.3, 3.6).round(); |
117 | /// assert_eq!(size_pos.width, 3.0); |
118 | /// assert_eq!(size_pos.height, 4.0); |
119 | /// let size_neg = Size::new(-3.3, -3.6).round(); |
120 | /// assert_eq!(size_neg.width, -3.0); |
121 | /// assert_eq!(size_neg.height, -4.0); |
122 | /// ``` |
123 | /// |
124 | /// [rounded]: f64::round |
125 | #[inline ] |
126 | pub fn round(self) -> Size { |
127 | Size::new(self.width.round(), self.height.round()) |
128 | } |
129 | |
130 | /// Returns a new `Size`, |
131 | /// with `width` and `height` [rounded up] to the nearest integer, |
132 | /// unless they are already an integer. |
133 | /// |
134 | /// # Examples |
135 | /// |
136 | /// ``` |
137 | /// use kurbo::Size; |
138 | /// let size_pos = Size::new(3.3, 3.6).ceil(); |
139 | /// assert_eq!(size_pos.width, 4.0); |
140 | /// assert_eq!(size_pos.height, 4.0); |
141 | /// let size_neg = Size::new(-3.3, -3.6).ceil(); |
142 | /// assert_eq!(size_neg.width, -3.0); |
143 | /// assert_eq!(size_neg.height, -3.0); |
144 | /// ``` |
145 | /// |
146 | /// [rounded up]: f64::ceil |
147 | #[inline ] |
148 | pub fn ceil(self) -> Size { |
149 | Size::new(self.width.ceil(), self.height.ceil()) |
150 | } |
151 | |
152 | /// Returns a new `Size`, |
153 | /// with `width` and `height` [rounded down] to the nearest integer, |
154 | /// unless they are already an integer. |
155 | /// |
156 | /// # Examples |
157 | /// |
158 | /// ``` |
159 | /// use kurbo::Size; |
160 | /// let size_pos = Size::new(3.3, 3.6).floor(); |
161 | /// assert_eq!(size_pos.width, 3.0); |
162 | /// assert_eq!(size_pos.height, 3.0); |
163 | /// let size_neg = Size::new(-3.3, -3.6).floor(); |
164 | /// assert_eq!(size_neg.width, -4.0); |
165 | /// assert_eq!(size_neg.height, -4.0); |
166 | /// ``` |
167 | /// |
168 | /// [rounded down]: f64::floor |
169 | #[inline ] |
170 | pub fn floor(self) -> Size { |
171 | Size::new(self.width.floor(), self.height.floor()) |
172 | } |
173 | |
174 | /// Returns a new `Size`, |
175 | /// with `width` and `height` [rounded away] from zero to the nearest integer, |
176 | /// unless they are already an integer. |
177 | /// |
178 | /// # Examples |
179 | /// |
180 | /// ``` |
181 | /// use kurbo::Size; |
182 | /// let size_pos = Size::new(3.3, 3.6).expand(); |
183 | /// assert_eq!(size_pos.width, 4.0); |
184 | /// assert_eq!(size_pos.height, 4.0); |
185 | /// let size_neg = Size::new(-3.3, -3.6).expand(); |
186 | /// assert_eq!(size_neg.width, -4.0); |
187 | /// assert_eq!(size_neg.height, -4.0); |
188 | /// ``` |
189 | /// |
190 | /// [rounded away]: FloatExt::expand |
191 | #[inline ] |
192 | pub fn expand(self) -> Size { |
193 | Size::new(self.width.expand(), self.height.expand()) |
194 | } |
195 | |
196 | /// Returns a new `Size`, |
197 | /// with `width` and `height` [rounded towards] zero to the nearest integer, |
198 | /// unless they are already an integer. |
199 | /// |
200 | /// # Examples |
201 | /// |
202 | /// ``` |
203 | /// use kurbo::Size; |
204 | /// let size_pos = Size::new(3.3, 3.6).trunc(); |
205 | /// assert_eq!(size_pos.width, 3.0); |
206 | /// assert_eq!(size_pos.height, 3.0); |
207 | /// let size_neg = Size::new(-3.3, -3.6).trunc(); |
208 | /// assert_eq!(size_neg.width, -3.0); |
209 | /// assert_eq!(size_neg.height, -3.0); |
210 | /// ``` |
211 | /// |
212 | /// [rounded towards]: f64::trunc |
213 | #[inline ] |
214 | pub fn trunc(self) -> Size { |
215 | Size::new(self.width.trunc(), self.height.trunc()) |
216 | } |
217 | |
218 | /// Returns the aspect ratio of a rectangle with the given size. |
219 | /// |
220 | /// If the width is `0`, the output will be `sign(self.height) * infinity`. If The width and |
221 | /// height are `0`, then the output will be `NaN`. |
222 | pub fn aspect_ratio(self) -> f64 { |
223 | self.height / self.width |
224 | } |
225 | |
226 | /// Convert this `Size` into a [`Rect`] with origin `(0.0, 0.0)`. |
227 | #[inline ] |
228 | pub const fn to_rect(self) -> Rect { |
229 | Rect::new(0., 0., self.width, self.height) |
230 | } |
231 | |
232 | /// Convert this `Size` into a [`RoundedRect`] with origin `(0.0, 0.0)` and |
233 | /// the provided corner radius. |
234 | #[inline ] |
235 | pub fn to_rounded_rect(self, radii: impl Into<RoundedRectRadii>) -> RoundedRect { |
236 | self.to_rect().to_rounded_rect(radii) |
237 | } |
238 | |
239 | /// Is this size [finite]? |
240 | /// |
241 | /// [finite]: f64::is_finite |
242 | #[inline ] |
243 | pub fn is_finite(self) -> bool { |
244 | self.width.is_finite() && self.height.is_finite() |
245 | } |
246 | |
247 | /// Is this size [NaN]? |
248 | /// |
249 | /// [NaN]: f64::is_nan |
250 | #[inline ] |
251 | pub fn is_nan(self) -> bool { |
252 | self.width.is_nan() || self.height.is_nan() |
253 | } |
254 | } |
255 | |
256 | impl fmt::Debug for Size { |
257 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
258 | write!(f, " {:?}W× {:?}H" , self.width, self.height) |
259 | } |
260 | } |
261 | |
262 | impl fmt::Display for Size { |
263 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
264 | write!(formatter, "(" )?; |
265 | fmt::Display::fmt(&self.width, f:formatter)?; |
266 | write!(formatter, "×" )?; |
267 | fmt::Display::fmt(&self.height, f:formatter)?; |
268 | write!(formatter, ")" ) |
269 | } |
270 | } |
271 | |
272 | impl MulAssign<f64> for Size { |
273 | #[inline ] |
274 | fn mul_assign(&mut self, other: f64) { |
275 | *self = Size { |
276 | width: self.width * other, |
277 | height: self.height * other, |
278 | }; |
279 | } |
280 | } |
281 | |
282 | impl Mul<Size> for f64 { |
283 | type Output = Size; |
284 | |
285 | #[inline ] |
286 | fn mul(self, other: Size) -> Size { |
287 | other * self |
288 | } |
289 | } |
290 | |
291 | impl Mul<f64> for Size { |
292 | type Output = Size; |
293 | |
294 | #[inline ] |
295 | fn mul(self, other: f64) -> Size { |
296 | Size { |
297 | width: self.width * other, |
298 | height: self.height * other, |
299 | } |
300 | } |
301 | } |
302 | |
303 | impl DivAssign<f64> for Size { |
304 | #[inline ] |
305 | fn div_assign(&mut self, other: f64) { |
306 | *self = Size { |
307 | width: self.width / other, |
308 | height: self.height / other, |
309 | }; |
310 | } |
311 | } |
312 | |
313 | impl Div<f64> for Size { |
314 | type Output = Size; |
315 | |
316 | #[inline ] |
317 | fn div(self, other: f64) -> Size { |
318 | Size { |
319 | width: self.width / other, |
320 | height: self.height / other, |
321 | } |
322 | } |
323 | } |
324 | |
325 | impl Add<Size> for Size { |
326 | type Output = Size; |
327 | #[inline ] |
328 | fn add(self, other: Size) -> Size { |
329 | Size { |
330 | width: self.width + other.width, |
331 | height: self.height + other.height, |
332 | } |
333 | } |
334 | } |
335 | |
336 | impl AddAssign<Size> for Size { |
337 | #[inline ] |
338 | fn add_assign(&mut self, other: Size) { |
339 | *self = *self + other; |
340 | } |
341 | } |
342 | |
343 | impl Sub<Size> for Size { |
344 | type Output = Size; |
345 | #[inline ] |
346 | fn sub(self, other: Size) -> Size { |
347 | Size { |
348 | width: self.width - other.width, |
349 | height: self.height - other.height, |
350 | } |
351 | } |
352 | } |
353 | |
354 | impl SubAssign<Size> for Size { |
355 | #[inline ] |
356 | fn sub_assign(&mut self, other: Size) { |
357 | *self = *self - other; |
358 | } |
359 | } |
360 | |
361 | impl From<(f64, f64)> for Size { |
362 | #[inline ] |
363 | fn from(v: (f64, f64)) -> Size { |
364 | Size { |
365 | width: v.0, |
366 | height: v.1, |
367 | } |
368 | } |
369 | } |
370 | |
371 | impl From<Size> for (f64, f64) { |
372 | #[inline ] |
373 | fn from(v: Size) -> (f64, f64) { |
374 | (v.width, v.height) |
375 | } |
376 | } |
377 | |
378 | #[cfg (test)] |
379 | mod tests { |
380 | use super::*; |
381 | |
382 | #[test ] |
383 | fn display() { |
384 | let s = Size::new(-0.12345, 9.87654); |
385 | assert_eq!(format!("{s}" ), "(-0.12345×9.87654)" ); |
386 | |
387 | let s = Size::new(-0.12345, 9.87654); |
388 | assert_eq!(format!("{s:+6.2}" ), "( -0.12× +9.88)" ); |
389 | } |
390 | |
391 | #[test ] |
392 | fn aspect_ratio() { |
393 | let s = Size::new(1.0, 1.0); |
394 | assert!((s.aspect_ratio() - 1.0).abs() < 1e-6); |
395 | } |
396 | } |
397 | |