/
font_list.rs
219 lines (192 loc) · 7.73 KB
/
font_list.rs
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate freetype;
extern crate fontconfig;
use fontconfig::fontconfig::{
FcChar8, FcResultMatch, FcSetSystem, FcPattern,
FcResultNoMatch, FcMatchPattern, FC_SLANT_ITALIC, FC_WEIGHT_BOLD, FC_SLANT_OBLIQUE
};
use fontconfig::fontconfig::{
FcConfigGetCurrent, FcConfigGetFonts, FcPatternGetString,
FcPatternDestroy, FcFontSetDestroy, FcConfigSubstitute,
FcDefaultSubstitute, FcPatternCreate, FcPatternAddString, FcPatternAddInteger,
FcFontMatch, FcFontSetList, FcObjectSetCreate, FcObjectSetDestroy,
FcObjectSetAdd, FcPatternGetInteger
};
use style::computed_values::font_style;
use font::{FontHandleMethods, UsedFontStyle};
use font_list::{FontEntry, FontFamily, FontFamilyMap};
use platform::font::FontHandle;
use platform::font_context::FontContextHandle;
use collections::HashMap;
use libc;
use libc::{c_int, c_char};
use std::ptr;
use std::str;
pub struct FontListHandle {
pub fctx: FontContextHandle,
}
impl FontListHandle {
pub fn new(fctx: &FontContextHandle) -> FontListHandle {
FontListHandle { fctx: fctx.clone() }
}
pub fn get_available_families(&self) -> FontFamilyMap {
let mut family_map : FontFamilyMap = HashMap::new();
unsafe {
let config = FcConfigGetCurrent();
let fontSet = FcConfigGetFonts(config, FcSetSystem);
for i in range(0, (*fontSet).nfont as int) {
let font = (*fontSet).fonts.offset(i);
let family: *FcChar8 = ptr::null();
let mut v: c_int = 0;
"family".to_c_str().with_ref(|FC_FAMILY| {
while FcPatternGetString(*font, FC_FAMILY, v, &family) == FcResultMatch {
let family_name = str::raw::from_c_str(family as *c_char);
debug!("Creating new FontFamily for family: {:s}", family_name);
let new_family = FontFamily::new(family_name);
family_map.insert(family_name, new_family);
v += 1;
}
});
}
}
return family_map;
}
pub fn load_variations_for_family(&self, family: &mut FontFamily) {
debug!("getting variations for {:?}", family);
unsafe {
let config = FcConfigGetCurrent();
let font_set = FcConfigGetFonts(config, FcSetSystem);
let font_set_array_ptr = &font_set;
let pattern = FcPatternCreate();
assert!(pattern.is_not_null());
"family".to_c_str().with_ref(|FC_FAMILY| {
family.family_name.to_c_str().with_ref(|family_name| {
let ok = FcPatternAddString(pattern, FC_FAMILY, family_name as *FcChar8);
assert!(ok != 0);
});
});
let object_set = FcObjectSetCreate();
assert!(object_set.is_not_null());
"file".to_c_str().with_ref(|FC_FILE| {
FcObjectSetAdd(object_set, FC_FILE);
});
"index".to_c_str().with_ref(|FC_INDEX| {
FcObjectSetAdd(object_set, FC_INDEX);
});
let matches = FcFontSetList(config, font_set_array_ptr, 1, pattern, object_set);
debug!("found {} variations", (*matches).nfont);
for i in range(0, (*matches).nfont as int) {
let font = (*matches).fonts.offset(i);
let file = "file".to_c_str().with_ref(|FC_FILE| {
let file: *FcChar8 = ptr::null();
if FcPatternGetString(*font, FC_FILE, 0, &file) == FcResultMatch {
str::raw::from_c_str(file as *libc::c_char)
} else {
fail!();
}
});
let index = "index".to_c_str().with_ref(|FC_INDEX| {
let index: libc::c_int = 0;
if FcPatternGetInteger(*font, FC_INDEX, 0, &index) == FcResultMatch {
index
} else {
fail!();
}
});
debug!("variation file: {}", file);
debug!("variation index: {}", index);
let font_handle = FontHandle::new_from_file_unstyled(&self.fctx,
file);
let font_handle = font_handle.unwrap();
debug!("Creating new FontEntry for face: {:s}", font_handle.face_name());
let entry = FontEntry::new(font_handle);
family.entries.push(entry);
}
FcFontSetDestroy(matches);
FcPatternDestroy(pattern);
FcObjectSetDestroy(object_set);
}
}
pub fn get_last_resort_font_families() -> Vec<~str> {
vec!("Roboto".to_owned())
}
}
struct AutoPattern {
pattern: *FcPattern
}
impl Drop for AutoPattern {
fn drop(&mut self) {
unsafe {
FcPatternDestroy(self.pattern);
}
}
}
pub fn path_from_identifier(name: ~str, style: &UsedFontStyle) -> Result<~str, ()> {
unsafe {
let config = FcConfigGetCurrent();
let wrapper = AutoPattern { pattern: FcPatternCreate() };
let pattern = wrapper.pattern;
let res = "family".to_c_str().with_ref(|FC_FAMILY| {
name.to_c_str().with_ref(|family| {
FcPatternAddString(pattern, FC_FAMILY, family as *FcChar8)
})
});
if res != 1 {
debug!("adding family to pattern failed");
return Err(());
}
match style.style {
font_style::normal => (),
font_style::italic => {
let res = "slant".to_c_str().with_ref(|FC_SLANT| {
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC)
});
if res != 1 {
debug!("adding slant to pattern failed");
return Err(());
}
},
font_style::oblique => {
let res = "slant".to_c_str().with_ref(|FC_SLANT| {
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_OBLIQUE)
});
if res != 1 {
debug!("adding slant(oblique) to pattern failed");
return Err(());
}
}
}
if style.weight.is_bold() {
let res = "weight".to_c_str().with_ref(|FC_WEIGHT| {
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD)
});
if res != 1 {
debug!("adding weight to pattern failed");
return Err(());
}
}
if FcConfigSubstitute(config, pattern, FcMatchPattern) != 1 {
debug!("substitution failed");
return Err(());
}
FcDefaultSubstitute(pattern);
let result = FcResultNoMatch;
let result_wrapper = AutoPattern { pattern: FcFontMatch(config, pattern, &result) };
let result_pattern = result_wrapper.pattern;
if result != FcResultMatch && result_pattern.is_null() {
debug!("obtaining match to pattern failed");
return Err(());
}
let file: *FcChar8 = ptr::null();
let res = "file".to_c_str().with_ref(|FC_FILE| {
FcPatternGetString(result_pattern, FC_FILE, 0, &file)
});
if res != FcResultMatch {
debug!("getting filename for font failed");
return Err(());
}
Ok(str::raw::from_c_str(file as *c_char))
}
}