removing 3rd party deps.
This commit is contained in:
parent
7f36c08fbf
commit
8bc8b1087c
27 changed files with 1995 additions and 54 deletions
|
|
@ -5,37 +5,44 @@ import (
|
|||
"fmt"
|
||||
"image/color"
|
||||
"io"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/image/font"
|
||||
|
||||
"github.com/ajstarks/svgo"
|
||||
"github.com/golang/freetype/truetype"
|
||||
)
|
||||
|
||||
// SVG returns a new png/raster renderer.
|
||||
func SVG(width, height int) Renderer {
|
||||
func SVG(width, height int) (Renderer, error) {
|
||||
buffer := bytes.NewBuffer([]byte{})
|
||||
canvas := svg.New(buffer)
|
||||
canvas := newCanvas(buffer)
|
||||
canvas.Start(width, height)
|
||||
return &vectorRenderer{
|
||||
b: buffer,
|
||||
c: canvas,
|
||||
s: &Style{},
|
||||
p: []string{},
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// vectorRenderer renders chart commands to a bitmap.
|
||||
type vectorRenderer struct {
|
||||
b *bytes.Buffer
|
||||
c *svg.SVG
|
||||
s *Style
|
||||
f *truetype.Font
|
||||
p []string
|
||||
fc *font.Drawer
|
||||
dpi float64
|
||||
b *bytes.Buffer
|
||||
c *canvas
|
||||
s *Style
|
||||
f *truetype.Font
|
||||
p []string
|
||||
fc *font.Drawer
|
||||
}
|
||||
|
||||
// SetDPI implements the interface method.
|
||||
func (vr *vectorRenderer) SetDPI(dpi float64) {
|
||||
vr.dpi = dpi
|
||||
}
|
||||
|
||||
// SetStrokeColor implements the interface method.
|
||||
func (vr *vectorRenderer) SetStrokeColor(c color.RGBA) {
|
||||
vr.s.StrokeColor = c
|
||||
}
|
||||
|
|
@ -130,20 +137,18 @@ func (vr *vectorRenderer) Text(body string, x, y int) {
|
|||
}
|
||||
|
||||
// MeasureText uses the truetype font drawer to measure the width of text.
|
||||
func (vr *vectorRenderer) MeasureText(body string) int {
|
||||
if vr.fc == nil && vr.f != nil {
|
||||
func (vr *vectorRenderer) MeasureText(body string) (width, height int) {
|
||||
if vr.f != nil {
|
||||
vr.fc = &font.Drawer{
|
||||
Face: truetype.NewFace(vr.f, &truetype.Options{
|
||||
DPI: DefaultDPI,
|
||||
DPI: vr.dpi,
|
||||
Size: vr.s.FontSize,
|
||||
}),
|
||||
}
|
||||
width = vr.fc.MeasureString(body).Ceil()
|
||||
height = int(math.Ceil(vr.s.FontSize))
|
||||
}
|
||||
if vr.fc != nil {
|
||||
dimensions := vr.fc.MeasureString(body)
|
||||
return dimensions.Floor()
|
||||
}
|
||||
return 0
|
||||
return
|
||||
}
|
||||
|
||||
func (vr *vectorRenderer) Save(w io.Writer) error {
|
||||
|
|
@ -151,3 +156,49 @@ func (vr *vectorRenderer) Save(w io.Writer) error {
|
|||
_, err := w.Write(vr.b.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
func newCanvas(w io.Writer) *canvas {
|
||||
return &canvas{
|
||||
w: w,
|
||||
}
|
||||
}
|
||||
|
||||
type canvas struct {
|
||||
w io.Writer
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
func (c *canvas) Start(width, height int) {
|
||||
c.width = width
|
||||
c.height = height
|
||||
c.w.Write([]byte(fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%d" height="%d">\n`, c.width, c.height)))
|
||||
}
|
||||
|
||||
func (c *canvas) Path(d string, style ...string) {
|
||||
if len(style) > 0 {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<path d="%s" style="%s"/>\n`, d, style[0])))
|
||||
} else {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<path d="%s"/>\n`, d)))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *canvas) Text(x, y int, body string, style ...string) {
|
||||
if len(style) > 0 {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<text x="%d" y="%d" style="%s">%s</text>`, x, y, style[0], body)))
|
||||
} else {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<text x="%d" y="%d">%s</text>`, x, y, body)))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *canvas) Circle(x, y, r int, style ...string) {
|
||||
if len(style) > 0 {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" style="%s">`, x, y, r, style[0])))
|
||||
} else {
|
||||
c.w.Write([]byte(fmt.Sprintf(`<circle cx="%d" cy="%d" r="%d">`, x, y, r)))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *canvas) End() {
|
||||
c.w.Write([]byte("</svg>"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue