plotive-base 0.2.0

Base library for plotive
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*!
 * Geometric primitives.
 *
 * Paths and transforms are publicly imported from tiny-skia-path.
 *
 * Y low coordinates are at the top.
 */

use strict_num::{FiniteF32, PositiveF32};
pub use tiny_skia_path::{Path, PathBuilder, PathSegment, PathVerb, Point, Transform};

/// A size in 2D space represented by width and height
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Size {
    w: f32,
    h: f32,
}

impl Size {
    /// Build a size from width and height
    pub const fn new(w: f32, h: f32) -> Self {
        Size { w, h }
    }

    /// The width
    pub const fn width(&self) -> f32 {
        self.w
    }

    /// The height
    pub const fn height(&self) -> f32 {
        self.h
    }

    /// Expand width and height by dw and dh
    pub const fn expand(&self, dw: f32, dh: f32) -> Size {
        Size {
            w: self.w + dw,
            h: self.h + dh,
        }
    }
}

/// A rectangle in 2D space represented by x, y, width and height
#[derive(Debug, Clone, Copy)]
pub struct Rect {
    x: FiniteF32,
    y: FiniteF32,
    w: PositiveF32,
    h: PositiveF32,
}

impl Rect {
    pub fn null() -> Self {
        Rect {
            x: FiniteF32::new(0.0).unwrap(),
            y: FiniteF32::new(0.0).unwrap(),
            w: PositiveF32::new(0.0).unwrap(),
            h: PositiveF32::new(0.0).unwrap(),
        }
    }

    /// Build a rectangle from x, y, width and height
    pub fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Self {
        Rect {
            x: FiniteF32::new(x).unwrap(),
            y: FiniteF32::new(y).unwrap(),
            w: PositiveF32::new(w).unwrap(),
            h: PositiveF32::new(h).unwrap(),
        }
    }

    /// Build a rectangle from top, right, bottom and left
    pub fn from_trbl(top: f32, right: f32, bottom: f32, left: f32) -> Self {
        Rect {
            x: FiniteF32::new(left).unwrap(),
            y: FiniteF32::new(top).unwrap(),
            w: PositiveF32::new(right - left).unwrap(),
            h: PositiveF32::new(bottom - top).unwrap(),
        }
    }

    /// Build a rectnagle from top left point and size
    pub fn from_ps(top_left: Point, size: Size) -> Self {
        Rect::from_xywh(top_left.x, top_left.y, size.w, size.h)
    }

    /// Build a rectangle from two corner points
    pub fn from_corners(p1: Point, p2: Point) -> Self {
        Rect::from_trbl(
            p1.y.min(p2.y),
            p1.x.max(p2.x),
            p1.y.max(p2.y),
            p1.x.min(p2.x),
        )
    }

    /// Pad the rectangle, removing padding from 4 sides
    pub fn pad(&self, padding: &Padding) -> Self {
        Rect {
            x: FiniteF32::new(self.x.get() + padding.left()).unwrap(),
            y: FiniteF32::new(self.y.get() + padding.top()).unwrap(),
            w: PositiveF32::new(self.w.get() - padding.sum_hor()).unwrap(),
            h: PositiveF32::new(self.h.get() - padding.sum_ver()).unwrap(),
        }
    }

    /// The top-left point of the rectangle
    pub const fn top_left(&self) -> Point {
        Point {
            x: self.left(),
            y: self.top(),
        }
    }

    /// The top-right point of the rectangle
    pub const fn top_right(&self) -> Point {
        Point {
            x: self.right(),
            y: self.top(),
        }
    }

    /// The bottom-right point of the rectangle
    pub const fn bottom_right(&self) -> Point {
        Point {
            x: self.right(),
            y: self.bottom(),
        }
    }

    /// The bottom-left point of the rectangle
    pub const fn bottom_left(&self) -> Point {
        Point {
            x: self.left(),
            y: self.bottom(),
        }
    }

    /// The size of the rectangle
    pub const fn size(&self) -> Size {
        Size {
            w: self.width(),
            h: self.height(),
        }
    }

    /// The X coordinate of the left side
    pub const fn x(&self) -> f32 {
        self.x.get()
    }

    /// The Y coordinate of the top side
    pub const fn y(&self) -> f32 {
        self.y.get()
    }

