From 41d8a21ba22696c8173c54ed09f3a9f902b5b30f Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Sun, 24 Dec 2017 12:38:59 +0100 Subject: [PATCH] Improve toSvgPathDesc some toArcs paths still does not work correctly --- draw2dsvg/gc.go | 105 ++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 38 deletions(-) diff --git a/draw2dsvg/gc.go b/draw2dsvg/gc.go index a542de2..212bd49 100644 --- a/draw2dsvg/gc.go +++ b/draw2dsvg/gc.go @@ -4,22 +4,22 @@ package draw2dsvg import ( - "fmt" - "image" - "image/color" - "strings" "bytes" + "fmt" "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dbase" + "image" + "image/color" + "math" + "strings" ) -const ( -) +const () -var ( -) +var () type drawType int + const ( filled drawType = 1 << iota stroked @@ -47,7 +47,7 @@ func NewGraphicContext(svg *Svg) *GraphicContext { func (gc *GraphicContext) Clear() { gc.svg.Groups = nil gc.svg.Groups = append(gc.svg.Groups, Group{ - // TODO add background color? + // TODO add background color? }) } @@ -56,25 +56,27 @@ func (gc *GraphicContext) Stroke(paths ...*draw2d.Path) { gc.drawPaths(stroked, paths...) gc.Current.Path.Clear() } + // Fill fills the paths with the color specified by SetFillColor func (gc *GraphicContext) Fill(paths ...*draw2d.Path) { gc.drawPaths(filled, paths...) gc.Current.Path.Clear() } + // FillStroke first fills the paths and than strokes them func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) { - gc.drawPaths(filled | stroked, paths...) + gc.drawPaths(filled|stroked, paths...) gc.Current.Path.Clear() } -func (gc *GraphicContext) drawPaths (drawType drawType, paths ...*draw2d.Path) { +func (gc *GraphicContext) drawPaths(drawType drawType, paths ...*draw2d.Path) { paths = append(paths, gc.Current.Path) svgPaths := make([]Path, len(paths)) for i, path := range paths { svgPaths[i].Desc = toSvgPathDesc(path) - if drawType & stroked == stroked { + if drawType&stroked == stroked { svgPaths[i].Stroke = toSvgRGBA(gc.Current.StrokeColor) svgPaths[i].StrokeWidth = toSvgLength(gc.Current.LineWidth) svgPaths[i].StrokeLinecap = gc.Current.Cap.String() @@ -82,7 +84,7 @@ func (gc *GraphicContext) drawPaths (drawType drawType, paths ...*draw2d.Path) { } else { svgPaths[i].Stroke = "none" } - if drawType & filled == filled { + if drawType&filled == filled { svgPaths[i].Fill = toSvgRGBA(gc.Current.FillColor) } else { svgPaths[i].Fill = "none" @@ -94,57 +96,70 @@ func (gc *GraphicContext) drawPaths (drawType drawType, paths ...*draw2d.Path) { }) } -func toSvgRGBA (c color.Color) string { // TODO move elsewhere +func toSvgRGBA(c color.Color) string { // TODO move elsewhere r, g, b, a := c.RGBA() return fmt.Sprintf("rgba(%v, %v, %v, %.3f)", r>>8, g>>8, b>>8, float64(a>>8)/255) } -func toSvgLength (l float64) string { +func toSvgLength(l float64) string { return fmt.Sprintf("%.4f", l) } -func toSvgPathDesc (p *draw2d.Path) string { // TODO move elsewhere +func toSvgPathDesc(p *draw2d.Path) string { // TODO move elsewhere parts := make([]string, len(p.Components)) - i := 0 - for j, cmp := range p.Components { + ps := p.Points + for i, cmp := range p.Components { switch cmp { case draw2d.MoveToCmp: - parts[j] = fmt.Sprintf("M %v %v", p.Points[i], p.Points[i+1]) - i += 2 + parts[i] = fmt.Sprintf("M %.4f,%.4f", ps[0], ps[1]) + ps = ps[2:] case draw2d.LineToCmp: - parts[j] = fmt.Sprintf("L %v %v", p.Points[i], p.Points[i+1]) - i += 2 + parts[i] = fmt.Sprintf("L %.4f,%.4f", ps[0], ps[1]) + ps = ps[2:] case draw2d.QuadCurveToCmp: - parts[j] = fmt.Sprintf("Q %v %v %v %v", p.Points[i], p.Points[i+1], p.Points[i+2], p.Points[i+3]) - i += 4 + parts[i] = fmt.Sprintf("Q %.4f,%.4f %.4f,%.4f", ps[0], ps[1], ps[2], ps[3]) + ps = ps[4:] case draw2d.CubicCurveToCmp: - parts[j] = fmt.Sprintf("C %v %v %v %v %v %v", p.Points[i], p.Points[i+1], p.Points[i+2], p.Points[i+3], p.Points[i+4], p.Points[i+5]) - i += 6 + parts[i] = fmt.Sprintf("C %.4f,%.4f %.4f,%.4f %.4f,%.4f", ps[0], ps[1], ps[2], ps[3], ps[4], ps[5]) + ps = ps[6:] case draw2d.ArcToCmp: + cx, cy := ps[0], ps[1] // center + rx, ry := ps[2], ps[3] // radii + fi := ps[4] + ps[5] // startAngle + angle + + // compute endpoint + sinfi, cosfi := math.Sincos(fi) + nom := math.Hypot(ry*cosfi, rx*sinfi) + x := cx + (rx*ry*cosfi)/nom + y := cy + (rx*ry*sinfi)/nom + x += 0.001 // dirty hack to ensure whole arc is drawn if start point equals endpoint + y += 0.001 + + // compute large and sweep flags large := 0 sweep := 0 - if p.Points[i+4] - p.Points[i+5] > 0 { // TODO this is probably not correct + if math.Abs(ps[5]) > math.Pi { large = 1 + } + if !math.Signbit(ps[5]) { sweep = 1 } + // rx ry x-axis-rotation large-arc-flag sweep-flag x y - parts[j] = fmt.Sprintf("A %v %v %v %v %v %v %v", - p.Points[i+2], // rx - p.Points[i+3], // ry - 0, // x-axis-rotation - large, // large-arc-flag - sweep, // // sweep-flag - p.Points[i], // x // TODO this is center of arc not an endpont - p.Points[i+1], // y // TODO -//- + parts[i] = fmt.Sprintf("A %.4f %.4f %v %v %v %.4f %.4f", + rx, ry, + 0, + large, sweep, + x, y, ) - i += 6 + ps = ps[6:] case draw2d.CloseCmp: - parts[j] = "Z" + parts[i] = "Z" } } - println("parts", parts) return strings.Join(parts, " ") } + /////////////////////////////////////// // TODO implement following methods (or remove if not neccesary) @@ -152,58 +167,72 @@ func toSvgPathDesc (p *draw2d.Path) string { // TODO move elsewhere func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) { } + // GetFontData gets the current FontData func (gc *GraphicContext) GetFontData() draw2d.FontData { return draw2d.FontData{} } + // GetFontName gets the current FontData as a string func (gc *GraphicContext) GetFontName() string { return "" } + // DrawImage draws the raster image in the current canvas func (gc *GraphicContext) DrawImage(image image.Image) { } + // Save the context and push it to the context stack func (gc *GraphicContext) Save() { } + // Restore remove the current context and restore the last one func (gc *GraphicContext) Restore() { } + // ClearRect fills the specified rectangle with a default transparent color func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int) { } + // SetDPI sets the current DPI func (gc *GraphicContext) SetDPI(dpi int) { } + // GetDPI gets the current DPI func (gc *GraphicContext) GetDPI() int { return 0 } + // GetStringBounds gets pixel bounds(dimensions) of given string func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) { return 0, 0, 0, 0 } + // CreateStringPath creates a path from the string s at x, y func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor float64) { return 0 } + // FillString draws the text at point (0, 0) func (gc *GraphicContext) FillString(text string) (cursor float64) { return 0 } + // FillStringAt draws the text at the specified point (x, y) func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) { return 0 } + // StrokeString draws the contour of the text at point (0, 0) func (gc *GraphicContext) StrokeString(text string) (cursor float64) { return 0 } + // StrokeStringAt draws the contour of the text at point (x, y) func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) { return 0