refactor to cleanup font inclusions

allows more degrees of freedom to free up space
This commit is contained in:
bunnie 2026-04-23 00:32:43 +08:00
parent a40eba8199
commit 16073c9cf8
4 changed files with 122 additions and 81 deletions

View file

@ -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 = []

View file

@ -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<GlyphSprite, usize> {
}
}
#[cfg(feature = "emoji")]
pub fn emoji_glyph(ch: char) -> Result<GlyphSprite, usize> {
match emoji::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {
@ -208,6 +213,7 @@ pub fn emoji_glyph(ch: char) -> Result<GlyphSprite, usize> {
}
}
#[cfg(feature = "emoji")]
pub fn emoji_large_glyph(ch: char) -> Result<GlyphSprite, usize> {
match emoji::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {
@ -232,6 +238,7 @@ pub fn emoji_large_glyph(ch: char) -> Result<GlyphSprite, usize> {
}
}
#[cfg(feature = "zh")]
pub fn zh_glyph(ch: char) -> Result<GlyphSprite, usize> {
match zh::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {
@ -256,6 +263,7 @@ pub fn zh_glyph(ch: char) -> Result<GlyphSprite, usize> {
}
}
#[cfg(feature = "ja")]
pub fn ja_glyph(ch: char) -> Result<GlyphSprite, usize> {
match ja::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {
@ -280,6 +288,7 @@ pub fn ja_glyph(ch: char) -> Result<GlyphSprite, usize> {
}
}
#[cfg(feature = "kr")]
pub fn kr_glyph(ch: char) -> Result<GlyphSprite, usize> {
match kr::CODEPOINTS.binary_search(&(ch as u32)) {
Ok(n) => {

View file

@ -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)
}
}
}

View file

@ -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;