1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
#SVGo: A Go library for SVG generation#
The library generates SVG as defined by the Scalable Vector Graphics 1.1 Specification (<http://www.w3.org/TR/SVG11/>).
Output goes to the specified io.Writer.
## Supported SVG elements and functions ##
circle, ellipse, polygon, polyline, rect (including roundrects), paths (general, arc,
cubic and quadratic bezier paths), line, image, text, linearGradient, radialGradient,
transforms (translate, rotate, scale, skewX, skewY)
## Metadata elements ##
desc, defs, g (style, transform, id), mask, title, (a)ddress, link, script, use
## Building and Usage ##
See svgdef.[svg|png|pdf] for a graphical view of the function calls
Usage:
goinstall github.com/ajstarks/svgo
to install into your Go environment. To update the library you can use:
goinstall -clean -u -v github.com/ajstarks/svgo
a minimal program, to generate SVG to standard output.
package main
import (
"github.com/ajstarks/svgo"
"os"
)
func main() {
width := 500
height := 500
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Circle(width/2, height/2, 100)
canvas.Text(width/2, height/2, "Hello, SVG", "text-anchor:middle;font-size:30px;fill:white")
canvas.End()
}
Drawing in a web server: (http://localhost:2003/circle)
package main
import (
"log"
"github.com/ajstarks/svgo"
"net/http"
)
func main() {
http.Handle("/circle", http.HandlerFunc(circle))
err := http.ListenAndServe(":2003", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func circle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
s := svg.New(w)
s.Start(500, 500)
s.Circle(250, 250, 125, "fill:none;stroke:black")
s.End()
}
You may view the SVG output with a browser that supports SVG (tested on Chrome, Opera, Firefox and Safari), or any other SVG user-agent such as Batik Squiggle. The test-svgo script tries to use reasonable defaults based on the GOOS and GOARCH environment variables.
The command:
$ ./newsvg foo.go
creates Go source file ready for your code, using $EDITOR
To create browsable documentation:
$ godoc -path=<svgo directory> -<http=:6060>
and click on the "Package documentation for svg" link
### Tutorial Video ###
A video describing how to use the package can be seen on YouTube at <http://www.youtube.com/watch?v=ze6O2Dj5gQ4>
## Package contents ##
* svg.go: Library
* newsvg: Coding template command
* svgdef: Creates a SVG representation of the API
* android: The Android logo
* bubtrail: Bubble trails
* bulletgraph: Bullet Graphs (via Stephen Few)
* colortab: Display SVG named colors with RGB values
* compx: Component diagrams
* flower: Random "flowers"
* fontcompare: Compare two fonts
* f50: Get 50 photos from Flickr based on a query
* funnel: Funnel from transparent circles
* gradient: Linear and radial gradients
* html5logo: HTML5 logo with draggable elements
* imfade: Show image fading
* lewitt: Version of Sol Lewitt's Wall Drawing 91
* ltr: Layer Tennis Remixes
* paths Demonstrate SVG paths
* planets: Show the scale of the Solar system
* pmap: Proportion maps
* randcomp: Compare random number generators
* richter: Gerhard Richter's 256 colors
* rl: Random lines (port of a Processing demo)
* skewabc: Skew ABC
* stockproduct: Visualize product and stock prices
* svgopher: SVGo Mascot
* tsg: Twitter Search Grid
* vismem: Visualize data from files
* webfonts: "Hello, World" with Google Web Fonts
* websvg: Generate SVG as a web server
## Functions and types ##
Many functions use x, y to specify an object's location, and w, h to specify the object's width and height.
Where applicable, a final optional argument specifies the style to be applied to the object.
The style strings follow the SVG standard; name:value pairs delimited by semicolons, or a
series of name="value" pairs. For example: `"fill:none; opacity:0.3"` or `fill="none" opacity="0.3"` (see: <http://www.w3.org/TR/SVG11/styling.html>)
The Offcolor type:
type Offcolor struct {
Offset uint8
Color string
Opacity float
}
is used to specify the offset, color, and opacity of stop colors in linear and radial gradients
### Structure, Scripting, Metadata, Transformation and Links ###
New(w io.Writer) *SVG
Constructor, Specify the output destination.
Start(w int, h int, attributes ...string)
begin the SVG document with the width w and height h. Optionally add additional elememts
(such as additional namespaces or scripting events)
<http://www.w3.org/TR/SVG11/struct.html#SVGElement>
Startview(w, h, minx, miny, vw, vh int)
begin the SVG document with the width w, height h, with a viewBox at minx, miny, vw, vh.
<http://www.w3.org/TR/SVG11/struct.html#SVGElement>
End()
end the SVG document
Script(scriptype string, data ...string)
Script defines a script with a specified type, (for example "application/javascript").
if the first variadic argument is a link, use only the link reference.
Otherwise, treat variadic arguments as the text of the script (marked up as CDATA).
if no data is specified, simply close the script element.
<http://www.w3.org/TR/SVG/script.html>
Group(s ...string)
begin a group, with arbitrary attributes
<http://www.w3.org/TR/SVG11/struct.html#GElement>
Gstyle(s string)
begin a group, with the specified style.
<http://www.w3.org/TR/SVG11/struct.html#GElement>
Gid(s string)
begin a group, with the specified id.
Gtransform(s string)
begin a group, with the specified transform, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
Translate(x, y int)
begins coordinate translation to (x,y), end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
Scale(n float64)
scales the coordinate system by n, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
ScaleXY(x, y float64)
scales the coordinate system by x, y. End with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
SkewX(a float64)
SkewX skews the x coordinate system by angle a, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
SkewY(a float64)
SkewY skews the y coordinate system by angle a, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
SkewXY(ax, ay float64)
SkewXY skews x and y coordinate systems by ax, ay respectively, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
Rotate(r float64)
rotates the coordinate system by r degrees, end with Gend().
<http://www.w3.org/TR/SVG11/coords.html#TransformAttribute>
TranslateRotate(x, y int, r float64)
translates the coordinate system to (x,y), then rotates to r degrees, end with Gend().
RotateTranslate(x, y int, r float64)
rotates the coordinate system r degrees, then translates to (x,y), end with Gend().
Gend()
end the group (must be paired with Gstyle, Gtransform, Gid).
ClipPath(s ...string)
Begin a ClipPath
<http://www.w3.org/TR/SVG/masking.html#ClippingPaths>
ClipEnd()
End a ClipPath
<http://www.w3.org/TR/SVG/masking.html#ClippingPaths>
Def()
begin a definition block.
<http://www.w3.org/TR/SVG11/struct.html#DefsElement>
DefEnd()
end a definition block.
Mask(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.
Desc(s string)
specify the text of the description.
<http://www.w3.org/TR/SVG11/struct.html#DescElement>
Title(s string)
specify the text of the title.
<http://www.w3.org/TR/SVG11/struct.html#TitleElement>
Link(href string, title string)
begin a link named "href", with the specified title.
<http://www.w3.org/TR/SVG11/linking.html#Links>
LinkEnd()
end the link.
Use(x int, y int, link string, s ...string)
place the object referenced at link at the location x, y.
<http://www.w3.org/TR/SVG11/struct.html#UseElement>
### Shapes ###
Circle(x int, y int, r int, s ...string)
draw a circle, centered at x,y with radius r.
<http://www.w3.org/TR/SVG11/shapes.html#CircleElement>

Ellipse(x int, y int, w int, h int, s ...string)
draw an ellipse, centered at x,y with radii w, and h.
<http://www.w3.org/TR/SVG11/shapes.html#EllipseElement>

Polygon(x []int, y []int, s ...string)
draw a series of line segments using an array of x, y coordinates.
<http://www.w3.org/TR/SVG11/shapes.html#PolygonElement>

Rect(x int, y int, w int, h int, s ...string)
draw a rectangle with upper left-hand corner at x,y, with width w, and height h.
<http://www.w3.org/TR/SVG11/shapes.html#RectElement>

CenterRect(x int, y int, w int, h int, s ...string)
draw a rectangle with its center at x,y, with width w, and height h.
Roundrect(x int, y int, w int, h int, rx int, ry int, s ...string)
draw a rounded rectangle with upper the left-hand corner at x,y,
with width w, and height h. The radii for the rounded portion
is specified by rx (width), and ry (height).

Square(x int, y int, s int, style ...string)
draw a square with upper left corner at x,y with sides of length s.

### Paths ###
Path(p string, s ...style)
draw the arbitrary path as specified in p, according to the style specified in s. <http://www.w3.org/TR/SVG11/paths.html>
Arc(sx int, sy int, ax int, ay int, r int, large bool, sweep bool, ex int, ey int, s ...string)
draw an elliptical arc beginning coordinate at sx,sy, ending coordinate at ex, ey
width and height of the arc are specified by ax, ay, the x axis rotation is r
if sweep is true, then the arc will be drawn in a "positive-angle" direction (clockwise),
if false, the arc is drawn counterclockwise.
if large is true, the arc sweep angle is greater than or equal to 180 degrees,
otherwise the arc sweep is less than 180 degrees.
<http://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands>

Bezier(sx int, sy int, cx int, cy int, px int, py int, ex int, ey int, s ...string)
draw a cubic bezier curve, beginning at sx,sy, ending at ex,ey
with control points at cx,cy and px,py.
<http://www.w3.org/TR/SVG11/paths.html#PathDataCubicBezierCommands>

Qbezier(sx int, sy int, cx int, cy int, ex int, ey int, tx int, ty int, s ...string)
draw a quadratic bezier curve, beginning at sx, sy, ending at tx,ty
with control points are at cx,cy, ex,ey.
<http://www.w3.org/TR/SVG11/paths.html#PathDataQuadraticBezierCommands>

Qbez(sx int, sy int, cx int, cy int, ex int, ey int, s...string)
draws a quadratic bezier curver, with optional style beginning at sx,sy, ending at ex, sy
with the control point at cx, cy.
<http://www.w3.org/TR/SVG11/paths.html#PathDataQuadraticBezierCommands>

### Lines ###
Line(x1 int, y1 int, x2 int, y2 int, s ...string)
draw a line segment between x1,y1 and x2,y2.
<http://www.w3.org/TR/SVG11/shapes.html#LineElement>

Polyline(x []int, y []int, s ...string)
draw a polygon using coordinates specified in x,y arrays.
<http://www.w3.org/TR/SVG11/shapes.html#PolylineElement>

### Image and Text ###
Image(x int, y int, w int, h int, link string, s ...string)
place at x,y (upper left hand corner), the image with width w, and height h, referenced at link.
<http://www.w3.org/TR/SVG11/struct.html#ImageElement>

Text(x int, y int, t string, s ...string)
Place the specified text, t at x,y according to the style specified in s.
<http://www.w3.org/TR/SVG11/text.html#TextElement>
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.
Textpath(t string, pathid string, s ...string)
places optionally styled text along a previously defined path.
<http://www.w3.org/TR/SVG11/text.html#TextPathElement>

### Color ###
RGB(r int, g int, b int) string
creates a style string for the fill color designated
by the (r)ed, g(reen), (b)lue components.
<http://www.w3.org/TR/css3-color/>
RGBA(r int, g int, b int, a float64) string
as above, but includes the color's opacity as a value
between 0.0 (fully transparent) and 1.0 (opaque).
### Gradients ###
LinearGradient(id string, x1, y1, x2, y2 uint8, sc []Offcolor)
constructs a linear color gradient identified by id,
along the vector defined by (x1,y1), and (x2,y2).
The stop color sequence defined in sc. Coordinates are expressed as percentages.
<http://www.w3.org/TR/SVG11/pservers.html#LinearGradients>

RadialGradient(id string, cx, cy, r, fx, fy uint8, sc []Offcolor)
constructs a radial color gradient identified by id,
centered at (cx,cy), with a radius of r.
(fx, fy) define the location of the focal point of the light source.
The stop color sequence defined in sc.
Coordinates are expressed as percentages.
<http://www.w3.org/TR/SVG11/pservers.html#RadialGradients>

### Utility ###
Grid(x int, y int, w int, h int, n int, s ...string)
draws a grid of straight lines starting at x,y, with a width w, and height h, and a size of n.

### Credits ###
Thanks to Jonathan Wright for the io.Writer update.
|