Skip to content

Commit

Permalink
add pattern support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajstarks committed May 1, 2014
1 parent 4bc4de0 commit 71809e1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
17 changes: 13 additions & 4 deletions README.markdown
Expand Up @@ -30,7 +30,7 @@ Output goes to the specified io.Writer.

### Metadata elements ###

desc, defs, g (style, transform, id), marker, mask, title, (a)ddress, link, script, use
desc, defs, g (style, transform, id), marker, mask, pattern, title, (a)ddress, link, script, use

## Building and Usage ##

Expand Down Expand Up @@ -290,19 +290,28 @@ is used to specify inputs and results for filter effects
DefEnd()
end a definition block.

Marker(id string, x, y, w, h int, s ...string)
Marker(id string, x, y, w, h int, s ...string)
define a marker
<http://www.w3.org/TR/SVG11/painting.html#MarkerElement>

MarkerEnd()

MarkerEnd()
end a marker

Mask(string, x int, y int, w int, h int, s ...string)

Mask(id string, x int, y int, w int, h int, s ...string)
creates a mask with a specified id, dimension, and optional style.
<http://www.w3.org/TR/SVG/masking.html>

MaskEnd()
ends the Mask element.


Pattern(id string, x, y, width, height int, putype string, s ...string)
define a Pattern with the specified dimensions, the putype can be either "user" or "obj", which sets the patternUnits
attribute to be either userSpaceOnUse or objectBoundingBox.
<http://www.w3.org/TR/SVG11/pservers.html#Patterns>

Desc(s string)
specify the text of the description.
<http://www.w3.org/TR/SVG11/struct.html#DescElement>
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Expand Up @@ -29,7 +29,7 @@ Filter Effects
Metadata elements
desc, defs, g (style, transform, id), mask, marker, title, (a)ddress, link, script, use
desc, defs, g (style, transform, id), mask, marker, pattern, title, (a)ddress, link, script, use
Usage: (assuming GOPATH is set)
Expand Down
33 changes: 33 additions & 0 deletions pattern/pattern.go
@@ -0,0 +1,33 @@
// pattern: test the pattern function
package main

import (
"github.com/ajstarks/svgo"
"fmt"
"os"
)

func main() {
canvas := svg.New(os.Stdout)
w, h := 500, 500
pct := 5
pw, ph := (w*pct)/100, (h*pct)/100
canvas.Start(w, h)

// define the pattern
canvas.Def()
canvas.Pattern("hatch", 0, 0, pw, ph, "user")
canvas.Gstyle("fill:none;stroke-width:1")
canvas.Path(fmt.Sprintf("M0,0 l%d,%d", pw, ph), "stroke:red")
canvas.Path(fmt.Sprintf("M%d,0 l-%d,%d", pw, pw, ph), "stroke:blue")
canvas.Gend()
canvas.PatternEnd()
canvas.DefEnd()

// use the pattern
canvas.Gstyle("stroke:black; stroke-width:2")
canvas.Circle(w/2, h/2, h/8, "fill:url(#hatch)")
canvas.CenterRect((w*4)/5, h/2, h/4, h/4, "fill:url(#hatch)")
canvas.Gend()
canvas.End()
}
21 changes: 20 additions & 1 deletion svg.go
Expand Up @@ -192,13 +192,32 @@ func (svg *SVG) Def() { svg.println(`<defs>`) }
func (svg *SVG) DefEnd() { svg.println(`</defs>`) }

// Marker defines a marker
// Standard reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement
func (svg *SVG) Marker(id string, x, y, width, height int, s ...string) {
svg.printf(`<marker id="%s" refX="%d" refY="%d" markerWidth="%d" markerHeight="%d" %s`, id, x, y, width, height, endstyle(s, ">\n"))
svg.printf(`<marker id="%s" refX="%d" refY="%d" markerWidth="%d" markerHeight="%d" %s`,
id, x, y, width, height, endstyle(s, ">\n"))
}

// MarkEnd ends a marker
func (svg *SVG) MarkerEnd() { svg.println(`</marker>`) }

// Pattern defines a pattern with the specified dimensions.
// The putype can be either "user" or "obj", which sets the patternUnits
// attribute to be either userSpaceOnUse or objectBoundingBox
// Standard reference: http://www.w3.org/TR/SVG11/pservers.html#Patterns
func (svg *SVG) Pattern(id string, x, y, width, height int, putype string, s ...string) {
puattr := "userSpaceOnUse"
if putype != "user" {
puattr = "objectBoundingBox"
}
svg.printf(`<pattern id="%s" x="%d" y="%d" width="%d" height="%d" patternUnits="%s" %s`,
id, x, y, width, height, puattr, endstyle(s, ">\n"))
}

// PatternEnd ends a marker
func (svg *SVG) PatternEnd() { svg.println(`</pattern>`) }


// Desc specified the text of the description tag.
// Standard Reference: http://www.w3.org/TR/SVG11/struct.html#DescElement
func (svg *SVG) Desc(s string) { svg.tt("desc", s) }
Expand Down
2 changes: 2 additions & 0 deletions svgdef/svgdef.go
Expand Up @@ -413,6 +413,7 @@ func defobjects(w, h int) {
"ClipPath(s ...string)/ClipEnd()",
"Def()/DefEnd()",
"Marker()/MarkerEnd()",
"Pattern()/PatternEnd()",
"Desc(s string)",
"Title(s string)",
"Script(type, data ...string)",
Expand All @@ -431,6 +432,7 @@ func defobjects(w, h int) {
"begin/end clip path",
"begin/end a defintion block",
"begin/end markers",
"begin/end pattern",
"set the description element",
"set the title element",
"define a script",
Expand Down

0 comments on commit 71809e1

Please sign in to comment.