ironrdp_pdu/
geometry.rs

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
use core::cmp::{max, min};

use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};

pub(crate) mod private {
    pub struct BaseRectangle {
        pub left: u16,
        pub top: u16,
        pub right: u16,
        pub bottom: u16,
    }

    impl BaseRectangle {
        pub fn empty() -> Self {
            Self {
                left: 0,
                top: 0,
                right: 0,
                bottom: 0,
            }
        }
    }

    pub trait RectangleImpl: Sized {
        fn from_base(rect: BaseRectangle) -> Self;
        fn to_base(&self) -> BaseRectangle;

        fn left(&self) -> u16;
        fn top(&self) -> u16;
        fn right(&self) -> u16;
        fn bottom(&self) -> u16;
    }
}

use private::*;

pub trait Rectangle: RectangleImpl {
    fn width(&self) -> u16;
    fn height(&self) -> u16;

    fn empty() -> Self {
        Self::from_base(BaseRectangle::empty())
    }

    fn union_all(rectangles: &[Self]) -> Self {
        Self::from_base(BaseRectangle {
            left: rectangles.iter().map(|r| r.left()).min().unwrap_or(0),
            top: rectangles.iter().map(|r| r.top()).min().unwrap_or(0),
            right: rectangles.iter().map(|r| r.right()).max().unwrap_or(0),
            bottom: rectangles.iter().map(|r| r.bottom()).max().unwrap_or(0),
        })
    }

    fn intersect(&self, other: &Self) -> Option<Self> {
        let a = self.to_base();
        let b = other.to_base();

        let result = BaseRectangle {
            left: max(a.left, b.left),
            top: max(a.top, b.top),
            right: min(a.right, b.right),
            bottom: min(a.bottom, b.bottom),
        };

        if result.left <= result.right && result.top <= result.bottom {
            Some(Self::from_base(result))
        } else {
            None
        }
    }

    #[must_use]
    fn union(&self, other: &Self) -> Self {
        let a = self.to_base();
        let b = other.to_base();

        let result = BaseRectangle {
            left: min(a.left, b.left),
            top: min(a.top, b.top),
            right: max(a.right, b.right),
            bottom: max(a.bottom, b.bottom),
        };

        Self::from_base(result)
    }
}

/// An **inclusive** rectangle.
///
/// This struct is defined as an **inclusive** rectangle.
/// That is, the pixel at coordinate (right, bottom) is included in the rectangle.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InclusiveRectangle {
    pub left: u16,
    pub top: u16,
    pub right: u16,
    pub bottom: u16,
}

/// An **exclusive** rectangle.
/// This struct is defined as an **exclusive** rectangle.
/// That is, the pixel at coordinate (right, bottom) is not included in the rectangle.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExclusiveRectangle {
    pub left: u16,
    pub top: u16,
    pub right: u16,
    pub bottom: u16,
}

macro_rules! impl_rectangle {
    ($type:ty) => {
        impl RectangleImpl for $type {
            fn from_base(rect: BaseRectangle) -> Self {
                Self {
                    left: rect.left,
                    top: rect.top,
                    right: rect.right,
                    bottom: rect.bottom,
                }
            }

            fn to_base(&self) -> BaseRectangle {
                BaseRectangle {
                    left: self.left,
                    top: self.top,
                    right: self.right,
                    bottom: self.bottom,
                }
            }

            fn left(&self) -> u16 {
                self.left
            }
            fn top(&self) -> u16 {
                self.top
            }
            fn right(&self) -> u16 {
                self.right
            }
            fn bottom(&self) -> u16 {
                self.bottom
            }
        }
    };
}

impl_rectangle!(InclusiveRectangle);
impl_rectangle!(ExclusiveRectangle);

impl Rectangle for InclusiveRectangle {
    fn width(&self) -> u16 {
        self.right - self.left + 1
    }

    fn height(&self) -> u16 {
        self.bottom - self.top + 1
    }
}

impl Rectangle for ExclusiveRectangle {
    fn width(&self) -> u16 {
        self.right - self.left
    }

    fn height(&self) -> u16 {
        self.bottom - self.top
    }
}

impl InclusiveRectangle {
    const NAME: &'static str = "InclusiveRectangle";

    pub const FIXED_PART_SIZE: usize = 2 /* left */ + 2 /* top */ + 2 /* right */ + 2 /* bottom */;

    pub const ENCODED_SIZE: usize = Self::FIXED_PART_SIZE;
}

impl Encode for InclusiveRectangle {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_fixed_part_size!(in: dst);

        dst.write_u16(self.left);
        dst.write_u16(self.top);
        dst.write_u16(self.right);
        dst.write_u16(self.bottom);

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

impl<'de> Decode<'de> for InclusiveRectangle {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let left = src.read_u16();
        let top = src.read_u16();
        let right = src.read_u16();
        let bottom = src.read_u16();

        Ok(Self {
            left,
            top,
            right,
            bottom,
        })
    }
}

impl ExclusiveRectangle {
    const NAME: &'static str = "ExclusiveRectangle";
    const FIXED_PART_SIZE: usize = 2 /* left */ + 2 /* top */ + 2 /* right */ + 2 /* bottom */;

    pub const ENCODED_SIZE: usize = Self::FIXED_PART_SIZE;
}

impl Encode for ExclusiveRectangle {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_fixed_part_size!(in: dst);

        dst.write_u16(self.left);
        dst.write_u16(self.top);
        dst.write_u16(self.right);
        dst.write_u16(self.bottom);

        Ok(())
    }

    fn name(&self) -> &'static str {
        Self::NAME
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

impl<'de> Decode<'de> for ExclusiveRectangle {
    fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);

        let left = src.read_u16();
        let top = src.read_u16();
        let right = src.read_u16();
        let bottom = src.read_u16();

        Ok(Self {
            left,
            top,
            right,
            bottom,
        })
    }
}