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
#[cfg(feature = "winit")]
use winit::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize, Pixel};
mod vector;
pub use vector::{DVec2, Quad, Vec2, Vec3};
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Coord(pub i32, pub i32);
impl Coord {
pub const ZERO: Coord = Coord(0, 0);
#[inline]
pub fn uniform(n: i32) -> Self {
Coord(n, n)
}
#[inline]
pub fn min(self, other: Self) -> Self {
Coord(self.0.min(other.0), self.1.min(other.1))
}
#[inline]
pub fn max(self, other: Self) -> Self {
Coord(self.0.max(other.0), self.1.max(other.1))
}
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self {
self.min(max).max(min)
}
#[inline]
pub fn transpose(self) -> Self {
Coord(self.1, self.0)
}
#[cfg(feature = "winit")]
pub fn from_logical<X: Pixel>(logical: LogicalPosition<X>, dpi_factor: f64) -> Self {
let pos = PhysicalPosition::<i32>::from_logical(logical, dpi_factor);
let pos: (i32, i32) = pos.into();
Coord(pos.0, pos.1)
}
}
impl From<(i32, i32)> for Coord {
#[inline]
fn from(coord: (i32, i32)) -> Coord {
Coord(coord.0, coord.1)
}
}
impl From<Size> for Coord {
#[inline]
fn from(size: Size) -> Coord {
Coord(size.0 as i32, size.1 as i32)
}
}
impl std::ops::Add for Coord {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Coord(self.0 + other.0, self.1 + other.1)
}
}
impl std::ops::Sub for Coord {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Coord(self.0 - other.0, self.1 - other.1)
}
}
impl std::ops::Add<Size> for Coord {
type Output = Self;
#[inline]
fn add(self, other: Size) -> Self {
Coord(self.0 + other.0 as i32, self.1 + other.1 as i32)
}
}
impl From<Coord> for kas_text::Vec2 {
fn from(pos: Coord) -> kas_text::Vec2 {
kas_text::Vec2(pos.0 as f32, pos.1 as f32)
}
}
#[cfg(feature = "winit")]
impl<X: Pixel> From<PhysicalPosition<X>> for Coord {
#[inline]
fn from(pos: PhysicalPosition<X>) -> Coord {
let pos: (i32, i32) = pos.cast::<i32>().into();
Coord(pos.0, pos.1)
}
}
#[cfg(feature = "winit")]
impl<X: Pixel> From<Coord> for PhysicalPosition<X> {
#[inline]
fn from(coord: Coord) -> PhysicalPosition<X> {
let pos: PhysicalPosition<i32> = (coord.0, coord.1).into();
pos.cast()
}
}
impl std::ops::AddAssign<Coord> for Coord {
#[inline]
fn add_assign(&mut self, rhs: Coord) {
self.0 += rhs.0;
self.1 += rhs.1;
}
}
impl std::ops::AddAssign<Size> for Coord {
#[inline]
fn add_assign(&mut self, rhs: Size) {
self.0 += rhs.0 as i32;
self.1 += rhs.1 as i32;
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct Size(pub u32, pub u32);
impl Size {
pub const ZERO: Size = Size(0, 0);
#[inline]
pub const fn uniform(v: u32) -> Self {
Size(v, v)
}
#[inline]
pub fn min(self, other: Self) -> Self {
Size(self.0.min(other.0), self.1.min(other.1))
}
#[inline]
pub fn max(self, other: Self) -> Self {
Size(self.0.max(other.0), self.1.max(other.1))
}
#[inline]
pub fn transpose(self) -> Self {
Size(self.1, self.0)
}
}
impl From<(u32, u32)> for Size {
fn from(size: (u32, u32)) -> Size {
Size(size.0, size.1)
}
}
impl From<Coord> for Size {
fn from(coord: Coord) -> Size {
Size(coord.0 as u32, coord.1 as u32)
}
}
#[cfg(feature = "winit")]
impl<X: Pixel> From<PhysicalSize<X>> for Size {
#[inline]
fn from(size: PhysicalSize<X>) -> Size {
let size: (u32, u32) = size.cast::<u32>().into();
Size(size.0, size.1)
}
}
#[cfg(feature = "winit")]
impl<X: Pixel> From<Size> for PhysicalSize<X> {
#[inline]
fn from(size: Size) -> PhysicalSize<X> {
let pos: PhysicalSize<u32> = (size.0, size.1).into();
pos.cast()
}
}
#[cfg(feature = "winit")]
impl From<Size> for winit::dpi::Size {
#[inline]
fn from(size: Size) -> winit::dpi::Size {
winit::dpi::Size::Physical((size.0, size.1).into())
}
}
impl From<Size> for kas_text::Vec2 {
fn from(size: Size) -> kas_text::Vec2 {
kas_text::Vec2(size.0 as f32, size.1 as f32)
}
}
impl std::ops::Add for Size {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Size(self.0 + other.0, self.1 + other.1)
}
}
impl std::ops::Sub for Size {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Size(self.0 - other.0, self.1 - other.1)
}
}
impl std::ops::Mul<u32> for Size {
type Output = Self;
#[inline]
fn mul(self, x: u32) -> Self {
Size(self.0 * x, self.1 * x)
}
}
impl std::ops::Mul<f32> for Size {
type Output = Self;
#[inline]
fn mul(self, x: f32) -> Self {
Size((self.0 as f32 * x) as u32, (self.1 as f32 * x) as u32)
}
}
impl std::ops::Div<u32> for Size {
type Output = Self;
#[inline]
fn div(self, x: u32) -> Self {
Size(self.0 / x, self.1 / x)
}
}
impl std::ops::AddAssign for Size {
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.1 += rhs.1;
}
}
impl std::ops::SubAssign for Size {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
self.1 -= rhs.1;
}
}
#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Rect {
pub pos: Coord,
pub size: Size,
}
impl Rect {
#[inline]
pub fn new(pos: Coord, size: Size) -> Self {
Rect { pos, size }
}
#[inline]
pub fn pos_end(&self) -> Coord {
self.pos + self.size
}
#[inline]
pub fn contains(&self, c: Coord) -> bool {
c.0 >= self.pos.0
&& c.0 < self.pos.0 + (self.size.0 as i32)
&& c.1 >= self.pos.1
&& c.1 < self.pos.1 + (self.size.1 as i32)
}
#[inline]
pub fn shrink(&self, n: u32) -> Rect {
let pos = self.pos + Coord::uniform(n as i32);
let w = self.size.0.saturating_sub(n + n);
let h = self.size.1.saturating_sub(n + n);
let size = Size(w, h);
Rect { pos, size }
}
}
impl std::ops::Add<Coord> for Rect {
type Output = Self;
#[inline]
fn add(self, offset: Coord) -> Self {
let pos = self.pos + offset;
Rect {
pos,
size: self.size,
}
}
}
impl std::ops::Sub<Coord> for Rect {
type Output = Self;
#[inline]
fn sub(self, offset: Coord) -> Self {
let pos = self.pos - offset;
Rect {
pos,
size: self.size,
}
}
}