aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/game_font.rs
blob: 6af2cc209dd8d34f77eb93f9c5a57fcf20be67e0 (plain) (blame)
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
//! Original game text font used by the mission pager.

use crate::ui_configuration::{field, number, object, read};
use fparkan_font::decode_tfnt;
pub use fparkan_font::BitmapFont;
use fparkan_nres::{decode, EntryId, ReadProfile};
use fparkan_vfs::Vfs;
use std::sync::Arc;

/// Load the `GAME_FONT` for the 800×600 pager profile witnessed in the original.
pub fn load_game_font(vfs: &dyn Vfs) -> Result<BitmapFont, String> {
    load_profile(vfs, 2, [800, 600], &["GAME_FONT"]).map(|[font]| font)
}

/// Load the font for composing controls in the original 640×480 layout space.
pub fn load_layout_font(vfs: &dyn Vfs) -> Result<BitmapFont, String> {
    load_profile(vfs, 1, [640, 480], &["GAME_FONT"]).map(|[font]| font)
}

/// Load the menu caption font for the original 800×600 font profile.
pub fn load_menu_font(vfs: &dyn Vfs) -> Result<BitmapFont, String> {
    load_profile(vfs, 2, [800, 600], &["MENU_FONT"]).map(|[font]| font)
}

/// The shipped sizes of the font used for tooltips.
pub const TOOL_FONT_EXTENTS: [[u32; 2]; 3] = [[640, 480], [800, 600], [1024, 768]];

/// Loads the original `TOOL_FONT` for one of the three screen profiles.
pub fn load_tool_font(vfs: &dyn Vfs, profile: usize) -> Result<BitmapFont, String> {
    let extent = *TOOL_FONT_EXTENTS
        .get(profile)
        .ok_or("unknown tool font profile")?;
    load_profile(vfs, (profile + 1) as u8, extent, &["TOOL_FONT"]).map(|[font]| font)
}

pub(crate) fn load_profile<const N: usize>(
    vfs: &dyn Vfs,
    profile: u8,
    dimensions: [u32; 2],
    names: &[&str; N],
) -> Result<[BitmapFont; N], String> {
    let bytes = read(vfs, "ui/game_resources.cfg")?;
    let cfg = String::from_utf8_lossy(&bytes);
    let aliases =
        object(&cfg, &format!("font_substitutes{profile}")).ok_or("missing game font profile")?;
    if [
        number(&aliases, "DIMENSION_X")?,
        number(&aliases, "DIMENSION_Y")?,
    ] != dimensions
    {
        return Err("unexpected dimensions for the game font profile".into());
    }
    let fonts = object(&cfg, "fonts").ok_or("missing font catalog")?;
    let archive = decode(read(vfs, field(&fonts, "LIBRARY")?)?, ReadProfile::Strict)
        .map_err(|error| error.to_string())?;
    let load = |name: &&str| {
        let key = field(&aliases, name)?.to_ascii_uppercase();
        let id = EntryId(number(&fonts, &key)?);
        let payload = archive.payload(id).map_err(|error| error.to_string())?;
        let font = decode_tfnt(Arc::from(payload)).map_err(|error| error.to_string())?;
        BitmapFont::prepare(&font)
    };
    let fonts = names.iter().map(load).collect::<Result<Vec<_>, _>>()?;
    fonts.try_into().map_err(|_| "invalid font count".into())
}

#[cfg(test)]
mod tests {
    use super::load_game_font;
    use fparkan_vfs::DirectoryVfs;

    #[test]
    #[ignore = "requires licensed corpus"]
    fn draws_russian_and_latin_glyphs_from_the_original_game_font() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
        let font = load_game_font(&DirectoryVfs::new(root)).unwrap();
        let (height, latin) = font.caption("A", 96, 80).unwrap();
        let (russian_height, russian) = font.caption("П", 96, 80).unwrap();
        assert_eq!(height, 34);
        assert_eq!(russian_height, height);
        assert_ne!(latin, russian);
        for image in [&latin, &russian] {
            assert!(image.chunks_exact(4).any(|pixel| pixel[0] == 255));
            assert!(image.chunks_exact(4).any(|pixel| pixel[0] == 64));
        }
        let (multiline, _) = font.caption("Курсант\nA", 96, 80).unwrap();
        assert_eq!(multiline, 45);
    }
}