mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 01:27:26 +08:00
This commit is contained in:
1 parent
c2ddde7c0e
commit
a4921194fa
3 files changed
+31
-20
No files matched your search
+19
-10
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
+8
-6
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user