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
mod clipper2;
pub(crate) use clipper2::*;

mod clipper_offset;
pub use clipper_offset::*;

mod double;
pub use double::*;

mod int64;
pub use int64::*;

#[cfg(test)]
mod tests;

pub(crate) unsafe fn malloc(size: usize) -> *mut std::os::raw::c_void {
    libc::malloc(size)
}

pub(crate) unsafe fn free(p: *mut std::os::raw::c_void) {
    libc::free(p)
}

#[derive(Clone, Copy)]
pub enum FillRule {
    EvenOdd,
    NonZero,
    Positive,
    Negative,
}

#[derive(Clone, Copy)]
pub enum ClipType {
    None,
    Intersection,
    Union,
    Difference,
    Xor,
}

#[derive(Clone, Copy)]
pub enum JoinType {
    SquaerJoin,
    RoundJoin,
    MiterJoin,
}

#[derive(Clone, Copy)]
pub enum EndType {
    PolygonEnd,
    JoinedEnd,
    SquaerEnd,
    RoundEnd,
}

#[derive(Clone, Copy)]
pub enum PointInPolygonResult {
    On,
    Inside,
    Outside,
}

impl From<ClipType> for ClipperClipType {
    fn from(value: ClipType) -> Self {
        match value {
            ClipType::None => ClipperClipType_NONE,
            ClipType::Intersection => ClipperClipType_INTERSECTION,
            ClipType::Union => ClipperClipType_UNION,
            ClipType::Difference => ClipperClipType_DIFFERENCE,
            ClipType::Xor => ClipperClipType_XOR,
        }
    }
}

impl From<FillRule> for ClipperFillRule {
    fn from(value: FillRule) -> Self {
        match value {
            FillRule::EvenOdd => ClipperFillRule_EVEN_ODD,
            FillRule::NonZero => ClipperFillRule_NON_ZERO,
            FillRule::Positive => ClipperFillRule_POSITIVE,
            FillRule::Negative => ClipperFillRule_NEGATIVE,
        }
    }
}

impl From<JoinType> for ClipperJoinType {
    fn from(value: JoinType) -> Self {
        match value {
            JoinType::SquaerJoin => ClipperJoinType_SQUARE_JOIN,
            JoinType::RoundJoin => ClipperJoinType_ROUND_JOIN,
            JoinType::MiterJoin => ClipperJoinType_MITER_JOIN,
        }
    }
}

impl From<EndType> for ClipperEndType {
    fn from(value: EndType) -> Self {
        match value {
            EndType::PolygonEnd => ClipperEndType_POLYGON_END,
            EndType::JoinedEnd => ClipperEndType_JOINED_END,
            EndType::SquaerEnd => ClipperEndType_SQUARE_END,
            EndType::RoundEnd => ClipperEndType_ROUND_END,
        }
    }
}

#[allow(non_upper_case_globals)]
impl Into<PointInPolygonResult> for ClipperPointInPolygonResult {
    fn into(self) -> PointInPolygonResult {
        match self {
            ClipperPointInPolygonResult_IS_ON => PointInPolygonResult::On,
            ClipperPointInPolygonResult_IS_INSIDE => PointInPolygonResult::Inside,
            ClipperPointInPolygonResult_IS_OUTSIDE => PointInPolygonResult::Outside,
            _ => panic!("Not Supported")
        }
    }
}