Skip to content

Commit

Permalink
add Textspan...Span...TextEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
ajstarks committed Apr 6, 2021
1 parent 7a3c8b5 commit 1f3ef52
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ A video describing how to use the package can be seen on YouTube at <http://www.
* richter: Gerhard Richter's 256 colors
* rl: Random lines (port of a Processing demo)
* skewabc: Skew ABC
* span: Text span
* stockproduct: Visualize product and stock prices
* svgopher: SVGo Mascot
* svgplay: SVGo sketching server
Expand Down Expand Up @@ -482,8 +483,19 @@ is used to specify inputs and results for filter effects
![Image](http://farm5.static.flickr.com/4058/5188556346_e5ce3dcbc2_m.jpg)

Text(x int, y int, t string, s ...string)
Place the specified text, t at x,y according to the style specified in s.
Place the specified text, t at x,y according to the optional style specified in s.
<http://www.w3.org/TR/SVG11/text.html#TextElement>

Textspan(x int, y int, t string, s ...string)
Place specified text, t at x,y according to the optional style specified in s.
<https://www.w3.org/TR/SVG11/text.html#TSpanElement>
Use this method with Span(...). End with TextEnd()

Span(t string, s ...string)
Create a text span t, using optional style s

TextEnd()
End a text span

Textlines(x, y int, s []string, size, spacing int, fill, align string)
Places lines of text in s, starting at x,y, at the specified size, fill, and alignment, and spacing.
Expand Down
21 changes: 21 additions & 0 deletions span/span.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;font-family:sans;fill:white")
canvas.Textspan(width/2, height/2, "Hello ", "font-size:30px")
canvas.Span("SVG", "font-family:serif;font-size:50px;fill:yellow")
canvas.TextEnd()
canvas.Gend()
canvas.End()
}
25 changes: 25 additions & 0 deletions svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,31 @@ func (svg *SVG) Text(x int, y int, t string, s ...string) {
svg.println(`</text>`)
}

// 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>`)
}

// Textpath places text optionally styled text along a previously defined path
// Standard Reference: http://www.w3.org/TR/SVG11/text.html#TextPathElement
func (svg *SVG) Textpath(t string, pathid string, s ...string) {
Expand Down

0 comments on commit 1f3ef52

Please sign in to comment.