mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-29 04:07:12 -06:00
Keep CJK UI fallback fonts to ui_fonts
The app already finds an installed font for CJK (and other) characters the UI fonts lack, the first time a frame paints one (ui_fonts): reading a system CJK font at every startup duplicated that, cost a font file's read and memory for interfaces that never show CJK, and broke its test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f0349fb2f8
commit
22443d8711
@@ -255,53 +255,9 @@ pub fn install_fonts(ctx: &egui::Context) {
|
||||
mono.extend(fallback);
|
||||
fonts.families.insert(FontFamily::Name(FONT_MONO.into()), mono);
|
||||
add_craft_fonts(&mut fonts);
|
||||
// System CJK fallback (PingFang / Heiti / Noto…) when craft-fonts are absent.
|
||||
if let Some((name, data)) = system_cjk_font() {
|
||||
fonts.font_data.insert(name.clone(), Arc::new(data));
|
||||
for stack in fonts.families.values_mut() {
|
||||
stack.push(name.clone());
|
||||
}
|
||||
}
|
||||
ctx.set_fonts(fonts);
|
||||
}
|
||||
|
||||
/// A CJK font already installed on the system, as a last-resort fallback.
|
||||
fn system_cjk_font() -> Option<(String, FontData)> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let paths: &[&str] = if cfg!(target_os = "macos") {
|
||||
&[
|
||||
"/System/Library/Fonts/PingFang.ttc",
|
||||
"/System/Library/Fonts/STHeiti Medium.ttc",
|
||||
"/System/Library/Fonts/STHeiti Light.ttc",
|
||||
"/System/Library/Fonts/Hiragino Sans GB.ttc",
|
||||
"/System/Library/Fonts/Supplemental/Songti.ttc",
|
||||
"/Library/Fonts/Arial Unicode.ttf",
|
||||
]
|
||||
} else if cfg!(target_os = "windows") {
|
||||
&[r"C:\Windows\Fonts\msyh.ttc", r"C:\Windows\Fonts\simhei.ttf", r"C:\Windows\Fonts\simsun.ttc"]
|
||||
} else {
|
||||
&[
|
||||
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
||||
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
|
||||
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
|
||||
]
|
||||
};
|
||||
for path in paths {
|
||||
if let Ok(bytes) = std::fs::read(path) {
|
||||
let mut data = FontData::from_owned(bytes);
|
||||
data.index = 0;
|
||||
return Some(("system-cjk".to_string(), data));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// The egui name of a craft-fonts face.
|
||||
pub(crate) fn craft_font_name(f: &vectorcraft_text::CraftFont) -> String {
|
||||
format!("craft-fonts {} {}", f.family, f.style)
|
||||
|
||||
Reference in New Issue
Block a user