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
#![no_std]
#![allow(unused)] // FIXME:
#![allow(clippy::from_over_into)]

#[macro_use]
extern crate std;
extern crate alloc;
#[macro_use]
extern crate log;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;

pub mod canvas;
pub mod client;
mod icons;
pub mod server;
pub mod standalone;
mod utils;
pub mod wl;

pub use utils::mmap;

use core::cmp::{max, min};
use core::fmt;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PixelFormat {
    RGBA8888,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Position {
    pub x: usize,
    pub y: usize,
}

impl Position {
    pub const fn new(x: usize, y: usize) -> Position {
        Position { x, y }
    }

    pub const fn zero() -> Position {
        Position::new(0, 0)
    }

    pub fn clamp(self, min_pos: Position, max_pos: Position) -> Position {
        Position::new(
            max(min_pos.x, min(self.x, max_pos.x)),
            max(min_pos.y, min(self.y, max_pos.y)),
        )
    }
}

impl core::ops::Add for Position {
    type Output = Position;
    fn add(self, rhs: Position) -> Self::Output {
        Position::new(self.x + rhs.x, self.y + rhs.y)
    }
}

impl core::ops::Sub for Position {
    type Output = Position;
    fn sub(self, rhs: Position) -> Self::Output {
        Position::new(self.x - rhs.x, self.y - rhs.y)
    }
}

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RectSize {
    pub width: usize,
    pub height: usize,
}

impl RectSize {
    pub const fn new(width: usize, height: usize) -> RectSize {
        RectSize { width, height }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RectArea {
    pub pos: Position,
    pub size: RectSize,
}

impl RectArea {
    pub const fn new(pos: Position, size: RectSize) -> RectArea {
        RectArea { pos, size }
    }

    pub fn overlapping(self, other: RectArea) -> Option<RectArea> {
        let p1 = self.pos;
        let p2 = other.pos;
        let s1 = self.size;
        let s2 = other.size;
        let non_overlapping = p1.x + s1.width < p2.x
            || p2.x + s2.width < p1.x
            || p1.y + s1.height < p2.y
            || p2.y + s2.height < p1.y;
        if non_overlapping {
            return None;
        }

        let start_x = max(p1.x, p2.x);
        let start_y = max(p1.y, p2.y);
        let end_x = min(p1.x + s1.width, p2.x + s2.width);
        let end_y = min(p1.y + s1.height, p2.y + s2.height);

        Some(RectArea::new(
            Position::new(start_x, start_y),
            RectSize::new(end_x - start_x, end_y - start_y),
        ))
    }

    pub fn contains_position(&self, pos: Position) -> bool {
        self.pos.x <= pos.x
            && pos.x < self.pos.x + self.size.width
            && self.pos.y <= pos.y
            && pos.y < self.pos.y + self.size.height
    }
}

#[derive(Debug, Clone, Copy)]
pub enum Color {
    Rgba8888 { r: u8, g: u8, b: u8, a: u8 },
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontFamily {
    Default,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontStyle {
    Regular,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontSize {
    Normal,
}

#[derive(Debug, Clone, Copy)]
pub struct FillStyle {
    pub color: Color,
}

#[derive(Debug, Clone, Copy)]
pub struct BorderStyle {
    pub color: Color,
    pub width: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rect_area_overlapping() {
        let a = RectArea::new(Position::new(20, 20), RectSize::new(10, 10));
        let b = RectArea::new(Position::new(19, 20), RectSize::new(10, 10));
        assert_eq!(
            a.overlapping(b),
            Some(RectArea::new(Position::new(20, 20), RectSize::new(9, 10)))
        );
    }
}