mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-08-30 12:12:40 +08:00
feat(字体渲染): 优化字体渲染
This commit is contained in:
+26
-4
@@ -20,13 +20,21 @@ import (
|
||||
)
|
||||
|
||||
type fontMatchRule struct {
|
||||
Keys []string
|
||||
Names []string
|
||||
Files []string
|
||||
System string
|
||||
Keys []string
|
||||
Names []string
|
||||
Files []string
|
||||
System string
|
||||
NoSyntheticBold bool
|
||||
}
|
||||
|
||||
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", "书宋", "方正书宋"},
|
||||
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 判断字体规则是否匹配
|
||||
// 入参: rule 字体匹配规则, name 字体名称
|
||||
// 返回: bool 是否匹配
|
||||
|
||||
+7
-3
@@ -835,9 +835,7 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
|
||||
if weight == 0 && dp != nil && dp.Weight > 0 {
|
||||
weight = dp.Weight
|
||||
}
|
||||
if weight >= 700 {
|
||||
fontStyle |= canvas.FontBold
|
||||
}
|
||||
syntheticBold := weight >= 700
|
||||
italic := obj.Italic
|
||||
if !italic && dp != nil && dp.Italic {
|
||||
italic = true
|
||||
@@ -849,6 +847,9 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
|
||||
embeddedFont := false
|
||||
if of, ok := r.Reader.fontCache[fontID]; ok {
|
||||
embeddedFont = of.FontFile != ""
|
||||
if !embeddedFont && fontNoSyntheticBold(of.FontName, of.FamilyName) {
|
||||
syntheticBold = false
|
||||
}
|
||||
if of.Bold {
|
||||
fontStyle |= canvas.FontBold
|
||||
}
|
||||
@@ -856,6 +857,9 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
|
||||
fontStyle |= canvas.FontItalic
|
||||
}
|
||||
}
|
||||
if syntheticBold {
|
||||
fontStyle |= canvas.FontBold
|
||||
}
|
||||
ff := r.loadFont(fontID)
|
||||
if ff == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user