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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
use std::mem;
use std::slice;
use std::ptr;
mod ffi;
#[cfg(all(debug_assertions, windows))]
mod link_windowsd;
#[cfg(any(not(debug_assertions), not(windows)))]
mod link;
pub struct TextureFont{
font: *mut ffi::texture_font_t,
bytes: Option<Vec<u8>>,
}
impl Drop for TextureFont{
fn drop(&mut self){
unsafe{ ffi::texture_font_delete(self.font) }
}
}
unsafe fn char_to_utf8(c: char) -> Vec<u8>{
let c = c.to_string();
CString::from_vec_unchecked(c.as_bytes().to_vec()).as_bytes().to_vec()
}
impl TextureFont{
pub fn load(path: &str, pt_size: f32, depth: usize) -> Option<TextureFont>{
unsafe{
let tex_atlas = ffi::texture_atlas_new(512, 512, depth as u64);
if tex_atlas == ptr::null_mut() {
return None
}
let path = CString::new(path.as_bytes()).unwrap();
let c_path = path.as_ptr();
let tex_font = ffi::texture_font_new_from_file( tex_atlas, pt_size, c_path);
if tex_font == ptr::null_mut() {
return None
}
let latin1 = (32 as u8 .. 255).collect::<Vec<_>>();
let glyphs_i32 = CString::new(latin1).unwrap();
ffi::texture_font_load_glyphs(tex_font, glyphs_i32.into_raw());
Some(TextureFont{
font: tex_font,
bytes: None,
})
}
}
pub fn load_from_memory(font_data: Vec<u8>, pt_size: f32, depth: usize) -> Option<TextureFont>{
unsafe{
let tex_atlas = ffi::texture_atlas_new(512,512, depth as u64);
if tex_atlas == ptr::null_mut() {
return None
}
let tex_font = ffi::texture_font_new_from_memory(
tex_atlas,
pt_size,
font_data.as_ptr() as *const c_void,
font_data.len() as u64);
if tex_font == ptr::null_mut() {
return None
}
let latin1 = (32 as u8 .. 255).collect::<Vec<_>>();
let glyphs_i32 = CString::new(latin1).unwrap();
ffi::texture_font_load_glyphs(tex_font, glyphs_i32.into_raw());
Some(TextureFont{
font: tex_font,
bytes: Some(font_data),
})
}
}
#[inline]
pub fn glyph(&self, c: char) -> Option<TextureGlyph>{
unsafe{
let glyph = ffi::texture_font_get_glyph(self.font, char_to_utf8(c).as_ptr() as *const c_char);
if glyph != ptr::null_mut() {
Some(TextureGlyph{
glyph
})
}else{
None
}
}
}
#[inline]
pub fn glyph_by_freetype_id(&self, glyph_id: u32) -> Option<TextureGlyph>{
unsafe{
let glyph = ffi::texture_font_get_glyph_by_id(self.font, glyph_id);
if glyph != ptr::null_mut() {
Some(TextureGlyph{
glyph
})
}else{
None
}
}
}
#[inline]
pub fn size(&self) -> f32{
unsafe{ (*self.font).size }
}
#[inline]
pub fn hinting(&self) -> i32{
unsafe{ (*self.font).hinting }
}
#[inline]
pub fn rendermode(&self) -> RenderMode{
unsafe{ mem::transmute((*self.font).rendermode) }
}
#[inline]
pub fn outline_thickness(&self) -> f32{
unsafe{ (*self.font).outline_thickness }
}
#[inline]
pub fn filtering(&self) -> i32{
unsafe{ (*self.font).filtering }
}
#[inline]
pub fn lcd_weights(&self) -> [ :: std :: os :: raw :: c_uchar ; 5usize ]{
unsafe{ (*self.font).lcd_weights }
}
#[inline]
pub fn kerning(&self) -> i32{
unsafe{ (*self.font).kerning }
}
#[inline]
pub fn height(&self) -> f32{
unsafe{ (*self.font).height }
}
#[inline]
pub fn linegap(&self) -> f32{
unsafe{ (*self.font).linegap }
}
#[inline]
pub fn ascender(&self) -> f32{
unsafe{ (*self.font).ascender }
}
#[inline]
pub fn descender(&self) -> f32{
unsafe{ (*self.font).descender }
}
#[inline]
pub fn underline_position(&self) -> f32{
unsafe{ (*self.font).underline_position }
}
#[inline]
pub fn underline_thickness(&self) -> f32{
unsafe{ (*self.font).underline_thickness }
}
#[inline]
pub fn atlas(&self) -> TextureAtlas{
TextureAtlas{ atlas: unsafe{ (*self.font).atlas } }
}
pub unsafe fn face(&self) -> ffi::FT_Face{
(*self.font).face
}
}
#[repr(u32)]
pub enum RenderMode{
Normal = 0,
OutlineEdge = 1,
OutlinePositive = 2,
OutlineNegatice = 3,
SignedDistanceField = 4,
}
pub struct TextureGlyph{
glyph: *mut ffi::texture_glyph_t
}
impl std::fmt::Debug for TextureGlyph{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("TextureGlyph")
.field("codepoint", &self.codepoint())
.field("glyph_id", &self.glyph_id())
.field("offset_x", &self.offset_x())
.field("offset_y", &self.offset_y())
.field("width", &self.width())
.field("height", &self.height())
.field("s0", &self.s0())
.field("t0", &self.t0())
.field("s1", &self.s1())
.field("t1", &self.t1())
.finish()
}
}
impl TextureGlyph{
#[inline]
pub fn kerning(&self, c: char) -> f32{
unsafe{
ffi::texture_glyph_get_kerning(self.glyph, char_to_utf8(c).as_ptr() as *const c_char)
}
}
#[inline]
pub fn codepoint(&self) -> u32 {
unsafe{ (*self.glyph).codepoint }
}
#[inline]
pub fn glyph_id(&self) -> u32 {
unsafe{ (*self.glyph).glyph_id }
}
#[inline]
pub fn width(&self) -> usize{
unsafe{ (*self.glyph).width as usize}
}
#[inline]
pub fn height(&self) -> usize{
unsafe{ (*self.glyph).height as usize }
}
#[inline]
pub fn offset_x(&self) -> i32{
unsafe{ (*self.glyph).offset_x }
}
#[inline]
pub fn offset_y(&self) -> i32{
unsafe{ (*self.glyph).offset_y }
}
#[inline]
pub fn advance_x(&self) -> f32{
unsafe{ (*self.glyph).advance_x }
}
#[inline]
pub fn advance_y(&self) -> f32 {
unsafe{ (*self.glyph).advance_y }
}
#[inline]
pub fn s0(&self) -> f32 {
unsafe{ (*self.glyph).s0 }
}
#[inline]
pub fn t0(&self) -> f32 {
unsafe{ (*self.glyph).t0 }
}
#[inline]
pub fn s1(&self) -> f32 {
unsafe{ (*self.glyph).s1 }
}
#[inline]
pub fn t1(&self) -> f32 {
unsafe{ (*self.glyph).t1 }
}
#[inline]
pub fn rendermode(&self) -> RenderMode {
unsafe{ mem::transmute((*self.glyph).rendermode) }
}
#[inline]
pub fn outline_thickness(&self) -> f32 {
unsafe{ (*self.glyph).outline_thickness }
}
}
pub struct TextureAtlas{
atlas: *mut ffi::texture_atlas_t
}
impl TextureAtlas{
#[inline]
pub fn width(&self) -> usize{
unsafe{ (*self.atlas).width as usize }
}
#[inline]
pub fn height(&self) -> usize{
unsafe{ (*self.atlas).height as usize }
}
#[inline]
pub fn depth(&self) -> usize{
unsafe{ (*self.atlas).depth as usize }
}
#[inline]
pub fn used(&self) -> usize{
unsafe{ (*self.atlas).used as usize }
}
#[inline]
pub fn id(&self) -> u32{
unsafe{ (*self.atlas).id }
}
#[inline]
pub fn data(&self) -> &[u8]{
unsafe{ slice::from_raw_parts((*self.atlas).data, self.width() * self.height() * self.depth()) }
}
}