Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaled transform breaks dashing #18

Open
Qendolin opened this issue Jun 19, 2023 · 1 comment
Open

Scaled transform breaks dashing #18

Qendolin opened this issue Jun 19, 2023 · 1 comment

Comments

@Qendolin
Copy link

Qendolin commented Jun 19, 2023

When rasterizing an SVG with a scaled transformation matrix dashing does not behave correctly.
My goal is to create a high resolution png render of the svg.

At scale 1.0 (correct):
Not Scaled SVG

At scale 2.0:
image

Code used:

package main

import (
	"image"
	"image/png"
	"os"

	"github.com/srwiley/oksvg"
	"github.com/srwiley/rasterx"
)

func main() {
	scale := 2.0

	in, err := os.Open("in.svg")
	check(err)
	defer in.Close()

	icon, err := oksvg.ReadIconStream(in)
	check(err)

	w, h := int(icon.ViewBox.W*scale), int(icon.ViewBox.H*scale)

	icon.SetTarget(0, 0, float64(w), float64(h))
	rgba := image.NewRGBA(image.Rect(0, 0, w, h))
	dasher := rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds()))
	icon.Draw(dasher, 1)

	out, err := os.Create("out.png")
	check(err)
	defer out.Close()

	err = png.Encode(out, rgba)
	check(err)
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}

SVG used:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="-5 -5 110 110">
    <path d="
      M 50, 50
	  l 0 -50
    " stroke="teal" fill="none" />
    <path d="
      M 0, 50
      a 50,50 0 1,0 100,0
      a 50,50 0 1,0 -100,0
    " stroke="teal" fill="none" stroke-dasharray="314" stroke-dashoffset="157" />
</svg>
@Qendolin
Copy link
Author

As a workaround adding

for i := range icon.SVGPaths {
	sp := &icon.SVGPaths[i]
	sp.DashOffset *= scale
	for j := range sp.Dash {
		sp.Dash[j] *= scale
	}
}

before drawing works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant