From b48fb2caccc7a72b85f07e6ab652081af57c7855 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 22:32:55 +0400 Subject: feat(ui): render mission hints with the original game font Resolve the witnessed 800x600 GAME_FONT through the installed resource configuration. Draw CP1251 bytes from the Tfnt atlas using the original variable-width metrics. Checked the original measurement routine and the first Constructor hint in the game window. --- crates/fparkan-assets/src/game_font.rs | 54 +++++++++++++++++++++++++++ crates/fparkan-assets/src/lib.rs | 2 + crates/fparkan-assets/src/satellite_map.rs | 53 +------------------------- crates/fparkan-assets/src/ui_configuration.rs | 54 +++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 52 deletions(-) create mode 100644 crates/fparkan-assets/src/game_font.rs create mode 100644 crates/fparkan-assets/src/ui_configuration.rs (limited to 'crates/fparkan-assets/src') diff --git a/crates/fparkan-assets/src/game_font.rs b/crates/fparkan-assets/src/game_font.rs new file mode 100644 index 00000000..556aa79d --- /dev/null +++ b/crates/fparkan-assets/src/game_font.rs @@ -0,0 +1,54 @@ +//! 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 { + let bytes = read(vfs, "ui/game_resources.cfg")?; + let cfg = String::from_utf8_lossy(&bytes); + let aliases = object(&cfg, "font_substitutes2").ok_or("missing 800×600 font profile")?; + if [ + number(&aliases, "DIMENSION_X")?, + number(&aliases, "DIMENSION_Y")?, + ] != [800, 600] + { + return Err("unexpected dimensions for the 800×600 font profile".into()); + } + let key = field(&aliases, "GAME_FONT")?.to_ascii_uppercase(); + let fonts = object(&cfg, "fonts").ok_or("missing font catalog")?; + let id = EntryId(number(&fonts, &key)?); + let archive = decode(read(vfs, field(&fonts, "LIBRARY")?)?, ReadProfile::Strict) + .map_err(|error| error.to_string())?; + 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) +} + +#[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); + } +} diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index db7484cf..75851628 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -35,8 +35,10 @@ mod chassis_profile; mod control_drive; mod control_weapon_sound; mod explosion_profile; +pub mod game_font; pub mod satellite_map; mod turret_elevation; +mod ui_configuration; pub use behavior_profile_values::{ decode_behavior_go_speed_percent, decode_behavior_work_rates, BehaviorWorkRates, }; diff --git a/crates/fparkan-assets/src/satellite_map.rs b/crates/fparkan-assets/src/satellite_map.rs index 49f8894e..9a18f2bd 100644 --- a/crates/fparkan-assets/src/satellite_map.rs +++ b/crates/fparkan-assets/src/satellite_map.rs @@ -1,11 +1,11 @@ //! Resources used by the satellite map overlay. +use crate::ui_configuration::{field, number, object, read}; use fparkan_nres::{decode, EntryId, ReadProfile}; use fparkan_path::{normalize_relative, PathPolicy}; use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository}; use fparkan_texm::{decode_mip_rgba8, decode_texm, RgbaImage}; use fparkan_vfs::Vfs; -use std::collections::BTreeMap; use std::sync::Arc; /// The mission map and compass cut from the original interface texture atlas. @@ -69,57 +69,6 @@ pub fn load_satellite_map( })) } -fn read(vfs: &dyn Vfs, path: &str) -> Result, String> { - let path = normalize_relative( - path.replace("\\\\", "\\").as_bytes(), - PathPolicy::StrictLegacy, - ) - .map_err(|error| error.to_string())?; - vfs.read(&path).map_err(|error| error.to_string()) -} - -fn object<'a>(text: &'a str, name: &str) -> Option> { - let mut lines = text - .lines() - .map(|line| line.split('#').next().unwrap_or_default().trim()); - for line in lines.by_ref() { - let mut words = line.split_whitespace(); - if words - .next() - .is_some_and(|word| word.eq_ignore_ascii_case("object")) - && words - .next() - .is_some_and(|word| word.eq_ignore_ascii_case(name)) - { - return Some( - lines - .take_while(|line| !line.eq_ignore_ascii_case("end")) - .filter_map(|line| line.split_once('=')) - .map(|(key, value)| (key.trim().to_ascii_uppercase(), value.trim())) - .collect(), - ); - } - } - None -} - -fn field<'a>(fields: &BTreeMap, key: &str) -> Result<&'a str, String> { - let raw = fields.get(key).ok_or_else(|| format!("missing {key}"))?; - if let Some(quoted) = raw.strip_prefix('"') { - return quoted - .split_once('"') - .map(|(value, _)| value) - .ok_or_else(|| format!("invalid quoted value for {key}")); - } - Ok(raw) -} - -fn number(fields: &BTreeMap, key: &str) -> Result { - field(fields, key)? - .parse() - .map_err(|_| format!("invalid number for {key}")) -} - fn texture(bytes: &[u8]) -> Result { let document = decode_texm(Arc::from(bytes)).map_err(|error| error.to_string())?; decode_mip_rgba8(&document, 0).map_err(|error| error.to_string()) diff --git a/crates/fparkan-assets/src/ui_configuration.rs b/crates/fparkan-assets/src/ui_configuration.rs new file mode 100644 index 00000000..e678d760 --- /dev/null +++ b/crates/fparkan-assets/src/ui_configuration.rs @@ -0,0 +1,54 @@ +use fparkan_path::{normalize_relative, PathPolicy}; +use fparkan_vfs::Vfs; +use std::{collections::BTreeMap, sync::Arc}; + +pub(super) fn read(vfs: &dyn Vfs, path: &str) -> Result, String> { + let path = normalize_relative( + path.replace("\\\\", "\\").as_bytes(), + PathPolicy::StrictLegacy, + ) + .map_err(|error| error.to_string())?; + vfs.read(&path).map_err(|error| error.to_string()) +} + +pub(super) fn object<'a>(text: &'a str, name: &str) -> Option> { + let mut lines = text + .lines() + .map(|line| line.split('#').next().unwrap_or_default().trim()); + for line in lines.by_ref() { + let mut words = line.split_whitespace(); + if words + .next() + .is_some_and(|word| word.eq_ignore_ascii_case("object")) + && words + .next() + .is_some_and(|word| word.eq_ignore_ascii_case(name)) + { + return Some( + lines + .take_while(|line| !line.eq_ignore_ascii_case("end")) + .filter_map(|line| line.split_once('=')) + .map(|(key, value)| (key.trim().to_ascii_uppercase(), value.trim())) + .collect(), + ); + } + } + None +} + +pub(super) fn field<'a>(fields: &BTreeMap, key: &str) -> Result<&'a str, String> { + let raw = fields.get(key).ok_or_else(|| format!("missing {key}"))?; + if let Some(quoted) = raw.strip_prefix('"') { + return quoted + .split_once('"') + .map(|(value, _)| value) + .ok_or_else(|| format!("invalid quoted value for {key}")); + } + Ok(raw) +} + +pub(super) fn number(fields: &BTreeMap, key: &str) -> Result { + field(fields, key)? + .parse() + .map_err(|_| format!("invalid number for {key}")) +} -- cgit v1.2.3