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
use crate::{ffi, Bound, EdgeHolder, EdgeSegment, Polarity};

/// Contour object
#[repr(transparent)]
pub struct Contour {
    raw: ffi::msdfgen_Contour,
}

impl Default for Contour {
    /// Creates new empty contour (with no edges)
    fn default() -> Self {
        let raw = unsafe { ffi::msdfgen_Contour_constructor() };
        Self { raw }
    }
}

impl Drop for Contour {
    fn drop(&mut self) {
        unsafe {
            ffi::msdfgen_Contour_destructor(&mut self.raw);
        }
    }
}

impl Contour {
    pub(crate) fn as_raw(&self) -> &ffi::msdfgen_Contour {
        &self.raw
    }

    /// Adds an edge to the contour.
    pub fn add_edge(&mut self, edge: &EdgeHolder) {
        unsafe { self.raw.addEdge(edge.as_raw()) }
    }

    /// Creates a new edge in the contour and returns its reference.
    pub fn add_edge_mut(&mut self) -> &mut EdgeHolder {
        unsafe { &mut *(self.raw.addEdge2() as *mut EdgeHolder) }
    }

    /// Adds segment as an edge to the contour.
    pub fn add_segment(&mut self, segment: impl EdgeSegment) {
        self.add_edge(&segment.into())
    }

    /// Adjusts the bounding box to fit the contour.
    pub fn bound(&self, bound: &mut Bound<f64>) {
        unsafe {
            self.raw.bound(
                &mut bound.left,
                &mut bound.bottom,
                &mut bound.right,
                &mut bound.top,
            )
        }
    }

    /// Gets the bounding box to fit the contour.
    pub fn get_bound(&self) -> Bound<f64> {
        let mut bound = Bound::default();
        self.bound(&mut bound);
        bound
    }

    /// Adjusts the bounding box to fit the contour border's mitered corners.
    pub fn bound_miters(
        &self,
        bound: &mut Bound<f64>,
        border: f64,
        miter_limit: f64,
        polarity: Polarity,
    ) {
        unsafe {
            self.raw.boundMiters(
                &mut bound.left,
                &mut bound.bottom,
                &mut bound.right,
                &mut bound.top,
                border,
                miter_limit,
                polarity as _,
            )
        }
    }

    /// Gets the bounding box to fit the contour border's mitered corners.
    pub fn get_bound_miters(
        &self,
        border: f64,
        miter_limit: f64,
        polarity: Polarity,
    ) -> Bound<f64> {
        let mut bound = Bound::default();
        self.bound_miters(&mut bound, border, miter_limit, polarity);
        bound
    }

    /// Computes the winding of the contour. Returns 1 if positive, -1 if negative.
    pub fn winding(&self) -> i32 {
        unsafe { self.raw.winding() }
    }
}