Skip to content

Commit 28e6f7e

Browse files
authored
Merge pull request #8 from kwonoj/fix-fontweight
refactor(fontweight): allow unknown fontweight value
2 parents 2d2b4c8 + 7ca7c5c commit 28e6f7e

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

src/font.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Font {
5050

5151
pub fn weight(&self) -> FontWeight {
5252
unsafe {
53-
mem::transmute::<u32, FontWeight>((*self.native.get()).GetWeight())
53+
FontWeight::from_u32((*self.native.get()).GetWeight().0)
5454
}
5555
}
5656

src/types.rs

+46-16
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,58 @@ use std::mem;
77
use winapi::um::dwrite::{DWRITE_FONT_STYLE, DWRITE_FONT_WEIGHT, DWRITE_FONT_STRETCH};
88

99
// mirrors DWRITE_FONT_WEIGHT
10-
#[repr(u32)]
1110
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Copy)]
1211
pub enum FontWeight {
13-
Thin = 100,
14-
ExtraLight = 200,
15-
Light = 300,
16-
SemiLight = 350,
17-
Regular = 400,
18-
Medium = 500,
19-
SemiBold = 600,
20-
Bold = 700,
21-
ExtraBold = 800,
22-
Black = 900,
23-
ExtraBlack = 950,
12+
Thin,
13+
ExtraLight,
14+
Light,
15+
SemiLight,
16+
Regular,
17+
Medium,
18+
SemiBold,
19+
Bold,
20+
ExtraBold,
21+
Black,
22+
ExtraBlack,
23+
Unknown(u32)
2424
}
2525

2626
impl FontWeight {
27-
fn t(&self) -> DWRITE_FONT_WEIGHT {
28-
unsafe { mem::transmute::<FontWeight, DWRITE_FONT_WEIGHT>(*self) }
27+
fn t(&self) -> winapi::DWRITE_FONT_WEIGHT {
28+
unsafe { mem::transmute::<u32, winapi::DWRITE_FONT_WEIGHT>(self.to_u32()) }
29+
}
30+
pub fn to_u32(&self) -> u32 {
31+
match self {
32+
FontWeight::Thin=> 100,
33+
FontWeight::ExtraLight=> 200,
34+
FontWeight::Light=> 300,
35+
FontWeight::SemiLight=> 350,
36+
FontWeight::Regular=> 400,
37+
FontWeight::Medium=> 500,
38+
FontWeight::SemiBold=> 600,
39+
FontWeight::Bold=> 700,
40+
FontWeight::ExtraBold=> 800,
41+
FontWeight::Black=> 900,
42+
FontWeight::ExtraBlack=> 950,
43+
FontWeight::Unknown(v) => *v as u32
44+
}
45+
}
46+
pub fn from_u32(v: u32) -> FontWeight {
47+
match v {
48+
100 => FontWeight::Thin,
49+
200 => FontWeight::ExtraLight,
50+
300 => FontWeight::Light,
51+
350 => FontWeight::SemiLight,
52+
400 => FontWeight::Regular,
53+
500 => FontWeight::Medium,
54+
600 => FontWeight::SemiBold,
55+
700 => FontWeight::Bold,
56+
800 => FontWeight::ExtraBold,
57+
900 => FontWeight::Black,
58+
950 => FontWeight::ExtraBlack,
59+
_ => FontWeight::Unknown(v)
60+
}
2961
}
30-
pub fn to_u32(&self) -> u32 { unsafe { mem::transmute::<FontWeight, u32>(*self) } }
31-
pub fn from_u32(v: u32) -> FontWeight { unsafe { mem::transmute::<u32, FontWeight>(v) } }
3262
}
3363

3464
// mirrors DWRITE_FONT_STRETCH

0 commit comments

Comments
 (0)