xege 0.2.3

Rust style safe warpper of C++ graphics libraries.
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::ptr::null;

use xege_ffi::*;

use crate::{DrawableDevice, Point, Rect, mat::IntoEGEMatrix};

/// Graphics path.
#[derive(Debug)]
pub struct Path {
    pub(crate) ptr: *mut ege_ege_path,
}

impl Path {
    /// Create a new graphics path.
    pub fn new() -> Self {
        Self {
            ptr: unsafe { ege_ege_path_create() },
        }
    }
}

impl Drop for Path {
    fn drop(&mut self) {
        unsafe { ege_ege_path_destroy(self.ptr) };
    }
}

impl Clone for Path {
    fn clone(&self) -> Self {
        Self {
            ptr: unsafe { ege_ege_path_clone(self.ptr) },
        }
    }
}

/// Path fill mode.
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PathFillMode {
    /// Default fill mode.
    Default = ege_fill_mode_FILLMODE_DEFAULT,
    /// Winding fill mode.
    Winding = ege_fill_mode_FILLMODE_WINDING,
    /// Alternate fill mode.
    Alternate = ege_fill_mode_FILLMODE_ALTERNATE,
}

/// Font styles.
#[bitmask_enum::bitmask(i32)]
pub enum FontStyle {
    Black = ege_font_styles_FONTSTYLE_BOLD,
    Underline = ege_font_styles_FONTSTYLE_UNDERLINE,
    Italic = ege_font_styles_FONTSTYLE_ITALIC,
    StrikeOut = ege_font_styles_FONTSTYLE_STRIKEOUT,
}

impl Path {
    /// Start a new subpath.
    pub fn start(&mut self) {
        unsafe { ege_ege_path_start(self.ptr) };
    }

    /// Close the current subpath.
    pub fn close(&mut self) {
        unsafe { ege_ege_path_close(self.ptr) };
    }

    /// Close all subpaths.
    pub fn closeall(&mut self) {
        unsafe { ege_ege_path_closeall(self.ptr) };
    }

    /// Reset the path.
    pub fn reset(&mut self) {
        unsafe { ege_ege_path_reset(self.ptr) };
    }

    /// Reverses the order of the points in the path.
    pub fn reverse(&mut self) {
        unsafe { ege_ege_path_reverse(self.ptr) };
    }

    /// Set the fill mode.
    pub fn setfillmode(&mut self, mode: PathFillMode) {
        unsafe { ege_ege_path_setfillmode(self.ptr, mode as i32) };
    }

    /// Widen the path.
    /// 
    /// # Parameters
    /// - `width`: The width of the stroke.
    /// - `matrix`: The transformation matrix.
    /// - `flatness`: The flatness of the stroke.
    pub fn widen(&mut self, width: f32, matrix: Option<impl IntoEGEMatrix>, flatness: f32) {
        let mut ptr = null();
        let mat;
        if let Some(matrix) = matrix {
            mat = matrix.into_ege_matrix();
            ptr = &mat as _;
        }
        unsafe {
            ege_ege_path_widen1(self.ptr, width, ptr, flatness);
        }
    }

    /// Flatten the path.
    /// 
    /// # Parameters
    /// - `matrix`: The transformation matrix.
    /// - `flatness`: The flatness of the stroke.
    pub fn flatten(&mut self, matrix: Option<impl IntoEGEMatrix>, flatness: f32) {
        let mut ptr = null();
        let mat;
        if let Some(matrix) = matrix {
            mat = matrix.into_ege_matrix();
            ptr = &mat as _;
        }
        unsafe {
            ege_ege_path_flatten1(self.ptr, ptr, flatness);
        }
    }

    /// Warp the path.
    /// 
    /// # Parameters
    /// - `points`: The control points.
    /// - `rect`: The destination rectangle.
    /// - `matrix`: The transformation matrix.
    /// - `flatness`: The flatness of the stroke.
    pub fn warp(
        &mut self,
        points: &[Point<f32>],
        rect: Rect<f32>,
        matrix: Option<impl IntoEGEMatrix>,
        flatness: f32,
    ) {
        let mut ptr = null();
        let mat;
        if let Some(matrix) = matrix {
            mat = matrix.into_ege_matrix();
            ptr = &mat as _;
        }
        assert!(points.len() == 3 || points.len() == 4);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();

        let rect = ege_ege_rect {
            x: rect.x,
            y: rect.y,
            w: rect.width,
            h: rect.height,
        };
        unsafe {
            ege_ege_path_warp1(
                self.ptr,
                points.as_ptr(),
                points.len() as i32,
                &rect,
                ptr,
                flatness,
            );
        }
    }

