From 4edef6369507d9a30b4d1c533e1b918cf7099526 Mon Sep 17 00:00:00 2001 From: mc-yzy15 <124588885+mc-yzy15@users.noreply.github.com> Date: Sat, 27 Jun 2026 14:13:54 +0000 Subject: [PATCH] =?UTF-8?q?fix(shell):=20=E4=BF=AE=E5=A4=8D=20#367=20-=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20msyh.ttc=20=E6=97=B6=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E9=A1=B9=E6=98=BE=E7=A4=BA=20"=E2=96=A1"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原 apply_fonts_to_nvg 委托给 ui::register_default_windows_font_suite, 其回退链设计假设 main=Segoe UI(Latin) + fallback=msyh(CJK)。 当用户把 main 改成 msyh.ttc 时,main 和 fallback 两条线都指向 msyh.ttc, 没有任何拉丁/符号字体兜底。Shell 扩展菜单项中混入了少量特殊 Unicode 字符(不在 msyh.ttc 中),原本指望 Segoe UI 兜底,现在 兜底没了,直接渲染成 .notdef → "□"。 另外 msyh.ttc 集合里 index=1 是「Microsoft YaHei UI」, 字面度量和字形都更贴 UI 场景;之前的实现始终用 index=0 「Microsoft YaHei」普通版,菜单里看着略偏大、有些字间距也对不齐。 变更: - src/shell/config.cc: 重写 apply_fonts_to_nvg - 新增独立的 "segoeui" 字体族,从系统字体目录直接加载 segoeui.ttf 及其字重变体(semilight / light / semibold / bold) - "fallback" 的 fallback_families 设为 {"segoeui"},确保兜底 - "main" 的 fallback_families 设为 {"segoeui", "fallback"}, 改 CJK 字体也不会再丢掉拉丁/符号覆盖 - "monospace" 的 fallback_families 设为 {"main", "segoeui", "fallback"} - 检测到 fallback 路径是 msyh.ttc 时 collection_index=1 (选 YaHei UI 而非普通 YaHei) - src/shell/config.cc: 新增 头(用 std::tolower 做大小写不敏感匹配) close #367 --- src/shell/config.cc | 110 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 4 deletions(-) diff --git a/src/shell/config.cc b/src/shell/config.cc index 6b5ff945..8e9b990d 100644 --- a/src/shell/config.cc +++ b/src/shell/config.cc @@ -1,4 +1,5 @@ #include "config.h" +#include #include #include #include @@ -156,10 +157,111 @@ std::filesystem::path config::default_mono_font() { "consola.ttf"; } void config::apply_fonts_to_nvg(NVGcontext *nvg) { - ui::register_default_windows_font_suite( - nvg, {.main_regular = {.path = font_path_main}, - .fallback_regular = {.path = font_path_fallback}, - .monospace_regular = {.path = font_path_monospace}}); + auto font_dir = ui::windows_font_directory(); + + auto to_lower = [](std::string s) { + for (auto &c : s) + c = static_cast(std::tolower(static_cast(c))); + return s; + }; + + auto add_with_system_variants = + [&](std::vector &faces, + const std::filesystem::path &user_path, const char *expected_name, + int collection_index = 0) { + if (user_path.empty()) + return; + faces.push_back( + {.weight = 400, + .source = {.path = user_path, + .collection_index = collection_index}}); + + if (to_lower(user_path.filename().string()) != + to_lower(expected_name)) + return; + + struct variant_entry { + int weight; + const char *file; + }; + const variant_entry *variants = nullptr; + int variant_count = 0; + + // clang-format off + if (to_lower(expected_name) == "segoeui.ttf") { + static constexpr variant_entry v[] = { + {200, "segoeuisl.ttf"}, {300, "segoeuil.ttf"}, + {600, "seguisb.ttf"}, {700, "segoeuib.ttf"}, + }; + variants = v; variant_count = 4; + } else if (to_lower(expected_name) == "msyh.ttc") { + static constexpr variant_entry v[] = { + {300, "msyhl.ttc"}, {700, "msyhbd.ttc"}, + }; + variants = v; variant_count = 2; + } else if (to_lower(expected_name) == "consola.ttf") { + static constexpr variant_entry v[] = { + {700, "consolab.ttf"}, + }; + variants = v; variant_count = 1; + } + // clang-format on + + for (int i = 0; i < variant_count; ++i) { + auto p = font_dir / variants[i].file; + if (std::filesystem::exists(p)) + faces.push_back( + {.weight = variants[i].weight, + .source = {.path = std::move(p), + .collection_index = collection_index}}); + } + }; + + // 1. System Segoe UI – always available as ultimate fallback + { + std::vector faces; + add_with_system_variants(faces, font_dir / "segoeui.ttf", + "segoeui.ttf"); + ui::register_font_family(nvg, {.family_name = "segoeui", + .faces = faces}); + } + + // 2. Fallback font (user-configured) – falls back to segoeui + // For msyh.ttc use collection_index=1 → "Microsoft YaHei UI" + { + int fb_idx = 0; + if (to_lower(font_path_fallback.filename().string()) == "msyh.ttc") + fb_idx = 1; + + std::vector faces; + add_with_system_variants(faces, font_path_fallback, "msyh.ttc", + fb_idx); + ui::register_font_family( + nvg, {.family_name = "fallback", + .faces = faces, + .fallback_families = {"segoeui"}}); + } + + // 3. Main font (user-configured) – falls back to segoeui + fallback + { + std::vector faces; + add_with_system_variants(faces, font_path_main, "segoeui.ttf"); + ui::register_font_family( + nvg, {.family_name = "main", + .faces = faces, + .fallback_families = {"segoeui", "fallback"}}); + } + + // 4. Monospace font (user-configured) – falls back to main + segoeui + + // fallback + { + std::vector faces; + add_with_system_variants(faces, font_path_monospace, "consola.ttf"); + ui::register_font_family( + nvg, {.family_name = "monospace", + .faces = faces, + .fallback_families = {"main", "segoeui", "fallback"}}); + } } void config::animated_float_conf::apply_to(ui::animated_color &anim, float delay) {