From f4291aaa74935486b9ff56e99ae48c26dfa63012 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 9 Jul 2026 17:12:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AD=97=E4=BD=93=E5=8A=A0=E8=BD=BD):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E4=BD=93=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_font_fs.go | 36 ++++++++++++++++++++---------------- ofdgo_font_info.go | 2 +- ofdgo_font_loader.go | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/ofdgo_font_fs.go b/ofdgo_font_fs.go index fbcd8c8..ee15f11 100644 --- a/ofdgo_font_fs.go +++ b/ofdgo_font_fs.go @@ -155,7 +155,7 @@ func (fsys *FontFS) matchPatternsStyle(patterns []string, bold, italic bool) []s appendFontFileMatch(&matches, seen, matcher.pattern, file.name, rank, bold, italic) } } - sortFontFileMatches(matches, bold, italic) + sortFontFileMatches(matches) return fontFileMatchNames(matches) } @@ -329,9 +329,10 @@ func (m fontPatternMatcher) styleSuffix(name string) string { } type fontFileMatch struct { - name string - pattern string - rank int + name string + rank int + styleRank int + sortName string } // appendFontFileMatch 追加字体文件匹配结果 @@ -341,9 +342,14 @@ func appendFontFileMatch(matches *[]fontFileMatch, seen map[string]int, pattern, return } key := strings.ToLower(name) - next := fontFileMatch{name: name, pattern: pattern, rank: rank} + next := fontFileMatch{ + name: name, + rank: rank, + styleRank: fontFileStyleRank(pattern, name, bold, italic), + sortName: strings.ToLower(path.Base(name)), + } if index, ok := seen[key]; ok { - if fontFileMatchLess(next, (*matches)[index], bold, italic) { + if fontFileMatchLess(next, (*matches)[index]) { (*matches)[index] = next } return @@ -353,26 +359,24 @@ func appendFontFileMatch(matches *[]fontFileMatch, seen map[string]int, pattern, } // sortFontFileMatches 排序字体文件匹配结果 -// 入参: matches 匹配结果, bold 是否粗体, italic 是否斜体 -func sortFontFileMatches(matches []fontFileMatch, bold, italic bool) { +// 入参: matches 匹配结果 +func sortFontFileMatches(matches []fontFileMatch) { sort.SliceStable(matches, func(i, j int) bool { - return fontFileMatchLess(matches[i], matches[j], bold, italic) + return fontFileMatchLess(matches[i], matches[j]) }) } // fontFileMatchLess 判断字体文件匹配结果优先级 -// 入参: left 左侧匹配结果, right 右侧匹配结果, bold 是否粗体, italic 是否斜体 +// 入参: left 左侧匹配结果, right 右侧匹配结果 // 返回: bool 左侧是否优先 -func fontFileMatchLess(left, right fontFileMatch, bold, italic bool) bool { +func fontFileMatchLess(left, right fontFileMatch) bool { if left.rank != right.rank { return left.rank < right.rank } - leftStyle := fontFileStyleRank(left.pattern, left.name, bold, italic) - rightStyle := fontFileStyleRank(right.pattern, right.name, bold, italic) - if leftStyle != rightStyle { - return leftStyle < rightStyle + if left.styleRank != right.styleRank { + return left.styleRank < right.styleRank } - return strings.ToLower(path.Base(left.name)) < strings.ToLower(path.Base(right.name)) + return left.sortName < right.sortName } // fontFileMatchNames 获取字体文件匹配名称 diff --git a/ofdgo_font_info.go b/ofdgo_font_info.go index d39ce8e..f20c31b 100644 --- a/ofdgo_font_info.go +++ b/ofdgo_font_info.go @@ -235,7 +235,7 @@ func fontFSMatchesStyle(fsys fs.FS, patterns []string, bold, italic bool) []stri appendFontFileMatch(&matches, seen, matcher.pattern, file.name, rank, bold, italic) } } - sortFontFileMatches(matches, bold, italic) + sortFontFileMatches(matches) return fontFileMatchNames(matches) } diff --git a/ofdgo_font_loader.go b/ofdgo_font_loader.go index 56d254e..1fd06ed 100644 --- a/ofdgo_font_loader.go +++ b/ofdgo_font_loader.go @@ -169,7 +169,7 @@ func (r *Renderer) matchFontFiles(dir string, patterns []string, bold, italic bo appendFontFileMatch(&matches, index, matcher.pattern, file.name, rank, bold, italic) } } - sortFontFileMatches(matches, bold, italic) + sortFontFileMatches(matches) return fontFileMatchNames(matches) }