2017-12-16 18:26:34 +00:00
|
|
|
// Copyright 2015 The draw2d Authors. All rights reserved.
|
|
|
|
// created: 16/12/2017 by Drahoslav Bednář
|
|
|
|
|
|
|
|
package draw2dsvg
|
|
|
|
|
|
|
|
import (
|
2017-12-24 11:38:59 +00:00
|
|
|
"bytes"
|
2017-12-22 21:40:56 +00:00
|
|
|
"fmt"
|
2017-12-24 11:38:59 +00:00
|
|
|
"github.com/llgcode/draw2d"
|
|
|
|
"github.com/llgcode/draw2d/draw2dbase"
|
2017-12-16 18:26:34 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
2017-12-24 11:38:59 +00:00
|
|
|
"math"
|
2017-12-22 21:40:56 +00:00
|
|
|
"strings"
|
2017-12-16 18:26:34 +00:00
|
|
|
)
|
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
const ()
|
2017-12-16 18:26:34 +00:00
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
var ()
|
2017-12-16 18:26:34 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
type drawType int
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
const (
|
|
|
|
filled drawType = 1 << iota
|
|
|
|
stroked
|
|
|
|
)
|
|
|
|
|
2017-12-21 14:52:26 +00:00
|
|
|
type SVG bytes.Buffer
|
|
|
|
|
2017-12-21 17:18:29 +00:00
|
|
|
func NewSvg() *Svg {
|
|
|
|
return &Svg{Xmlns: "http://www.w3.org/2000/svg"}
|
2017-12-16 18:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphicContext implements the draw2d.GraphicContext interface
|
2017-12-21 14:52:26 +00:00
|
|
|
// It provides draw2d with a svg backend
|
2017-12-16 18:26:34 +00:00
|
|
|
type GraphicContext struct {
|
|
|
|
*draw2dbase.StackGraphicContext
|
2017-12-21 17:18:29 +00:00
|
|
|
svg *Svg
|
2017-12-16 18:26:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 17:18:29 +00:00
|
|
|
func NewGraphicContext(svg *Svg) *GraphicContext {
|
2017-12-16 18:26:34 +00:00
|
|
|
gc := &GraphicContext{draw2dbase.NewStackGraphicContext(), svg}
|
|
|
|
return gc
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
// Clear fills the current canvas with a default transparent color
|
|
|
|
func (gc *GraphicContext) Clear() {
|
|
|
|
gc.svg.Groups = nil
|
|
|
|
gc.svg.Groups = append(gc.svg.Groups, Group{
|
2017-12-24 11:38:59 +00:00
|
|
|
// TODO add background color?
|
2017-12-22 21:40:56 +00:00
|
|
|
})
|
2017-12-16 18:26:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
// Stroke strokes the paths with the color specified by SetStrokeColor
|
|
|
|
func (gc *GraphicContext) Stroke(paths ...*draw2d.Path) {
|
|
|
|
gc.drawPaths(stroked, paths...)
|
|
|
|
gc.Current.Path.Clear()
|
2017-12-16 18:26:34 +00:00
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
// 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()
|
2017-12-16 18:26:34 +00:00
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
// FillStroke first fills the paths and than strokes them
|
|
|
|
func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) {
|
2017-12-24 11:38:59 +00:00
|
|
|
gc.drawPaths(filled|stroked, paths...)
|
2017-12-22 21:40:56 +00:00
|
|
|
gc.Current.Path.Clear()
|
|
|
|
}
|
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
func (gc *GraphicContext) drawPaths(drawType drawType, paths ...*draw2d.Path) {
|
2017-12-22 21:40:56 +00:00
|
|
|
paths = append(paths, gc.Current.Path)
|
|
|
|
|
|
|
|
svgPaths := make([]Path, len(paths))
|
|
|
|
|
|
|
|
for i, path := range paths {
|
|
|
|
svgPaths[i].Desc = toSvgPathDesc(path)
|
2017-12-24 11:38:59 +00:00
|
|
|
if drawType&stroked == stroked {
|
2017-12-22 21:40:56 +00:00
|
|
|
svgPaths[i].Stroke = toSvgRGBA(gc.Current.StrokeColor)
|
2017-12-24 11:32:29 +00:00
|
|
|
svgPaths[i].StrokeWidth = toSvgLength(gc.Current.LineWidth)
|
|
|
|
svgPaths[i].StrokeLinecap = gc.Current.Cap.String()
|
|
|
|
svgPaths[i].StrokeLinejoin = gc.Current.Join.String()
|
|
|
|
} else {
|
|
|
|
svgPaths[i].Stroke = "none"
|
2017-12-22 21:40:56 +00:00
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
if drawType&filled == filled {
|
2017-12-22 21:40:56 +00:00
|
|
|
svgPaths[i].Fill = toSvgRGBA(gc.Current.FillColor)
|
2017-12-24 11:32:29 +00:00
|
|
|
} else {
|
|
|
|
svgPaths[i].Fill = "none"
|
2017-12-22 21:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gc.svg.Groups = append(gc.svg.Groups, Group{
|
|
|
|
Paths: svgPaths,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
func toSvgRGBA(c color.Color) string { // TODO move elsewhere
|
2017-12-22 21:40:56 +00:00
|
|
|
r, g, b, a := c.RGBA()
|
2017-12-24 11:32:29 +00:00
|
|
|
return fmt.Sprintf("rgba(%v, %v, %v, %.3f)", r>>8, g>>8, b>>8, float64(a>>8)/255)
|
|
|
|
}
|
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
func toSvgLength(l float64) string {
|
2017-12-24 11:32:29 +00:00
|
|
|
return fmt.Sprintf("%.4f", l)
|
2017-12-22 21:40:56 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 11:38:59 +00:00
|
|
|
func toSvgPathDesc(p *draw2d.Path) string { // TODO move elsewhere
|
2017-12-22 21:40:56 +00:00
|
|
|
parts := make([]string, len(p.Components))
|
2017-12-24 11:38:59 +00:00
|
|
|
ps := p.Points
|
|
|
|
for i, cmp := range p.Components {
|
2017-12-22 21:40:56 +00:00
|
|
|
switch cmp {
|
|
|
|
case draw2d.MoveToCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
parts[i] = fmt.Sprintf("M %.4f,%.4f", ps[0], ps[1])
|
|
|
|
ps = ps[2:]
|
2017-12-22 21:40:56 +00:00
|
|
|
case draw2d.LineToCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
parts[i] = fmt.Sprintf("L %.4f,%.4f", ps[0], ps[1])
|
|
|
|
ps = ps[2:]
|
2017-12-22 21:40:56 +00:00
|
|
|
case draw2d.QuadCurveToCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
parts[i] = fmt.Sprintf("Q %.4f,%.4f %.4f,%.4f", ps[0], ps[1], ps[2], ps[3])
|
|
|
|
ps = ps[4:]
|
2017-12-22 21:40:56 +00:00
|
|
|
case draw2d.CubicCurveToCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
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:]
|
2017-12-22 21:40:56 +00:00
|
|
|
case draw2d.ArcToCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
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
|
2017-12-22 21:40:56 +00:00
|
|
|
large := 0
|
|
|
|
sweep := 0
|
2017-12-24 11:38:59 +00:00
|
|
|
if math.Abs(ps[5]) > math.Pi {
|
2017-12-22 21:40:56 +00:00
|
|
|
large = 1
|
2017-12-24 11:38:59 +00:00
|
|
|
}
|
|
|
|
if !math.Signbit(ps[5]) {
|
2017-12-22 21:40:56 +00:00
|
|
|
sweep = 1
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
// rx ry x-axis-rotation large-arc-flag sweep-flag x y
|
2017-12-24 11:38:59 +00:00
|
|
|
parts[i] = fmt.Sprintf("A %.4f %.4f %v %v %v %.4f %.4f",
|
|
|
|
rx, ry,
|
|
|
|
0,
|
|
|
|
large, sweep,
|
|
|
|
x, y,
|
2017-12-22 21:40:56 +00:00
|
|
|
)
|
2017-12-24 11:38:59 +00:00
|
|
|
ps = ps[6:]
|
2017-12-22 21:40:56 +00:00
|
|
|
case draw2d.CloseCmp:
|
2017-12-24 11:38:59 +00:00
|
|
|
parts[i] = "Z"
|
2017-12-22 21:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(parts, " ")
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-22 21:40:56 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// TODO implement following methods (or remove if not neccesary)
|
2017-12-16 18:26:34 +00:00
|
|
|
|
|
|
|
// SetFontData sets the current FontData
|
|
|
|
func (gc *GraphicContext) SetFontData(fontData draw2d.FontData) {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// GetFontData gets the current FontData
|
|
|
|
func (gc *GraphicContext) GetFontData() draw2d.FontData {
|
|
|
|
return draw2d.FontData{}
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// GetFontName gets the current FontData as a string
|
|
|
|
func (gc *GraphicContext) GetFontName() string {
|
|
|
|
return ""
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// DrawImage draws the raster image in the current canvas
|
|
|
|
func (gc *GraphicContext) DrawImage(image image.Image) {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// Save the context and push it to the context stack
|
|
|
|
func (gc *GraphicContext) Save() {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// Restore remove the current context and restore the last one
|
|
|
|
func (gc *GraphicContext) Restore() {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// ClearRect fills the specified rectangle with a default transparent color
|
|
|
|
func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int) {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// SetDPI sets the current DPI
|
|
|
|
func (gc *GraphicContext) SetDPI(dpi int) {
|
|
|
|
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// GetDPI gets the current DPI
|
|
|
|
func (gc *GraphicContext) GetDPI() int {
|
|
|
|
return 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// GetStringBounds gets pixel bounds(dimensions) of given string
|
|
|
|
func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
|
|
|
|
return 0, 0, 0, 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// CreateStringPath creates a path from the string s at x, y
|
|
|
|
func (gc *GraphicContext) CreateStringPath(text string, x, y float64) (cursor float64) {
|
|
|
|
return 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// FillString draws the text at point (0, 0)
|
|
|
|
func (gc *GraphicContext) FillString(text string) (cursor float64) {
|
|
|
|
return 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// FillStringAt draws the text at the specified point (x, y)
|
|
|
|
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (cursor float64) {
|
|
|
|
return 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// StrokeString draws the contour of the text at point (0, 0)
|
|
|
|
func (gc *GraphicContext) StrokeString(text string) (cursor float64) {
|
|
|
|
return 0
|
|
|
|
}
|
2017-12-24 11:38:59 +00:00
|
|
|
|
2017-12-16 18:26:34 +00:00
|
|
|
// StrokeStringAt draws the contour of the text at point (x, y)
|
|
|
|
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64) {
|
|
|
|
return 0
|
|
|
|
}
|