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
use crate::prelude::*;
use skia_bindings as sb;
use skia_bindings::{SkFontStyle, SkFontStyle_Slant, SkFontStyle_Weight, SkFontStyle_Width};
use std::ops::Deref;

/// Wrapper type of a font weight.
///
/// Use Weight::from() to create a weight from an i32.
/// Use *weight to pull out the wrapped value of the Weight.
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Weight(i32);

impl NativeTransmutable<i32> for Weight {}

#[test]
fn test_weight_layout() {
    Weight::test_layout()
}

impl From<i32> for Weight {
    fn from(weight: i32) -> Self {
        Weight(weight)
    }
}

impl Deref for Weight {
    type Target = i32;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[allow(non_upper_case_globals)]
impl Weight {
    #[deprecated(note = "use INVISIBLE")]
    pub const Invisible: Self = Self(SkFontStyle_Weight::kInvisible_Weight as _);
    #[deprecated(note = "use THIN")]
    pub const Thin: Self = Self(SkFontStyle_Weight::kThin_Weight as _);
    #[deprecated(note = "use EXTRA_LIGHT")]
    pub const ExtraLight: Self = Self(SkFontStyle_Weight::kExtraLight_Weight as _);
    #[deprecated(note = "use LIGHT")]
    pub const Light: Self = Self(SkFontStyle_Weight::kLight_Weight as _);
    #[deprecated(note = "use NORMAL")]
    pub const Normal: Self = Self(SkFontStyle_Weight::kNormal_Weight as _);
    #[deprecated(note = "use MEDIUM")]
    pub const Medium: Self = Self(SkFontStyle_Weight::kMedium_Weight as _);
    #[deprecated(note = "use SEMI_BOLD")]
    pub const SemiBold: Self = Self(SkFontStyle_Weight::kSemiBold_Weight as _);
    #[deprecated(note = "use BOLD")]
    pub const Bold: Self = Self(SkFontStyle_Weight::kBold_Weight as _);
    #[deprecated(note = "use EXTRA_BOLD")]
    pub const ExtraBold: Self = Self(SkFontStyle_Weight::kExtraBold_Weight as _);
    #[deprecated(note = "use BLACK")]
    pub const Black: Self = Self(SkFontStyle_Weight::kBlack_Weight as _);
    #[deprecated(note = "use EXTRA_BLACK")]
    pub const ExtraBlack: Self = Self(SkFontStyle_Weight::kExtraBlack_Weight as _);

    pub const INVISIBLE: Self = Self(SkFontStyle_Weight::kInvisible_Weight as _);
    pub const THIN: Self = Self(SkFontStyle_Weight::kThin_Weight as _);
    pub const EXTRA_LIGHT: Self = Self(SkFontStyle_Weight::kExtraLight_Weight as _);
    pub const LIGHT: Self = Self(SkFontStyle_Weight::kLight_Weight as _);
    pub const NORMAL: Self = Self(SkFontStyle_Weight::kNormal_Weight as _);
    pub const MEDIUM: Self = Self(SkFontStyle_Weight::kMedium_Weight as _);
    pub const SEMI_BOLD: Self = Self(SkFontStyle_Weight::kSemiBold_Weight as _);
    pub const BOLD: Self = Self(SkFontStyle_Weight::kBold_Weight as _);
    pub const EXTRA_BOLD: Self = Self(SkFontStyle_Weight::kExtraBold_Weight as _);
    pub const BLACK: Self = Self(SkFontStyle_Weight::kBlack_Weight as _);
    pub const EXTRA_BLACK: Self = Self(SkFontStyle_Weight::kExtraBlack_Weight as _);
}

/// Wrapper type for the width of a font.
///
/// To create a width of a font from an i32, use Width::from().
/// To access the underlying value of the font weight, dereference *weight.
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Width(i32);

impl NativeTransmutable<i32> for Width {}

#[test]
fn test_width_layout() {
    Width::test_layout()
}

impl From<i32> for Width {
    fn from(width: i32) -> Self {
        Width(width)
    }
}

impl Deref for Width {
    type Target = i32;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[allow(non_upper_case_globals)]
impl Width {
    #[deprecated(note = "use ULTRA_CONDENSED")]
    pub const UltraCondensed: Self = Self(SkFontStyle_Width::kUltraCondensed_Width as _);
    #[deprecated(note = "use EXTRA_CONDENSED")]
    pub const ExtraCondensed: Self = Self(SkFontStyle_Width::kExtraCondensed_Width as _);
    #[deprecated(note = "use CONDENSED")]
    pub const Condensed: Self = Self(SkFontStyle_Width::kCondensed_Width as _);
    #[deprecated(note = "use SEMI_CONDENSED")]
    pub const SemiCondensed: Self = Self(SkFontStyle_Width::kSemiCondensed_Width as _);
    #[deprecated(note = "use NORMAL")]
    pub const Normal: Self = Self(SkFontStyle_Width::kNormal_Width as _);
    #[deprecated(note = "use SEMI_EXPANDED")]
    pub const SemiExpanded: Self = Self(SkFontStyle_Width::kSemiExpanded_Width as _);
    #[deprecated(note = "use EXPANDED")]
    pub const Expanded: Self = Self(SkFontStyle_Width::kExpanded_Width as _);
    #[deprecated(note = "use EXTRA_EXPANDED")]
    pub const ExtraExpanded: Self = Self(SkFontStyle_Width::kExtraExpanded_Width as _);
    #[deprecated(note = "use ULTRA_EXPANDED")]
    pub const UltraExpanded: Self = Self(SkFontStyle_Width::kUltraExpanded_Width as _);

