diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 23:14:47 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 23:14:47 +0300 |
| commit | c3e2add9a189879b5e800344e07645af591a2cbd (patch) | |
| tree | a2ac87f7cba43bc28f913a667524f01423a47c4a /crates/fparkan-font/src | |
| parent | a6cb9f56ab34bc435ccf7dad613a2c2ce8b417e6 (diff) | |
| download | fparkan-c3e2add9a189879b5e800344e07645af591a2cbd.tar.xz fparkan-c3e2add9a189879b5e800344e07645af591a2cbd.zip | |
feat(ui): show live weapon readings in the hero cockpit
Diffstat (limited to 'crates/fparkan-font/src')
| -rw-r--r-- | crates/fparkan-font/src/bitmap.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/crates/fparkan-font/src/bitmap.rs b/crates/fparkan-font/src/bitmap.rs index 7f628b71..c28ce7f4 100644 --- a/crates/fparkan-font/src/bitmap.rs +++ b/crates/fparkan-font/src/bitmap.rs @@ -122,6 +122,42 @@ impl BitmapFont { Ok(((occupied + PADDING) as u32, pixels)) } + /// Renders a single line centered in a transparent RGBA image. + pub fn centered_line(&self, text: &str, width: u16, height: u16) -> Result<RgbaImage, String> { + let (bytes, _, unmappable) = WINDOWS_1251.encode(text); + if unmappable { + return Err("game text contains characters outside CP1251".into()); + } + let (width, height) = (usize::from(width), usize::from(height)); + if height < self.glyph_height || width == 0 { + return Err("game text rectangle is too small".into()); + } + let len = width + .checked_mul(height) + .and_then(|area| area.checked_mul(4)) + .filter(|&len| len <= isize::MAX as usize) + .ok_or("game text rectangle is too large")?; + let mut pixels = vec![0; len]; + let advance = bytes.iter().map(|&byte| self.advance(byte)).sum::<usize>(); + let mut x = width.saturating_sub(advance) / 2; + let y = (height - self.glyph_height) / 2; + for &byte in bytes.iter() { + self.draw_glyph( + self.glyphs[usize::from(byte)], + &mut pixels, + width, + [x, y], + width - x, + ); + x = (x + self.advance(byte)).min(width); + } + Ok(RgbaImage { + width: width as u32, + height: height as u32, + rgba8: pixels, + }) + } + fn advance(&self, byte: u8) -> usize { self.glyphs[usize::from(byte)].width - 1 + self.spacing } |
