Skip to content

Commit

Permalink
feat: add p/demo/svg (gnolang#905)
Browse files Browse the repository at this point in the history
* feat: add p/demo/svg

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

* chore: fixup

* chore: fixup

* chore: fixup

* chore: add gno.mod

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

---------

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul authored Jul 4, 2023
1 parent 2b2156f commit 6d137df
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/gno.land/p/demo/svg/doc.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Package svg is a minimalist SVG generation library for Gno.
The svg package provides a simple and lightweight solution for programmatically generating SVG (Scalable Vector Graphics) markup in Gno. It allows you to create basic shapes like rectangles and circles, and output the generated SVG to a
Example:
import "gno.land/p/demo/svg""
func Foo() string {
canvas := svg.Canvas{Width: 200, Height: 200}
canvas.DrawRectangle(50, 50, 100, 100, "red")
canvas.DrawCircle(100, 100, 50, "blue")
return canvas.String()
}
*/
package svg // import "gno.land/p/demo/svg"
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/svg/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/p/demo/svg

require (
"gno.land/p/demo/ufmt" v0.0.0-latest
)
82 changes: 82 additions & 0 deletions examples/gno.land/p/demo/svg/svg.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package svg

import "gno.land/p/demo/ufmt"

type Canvas struct {
Width int
Height int
Elems []Elem
}

type Elem interface{ String() string }

func (c Canvas) String() string {
output := ""
output += ufmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d">`, c.Width, c.Height)
for _, elem := range c.Elems {
output += elem.String()
}
output += "</svg>"
return output
}

func (c *Canvas) Append(elem Elem) {
c.Elems = append(c.Elems, elem)
}

type Circle struct {
CX int // center X
CY int // center Y
R int // radius
Fill string
}

func (c Circle) String() string {
return ufmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" fill="%s" />`, c.CX, c.CY, c.R, c.Fill)
}

func (c *Canvas) DrawCircle(cx, cy, r int, fill string) {
c.Append(Circle{
CX: cx,
CY: cy,
R: r,
Fill: fill,
})
}

type Rectangle struct {
X, Y, Width, Height int
Fill string
}

func (c Rectangle) String() string {
return ufmt.Sprintf(`<rect x="%d" y="%d" width="%d" height="%d" fill="%s" />`, c.X, c.Y, c.Width, c.Height, c.Fill)
}

func (c *Canvas) DrawRectangle(x, y, width, height int, fill string) {
c.Append(Rectangle{
X: x,
Y: y,
Width: width,
Height: height,
Fill: fill,
})
}

type Text struct {
X, Y int
Text, Fill string
}

func (c Text) String() string {
return ufmt.Sprintf(`<text x="%d" y="%d" fill="%s">%s</text>`, c.X, c.Y, c.Fill, c.Text)
}

func (c *Canvas) DrawText(x, y int, text, fill string) {
c.Append(Text{
X: x,
Y: y,
Text: text,
Fill: fill,
})
}
15 changes: 15 additions & 0 deletions examples/gno.land/p/demo/svg/z0_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// PKGPATH: gno.land/p/demo/svg_test
package svg_test

import "gno.land/p/demo/svg"

func main() {
canvas := svg.Canvas{Width: 500, Height: 500}
canvas.DrawRectangle(50, 50, 100, 100, "red")
canvas.DrawCircle(100, 100, 50, "blue")
canvas.DrawText(100, 100, "hello world!", "magenta")
println(canvas)
}

// Output:
// <svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><rect x="50" y="50" width="100" height="100" fill="red" /><circle cx="100" cy="100" r="50" fill="blue" /><text x="100" y="100" fill="magenta">hello world!</text></svg>
19 changes: 19 additions & 0 deletions examples/gno.land/p/demo/svg/z1_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// PKGPATH: gno.land/p/demo/svg_test
package svg_test

import "gno.land/p/demo/svg"

func main() {
canvas := svg.Canvas{
Width: 500, Height: 500,
Elems: []svg.Elem{
svg.Rectangle{50, 50, 100, 100, "red"},
svg.Circle{50, 50, 100, "red"},
svg.Text{100, 100, "hello world!", "magenta"},
},
}
println(canvas)
}

// Output:
// <svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><rect x="50" y="50" width="100" height="100" fill="red" /><circle cx="50" cy="50" r="100" fill="red" /><text x="100" y="100" fill="magenta">hello world!</text></svg>

0 comments on commit 6d137df

Please sign in to comment.