ajstarks / svgo

Go Language Library for SVG generation
Other
2.15k stars 170 forks source link

Support importing a svg into the SVG object #57

Open hhhapz opened 3 years ago

hhhapz commented 3 years ago

Would be really cool if we could get functionality that allowed us to take an already existing svg (a file, for example) and import it into svgo and modify it!

This would be really helpful when working with large preexisting svgs and adding dynamic content on top of it :)

Here how I envision it to be used:

buf, err := os.OpenFile("my.svg")
if err != nil {
  return err
}
canvas, err := svg.Import(buf, os.Stdout)
if err != nil {
  return err
}
canvas.DoAwesomeThings(...)
// ...
canvas.End() // my.svg with awesome things done to it printed to stdout
hhhapz commented 3 years ago

I suppose this could be achieved two fold, by writing a partial svg to the writer before using svgo, or embedding the svg, especially as the target for this library is generation specifically. Curious to know what your thoughts are on this, and if it's out of scope, feel free to close the issue :). Thanks for the great lib 😀

ajstarks commented 3 years ago

See: https://github.com/ajstarks/svgo/blob/master/codepic/codepic.go for an approach to this.

hhhapz commented 3 years ago

Hey @ajstarks the placepic solution is really elegant! Thanks for writing it. Would you be open if I opened a PR that is a bit more simplified version of it that focuses more on embedding an svg within another? Perhaps under the name embed.go. Let me know if you are cool with that :)

ajstarks commented 3 years ago

sure, I'm ok with reviewing embed.go.