mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
The previous bitmap cache was a three-level rbtree keyed by (wchar_t, font_id, size), which had two problems: 1. The wchar_t key collapses Unicode codepoints that map to the same glyph in a given face into separate cache slots, missing real reuse opportunities and bloating memory under text-heavy workloads. 2. There was no upper bound; long-running apps would grow the cache without limit until pd_font_library_destroy. Replace it with a flat hash table keyed by a packed 64-bit (face_id, glyph_index, size) tuple, plus a doubly-linked LRU list for O(1) eviction. - pd_font_engine gets a new get_glyph_index hook; FreeType maps via FT_Get_Char_Index, the in-core engine treats codepoints as identity (no cmap). - pd_font_cache_add_bitmap / pd_font_cache_get_bitmap now resolve the glyph index via the engine before keying the cache, so the same glyph reused across composed sequences hits the same entry. - pd_font_cache_set_capacity / pd_font_cache_count expose the LRU bound (default 8192 entries, 0 = unlimited) for runtime tuning and tests. Shrinking the capacity evicts immediately. - list_t is used with append at tail / evict from head so the yutil tail.prev pointer stays consistent (list_link does not update it). Adds lib/pandagl/tests/test_font_cache.c covering hit identity, size differentiation, unknown-codepoint fallback, and LRU eviction.
96 lines
3.5 KiB
C
96 lines
3.5 KiB
C
/*
|
||
* lib/pandagl/tests/test_font_cache.c
|
||
*
|
||
* Copyright (c) 2026, Liu Chao <i@lc-soft.io> All rights reserved.
|
||
*
|
||
* SPDX-License-Identifier: MIT
|
||
*
|
||
* This file is part of LCUI, distributed under the MIT License found in the
|
||
* LICENSE.TXT file in the root directory of this source tree.
|
||
*/
|
||
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <pandagl.h>
|
||
#include <ctest.h>
|
||
|
||
/*
|
||
* 字形位图缓存的功能测试:
|
||
* - 同一 (face, ch, size) 第二次请求应命中缓存,返回相同指针;
|
||
* - 不同 size 应得到不同 entry;
|
||
* - 渲染失败的字符回退到 .notdef,不应崩溃。
|
||
*/
|
||
|
||
static void should_return_same_pointer_on_cache_hit(void)
|
||
{
|
||
const pd_glyph_bitmap_t *bmp1 = NULL;
|
||
const pd_glyph_bitmap_t *bmp2 = NULL;
|
||
|
||
ctest_equal_int("first lookup should succeed",
|
||
pd_font_cache_get_bitmap('A', -1, 14, &bmp1), 0);
|
||
ctest_equal_int("second lookup should succeed",
|
||
pd_font_cache_get_bitmap('A', -1, 14, &bmp2), 0);
|
||
ctest_equal_bool("cached bitmaps should share the same address",
|
||
bmp1 == bmp2, true);
|
||
}
|
||
|
||
static void should_treat_different_sizes_as_different_entries(void)
|
||
{
|
||
const pd_glyph_bitmap_t *bmp1 = NULL;
|
||
const pd_glyph_bitmap_t *bmp2 = NULL;
|
||
|
||
pd_font_cache_get_bitmap('B', -1, 12, &bmp1);
|
||
pd_font_cache_get_bitmap('B', -1, 24, &bmp2);
|
||
ctest_equal_bool("different sizes should yield different entries",
|
||
bmp1 != NULL && bmp2 != NULL && bmp1 != bmp2, true);
|
||
}
|
||
|
||
static void should_not_crash_on_unknown_codepoint(void)
|
||
{
|
||
const pd_glyph_bitmap_t *bmp = NULL;
|
||
/* 超出 BMP 的字符大概率没有 glyph,应回退而非崩溃 */
|
||
int ret = pd_font_cache_get_bitmap(0x10FFFD, -1, 14, &bmp);
|
||
ctest_equal_bool("should return non-positive code", ret <= 0, true);
|
||
}
|
||
|
||
static void should_evict_least_recently_used(void)
|
||
{
|
||
const pd_glyph_bitmap_t *bmp = NULL;
|
||
size_t before;
|
||
|
||
/* 先用很大上限,预热若干 entry */
|
||
pd_font_cache_set_capacity(1024);
|
||
pd_font_cache_get_bitmap('X', -1, 10, &bmp);
|
||
pd_font_cache_get_bitmap('Y', -1, 10, &bmp);
|
||
pd_font_cache_get_bitmap('Z', -1, 10, &bmp);
|
||
before = pd_font_cache_count();
|
||
ctest_equal_bool("should hold at least 3 entries", before >= 3, true);
|
||
|
||
/* 把上限缩到 2,应立即驱逐 */
|
||
pd_font_cache_set_capacity(2);
|
||
ctest_equal_uint("count should drop to capacity",
|
||
(unsigned)pd_font_cache_count(), 2u);
|
||
|
||
/* 添加新 entry 时仍不超过上限 */
|
||
pd_font_cache_get_bitmap('W', -1, 10, &bmp);
|
||
ctest_equal_uint("count should stay at capacity after new insert",
|
||
(unsigned)pd_font_cache_count(), 2u);
|
||
|
||
/* 恢复无限制 */
|
||
pd_font_cache_set_capacity(0);
|
||
}
|
||
|
||
void test_pandagl_font_cache(void)
|
||
{
|
||
pd_font_library_init();
|
||
ctest_describe("glyph cache hit returns same pointer",
|
||
should_return_same_pointer_on_cache_hit);
|
||
ctest_describe("glyph cache differentiates by size",
|
||
should_treat_different_sizes_as_different_entries);
|
||
ctest_describe("glyph cache handles unknown codepoint",
|
||
should_not_crash_on_unknown_codepoint);
|
||
ctest_describe("glyph cache lru eviction",
|
||
should_evict_least_recently_used);
|
||
pd_font_library_destroy();
|
||
}
|