mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 09:37:26 +08:00
This commit is contained in:
1 parent
c2ddde7c0e
commit
a4921194fa
3 files changed
+29
-18
No files matched your search
+17
-8
@@ -56,8 +56,8 @@ func NewFontFS(fonts []FontFile) *FontFS {
|
|||||||
}
|
}
|
||||||
sort.Strings(fsys.names)
|
sort.Strings(fsys.names)
|
||||||
fsys.candidates = fontFileCandidates(fsys.names, path.Base)
|
fsys.candidates = fontFileCandidates(fsys.names, path.Base)
|
||||||
for _, file := range fsys.candidates {
|
for i, file := range fsys.candidates {
|
||||||
fsys.candidates = appendFontFileNames(fsys.candidates, file, fontFileNames(bytes.NewReader(fsys.files[file.name])))
|
fsys.candidates = appendFontFileNames(fsys.candidates, i, fontFileNames(bytes.NewReader(fsys.files[file.name])))
|
||||||
}
|
}
|
||||||
return fsys
|
return fsys
|
||||||
}
|
}
|
||||||
@@ -192,15 +192,20 @@ func fontFileMatches(candidates []fontFileCandidate, names []string, bold, itali
|
|||||||
seen := make(map[string]int, len(candidates))
|
seen := make(map[string]int, len(candidates))
|
||||||
if len(names) == 0 {
|
if len(names) == 0 {
|
||||||
for _, name := range fontDefaultSystemNames() {
|
for _, name := range fontDefaultSystemNames() {
|
||||||
for _, match := range fontFileMatches(candidates, []string{name}, bold, italic) {
|
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)
|
key := strings.ToLower(match.name)
|
||||||
if _, ok := seen[key]; !ok {
|
if _, ok := seen[key]; !ok {
|
||||||
seen[key] = len(matches)
|
seen[key] = len(unique)
|
||||||
matches = append(matches, match)
|
unique = append(unique, match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return unique
|
||||||
return matches
|
|
||||||
}
|
}
|
||||||
for i, name := range names {
|
for i, name := range names {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
@@ -428,11 +433,15 @@ func appendFontFileMatch(matches *[]fontFileMatch, seen map[string]int, matcher
|
|||||||
if rank == fontMatchNone {
|
if rank == fontMatchNone {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
suffix := matcher.styleSuffixNormalized(file.normalized)
|
||||||
|
if rank == fontMatchFuzzy {
|
||||||
|
suffix = matcher.styleSuffixNormalized(fontNormalizeName(file.base))
|
||||||
|
}
|
||||||
next := fontFileMatch{
|
next := fontFileMatch{
|
||||||
name: file.name,
|
name: file.name,
|
||||||
priority: matcher.priority,
|
priority: matcher.priority,
|
||||||
rank: rank,
|
rank: rank,
|
||||||
styleRank: fontFileStyleRank(matcher.styleSuffixNormalized(file.normalized), bold, italic),
|
styleRank: fontFileStyleRank(suffix, bold, italic),
|
||||||
sortName: file.lowerBase,
|
sortName: file.lowerBase,
|
||||||
}
|
}
|
||||||
if index, ok := seen[file.fold]; ok {
|
if index, ok := seen[file.fold]; ok {
|
||||||
|
|||||||
@@ -271,12 +271,12 @@ func (r *Renderer) matchFontFiles(dir string, names []string, bold, italic bool)
|
|||||||
if !ok {
|
if !ok {
|
||||||
files, _ := filepath.Glob(filepath.Join(dir, "*"))
|
files, _ := filepath.Glob(filepath.Join(dir, "*"))
|
||||||
candidates = fontFileCandidates(files, filepath.Base)
|
candidates = fontFileCandidates(files, filepath.Base)
|
||||||
for _, candidate := range candidates {
|
for i, candidate := range candidates {
|
||||||
file, err := os.Open(candidate.name)
|
file, err := os.Open(candidate.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
candidates = appendFontFileNames(candidates, candidate, fontFileNames(file))
|
candidates = appendFontFileNames(candidates, i, fontFileNames(file))
|
||||||
file.Close()
|
file.Close()
|
||||||
}
|
}
|
||||||
r.fontDirCandidates[dir] = candidates
|
r.fontDirCandidates[dir] = candidates
|
||||||
@@ -296,7 +296,7 @@ func (r *Renderer) matchFontFS(index int, names []string, bold, italic bool) []f
|
|||||||
if !ok {
|
if !ok {
|
||||||
files, _ := fs.Glob(fsys, "*")
|
files, _ := fs.Glob(fsys, "*")
|
||||||
candidates = fontFileCandidates(files, path.Base)
|
candidates = fontFileCandidates(files, path.Base)
|
||||||
for _, candidate := range candidates {
|
for i, candidate := range candidates {
|
||||||
file, err := fsys.Open(candidate.name)
|
file, err := fsys.Open(candidate.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -306,7 +306,7 @@ func (r *Renderer) matchFontFS(index int, names []string, bold, italic bool) []f
|
|||||||
data, _ := io.ReadAll(file)
|
data, _ := io.ReadAll(file)
|
||||||
reader = bytes.NewReader(data)
|
reader = bytes.NewReader(data)
|
||||||
}
|
}
|
||||||
candidates = appendFontFileNames(candidates, candidate, fontFileNames(reader))
|
candidates = appendFontFileNames(candidates, i, fontFileNames(reader))
|
||||||
file.Close()
|
file.Close()
|
||||||
}
|
}
|
||||||
r.fontFSCandidates[index] = candidates
|
r.fontFSCandidates[index] = candidates
|
||||||
|
|||||||
+8
-6
@@ -33,14 +33,16 @@ type fontNameRecord struct {
|
|||||||
Offset uint16
|
Offset uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendFontFileNames 追加字体内部名称对应的候选
|
// appendFontFileNames 使用字体内部名称匹配,缺失时保留文件名
|
||||||
// 入参: candidates 字体候选, file 字体文件, names 字体内部名称
|
// 入参: candidates 字体候选, index 文件候选索引, names 字体内部名称
|
||||||
// 返回: []fontFileCandidate 字体候选列表
|
// 返回: []fontFileCandidate 字体候选列表
|
||||||
func appendFontFileNames(candidates []fontFileCandidate, file fontFileCandidate, names []string) []fontFileCandidate {
|
func appendFontFileNames(candidates []fontFileCandidate, index int, names []string) []fontFileCandidate {
|
||||||
base := file.normalized
|
file := candidates[index]
|
||||||
for _, name := range names {
|
for i, name := range names {
|
||||||
file.normalized = fontNormalizeName(name)
|
file.normalized = fontNormalizeName(name)
|
||||||
if file.normalized != base {
|
if i == 0 {
|
||||||
|
candidates[index] = file
|
||||||
|
} else {
|
||||||
candidates = append(candidates, file)
|
candidates = append(candidates, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user