    /// Convert the path to the outline of the path.
    /// 
    /// # Parameters
    /// - `matrix`: The transformation matrix.
    /// - `flatness`: The flatness of the stroke.
    pub fn outline(&mut self, matrix: Option<impl IntoEGEMatrix>, flatness: f32) {
        let mut ptr = null();
        let mat;
        if let Some(matrix) = matrix {
            mat = matrix.into_ege_matrix();
            ptr = &mat as _;
        }
        unsafe {
            ege_ege_path_outline1(self.ptr, ptr, flatness);
        }
    }

    /// Is the point in the path?
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the point.
    /// - `y`: The y-coordinate of the point.
    /// - `image`: The image to test against.
    pub fn inpath(&self, x: f32, y: f32, image: Option<&impl DrawableDevice>) -> bool {
        if let Some(device) = image {
            unsafe { ege_ege_path_inpath1(self.ptr, x, y, device.const_ptr()) }
        } else {
            unsafe { ege_ege_path_inpath(self.ptr, x, y) }
        }
    }

    /// Is the point on the stroke of the path?
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the point.
    /// - `y`: The y-coordinate of the point.
    /// - `image`: The image to test against.
    pub fn instroke(&self, x: f32, y: f32, image: Option<&impl DrawableDevice>) -> bool {
        if let Some(device) = image {
            unsafe { ege_ege_path_instroke1(self.ptr, x, y, device.const_ptr()) }
        } else {
            unsafe { ege_ege_path_instroke(self.ptr, x, y) }
        }
    }

    /// Gets the last point of the path.
    pub fn lastpoint(&self) -> Point<f32> {
        let p = unsafe { ege_ege_path_lastpoint(self.ptr) };
        Point { x: p.x, y: p.y }
    }

    /// Get the number of points in the path.
    pub fn pointcount(&self) -> i32 {
        unsafe { ege_ege_path_pointcount(self.ptr) }
    }

    /// Get the bounding box of the path.
    /// 
    /// # Parameters
    /// - `matrix`: The transformation matrix.
    /// - `image`: The image to test against.
    pub fn getbounds(
        &self,
        matrix: Option<impl IntoEGEMatrix>,
        image: Option<&impl DrawableDevice>,
    ) -> Rect<f32> {
        let mut ptr = null();
        let mat;
        if let Some(matrix) = matrix {
            mat = matrix.into_ege_matrix();
            ptr = &mat as _;
        }
        if let Some(device) = image {
            let r = unsafe { ege_ege_path_getbounds1(self.ptr, ptr, device.const_ptr()) };
            Rect {
                x: r.x,
                y: r.y,
                width: r.w,
                height: r.h,
            }
        } else {
            let r = unsafe { ege_ege_path_getbounds(self.ptr, ptr) };
            Rect {
                x: r.x,
                y: r.y,
                width: r.w,
                height: r.h,
            }
        }
    }

    /// Transform the path.
    /// 
    /// # Parameters
    /// - `matrix`: The transformation matrix.
    pub fn transform(&mut self, matrix: impl IntoEGEMatrix) {
        let mat = matrix.into_ege_matrix();
        unsafe { ege_ege_path_transform(self.ptr, &mat) };
    }

    /// Add a path to the current path.
    /// 
    /// # Parameters
    /// - `other`: The other path.
    /// - `connnet`: Whether to connect the paths.
    pub fn addpath(&mut self, other: &Self, connnet: bool) {
        unsafe { ege_ege_path_addpath(self.ptr, other.ptr, connnet) };
    }

    /// Add a line to the current path.
    /// 
    /// # Parameters
    /// - `x1`: The x-coordinate of the first point.
    /// - `y1`: The y-coordinate of the first point.
    /// - `x2`: The x-coordinate of the second point.
    /// - `y2`: The y-coordinate of the second point.
    pub fn addline(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) {
        unsafe { ege_ege_path_addline(self.ptr, x1, y1, x2, y2) };
    }

    /// Add a arc to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the center.
    /// - `y`: The y-coordinate of the center.
    /// - `width`: The width of the arc.
    /// - `height`: The height of the arc.
    /// - `start`: The start angle in radians.
    /// - `sweep`: The sweep angle in radians.
    pub fn addarc(&mut self, x: f32, y: f32, width: f32, height: f32, start: f32, sweep: f32) {
        unsafe { ege_ege_path_addarc(self.ptr, x, y, width, height, start, sweep) };
    }

