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
//! Primitives to work with layout and opaque geometry.

use num_traits::Float;
use std::borrow::Borrow;
use strum_macros::EnumString;

use crate::geometry::Size;

/// Implementations of the flexbox algorithm.
pub mod algorithm;
mod tree;

pub use algorithm::Algorithm;
pub use tree::{Layout, LayoutNode, LayoutTree, MeasureFunc};

/// Opaque coordinate within a reference system.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct LayoutAnchor<T>
where
    T: Float,
{
    x: T,
    y: T,
}

impl<T> LayoutAnchor<T>
where
    T: Float,
{
    /// Returns a new anchor with the given absolute coordinates (i.e. relative
    /// to the origin of the reference system).
    pub fn new(x: T, y: T) -> LayoutAnchor<T> {
        LayoutAnchor { x, y }
    }

    /// Returns the distance between this anchor and the given anchor.
    pub fn distance(self, other: Self) -> LayoutDistance<T> {
        LayoutDistance {
            dx: other.borrow().x - self.x,
            dy: other.borrow().y - self.y,
        }
    }
}

/// Represents the layout direction of a language (i.e. left-to-right vs.
/// right-to-left).
#[derive(Copy, Clone, Debug, Eq, PartialEq, EnumString)]
pub enum LayoutDirection {
    /// Left to right. This is the most commonly used direction in most
    /// languages.
    #[strum(serialize = "ltr")]
    LTR,

    /// Right to left. This direction is used in Hebrew and Arabic, for example.
    #[strum(serialize = "rtl")]
    RTL,
}

/// Represents a potentially direction dependent horizontal layout axis.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum LayoutAxisX<T> {
    /// A horizontal axis that depends on the layout direction.
    DirectionDependent {
        /// Contains the layout direction dependent leading value for this
        /// horizontal axis.
        leading: T,

        /// Contains the layout direction dependent leading value for this
        /// horizontal axis.
        trailing: T,
    },

    /// A horizontal axis that is independent of the layout direction.
    DirectionIndependent {
        /// Contains the layout direction independent left value for this
        /// horizontal axis.
        left: T,

        /// Contains the layout direction independent right value for this
        /// horizontal axis.
        right: T,
    },
}

impl<T> LayoutAxisX<T> {
    /// Returns a layout direction dependent horizontal axis with the given
    /// leading and trailing values.
    pub fn dependent(leading: T, trailing: T) -> LayoutAxisX<T> {
        LayoutAxisX::DirectionDependent { leading, trailing }
    }

    /// Returns a layout direction independent horizontal axis with the given
    /// left and right values.
    pub fn independent(left: T, right: T) -> LayoutAxisX<T> {
        LayoutAxisX::DirectionIndependent { left, right }
    }

    /// Resolves the left value of this horizontal axis using the given layout
    /// direction.
    pub fn left(&self, direction: LayoutDirection) -> &T {
        match self {
            LayoutAxisX::DirectionDependent { leading, trailing } => match direction {
                LayoutDirection::LTR => leading,
                LayoutDirection::RTL => trailing,
            },
            LayoutAxisX::DirectionIndependent { left, .. } => left,
        }
    }

    /// Resolves the right value of this horizontal axis using the given layout
    /// direction.
    pub fn right(&self, direction: LayoutDirection) -> &T {
        match self {
            LayoutAxisX::DirectionDependent { leading, trailing } => match direction {
                LayoutDirection::LTR => trailing,
                LayoutDirection::RTL => leading,
            },
            LayoutAxisX::DirectionIndependent { right, .. } => right,
        }
    }
}

impl<T> Default for LayoutAxisX<T>
where
    T: Default,
{
    fn default() -> Self {
        LayoutAxisX::DirectionIndependent {
            left: T::default(),
            right: T::default(),
        }
    }
}

/// Represents a direction independent vertical layout axis.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct LayoutAxisY<T> {
    /// Contains the horizontal axis for the top edge.
    pub top: T,

    /// Contains the horizontal axis for the bottom edge.
    pub bottom: T,
}

