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
use std::any::TypeId;
use std::hash::Hash;
use winapi::{HFONT, DWORD, c_int};
use ui::Ui;
use controls::AnyHandle;
use resources::{ResourceT, Resource};
use error::{Error, SystemError};
use defs::{FONT_DECO_ITALIC, FONT_DECO_UNDERLINE, FONT_DECO_STRIKEOUT};
#[derive(Clone)]
pub struct FontT<S: Clone+Into<String>> {
pub family: S,
pub size: c_int,
pub weight: c_int,
pub decoration: u32,
}
impl<ID: Clone+Hash, S: Clone+Into<String>> ResourceT<ID> for FontT<S> {
fn type_id(&self) -> TypeId { TypeId::of::<Font>() }
#[allow(unused_variables)]
fn build(&self, ui: &Ui<ID>) -> Result<Box<Resource>, Error> {
use gdi32::CreateFontW;
use winapi::{DEFAULT_CHARSET, CLEARTYPE_QUALITY, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, VARIABLE_PITCH};
use low::other_helper::to_utf16;
let use_italic = ((self.decoration & FONT_DECO_ITALIC) != 0) as DWORD;
let use_underline = ((self.decoration & FONT_DECO_UNDERLINE) != 0) as DWORD;
let use_strikeout = ((self.decoration & FONT_DECO_STRIKEOUT) != 0) as DWORD;
let family_name = to_utf16(self.family.clone().into().as_ref());
let handle = unsafe{ CreateFontW(
self.size as c_int,
0, 0, 0,
self.weight,
use_italic,
use_underline,
use_strikeout,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
VARIABLE_PITCH,
family_name.as_ptr(),
) };
if handle.is_null() {
Err(Error::System(SystemError::FontCreation))
} else {
Ok( Box::new( Font{ handle: handle } ) )
}
}
}
pub struct Font {
handle: HFONT
}
impl Resource for Font {
fn handle(&self) -> AnyHandle { AnyHandle::HFONT(self.handle) }
fn free(&mut self) {
use gdi32::DeleteObject;
unsafe{ DeleteObject(::std::mem::transmute(self.handle)); }
}
}