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
// page.rs
//
// Copyright (c) 2021  Douglas P Lau
//

/// Page aspect ratio
#[derive(Clone, Copy)]
pub enum AspectRatio {
    Landscape,
    Square,
    Portrait,
}

/// Edge of rendered item
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Edge {
    Top,
    Left,
    Bottom,
    Right,
}

/// Rendering rectangle
#[derive(Clone, Copy)]
pub struct Rect {
    pub x: i32,
    pub y: i32,
    pub width: u16,
    pub height: u16,
}

impl AspectRatio {
    pub(crate) fn rect(self) -> Rect {
        match self {
            AspectRatio::Landscape => Rect::new(0, 0, 2000, 1500),
            AspectRatio::Square => Rect::new(0, 0, 2000, 2000),
            AspectRatio::Portrait => Rect::new(0, 0, 1500, 2000),
        }
    }
}

impl Rect {
    pub fn new(x: i32, y: i32, width: u16, height: u16) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }
    pub fn right(&self) -> i32 {
        self.x + i32::from(self.width)
    }
    pub fn bottom(&self) -> i32 {
        self.y + i32::from(self.height)
    }
    pub fn inset(mut self, value: u16) -> Self {
        let vi = i32::from(value);
        self.x += vi;
        self.y += vi;
        let v2 = 2 * value;
        self.width = self.width.saturating_sub(v2);
        self.height = self.height.saturating_sub(v2);
        self
    }

    pub fn split(&mut self, edge: Edge, value: u16) -> Self {
        match edge {
            Edge::Top => {
                let y = self.y;
                let height = self.height.saturating_sub(value);
                let h = self.height - height;
                self.y += h as i32;
                self.height = height;
                Rect::new(self.x, y, self.width, h)
            }
            Edge::Left => {
                let x = self.x;
                let width = self.width.saturating_sub(value);
                let w = self.width - width;
                self.x += w as i32;
                self.width = width;
                Rect::new(x, self.y, w, self.height)
            }
            Edge::Bottom => {
                let height = self.height.saturating_sub(value);
                let h = self.height - height;
                let y = self.y + i32::from(height);
                self.height = height;
                Rect::new(self.x, y, self.width, h)
            }
            Edge::Right => {
                let width = self.width.saturating_sub(value);
                let w = self.width - width;
                let x = self.x + i32::from(width);
                self.width = width;
                Rect::new(x, self.y, w, self.height)
            }
        }
    }
    pub fn intersect_horiz(&mut self, rhs: &Rect) {
        let x = self.x.max(rhs.x);
        let x2 = self.right().min(rhs.right());
        self.x = x;
        self.width = (x2 - x) as u16;
    }
    pub fn intersect_vert(&mut self, rhs: &Rect) {
        let y = self.y.max(rhs.y);
        let y2 = self.bottom().min(rhs.bottom());
        self.y = y;
        self.height = (y2 - y) as u16;
    }
}