kurbo/
rounded_rect_radii.rs

1// Copyright 2021 the Kurbo Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! A description of the radii for each corner of a rounded rectangle.
5
6use core::convert::From;
7
8#[allow(unused_imports)] // This is unused in later versions of Rust because of additions to core::f32
9#[cfg(not(feature = "std"))]
10use crate::common::FloatFuncs;
11
12/// Radii for each corner of a rounded rectangle.
13///
14/// The use of `top` as in `top_left` assumes a y-down coordinate space. Piet
15/// (and Druid by extension) uses a y-down coordinate space, but Kurbo also
16/// supports a y-up coordinate space, in which case `top_left` would actually
17/// refer to the bottom-left corner, and vice versa. Top may not always
18/// actually be the top, but `top` corners will always have a smaller y-value
19/// than `bottom` corners.
20#[derive(Clone, Copy, Default, Debug, PartialEq)]
21#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct RoundedRectRadii {
24    /// The radius of the top-left corner.
25    pub top_left: f64,
26    /// The radius of the top-right corner.
27    pub top_right: f64,
28    /// The radius of the bottom-right corner.
29    pub bottom_right: f64,
30    /// The radius of the bottom-left corner.
31    pub bottom_left: f64,
32}
33
34impl RoundedRectRadii {
35    /// Create a new `RoundedRectRadii`. This function takes radius values for
36    /// the four corners. The argument order is `top_left`, `top_right`,
37    /// `bottom_right`, `bottom_left`, or clockwise starting from `top_left`.
38    #[inline(always)]
39    pub const fn new(top_left: f64, top_right: f64, bottom_right: f64, bottom_left: f64) -> Self {
40        RoundedRectRadii {
41            top_left,
42            top_right,
43            bottom_right,
44            bottom_left,
45        }
46    }
47
48    /// Create a new `RoundedRectRadii` from a single radius. The `radius`
49    /// argument will be set as the radius for all four corners.
50    #[inline(always)]
51    pub const fn from_single_radius(radius: f64) -> Self {
52        RoundedRectRadii {
53            top_left: radius,
54            top_right: radius,
55            bottom_right: radius,
56            bottom_left: radius,
57        }
58    }
59
60    /// Takes the absolute value of all corner radii.
61    pub fn abs(&self) -> Self {
62        RoundedRectRadii::new(
63            self.top_left.abs(),
64            self.top_right.abs(),
65            self.bottom_right.abs(),
66            self.bottom_left.abs(),
67        )
68    }
69
70    /// For each corner, takes the min of that corner's radius and `max`.
71    pub fn clamp(&self, max: f64) -> Self {
72        RoundedRectRadii::new(
73            self.top_left.min(max),
74            self.top_right.min(max),
75            self.bottom_right.min(max),
76            self.bottom_left.min(max),
77        )
78    }
79
80    /// Returns `true` if all radius values are finite.
81    pub fn is_finite(&self) -> bool {
82        self.top_left.is_finite()
83            && self.top_right.is_finite()
84            && self.bottom_right.is_finite()
85            && self.bottom_left.is_finite()
86    }
87
88    /// Returns `true` if any corner radius value is NaN.
89    pub fn is_nan(&self) -> bool {
90        self.top_left.is_nan()
91            || self.top_right.is_nan()
92            || self.bottom_right.is_nan()
93            || self.bottom_left.is_nan()
94    }
95
96    /// If all radii are equal, returns the value of the radii. Otherwise,
97    /// returns `None`.
98    pub fn as_single_radius(&self) -> Option<f64> {
99        let epsilon = 1e-9;
100
101        if (self.top_left - self.top_right).abs() < epsilon
102            && (self.top_right - self.bottom_right).abs() < epsilon
103            && (self.bottom_right - self.bottom_left).abs() < epsilon
104        {
105            Some(self.top_left)
106        } else {
107            None
108        }
109    }
110}
111
112impl From<f64> for RoundedRectRadii {
113    #[inline(always)]
114    fn from(radius: f64) -> Self {
115        RoundedRectRadii::from_single_radius(radius)
116    }
117}
118
119impl From<(f64, f64, f64, f64)> for RoundedRectRadii {
120    #[inline(always)]
121    fn from(radii: (f64, f64, f64, f64)) -> Self {
122        RoundedRectRadii::new(radii.0, radii.1, radii.2, radii.3)
123    }
124}