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
            }

            // println!("allocated tex atlas  {},{}x{}", (*tex_atlas).width, (*tex_atlas).height, (*tex_atlas).depth);
            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());
            // println!("loaded {} glyphs", ffi::vector_size((*tex_font).glyphs));

            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
            }

            // println!("allocated tex atlas  {},{}x{}", (*tex_atlas).width, (*tex_atlas).height, (*tex_atlas).depth);
            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());

            // println!("loaded {} glyphs", ffi::vector_size((*tex_font).glyphs));

            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
            }
        }
	}

    /// Font size
    #[inline]
    pub fn size(&self) -> f32{
		unsafe{ (*self.font).size }
	}

    /// Whether to use autohint when rendering font
    #[inline]
    pub fn hinting(&self) -> i32{
		unsafe{ (*self.font).hinting }
	}

    /// Mode the font is rendering its next glyph
    #[inline]
    pub fn rendermode(&self) -> RenderMode{
		unsafe{ mem::transmute((*self.font).rendermode) }
	}

    /// Outline thickness
    #[inline]
    pub fn outline_thickness(&self) -> f32{
		unsafe{ (*self.font).outline_thickness }
	}

    /// Whether to use our own lcd filter.
    #[inline]
    pub fn filtering(&self) -> i32{
		unsafe{ (*self.font).filtering }
	}

    /// LCD filter weights
    #[inline]
    pub fn lcd_weights(&self) -> [ :: std :: os :: raw :: c_uchar ; 5usize ]{
		unsafe{ (*self.font).lcd_weights }
	}

    /// Whether to use kerning if available
    #[inline]
    pub fn kerning(&self) -> i32{
		unsafe{ (*self.font).kerning }
	}

    /// This field is simply used to compute a default line spacing (i.e., the
    /// baseline-to-baseline distance) when writing text with this font. Note
    /// that it usually is larger than the sum of the ascender and descender
    /// taken as absolute values. There is also no guarantee that no glyphs
    /// extend above or below subsequent baselines when using this distance.
    #[inline]
    pub fn height(&self) -> f32{
		unsafe{ (*self.font).height }
	}

    /// This field is the distance that must be placed between two lines of
    /// text. The baseline-to-baseline distance should be computed as:
    /// ascender - descender + linegap
    #[inline]
    pub fn linegap(&self) -> f32{
		unsafe{ (*self.font).linegap }
	}

    /// The ascender is the vertical distance from the horizontal baseline to
    /// the highest 'character' coordinate in a font face. Unfortunately, font
    /// formats define the ascender differently. For some, it represents the
    /// ascent of all capital latin characters (without accents), for others it
    /// is the ascent of the highest accented character, and finally, other
    /// formats define it as being equal to bbox.yMax.
    #[inline]
    pub fn ascender(&self) -> f32{
		unsafe{ (*self.font).ascender }
	}

    /// The descender is the vertical distance from the horizontal baseline to
    /// the lowest 'character' coordinate in a font face. Unfortunately, font
    /// formats define the descender differently. For some, it represents the
    /// descent of all capital latin characters (without accents), for others it
    /// is the ascent of the lowest accented character, and finally, other
    /// formats define it as being equal to bbox.yMin. This field is negative
    /// for values below the baseline.
    #[inline]
    pub fn descender(&self) -> f32{
		unsafe{ (*self.font).descender }
	}

    /// The position of the underline line for this face. It is the center of
    /// the underlining stem. Only relevant for scalable formats.
    #[inline]
    pub fn underline_position(&self) -> f32{
		unsafe{ (*self.font).underline_position }
	}

    /// The thickness of the underline for this face. Only relevant for scalable
    /// formats.
    #[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)
        }
	}

    /// Unicode codepoint this glyph represents in UTF-32 LE encoding.
    #[inline]
    pub fn codepoint(&self) -> u32 {
        unsafe{ (*self.glyph).codepoint }
    }

    /// Unicode codepoint this glyph represents in UTF-32 LE encoding.
    #[inline]
    pub fn glyph_id(&self) -> u32 {
        unsafe{ (*self.glyph).glyph_id }
    }

    /// Glyph's width in pixels.
    #[inline]
    pub fn width(&self) ->  usize{
        unsafe{ (*self.glyph).width as usize}
    }

    /// Glyph's height in pixels.
    #[inline]
    pub fn height(&self) ->  usize{
        unsafe{ (*self.glyph).height as usize }
    }

    /// Glyph's left bearing expressed in integer pixels.
    #[inline]
    pub fn offset_x(&self) ->  i32{
        unsafe{ (*self.glyph).offset_x }
    }

    /// Glyphs's top bearing expressed in integer pixels.
    ///
    /// Remember that this is the distance from the baseline to the top-most
    /// glyph scanline, upwards y coordinates being positive.
    #[inline]
    pub fn offset_y(&self) ->  i32{
        unsafe{ (*self.glyph).offset_y }
    }

    /// For horizontal text layouts, this is the horizontal distance (in
    /// fractional pixels) used to increment the pen position when the glyph is
    /// drawn as part of a string of text.
    #[inline]
    pub fn advance_x(&self) ->  f32{
        unsafe{ (*self.glyph).advance_x }
    }

    /// For vertical text layouts, this is the vertical distance (in fractional
   /// pixels) used to increment therendermode_t pen position when the glyph is drawn as
   /// part of a string of text.
   #[inline]
    pub fn advance_y(&self) ->  f32 {
        unsafe{ (*self.glyph).advance_y }
    }

    /// First normalized texture coordinate (x) of top-left corner
    #[inline]
    pub fn s0(&self) ->  f32 {
        unsafe{ (*self.glyph).s0 }
    }

    /// Second normalized texture coordinate (y) of top-left corner
    #[inline]
    pub fn t0(&self) ->  f32 {
        unsafe{ (*self.glyph).t0 }
    }

    /// First normalized texture coordinate (x) of bottom-right corner
    #[inline]
    pub fn s1(&self) ->  f32 {
        unsafe{ (*self.glyph).s1 }
    }

    /// Second normalized texture coordinate (y) of bottom-right corner
    #[inline]
    pub fn t1(&self) ->  f32 {
        unsafe{ (*self.glyph).t1 }
    }

    /// Mode this glyph was rendered
    #[inline]
    pub fn rendermode(&self) ->  RenderMode {
        unsafe{ mem::transmute((*self.glyph).rendermode) }
    }

    /// Glyph outline thickness
    #[inline]
    pub fn outline_thickness(&self) -> f32 {
        unsafe{ (*self.glyph).outline_thickness }
    }
}


pub struct TextureAtlas{
    atlas: *mut ffi::texture_atlas_t
}

impl TextureAtlas{
    /// Width (in pixels) of the underlying texture
    #[inline]
	pub fn width(&self) -> usize{
		unsafe{ (*self.atlas).width as usize }
	}

    /// Height (in pixels) of the underlying texture
    #[inline]
	pub fn height(&self) -> usize{
		unsafe{ (*self.atlas).height as usize }
	}

    /// Depth (in bytes) of the underlying texture
    #[inline]
	pub fn depth(&self) -> usize{
		unsafe{ (*self.atlas).depth as usize }
	}

    /// Allocated surface size
    #[inline]
	pub fn used(&self) -> usize{
		unsafe{ (*self.atlas).used as usize }
	}

    /// Texture identity (OpenGL)
    #[inline]
	pub fn id(&self) -> u32{
		unsafe{ (*self.atlas).id }
	}

    /// Atlas data
    #[inline]
	pub fn data(&self) -> &[u8]{
		unsafe{ slice::from_raw_parts((*self.atlas).data, self.width() * self.height() * self.depth()) }
	}

}