skia_safe/core/
font_style.rs

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
use crate::prelude::*;
use skia_bindings::{self as sb, SkFontStyle, SkFontStyle_Weight, SkFontStyle_Width};
use std::{fmt, 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, Eq, PartialOrd, Ord, Debug)]
#[repr(transparent)]
pub struct Weight(i32);

native_transmutable!(i32, Weight, weight_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 {
    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, Eq, PartialOrd, Ord, Debug)]
#[repr(transparent)]
pub struct Width(i32);

native_transmutable!(i32, Width, width_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 {
    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 _);
}

pub use skia_bindings::SkFontStyle_Slant as Slant;
variant_name!(Slant::Upright);

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

native_transmutable!(SkFontStyle, FontStyle, font_style_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 fmt::Debug for FontStyle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FontStyle")
            .field("weight", &self.weight())
            .field("width", &self.width())
            .field("slant", &self.slant())
            .finish()
    }
}

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)
        })
    }

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

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

    pub fn slant(self) -> Slant {
        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);
}