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 continue
} }
if gFlag { if gFlag {
gCount, _ = strconv.Atoi(p) var err error
gCount, err = strconv.Atoi(p)
if err != nil {
gCount = 0
}
gFlag = false gFlag = false
continue continue
} }
+30 -16
View File
@@ -714,14 +714,11 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
fontStyle |= canvas.FontItalic fontStyle |= canvas.FontItalic
} }
for _, dir := range r.fontDirs { for _, dir := range r.fontDirs {
matches, _ := filepath.Glob(filepath.Join(dir, of.FontName+"*")) matches := r.globFontFiles(dir, of.FontName+"*")
for _, m := range matches { for _, m := range matches {
ext := strings.ToLower(filepath.Ext(m)) if err := ff.LoadFontFile(m, fontStyle); err == nil {
if ext == ".ttf" || ext == ".otf" || ext == ".ttc" { r.FontMap[fontID] = ff
if err := ff.LoadFontFile(m, fontStyle); err == nil { return ff
r.FontMap[fontID] = ff
return ff
}
} }
} }
} }
@@ -781,16 +778,17 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
} }
} }
winFontDir := `C:\Windows\Fonts` winFontDir := `C:\Windows\Fonts`
matches, _ := filepath.Glob(filepath.Join(winFontDir, "*"+targetName+"*")) matches := r.globFontFiles(winFontDir, "*"+targetName+"*")
if len(matches) == 0 { if len(matches) == 0 {
if targetName == "SimSun" { switch targetName {
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simsun.ttc")) case "SimSun":
} else if targetName == "KaiTi" { matches = r.globFontFiles(winFontDir, "simsun.ttc")
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simkai.ttf")) case "KaiTi":
} else if targetName == "SimHei" { matches = r.globFontFiles(winFontDir, "simkai.ttf")
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simhei.ttf")) case "SimHei":
} else if targetName == "FangSong" { matches = r.globFontFiles(winFontDir, "simhei.ttf")
matches, _ = filepath.Glob(filepath.Join(winFontDir, "simfang.ttf")) case "FangSong":
matches = r.globFontFiles(winFontDir, "simfang.ttf")
} }
} }
for _, m := range matches { for _, m := range matches {
@@ -800,9 +798,25 @@ func (r *Renderer) loadFont(fontID string) *canvas.FontFamily {
} }
} }
} }
r.FontMap[fontID] = defaultFont
return 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 渲染印章 // renderStamp 渲染印章
// 入参: ctx 画布上下文, s 印章对象, pageH 页面高度 // 入参: ctx 画布上下文, s 印章对象, pageH 页面高度
func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) { func (r *Renderer) renderStamp(ctx *canvas.Context, s Stamp, pageH float64) {