1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::{fmt, ops};

use zng_var::{animation::Transitionable, impl_from_and_into_var};

use super::{impl_length_comp_conversions, DipPoint, Factor, Factor2d, FactorPercent, Layout1d, LayoutMask, Length, PxPoint, Size, Vector};

/// 2D point in [`Length`] units.
#[derive(Clone, Default, PartialEq, serde::Serialize, serde::Deserialize, Transitionable)]
pub struct Point {
    /// *x* offset in length units.
    pub x: Length,
    /// *y* offset in length units.
    pub y: Length,
}
impl fmt::Debug for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_struct("Point").field("x", &self.x).field("y", &self.y).finish()
        } else {
            write!(f, "({:?}, {:?})", self.x, self.y)
        }
    }
}
impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(p) = f.precision() {
            write!(f, "({:.p$}, {:.p$})", self.x, self.y, p = p)
        } else {
            write!(f, "({}, {})", self.x, self.y)
        }
    }
}
impl Point {
    /// New x, y from any [`Length`] unit.
    pub fn new<X: Into<Length>, Y: Into<Length>>(x: X, y: Y) -> Self {
        Point { x: x.into(), y: y.into() }
    }

    /// New x, y from single value of any [`Length`] unit.
    pub fn splat(xy: impl Into<Length>) -> Self {
        let xy = xy.into();
        Point { x: xy.clone(), y: xy }
    }

    /// ***x:*** [`Length::zero`], ***y:*** [`Length::zero`].
    pub fn zero() -> Self {
        Self::new(Length::zero(), Length::zero())
    }

    /// Point at the top-middle of the available space.
    ///
    /// ***x:*** [`Length::half`], ***y:*** [`Length::zero`]
    pub fn top() -> Self {
        Self::new(Length::half(), Length::zero())
    }

    /// Point at the bottom-middle of the available space.
    ///
    /// ***x:*** [`Length::half`], ***y:*** [`Length::fill`]
    pub fn bottom() -> Self {
        Self::new(Length::half(), Length::fill())
    }

    /// Point at the middle-left of the available space.
    ///
    /// ***x:*** [`Length::zero`], ***y:*** [`Length::half`]
    pub fn left() -> Self {
        Self::new(Length::zero(), Length::half())
    }

    /// Point at the middle-right of the available space.
    ///
    /// ***x:*** [`Length::fill`], ***y:*** [`Length::half`]
    pub fn right() -> Self {
        Self::new(Length::fill(), Length::half())
    }

    /// Point at the top-left of the available space.
    ///
    /// ***x:*** [`Length::zero`], ***y:*** [`Length::zero`]
    pub fn top_left() -> Self {
        Self::zero()
    }

    /// Point at the top-right of the available space.
    ///
    /// ***x:*** [`Length::fill`], ***y:*** [`Length::zero`]
    pub fn top_right() -> Self {
        Self::new(Length::fill(), Length::zero())
    }

    /// Point at the bottom-left of the available space.
    ///
    /// ***x:*** [`Length::zero`], ***y:*** [`Length::fill`]
    pub fn bottom_left() -> Self {
        Self::new(Length::zero(), Length::fill())
    }

    /// Point at the bottom-right of the available space.
    ///
    /// ***x:*** [`Length::fill`], ***y:*** [`Length::fill`]
    pub fn bottom_right() -> Self {
        Self::new(Length::fill(), Length::fill())
    }

    /// Point at the center.
    ///
    /// ***x:*** [`Length::half`], ***y:*** [`Length::half`]
    pub fn center() -> Self {
        Self::new(Length::half(), Length::half())
    }

    /// Swap `x` and `y`.
    pub fn yx(self) -> Self {
        Point { y: self.x, x: self.y }
    }

    /// Returns `(x, y)`.
    pub fn as_tuple(self) -> (Length, Length) {
        (self.x, self.y)
    }

    /// Returns `[x, y]`.
    pub fn as_array(self) -> [Length; 2] {
        [self.x, self.y]
    }

    /// Returns `true` if all values are [`Length::Default`].
    pub fn is_default(&self) -> bool {
        self.x.is_default() && self.y.is_default()
    }

    /// Replaces [`Length::Default`] values with `overwrite` values.
    pub fn replace_default(&mut self, overwrite: &Point) {
        self.x.replace_default(&overwrite.x);
        self.y.replace_default(&overwrite.y);
    }

    /// Cast to [`Vector`].
    pub fn as_vector(self) -> Vector {
        Vector { x: self.x, y: self.y }
    }
}
impl super::Layout2d for Point {
    type Px = PxPoint;

    fn layout_dft(&self, default: Self::Px) -> Self::Px {
        PxPoint::new(self.x.layout_dft_x(default.x), self.y.layout_dft_y(default.y))
    }

