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

Consider using twpayne/go-svg for SVG XML generation #301

Closed
twpayne opened this issue May 13, 2024 · 2 comments
Closed

Consider using twpayne/go-svg for SVG XML generation #301

twpayne opened this issue May 13, 2024 · 2 comments

Comments

@twpayne
Copy link
Contributor

twpayne commented May 13, 2024

Refs #292 (comment).

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?

@tdewolff
Copy link
Owner

tdewolff commented May 13, 2024

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.

@twpayne
Copy link
Contributor Author

twpayne commented May 31, 2024

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.

@twpayne twpayne closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
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

2 participants