From 4a00e7833e86af7b0789d42bea41ca0510eb3186 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Mon, 19 Jan 2026 12:29:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81):=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=AE=E5=A4=8D=E4=B8=8E=E9=A3=8E=E6=A0=BC?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_geom.go | 6 +++++- ofdgo_renderer.go | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/ofdgo_geom.go b/ofdgo_geom.go index b45a4d1..11a16cd 100644 --- a/ofdgo_geom.go +++ b/ofdgo_geom.go @@ -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 } diff --git a/ofdgo_renderer.go b/ofdgo_renderer.go index 2a9c1cd..70a6e39 100644 --- a/ofdgo_renderer.go +++ b/ofdgo_renderer.go @@ -714,14 +714,11 @@ 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 - } + if err := ff.LoadFontFile(m, fontStyle); err == nil { + r.FontMap[fontID] = ff + return ff } } } @@ -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) {