    pub const ULTRA_CONDENSED: Self = Self(SkFontStyle_Width::kUltraCondensed_Width as _);
    pub const EXTRA_CONDENSED: Self = Self(SkFontStyle_Width::kExtraCondensed_Width as _);
    pub const CONDENSED: Self = Self(SkFontStyle_Width::kCondensed_Width as _);
    pub const SEMI_CONDENSED: Self = Self(SkFontStyle_Width::kSemiCondensed_Width as _);
    pub const NORMAL: Self = Self(SkFontStyle_Width::kNormal_Width as _);
    pub const SEMI_EXPANDED: Self = Self(SkFontStyle_Width::kSemiExpanded_Width as _);
    pub const EXPANDED: Self = Self(SkFontStyle_Width::kExpanded_Width as _);
    pub const EXTRA_EXPANDED: Self = Self(SkFontStyle_Width::kExtraExpanded_Width as _);
    pub const ULTRA_EXPANDED: Self = Self(SkFontStyle_Width::kUltraExpanded_Width as _);
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum Slant {
    Upright = SkFontStyle_Slant::kUpright_Slant as _,
    Italic = SkFontStyle_Slant::kItalic_Slant as _,
    Oblique = SkFontStyle_Slant::kOblique_Slant as _,
}

impl NativeTransmutable<SkFontStyle_Slant> for Slant {}

#[test]
fn test_slant_layout() {
    Slant::test_layout()
}

// TODO: implement Display
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct FontStyle(SkFontStyle);

impl NativeTransmutable<SkFontStyle> for FontStyle {}
#[test]
fn test_font_style_layout() {
    FontStyle::test_layout()
}

impl PartialEq for FontStyle {
    fn eq(&self, rhs: &Self) -> bool {
        unsafe { sb::C_SkFontStyle_Equals(self.native(), rhs.native()) }
    }
}

impl Default for FontStyle {
    fn default() -> Self {
        FontStyle::construct(|fs| unsafe { sb::C_SkFontStyle_Construct(fs) })
    }
}

impl FontStyle {
    pub fn new(weight: Weight, width: Width, slant: Slant) -> Self {
        Self::construct(|fs| unsafe {
            sb::C_SkFontStyle_Construct2(
                fs,
                weight.into_native(),
                width.into_native(),
                slant.into_native(),
            )
        })
    }

    pub fn weight(self) -> Weight {
        Weight::from_native(unsafe { sb::C_SkFontStyle_weight(self.native()) })
    }

    pub fn width(self) -> Width {
        Width::from_native(unsafe { sb::C_SkFontStyle_width(self.native()) })
    }

    pub fn slant(self) -> Slant {
        Slant::from_native(unsafe { sb::C_SkFontStyle_slant(self.native()) })
    }

    pub fn normal() -> FontStyle {
        *font_style_static::NORMAL
    }

    pub fn bold() -> FontStyle {
        *font_style_static::BOLD
    }

    pub fn italic() -> FontStyle {
        *font_style_static::ITALIC
    }

    pub fn bold_italic() -> FontStyle {
        *font_style_static::BOLD_ITALIC
    }
}

mod font_style_static {
    use super::{FontStyle, Slant, Weight, Width};

    lazy_static! {
        pub static ref NORMAL: FontStyle =
            FontStyle::new(Weight::NORMAL, Width::NORMAL, Slant::Upright);
        pub static ref BOLD: FontStyle =
            FontStyle::new(Weight::BOLD, Width::NORMAL, Slant::Upright);
        pub static ref ITALIC: FontStyle =
            FontStyle::new(Weight::NORMAL, Width::NORMAL, Slant::Italic);
        pub static ref BOLD_ITALIC: FontStyle =
            FontStyle::new(Weight::BOLD, Width::NORMAL, Slant::Italic);
    }
}

#[test]
fn test_equality() {
    let style: FontStyle = Default::default();
    let style2: FontStyle = Default::default();
    assert!(style == style2);
}