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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::intrinsics::uninit;
use std::result::Result::{Ok};
use std::ptr;
use std::ptr::PtrExt;
use std::collections::HashSet;
use super::{ppb, ffi};
use super::{ToVar, Resource, ToFFIBool};
use super::ffi::{Struct_PP_FontMetrics_Dev, Struct_PP_TextRun_Dev,
Struct_PP_FontDescription_Dev, PP_FontFamily_Dev};
use super::StringVar;
use imagedata;
#[derive(Eq, PartialEq, Clone, Hash, Copy)]
pub enum Family {
DefaultFamily,
SerifFamily,
SansSerifFamily,
MonospaceFamily,
}
impl Family {
fn new_from_ffi(v: ffi::PP_FontFamily_Dev) -> Family {
match v {
ffi::PP_FONTFAMILY_DEFAULT => Family::DefaultFamily,
ffi::PP_FONTFAMILY_SERIF => Family::SerifFamily,
ffi::PP_FONTFAMILY_SANSSERIF => Family::SansSerifFamily,
ffi::PP_FONTFAMILY_MONOSPACE => Family::MonospaceFamily,
_ => unreachable!(),
}
}
fn to_ffi(&self) -> PP_FontFamily_Dev {
match self {
&Family::DefaultFamily => ffi::PP_FONTFAMILY_DEFAULT,
&Family::SerifFamily => ffi::PP_FONTFAMILY_SERIF,
&Family::SansSerifFamily => ffi::PP_FONTFAMILY_SANSSERIF,
&Family::MonospaceFamily => ffi::PP_FONTFAMILY_MONOSPACE,
}
}
}
#[derive(Eq, PartialEq, Clone, Hash, Copy)]
pub enum Weight {
ValueWeight(u16),
NormalWeight,
BoldWeight,
}
impl Weight {
fn new_from_ffi(v: ffi::PP_FontWeight_Dev) -> Weight {
match v {
ffi::PP_FONTWEIGHT_100 => Weight::ValueWeight(100),
ffi::PP_FONTWEIGHT_200 => Weight::ValueWeight(200),
ffi::PP_FONTWEIGHT_300 => Weight::ValueWeight(300),
ffi::PP_FONTWEIGHT_400 => Weight::NormalWeight,
ffi::PP_FONTWEIGHT_500 => Weight::ValueWeight(500),
ffi::PP_FONTWEIGHT_600 => Weight::ValueWeight(600),
ffi::PP_FONTWEIGHT_700 => Weight::BoldWeight,
ffi::PP_FONTWEIGHT_800 => Weight::ValueWeight(800),
ffi::PP_FONTWEIGHT_900 => Weight::ValueWeight(900),
_ => unreachable!(),
}
}
fn to_ffi(&self) -> ffi::PP_FontWeight_Dev {
match self {
&Weight::ValueWeight(v) if v <= 100 => ffi::PP_FONTWEIGHT_100,
&Weight::ValueWeight(v) if v > 100 && v <= 200 => ffi::PP_FONTWEIGHT_200,
&Weight::ValueWeight(v) if v > 200 && v <= 300 => ffi::PP_FONTWEIGHT_300,
&Weight::ValueWeight(v) if v > 300 && v <= 400 => ffi::PP_FONTWEIGHT_400,
&Weight::NormalWeight => ffi::PP_FONTWEIGHT_400,
&Weight::ValueWeight(v) if v > 400 && v <= 500 => ffi::PP_FONTWEIGHT_500,
&Weight::ValueWeight(v) if v > 500 && v <= 600 => ffi::PP_FONTWEIGHT_600,
&Weight::ValueWeight(v) if v > 600 && v <= 700 => ffi::PP_FONTWEIGHT_700,
&Weight::BoldWeight => ffi::PP_FONTWEIGHT_700,
&Weight::ValueWeight(v) if v > 700 && v <= 800 => ffi::PP_FONTWEIGHT_800,
&Weight::ValueWeight(_) => ffi::PP_FONTWEIGHT_900,
}
}
}
pub type Metrics = ffi::Struct_PP_FontMetrics_Dev;
impl Clone for super::ffi::Struct_PP_FontMetrics_Dev {
fn clone(&self) -> Metrics {
use core::mem::transmute_copy;
unsafe {
transmute_copy(self)
}
}
}
fn new_metrics_from_ffi(metrics: ffi::Struct_PP_FontMetrics_Dev) -> Metrics {
metrics
}
#[derive(Eq, PartialEq, Clone, Hash)]
pub struct Description {
face: Option<StringVar>,
family: Family,
size: u32,
weight: Weight,
italic: bool,
small_caps: bool,
letter_spacing: i32,
word_spacing: i32,
}
impl Description {
pub fn new_from_family(fam: Family) -> Description {
Description {
face: None,
family: fam,
size: 12,
weight: Weight::NormalWeight,
italic: false,
small_caps: false,
letter_spacing: 0,
word_spacing: 0,
}
}
fn new_from_ffi(v: Struct_PP_FontDescription_Dev) -> Description {
Description {
face: Some(StringVar::new_from_var(v.face)),
family: Family::new_from_ffi(v.family),
size: v.size,
weight: Weight::new_from_ffi(v.weight),
italic: v.italic != 0,
small_caps: v.small_caps != 0,
letter_spacing: v.letter_spacing,
word_spacing: v.word_spacing,
}
}
pub unsafe fn to_ffi(&self) -> Struct_PP_FontDescription_Dev {
let mut desc: Struct_PP_FontDescription_Dev = uninit();
desc.face = self.face.to_var();
desc.family = self.family.to_ffi();
desc.size = self.size;
desc.weight = self.weight.to_ffi();
desc.italic = self.italic.to_ffi_bool();
desc.small_caps = self.small_caps.to_ffi_bool();
desc.letter_spacing = self.letter_spacing;
desc
}
}
#[derive(Hash, Eq, PartialEq, Show)] pub struct Font(ffi::PP_Resource);
impl_resource_for!(Font, ResourceType::FontRes);
impl Font {
pub fn describe(&self) -> Option<(Description, Metrics)> {
let mut desc: Struct_PP_FontDescription_Dev = Struct_PP_FontDescription_Dev {
face: {super::NullVar}.to_var(),
.. unsafe { uninit() }
};
let mut metr: Struct_PP_FontMetrics_Dev = unsafe { uninit() };
match (ppb::get_font().Describe.unwrap())
(self.unwrap(),
&mut desc as *mut Struct_PP_FontDescription_Dev,
&mut metr as *mut Struct_PP_FontMetrics_Dev) {
0 => None,
_ => Some((Description::new_from_ffi(desc),
new_metrics_from_ffi(metr))),
}
}
pub fn measure_text<T: super::ToStringVar +
super::ToVar>(&self,
text: &T,
rtl: bool,
override_direction: bool) -> Option<i32> {
let text_run = Struct_PP_TextRun_Dev {
text: text.to_var(),
rtl: rtl.to_ffi_bool(),
override_direction: override_direction.to_ffi_bool(),
};
let result = (ppb::get_font().MeasureText.unwrap())
(self.unwrap(),
&text_run as *const Struct_PP_TextRun_Dev);
if result == -1 { None }
else { Some(result) }
}
pub fn draw_text<TStr: super::ToStringVar +
super::ToVar>(&self,
image: &imagedata::ImageData,
text: &TStr,
rtl: bool,
override_direction: bool,
pos: super::Point,
color: u32,
clip: Option<super::Rect>,
image_data_is_opaque: bool) -> super::Code {
let font_res = self.unwrap();
let image_res = image.unwrap();
let text_run = Struct_PP_TextRun_Dev {
text: text.to_var(),
rtl: rtl as u32,
override_direction: override_direction as u32,
};
let pos_ptr = &pos as *const super::Point;
let clip_ptr = if clip.is_some() { clip.as_ref().unwrap() as *const super::Rect}
else { ptr::null() };
if (ppb::get_font().DrawTextAt.unwrap())
(font_res,
image_res,
&text_run as *const Struct_PP_TextRun_Dev,
pos_ptr,
color,
clip_ptr,
image_data_is_opaque as ffi::PP_Bool) != -1 as u32 {
super::Code::Ok
} else { super::Code::Failed }
}
pub fn char_offset_for_pixel<TStr: super::ToStringVar +
super::ToVar>(&self,
text: &TStr,
rtl: bool,
override_direction: bool,
position: i32) -> Option<u32> {
let text_run = Struct_PP_TextRun_Dev {
text: text.to_var(),
rtl: rtl as u32,
override_direction: override_direction as u32,
};
let result = (ppb::get_font().CharacterOffsetForPixel.unwrap())
(self.unwrap(),
&text_run as *const Struct_PP_TextRun_Dev,
position);
if result == -1 as u32 {
None
} else {
Some(result)
}
}
pub fn pixel_offset_for_character<TStr: super::ToStringVar +
super::ToVar>(&self,
text: &TStr,
rtl: bool,
override_direction: bool,
char_offset: u32) -> Option<i32> {
let text_run = Struct_PP_TextRun_Dev {
text: text.to_var(),
rtl: rtl as u32,
override_direction: override_direction as u32,
};
let result = (ppb::get_font().PixelOffsetForCharacter.unwrap())
(self.unwrap(),
&text_run as *const Struct_PP_TextRun_Dev,
char_offset);
if result == -1 { None }
else { Some(result) }
}
}
pub trait FontFamilies {
fn get_font_families(&self) -> HashSet<String>;
}
impl FontFamilies for super::Instance {
fn get_font_families(&self) -> HashSet<String> {
let mut dest = HashSet::new();
let fam_str = (ppb::get_font().GetFontFamilies.unwrap())(self.instance);
let fam_str = StringVar::new_from_var(fam_str).to_string();
for font in fam_str.as_slice().split('\0') {
dest.insert(font.to_string());
}
dest
}
}