feat(web): lazy per-script font loading for international CAD text

The web build has no system-font access, so CAD text in non-Latin scripts
(Cyrillic, Greek, CJK, …) rendered as nothing — build_fallback was a hard
None on wasm (#141). Instead, ship per-script Noto subsets under web/fonts
(one alphabet per file, SIL OFL 1.1) and fetch them lazily over HTTP the
first time a drawing uses that script, then outline glyphs with ttf-parser.

- web_font: Script enum + script_of (char → script font), a per-script
  store with lazy fetch, and a pending queue drained by the app loop.
- CJK is split by language (chinese/japanese/korean). Han ideographs share
  code points but differ by language, so the shared block is routed by the
  document's $DWGCODEPAGE (932→JP, 949→KR, GB/936→CN); kana is always
  Japanese, Hangul always Korean. Re-tessellates when the language changes.
- build_fallback (wasm) outlines from the fetched subset; clear_fallback_cache
  lets glyphs that missed while a font was in flight reappear on load.
- PollWebFonts subscription (web only) + WebFontLoaded message drive fetches.
- Trunk copy-dir serves web/fonts; nothing is bundled into the native binary
  (desktop keeps using system fonts).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Opus 4.8 committed 2026-06-20 16:13:26 +03:00
1 parent d36cc9786f
commit 54106ca9b0
21 files changed
+453 -5

No files matched your search

+5
View File
@@ -794,6 +794,11 @@ pub enum DsField {
#[derive(Debug, Clone)]
pub enum Message {
Tick(Instant),
/// Web: periodic check for per-script fonts a drawing needs but hasn't
/// fetched yet (#141). Native: never emitted.
PollWebFonts,
/// Web: a per-script font finished fetching — `Ok(bytes)` or `Err(reason)`.
WebFontLoaded(crate::scene::text::web_font::Script, Result<Vec<u8>, String>),
OpenFile,
/// File picker returned. `Some((path, size_in_bytes))` → start loading;
/// `None` → user cancelled the dialog (no overlay shown).
+53
View File
@@ -364,6 +364,42 @@ impl OpenCADStudio {
fn update_inner(&mut self, msg: Message) -> Task<Message> {
match msg {
// Web: a drawing referenced a script whose Noto subset isn't loaded
// yet (recorded during text tessellation). Kick off one fetch per
// pending script; the result comes back as `WebFontLoaded`. (#141)
Message::PollWebFonts => {
let pending = crate::scene::text::web_font::take_pending();
if pending.is_empty() {
return Task::none();
}
Task::batch(pending.into_iter().map(|script| {
Task::perform(crate::scene::text::web_font::fetch(script), move |res| {
Message::WebFontLoaded(script, res)
})
}))
}
// Web: a per-script font arrived. Store it, drop the stale fallback
// glyph cache (entries that resolved to nothing while it loaded),
// and re-tessellate so the text appears. (#141)
Message::WebFontLoaded(script, res) => {
match res {
Ok(bytes) => {
crate::scene::text::web_font::insert(script, Some(bytes));
crate::scene::text::ttf_glyph::clear_fallback_cache();
for tab in self.tabs.iter_mut() {
tab.scene.bump_geometry();
}
}
Err(e) => {
crate::scene::text::web_font::insert(script, None);
self.command_line
.push_error(&format!("Font load failed ({script:?}): {e}"));
}
}
Task::none()
}
Message::Tick(t) => {
let i = self.active_tab;
self.tabs[i].scene.update(t - self.start);
@@ -500,6 +536,15 @@ impl OpenCADStudio {
self.tabs[i].current_path = Some(path.clone());
self.tabs[i].scene.document = doc;
// Route shared CJK ideographs to the language matching this
// drawing's code page (web per-language font split). Drop the
// glyph cache if it changed so Han re-resolves to the new
// language's font; geometry is (re)built below regardless. (#141)
if crate::scene::text::web_font::set_cjk_lang_from_codepage(
&self.tabs[i].scene.document.header.code_page,
) {
crate::scene::text::ttf_glyph::clear_fallback_cache();
}
// Current model-space annotation scale comes from the drawing's
// CANNOSCALEVALUE (paper/drawing factor); the multiplier we use
// for text/dim sizing is its inverse (1:50 -> 0.02 -> 50.0).
@@ -1298,6 +1343,14 @@ impl OpenCADStudio {
self.sync_ribbon_from_selection();
// Grid/snap follow the newly active drawing's viewport.
self.adopt_view_display(idx);
// Shared CJK ideographs follow the newly active drawing's
// language; re-tessellate if it differs from the last. (#141)
if crate::scene::text::web_font::set_cjk_lang_from_codepage(
&self.tabs[idx].scene.document.header.code_page,
) {
crate::scene::text::ttf_glyph::clear_fallback_cache();
self.tabs[idx].scene.bump_geometry();
}
}
Task::none()
}
+9
View File
@@ -1630,11 +1630,20 @@ impl OpenCADStudio {
} else {
Subscription::none()
};
// Web: poll for per-script fonts that a drawing's text needs but hasn't
// fetched yet. Cheap — `PollWebFonts` is a no-op when nothing is
// pending. Native has system fonts, so no polling. (#141)
#[cfg(target_arch = "wasm32")]
let web_fonts =
iced::time::every(std::time::Duration::from_millis(300)).map(|_| Message::PollWebFonts);
#[cfg(not(target_arch = "wasm32"))]
let web_fonts = Subscription::none();
iced::Subscription::batch([
frames,
history_tick,
grip_dwell,
caret_blink,
web_fonts,
event::listen_with(|ev, status, win_id| {
use iced::event::Status;
match ev {
+1
View File
@@ -2,4 +2,5 @@ pub mod lff;
pub mod font_face;
pub mod sysfont;
pub mod ttf_glyph;
pub mod web_font;
pub mod complex_lt;
+28 -4
View File
@@ -263,11 +263,35 @@ pub fn fallback_glyph(ch: char) -> Option<Arc<Glyph>> {
built
}
/// Web: no system fonts → no cosmic-text fallback (LFF-missing glyphs simply
/// do not render).
/// Drop the cached fallback glyphs so the next lookup re-resolves. The web
/// build calls this when a per-script font finishes loading: glyphs that
/// resolved to `None` while the font was still in flight then get a real
/// outline. (#141)
pub fn clear_fallback_cache() {
fallback_cache().lock().unwrap().clear();
}
/// Web: outline the glyph from the lazily-fetched per-script Noto subset that
/// covers it. Returns `None` while that font is still loading (the char renders
/// once it arrives and the fallback cache is cleared). (#141)
#[cfg(target_arch = "wasm32")]
fn build_fallback(_ch: char) -> Option<Arc<Glyph>> {
None
fn build_fallback(ch: char) -> Option<Arc<Glyph>> {
let script = crate::scene::text::web_font::script_of(ch)?;
let bytes = crate::scene::text::web_font::request(script)?;
let face = ttf_parser::Face::parse(&bytes, 0).ok()?;
let gid = face.glyph_index(ch)?;
let k = cap_scale(&face);
let advance = face.glyph_hor_advance(gid).unwrap_or(0) as f32 * k;
let mut fl = OutlineFlattener::new(k);
face.outline_glyph(gid, &mut fl);
fl.flush();
if fl.contours.is_empty() {
return None;
}
Some(Arc::new(Glyph {
strokes: fl.contours,
advance,
}))
}
#[cfg(not(target_arch = "wasm32"))]
+199
View File
@@ -0,0 +1,199 @@
//! Per-script web font store + lazy fetch (#141).
//!
//! The desktop build outlines glyphs for non-LFF scripts from the user's
//! installed system fonts (see `ttf_glyph::build_fallback`). The web build has
//! no system-font access, so it instead lazily fetches a per-script Noto subset
//! — served from `fonts/<script>.ttf`, one alphabet per file — the first time a
//! drawing uses that script, then outlines glyphs from it. Splitting per script
//! keeps each download small (Latin/Cyrillic/Greek ~50–100 KB; CJK loads only
//! when a CJK drawing is opened).
//!
//! The store and fetch are web-only; the desktop side keeps no-op stubs so the
//! shared call sites (`ttf_glyph`, the app message loop) compile unchanged.
use std::sync::atomic::{AtomicU8, Ordering};
/// A script we ship a Noto subset for. [`script_of`] maps a char to one.
///
/// CJK is split by language — Chinese, Japanese and Korean each get their own
/// file. Their ideographs (Han, U+4E00–9FFF) share the same code points but
/// differ in glyph shape, so the shared block is routed by the document's
/// language (see [`set_cjk_lang_from_codepage`]); kana is always Japanese and
/// Hangul always Korean.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Script {
Latin,
Cyrillic,
Greek,
Arabic,
Hebrew,
Thai,
Devanagari,
Chinese,
Japanese,
Korean,
}
impl Script {
/// Same-origin asset path the web build fetches this script's font from.
/// Must match the files produced by `web/fonts/generate.sh`.
pub fn asset(self) -> &'static str {
match self {
Script::Latin => "fonts/latin.ttf",
Script::Cyrillic => "fonts/cyrillic.ttf",
Script::Greek => "fonts/greek.ttf",
Script::Arabic => "fonts/arabic.ttf",
Script::Hebrew => "fonts/hebrew.ttf",
Script::Thai => "fonts/thai.ttf",
Script::Devanagari => "fonts/devanagari.ttf",
Script::Chinese => "fonts/chinese.ttf",
Script::Japanese => "fonts/japanese.ttf",
Script::Korean => "fonts/korean.ttf",
}
}
}
/// Language used to render shared Han ideographs: 0 = Chinese, 1 = Japanese,
/// 2 = Korean. Set from the active document's code page.
static CJK_LANG: AtomicU8 = AtomicU8::new(0);
fn cjk_lang() -> Script {
match CJK_LANG.load(Ordering::Relaxed) {
1 => Script::Japanese,
2 => Script::Korean,
_ => Script::Chinese,
}
}
/// Point the shared-Han routing at a language based on a DWG/DXF code page
/// (`$DWGCODEPAGE`), e.g. `ANSI_932` → Japanese, `ANSI_949` → Korean, GB/936 or
/// anything else → Chinese. Returns `true` if the language changed (the caller
/// then clears the glyph cache and re-tessellates).
pub fn set_cjk_lang_from_codepage(code_page: &str) -> bool {
let c = code_page.to_ascii_uppercase();
let lang = if c.contains("932") || c.contains("SJIS") || c.contains("SHIFT") {
1 // Japanese (Shift-JIS)
} else if c.contains("949") || c.contains("KOR") || c.contains("UHC") {
2 // Korean
} else {
0 // Chinese (936 / GB / 950 / Big5) or non-CJK default
};
CJK_LANG.swap(lang, Ordering::Relaxed) != lang
}
/// The script font that covers `ch`, or `None` for control / uncovered code
/// points. Ranges mirror the subset unicode ranges in `web/fonts/generate.sh`.
pub fn script_of(ch: char) -> Option<Script> {
Some(match ch as u32 {
0x0000..=0x024F | 0x1E00..=0x1EFF | 0x2000..=0x206F => Script::Latin,
0x0370..=0x03FF | 0x1F00..=0x1FFF => Script::Greek,
0x0400..=0x052F | 0x2DE0..=0x2DFF | 0xA640..=0xA69F => Script::Cyrillic,
0x0590..=0x05FF | 0xFB1D..=0xFB4F => Script::Hebrew,
0x0600..=0x06FF | 0x0750..=0x077F | 0x08A0..=0x08FF | 0xFB50..=0xFDFF | 0xFE70..=0xFEFF => {
Script::Arabic
}
0x0900..=0x097F => Script::Devanagari,
// Hangul → always Korean; kana → always Japanese.
0x1100..=0x11FF | 0x3130..=0x318F | 0xAC00..=0xD7A3 => Script::Korean,
0x3040..=0x30FF | 0x31F0..=0x31FF => Script::Japanese,
// Shared Han + CJK symbols + fullwidth → routed by the document language.
0x3000..=0x303F | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0xFF00..=0xFFEF => {
cjk_lang()
}
_ => return None,
})
}
// ── Web store ───────────────────────────────────────────────────────────────
#[cfg(target_arch = "wasm32")]
mod imp {
use super::Script;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
enum State {
Loading,
Loaded(Arc<Vec<u8>>),
Failed,
}
#[derive(Default)]
struct Store {
states: HashMap<Script, State>,
/// Scripts requested but not yet fetched; the app drains this and kicks
/// off the fetch tasks.
pending: Vec<Script>,
}
fn store() -> &'static Mutex<Store> {
static S: OnceLock<Mutex<Store>> = OnceLock::new();
S.get_or_init(|| Mutex::new(Store::default()))
}
/// Loaded font bytes for `script`, or `None` — queueing a fetch the first
/// time a script is missed so the app loop can load it.
pub fn request(script: Script) -> Option<Arc<Vec<u8>>> {
let mut s = store().lock().unwrap();
match s.states.get(&script) {
Some(State::Loaded(b)) => Some(b.clone()),
Some(_) => None, // Loading or Failed — don't re-queue.
None => {
s.states.insert(script, State::Loading);
s.pending.push(script);
None
}
}
}
/// Drain the scripts awaiting a fetch.
pub fn take_pending() -> Vec<Script> {
std::mem::take(&mut store().lock().unwrap().pending)
}
/// Record a fetch result: `Some(bytes)` on success, `None` on failure.
pub fn insert(script: Script, bytes: Option<Vec<u8>>) {
let st = match bytes {
Some(b) => State::Loaded(Arc::new(b)),
None => State::Failed,
};
store().lock().unwrap().states.insert(script, st);
}
/// Fetch a script font over HTTP from the same origin.
pub async fn fetch(script: Script) -> Result<Vec<u8>, String> {
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
let win = web_sys::window().ok_or("no window")?;
let resp_val = JsFuture::from(win.fetch_with_str(script.asset()))
.await
.map_err(|e| format!("{e:?}"))?;
let resp: web_sys::Response = resp_val.dyn_into().map_err(|_| "bad response".to_string())?;
if !resp.ok() {
return Err(format!("HTTP {}", resp.status()));
}
let ab = JsFuture::from(resp.array_buffer().map_err(|e| format!("{e:?}"))?)
.await
.map_err(|e| format!("{e:?}"))?;
Ok(js_sys::Uint8Array::new(&ab).to_vec())
}
}
#[cfg(not(target_arch = "wasm32"))]
mod imp {
use super::Script;
use std::sync::Arc;
pub fn request(_script: Script) -> Option<Arc<Vec<u8>>> {
None
}
pub fn take_pending() -> Vec<Script> {
Vec::new()
}
pub fn insert(_script: Script, _bytes: Option<Vec<u8>>) {}
pub async fn fetch(_script: Script) -> Result<Vec<u8>, String> {
Err("web only".into())
}
}
pub use imp::{fetch, insert, request, take_pending};