Skip to content

refactor(fontweight): allow unknown fontweight value #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Font {

pub fn weight(&self) -> FontWeight {
unsafe {
mem::transmute::<u32, FontWeight>((*self.native.get()).GetWeight())
FontWeight::from_u32((*self.native.get()).GetWeight().0)
}
}

Expand Down
62 changes: 46 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,58 @@ use std::mem;
use winapi::um::dwrite::{DWRITE_FONT_STYLE, DWRITE_FONT_WEIGHT, DWRITE_FONT_STRETCH};

// mirrors DWRITE_FONT_WEIGHT
#[repr(u32)]
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Copy)]
pub enum FontWeight {
Thin = 100,
ExtraLight = 200,
Light = 300,
SemiLight = 350,
Regular = 400,
Medium = 500,
SemiBold = 600,
Bold = 700,
ExtraBold = 800,
Black = 900,
ExtraBlack = 950,
Thin,
ExtraLight,
Light,
SemiLight,
Regular,
Medium,
SemiBold,
Bold,
ExtraBold,
Black,
ExtraBlack,
Unknown(u32)
}

impl FontWeight {
fn t(&self) -> DWRITE_FONT_WEIGHT {
unsafe { mem::transmute::<FontWeight, DWRITE_FONT_WEIGHT>(*self) }
fn t(&self) -> winapi::DWRITE_FONT_WEIGHT {
unsafe { mem::transmute::<u32, winapi::DWRITE_FONT_WEIGHT>(self.to_u32()) }
}
pub fn to_u32(&self) -> u32 {
match self {
FontWeight::Thin=> 100,
FontWeight::ExtraLight=> 200,
FontWeight::Light=> 300,
FontWeight::SemiLight=> 350,
FontWeight::Regular=> 400,
FontWeight::Medium=> 500,
FontWeight::SemiBold=> 600,
FontWeight::Bold=> 700,
FontWeight::ExtraBold=> 800,
FontWeight::Black=> 900,
FontWeight::ExtraBlack=> 950,
FontWeight::Unknown(v) => *v as u32
}
}
pub fn from_u32(v: u32) -> FontWeight {
match v {
100 => FontWeight::Thin,
200 => FontWeight::ExtraLight,
300 => FontWeight::Light,
350 => FontWeight::SemiLight,
400 => FontWeight::Regular,
500 => FontWeight::Medium,
600 => FontWeight::SemiBold,
700 => FontWeight::Bold,
800 => FontWeight::ExtraBold,
900 => FontWeight::Black,
950 => FontWeight::ExtraBlack,
_ => FontWeight::Unknown(v)
}
}
pub fn to_u32(&self) -> u32 { unsafe { mem::transmute::<FontWeight, u32>(*self) } }
pub fn from_u32(v: u32) -> FontWeight { unsafe { mem::transmute::<u32, FontWeight>(v) } }
}

// mirrors DWRITE_FONT_STRETCH
Expand Down