//! 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 { 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 { 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 { 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 { 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( 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::, _>>()?; 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); } }