    /// Add a polyline to the current path.
    /// 
    /// # Parameters
    /// - `points`: The points of the polyline.
    pub fn addpolyline(&mut self, points: &[Point<f32>]) {
        #[cfg(debug_assertions)]
        assert!(points.len() >= 2);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();
        unsafe { ege_ege_path_addpolyline(self.ptr, points.len() as i32, points.as_ptr()) };
    }

    /// Add a bezier curve to the current path.
    /// 
    /// # Parameters
    /// - `points`: The points of the bezier curve.
    pub fn addbezier(&mut self, points: &[Point<f32>]) {
        #[cfg(debug_assertions)]
        assert!(points.len() >= 4 && points.len() % 3 == 1);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();
        unsafe { ege_ege_path_addbezier(self.ptr, points.len() as i32, points.as_ptr()) };
    }

    /// Add a curve to the current path.
    /// 
    /// # Parameters
    /// - `points`: The points of the curve.
    /// - `tension`: The tension of the curve.
    pub fn addcurve(&mut self, points: &[Point<f32>], tension: f32) {
        #[cfg(debug_assertions)]
        assert!(points.len() >= 2);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();
        unsafe { ege_ege_path_addcurve1(self.ptr, points.len() as i32, points.as_ptr(), tension) };
    }

    /// Add a circle to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the center.
    /// - `y`: The y-coordinate of the center.
    /// - `radius`: The radius of the circle.
    pub fn addcircle(&mut self, x: f32, y: f32, radius: f32) {
        unsafe { ege_ege_path_addcircle(self.ptr, x, y, radius) };
    }

    /// Add a rectangle to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the top-left corner.
    /// - `y`: The y-coordinate of the top-left corner.
    /// - `width`: The width of the rectangle.
    /// - `height`: The height of the rectangle.
    pub fn addrect(&mut self, x: f32, y: f32, width: f32, height: f32) {
        unsafe { ege_ege_path_addrect(self.ptr, x, y, width, height) };
    }

    /// Add an ellipse to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the center.
    /// - `y`: The y-coordinate of the center.
    /// - `width`: The width of the ellipse.
    /// - `height`: The height of the ellipse.
    pub fn addellipse(&mut self, x: f32, y: f32, width: f32, height: f32) {
        unsafe { ege_ege_path_addellipse(self.ptr, x, y, width, height) };
    }

    /// Add a pie to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the center.
    /// - `y`: The y-coordinate of the center.
    /// - `width`: The width of the pie.
    /// - `height`: The height of the pie.
    /// - `start`: The start angle in radians.
    /// - `sweep`: The sweep angle in radians.
    pub fn addpie(&mut self, x: f32, y: f32, width: f32, height: f32, start: f32, sweep: f32) {
        unsafe { ege_ege_path_addpie(self.ptr, x, y, width, height, start, sweep) };
    }

    /// Add a polygon to the current path.
    /// 
    /// # Parameters
    /// - `points`: The points of the polygon.
    pub fn addpolygon(&mut self, points: &[Point<f32>]) {
        #[cfg(debug_assertions)]
        assert!(points.len() >= 3);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();
        unsafe { ege_ege_path_addpolygon(self.ptr, points.len() as i32, points.as_ptr()) };
    }

    /// Add a closed curve to the current path.
    /// 
    /// # Parameters
    /// - `points`: The points of the curve.
    /// - `tension`: The tension of the curve.
    pub fn addclosedcurve(&mut self, points: &[Point<f32>], tension: f32) {
        #[cfg(debug_assertions)]
        assert!(points.len() >= 3);
        let points = points
            .into_iter()
            .map(|&Point { x, y }| ege_ege_point { x, y })
            .collect::<Vec<_>>();
        unsafe {
            ege_ege_path_addclosedcurve1(self.ptr, points.len() as i32, points.as_ptr(), tension)
        };
    }

    /// Add a text to the current path.
    /// 
    /// # Parameters
    /// - `x`: The x-coordinate of the left edge of the text.
    /// - `y`: The y-coordinate of the top edge of the text.
    /// - `text`: The text to add.
    /// - `font`: The font to use.
    /// - `size`: The size of the font.
    /// - `style`: The font styles.
    pub fn addtext(&mut self, x: f32, y: f32, text: &str, font: &str, size: f32, style: FontStyle) {
        let text = text.encode_utf16().collect::<Vec<u16>>();
        let font = font.encode_utf16().chain(Some(0)).collect::<Vec<u16>>();
        unsafe {
            ege_ege_path_addtext1(
                self.ptr,
                x,
                y,
                text.as_ptr(),
                size,
                text.len() as i32,
                font.as_ptr(),
                style.bits() as _,
            )
        };
    }
}