feat(文字渲染): 加强文字渲染能力

This commit is contained in:
2026-07-01 09:58:59 +08:00
parent 074706a72c
commit 547a938359
2 changed files with 30 additions and 6 deletions
+14 -3
View File
@@ -802,6 +802,10 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
if obj.VScale != 0 { if obj.VScale != 0 {
sizeMM *= obj.VScale sizeMM *= obj.VScale
} }
hScale := obj.HScale
if hScale == 0 {
hScale = 1
}
useTextMatrix := hasTextMatrix(ctm) useTextMatrix := hasTextMatrix(ctm)
if scale := ctm.YScale(); scale > 0 && !useTextMatrix { if scale := ctm.YScale(); scale > 0 && !useTextMatrix {
sizeMM *= scale sizeMM *= scale
@@ -888,7 +892,7 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
if dx, ok := textDelta(dxs, i-1); ok { if dx, ok := textDelta(dxs, i-1); ok {
cx += dx cx += dx
} else if len(dys) == 0 { } else if len(dys) == 0 {
cx += face.TextWidth(str) cx += face.TextWidth(str) * hScale
} }
} }
if i < len(ys) { if i < len(ys) {
@@ -900,7 +904,7 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
} }
tx, ty := ctm.Transform(cx, cy) tx, ty := ctm.Transform(cx, cy)
canvasX, canvasY := tx+bx, pageH-(ty+by) canvasX, canvasY := tx+bx, pageH-(ty+by)
textWidth := face.TextWidth(str) textWidth := face.TextWidth(str) * hScale
glyphFillPaint := fillPaint glyphFillPaint := fillPaint
if fillColorNode != nil && fillColorNode.AxialShd != nil { if fillColorNode != nil && fillColorNode.AxialShd != nil {
glyphFillPaint = parseFillPaint(fillColorNode, bx, by, pageH, canvasX, canvasY) glyphFillPaint = parseFillPaint(fillColorNode, bx, by, pageH, canvasX, canvasY)
@@ -908,9 +912,16 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64
if glyphFillPaint != nil { if glyphFillPaint != nil {
ctx.SetFill(glyphFillPaint) ctx.SetFill(glyphFillPaint)
drawGlyph := func(x, y float64) { drawGlyph := func(x, y float64) {
if hScale != 1 {
ctx.Push()
ctx.Translate(x, y)
ctx.Scale(hScale, 1)
x, y = 0, 0
defer ctx.Pop()
}
if embeddedFont { if embeddedFont {
path, width := face.ToPath(str) path, width := face.ToPath(str)
textWidth = width textWidth = width * hScale
ctx.DrawPath(x, y, path) ctx.DrawPath(x, y, path)
} else { } else {
textFace := face textFace := face
+16 -3
View File
@@ -35,9 +35,9 @@ func parseColor(val string) color.Color {
func parseColorWithAlpha(val string, alpha *int) color.Color { func parseColorWithAlpha(val string, alpha *int) color.Color {
parts := strings.Fields(val) parts := strings.Fields(val)
if len(parts) >= 3 { if len(parts) >= 3 {
r, _ := strconv.Atoi(parts[0]) r := parseColorComponent(parts[0])
g, _ := strconv.Atoi(parts[1]) g := parseColorComponent(parts[1])
b, _ := strconv.Atoi(parts[2]) b := parseColorComponent(parts[2])
a := 255 a := 255
if alpha != nil { if alpha != nil {
a = *alpha a = *alpha
@@ -53,6 +53,19 @@ func parseColorWithAlpha(val string, alpha *int) color.Color {
return color.Black return color.Black
} }
// parseColorComponent 解析颜色分量
// 入参: s 颜色分量
// 返回: int 颜色分量值
func parseColorComponent(s string) int {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "#") {
v, _ := strconv.ParseInt(strings.TrimPrefix(s, "#"), 16, 0)
return int(v)
}
v, _ := strconv.Atoi(s)
return v
}
// clampColor 限制颜色分量范围 // clampColor 限制颜色分量范围
// 入参: v 颜色分量 // 入参: v 颜色分量
// 返回: int 颜色分量 // 返回: int 颜色分量