mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-08-30 12:12:40 +08:00
feat(优化代码): 错误修复与风格优化
This commit is contained in:
+5
-1
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-13
@@ -714,17 +714,14 @@ 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 ext == ".ttf" || ext == ".otf" || ext == ".ttc" {
|
|
||||||
if err := ff.LoadFontFile(m, fontStyle); err == nil {
|
if err := ff.LoadFontFile(m, fontStyle); err == nil {
|
||||||
r.FontMap[fontID] = ff
|
r.FontMap[fontID] = ff
|
||||||
return ff
|
return ff
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
for _, fsys := range r.fontFS {
|
for _, fsys := range r.fontFS {
|
||||||
if matches, err := fs.Glob(fsys, of.FontName+"*"); err == nil {
|
if matches, err := fs.Glob(fsys, of.FontName+"*"); err == nil {
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user