mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
feat(pandagl): add quiet font loading for optional fonts
pd_font_try_load_file() reports failures at debug level so platform probes, test candidates and CSS @font-face no longer spam errors for missing system fonts. pd_font_load_file() and XML font resources still report errors, and initialization warns once when no usable default font exists.
This commit is contained in:
@@ -195,6 +195,12 @@ PD_PUBLIC size_t pd_font_cache_count(void);
|
||||
/** 载入字体至数据库中 */
|
||||
PD_PUBLIC int pd_font_load_file(const char *filepath);
|
||||
|
||||
/**
|
||||
* 载入字体至数据库中,失败时不记录错误日志
|
||||
* 适用于探测平台候选字体,避免逐个报告不存在的文件
|
||||
*/
|
||||
PD_PUBLIC int pd_font_try_load_file(const char *filepath);
|
||||
|
||||
/** 初始化字体处理模块 */
|
||||
PD_PUBLIC void pd_font_library_init(void);
|
||||
|
||||
|
||||
@@ -750,7 +750,7 @@ void pd_font_set_default(int id)
|
||||
}
|
||||
}
|
||||
|
||||
int pd_font_load_file(const char *filepath)
|
||||
static int pd_font_load_file_impl(const char *filepath, bool quiet)
|
||||
{
|
||||
pd_font_face_t **fonts;
|
||||
int i, num_fonts, id;
|
||||
@@ -761,7 +761,13 @@ int pd_font_load_file(const char *filepath)
|
||||
}
|
||||
num_fonts = fontlib.engine->open(filepath, &fonts);
|
||||
if (num_fonts < 1) {
|
||||
logger_error("[font] failed to load file: %s\n", filepath);
|
||||
if (quiet) {
|
||||
logger_debug("[font] skip unavailable file: %s\n",
|
||||
filepath);
|
||||
} else {
|
||||
logger_error("[font] failed to load file: %s\n",
|
||||
filepath);
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
for (i = 0; i < num_fonts; ++i) {
|
||||
@@ -776,6 +782,16 @@ int pd_font_load_file(const char *filepath)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pd_font_load_file(const char *filepath)
|
||||
{
|
||||
return pd_font_load_file_impl(filepath, false);
|
||||
}
|
||||
|
||||
int pd_font_try_load_file(const char *filepath)
|
||||
{
|
||||
return pd_font_load_file_impl(filepath, true);
|
||||
}
|
||||
|
||||
int pd_font_render_glyph(pd_glyph_bitmap_t *buff, unsigned ch, int font_id,
|
||||
int pixel_size)
|
||||
{
|
||||
|
||||
@@ -21,25 +21,33 @@ static void test_segoe_ui_font_load(void)
|
||||
{
|
||||
pd_font_face_t *font;
|
||||
int id;
|
||||
if (pd_font_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
return;
|
||||
}
|
||||
ctest_equal_int("should load segoeui.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeui.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeui.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load segoeuib.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeuib.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuib.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load segoeuii.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeuii.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuii.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load segoeuil.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeuil.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuil.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load segoeuisl.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeuisl.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuisl.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load segoeuiz.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/segoeuiz.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuiz.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load seguibl.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/seguibl.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/seguibl.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load seguili.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/seguili.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/seguili.ttf"),
|
||||
0);
|
||||
id = GetSegoeUIFont(PD_FONT_STYLE_NORMAL, PD_FONT_WEIGHT_NORMAL);
|
||||
ctest_equal_bool("should resolve font (style: normal, weight: normal)",
|
||||
id > 0, true);
|
||||
@@ -97,17 +105,21 @@ static void test_arial_font_load(void)
|
||||
{
|
||||
int id;
|
||||
pd_font_face_t *font;
|
||||
if (pd_font_load_file("C:/windows/fonts/arial.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/arial.ttf") != 0) {
|
||||
return;
|
||||
}
|
||||
ctest_equal_int("should load arialbd.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/arialbd.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/arialbd.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load ariblk.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/ariblk.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/ariblk.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load arialbi.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/arialbi.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/arialbi.ttf"),
|
||||
0);
|
||||
ctest_equal_int("should load ariali.ttf",
|
||||
pd_font_load_file("C:/windows/fonts/ariali.ttf"), 0);
|
||||
pd_font_try_load_file("C:/windows/fonts/ariali.ttf"),
|
||||
0);
|
||||
id = GetArialFont(PD_FONT_STYLE_NORMAL, PD_FONT_WEIGHT_NORMAL);
|
||||
ctest_equal_bool("should resolve font (style: normal, weight: normal)",
|
||||
id > 0, true);
|
||||
@@ -168,7 +180,7 @@ static void test_variable_font_weights(void)
|
||||
size_t c;
|
||||
int normal_id, medium_id, bold_id;
|
||||
|
||||
if (pd_font_load_file("C:/windows/fonts/NotoSansSC-VF.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/NotoSansSC-VF.ttf") != 0) {
|
||||
return;
|
||||
}
|
||||
for (c = 0; c < sizeof(candidates) / sizeof(candidates[0]); ++c) {
|
||||
@@ -204,10 +216,10 @@ static void test_cjk_font_fallback_glyph(void)
|
||||
unsigned cjk = 0x4E2D; /* 中 */
|
||||
int latin_id, cjk_id;
|
||||
|
||||
if (pd_font_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
return;
|
||||
}
|
||||
if (pd_font_load_file("C:/windows/fonts/msyh.ttc") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/msyh.ttc") != 0) {
|
||||
return;
|
||||
}
|
||||
latin_id = pd_font_get_id("Segoe UI", PD_FONT_STYLE_NORMAL,
|
||||
@@ -227,16 +239,16 @@ static void test_weight_fallback_safety(void)
|
||||
{
|
||||
int id;
|
||||
|
||||
if (pd_font_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/windows/fonts/segoeui.ttf") != 0) {
|
||||
return;
|
||||
}
|
||||
/* Load a sparse set of weights: Regular, Light, Semilight, Bold, Black.
|
||||
* Requests for the missing medium/semibold/extra-bold tiers must fall
|
||||
* back to a neighbour instead of dereferencing a NULL slot. */
|
||||
pd_font_load_file("C:/windows/fonts/segoeuil.ttf");
|
||||
pd_font_load_file("C:/windows/fonts/segoeuisl.ttf");
|
||||
pd_font_load_file("C:/windows/fonts/segoeuib.ttf");
|
||||
pd_font_load_file("C:/windows/fonts/seguibl.ttf");
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuil.ttf");
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuisl.ttf");
|
||||
pd_font_try_load_file("C:/windows/fonts/segoeuib.ttf");
|
||||
pd_font_try_load_file("C:/windows/fonts/seguibl.ttf");
|
||||
id = pd_font_get_id("Segoe UI", PD_FONT_STYLE_NORMAL,
|
||||
PD_FONT_WEIGHT_SEMI_BOLD);
|
||||
ctest_equal_bool("semibold request should fall back to a face", id > 0,
|
||||
@@ -256,7 +268,7 @@ void test_pandagl_font_load(void)
|
||||
{
|
||||
pd_font_library_init();
|
||||
ctest_equal_int("should load font from file",
|
||||
pd_font_load_file("test_font_load.ttf"), 0);
|
||||
pd_font_try_load_file("test_font_load.ttf"), 0);
|
||||
#ifdef _WIN32
|
||||
ctest_describe("segoe ui font load", test_segoe_ui_font_load);
|
||||
ctest_describe("arial font load", test_arial_font_load);
|
||||
|
||||
@@ -78,10 +78,10 @@ static int scan_char_bounds(const pd_canvas_t *canvas, int x_left, int x_right,
|
||||
|
||||
static int try_load_dual_font(void)
|
||||
{
|
||||
if (pd_font_load_file("C:/Windows/Fonts/consola.ttf") != 0) {
|
||||
if (pd_font_try_load_file("C:/Windows/Fonts/consola.ttf") != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (pd_font_load_file("C:/Windows/Fonts/msyh.ttc") != 0) {
|
||||
if (pd_font_try_load_file("C:/Windows/Fonts/msyh.ttc") != 0) {
|
||||
return -1;
|
||||
}
|
||||
g_consolas_id = pd_font_get_id("Consolas", 0, 0);
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ static void ui_on_parsed_font_face(const css_font_face_t *face)
|
||||
ui_event_t e;
|
||||
|
||||
ui_event_init(&e, "css_font_face_load");
|
||||
pd_font_load_file(face->src);
|
||||
pd_font_try_load_file(face->src);
|
||||
ui_post_event(&e, (css_font_face_t *)face, NULL);
|
||||
}
|
||||
|
||||
|
||||
+14
-8
@@ -48,9 +48,11 @@ static void lcui_windows_fonts_init(void)
|
||||
"C:/Windows/Fonts/NotoSansSC-VF.ttf" };
|
||||
|
||||
for (i = 0; i < sizeof(fonts) / sizeof(char *); ++i) {
|
||||
pd_font_load_file(fonts[i]);
|
||||
pd_font_try_load_file(fonts[i]);
|
||||
}
|
||||
if (!lcui_fonts_set_default("Microsoft YaHei")) {
|
||||
logger_warning("[font] no usable default font\n");
|
||||
}
|
||||
lcui_fonts_set_default("Microsoft YaHei");
|
||||
pd_font_family_set_alias("serif", "Simsun");
|
||||
pd_font_family_set_alias("sans-serif", "Microsoft YaHei");
|
||||
pd_font_family_set_alias("monospace", "Consolas");
|
||||
@@ -77,7 +79,7 @@ static void lcui_fc_fonts_init(void)
|
||||
for (i = 0; i < sizeof(sans) / sizeof(sans[0]); ++i) {
|
||||
path = pd_font_find_path(sans[i]);
|
||||
if (path) {
|
||||
pd_font_load_file(path);
|
||||
pd_font_try_load_file(path);
|
||||
free(path);
|
||||
}
|
||||
if (!has_default) {
|
||||
@@ -96,7 +98,7 @@ static void lcui_fc_fonts_init(void)
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
pd_font_load_file(path);
|
||||
pd_font_try_load_file(path);
|
||||
free(path);
|
||||
pd_font_family_set_alias("monospace", mono[i]);
|
||||
break;
|
||||
@@ -106,11 +108,14 @@ static void lcui_fc_fonts_init(void)
|
||||
if (!path) {
|
||||
continue;
|
||||
}
|
||||
pd_font_load_file(path);
|
||||
pd_font_try_load_file(path);
|
||||
free(path);
|
||||
pd_font_family_set_alias("serif", serif[i]);
|
||||
break;
|
||||
}
|
||||
if (!has_default) {
|
||||
logger_warning("[font] no usable default font\n");
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -148,11 +153,12 @@ static void lcui_linux_fonts_init(void)
|
||||
};
|
||||
|
||||
for (i = 0; i < sizeof(fonts) / sizeof(char *); ++i) {
|
||||
pd_font_load_file(fonts[i]);
|
||||
pd_font_try_load_file(fonts[i]);
|
||||
}
|
||||
// TODO: 使用系统已设置的默认字体
|
||||
if (!lcui_fonts_set_default("Noto Sans CJK SC")) {
|
||||
lcui_fonts_set_default("Ubuntu");
|
||||
if (!lcui_fonts_set_default("Noto Sans CJK SC") &&
|
||||
!lcui_fonts_set_default("Ubuntu")) {
|
||||
logger_warning("[font] no usable default font\n");
|
||||
}
|
||||
pd_font_family_set_alias("sans-serif", "Ubuntu");
|
||||
pd_font_family_set_alias("serif", "DejaVu Serif");
|
||||
|
||||
Reference in New Issue
Block a user