aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-font
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 01:17:56 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 01:17:56 +0300
commitb8f6c1e8e169bc8d8ff0f85de1e3ddd478026e3d (patch)
tree32590dc403bb21bea05d0a61bc273e6f3ca8dd81 /crates/fparkan-font
parenteab46d9be33596b6c689a7a34b90837a89400a89 (diff)
downloadfparkan-b8f6c1e8e169bc8d8ff0f85de1e3ddd478026e3d.tar.xz
fparkan-b8f6c1e8e169bc8d8ff0f85de1e3ddd478026e3d.zip
feat(ui): render current messages in the native cockpit pager
Diffstat (limited to 'crates/fparkan-font')
-rw-r--r--crates/fparkan-font/src/bitmap.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/fparkan-font/src/bitmap.rs b/crates/fparkan-font/src/bitmap.rs
index 6360b367..0110de40 100644
--- a/crates/fparkan-font/src/bitmap.rs
+++ b/crates/fparkan-font/src/bitmap.rs
@@ -143,6 +143,43 @@ impl BitmapFont {
Ok([width, height])
}
+ /// Wraps pager tokens by their atlas advances. A newline starts a row
+ /// when it opens a token, while spaces inside a row retain their width.
+ pub fn wrap_pager_lines(&self, mut text: &str, max_width: u16) -> Result<Vec<String>, String> {
+ let mut lines = Vec::new();
+ let mut line = String::new();
+ let mut width = 0;
+ while !text.is_empty() {
+ if let Some(after) = text.strip_prefix('\n') {
+ lines.push(std::mem::take(&mut line));
+ text = after;
+ width = 0;
+ continue;
+ }
+ let is_space = text.starts_with(' ');
+ let end = if is_space {
+ text.len() - text.trim_start_matches(' ').len()
+ } else {
+ text.find(' ').unwrap_or(text.len())
+ };
+ let token = &text[..end];
+ text = &text[end..];
+ let advance = usize::from(self.line_metrics(token)?[0]);
+ if !line.is_empty() && width + advance > usize::from(max_width) {
+ lines.push(std::mem::take(&mut line));
+ width = 0;
+ }
+ if !is_space || !line.is_empty() {
+ line.push_str(token);
+ width += advance;
+ }
+ }
+ if !line.is_empty() {
+ lines.push(line);
+ }
+ Ok(lines)
+ }
+
/// Composes a single line from a game resource's original CP1251 bytes.
pub fn centered_line_bytes(
&self,