svg
svg copied to clipboard
No drawing instructions
Hi,
I have this little test program:
package main
import (
"bytes"
"fmt"
"github.com/rustyoz/svg"
"github.com/xyproto/tinysvg"
"image"
)
func Render(svgdata []byte) (*image.RGBA, error) {
p, err := svg.ParseSvgFromReader(bytes.NewReader(svgdata), "Untitled", 1.0)
if err != nil {
return nil, err
}
fmt.Println("title:", p.Title)
fmt.Println("groups:")
for _, group := range p.Groups {
fmt.Println("group:", group)
}
fmt.Println("width:", p.Width)
fmt.Println("height:", p.Height)
fmt.Println("viewbox", p.ViewBox)
for _, element := range p.Elements {
fmt.Println("element:", element)
}
fmt.Println("name:", p.Name)
fmt.Println("transform:", p.Transform)
drawingInstructionChan, errorChan := p.ParseDrawingInstructions()
for err := range errorChan {
if err != nil {
fmt.Println("error", err)
} else {
fmt.Println("no error")
}
}
for drawingInstruction := range drawingInstructionChan {
fmt.Println("drawing instruction", drawingInstruction)
}
img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{256, 256}})
return img, nil
}
func main() {
document, svgTag := tinysvg.NewTinySVG(256, 256)
svgTag.Describe("Diagram")
roundedRectangle := svgTag.AddRoundedRect(30, 10, 5, 5, 20, 20)
roundedRectangle.Fill("red")
fmt.Println(document)
_, err := Render(document.Bytes())
if err != nil {
panic(err)
}
}
However, I can't see any drawing instructions appearing. I would expect this line to kick in at least once:
fmt.Println("drawing instruction", drawingInstruction)
What am I doing wrong?
This is a feature request for adding some documentation or a small example program for simple use of this package.
Hi @xyproto
It appears that the later developers, (I only did the first few commits) haven't implemented the drawing instruction parser for rectangles fully.
Compare the implementation for a circle
// ParseDrawingInstructions implements the DrawingInstructionParser
// interface
func (c *Circle) ParseDrawingInstructions() (chan *DrawingInstruction, chan error) {
draw := make(chan *DrawingInstruction)
errs := make(chan error)
go func() {
defer close(draw)
defer close(errs)
draw <- &DrawingInstruction{
Kind: CircleInstruction,
M: &Tuple{c.Cx, c.Cy},
Radius: &c.Radius,
}
draw <- &DrawingInstruction{Kind: PaintInstruction, Fill: &c.Fill}
}()
return draw, errs
}
as compared to a rectangle:
// ParseDrawingInstructions implements the DrawingInstructionParser
// interface
func (r *Rect) ParseDrawingInstructions() (chan *DrawingInstruction, chan error) {
draw := make(chan *DrawingInstruction)
errs := make(chan error)
defer close(draw)
defer close(errs)
return draw, errs
}
Your test code has found this oversight. Unfortunately i don't have the time to fix this.
I would search for references to the later fork https://github.com/vytis/svg to find examples of it being used. Good luck
Thanks for the information and the detailed reply! It's helpful to know the current state of the implementation, and I wasn't aware of the fork.