Initial commit for svg context

Satisfy draw2dc.GrapghicContext using empty methods
This commit is contained in:
Drahoslav 2017-12-16 19:26:34 +01:00
parent c41aa97d30
commit 96883adea4
4 changed files with 248 additions and 0 deletions

11
draw2dsvg/doc.go Normal file
View File

@ -0,0 +1,11 @@
// Copyright 2015 The draw2d Authors. All rights reserved.
// created: 16/12/2017 by Drahoslav Bednář
// Package draw2svg provides a graphic context that can draw
// vector graphics and text on svg file using the svgo package.
//
// Quick Start
// The following Go code geneartes a simple drawing and saves it
// to a svg document:
// // TODO
package draw2dsvg

24
draw2dsvg/fileutil.go Normal file
View File

@ -0,0 +1,24 @@
package draw2dsvg
import (
"os"
"bytes"
"errors"
svgo "github.com/ajstarks/svgo/float"
)
func SaveToSvgFile(filePath string, svg *svgo.SVG) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
if b, ok := svg.Writer.(*bytes.Buffer); ok {
bytes.NewBuffer(b.Bytes()).WriteTo(f) // clone buffer to make multiple writes possible
} else {
return errors.New("Svg has not been not created from with NewSvg (dow not have byte.Buffer as its Writer)")
}
return nil
}

182
draw2dsvg/gc.go Normal file
View File

@ -0,0 +1,182 @@
// Copyright 2015 The draw2d Authors. All rights reserved.
// created: 16/12/2017 by Drahoslav Bednář
package draw2dsvg
import (
"image"
"bytes"
"image/color"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dbase"
svgo "github.com/ajstarks/svgo/float"
)
const (
)
var (
)
func NewSvg() *svgo.SVG {
return svgo.New(&bytes.Buffer{})
}
// GraphicContext implements the draw2d.GraphicContext interface
// It provides draw2d with a svg backend (based on svgo)
type GraphicContext struct {
*draw2dbase.StackGraphicContext
svg *svgo.SVG
}
func NewGraphicContext(svg *svgo.SVG) *GraphicContext {
gc := &GraphicContext{draw2dbase.NewStackGraphicContext(), svg}
return gc
}
// TODO implement all following methods
// BeginPath creates a new path
func (gc *GraphicContext) BeginPath() {
}
// GetPath copies the current path, then returns it
func (gc *GraphicContext) GetPath() draw2d.Path {
return draw2d.Path{}
}
// GetMatrixTransform returns the current transformation matrix
func (gc *GraphicContext) GetMatrixTransform() draw2d.Matrix {
return draw2d.Matrix{}
}
// SetMatrixTransform sets the current transformation matrix
func (gc *GraphicContext) SetMatrixTransform(tr draw2d.Matrix) {
}
// ComposeMatrixTransform composes the current transformation matrix with tr
func (gc *GraphicContext) ComposeMatrixTransform(tr draw2d.Matrix) {
}
// Rotate applies a rotation to the current transformation matrix. angle is in radian.
func (gc *GraphicContext) Rotate(angle float64) {
}
// Translate applies a translation to the current transformation matrix.
func (gc *GraphicContext) Translate(tx, ty float64) {
}
// Scale applies a scale to the current transformation matrix.
func (gc *GraphicContext) Scale(sx, sy float64) {
}
// SetStrokeColor sets the current stroke color
func (gc *GraphicContext) SetStrokeColor(c color.Color) {
}
// SetFillColor sets the current fill color
func (gc *GraphicContext) SetFillColor(c color.Color) {
}
// SetFillRule sets the current fill rule
func (gc *GraphicContext) SetFillRule(f draw2d.FillRule) {
}
// SetLineWidth sets the current line width
func (gc *GraphicContext) SetLineWidth(lineWidth float64) {
}
// SetLineCap sets the current line cap
func (gc *GraphicContext) SetLineCap(cap draw2d.LineCap) {
}
// SetLineJoin sets the current line join
func (gc *GraphicContext) SetLineJoin(join draw2d.LineJoin) {
}
// SetLineDash sets the current dash
func (gc *GraphicContext) SetLineDash(dash []float64, dashOffset float64) {
}
// SetFontSize sets the current font size
func (gc *GraphicContext) SetFontSize(fontSize float64) {
}
// GetFontSize gets the current font size
func (gc *GraphicContext) GetFontSize() float64 {
return 0
}
// SetFontData sets the current FontData
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() {
}
// Clear fills the current canvas with a default transparent color
func (gc *GraphicContext) Clear() {
}
// 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
}
// Stroke strokes the paths with the color specified by SetStrokeColor
func (gc *GraphicContext) Stroke(paths ...*draw2d.Path) {
}
// Fill fills the paths with the color specified by SetFillColor
func (gc *GraphicContext) Fill(paths ...*draw2d.Path) {
}
// FillStroke first fills the paths and than strokes them
func (gc *GraphicContext) FillStroke(paths ...*draw2d.Path) {
}

31
draw2dsvg/test_test.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright 2015 The draw2d Authors. All rights reserved.
// created: 16/12/2017 by Drahoslav Bednář
// Package draw2dsvg_test gives test coverage with the command:
// go test -cover ./... | grep -v "no test"
// (It should be run from its parent draw2d directory.)
package draw2dsvg_test
import (
"testing"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dsvg"
)
type sample func(gc draw2d.GraphicContext, ext string) (string, error)
func test(t *testing.T, draw sample) {
// Initialize the graphic context on an pdf document
dest := draw2dsvg.NewSvg()
gc := draw2dsvg.NewGraphicContext(dest)
// Draw sample
output, err := draw(gc, "svg")
if err != nil {
t.Errorf("Drawing %q failed: %v", output, err)
return
}
err = draw2dsvg.SaveToSvgFile(output, dest)
if err != nil {
t.Errorf("Saving %q failed: %v", output, err)
}
}