1 #![allow(unknown_lints)]
2
3 extern crate stb_truetype;
4
5 use stb_truetype::FontInfo;
6 use std::borrow::Cow;
7
8 #[allow(cast_ptr_alignment)] // FIXME seems a bit dodgy
main()9 fn main() {
10 let file = &include_bytes!("../fonts/Gudea-Regular.ttf")[..];
11 let font = FontInfo::new(Cow::Borrowed(file), 0).unwrap();
12
13 for info in font.get_font_name_strings() {
14 let (name, pl_en_la, na) = info;
15 let name = (match pl_en_la {
16 Some(stb_truetype::PlatformEncodingLanguageId::Mac(
17 Some(Ok(stb_truetype::MacEid::Roman)),
18 _,
19 )) => ::std::str::from_utf8(name).ok().map(Cow::Borrowed),
20 Some(stb_truetype::PlatformEncodingLanguageId::Microsoft(
21 Some(Ok(stb_truetype::MicrosoftEid::UnicodeBMP)),
22 _,
23 )) => {
24 let name16be = unsafe {
25 ::std::slice::from_raw_parts(name.as_ptr() as *const u16, name.len() / 2)
26 };
27 let name16 = name16be
28 .iter()
29 .map(|&v| u16::from_be(v))
30 .collect::<Vec<_>>();
31 String::from_utf16(&name16).ok().map(Cow::Owned)
32 }
33 Some(stb_truetype::PlatformEncodingLanguageId::Microsoft(
34 Some(Ok(stb_truetype::MicrosoftEid::UnicodeFull)),
35 _,
36 )) => {
37 let name16be = unsafe {
38 ::std::slice::from_raw_parts(name.as_ptr() as *const u16, name.len() / 2)
39 };
40 let name16 = name16be
41 .iter()
42 .map(|&v| u16::from_be(v))
43 .collect::<Vec<_>>();
44 String::from_utf16(&name16).ok().map(Cow::Owned)
45 }
46 Some(_) => Some(Cow::Borrowed("(Unknown encoding)")),
47 None => Some(Cow::Borrowed("(Unknown Platform ID)")),
48 }).unwrap_or(Cow::Borrowed("(Encoding error)"));
49 println!("{:?}, {:?}, {:?}", name, pl_en_la, na);
50 }
51 }
52