From 16073c9cf872abcfd2d370bb950a39dda79b5916 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 23 Apr 2026 00:32:43 +0800 Subject: [PATCH] refactor to cleanup font inclusions allows more degrees of freedom to free up space --- libs/blitstr2/Cargo.toml | 17 ++-- libs/blitstr2/src/fonts.rs | 9 ++ libs/blitstr2/src/lib.rs | 25 +++++ libs/blitstr2/src/style_macros.rs | 152 +++++++++++++++--------------- 4 files changed, 122 insertions(+), 81 deletions(-) diff --git a/libs/blitstr2/Cargo.toml b/libs/blitstr2/Cargo.toml index ad71706c3..09375c379 100644 --- a/libs/blitstr2/Cargo.toml +++ b/libs/blitstr2/Cargo.toml @@ -19,13 +19,18 @@ rkyv = { version = "0.8.8", default-features = false, features = [ ] } [features] +zh = [] +ja = [] +kr = [] +emoji = [] + # selection of board settings for display resolution bao1x = [] -board-baosec = [] -board-baosor = [] -hosted-baosec = [] -hosted = [] -renode = [] -precursor = [] +board-baosec = ["emoji", "ja"] +board-baosor = ["emoji", "ja", "zh", "kr"] +hosted-baosec = ["emoji", "ja", "zh", "kr"] +hosted = ["emoji", "ja", "zh", "kr"] +renode = ["emoji", "ja", "zh", "kr"] +precursor = ["emoji", "ja", "zh", "kr"] default = [] diff --git a/libs/blitstr2/src/fonts.rs b/libs/blitstr2/src/fonts.rs index e50e29af8..ba41467bc 100644 --- a/libs/blitstr2/src/fonts.rs +++ b/libs/blitstr2/src/fonts.rs @@ -3,13 +3,17 @@ // #![allow(dead_code)] pub mod bold; +#[cfg(feature = "emoji")] pub mod emoji; +#[cfg(feature = "ja")] pub mod ja; +#[cfg(feature = "kr")] pub mod kr; pub mod mono; pub mod regular; pub mod small; pub mod tall; +#[cfg(feature = "zh")] pub mod zh; use crate::GlyphSprite; @@ -184,6 +188,7 @@ pub fn tall_glyph(ch: char) -> Result { } } +#[cfg(feature = "emoji")] pub fn emoji_glyph(ch: char) -> Result { match emoji::CODEPOINTS.binary_search(&(ch as u32)) { Ok(n) => { @@ -208,6 +213,7 @@ pub fn emoji_glyph(ch: char) -> Result { } } +#[cfg(feature = "emoji")] pub fn emoji_large_glyph(ch: char) -> Result { match emoji::CODEPOINTS.binary_search(&(ch as u32)) { Ok(n) => { @@ -232,6 +238,7 @@ pub fn emoji_large_glyph(ch: char) -> Result { } } +#[cfg(feature = "zh")] pub fn zh_glyph(ch: char) -> Result { match zh::CODEPOINTS.binary_search(&(ch as u32)) { Ok(n) => { @@ -256,6 +263,7 @@ pub fn zh_glyph(ch: char) -> Result { } } +#[cfg(feature = "ja")] pub fn ja_glyph(ch: char) -> Result { match ja::CODEPOINTS.binary_search(&(ch as u32)) { Ok(n) => { @@ -280,6 +288,7 @@ pub fn ja_glyph(ch: char) -> Result { } } +#[cfg(feature = "kr")] pub fn kr_glyph(ch: char) -> Result { match kr::CODEPOINTS.binary_search(&(ch as u32)) { Ok(n) => { diff --git a/libs/blitstr2/src/lib.rs b/libs/blitstr2/src/lib.rs index 0beab11cd..25b33295a 100644 --- a/libs/blitstr2/src/lib.rs +++ b/libs/blitstr2/src/lib.rs @@ -61,3 +61,28 @@ pub fn glyph_height_hint(g: GlyphStyle) -> usize { GlyphStyle::Tall => 19, } } + +/// Find glyph for char using latin regular, emoji, ja, zh, and kr font data +pub fn style_glyph(locale: &'static str, ch: char, base_style: &GlyphStyle) -> GlyphSprite { + match locale { + #[cfg(feature = "zh")] + "zh" => { + style_wrapper!(zh_rules, base_style, ch) + } + #[cfg(feature = "ja")] + "jp" => { + style_wrapper!(jp_rules, base_style, ch) + } + #[cfg(feature = "kr")] + "kr" => { + style_wrapper!(kr_rules, base_style, ch) + } + "en-tts" => { + style_wrapper!(en_audio_rules, base_style, ch) + } + // default to English rules + _ => { + style_wrapper!(english_rules, base_style, ch) + } + } +} diff --git a/libs/blitstr2/src/style_macros.rs b/libs/blitstr2/src/style_macros.rs index 219769157..7f0688483 100644 --- a/libs/blitstr2/src/style_macros.rs +++ b/libs/blitstr2/src/style_macros.rs @@ -30,78 +30,79 @@ macro_rules! style_wrapper { } #[macro_export] +#[cfg(feature = "zh")] macro_rules! zh_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match zh_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match ja_glyph($ch) { - Ok(g) => g, - _ => match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, + ($base_style:expr, $emoji_style:expr, $ch:ident) => {{ + // Always-present: zh itself, base, emoji + #[allow(unused_mut)] + let mut result = + zh_glyph($ch).ok().or_else(|| $base_style($ch).ok()).or_else(|| $emoji_style($ch).ok()); + + #[cfg(feature = "ja")] + if result.is_none() { + result = ja_glyph($ch).ok(); } - }; + + #[cfg(feature = "kr")] + if result.is_none() { + result = kr_glyph($ch).ok(); + } + + // Always-present: replacement and null sentinel + result.or_else(|| $base_style(REPLACEMENT).ok()).unwrap_or(NULL_GLYPH_SPRITE) + }}; } #[macro_export] +#[cfg(feature = "ja")] macro_rules! jp_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match ja_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, + ($base_style:expr, $emoji_style:expr, $ch:ident) => {{ + // Always-present: ja itself, base, emoji + #[allow(unused_mut)] + let mut result = + ja_glyph($ch).ok().or_else(|| $base_style($ch).ok()).or_else(|| $emoji_style($ch).ok()); + + // Optional: zh fallback only if zh is in the build + #[cfg(feature = "zh")] + if result.is_none() { + result = zh_glyph($ch).ok(); } - }; + + // Optional: kr fallback only if kr is in the build + #[cfg(feature = "kr")] + if result.is_none() { + result = kr_glyph($ch).ok(); + } + + // Always-present: replacement and null sentinel + result.or_else(|| $base_style(REPLACEMENT).ok()).unwrap_or(NULL_GLYPH_SPRITE) + }}; } #[macro_export] +#[cfg(feature = "kr")] macro_rules! kr_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match kr_glyph($ch) { - Ok(g) => g, - _ => match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - _ => match ja_glyph($ch) { - Ok(g) => g, - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, - }, - }, + ($base_style:expr, $emoji_style:expr, $ch:ident) => {{ + // Always-present: kr itself, base, emoji + #[allow(unused_mut)] + let mut result = + kr_glyph($ch).ok().or_else(|| $base_style($ch).ok()).or_else(|| $emoji_style($ch).ok()); + + // Optional: ja fallback only if ja is in the build + #[cfg(feature = "ja")] + if result.is_none() { + result = ja_glyph($ch).ok(); } - }; + + // Optional: zh fallback only if zh is in the build + #[cfg(feature = "zh")] + if result.is_none() { + result = zh_glyph($ch).ok(); + } + + // Always-present: replacement and null sentinel + result.or_else(|| $base_style(REPLACEMENT).ok()).unwrap_or(NULL_GLYPH_SPRITE) + }}; } #[macro_export] @@ -150,27 +151,28 @@ macro_rules! english_rules { // Reflect reduced font space on small-memory footprint devices #[cfg(any(feature = "board-baosec", feature = "hosted-baosec"))] macro_rules! english_rules { - ($base_style:expr, $emoji_style:expr, $ch:ident) => { - match $base_style($ch) { - Ok(g) => g, - _ => match $emoji_style($ch) { - Ok(g) => g, - // add more character sets to bao1x target by adding match clauses here - _ => match zh_glyph($ch) { - Ok(g) => g, - _ => match $base_style(REPLACEMENT) { - Ok(g) => g, - _ => NULL_GLYPH_SPRITE, - }, - }, - }, + ($base_style:expr, $emoji_style:expr, $ch:ident) => {{ + // Always-present: base, emoji + #[allow(unused_mut)] + let mut result = $base_style($ch).ok().or_else(|| $emoji_style($ch).ok()); + + // Optional: zh fallback only if zh is in the build + #[cfg(feature = "zh")] + if result.is_none() { + result = zh_glyph($ch).ok(); } - }; + + // Always-present: replacement and null sentinel + result.or_else(|| $base_style(REPLACEMENT).ok()).unwrap_or(NULL_GLYPH_SPRITE) + }}; } pub use en_audio_rules; pub use english_rules; +#[cfg(feature = "ja")] pub use jp_rules; +#[cfg(feature = "kr")] pub use kr_rules; pub use style_wrapper; +#[cfg(feature = "zh")] pub use zh_rules;