//! 下载线程与字体注册(M168):`install.rs` 的「动作」那一半。 //! //! ## 为什么拆出来 //! //! `install.rs` 是「状态 + 判据 + 触发」,已经贴着 400 行硬线。这里只放真正干活的两件: //! //! 1. [`start`]:起一条后台线程(进程内同时只有一条),把「正在下载 → 结果」写回状态槽; //! 2. [`register_into_font_system`]:把字节交给 iced 的全局字体系统(运行时注册)。 //! //! ## 为什么下载在后台线程 //! //! 一份 17 MiB 的字体在弱网下要几十秒,而两个触发点(启动编排、设置页切语言)都在 UI 线程上。 //! 在 UI 线程同步下完 = 窗口几十秒不响应(启动路径甚至是「窗口还没出来」)。 //! //! ## 线程里做了什么(顺序即判据) //! //! ```text //! CdnFetcher(缓存命中 ⇒ 零网络;否则共享引擎下载 + sha256 复验) //! → i18n::font::ensure 只有 NeedsDownload 才调 fetcher //! → 成功:注册进 iced,状态 Ready{family, from_cache} //! → 失败:状态 Failed{class},**什么都不注册**(降级回系统字体,界面照常可用) //! ``` //! //! ## 诚实边界 //! //! - **抓得住**:状态流转、失败不注册、日志三行(start / ok / failed); //! - **抓不住**:真机上字体注册之后**出字的样子**(cosmic-text 用 `wght` 轴选字重这件事只核对了 //! 上游源码,没有真机截图对照;macOS / Linux 完全没有真机)。 use unlucky_i18n::font::{self, Strategy}; use super::{ensure_for, update, CjkInstall, FetchClass}; use crate::fonts::cdn::CdnFetcher; use crate::platform::data; /// 起一条后台下载(进程内同时只允许一条:`install::after_probe` 负责那道闸)。 pub(super) fn start(font: &'static font::CjkFont) { let font::Source::Pinned { bytes, .. } = font.source; let Some(cache_root) = data::cache_root() else { update(CjkInstall::Failed { class: FetchClass::Disk, message: "数据根解析不出来,字体无处可落".to_owned(), }); return; }; let fetcher = match CdnFetcher::new(font, cache_root) { Ok(fetcher) => fetcher, Err(error) => { update(CjkInstall::Failed { class: FetchClass::from(&error), message: error.message().to_owned(), }); return; } }; let cache_file = fetcher.cache_file(); update(CjkInstall::Downloading { bytes }); eprintln!( "[font] cjk-download start family=\"{}\" bytes={bytes} file={} cache_present={}", font.family, cache_file.display(), // 这条只是「文件在不在」(诊断用),**不是** from_cache:坏缓存会被删掉重下, // 真正的命中结论在 `fetcher.was_cached()`(取完之后)。 fetcher.has_cached() ); // 判据说「需要」,这条线程才存在:不需要的三种情形在 `after_probe` 就返回了。 std::thread::spawn(move || { let state = ensure_for( Strategy::NeedsDownload(font), &fetcher, ®ister_into_font_system, ); // `from_cache` 只认下载器**这次**的结论(复审 r1 次要项):取之前「文件在不在」会说谎—— // 坏缓存会被删掉重下,那时 `has_cached()` 还是 true,而真相是「联网下了一次」。 let from_cache = fetcher.was_cached().unwrap_or(false); let state = match state { CjkInstall::Ready { family, .. } => CjkInstall::Ready { family, from_cache }, other => other, }; match &state { CjkInstall::Ready { family, from_cache } => eprintln!( "[font] cjk-download ok family=\"{family}\" from_cache={from_cache} (中文字形现在由它兜底,界面不需要重启)" ), CjkInstall::Failed { class, message } => eprintln!( "[font] cjk-download failed class={} {message}(降级:继续用系统字体,界面照常可用)", class.as_str() ), other => eprintln!("[font] cjk-download {}", other.describe()), } update(state); }); } /// 把字体字节注册进 iced 的**全局字体系统**(`iced_graphics` 的 `FontSystem::load_font`)。 /// /// 返回注册后的族名(从字体库里读回来的):变量字体的 `name` 表里族名是 `Noto Sans SC`、 /// 而默认实例是 Thin,所以**不能**假设「注册了就叫 NOTO_SANS_SC.family」——这里读回来核对。 /// /// # Errors /// 字体系统锁被毒掉、或注册后字体库里找不到那个族(字节不是字体 / `fontdb` 不认)。 pub fn register_into_font_system(bytes: Vec) -> Result { let lock = iced::advanced::graphics::text::font_system(); let mut font_system = lock .write() .map_err(|_| "字体系统的读写锁被毒掉(另一处注册 panic 过)".to_owned())?; font_system.load_font(std::borrow::Cow::Owned(bytes)); // 先把族名抄出来再判:`faces()` 的迭代器借着 `font_system`,直接链到 `ok_or_else` 会让 // 那个借用活过守卫(E0597)。 let families: Vec = font_system .raw() .db() .faces() .flat_map(|face| face.families.iter()) .map(|(family, _)| family.clone()) .collect(); families .iter() .find(|family| family.as_str() == font::NOTO_SANS_SC.family) .cloned() .ok_or_else(|| format!("注册后在字体库里找不到 \"{}\"", font::NOTO_SANS_SC.family)) }