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

Black background when converting SVG to PNG #34

Open
giancarlobastos opened this issue Nov 19, 2021 · 9 comments
Open

Black background when converting SVG to PNG #34

giancarlobastos opened this issue Nov 19, 2021 · 9 comments

Comments

@giancarlobastos
Copy link

I'm trying to convert a svg file to png, but the output file ended with a black background instead of white.

package converter

import (
	"image"
	"image/png"

	"net/http"
	"os"

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

func ConvertSvgToPng() error {
	url := "https://s.glbimg.com/es/sde/f/organizacoes/2020/02/12/botsvg.svg"
	w, h := 512, 512

	response, err := http.Get(url)
	if err != nil {
		return err
	}
	defer response.Body.Close()

	icon, _ := oksvg.ReadIconStream(response.Body)
	icon.SetTarget(0, 0, float64(w), float64(h))

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

	out, err := os.Create("out.png")
	if err != nil {
		return err
	}
	defer out.Close()

	err = png.Encode(out, rgba)
	if err != nil {
		return err
	}
	return err
}

SVG file: https://s.glbimg.com/es/sde/f/organizacoes/2020/02/12/botsvg.svg

PNG output:
out

@mysugar
Copy link

mysugar commented Mar 12, 2022

Hi ,have you solved this problem? @giancarlobastos

@yeldiRium
Copy link
Contributor

yeldiRium commented Aug 12, 2022

Some input on this: I believe this is due to oksvg not supporting style tags in SVG. I can't say for sure, but I'm having the same problem and removing the style block and putting the style attributes directly into the path tags solved the rendering issue for me.

Edit: I've looked at @giancarlobastos SVG and it also uses a style block to assign fill colors to the paths. I think we are experiencing the same issue.

@yeldiRium
Copy link
Contributor

yeldiRium commented Aug 12, 2022

I've perused the code and I've found something that confuses me:

style tags should be handled here

if se.Name.Local == "style" && cursor.inDefs {
and the code to do so looks fine to me. Once the endElement for the style tag is found, its text content is parsed and added to the styles here
icon.classes, err = parseClasses(classInfo)

But before the style tag can be handled, the readStartElement call here

err = cursor.readStartElement(se)
will always fail, since style tags are not handled there and not a valid draw function, so this line will fail
df, ok := drawFuncs[se.Name.Local]

I'm not that far into the code, so I'm not sure enough on how to fix this to provide a PR. However, my intuition is to handle style tags before and without calling readStartElement on them. @srwiley what do you think about this? Can you do anything with this research?

@yeldiRium
Copy link
Contributor

yeldiRium commented Aug 12, 2022

One more update from me (sorry for the multipost):

After looking at the tests I realize that the style tag is supported, but only within an enclosing defs tag. That is understandable, but is probably not how most SVGs look and as far as I know there is nothing in the SVG spec that says that style tags need to be enclosed in defs tags. @srwiley would you be fine with changing this requirement?

@srwiley
Copy link
Owner

srwiley commented Aug 13, 2022 via email

@yeldiRium
Copy link
Contributor

yeldiRium commented Aug 17, 2022

That is great to hear. Would you mind taking a look at my PR then?
I'm not sure how clean my implementation is, since I basically circumvent the inDefs check by setting inDefsStyle without it. But I added a test and top level styles work the way they are supposed to.

@goloroden
Copy link

@srwiley To me the solution that @yeldiRium came up with looks pretty good. Is there a chance that the according PR gets merged soon?

@srwiley
Copy link
Owner

srwiley commented Sep 2, 2022 via email

@goloroden
Copy link

Yes, there are tests to support the code (and given the discussion above it seems as if @yeldiRium did a pretty decent job 😊).

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

5 participants