From 547a938359540a0818d9125fdefae17432abc710 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Wed, 1 Jul 2026 09:58:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=87=E5=AD=97=E6=B8=B2=E6=9F=93):=20?= =?UTF-8?q?=E5=8A=A0=E5=BC=BA=E6=96=87=E5=AD=97=E6=B8=B2=E6=9F=93=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_renderer.go | 17 ++++++++++++++--- ofdgo_text.go | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ofdgo_renderer.go b/ofdgo_renderer.go index 5918e60..42e41c4 100644 --- a/ofdgo_renderer.go +++ b/ofdgo_renderer.go @@ -802,6 +802,10 @@ func (r *Renderer) renderText(ctx *canvas.Context, obj TextObject, pageH float64 if obj.VScale != 0 { sizeMM *= obj.VScale } + hScale := obj.HScale + if hScale == 0 { + hScale = 1 + } useTextMatrix := hasTextMatrix(ctm) if scale := ctm.YScale(); scale > 0 && !useTextMatrix { 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 { cx += dx } else if len(dys) == 0 { - cx += face.TextWidth(str) + cx += face.TextWidth(str) * hScale } } 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) canvasX, canvasY := tx+bx, pageH-(ty+by) - textWidth := face.TextWidth(str) + textWidth := face.TextWidth(str) * hScale glyphFillPaint := fillPaint if fillColorNode != nil && fillColorNode.AxialShd != nil { 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 { ctx.SetFill(glyphFillPaint) 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 { path, width := face.ToPath(str) - textWidth = width + textWidth = width * hScale ctx.DrawPath(x, y, path) } else { textFace := face diff --git a/ofdgo_text.go b/ofdgo_text.go index 400af31..4cba91d 100644 --- a/ofdgo_text.go +++ b/ofdgo_text.go @@ -35,9 +35,9 @@ func parseColor(val string) color.Color { func parseColorWithAlpha(val string, alpha *int) color.Color { parts := strings.Fields(val) if len(parts) >= 3 { - r, _ := strconv.Atoi(parts[0]) - g, _ := strconv.Atoi(parts[1]) - b, _ := strconv.Atoi(parts[2]) + r := parseColorComponent(parts[0]) + g := parseColorComponent(parts[1]) + b := parseColorComponent(parts[2]) a := 255 if alpha != nil { a = *alpha @@ -53,6 +53,19 @@ func parseColorWithAlpha(val string, alpha *int) color.Color { 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 限制颜色分量范围 // 入参: v 颜色分量 // 返回: int 颜色分量