    /// The center point of the rectangle
    pub const fn center(&self) -> Point {
        Point {
            x: self.center_x(),
            y: self.center_y(),
        }
    }

    /// The horizontal center X coordinate
    pub const fn center_x(&self) -> f32 {
        self.x() + self.width() / 2.0
    }

    /// The vertical center Y coordinate
    pub const fn center_y(&self) -> f32 {
        self.y() + self.height() / 2.0
    }

    /// The width of the rectangle
    pub const fn width(&self) -> f32 {
        self.w.get()
    }

    /// The height of the rectangle
    pub const fn height(&self) -> f32 {
        self.h.get()
    }

    /// The top Y coordinate
    pub const fn top(&self) -> f32 {
        self.y.get()
    }

    /// The right X coordinate
    pub const fn right(&self) -> f32 {
        self.x.get() + self.w.get()
    }

    /// The bottom Y coordinate
    pub const fn bottom(&self) -> f32 {
        self.y.get() + self.h.get()
    }

    /// The left X coordinate
    pub const fn left(&self) -> f32 {
        self.x.get()
    }

    /// Set the top Y coordinate
    pub fn set_top(&mut self, top: f32) {
        self.y = FiniteF32::new(top).unwrap();
    }

    /// Set the right X coordinate
    pub fn set_right(&mut self, right: f32) {
        self.w = PositiveF32::new(right - self.x()).unwrap();
    }

    /// Set the bottom Y coordinate
    pub fn set_bottom(&mut self, bottom: f32) {
        self.h = PositiveF32::new(bottom - self.y()).unwrap();
    }

    /// Set the left X coordinate
    pub fn set_left(&mut self, left: f32) {
        self.x = FiniteF32::new(left).unwrap();
    }

    /// Shift the top side down by shift
    pub fn shifted_top_side(&self, shift: f32) -> Rect {
        Rect {
            x: self.x,
            y: FiniteF32::new(self.y.get() + shift).unwrap(),
            w: self.w,
            h: PositiveF32::new(self.h.get() - shift).unwrap(),
        }
    }

    /// Shift the right side right by shift
    pub fn shifted_right_side(&self, shift: f32) -> Rect {
        Rect {
            x: self.x,
            y: self.y,
            w: PositiveF32::new(self.w.get() + shift).unwrap(),
            h: self.h,
        }
    }

    /// Shift the bottom side down by shift
    pub fn shifted_bottom_side(&self, shift: f32) -> Rect {
        Rect {
            x: self.x,
            y: self.y,
            w: self.w,
            h: PositiveF32::new(self.h.get() + shift).unwrap(),
        }
    }

    /// Shift the left side right by shift
    pub fn shifted_left_side(&self, shift: f32) -> Rect {
        Rect {
            x: FiniteF32::new(self.x.get() + shift).unwrap(),
            y: self.y,
            w: PositiveF32::new(self.w.get() - shift).unwrap(),
            h: self.h,
        }
    }

    /// Shift the top side down by shift (in-place)
    pub fn shift_top_side(&mut self, shift: f32) {
        self.y = FiniteF32::new(self.y.get() + shift).unwrap();
        self.h = PositiveF32::new(self.h.get() - shift).unwrap();
    }

    /// Shift the right side right by shift (in-place)
    pub fn shift_right_side(&mut self, shift: f32) {
        self.w = PositiveF32::new(self.w.get() + shift).unwrap();
    }

    /// Shift the bottom side down by shift (in-place)
    pub fn shift_bottom_side(&mut self, shift: f32) {
        self.h = PositiveF32::new(self.h.get() + shift).unwrap();
    }

    /// Shift the left side right by shift (in-place)
    pub fn shift_left_side(&mut self, shift: f32) {
        self.x = FiniteF32::new(self.x.get() + shift).unwrap();
        self.w = PositiveF32::new(self.w.get() - shift).unwrap();
    }

    /// Build a copy of the rect with a new top side
    pub fn with_top(self, top: f32) -> Rect {
        let new_h = self.bottom() - top;
        Rect {
            y: FiniteF32::new(top).unwrap(),
            h: PositiveF32::new(new_h).unwrap(),
            ..self
        }
    }

    /// Build a copy of the rect with a new right side
    pub fn with_right(self, right: f32) -> Rect {
        let new_w = right - self.x();
        Rect {
            w: PositiveF32::new(new_w).unwrap(),
            ..self
        }
    }

    /// Build a copy of the rect with a new bottom side
    pub fn with_bottom(self, bottom: f32) -> Rect {
        let new_h = bottom - self.y();
        Rect {
            h: PositiveF32::new(new_h).unwrap(),
            ..self
        }
    }

    /// Build a copy of the rect with a new left side
    pub fn with_left(self, left: f32) -> Rect {
        let new_w = self.right() - left;
        Rect {
            x: FiniteF32::new(left).unwrap(),
            w: PositiveF32::new(new_w).unwrap(),
            ..self
        }
    }

    /// Unite two rectangles into one that contains both
    pub fn unite(r1: &Rect, r2: &Rect) -> Rect {
        let left = r1.left().min(r2.left());
        let top = r1.top().min(r2.top());
        let right = r1.right().max(r2.right());
        let bottom = r1.bottom().max(r2.bottom());
        Rect::from_trbl(top, right, bottom, left)
    }

    /// Unite two optional rectangles into one that contains both
    pub fn unite_opt(r1: Option<&Rect>, r2: Option<&Rect>) -> Option<Rect> {
        match (r1, r2) {
            (Some(r1), Some(r2)) => Some(Rect::unite(r1, r2)),
            (Some(r1), None) => Some(*r1),
            (None, Some(r2)) => Some(*r2),
            (None, None) => None,
        }
    }

    /// Transform the rectangle with a transform
    pub fn transform(self, transform: &Transform) -> Rect {
        let mut tlbr = [
            Point {
                x: self.left(),
                y: self.top(),
            },
            Point {
                x: self.right(),
                y: self.bottom(),
            },
        ];
        transform.map_points(&mut tlbr);

        let [p1, p2] = tlbr;
        let x = p1.x.min(p2.x);
        let y = p1.y.min(p2.y);
        let width = (p2.x - p1.x).abs();
        let height = (p2.y - p1.y).abs();

        Rect::from_xywh(x, y, width, height)
    }

    /// Translate the rectangle by dx and dy
    pub fn translate(&self, dx: f32, dy: f32) -> Rect {
        Rect {
            x: FiniteF32::new(self.x.get() + dx).unwrap(),
            y: FiniteF32::new(self.y.get() + dy).unwrap(),
            w: self.w,
            h: self.h,
        }
    }

    /// Scale the rectangle about a center point
    ///
    /// Panics if the scale factor is not positive.
    pub fn scale_about(&self, center: Point, scale_factor: f32) -> Self {
        assert!(scale_factor > 0.0, "Scale factor must be positive");

        let width = self.width() * scale_factor;
        let height = self.height() * scale_factor;

        let left = center.x - (center.x - self.left()) * scale_factor;
        let top = center.y - (center.y - self.top()) * scale_factor;

        Rect::from_xywh(left, top, width, height)
    }

    /// Test if the rectangle contains a point
    pub fn contains_point(&self, point: &Point) -> bool {
        point.x >= self.left()
            && point.x <= self.right()
            && point.y >= self.top()
            && point.y <= self.bottom()
    }

    /// Build a path from the rectangle
    pub fn to_path(&self) -> Path {
        PathBuilder::from_rect(
            tiny_skia_path::Rect::from_xywh(self.x.get(), self.y.get(), self.w.get(), self.h.get())
                .unwrap(),
        )
    }
}

/// Padding within a graphical element
#[derive(Debug, Clone, Copy)]
pub enum Padding {
    /// Uniform padding in all directions
    Even(f32),
    /// Vertical and horizontal padding
    Center {
        /// Vertical padding
        v: f32,
        /// Horizontal padding
        h: f32,
    },
    /// Top, right, bottom and left padding
    Custom {
        /// Top padding
        t: f32,
        /// Right padding
        r: f32,
        /// Bottom padding
        b: f32,
        /// Left padding
        l: f32,
    },
}

impl Padding {
    /// The top padding
    pub const fn top(&self) -> f32 {
        match self {
            Padding::Even(p) => *p,
            Padding::Center { v, .. } => *v,
            Padding::Custom { t, .. } => *t,
        }
    }

    /// The right padding
    pub const fn right(&self) -> f32 {
        match self {
            Padding::Even(p) => *p,
            Padding::Center { h, .. } => *h,
            Padding::Custom { r, .. } => *r,
        }
    }

    /// The bottom padding
    pub const fn bottom(&self) -> f32 {
        match self {
            Padding::Even(p) => *p,
            Padding::Center { v, .. } => *v,
            Padding::Custom { b, .. } => *b,
        }
    }

    /// The left padding
    pub const fn left(&self) -> f32 {
        match self {
            Padding::Even(p) => *p,
            Padding::Center { h, .. } => *h,
            Padding::Custom { l, .. } => *l,
        }
    }

    /// The total vertical padding
    pub const fn sum_ver(&self) -> f32 {
        match self {
            Padding::Even(p) => *p * 2.0,
            Padding::Center { v, .. } => *v * 2.0,
            Padding::Custom { t, b, .. } => *t + *b,
        }
    }

    /// The total horizontal padding
    pub const fn sum_hor(&self) -> f32 {
        match self {
            Padding::Even(p) => *p * 2.0,
            Padding::Center { h, .. } => *h * 2.0,
            Padding::Custom { l, r, .. } => *l + *r,
        }
    }
}

impl From<f32> for Padding {
    fn from(value: f32) -> Self {
        Padding::Even(value)
    }
}

impl From<(f32, f32)> for Padding {
    fn from((v, h): (f32, f32)) -> Self {
        Padding::Center { v, h }
    }
}

impl From<(f32, f32, f32, f32)> for Padding {
    fn from((t, r, b, l): (f32, f32, f32, f32)) -> Self {
        Padding::Custom { t, r, b, l }
    }
}

/// Margin around a graphical element
#[derive(Debug, Clone, Copy)]
pub enum Margin {
    /// Uniform margin in all directions
    Even(f32),
    /// Vertical and horizontal margin
    Center {
        /// Vertical margin
        v: f32,
        /// Horizontal margin
        h: f32,
    },
    /// Top, right, bottom and left margin
    Custom {
        /// Top margin
        t: f32,
        /// Right margin
        r: f32,
        /// Bottom margin
        b: f32,
        /// Left margin
        l: f32,
    },
}

impl Margin {
    /// The top margin
    pub const fn top(&self) -> f32 {
        match self {
            Margin::Even(p) => *p,
            Margin::Center { v, .. } => *v,
            Margin::Custom { t, .. } => *t,
        }
    }

    /// The right margin
    pub const fn right(&self) -> f32 {
        match self {
            Margin::Even(p) => *p,
            Margin::Center { h, .. } => *h,
            Margin::Custom { r, .. } => *r,
        }
    }

    /// The bottom margin
    pub const fn bottom(&self) -> f32 {
        match self {
            Margin::Even(p) => *p,
            Margin::Center { v, .. } => *v,
            Margin::Custom { b, .. } => *b,
        }
    }

    /// The left margin
    pub const fn left(&self) -> f32 {
        match self {
            Margin::Even(p) => *p,
            Margin::Center { h, .. } => *h,
            Margin::Custom { l, .. } => *l,
        }
    }

    /// The total vertical margin
    pub const fn sum_ver(&self) -> f32 {
        match self {
            Margin::Even(p) => *p * 2.0,
            Margin::Center { v, .. } => *v * 2.0,
            Margin::Custom { t, b, .. } => *t + *b,
        }
    }

    /// The total horizontal margin
    pub const fn sum_hor(&self) -> f32 {
        match self {
            Margin::Even(p) => *p * 2.0,
            Margin::Center { h, .. } => *h * 2.0,
            Margin::Custom { l, r, .. } => *l + *r,
        }
    }
}

impl From<f32> for Margin {
    fn from(value: f32) -> Self {
        Margin::Even(value)
    }
}

impl From<(f32, f32)> for Margin {
    fn from((v, h): (f32, f32)) -> Self {
        Margin::Center { v, h }
    }
}

impl From<(f32, f32, f32, f32)> for Margin {
    fn from((t, r, b, l): (f32, f32, f32, f32)) -> Self {
        Margin::Custom { t, r, b, l }
    }
}