diff --git a/cmd/pdftext/util.go b/cmd/pdftext/util.go index 8cd32440..f4db6c36 100644 --- a/cmd/pdftext/util.go +++ b/cmd/pdftext/util.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/minify/v2" ) diff --git a/examples/amsterdam-centre/main.go b/examples/amsterdam-centre/main.go index 7dfbe220..d8bf24b7 100644 --- a/examples/amsterdam-centre/main.go +++ b/examples/amsterdam-centre/main.go @@ -11,8 +11,8 @@ import ( "github.com/paulmach/osm" "github.com/paulmach/osm/osmapi" "github.com/paulmach/osm/osmgeojson" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) func main() { diff --git a/examples/co2-mauna-loa/main.go b/examples/co2-mauna-loa/main.go index 0cb0f596..fa9a3357 100644 --- a/examples/co2-mauna-loa/main.go +++ b/examples/co2-mauna-loa/main.go @@ -9,8 +9,8 @@ import ( "os" "strconv" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var fontFamily *canvas.FontFamily diff --git a/examples/fyne/main.go b/examples/fyne/main.go index f9b97bda..10158e1c 100644 --- a/examples/fyne/main.go +++ b/examples/fyne/main.go @@ -3,8 +3,8 @@ package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" - "github.com/tdewolff/canvas" - canvasFyne "github.com/tdewolff/canvas/renderers/fyne" + "github.com/Seanld/canvas" + canvasFyne "github.com/Seanld/canvas/renderers/fyne" ) func main() { diff --git a/examples/go-chart/main.go b/examples/go-chart/main.go index 8aa4761e..11713ddc 100644 --- a/examples/go-chart/main.go +++ b/examples/go-chart/main.go @@ -5,8 +5,8 @@ import ( "time" "github.com/golang/freetype/truetype" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" ) diff --git a/examples/gonum-plot/main.go b/examples/gonum-plot/main.go index 41b1813b..5f6066aa 100644 --- a/examples/gonum-plot/main.go +++ b/examples/gonum-plot/main.go @@ -3,8 +3,8 @@ package main import ( "log" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" diff --git a/examples/html-canvas/main.go b/examples/html-canvas/main.go index c3d265ce..52be1f28 100644 --- a/examples/html-canvas/main.go +++ b/examples/html-canvas/main.go @@ -7,8 +7,8 @@ package main import ( "syscall/js" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/htmlcanvas" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/htmlcanvas" ) func main() { diff --git a/examples/opengl/main.go b/examples/opengl/main.go index 2655f9e5..dc8b6851 100644 --- a/examples/opengl/main.go +++ b/examples/opengl/main.go @@ -8,8 +8,8 @@ import ( "github.com/go-gl/gl/v3.3-core/gl" "github.com/go-gl/glfw/v3.3/glfw" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/opengl" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/opengl" ) func main() { diff --git a/examples/tex/main.go b/examples/tex/main.go index f94c9dcf..51ed35b0 100644 --- a/examples/tex/main.go +++ b/examples/tex/main.go @@ -3,8 +3,8 @@ package main import ( "os" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/tex" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/tex" ) func main() { diff --git a/examples/text-document/main.go b/examples/text-document/main.go index 6b7d394c..52c363a5 100644 --- a/examples/text-document/main.go +++ b/examples/text-document/main.go @@ -6,8 +6,8 @@ import ( "os" "time" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/pdf" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/pdf" ) var fontLatin *canvas.FontFamily diff --git a/font.go b/font.go index aff21fae..288cf675 100644 --- a/font.go +++ b/font.go @@ -9,7 +9,7 @@ import ( "reflect" "sync" - "github.com/tdewolff/canvas/text" + "github.com/Seanld/canvas/text" "github.com/tdewolff/font" ) @@ -693,11 +693,14 @@ func (face *FontFace) LineHeight() float64 { return metrics.Ascent + metrics.Descent } +func (face *FontFace) Glyphs(s string) []text.Glyph { + ppem := face.PPEM(DefaultResolution) + return face.Font.shaper.Shape(s, ppem, face.Direction, face.Script, face.Language, face.Font.features, face.Font.variations) +} + // TextWidth returns the width of a given string in millimeters. func (face *FontFace) TextWidth(s string) float64 { - ppem := face.PPEM(DefaultResolution) - glyphs := face.Font.shaper.Shape(s, ppem, face.Direction, face.Script, face.Language, face.Font.features, face.Font.variations) - return face.textWidth(glyphs) + return face.textWidth(face.Glyphs(s)) } func (face *FontFace) textWidth(glyphs []text.Glyph) float64 { @@ -740,8 +743,7 @@ func (face *FontFace) Decorate(width float64) *Path { // ToPath converts a string to its glyph paths. func (face *FontFace) ToPath(s string) (*Path, float64, error) { ppem := face.PPEM(DefaultResolution) - glyphs := face.Font.shaper.Shape(s, ppem, face.Direction, face.Script, face.Language, face.Font.features, face.Font.variations) - return face.toPath(glyphs, ppem) + return face.toPath(face.Glyphs(s), ppem) } func (face *FontFace) toPath(glyphs []text.Glyph, ppem uint16) (*Path, float64, error) { diff --git a/go.mod b/go.mod index b4c2be68..6eaa35f7 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/tdewolff/canvas +module github.com/Seanld/canvas go 1.22.0 diff --git a/preview.go b/preview.go index da5f947f..b7f7cbda 100644 --- a/preview.go +++ b/preview.go @@ -8,7 +8,7 @@ import ( "path/filepath" "runtime" - "github.com/tdewolff/canvas/text" + "github.com/Seanld/canvas/text" ) func loadFont(name string, style FontStyle) ([]byte, error) { diff --git a/renderers/fyne/fyne.go b/renderers/fyne/fyne.go index 2fd2df04..02c420aa 100644 --- a/renderers/fyne/fyne.go +++ b/renderers/fyne/fyne.go @@ -3,8 +3,8 @@ package fyne import ( "fyne.io/fyne/v2" fyneCanvas "fyne.io/fyne/v2/canvas" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/rasterizer" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/rasterizer" ) type Fyne struct { diff --git a/renderers/gochart.go b/renderers/gochart.go index bf00c543..0420dac1 100644 --- a/renderers/gochart.go +++ b/renderers/gochart.go @@ -5,7 +5,7 @@ import ( "math" "github.com/golang/freetype/truetype" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/font" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" diff --git a/renderers/gonumplot.go b/renderers/gonumplot.go index 3807bde5..5d399d2c 100644 --- a/renderers/gonumplot.go +++ b/renderers/gonumplot.go @@ -5,7 +5,7 @@ import ( "image/color" "math" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" canvasFont "github.com/tdewolff/font" "golang.org/x/image/font" gonumFont "gonum.org/v1/plot/font" diff --git a/renderers/htmlcanvas/htmlcanvas.go b/renderers/htmlcanvas/htmlcanvas.go index 30fe6b0e..5b3c762f 100644 --- a/renderers/htmlcanvas/htmlcanvas.go +++ b/renderers/htmlcanvas/htmlcanvas.go @@ -7,7 +7,7 @@ import ( "math" "syscall/js" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" ) // HTMLCanvas is an HTMLCanvas renderer. diff --git a/renderers/opengl/opengl.go b/renderers/opengl/opengl.go index 01d4f29d..1ad3ef78 100644 --- a/renderers/opengl/opengl.go +++ b/renderers/opengl/opengl.go @@ -8,8 +8,8 @@ import ( "strings" "github.com/go-gl/gl/v3.3-core/gl" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/rasterizer" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/rasterizer" ) // OpenGL is an open graphics library renderer. diff --git a/renderers/pdf/pdf.go b/renderers/pdf/pdf.go index 4cf8d9cf..11b73180 100644 --- a/renderers/pdf/pdf.go +++ b/renderers/pdf/pdf.go @@ -6,7 +6,7 @@ import ( "io" "math" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" ) type Options struct { diff --git a/renderers/pdf/pdf_test.go b/renderers/pdf/pdf_test.go index 5f035e96..1ab66076 100644 --- a/renderers/pdf/pdf_test.go +++ b/renderers/pdf/pdf_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/test" ) diff --git a/renderers/pdf/util.go b/renderers/pdf/util.go index 08c1cb09..f893ea24 100644 --- a/renderers/pdf/util.go +++ b/renderers/pdf/util.go @@ -5,7 +5,7 @@ import ( "math" "strings" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/minify/v2" ) diff --git a/renderers/pdf/writer.go b/renderers/pdf/writer.go index a72d5304..079b27cf 100644 --- a/renderers/pdf/writer.go +++ b/renderers/pdf/writer.go @@ -15,8 +15,8 @@ import ( "time" "unicode/utf16" - "github.com/tdewolff/canvas" - canvasText "github.com/tdewolff/canvas/text" + "github.com/Seanld/canvas" + canvasText "github.com/Seanld/canvas/text" canvasFont "github.com/tdewolff/font" "golang.org/x/text/encoding/charmap" ) diff --git a/renderers/ps/ps.go b/renderers/ps/ps.go index 92547a3b..b01e76a6 100644 --- a/renderers/ps/ps.go +++ b/renderers/ps/ps.go @@ -10,7 +10,7 @@ import ( "strings" "time" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/minify/v2" ) diff --git a/renderers/ps/ps_test.go b/renderers/ps/ps_test.go index 9ce165df..a7459422 100644 --- a/renderers/ps/ps_test.go +++ b/renderers/ps/ps_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" ) func TestPS(t *testing.T) { diff --git a/renderers/rasterizer/rasterizer.go b/renderers/rasterizer/rasterizer.go index 0637849b..4d1ca61d 100644 --- a/renderers/rasterizer/rasterizer.go +++ b/renderers/rasterizer/rasterizer.go @@ -4,7 +4,7 @@ import ( "image" "math" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "golang.org/x/image/draw" "golang.org/x/image/math/f64" "golang.org/x/image/vector" diff --git a/renderers/rasterizer/util.go b/renderers/rasterizer/util.go index e9223918..e4cf4417 100644 --- a/renderers/rasterizer/util.go +++ b/renderers/rasterizer/util.go @@ -4,7 +4,7 @@ import ( "image" "image/color" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "golang.org/x/image/draw" ) diff --git a/renderers/renderers.go b/renderers/renderers.go index d23c1035..d6fa31b3 100644 --- a/renderers/renderers.go +++ b/renderers/renderers.go @@ -10,12 +10,12 @@ import ( "path/filepath" "strings" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/pdf" - "github.com/tdewolff/canvas/renderers/ps" - "github.com/tdewolff/canvas/renderers/rasterizer" - "github.com/tdewolff/canvas/renderers/svg" - "github.com/tdewolff/canvas/renderers/tex" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/pdf" + "github.com/Seanld/canvas/renderers/ps" + "github.com/Seanld/canvas/renderers/rasterizer" + "github.com/Seanld/canvas/renderers/svg" + "github.com/Seanld/canvas/renderers/tex" "golang.org/x/image/bmp" "golang.org/x/image/tiff" ) diff --git a/renderers/renderers_formats.go b/renderers/renderers_formats.go index 280f2c54..82b67a10 100644 --- a/renderers/renderers_formats.go +++ b/renderers/renderers_formats.go @@ -8,8 +8,8 @@ import ( "github.com/Kagami/go-avif" webp "github.com/kolesa-team/go-webp/encoder" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers/rasterizer" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers/rasterizer" ) func WebP(opts ...interface{}) canvas.Writer { diff --git a/renderers/renderers_noformats.go b/renderers/renderers_noformats.go index 30768e5c..3ca1f07f 100644 --- a/renderers/renderers_noformats.go +++ b/renderers/renderers_noformats.go @@ -5,7 +5,7 @@ package renderers import ( "fmt" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" ) // WebP returns a Webp writer that uses libwebp and accepts the following options: canvas.Resolution, canvas.Colorspace, github.com/kolesa-team/go-webp/encoder.*Options diff --git a/renderers/svg/svg.go b/renderers/svg/svg.go index 3963b3e8..947aada8 100644 --- a/renderers/svg/svg.go +++ b/renderers/svg/svg.go @@ -14,8 +14,8 @@ import ( "math" "strings" - "github.com/tdewolff/canvas" - canvasText "github.com/tdewolff/canvas/text" + "github.com/Seanld/canvas" + canvasText "github.com/Seanld/canvas/text" "github.com/tdewolff/font" ) diff --git a/renderers/svg/util.go b/renderers/svg/util.go index 61c65f58..c3b08e07 100644 --- a/renderers/svg/util.go +++ b/renderers/svg/util.go @@ -5,7 +5,7 @@ import ( "math" "strings" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/minify/v2" ) diff --git a/renderers/tex/tex.go b/renderers/tex/tex.go index 00a68fb7..e8e29743 100644 --- a/renderers/tex/tex.go +++ b/renderers/tex/tex.go @@ -7,7 +7,7 @@ import ( "io" "math" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" ) // TeX is a TeX/PGF renderer. Be aware that TeX/PGF does not support transparency of colors. diff --git a/renderers/tex/util.go b/renderers/tex/util.go index c76ea49f..2374769e 100644 --- a/renderers/tex/util.go +++ b/renderers/tex/util.go @@ -5,7 +5,7 @@ import ( "math" "strings" - "github.com/tdewolff/canvas" + "github.com/Seanld/canvas" "github.com/tdewolff/minify/v2" ) diff --git a/resources/boolean/main.go b/resources/boolean/main.go index e3aed5a4..0584af48 100644 --- a/resources/boolean/main.go +++ b/resources/boolean/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var font *canvas.FontFamily diff --git a/resources/docs/boolean/main.go b/resources/docs/boolean/main.go index a06b81b4..a6e6e0e8 100644 --- a/resources/docs/boolean/main.go +++ b/resources/docs/boolean/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var font *canvas.FontFamily diff --git a/resources/docs/colors/main.go b/resources/docs/colors/main.go index 377876a3..5d868015 100644 --- a/resources/docs/colors/main.go +++ b/resources/docs/colors/main.go @@ -5,8 +5,8 @@ import ( "math" "sort" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) func hue(col color.RGBA) float64 { diff --git a/resources/docs/getting-started/main.go b/resources/docs/getting-started/main.go index 76746841..3bcb535c 100644 --- a/resources/docs/getting-started/main.go +++ b/resources/docs/getting-started/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) func main() { diff --git a/resources/docs/hatches/main.go b/resources/docs/hatches/main.go index a25f8baf..2ed5d7da 100644 --- a/resources/docs/hatches/main.go +++ b/resources/docs/hatches/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var face *canvas.FontFace diff --git a/resources/docs/paths/main.go b/resources/docs/paths/main.go index af83bc94..709cb4cf 100644 --- a/resources/docs/paths/main.go +++ b/resources/docs/paths/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var fontFamily *canvas.FontFamily diff --git a/resources/docs/stroke/main.go b/resources/docs/stroke/main.go index 24a65187..7496f14c 100644 --- a/resources/docs/stroke/main.go +++ b/resources/docs/stroke/main.go @@ -4,8 +4,8 @@ import ( "image/color" "math" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var fontFamily *canvas.FontFamily diff --git a/resources/docs/text/main.go b/resources/docs/text/main.go index 5ebea673..931e754e 100644 --- a/resources/docs/text/main.go +++ b/resources/docs/text/main.go @@ -5,8 +5,8 @@ import ( "image/png" "os" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var fontFamily, fontLatin, fontArabic, fontDevanagari, fontMongolian, fontCJK *canvas.FontFamily diff --git a/resources/docs/tiling/main.go b/resources/docs/tiling/main.go index 17625516..d31146fa 100644 --- a/resources/docs/tiling/main.go +++ b/resources/docs/tiling/main.go @@ -4,8 +4,8 @@ import ( "image/color" "math" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) func main() { diff --git a/resources/preview/main.go b/resources/preview/main.go index eb58e581..8b6afd7c 100644 --- a/resources/preview/main.go +++ b/resources/preview/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) func main() { diff --git a/resources/title/main.go b/resources/title/main.go index b19c90b1..d3da999d 100644 --- a/resources/title/main.go +++ b/resources/title/main.go @@ -3,8 +3,8 @@ package main import ( "image/color" - "github.com/tdewolff/canvas" - "github.com/tdewolff/canvas/renderers" + "github.com/Seanld/canvas" + "github.com/Seanld/canvas/renderers" ) var font *canvas.FontFamily diff --git a/svg.go b/svg.go index 481a105a..0da47f2a 100644 --- a/svg.go +++ b/svg.go @@ -810,56 +810,56 @@ func (svg *svgParser) getFontFace() *FontFace { return fontFamily.Face(fontSize, svg.ctx.Style.Fill.Color) } -func (svg *svgParser) drawShape(tag string, attrs map[string]string) { +func (svg *svgParser) toPath(tag string, attrs map[string]string) (x float64, y float64, path *Path) { switch tag { case "circle": - cx := svg.parseDimension(attrs["cx"], svg.width) - cy := svg.parseDimension(attrs["cy"], svg.height) - r := svg.parseDimension(attrs["r"], svg.diagonal) - svg.ctx.DrawPath(cx, cy, Circle(r)) + x = svg.parseDimension(attrs["cx"], svg.width) + y = svg.parseDimension(attrs["cy"], svg.height) + path = Circle( + svg.parseDimension(attrs["r"], svg.diagonal), + ) case "ellipse": - cx := svg.parseDimension(attrs["cx"], svg.width) - cy := svg.parseDimension(attrs["cy"], svg.height) - rx := svg.parseDimension(attrs["rx"], svg.width) - ry := svg.parseDimension(attrs["ry"], svg.height) - svg.ctx.DrawPath(cx, cy, Ellipse(rx, ry)) + x = svg.parseDimension(attrs["cx"], svg.width) + y = svg.parseDimension(attrs["cy"], svg.height) + path = Ellipse( + svg.parseDimension(attrs["rx"], svg.width), + svg.parseDimension(attrs["ry"], svg.height), + ) case "path": - p, err := ParseSVGPath(attrs["d"]) + var err error + path, err = ParseSVGPath(attrs["d"]) if err != nil && svg.err == nil { svg.err = parse.NewErrorLexer(svg.z, "bad path: %w", err) } - svg.ctx.DrawPath(0, 0, p) case "polygon", "polyline": + path = &Path{} points := svg.parsePoints(attrs["points"]) - p := &Path{} for i := 0; i+1 < len(points); i += 2 { if i == 0 { - p.MoveTo(points[0], points[1]) + path.MoveTo(points[0], points[1]) } else { - p.LineTo(points[i], points[i+1]) + path.LineTo(points[i], points[i+1]) } } if tag == "polygon" { - p.Close() + path.Close() } - svg.ctx.DrawPath(0.0, 0.0, p) case "line": - p := &Path{} x1 := svg.parseDimension(attrs["x1"], svg.width) y1 := svg.parseDimension(attrs["y1"], svg.height) x2 := svg.parseDimension(attrs["x2"], svg.width) y2 := svg.parseDimension(attrs["y2"], svg.height) - - p.MoveTo(x1, y1) - p.LineTo(x2, y2) - svg.ctx.DrawPath(0.0, 0.0, p) + path = &Path{} + path.MoveTo(x1, y1) + path.LineTo(x2, y2) case "rect": - x := svg.parseDimension(attrs["x"], svg.width) - y := svg.parseDimension(attrs["y"], svg.height) + x = svg.parseDimension(attrs["x"], svg.width) + y = svg.parseDimension(attrs["y"], svg.height) width := svg.parseDimension(attrs["width"], svg.width) height := svg.parseDimension(attrs["height"], svg.height) + path = &Path{} if attrs["rx"] == "" && attrs["ry"] == "" { - svg.ctx.DrawPath(x, y, Rectangle(width, height)) + path = Rectangle(width, height) } else { // TODO: handle both rx and ry var r float64 @@ -868,19 +868,42 @@ func (svg *svgParser) drawShape(tag string, attrs map[string]string) { } else { r = svg.parseDimension(attrs["ry"], svg.height) } - svg.ctx.DrawPath(x, y, RoundedRectangle(width, height, r)) + path = RoundedRectangle(width, height, r) } case "text": svg.state.textX = svg.parseDimension(attrs["x"], svg.width) svg.state.textY = svg.parseDimension(attrs["y"], svg.height) } + + return +} + +func (svg *svgParser) drawShape(tag string, attrs map[string]string) { + svg.ctx.DrawPath(svg.toPath(tag, attrs)) +} + +type SVGPath struct { + Tag string + Attrs map[string]string + X, Y float64 + *Path } func ParseSVG(r io.Reader) (*Canvas, error) { + cvs, _, err := parseSVGFull(r) + return cvs, err +} + +func ParseSVGWithPaths(r io.Reader) (*Canvas, []SVGPath, error) { + return parseSVGFull(r) +} + +func parseSVGFull(r io.Reader) (*Canvas, []SVGPath, error) { z := parse.NewInput(r) defer z.Restore() l := xml.NewLexer(z) + var paths []SVGPath svg := svgParser{ z: z, defs: map[string]svgDef{}, @@ -892,16 +915,16 @@ func ParseSVG(r io.Reader) (*Canvas, error) { switch tt { case xml.ErrorToken: if l.Err() != io.EOF { - return svg.c, l.Err() + return svg.c, paths, l.Err() } else if svg.err != nil { - return svg.c, svg.err + return svg.c, paths, svg.err } else if svg.c == nil { - return svg.c, fmt.Errorf("expected SVG tag") + return svg.c, paths, fmt.Errorf("expected SVG tag") } if svg.c.W == 0.0 || svg.c.H == 0.0 { svg.c.Fit(0.0) } - return svg.c, nil + return svg.c, paths, nil case xml.StartTagToken: tag := string(data[1:]) tt, attrNames, attrs := svg.parseAttributes(l) @@ -911,7 +934,7 @@ func ParseSVG(r io.Reader) (*Canvas, error) { width, height, viewbox := svg.parseViewBox(attrs["width"], attrs["height"], attrs["viewBox"]) svg.init(width, height, viewbox) } else if tag != "svg" && svg.c == nil { - return svg.c, fmt.Errorf("expected SVG tag") + return svg.c, paths, fmt.Errorf("expected SVG tag") } // handle special tags @@ -921,7 +944,7 @@ func ParseSVG(r io.Reader) (*Canvas, error) { svg.parseStyle(data) tt, data = l.Next() // end token } else { - return svg.c, fmt.Errorf("bad style tag") + return svg.c, paths, fmt.Errorf("bad style tag") } break } else if tag == "defs" { @@ -941,8 +964,30 @@ func ParseSVG(r io.Reader) (*Canvas, error) { } svg.setStyling(props) - // draw shapes such as circles, paths, etc. - svg.drawShape(tag, attrs) + pathX, pathY, path := svg.toPath(tag, attrs) + if path != nil { + // draw shapes such as circles, paths, etc. + svg.ctx.DrawPath(pathX, pathY, path) + + // Copy tag attributes map, excluding `d` which is where + // path data is stored. Since the path is already returned as + // `*Path`, there's not much point to returning `d`, and for + // large/many paths it can be wasteful of memory to return it. + attrsNoD := map[string]string{} + for key, value := range attrs { + if key != "d" { + attrsNoD[key] = value + } + } + + paths = append(paths, SVGPath{ + Tag: tag, + Attrs: attrsNoD, + X: pathX, + Y: pathY, + Path: path, + }) + } // set linearGradient, markers, etc. // these defs depend on the shape or size of the path diff --git a/tests/latex/main.go b/tests/latex/main.go index b5bbe88b..22d4d60c 100644 --- a/tests/latex/main.go +++ b/tests/latex/main.go @@ -2,7 +2,7 @@ package fuzz -import "github.com/tdewolff/canvas" +import "github.com/Seanld/canvas" // Fuzz is a fuzz test. func Fuzz(data []byte) int { diff --git a/tests/svg/main.go b/tests/svg/main.go index 85e7497a..e8fee005 100644 --- a/tests/svg/main.go +++ b/tests/svg/main.go @@ -2,7 +2,7 @@ package fuzz -import "github.com/tdewolff/canvas" +import "github.com/Seanld/canvas" // Fuzz is a fuzz test. func Fuzz(data []byte) int { diff --git a/text.go b/text.go index 1f105e61..9b5de64b 100644 --- a/text.go +++ b/text.go @@ -11,7 +11,7 @@ import ( "strings" "unicode/utf8" - "github.com/tdewolff/canvas/text" + "github.com/Seanld/canvas/text" "github.com/tdewolff/font" )