From 614483f205d40c4747b15975425b7273074ec309 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Wed, 1 Jul 2026 15:01:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AD=97=E4=BD=93=E6=B8=B2=E6=9F=93):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E4=BD=93=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_font_match.go | 30 ++++++++++++++++++++++++++---- ofdgo_renderer.go | 10 +++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ofdgo_font_match.go b/ofdgo_font_match.go index 9080558..33022e9 100644 --- a/ofdgo_font_match.go +++ b/ofdgo_font_match.go @@ -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 是否匹配 diff --git a/ofdgo_renderer.go b/ofdgo_renderer.go index 7b84f75..2e85fc0 100644 --- a/ofdgo_renderer.go +++ b/ofdgo_renderer.go @@ -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