feat(字体渲染): 优化字体渲染

This commit is contained in:
2026-07-01 15:01:32 +08:00
parent e5e8063c4e
commit 614483f205
2 changed files with 33 additions and 7 deletions
+22
View File
@@ -24,9 +24,17 @@ type fontMatchRule struct {
Names []string Names []string
Files []string Files []string
System string System string
NoSyntheticBold bool
} }
var fontMatchRules = []fontMatchRule{ var fontMatchRules = []fontMatchRule{
{
Keys: []string{"小标宋", "方正小标宋", "xiaobiaosong", "fzxiaobiaosong", "fzxbs"},
Names: []string{"小标宋体", "方正小标宋简体", "FZXiaoBiaoSong-B05", "FZXiaoBiaoSong-B05S"},
Files: []string{"FZXBSJW.TTF", "fzxbs*.ttf", "xiaobiaosong*.ttf"},
System: "FZXiaoBiaoSong-B05S",
NoSyntheticBold: true,
},
{ {
Keys: []string{"宋体", "新宋体", "simsun", "nsimsun", "songti", "书宋", "方正书宋"}, Keys: []string{"宋体", "新宋体", "simsun", "nsimsun", "songti", "书宋", "方正书宋"},
Names: []string{"宋体", "新宋体", "SimSun", "NSimSun", "SongTi"}, Names: []string{"宋体", "新宋体", "SimSun", "NSimSun", "SongTi"},
@@ -169,6 +177,20 @@ func fontDefaultSystemNames() []string {
} }
} }
// fontNoSyntheticBold 判断字体是否禁用合成粗体
// 入参: names 字体名称列表
// 返回: bool 是否禁用合成粗体
func fontNoSyntheticBold(names ...string) bool {
for _, name := range names {
for _, rule := range fontMatchRules {
if rule.NoSyntheticBold && fontRuleMatch(rule, name) {
return true
}
}
}
return false
}
// fontRuleMatch 判断字体规则是否匹配 // fontRuleMatch 判断字体规则是否匹配
// 入参: rule 字体匹配规则, name 字体名称 // 入参: rule 字体匹配规则, name 字体名称
// 返回: bool 是否匹配 // 返回: bool 是否匹配
+7 -3
View File
@@ -835,9 +835,7 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
if weight == 0 && dp != nil && dp.Weight > 0 { if weight == 0 && dp != nil && dp.Weight > 0 {
weight = dp.Weight weight = dp.Weight
} }
if weight >= 700 { syntheticBold := weight >= 700
fontStyle |= canvas.FontBold
}
italic := obj.Italic italic := obj.Italic
if !italic && dp != nil && dp.Italic { if !italic && dp != nil && dp.Italic {
italic = true italic = true
@@ -849,6 +847,9 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
embeddedFont := false embeddedFont := false
if of, ok := r.Reader.fontCache[fontID]; ok { if of, ok := r.Reader.fontCache[fontID]; ok {
embeddedFont = of.FontFile != "" embeddedFont = of.FontFile != ""
if !embeddedFont && fontNoSyntheticBold(of.FontName, of.FamilyName) {
syntheticBold = false
}
if of.Bold { if of.Bold {
fontStyle |= canvas.FontBold fontStyle |= canvas.FontBold
} }
@@ -856,6 +857,9 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
fontStyle |= canvas.FontItalic fontStyle |= canvas.FontItalic
} }
} }
if syntheticBold {
fontStyle |= canvas.FontBold
}
ff := r.loadFont(fontID) ff := r.loadFont(fontID)
if ff == nil { if ff == nil {
return return