| Bring vidya in cfd3e36 nandi 19d ago | 1 | //! Extra glyph coverage for UI punctuation, math-in-prose, and block art. |
| 2 | //! |
| 3 | //! egui’s default Ubuntu Light lacks many symbols used in HIG-style copy |
| 4 | //! (`→`, `●`, `○`, `▾`/`▴`, en/em dashes, curly quotes, …), common LLM |
| 5 | //! math outside `$…$` (`ℝ`, `ⁿ`, `∑`, Greek, …), and box/block elements |
| 6 | //! used in Bluesky bios (`█▄▀░`, box drawing). On Android those codepoints |
| 7 | //! render as hollow boxes (“tofu”). |
| 8 | //! |
| 9 | //! [`install_symbol_font`] registers a DejaVu Sans subset as a **fallback** |
| 10 | //! so primary text stays Ubuntu, but missing symbols still draw. |
| 11 | |
| 12 | use egui::{ |
| 13 | epaint::text::{FontInsert, FontPriority, InsertFontFamily}, |
| 14 | Context, FontData, FontFamily, |
| 15 | }; |
| 16 | |
| 17 | /// Subset of DejaVu Sans covering UI symbols, math-in-prose, and box/block art. |
| 18 | /// See `assets/NOTICE` and `scripts/rebuild-symbols-ttf.sh`. |
| 19 | static VIDYA_SYMBOLS_TTF: &[u8] = include_bytes!("../assets/vidya-symbols.ttf"); |
| 20 | |
| 21 | const FONT_NAME: &str = "vidya-symbols"; |
| 22 | |
| 23 | /// Install the symbol fallback font (idempotent). |
| 24 | /// |
| 25 | /// Safe to call every frame / from [`crate::apply`]: egui skips re-install when |
| 26 | /// the font name is already present. |
| 27 | pub fn install_symbol_font(ctx: &Context) { |
| 28 | ctx.add_font(FontInsert::new( |
| 29 | FONT_NAME, |
| 30 | FontData::from_static(VIDYA_SYMBOLS_TTF), |
| 31 | vec![ |
| 32 | InsertFontFamily { |
| 33 | family: FontFamily::Proportional, |
| 34 | // After Ubuntu / emoji — only used when those lack the glyph. |
| 35 | priority: FontPriority::Lowest, |
| 36 | }, |
| 37 | InsertFontFamily { |
| 38 | family: FontFamily::Monospace, |
| 39 | priority: FontPriority::Lowest, |
| 40 | }, |
| 41 | ], |
| 42 | )); |
| 43 | } |
| 44 | |
| 45 | #[cfg(test)] |
| 46 | mod tests { |
| 47 | use super::{install_symbol_font, VIDYA_SYMBOLS_TTF}; |
| 48 | |
| 49 | /// Glyphs from a real Bluesky bio (nandi.uk) that previously tofued in Sleek. |
| 50 | const BLOCK_ART: &str = "█▄▀░"; |
| 51 | |
| 52 | #[test] |
| 53 | fn symbol_font_covers_block_elements() { |
| 54 | assert!( |
| 55 | VIDYA_SYMBOLS_TTF.len() > 10_000, |
| 56 | "vidya-symbols.ttf looks empty/truncated" |
| 57 | ); |
| 58 | |
| 59 | let ctx = egui::Context::default(); |
| 60 | install_symbol_font(&ctx); |
| 61 | // Allocate fonts so fallback families are built. |
| 62 | ctx.begin_pass(egui::RawInput::default()); |
| 63 | |
| 64 | let missing: Vec<char> = ctx.fonts(|fonts| { |
| 65 | let font_id = egui::FontId::proportional(16.0); |
| 66 | BLOCK_ART |
| 67 | .chars() |
| 68 | .filter(|&c| !fonts.has_glyph(&font_id, c)) |
| 69 | .collect() |
| 70 | }); |
| 71 | assert!( |
| 72 | missing.is_empty(), |
| 73 | "proportional fallback missing glyphs: {missing:?}" |
| 74 | ); |
| 75 | } |
| 76 | } |