/// Opaque rectangle defined by coordinates within a reference system.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct LayoutGuide<T>
where
    T: Float,
{
    origin: LayoutAnchor<T>,
    size: Size<T>,
    direction: LayoutDirection,
}

impl<T> LayoutGuide<T>
where
    T: Float,
{
    /// Returns a new layout guide with the given opaque origin coordinate and
    /// the given concrete size and layout direction.
    pub fn new(
        origin: LayoutAnchor<T>,
        size: Size<T>,
        direction: LayoutDirection,
    ) -> LayoutGuide<T> {
        LayoutGuide {
            origin,
            size,
            direction,
        }
    }

    /// Returns a layout anchor for the top left corner.
    pub fn top_left(&self) -> LayoutAnchor<T> {
        self.origin
    }

    /// Returns a layout anchor for the top right corner.
    pub fn top_right(&self) -> LayoutAnchor<T> {
        let mut anchor = self.origin;
        anchor.x = anchor.x + self.size.width;
        anchor
    }

    /// Returns a layout anchor for the top leading corner (which is either the
    /// top left corner or the top right corner depending on the layout
    /// direction).
    pub fn top_leading(&self) -> LayoutAnchor<T> {
        match self.direction {
            LayoutDirection::LTR => self.top_left(),
            LayoutDirection::RTL => self.top_right(),
        }
    }

    /// Returns a layout anchor for the top trailing corner (which is either the
    /// top right corner or the top left corner depending on the layout
    /// direction).
    pub fn top_trailing(&self) -> LayoutAnchor<T> {
        match self.direction {
            LayoutDirection::LTR => self.top_right(),
            LayoutDirection::RTL => self.top_left(),
        }
    }

    /// Returns a layout anchor for the bottom left corner.
    pub fn bottom_left(&self) -> LayoutAnchor<T> {
        let mut anchor = self.origin;
        anchor.y = anchor.y + self.size.height;
        anchor
    }

    /// Returns a layout anchor for the bottom right corner.
    pub fn bottom_right(&self) -> LayoutAnchor<T> {
        let mut anchor = self.origin;
        anchor.x = anchor.x + self.size.width;
        anchor.y = anchor.y + self.size.height;
        anchor
    }

    /// Returns a layout anchor for the bottom leading corner (which is either
    /// the bottom left corner or the bottom right corner depending on the
    /// layout direction).
    pub fn bottom_leading(&self) -> LayoutAnchor<T> {
        match self.direction {
            LayoutDirection::LTR => self.bottom_left(),
            LayoutDirection::RTL => self.bottom_right(),
        }
    }

    /// Returns a layout anchor for the bottom trailing corner (which is either
    /// the bottom right corner or the bottom left corner depending on the
    /// layout direction).
    pub fn bottom_trailing(&self) -> LayoutAnchor<T> {
        match self.direction {
            LayoutDirection::LTR => self.bottom_right(),
            LayoutDirection::RTL => self.bottom_left(),
        }
    }
}

/// Distance between opaque coordinates within a reference system.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct LayoutDistance<T> {
    dx: T,
    dy: T,
}

impl<T> LayoutDistance<T>
where
    T: Float,
{
    /// Returns the L1 norm of this distance vector.
    pub fn l1_norm(&self) -> T
    where
        T: Float,
    {
        self.dx.abs() + self.dy.abs()
    }

    /// Returns the L2 norm of this distance vector.
    pub fn l2_norm(&self) -> T {
        self.dx * self.dx + self.dy * self.dy
    }
}

#[cfg(test)]
mod tests {
    use super::LayoutAnchor;

    #[test]
    fn test_l1_norm() {
        let a = LayoutAnchor::new(4.0, -42.0);
        let b = LayoutAnchor::new(-2.0, -8.0);

        assert_eq!(a.distance(b).l1_norm(), 40.0);
        assert_eq!(b.distance(a).l1_norm(), 40.0);
    }

    #[test]
    fn test_l2_norm() {
        let a = LayoutAnchor::new(4.0, -42.0);
        let b = LayoutAnchor::new(-2.0, -8.0);

        assert_eq!(a.distance(b).l2_norm(), 1192.0);
        assert_eq!(b.distance(a).l2_norm(), 1192.0);
    }
}