From 89b8b93b2021e7385ce635a99a733c41ded75845 Mon Sep 17 00:00:00 2001 From: Anthony Starks Date: Sun, 15 Apr 2012 19:56:09 -0400 Subject: [PATCH] update README, add svgrid, svgplot definition script --- README.markdown | 1 + svgplot/mksvgplotdef | 24 ++++++++++++ svgplot/test.d | 51 +++++++++++++++++++++++++ svgrid/svgrid.go | 90 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 svgplot/mksvgplotdef create mode 100644 svgplot/test.d create mode 100644 svgrid/svgrid.go diff --git a/README.markdown b/README.markdown index 42c2d40..60ed610 100644 --- a/README.markdown +++ b/README.markdown @@ -116,6 +116,7 @@ A video describing how to use the package can be seen on YouTube at f00.svg +svgplot $gopts -connect -label=connect $data > f01.svg +svgplot $gopts -showbar -connect=f -label=showbar $data > f02.svg +svgplot $gopts -horizon -connect=f -label=horizon $data > f03.svg +svgplot $gopts -showdot -connect=f -label=showdot $data > f04.svg +svgplot $gopts -showx -label=showx $data > f05.svg +svgplot $gopts -showy -label=showy $data > f06.svg +svgplot $gopts -showfile -label=showfile $data > f07.svg +# attr +svgplot $gopts -bgcolor=lightsteelblue -label=bgcolor $data > f08.svg +svgplot $gopts -linecolor=red -label=linecolor $data > f09.svg +svgplot $gopts -barcolor=red -showbar -connect=f -label=barcolor $data > f10.svg +svgplot $gopts -hcolor=red -horizon -connect=f -label=hcolor $data > f11.svg +svgplot $gopts -showdot -dotcolor=red -connect=f -label=dotcolor $data > f12.svg +svgplot $gopts -label=fontsize $data > f13.svg +svgplot $gopts -font=Courier -label="font" $data > f14.svg +svgplot $gopts -label=labelcolor -labelcolor=red $data > f15.svg +svgrid -h 1200 -w 1024 -r=f -x=50 -y=50 -g 10 -c 8 f??.svg > svgplotdef.svg + diff --git a/svgplot/test.d b/svgplot/test.d new file mode 100644 index 0000000..e79c144 --- /dev/null +++ b/svgplot/test.d @@ -0,0 +1,51 @@ +0 493.65 +1 497.57 +2 506.38 +3 521.03 +4 532.44 +5 535.36 +6 546.60 +7 531.99 +8 527.28 +9 534.01 +10 538.26 +11 528.94 +12 597.62 +13 594.94 +14 602.55 +15 595.35 +16 606.99 +17 618.23 +18 618.98 +19 622.52 +20 607.22 +21 610.94 +22 603.69 +23 606.77 +24 592.40 +25 601.17 +26 577.52 +27 579.04 +28 546.02 +29 573.41 +30 549.01 +31 562.13 +32 563.77 +33 557.23 +34 539.00 +35 533.15 +36 504.88 +37 490.92 +38 498.17 +39 518.82 +40 523.29 +41 520.04 +42 526.86 +43 539.08 +44 540.70 +45 540.96 +46 532.50 +47 524.84 +48 522.18 +49 534.03 +50 534.96 diff --git a/svgrid/svgrid.go b/svgrid/svgrid.go new file mode 100644 index 0000000..5840cf5 --- /dev/null +++ b/svgrid/svgrid.go @@ -0,0 +1,90 @@ +// svgrid -- composite SVG files in a grid +package main + +import ( + "encoding/xml" + "flag" + "fmt" + "io" + "os" + + "github.com/ajstarks/svgo" +) + +// SVG is a SVG document +type SVG struct { + Width int `xml:"width,attr"` + Height int `xml:"height,attr"` + Doc string `xml:",innerxml"` +} + +var ( + byrow bool + startx, starty, count, gutter, gwidth, gheight int + canvas = svg.New(os.Stdout) +) + +// init sets up command line options +func init() { + flag.BoolVar(&byrow, "r", true, "order row wise") + flag.IntVar(&startx, "x", 0, "begin x") + flag.IntVar(&starty, "y", 0, "begin y") + flag.IntVar(&count, "c", 3, "columns or rows") + flag.IntVar(&gutter, "g", 100, "gutter") + flag.IntVar(&gwidth, "w", 1024, "width") + flag.IntVar(&gheight, "h", 768, "height") + flag.Parse() +} + +// placepic puts a SVG file at a location +func placepic(x, y int, filename string) (int, int) { + var s SVG + f, err := os.Open(filename) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + return 0, 0 + } + defer f.Close() + if err := xml.NewDecoder(f).Decode(&s); err != nil { + fmt.Fprintf(os.Stderr, "Unable to parse (%v)\n", err) + return 0, 0 + } + canvas.Group(`clip-path="url(#pic)"`, fmt.Sprintf(`transform="translate(%d,%d)"`, x, y)) + canvas.ClipPath(`id="pic"`) + canvas.Rect(0, 0, s.Width, s.Height) + canvas.ClipEnd() + io.WriteString(canvas.Writer, s.Doc) + canvas.Gend() + return s.Width, s.Height +} + +// compose places files row or column-wise +func compose(x, y, n int, rflag bool, files []string) { + px := x + py := y + var pw, ph int + for i, f := range files { + if i > 0 && i%n == 0 { + if rflag { + px = x + py += gutter + ph + } else { + px += gutter + pw + py = y + } + } + pw, ph = placepic(px, py, f) + if rflag { + px += gutter + pw + } else { + py += gutter + ph + } + } +} + +// main lays out files as specified on the command line +func main() { + canvas.Start(gwidth, gheight) + compose(startx, starty, count, byrow, flag.Args()) + canvas.End() +}