mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-08-30 12:12:40 +08:00
feat(横线渲染): 修复横线渲染
This commit is contained in:
+30
-5
@@ -462,12 +462,20 @@ func (r *Renderer) renderImage(ctx *canvas.Context, obj ImageObject, pageH float
|
|||||||
// 入参: ctx 画布上下文, obj 路径对象, pageH 页面高度, defaultFill 默认填充色, defaultStroke 默认描边色, defaultLW 默认线宽, parentCTM 父级CTM
|
// 入参: ctx 画布上下文, obj 路径对象, pageH 页面高度, defaultFill 默认填充色, defaultStroke 默认描边色, defaultLW 默认线宽, parentCTM 父级CTM
|
||||||
func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64, defaultFill, defaultStroke color.Color, defaultLW float64, parentCTM *Matrix) {
|
func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64, defaultFill, defaultStroke color.Color, defaultLW float64, parentCTM *Matrix) {
|
||||||
ctx.Push()
|
ctx.Push()
|
||||||
|
bx, by := 0.0, 0.0
|
||||||
|
if obj.Boundary != "" {
|
||||||
|
if box, err := ParseBox(obj.Boundary); err == nil {
|
||||||
|
bx, by = box.X, box.Y
|
||||||
|
}
|
||||||
|
}
|
||||||
ctm := NewMatrix(obj.CTM)
|
ctm := NewMatrix(obj.CTM)
|
||||||
if parentCTM != nil {
|
if parentCTM != nil {
|
||||||
ctm = parentCTM.Multiply(ctm)
|
ctm = parentCTM.Multiply(ctm)
|
||||||
}
|
}
|
||||||
r.applyClips(ctx, obj.Clips, pageH, &ctm)
|
r.applyClips(ctx, obj.Clips, pageH, &ctm)
|
||||||
fillColor, strokeColor := defaultFill, defaultStroke
|
fillColor, strokeColor := defaultFill, defaultStroke
|
||||||
|
var fillPaint any = fillColor
|
||||||
|
var strokePaint any = strokeColor
|
||||||
lineWidth := defaultLW
|
lineWidth := defaultLW
|
||||||
if lineWidth == 0 {
|
if lineWidth == 0 {
|
||||||
lineWidth = 0.353
|
lineWidth = 0.353
|
||||||
@@ -483,9 +491,11 @@ func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64
|
|||||||
}
|
}
|
||||||
if dp.FillColor != nil {
|
if dp.FillColor != nil {
|
||||||
fillColor = parseFillColor(dp.FillColor)
|
fillColor = parseFillColor(dp.FillColor)
|
||||||
|
fillPaint = parseFillPaint(dp.FillColor, bx, by, pageH, 0, 0)
|
||||||
}
|
}
|
||||||
if dp.StrokeColor != nil {
|
if dp.StrokeColor != nil {
|
||||||
strokeColor = parseStrokeColor(dp.StrokeColor)
|
strokeColor = parseStrokeColor(dp.StrokeColor)
|
||||||
|
strokePaint = parseStrokePaint(dp.StrokeColor, bx, by, pageH, 0, 0)
|
||||||
}
|
}
|
||||||
if dp.Cap == "Round" {
|
if dp.Cap == "Round" {
|
||||||
lineCap = canvas.RoundCap
|
lineCap = canvas.RoundCap
|
||||||
@@ -508,9 +518,11 @@ func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64
|
|||||||
}
|
}
|
||||||
if obj.FillColor != nil {
|
if obj.FillColor != nil {
|
||||||
fillColor = parseFillColor(obj.FillColor)
|
fillColor = parseFillColor(obj.FillColor)
|
||||||
|
fillPaint = parseFillPaint(obj.FillColor, bx, by, pageH, 0, 0)
|
||||||
}
|
}
|
||||||
if obj.StrokeColor != nil {
|
if obj.StrokeColor != nil {
|
||||||
strokeColor = parseStrokeColor(obj.StrokeColor)
|
strokeColor = parseStrokeColor(obj.StrokeColor)
|
||||||
|
strokePaint = parseStrokePaint(obj.StrokeColor, bx, by, pageH, 0, 0)
|
||||||
}
|
}
|
||||||
if obj.Cap != "" {
|
if obj.Cap != "" {
|
||||||
if obj.Cap == "Round" {
|
if obj.Cap == "Round" {
|
||||||
@@ -534,13 +546,23 @@ func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64
|
|||||||
dashPattern = parseFloats(obj.DashPattern)
|
dashPattern = parseFloats(obj.DashPattern)
|
||||||
dashOffset = obj.DashOffset
|
dashOffset = obj.DashOffset
|
||||||
}
|
}
|
||||||
|
if scale := math.Sqrt(math.Abs(ctm.a*ctm.d - ctm.b*ctm.c)); scale > 0 {
|
||||||
|
lineWidth *= scale
|
||||||
|
dashOffset *= scale
|
||||||
|
for i := range dashPattern {
|
||||||
|
dashPattern[i] *= scale
|
||||||
|
}
|
||||||
|
}
|
||||||
p := r.buildPath(obj, pageH, ctm)
|
p := r.buildPath(obj, pageH, ctm)
|
||||||
shouldFill := true
|
shouldFill := true
|
||||||
if obj.Fill != nil {
|
if obj.Fill != nil {
|
||||||
shouldFill = *obj.Fill
|
shouldFill = *obj.Fill
|
||||||
}
|
}
|
||||||
if shouldFill && fillColor != nil {
|
if fillPaint == nil {
|
||||||
ctx.SetFillColor(fillColor)
|
fillPaint = fillColor
|
||||||
|
}
|
||||||
|
if shouldFill && fillPaint != nil {
|
||||||
|
ctx.SetFill(fillPaint)
|
||||||
ctx.SetStrokeColor(canvas.Transparent)
|
ctx.SetStrokeColor(canvas.Transparent)
|
||||||
ctx.DrawPath(0, 0, p)
|
ctx.DrawPath(0, 0, p)
|
||||||
}
|
}
|
||||||
@@ -549,11 +571,14 @@ func (r *Renderer) renderPath(ctx *canvas.Context, obj PathObject, pageH float64
|
|||||||
shouldStroke = *obj.Stroke
|
shouldStroke = *obj.Stroke
|
||||||
}
|
}
|
||||||
if shouldStroke {
|
if shouldStroke {
|
||||||
if strokeColor == nil {
|
if strokePaint == nil {
|
||||||
strokeColor = color.Transparent
|
strokePaint = strokeColor
|
||||||
|
}
|
||||||
|
if strokePaint == nil {
|
||||||
|
strokePaint = color.Transparent
|
||||||
}
|
}
|
||||||
ctx.SetFillColor(canvas.Transparent)
|
ctx.SetFillColor(canvas.Transparent)
|
||||||
ctx.SetStrokeColor(strokeColor)
|
ctx.SetStroke(strokePaint)
|
||||||
ctx.SetStrokeWidth(lineWidth)
|
ctx.SetStrokeWidth(lineWidth)
|
||||||
ctx.SetStrokeCapper(lineCap)
|
ctx.SetStrokeCapper(lineCap)
|
||||||
ctx.SetStrokeJoiner(lineJoin)
|
ctx.SetStrokeJoiner(lineJoin)
|
||||||
|
|||||||
+17
-1
@@ -15,10 +15,10 @@
|
|||||||
package ofdgo
|
package ofdgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/tdewolff/canvas"
|
||||||
"image/color"
|
"image/color"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"github.com/tdewolff/canvas"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// parseColor 解析颜色字符串
|
// parseColor 解析颜色字符串
|
||||||
@@ -88,6 +88,22 @@ func parseStrokeColor(strokeColor *StrokeColor) color.Color {
|
|||||||
return parseAxialShdColor(strokeColor.AxialShd, strokeColor.Alpha)
|
return parseAxialShdColor(strokeColor.AxialShd, strokeColor.Alpha)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseStrokePaint 解析勾边画刷
|
||||||
|
// 入参: strokeColor 勾边颜色节点, x X坐标, y Y坐标, pageH 页面高度, originX 原点X坐标, originY 原点Y坐标
|
||||||
|
// 返回: any 勾边画刷
|
||||||
|
func parseStrokePaint(strokeColor *StrokeColor, x, y, pageH, originX, originY float64) any {
|
||||||
|
if strokeColor == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(strokeColor.Value) != "" {
|
||||||
|
return parseColorWithAlpha(strokeColor.Value, strokeColor.Alpha)
|
||||||
|
}
|
||||||
|
if gradient := parseAxialShdGradient(strokeColor.AxialShd, strokeColor.Alpha, x, y, pageH, originX, originY); gradient != nil {
|
||||||
|
return gradient
|
||||||
|
}
|
||||||
|
return parseAxialShdColor(strokeColor.AxialShd, strokeColor.Alpha)
|
||||||
|
}
|
||||||
|
|
||||||
// parseAxialShdColor 解析轴向渐变颜色
|
// parseAxialShdColor 解析轴向渐变颜色
|
||||||
// 入参: axialShd 轴向渐变节点, alpha 透明度
|
// 入参: axialShd 轴向渐变节点, alpha 透明度
|
||||||
// 返回: color.Color 颜色对象
|
// 返回: color.Color 颜色对象
|
||||||
|
|||||||
Reference in New Issue
Block a user