    fn affect_mask(&self) -> LayoutMask {
        self.x.affect_mask() | self.y.affect_mask()
    }
}
impl_length_comp_conversions! {
    fn from(x: X, y: Y) -> Point {
        Point::new(x, y)
    }
}
impl_from_and_into_var! {
    /// Splat.
    fn from(all: Length) -> Point {
        Point::splat(all)
    }
    /// Splat relative length.
    fn from(percent: FactorPercent) -> Point {
        Point::splat(percent)
    }
    /// Splat relative length.
    fn from(norm: Factor) -> Point {
        Point::splat(norm)
    }

    /// Splat exact length.
    fn from(f: f32) -> Point {
        Point::splat(f)
    }
    /// Splat exact length.
    fn from(i: i32) -> Point {
        Point::splat(i)
    }
    fn from(p: PxPoint) -> Point {
        Point::new(p.x, p.y)
    }
    fn from(p: DipPoint) -> Point {
        Point::new(p.x, p.y)
    }
    fn from(v: Vector) -> Point {
        v.as_point()
    }
}
impl<V: Into<Vector>> ops::Add<V> for Point {
    type Output = Self;

    fn add(mut self, rhs: V) -> Self {
        self += rhs;
        self
    }
}
impl<'a, 'b> ops::Add<&'a Vector> for &'b Point {
    type Output = Point;

    fn add(self, rhs: &'a Vector) -> Self::Output {
        self.clone() + rhs.clone()
    }
}
impl<'a, 'b> ops::Add<&'a Size> for &'b Point {
    type Output = Point;

    fn add(self, rhs: &'a Size) -> Self::Output {
        self.clone() + rhs.clone()
    }
}
impl<V: Into<Vector>> ops::AddAssign<V> for Point {
    fn add_assign(&mut self, rhs: V) {
        let rhs = rhs.into();
        self.x += rhs.x;
        self.y += rhs.y;
    }
}
impl<'a> ops::AddAssign<&'a Vector> for Point {
    fn add_assign(&mut self, rhs: &'a Vector) {
        *self += rhs.clone();
    }
}
impl<'a> ops::AddAssign<&'a Size> for Point {
    fn add_assign(&mut self, rhs: &'a Size) {
        *self += rhs.clone();
    }
}
impl<V: Into<Vector>> ops::Sub<V> for Point {
    type Output = Self;

    fn sub(mut self, rhs: V) -> Self {
        self -= rhs;
        self
    }
}
impl<'a, 'b> ops::Sub<&'a Vector> for &'b Point {
    type Output = Point;

    fn sub(self, rhs: &'a Vector) -> Self::Output {
        self.clone() - rhs.clone()
    }
}
impl<'a, 'b> ops::Sub<&'a Size> for &'b Point {
    type Output = Point;

    fn sub(self, rhs: &'a Size) -> Self::Output {
        self.clone() - rhs.clone()
    }
}
impl<V: Into<Vector>> ops::SubAssign<V> for Point {
    fn sub_assign(&mut self, rhs: V) {
        let rhs = rhs.into();
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}
impl<'a> ops::SubAssign<&'a Vector> for Point {
    fn sub_assign(&mut self, rhs: &'a Vector) {
        *self -= rhs.clone();
    }
}
impl<'a> ops::SubAssign<&'a Size> for Point {
    fn sub_assign(&mut self, rhs: &'a Size) {
        *self -= rhs.clone();
    }
}
impl<S: Into<Factor2d>> ops::Mul<S> for Point {
    type Output = Self;

    fn mul(mut self, rhs: S) -> Self {
        self *= rhs;
        self
    }
}
impl<'a, S: Into<Factor2d>> ops::Mul<S> for &'a Point {
    type Output = Point;

    fn mul(self, rhs: S) -> Self::Output {
        self.clone() * rhs
    }
}
impl<S: Into<Factor2d>> ops::MulAssign<S> for Point {
    fn mul_assign(&mut self, rhs: S) {
        let rhs = rhs.into();
        self.x *= rhs.x;
        self.y *= rhs.y;
    }
}
impl<S: Into<Factor2d>> ops::Div<S> for Point {
    type Output = Self;

    fn div(mut self, rhs: S) -> Self {
        self /= rhs;
        self
    }
}
impl<'a, S: Into<Factor2d>> ops::Div<S> for &'a Point {
    type Output = Point;

    fn div(self, rhs: S) -> Self::Output {
        self.clone() / rhs
    }
}
impl<S: Into<Factor2d>> ops::DivAssign<S> for Point {
    fn div_assign(&mut self, rhs: S) {
        let rhs = rhs.into();
        self.x /= rhs.x;
        self.y /= rhs.y;
    }
}
impl ops::Neg for Point {
    type Output = Self;

    fn neg(self) -> Self {
        Point { x: -self.x, y: -self.y }
    }
}

impl<'a> ops::Neg for &'a Point {
    type Output = Point;

    fn neg(self) -> Self::Output {
        -self.clone()
    }
}