From a4921194fa126ecaad79ca458ffdea937e167e2c Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Mon, 14 Sep 2026 17:09:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AD=97=E4=BD=93=E5=8C=B9=E9=85=8D):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=97=E4=BD=93=E8=AF=AF=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E5=96=84=E5=9B=9E=E9=80=80=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_font_fs.go | 29 +++++++++++++++++++---------- ofdgo_font_loader.go | 8 ++++---- ofdgo_font_name.go | 14 ++++++++------ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/ofdgo_font_fs.go b/ofdgo_font_fs.go index dfaa690..067954f 100644 --- a/ofdgo_font_fs.go +++ b/ofdgo_font_fs.go @@ -56,8 +56,8 @@ func NewFontFS(fonts []FontFile) *FontFS { } sort.Strings(fsys.names) fsys.candidates = fontFileCandidates(fsys.names, path.Base) - for _, file := range fsys.candidates { - fsys.candidates = appendFontFileNames(fsys.candidates, file, fontFileNames(bytes.NewReader(fsys.files[file.name]))) + for i, file := range fsys.candidates { + fsys.candidates = appendFontFileNames(fsys.candidates, i, fontFileNames(bytes.NewReader(fsys.files[file.name]))) } return fsys } @@ -192,15 +192,20 @@ func fontFileMatches(candidates []fontFileCandidate, names []string, bold, itali seen := make(map[string]int, len(candidates)) if len(names) == 0 { for _, name := range fontDefaultSystemNames() { - for _, match := range fontFileMatches(candidates, []string{name}, bold, italic) { - key := strings.ToLower(match.name) - if _, ok := seen[key]; !ok { - seen[key] = len(matches) - matches = append(matches, match) - } + matches = append(matches, fontFileMatches(candidates, []string{name}, bold, italic)...) + } + sort.SliceStable(matches, func(i, j int) bool { + return matches[i].rank < matches[j].rank + }) + unique := matches[:0] + for _, match := range matches { + key := strings.ToLower(match.name) + if _, ok := seen[key]; !ok { + seen[key] = len(unique) + unique = append(unique, match) } } - return matches + return unique } for i, name := range names { if name == "" { @@ -428,11 +433,15 @@ func appendFontFileMatch(matches *[]fontFileMatch, seen map[string]int, matcher if rank == fontMatchNone { return } + suffix := matcher.styleSuffixNormalized(file.normalized) + if rank == fontMatchFuzzy { + suffix = matcher.styleSuffixNormalized(fontNormalizeName(file.base)) + } next := fontFileMatch{ name: file.name, priority: matcher.priority, rank: rank, - styleRank: fontFileStyleRank(matcher.styleSuffixNormalized(file.normalized), bold, italic), + styleRank: fontFileStyleRank(suffix, bold, italic), sortName: file.lowerBase, } if index, ok := seen[file.fold]; ok { diff --git a/ofdgo_font_loader.go b/ofdgo_font_loader.go index e29131a..666a66e 100644 --- a/ofdgo_font_loader.go +++ b/ofdgo_font_loader.go @@ -271,12 +271,12 @@ func (r *Renderer) matchFontFiles(dir string, names []string, bold, italic bool) if !ok { files, _ := filepath.Glob(filepath.Join(dir, "*")) candidates = fontFileCandidates(files, filepath.Base) - for _, candidate := range candidates { + for i, candidate := range candidates { file, err := os.Open(candidate.name) if err != nil { continue } - candidates = appendFontFileNames(candidates, candidate, fontFileNames(file)) + candidates = appendFontFileNames(candidates, i, fontFileNames(file)) file.Close() } r.fontDirCandidates[dir] = candidates @@ -296,7 +296,7 @@ func (r *Renderer) matchFontFS(index int, names []string, bold, italic bool) []f if !ok { files, _ := fs.Glob(fsys, "*") candidates = fontFileCandidates(files, path.Base) - for _, candidate := range candidates { + for i, candidate := range candidates { file, err := fsys.Open(candidate.name) if err != nil { continue @@ -306,7 +306,7 @@ func (r *Renderer) matchFontFS(index int, names []string, bold, italic bool) []f data, _ := io.ReadAll(file) reader = bytes.NewReader(data) } - candidates = appendFontFileNames(candidates, candidate, fontFileNames(reader)) + candidates = appendFontFileNames(candidates, i, fontFileNames(reader)) file.Close() } r.fontFSCandidates[index] = candidates diff --git a/ofdgo_font_name.go b/ofdgo_font_name.go index b8c95c7..c3889c7 100644 --- a/ofdgo_font_name.go +++ b/ofdgo_font_name.go @@ -33,14 +33,16 @@ type fontNameRecord struct { Offset uint16 } -// appendFontFileNames 追加字体内部名称对应的候选 -// 入参: candidates 字体候选, file 字体文件, names 字体内部名称 +// appendFontFileNames 使用字体内部名称匹配,缺失时保留文件名 +// 入参: candidates 字体候选, index 文件候选索引, names 字体内部名称 // 返回: []fontFileCandidate 字体候选列表 -func appendFontFileNames(candidates []fontFileCandidate, file fontFileCandidate, names []string) []fontFileCandidate { - base := file.normalized - for _, name := range names { +func appendFontFileNames(candidates []fontFileCandidate, index int, names []string) []fontFileCandidate { + file := candidates[index] + for i, name := range names { file.normalized = fontNormalizeName(name) - if file.normalized != base { + if i == 0 { + candidates[index] = file + } else { candidates = append(candidates, file) } }