unbanished_formats/ui/
fonts.rs1use super::*;
4
5#[derive(Debug, Clone)]
9pub struct Font {
10 pub name: String,
12 pub size: i32,
14 pub sheet_width: i32,
16 pub sheet_height: i32,
18 pub character_set: Ref,
20 pub sprite_sheet: Ref,
22 pub material: Ref,
24 pub char_map: Vec<i32>,
26 pub glyphs: Vec<[i32; 4]>,
29 pub unknown: [i32; 3],
32}
33
34pub fn font(data: &[u8]) -> Result<Font> {
36 expect(data, "Font", |r| {
37 r.block(|r, version| {
38 if version < 2 {
39 return Err(Error::Unsupported("Font block version < 2".into()));
40 }
41 let name = r.string()?;
42 let size = r.i32()?;
43 let _sheet_path = r.string()?;
44 let sheet_width = r.i32()?;
45 let sheet_height = r.i32()?;
46 let _material_path = r.string()?;
47 let _image_path = r.string()?;
48 let character_set = r.external_ref()?;
49 let sprite_sheet = r.external_ref()?;
50 let material = r.external_ref()?;
51 let char_map = (0..r.count()?).map(|_| r.i32()).collect::<Result<_>>()?;
52 let glyphs = (0..r.count()?).map(|_| i32s(r)).collect::<Result<_>>()?;
53 Ok(Font {
54 name,
55 size,
56 sheet_width,
57 sheet_height,
58 character_set,
59 sprite_sheet,
60 material,
61 char_map,
62 glyphs,
63 unknown: i32s(r)?,
64 })
65 })
66 })
67}
68
69pub fn string_table(data: &[u8]) -> Result<Vec<(String, String)>> {
73 expect(data, "StringTable", |r| {
74 r.block(|r, _| {
75 (0..r.count()?)
76 .map(|_| Ok((r.string()?, r.string()?)))
77 .collect()
78 })
79 })
80}
81
82impl Font {
83 pub fn baseline_offset(&self) -> i32 {
87 self.size - self.unknown[0]
88 }
89
90 pub fn glyph_index(&self, ch: char) -> Option<usize> {
92 let g = *self.char_map.get(ch as usize)?;
93 (g >= 0 && g != 0xFFFF && (g as usize) < self.glyphs.len()).then_some(g as usize)
94 }
95}