feat(优化代码): 错误修复与风格优化

This commit is contained in:
2026-01-19 12:29:05 +08:00
parent fbfdbb943d
commit 4a00e7833e
2 changed files with 35 additions and 17 deletions
+5 -1
View File
@@ -127,7 +127,11 @@ func parseFloatsWithG(s string) []float64 {
continue
}
if gFlag {
gCount, _ = strconv.Atoi(p)
var err error
gCount, err = strconv.Atoi(p)
if err != nil {
gCount = 0
}
gFlag = false
continue
}
+27 -13
View File
@@ -714,17 +714,14 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
fontStyle |= canvas.FontItalic
}
for _, dir := range r.fontDirs {
matches, _ := filepath.Glob(filepath.Join(dir, of.FontName+"*"))
matches := r.globFontFiles(dir, of.FontName+"*")
for _, m := range matches {
ext := strings.ToLower(filepath.Ext(m))
if ext == ".ttf" || ext == ".otf" || ext == ".ttc" {
if err := ff.LoadFontFile(m, fontStyle); err == nil {
r.FontMap[fontID] = ff
return ff
}
}
}
}
for _, fsys := range r.fontFS {
if matches, err := fs.Glob(fsys, of.FontName+"*"); err == nil {
for _, m := range matches {
@@ -781,16 +778,17 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
}
}
winFontDir := `C:\Windows\Fonts`
matches, _ := filepath.Glob(filepath.Join(winFontDir, "*"+targetName+"*"))
matches := r.globFontFiles(winFontDir, "*"+targetName+"*")
if len(matches) == 0 {
if targetName == "SimSun" {
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simsun.ttc"))
} else if targetName == "KaiTi" {
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simkai.ttf"))
} else if targetName == "SimHei" {
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simhei.ttf"))
} else if targetName == "FangSong" {
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simfang.ttf"))
switch targetName {
case "SimSun":
matches = r.globFontFiles(winFontDir, "simsun.ttc")
case "KaiTi":
matches = r.globFontFiles(winFontDir, "simkai.ttf")
case "SimHei":
matches = r.globFontFiles(winFontDir, "simhei.ttf")
case "FangSong":
matches = r.globFontFiles(winFontDir, "simfang.ttf")
}
}
for _, m := range matches {
@@ -800,9 +798,25 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
}
}
}
r.FontMap[fontID] = defaultFont
return defaultFont
}
// globFontFiles 查找字体文件
// 入参: dir 目录, pattern 模式
// 返回: []string 文件列表
func (r *Renderer) globFontFiles(dir, pattern string) []string {
matches, _ := filepath.Glob(filepath.Join(dir, pattern))
var result []string
for _, m := range matches {
ext := strings.ToLower(filepath.Ext(m))
if ext == ".ttf" || ext == ".otf" || ext == ".ttc" {
result = append(result, m)
}
}
return result
}
// renderStamp 渲染印章
// 入参: ctx 画布上下文, s 印章对象, pageH 页面高度
func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) {