Closed Iriel closed 3 years ago
This looks interesting. How about:
// Textspan begins text, assuming a tspan will be included, end with TextEnd()
// Standard Reference: https://www.w3.org/TR/SVG11/text.html#TSpanElement
func (svg *SVG) Textspan(x int, y int, t string, s ...string) {
svg.printf(`<text %s %s`, loc(x, y), endstyle(s, ">"))
xml.Escape(svg.Writer, []byte(t))
}
// Span makes styled spanned text, should be proceeded by Textspan
// Standard Reference: https://www.w3.org/TR/SVG11/text.html#TSpanElement
func (svg *SVG) Span(t string, s ...string) {
if len(s) == 0 {
xml.Escape(svg.Writer, []byte(t))
return
}
svg.printf(`<tspan %s`, endstyle(s, ">"))
xml.Escape(svg.Writer, []byte(t))
svg.printf(`</tspan>`)
}
// TextEnd ends spanned text
// Standard Reference: https://www.w3.org/TR/SVG11/text.html#TSpanElement
func (svg *SVG) TextEnd() {
svg.println(`</text>`)
}
Example use:
package main
import (
"os"
svg "github.com/ajstarks/svgo"
)
func main() {
width := 500
height := 500
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Circle(width/2, height/2, 100)
canvas.Gstyle("text-anchor:middle;fill:white")
canvas.Textspan(width/2, height/2, "Hello ", "font-size:30px")
canvas.Span("SVG", "font-size:50px")
canvas.TextEnd()
canvas.Gend()
canvas.End()
}
Lovely thanks!
Thanks for the useful library, one thing I found missing (and had to hack together myself) was support for tspan - as I was trying to center text with mixed styling.
This is what I ended up with in non-method form (the _svg functions are my replicas of the svgo private methods)