mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 16:44:23 +08:00
This commit is contained in:
1 parent
c15547f0d2
commit
f44d0d8a60
30 files changed
+2075
-463
No files matched your search
@@ -14,51 +14,7 @@
|
||||
|
||||
package ofdgo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
|
||||
"github.com/tdewolff/canvas"
|
||||
)
|
||||
|
||||
// fontSourceKind 字体来源类型
|
||||
type fontSourceKind uint8
|
||||
|
||||
const (
|
||||
fontSourceFile fontSourceKind = iota
|
||||
fontSourceFS
|
||||
fontSourceSystem
|
||||
)
|
||||
|
||||
// fontSource 字体来源
|
||||
type fontSource struct {
|
||||
kind fontSourceKind
|
||||
index int
|
||||
face int
|
||||
name string
|
||||
exact bool
|
||||
priority int
|
||||
}
|
||||
|
||||
// fontSourceKey 字体来源去重键
|
||||
type fontSourceKey struct {
|
||||
kind fontSourceKind
|
||||
index int
|
||||
face int
|
||||
name string
|
||||
}
|
||||
|
||||
// fontCacheKey 字体加载缓存键
|
||||
type fontCacheKey struct {
|
||||
fontSourceKey
|
||||
style canvas.FontStyle
|
||||
}
|
||||
import "github.com/tdewolff/canvas"
|
||||
|
||||
// loadFont 加载字体
|
||||
// 入参: fontID 字体ID
|
||||
@@ -86,9 +42,7 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
|
||||
r.renderError = err
|
||||
return nil
|
||||
}
|
||||
if r.Reader.fontCache[fontID] != nil {
|
||||
r.canvasState().fontMap[fontID] = ff
|
||||
}
|
||||
r.canvasState().fontMap[fontID] = ff
|
||||
return ff
|
||||
}
|
||||
|
||||
@@ -105,211 +59,3 @@ func canvasFontStyle(font *Font) canvas.FontStyle {
|
||||
}
|
||||
return style
|
||||
}
|
||||
|
||||
// fontSources 获取字体来源列表
|
||||
// 入参: fontID 字体ID, font OFD字体定义, style Canvas字体样式
|
||||
// 返回: []fontSource 字体来源列表
|
||||
func (r *Renderer) fontSources(fontID string, font *Font, style canvas.FontStyle) []fontSource {
|
||||
if sources, ok := r.canvasState().fontSourceCache[fontID]; ok {
|
||||
return sources
|
||||
}
|
||||
bold := style&canvas.FontBold != 0
|
||||
italic := style&canvas.FontItalic != 0
|
||||
names := []string{font.FontName, font.FamilyName}
|
||||
sources := make([]fontSource, 0)
|
||||
seen := make(map[fontSourceKey]bool)
|
||||
for _, dir := range r.fontDirs {
|
||||
for _, match := range r.matchFontFiles(dir, names, bold, italic) {
|
||||
sources = appendFontSource(sources, seen, fontSource{kind: fontSourceFile, name: match.name, face: match.face, exact: true, priority: min(match.priority, 0)})
|
||||
}
|
||||
}
|
||||
for index := range r.fontFS {
|
||||
for _, match := range r.matchFontFS(index, names, bold, italic) {
|
||||
sources = appendFontSource(sources, seen, fontSource{kind: fontSourceFS, index: index, name: match.name, face: match.face, exact: true, priority: min(match.priority, 0)})
|
||||
}
|
||||
}
|
||||
sortFontSources(sources)
|
||||
systemStart := len(sources)
|
||||
if canLoadSystemFonts() {
|
||||
for _, dir := range systemFontDirs() {
|
||||
for _, match := range r.matchFontFiles(dir, names, bold, italic) {
|
||||
sources = appendFontSource(sources, seen, fontSource{kind: fontSourceFile, name: match.name, face: match.face, exact: true, priority: min(match.priority, 0)})
|
||||
}
|
||||
}
|
||||
for _, name := range fontSystemNames(names...) {
|
||||
source := fontSource{kind: fontSourceSystem, name: name, exact: true}
|
||||
for i, requested := range names {
|
||||
if fontNormalizeName(name) == fontNormalizeName(requested) {
|
||||
source.priority = (i - len(names)) * 2
|
||||
break
|
||||
}
|
||||
}
|
||||
sources = appendFontSource(sources, seen, source)
|
||||
}
|
||||
}
|
||||
sortFontSources(sources[systemStart:])
|
||||
for index := range r.fontFS {
|
||||
for _, match := range r.matchFontFS(index, nil, bold, italic) {
|
||||
sources = appendFontSource(sources, seen, fontSource{kind: fontSourceFS, index: index, name: match.name, face: match.face})
|
||||
}
|
||||
}
|
||||
for index, fsys := range r.fontFS {
|
||||
names, _ := fs.Glob(fsys, "*")
|
||||
for _, name := range names {
|
||||
sources = appendFontSource(sources, seen, fontSource{kind: fontSourceFS, index: index, name: name})
|
||||
}
|
||||
}
|
||||
r.canvasState().fontSourceCache[fontID] = sources
|
||||
return sources
|
||||
}
|
||||
|
||||
// sortFontSources 将完整名称匹配排在回退候选之前
|
||||
// 入参: sources 字体来源列表
|
||||
func sortFontSources(sources []fontSource) {
|
||||
sort.SliceStable(sources, func(i, j int) bool {
|
||||
return sources[i].priority < sources[j].priority
|
||||
})
|
||||
}
|
||||
|
||||
// appendFontSource 追加字体来源
|
||||
// 入参: sources 字体来源列表, seen 去重映射, source 字体来源
|
||||
// 返回: []fontSource 字体来源列表
|
||||
func appendFontSource(sources []fontSource, seen map[fontSourceKey]bool, source fontSource) []fontSource {
|
||||
if source.name == "" {
|
||||
return sources
|
||||
}
|
||||
key := fontSourceKey{kind: source.kind, index: source.index, name: source.name, face: source.face}
|
||||
if seen[key] {
|
||||
return sources
|
||||
}
|
||||
seen[key] = true
|
||||
return append(sources, source)
|
||||
}
|
||||
|
||||
// fontSourceMatch 获取匹配的字体来源
|
||||
// 入参: fontID 字体ID, font OFD字体定义, exact 是否禁止无关回退
|
||||
// 返回: fontSource 字体来源, *canvas.FontFamily 已加载的字体族,未匹配时为空
|
||||
func (r *Renderer) fontSourceMatch(fontID string, font *Font, exact bool) (fontSource, *canvas.FontFamily) {
|
||||
if source, ok := r.canvasState().fontSourceUsed[fontID]; ok && (!exact || source.exact) && r.canvasState().fontMap[fontID] != nil {
|
||||
return source, r.canvasState().fontMap[fontID]
|
||||
}
|
||||
style := canvasFontStyle(font)
|
||||
for _, source := range r.fontSources(fontID, font, style) {
|
||||
if exact && !source.exact {
|
||||
continue
|
||||
}
|
||||
key := fontCacheKey{fontSourceKey: fontSourceKey{kind: source.kind, index: source.index, name: source.name, face: source.face}, style: style}
|
||||
if cached, ok := r.canvasState().fontCache[key]; ok {
|
||||
if cached != nil {
|
||||
return source, cached
|
||||
}
|
||||
continue
|
||||
}
|
||||
family := canvas.NewFontFamily(font.FontName)
|
||||
if loaded := r.loadFontSource(family, source, style); loaded != nil {
|
||||
return source, loaded
|
||||
}
|
||||
}
|
||||
return fontSource{}, nil
|
||||
}
|
||||
|
||||
// loadFontSource 加载字体来源
|
||||
// 入参: family 字体族, source 字体来源, style Canvas字体样式
|
||||
// 返回: *canvas.FontFamily 字体族
|
||||
func (r *Renderer) loadFontSource(family *canvas.FontFamily, source fontSource, style canvas.FontStyle) *canvas.FontFamily {
|
||||
key := fontCacheKey{fontSourceKey: fontSourceKey{kind: source.kind, index: source.index, name: source.name, face: source.face}, style: style}
|
||||
if cached, ok := r.canvasState().fontCache[key]; ok {
|
||||
return cached
|
||||
}
|
||||
var err error
|
||||
var data []byte
|
||||
switch source.kind {
|
||||
case fontSourceFile:
|
||||
data, err = os.ReadFile(source.name)
|
||||
case fontSourceFS:
|
||||
data, err = fs.ReadFile(r.fontFS[source.index], source.name)
|
||||
case fontSourceSystem:
|
||||
err = family.LoadSystemFont(source.name, style)
|
||||
}
|
||||
if err == nil && source.kind != fontSourceSystem {
|
||||
if bytes.HasPrefix(data, []byte("ttcf")) {
|
||||
data, err = extractCollectionFont(data, source.face)
|
||||
}
|
||||
if err == nil {
|
||||
err = family.LoadFont(data, 0, style)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
r.canvasState().fontCache[key] = nil
|
||||
return nil
|
||||
}
|
||||
r.canvasState().fontCache[key] = family
|
||||
return family
|
||||
}
|
||||
|
||||
// matchFontFiles 查找字体文件
|
||||
// 入参: dir 字体目录, names 字体名称, bold 是否粗体, italic 是否斜体
|
||||
// 返回: []fontFileMatch 字体匹配列表
|
||||
func (r *Renderer) matchFontFiles(dir string, names []string, bold, italic bool) []fontFileMatch {
|
||||
candidates, ok := r.canvasState().fontDirCandidates[dir]
|
||||
if !ok {
|
||||
files, _ := filepath.Glob(filepath.Join(dir, "*"))
|
||||
candidates = fontFileCandidates(files, filepath.Base)
|
||||
for i, candidate := range candidates {
|
||||
file, err := os.Open(candidate.name)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
candidates = appendFontFileNames(candidates, i, fontFileNames(file))
|
||||
file.Close()
|
||||
}
|
||||
r.canvasState().fontDirCandidates[dir] = candidates
|
||||
}
|
||||
return fontFileMatches(candidates, names, bold, italic)
|
||||
}
|
||||
|
||||
// matchFontFS 匹配字体文件系统中的字体名称
|
||||
// 入参: index 字体文件系统索引, names 字体名称, bold 是否粗体, italic 是否斜体
|
||||
// 返回: []fontFileMatch 字体匹配列表
|
||||
func (r *Renderer) matchFontFS(index int, names []string, bold, italic bool) []fontFileMatch {
|
||||
fsys := r.fontFS[index]
|
||||
if fonts, ok := fsys.(*FontFS); ok {
|
||||
return fonts.matchStyle(names, bold, italic)
|
||||
}
|
||||
candidates, ok := r.canvasState().fontFSCandidates[index]
|
||||
if !ok {
|
||||
files, _ := fs.Glob(fsys, "*")
|
||||
candidates = fontFileCandidates(files, path.Base)
|
||||
for i, candidate := range candidates {
|
||||
file, err := fsys.Open(candidate.name)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
reader, ok := file.(io.ReaderAt)
|
||||
if !ok {
|
||||
data, _ := io.ReadAll(file)
|
||||
reader = bytes.NewReader(data)
|
||||
}
|
||||
candidates = appendFontFileNames(candidates, i, fontFileNames(reader))
|
||||
file.Close()
|
||||
}
|
||||
r.canvasState().fontFSCandidates[index] = candidates
|
||||
}
|
||||
return fontFileMatches(candidates, names, bold, italic)
|
||||
}
|
||||
|
||||
// systemFontDirs 获取系统字体目录
|
||||
// 返回: []string 字体目录列表
|
||||
func systemFontDirs() []string {
|
||||
switch runtime.GOOS {
|
||||
case "android":
|
||||
return []string{"/product/fonts", "/system/fonts"}
|
||||
case "darwin":
|
||||
return []string{`/Library/Fonts`}
|
||||
case "linux":
|
||||
return []string{`/usr/share/fonts`}
|
||||
case "windows":
|
||||
return []string{`C:\Windows\Fonts`}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user