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
use geo::{Coordinate, MultiPolygon};
use std::fmt;

/// A feature which aligns a sequence of features vertically.
#[derive(Debug, Clone)]
pub struct Column<U = super::Unit> {
    array: Vec<U>,
    align: crate::Align,
    bbox: bool,
}

impl<U: super::Feature + fmt::Debug + Clone> Column<U> {
    /// Lays out the given features in an array going downwards, with
    /// their leftmost elements aligned.
    pub fn align_left(array: Vec<U>) -> Self {
        Self::new(array, crate::Align::Start)
    }

    /// Lays out the given features in an array going downwards, with
    /// their rightmost elements aligned.``
    pub fn align_right(array: Vec<U>) -> Self {
        Self::new(array, crate::Align::End)
    }

    /// Lays out the given features in an array going downwards, with
    /// each element aligned to the center.
    pub fn align_center(array: Vec<U>) -> Self {
        Self::new(array, crate::Align::Center)
    }

    fn new(mut array: Vec<U>, align: crate::Align) -> Self {
        // Position any containing geometry to exist entirely in positive
        // (x>=0, y>=0) coordinate space.
        for e in array.iter_mut() {
            if let Some(b) = e.edge_union() {
                use geo::bounding_rect::BoundingRect;
                let v = b.bounding_rect().unwrap().min();
                e.translate(-v);
            }
        }

        Self {
            align,
            array,
            bbox: true,
        }
    }

    fn all_bounds(&self) -> Vec<geo::Rect<f64>> {
        self.array
            .iter()
            .map(|f| match f.edge_union() {
                Some(edge) => {
                    use geo::bounding_rect::BoundingRect;
                    edge.bounding_rect().unwrap()
                }
                None => geo::Rect::new(
                    Coordinate::<f64> { x: 0., y: 0. },
                    Coordinate::<f64> { x: 0., y: 0. },
                ),
            })
            .collect()
    }

    fn largest(&self) -> geo::Rect<f64> {
        self.all_bounds()
            .into_iter()
            .max_by(|x, y| x.width().partial_cmp(&y.width()).unwrap())
            .unwrap()
    }

    fn translations<'a>(
        &'a self,
        largest: geo::Rect<f64>,
    ) -> Box<dyn Iterator<Item = Option<(f64, f64)>> + 'a> {
        Box::new(
            self.array
                .iter()
                .map(|f| match f.edge_union() {
                    Some(edge_geo) => Some(edge_geo.clone()),
                    None => None,
                })
                .scan(0f64, |y_off, f| {
                    // accumulate the heights of each element so we can
                    // adjust them to tile downwards.
                    use geo::bounding_rect::BoundingRect;
                    let h = match f {
                        Some(ref f) => f.clone().bounding_rect().unwrap().height(),
                        None => 0.0,
                    };
                    let out = Some((f, *y_off));
                    *y_off = *y_off + h;
                    out
                })
                .map(move |(g, y_off)| match g {
                    Some(g) => {
                        use geo::bounding_rect::BoundingRect;
                        let bounds = g.bounding_rect().unwrap();

                        Some(match self.align {
                            crate::Align::Start => (largest.min().x - bounds.min().x, y_off),
                            crate::Align::End => (largest.max().x - bounds.max().x, y_off),
                            crate::Align::Center => (largest.center().x - bounds.center().x, y_off),
                        })
                    }
                    None => None,
                }),
        )
    }
}

impl<U: super::Feature + fmt::Debug> fmt::Display for Column<U> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Column(align = {:?}, {:?})", self.align, self.array)
    }
}

