Skip to main content

unbanished_formats/ui/
fonts.rs

1//! Fonts and string tables: a font's glyphs and metrics, and a table's keyed strings.
2
3use super::*;
4
5/// `UI::Font`, block version 2.
6///
7/// Original: Runtime `0x180119f60`.
8#[derive(Debug, Clone)]
9pub struct Font {
10    /// The font's name.
11    pub name: String,
12    /// The font's size, in pixels.
13    pub size: i32,
14    /// The glyph sheet's width.
15    pub sheet_width: i32,
16    /// The glyph sheet's height.
17    pub sheet_height: i32,
18    /// The character set resource.
19    pub character_set: Ref,
20    /// Sprite sheet holding one sprite per glyph, in glyph order.
21    pub sprite_sheet: Ref,
22    /// The material the glyphs are drawn with.
23    pub material: Ref,
24    /// Code point -> glyph index; negative or 0xffff means no glyph.
25    pub char_map: Vec<i32>,
26    /// Per glyph: (a, c, b, d): advance = a + b, with the previous glyph's c added;
27    /// d raises the bottom of the sprite above the baseline.
28    pub glyphs: Vec<[i32; 4]>,
29    // +0x54
30    /// Serialized descent, ellipsis advance, and space advance (Runtime Font/58/5c).
31    pub unknown: [i32; 3],
32}
33
34/// Read a [`Font`] from the bytes of its resource.
35pub 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
69/// Key -> display string.
70///
71/// Original: `UI::StringTable`, Runtime `0x1801201d0`.
72pub 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    /// Baseline relative to a text element's top, as used by TextElement::Draw.
84    ///
85    /// Original: Runtime `0x180121b10`.
86    pub fn baseline_offset(&self) -> i32 {
87        self.size - self.unknown[0]
88    }
89
90    /// The glyph of a character.
91    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}