cairo/
paths.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{iter::FusedIterator, ptr};
4
5use crate::{PathDataType, ffi};
6
7#[derive(Debug)]
8#[doc(alias = "cairo_path_t")]
9pub struct Path(ptr::NonNull<ffi::cairo_path_t>);
10
11impl Path {
12    #[inline]
13    pub fn as_ptr(&self) -> *mut ffi::cairo_path_t {
14        self.0.as_ptr()
15    }
16
17    #[inline]
18    pub unsafe fn from_raw_full(pointer: *mut ffi::cairo_path_t) -> Path {
19        unsafe {
20            debug_assert!(!pointer.is_null());
21            Path(ptr::NonNull::new_unchecked(pointer))
22        }
23    }
24
25    pub fn iter(&self) -> PathSegments<'_> {
26        use std::slice;
27
28        unsafe {
29            let ptr: *mut ffi::cairo_path_t = self.as_ptr();
30            let length = (*ptr).num_data as usize;
31            let data_ptr = (*ptr).data;
32            let data_vec = if length != 0 && !data_ptr.is_null() {
33                slice::from_raw_parts(data_ptr, length)
34            } else {
35                &[]
36            };
37
38            PathSegments {
39                data: data_vec,
40                i: 0,
41                num_data: length,
42            }
43        }
44    }
45}
46
47impl Drop for Path {
48    #[inline]
49    fn drop(&mut self) {
50        unsafe {
51            ffi::cairo_path_destroy(self.as_ptr());
52        }
53    }
54}
55
56#[derive(Debug, Clone, Copy, PartialEq)]
57pub enum PathSegment {
58    MoveTo((f64, f64)),
59    LineTo((f64, f64)),
60    CurveTo((f64, f64), (f64, f64), (f64, f64)),
61    ClosePath,
62}
63
64pub struct PathSegments<'a> {
65    data: &'a [ffi::cairo_path_data],
66    i: usize,
67    num_data: usize,
68}
69
70impl Iterator for PathSegments<'_> {
71    type Item = PathSegment;
72
73    fn next(&mut self) -> Option<PathSegment> {
74        if self.i >= self.num_data {
75            return None;
76        }
77
78        unsafe {
79            let res = match PathDataType::from(self.data[self.i].header.data_type) {
80                PathDataType::MoveTo => PathSegment::MoveTo(to_tuple(&self.data[self.i + 1].point)),
81                PathDataType::LineTo => PathSegment::LineTo(to_tuple(&self.data[self.i + 1].point)),
82                PathDataType::CurveTo => PathSegment::CurveTo(
83                    to_tuple(&self.data[self.i + 1].point),
84                    to_tuple(&self.data[self.i + 2].point),
85                    to_tuple(&self.data[self.i + 3].point),
86                ),
87                PathDataType::ClosePath => PathSegment::ClosePath,
88                PathDataType::__Unknown(x) => panic!("Unknown value: {x}"),
89            };
90
91            self.i += self.data[self.i].header.length as usize;
92
93            Some(res)
94        }
95    }
96}
97
98impl FusedIterator for PathSegments<'_> {}
99
100fn to_tuple(pair: &[f64; 2]) -> (f64, f64) {
101    (pair[0], pair[1])
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::{context::*, enums::Format, image_surface::*};
108
109    fn make_cr() -> Context {
110        let surface = ImageSurface::create(Format::Rgb24, 1, 1).unwrap();
111
112        Context::new(&surface).expect("Can't create a Cairo context")
113    }
114
115    fn assert_path_equals_segments(expected: &Path, actual: &[PathSegment]) {
116        // First ensure the lengths are equal
117
118        let expected_iter = expected.iter();
119        let actual_iter = actual.iter();
120
121        assert_eq!(expected_iter.count(), actual_iter.count());
122
123        // Then actually compare the contents
124
125        let expected_iter = expected.iter();
126        let actual_iter = actual.iter();
127
128        let iter = expected_iter.zip(actual_iter);
129        for (e, a) in iter {
130            assert_eq!(e, *a);
131        }
132    }
133
134    #[test]
135    fn empty_path_doesnt_iter() {
136        let cr = make_cr();
137
138        let path = cr.copy_path().expect("Invalid context");
139
140        assert!(path.iter().next().is_none());
141    }
142
143    #[test]
144    fn moveto() {
145        let cr = make_cr();
146
147        cr.move_to(1.0, 2.0);
148
149        let path = cr.copy_path().expect("Invalid path");
150
151        assert_path_equals_segments(&path, &[PathSegment::MoveTo((1.0, 2.0))]);
152    }
153
154    #[test]
155    fn moveto_lineto_moveto() {
156        let cr = make_cr();
157
158        cr.move_to(1.0, 2.0);
159        cr.line_to(3.0, 4.0);
160        cr.move_to(5.0, 6.0);
161
162        let path = cr.copy_path().expect("Invalid path");
163
164        assert_path_equals_segments(
165            &path,
166            &[
167                PathSegment::MoveTo((1.0, 2.0)),
168                PathSegment::LineTo((3.0, 4.0)),
169                PathSegment::MoveTo((5.0, 6.0)),
170            ],
171        );
172    }
173
174    #[test]
175    fn moveto_closepath() {
176        let cr = make_cr();
177
178        cr.move_to(1.0, 2.0);
179        cr.close_path();
180
181        let path = cr.copy_path().expect("Invalid path");
182
183        // Note that Cairo represents a close_path as closepath+moveto,
184        // so that the next subpath will have a starting point,
185        // from the extra moveto.
186        assert_path_equals_segments(
187            &path,
188            &[
189                PathSegment::MoveTo((1.0, 2.0)),
190                PathSegment::ClosePath,
191                PathSegment::MoveTo((1.0, 2.0)),
192            ],
193        );
194    }
195    #[test]
196    fn curveto_closed_subpath_lineto() {
197        let cr = make_cr();
198
199        cr.move_to(1.0, 2.0);
200        cr.curve_to(3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
201        cr.close_path();
202        cr.line_to(9.0, 10.0);
203
204        let path = cr.copy_path().expect("Invalid path");
205
206        assert_path_equals_segments(
207            &path,
208            &[
209                PathSegment::MoveTo((1.0, 2.0)),
210                PathSegment::CurveTo((3.0, 4.0), (5.0, 6.0), (7.0, 8.0)),
211                PathSegment::ClosePath,
212                PathSegment::MoveTo((1.0, 2.0)),
213                PathSegment::LineTo((9.0, 10.0)),
214            ],
215        );
216    }
217}