impl<U: super::Feature + fmt::Debug + Clone> super::Feature for Column<U> {
    fn name(&self) -> &'static str {
        "Column"
    }

    fn edge_union(&self) -> Option<MultiPolygon<f64>> {
        let out = self
            .array
            .iter()
            .map(|f| match f.edge_union() {
                Some(edge_geo) => Some(edge_geo.clone()),
                None => None,
            })
            .zip(self.translations(self.largest()).into_iter())
            .filter(|(f, t)| f.is_some() && t.is_some())
            .map(|(f, t)| (f.unwrap(), t.unwrap()))
            .fold(None, |mut acc, (g, (tx, ty))| {
                use geo::translate::Translate;
                use geo_booleanop::boolean::BooleanOp;
                if let Some(current) = acc {
                    acc = Some(g.translate(tx, ty).union(&current));
                } else {
                    acc = Some(g.translate(tx, ty));
                };
                acc
            });

        // If we are in bbox mode, all we need to do is compute the bounding
        // box and use that as our outer geometry.
        if self.bbox {
            match out {
                None => None,
                Some(poly) => {
                    use geo::bounding_rect::BoundingRect;
                    Some(poly.bounding_rect().unwrap().to_polygon().into())
                }
            }
        } else {
            out
        }
    }

    fn translate(&mut self, v: Coordinate<f64>) {
        for e in self.array.iter_mut() {
            e.translate(v);
        }
    }

    fn interior(&self) -> Vec<super::InnerAtom> {
        let largest = self.largest();

        self.array
            .iter()
            .map(|f| f.interior())
            .zip(self.translations(largest).into_iter())
            .map(|(f, t)| {
                let (tx, ty) = match t {
                    Some((tx, ty)) => (tx, ty),
                    None => (0., 0.),
                };

                f.into_iter().map(move |mut a| {
                    a.translate(tx, ty);
                    a
                })
            })
            .flatten()
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::features::Rect;
    use test_case::test_case;

    #[test]
    fn bounds() {
        let a = Column::align_left(vec![
            Rect::with_center([0., 0.].into(), 2., 3.),
            Rect::with_center([0., 0.].into(), 3., 2.),
        ]);

        assert_eq!(
            a.all_bounds(),
            vec![
                geo::Rect::new::<geo::Coordinate<_>>([0., 0.].into(), [2., 3.].into()),
                geo::Rect::new::<geo::Coordinate<_>>([0., 0.].into(), [3., 2.].into()),
            ]
        );
    }

    #[test]
    fn largest() {
        let a = Column::align_left(vec![
            Rect::with_center([0., 0.].into(), 2., 3.),
            Rect::with_center([0., 0.].into(), 3., 2.),
        ]);

        assert_eq!(
            a.largest(),
            geo::Rect::new::<geo::Coordinate<_>>([0., 0.].into(), [3., 2.].into(),),
        );
    }

    #[test_case(
        vec![
            Rect::with_center([0., 0.].into(), 4., 2.),
            Rect::with_center([0., 0.].into(), 2., 2.),
        ], vec![
            Some((0., 0.)),
            Some((0., 2.)),
        ] ; "origin centered"
    )
    ]
    fn translations_left(inners: Vec<Rect>, want: Vec<Option<(f64, f64)>>) {
        let a = Column::align_left(inners);
        assert_eq!(a.translations(a.largest()).collect::<Vec<_>>(), want,);
    }

    #[test_case( vec![
        Rect::with_center([0., 0.].into(), 2., 2.),
        Rect::with_center([0., 0.].into(), 4., 2.),
    ], vec![
        Some((1., 0.)),
        Some((0., 2.)),
    ] ; "origin centered")]
    #[test_case( vec![
        Rect::new([0., 0.].into(), [2., 3.].into()),
        Rect::new([0., 0.].into(), [2., 2.].into()),
    ], vec![
        Some((0., 0.)),
        Some((0., 3.)),
    ] ; "origin tl zeroed")]
    fn translations_center(inners: Vec<Rect>, want: Vec<Option<(f64, f64)>>) {
        let a = Column::align_center(inners);
        assert_eq!(a.translations(a.largest()).collect::<Vec<_>>(), want,);
    }
}