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) {