diff --git a/src/font.rs b/src/font.rs index 6e055e2..360717c 100644 --- a/src/font.rs +++ b/src/font.rs @@ -50,7 +50,7 @@ impl Font { pub fn weight(&self) -> FontWeight { unsafe { - mem::transmute::((*self.native.get()).GetWeight()) + FontWeight::from_u32((*self.native.get()).GetWeight().0) } } diff --git a/src/types.rs b/src/types.rs index 839f880..fca0b32 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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::(*self) } + fn t(&self) -> winapi::DWRITE_FONT_WEIGHT { + unsafe { mem::transmute::(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::(*self) } } - pub fn from_u32(v: u32) -> FontWeight { unsafe { mem::transmute::(v) } } } // mirrors DWRITE_FONT_STRETCH