From 21bd592358c575fd7caf266d6e808a114b39fc88 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Wed, 14 Oct 2026 01:05:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AD=97=E4=BD=93=E8=83=BD=E5=8A=9B):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=9B=86=E5=90=88=E5=AD=97=E4=BD=93=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_font_source.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/ofdgo_font_source.go b/ofdgo_font_source.go index cf53f7b..d2dd8e5 100644 --- a/ofdgo_font_source.go +++ b/ofdgo_font_source.go @@ -61,12 +61,13 @@ type fontSourceKey struct { name string } -// fontSourceCache 保存字体文件候选与原始数据,不包含绘图库对象 +// fontSourceCache 保存字体候选及独立字体数据,原始集合按字节预算淘汰,不包含绘图库对象 type fontSourceCache struct { candidates map[string][]fontSource directories map[string][]fontFileCandidate filesystems map[int][]fontFileCandidate data map[fontSourceKey]fontSourceData + collections *renderCache[fontSourceKey, []byte] fallback ResolvedFont fallbackRead bool } @@ -80,7 +81,7 @@ type fontSourceData struct { // newFontSourceCache 创建独立字体来源缓存 // 返回: *fontSourceCache 字体来源缓存 func newFontSourceCache() *fontSourceCache { - return &fontSourceCache{candidates: make(map[string][]fontSource), directories: make(map[string][]fontFileCandidate), filesystems: make(map[int][]fontFileCandidate), data: make(map[fontSourceKey]fontSourceData)} + return &fontSourceCache{candidates: make(map[string][]fontSource), directories: make(map[string][]fontFileCandidate), filesystems: make(map[int][]fontFileCandidate), data: make(map[fontSourceKey]fontSourceData), collections: &renderCache[fontSourceKey, []byte]{limit: 32 << 20}} } // child 复用相同来源的文件索引与数据,匹配和回退状态保持独立 @@ -88,6 +89,7 @@ func newFontSourceCache() *fontSourceCache { func (c *fontSourceCache) child() *fontSourceCache { cache := newFontSourceCache() cache.directories, cache.filesystems, cache.data = c.directories, c.filesystems, c.data + cache.collections = c.collections return cache } @@ -106,14 +108,21 @@ func (r *Renderer) readFontSource(source fontSource, definition *Font) ([]byte, if cached, ok := r.fontSourcesCache.data[key]; ok { return cached.data, cached.err } - var data []byte + fileKey := key + fileKey.face = 0 + data, cached := r.fontSourcesCache.collections.get(fileKey) var err error - if key.kind == fontSourceFS { - data, err = fs.ReadFile(r.fontFS[key.index], key.name) - } else { - data, err = os.ReadFile(key.name) + if !cached { + if key.kind == fontSourceFS { + data, err = fs.ReadFile(r.fontFS[key.index], key.name) + } else { + data, err = os.ReadFile(key.name) + } } if err == nil && bytes.HasPrefix(data, []byte("ttcf")) { + if !cached { + r.fontSourcesCache.collections.put(fileKey, data, min(cap(data), r.fontSourcesCache.collections.limit)+256) + } data, err = basefont.ExtractCollectionFont(data, key.face) } if err == nil {