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
use crate::prelude::*;
use crate::scalar;
use skia_bindings as sb;
use skia_bindings::{SkISize, SkSize};

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
pub struct ISize {
    pub width: i32,
    pub height: i32,
}

impl NativeTransmutable<SkISize> for ISize {}

#[test]
fn test_isize_layout() {
    ISize::test_layout()
}

impl ISize {
    pub const fn new(w: i32, h: i32) -> ISize {
        ISize {
            width: w,
            height: h,
        }
    }

    pub fn new_empty() -> ISize {
        Self::new(0, 0)
    }

    pub fn set(&mut self, w: i32, h: i32) {
        *self = Self::new(w, h);
    }

    pub fn is_zero(self) -> bool {
        self.width == 0 && self.height == 0
    }

    pub fn is_empty(self) -> bool {
        self.width <= 0 || self.height <= 0
    }

    pub fn set_empty(&mut self) {
        *self = Self::new_empty();
    }

    // TODO: should the functions with() and height() be supported?

    pub fn equals(self, w: i32, h: i32) -> bool {
        self == Self::new(w, h)
    }
}

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Default, Debug)]
pub struct Size {
    pub width: scalar,
    pub height: scalar,
}

impl NativeTransmutable<SkSize> for Size {}

#[test]
fn test_size_layout() {
    Size::test_layout()
}

impl Size {
    pub const fn new(w: scalar, h: scalar) -> Size {
        Size {
            width: w,
            height: h,
        }
    }

    pub fn from_isize(src: ISize) -> Size {
        Self::new(src.width as _, src.height as _)
    }

    pub fn new_empty() -> Self {
        Self::new(0.0, 0.0)
    }

    pub fn set(&mut self, w: scalar, h: scalar) {
        *self = Self::new(w, h);
    }

    pub fn is_zero(self) -> bool {
        self.width == 0.0 && self.height == 0.0
    }

    pub fn is_empty(self) -> bool {
        self.width <= 0.0 || self.height <= 0.0
    }

    pub fn set_empty(&mut self) {
        *self = Self::new_empty()
    }

    // TODO: should width() and height() be supported?

    pub fn equals(self, w: scalar, h: scalar) -> bool {
        self == Self::new(w, h)
    }

    pub fn to_round(self) -> ISize {
        ISize::from_native(unsafe { sb::C_SkSize_toRound(self.native()) })
    }

    pub fn to_ceil(self) -> ISize {
        ISize::from_native(unsafe { sb::C_SkSize_toCeil(self.native()) })
    }

    pub fn to_floor(self) -> ISize {
        ISize::from_native(unsafe { sb::C_SkSize_toFloor(self.native()) })
    }
}

//
// From
//

impl From<(i32, i32)> for ISize {
    fn from(source: (i32, i32)) -> Self {
        Self::new(source.0, source.1)
    }
}

impl From<(scalar, scalar)> for Size {
    fn from(source: (scalar, scalar)) -> Self {
        Self::new(source.0, source.1)
    }
}

impl From<ISize> for Size {
    fn from(size: ISize) -> Self {
        Self::new(size.width as _, size.height as _)
    }
}

// TODO: this is experimental.
impl From<(i32, i32)> for Size {
    fn from(source: (i32, i32)) -> Self {
        (source.0 as scalar, source.1 as scalar).into()
    }
}