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
// Copyright 2013-2015, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use enums::{PathDataType, Status};
use ffi;
use ffi::cairo_path_t;
use std::fmt;
use std::iter::Iterator;

#[derive(Debug)]
pub struct Path(*mut cairo_path_t);

impl Path {
    pub fn as_ptr(&self) -> *mut cairo_path_t {
        self.0
    }

    pub fn ensure_status(&self) {
        unsafe {
            let ptr: *mut cairo_path_t = self.as_ptr();
            Status::from((*ptr).status).ensure_valid()
        }
    }

    pub unsafe fn from_raw_full(pointer: *mut cairo_path_t) -> Path {
        Path(pointer)
    }

    pub fn iter(&self) -> PathSegments {
        use std::slice;

        unsafe {
            let ptr: *mut cairo_path_t = self.as_ptr();
            let length = (*ptr).num_data as usize;
            let data_ptr = (*ptr).data;
            let data_vec = if length != 0 && !data_ptr.is_null() {
                slice::from_raw_parts(data_ptr, length)
            } else {
                &[]
            };

            PathSegments {
                data: data_vec,
                i: 0,
                num_data: length,
            }
        }
    }
}

impl Drop for Path {
    fn drop(&mut self) {
        unsafe {
            ffi::cairo_path_destroy(self.as_ptr());
        }
    }
}

impl fmt::Display for Path {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Path")
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PathSegment {
    MoveTo((f64, f64)),
    LineTo((f64, f64)),
    CurveTo((f64, f64), (f64, f64), (f64, f64)),
    ClosePath,
}

impl fmt::Display for PathSegment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "PathSegment::{}",
            match *self {
                PathSegment::MoveTo(_) => "MoveTo",
                PathSegment::LineTo(_) => "LineTo",
                PathSegment::CurveTo(_, _, _) => "CurveTo",
                PathSegment::ClosePath => "ClosePath",
            }
        )
    }
}

pub struct PathSegments<'a> {
    data: &'a [ffi::cairo_path_data],
    i: usize,
    num_data: usize,
}

impl<'a> Iterator for PathSegments<'a> {
    type Item = PathSegment;

    fn next(&mut self) -> Option<PathSegment> {
        if self.i >= self.num_data {
            return None;
        }

        unsafe {
            let res = match PathDataType::from(self.data[self.i].header.data_type) {
                PathDataType::MoveTo => PathSegment::MoveTo(to_tuple(&self.data[self.i + 1].point)),
                PathDataType::LineTo => PathSegment::LineTo(to_tuple(&self.data[self.i + 1].point)),
                PathDataType::CurveTo => PathSegment::CurveTo(
                    to_tuple(&self.data[self.i + 1].point),
                    to_tuple(&self.data[self.i + 2].point),
                    to_tuple(&self.data[self.i + 3].point),
                ),
                PathDataType::ClosePath => PathSegment::ClosePath,
                PathDataType::__Unknown(x) => panic!("Unknown value: {}", x),
            };

            self.i += self.data[self.i].header.length as usize;

            Some(res)
        }
    }
}

impl<'a> fmt::Display for PathSegments<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "PathSegments")
    }
}

fn to_tuple(pair: &[f64; 2]) -> (f64, f64) {
    (pair[0], pair[1])
}

#[cfg(test)]
mod tests {
    use super::*;
    use context::*;
    use enums::Format;
    use image_surface::*;

    fn make_cr() -> Context {
        let surface = ImageSurface::create(Format::Rgb24, 1, 1).unwrap();

        Context::new(&surface)
    }

    fn assert_path_equals_segments(expected: &Path, actual: &Vec<PathSegment>) {
        // First ensure the lengths are equal

        let expected_iter = expected.iter();
        let actual_iter = actual.iter();

        assert_eq!(expected_iter.count(), actual_iter.count());

        // Then actually compare the contents

        let expected_iter = expected.iter();
        let actual_iter = actual.iter();

        let mut iter = expected_iter.zip(actual_iter);

        while let Some((e, a)) = iter.next() {
            assert_eq!(e, *a);
        }
    }

    #[test]
    fn empty_path_doesnt_iter() {
        let cr = make_cr();

        let path = cr.copy_path();

        assert!(path.iter().next().is_none());
    }

    #[test]
    fn moveto() {
        let cr = make_cr();

        cr.move_to(1.0, 2.0);

        let path = cr.copy_path();

        assert_path_equals_segments(&path, &vec![PathSegment::MoveTo((1.0, 2.0))]);
    }

    #[test]
    fn moveto_lineto_moveto() {
        let cr = make_cr();

        cr.move_to(1.0, 2.0);
        cr.line_to(3.0, 4.0);
        cr.move_to(5.0, 6.0);

        let path = cr.copy_path();

        assert_path_equals_segments(
            &path,
            &vec![
                PathSegment::MoveTo((1.0, 2.0)),
                PathSegment::LineTo((3.0, 4.0)),
                PathSegment::MoveTo((5.0, 6.0)),
            ],
        );
    }

    #[test]
    fn moveto_closepath() {
        let cr = make_cr();

        cr.move_to(1.0, 2.0);
        cr.close_path();

        let path = cr.copy_path();

        // Note that Cairo represents a close_path as closepath+moveto,
        // so that the next subpath will have a starting point,
        // from the extra moveto.
        assert_path_equals_segments(
            &path,
            &vec![
                PathSegment::MoveTo((1.0, 2.0)),
                PathSegment::ClosePath,
                PathSegment::MoveTo((1.0, 2.0)),
            ],
        );
    }
    #[test]
    fn curveto_closed_subpath_lineto() {
        let cr = make_cr();

        cr.move_to(1.0, 2.0);
        cr.curve_to(3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
        cr.close_path();
        cr.line_to(9.0, 10.0);

        let path = cr.copy_path();

        assert_path_equals_segments(
            &path,
            &vec![
                PathSegment::MoveTo((1.0, 2.0)),
                PathSegment::CurveTo((3.0, 4.0), (5.0, 6.0), (7.0, 8.0)),
                PathSegment::ClosePath,
                PathSegment::MoveTo((1.0, 2.0)),
                PathSegment::LineTo((9.0, 10.0)),
            ],
        );
    }
}