Consider using twpayne/go-svg for SVG XML generation
Refs https://github.com/tdewolff/canvas/pull/292#issuecomment-2059150687.
Currently tdewolff/canvas's SVG back end uses fmt.Fprintf to generate the SVG XML strings, for example:
https://github.com/tdewolff/canvas/blob/master/renderers/svg/svg.go#L290-L302
This requires great care to ensure that the resulting document is valid.
I've just developed github.com/twpayne/go-svg which allows you to generate SVG XML at a high level, for example:
svgElement := svg.New().WidthHeight(4, 4, svg.CM).ViewBox(0, 0, 400, 400).AppendChildren(
svg.Title(svg.CharData("Example triangle01- simple example of a 'path'")),
svg.Desc(svg.CharData("A path that draws a triangle")),
svg.Rect().XYWidthHeight(1, 1, 398, 398, svg.Number).Fill("none").Stroke("blue"),
svg.Path().D(svgpath.New().
MoveToAbs([]float64{100, 100}).
LineToAbs([]float64{300, 100}).
LineToAbs([]float64{200, 300}).
ClosePath(),
).Fill("red").Stroke("blue").StrokeWidth(svg.Number(3)),
)
if _, err := svgElement.WriteToIndent(os.Stdout, "", " "); err != nil {
panic(err)
}
Note that twpayne/go-svg is NOT a competitor to tdewolff/canvas. twpayne/go-svg is a low-level SVG XML generation library that could be a useful building block for a high level vector graphics library like tdewolff/canvas.
Maybe this is of interest?
Hi Tom, that looks like an awesome library you've been working on, nice! That would definitely improve the readability of the SVG renderers and I'll take a look into using it. I'm a bit swamped with other stuff but will pick this up as I have more time on my hands.
On a side note, the issue you reference is not about writing but about reading SVG. I intend to build an extension on my SVG (and HTML, and XML) parsers in github.com/tdewolff/parse, which are only lexers as of now.
On a side note, the issue you reference is not about writing but about reading SVG.
Oops, I misunderstood that. Thanks for the clarification. Closing this issue then.