| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"math" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"golang.org/x/image/font" |
| 11 |
|
| 12 |
"github.com/golang/freetype/truetype" |
| 13 |
"github.com/wcharczuk/go-chart/v2/drawing" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
func SVG(width, height int) (Renderer, error) { |
| 18 |
buffer := bytes.NewBuffer([]byte{}) |
| 19 |
canvas := newCanvas(buffer) |
| 20 |
canvas.Start(width, height) |
| 21 |
return &vectorRenderer{ |
| 22 |
b: buffer, |
| 23 |
c: canvas, |
| 24 |
s: &Style{}, |
| 25 |
p: []string{}, |
| 26 |
dpi: DefaultDPI, |
| 27 |
}, nil |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func SVGWithCSS(css string, nonce string) func(width, height int) (Renderer, error) { |
| 33 |
return func(width, height int) (Renderer, error) { |
| 34 |
buffer := bytes.NewBuffer([]byte{}) |
| 35 |
canvas := newCanvas(buffer) |
| 36 |
canvas.css = css |
| 37 |
canvas.nonce = nonce |
| 38 |
canvas.Start(width, height) |
| 39 |
return &vectorRenderer{ |
| 40 |
b: buffer, |
| 41 |
c: canvas, |
| 42 |
s: &Style{}, |
| 43 |
p: []string{}, |
| 44 |
dpi: DefaultDPI, |
| 45 |
}, nil |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
type vectorRenderer struct { |
| 51 |
dpi float64 |
| 52 |
b *bytes.Buffer |
| 53 |
c *canvas |
| 54 |
s *Style |
| 55 |
p []string |
| 56 |
fc *font.Drawer |
| 57 |
} |
| 58 |
|
| 59 |
func (vr *vectorRenderer) ResetStyle() { |
| 60 |
vr.s = &Style{Font: vr.s.Font} |
| 61 |
vr.fc = nil |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func (vr *vectorRenderer) GetDPI() float64 { |
| 66 |
return vr.dpi |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (vr *vectorRenderer) SetDPI(dpi float64) { |
| 71 |
vr.dpi = dpi |
| 72 |
vr.c.dpi = dpi |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (vr *vectorRenderer) SetClassName(classname string) { |
| 77 |
vr.s.ClassName = classname |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func (vr *vectorRenderer) SetStrokeColor(c drawing.Color) { |
| 82 |
vr.s.StrokeColor = c |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (vr *vectorRenderer) SetFillColor(c drawing.Color) { |
| 87 |
vr.s.FillColor = c |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (vr *vectorRenderer) SetStrokeWidth(width float64) { |
| 92 |
vr.s.StrokeWidth = width |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (vr *vectorRenderer) SetStrokeDashArray(dashArray []float64) { |
| 97 |
vr.s.StrokeDashArray = dashArray |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (vr *vectorRenderer) MoveTo(x, y int) { |
| 102 |
vr.p = append(vr.p, fmt.Sprintf("M %d %d", x, y)) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func (vr *vectorRenderer) LineTo(x, y int) { |
| 107 |
vr.p = append(vr.p, fmt.Sprintf("L %d %d", x, y)) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func (vr *vectorRenderer) QuadCurveTo(cx, cy, x, y int) { |
| 112 |
vr.p = append(vr.p, fmt.Sprintf("Q%d,%d %d,%d", cx, cy, x, y)) |
| 113 |
} |
| 114 |
|
| 115 |
func (vr *vectorRenderer) ArcTo(cx, cy int, rx, ry, startAngle, delta float64) { |
| 116 |
startAngle = RadianAdd(startAngle, _pi2) |
| 117 |
endAngle := RadianAdd(startAngle, delta) |
| 118 |
|
| 119 |
startx := cx + int(rx*math.Sin(startAngle)) |
| 120 |
starty := cy - int(ry*math.Cos(startAngle)) |
| 121 |
|
| 122 |
if len(vr.p) > 0 { |
| 123 |
vr.p = append(vr.p, fmt.Sprintf("L %d %d", startx, starty)) |
| 124 |
} else { |
| 125 |
vr.p = append(vr.p, fmt.Sprintf("M %d %d", startx, starty)) |
| 126 |
} |
| 127 |
|
| 128 |
endx := cx + int(rx*math.Sin(endAngle)) |
| 129 |
endy := cy - int(ry*math.Cos(endAngle)) |
| 130 |
|
| 131 |
dd := RadiansToDegrees(delta) |
| 132 |
|
| 133 |
largeArcFlag := 0 |
| 134 |
if delta > _pi { |
| 135 |
largeArcFlag = 1 |
| 136 |
} |
| 137 |
|
| 138 |
vr.p = append(vr.p, fmt.Sprintf("A %d %d %0.2f %d 1 %d %d", int(rx), int(ry), dd, largeArcFlag, endx, endy)) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (vr *vectorRenderer) Close() { |
| 143 |
vr.p = append(vr.p, "Z") |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func (vr *vectorRenderer) Stroke() { |
| 148 |
vr.drawPath(vr.s.GetStrokeOptions()) |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func (vr *vectorRenderer) Fill() { |
| 153 |
vr.drawPath(vr.s.GetFillOptions()) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func (vr *vectorRenderer) FillStroke() { |
| 158 |
vr.drawPath(vr.s.GetFillAndStrokeOptions()) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func (vr *vectorRenderer) drawPath(s Style) { |
| 163 |
vr.c.Path(strings.Join(vr.p, "\n"), vr.s.GetFillAndStrokeOptions()) |
| 164 |
vr.p = []string{} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func (vr *vectorRenderer) Circle(radius float64, x, y int) { |
| 169 |
vr.c.Circle(x, y, int(radius), vr.s.GetFillAndStrokeOptions()) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (vr *vectorRenderer) SetFont(f *truetype.Font) { |
| 174 |
vr.s.Font = f |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func (vr *vectorRenderer) SetFontColor(c drawing.Color) { |
| 179 |
vr.s.FontColor = c |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
func (vr *vectorRenderer) SetFontSize(size float64) { |
| 184 |
vr.s.FontSize = size |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func (vr *vectorRenderer) Text(body string, x, y int) { |
| 189 |
vr.c.Text(x, y, body, vr.s.GetTextOptions()) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func (vr *vectorRenderer) MeasureText(body string) (box Box) { |
| 194 |
if vr.s.GetFont() != nil { |
| 195 |
vr.fc = &font.Drawer{ |
| 196 |
Face: truetype.NewFace(vr.s.GetFont(), &truetype.Options{ |
| 197 |
DPI: vr.dpi, |
| 198 |
Size: vr.s.FontSize, |
| 199 |
}), |
| 200 |
} |
| 201 |
w := vr.fc.MeasureString(body).Ceil() |
| 202 |
|
| 203 |
box.Right = w |
| 204 |
box.Bottom = int(drawing.PointsToPixels(vr.dpi, vr.s.FontSize)) |
| 205 |
if vr.c.textTheta == nil { |
| 206 |
return |
| 207 |
} |
| 208 |
box = box.Corners().Rotate(RadiansToDegrees(*vr.c.textTheta)).Box() |
| 209 |
} |
| 210 |
return |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func (vr *vectorRenderer) SetTextRotation(radians float64) { |
| 215 |
vr.c.textTheta = &radians |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
func (vr *vectorRenderer) ClearTextRotation() { |
| 220 |
vr.c.textTheta = nil |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
func (vr *vectorRenderer) Save(w io.Writer) error { |
| 225 |
vr.c.End() |
| 226 |
_, err := w.Write(vr.b.Bytes()) |
| 227 |
return err |
| 228 |
} |
| 229 |
|
| 230 |
func newCanvas(w io.Writer) *canvas { |
| 231 |
return &canvas{ |
| 232 |
w: w, |
| 233 |
dpi: DefaultDPI, |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
type canvas struct { |
| 238 |
w io.Writer |
| 239 |
dpi float64 |
| 240 |
textTheta *float64 |
| 241 |
width int |
| 242 |
height int |
| 243 |
css string |
| 244 |
nonce string |
| 245 |
} |
| 246 |
|
| 247 |
func (c *canvas) Start(width, height int) { |
| 248 |
c.width = width |
| 249 |
c.height = height |
| 250 |
c.w.Write([]byte(fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 %d %d">`, c.width, c.height))) |
| 251 |
if c.css != "" { |
| 252 |
c.w.Write([]byte(`<style type="text/css"`)) |
| 253 |
if c.nonce != "" { |
| 254 |
|
| 255 |
c.w.Write([]byte(fmt.Sprintf(` nonce="%s"`, c.nonce))) |
| 256 |
} |
| 257 |
|
| 258 |
c.w.Write([]byte(fmt.Sprintf(`><![CDATA[%s]]></style>`, c.css))) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func (c *canvas) Path(d string, style Style) { |
| 263 |
var strokeDashArrayProperty string |
| 264 |
if len(style.StrokeDashArray) > 0 { |
| 265 |
strokeDashArrayProperty = c.getStrokeDashArray(style) |
| 266 |
} |
| 267 |
c.w.Write([]byte(fmt.Sprintf(`<path %s d="%s" %s/>`, strokeDashArrayProperty, d, c.styleAsSVG(style)))) |
| 268 |
} |
| 269 |
|
| 270 |
func (c *canvas) Text(x, y int, body string, style Style) { |
| 271 |
if c.textTheta == nil { |
| 272 |
c.w.Write([]byte(fmt.Sprintf(`<text x="%d" y="%d" %s>%s</text>`, x, y, c.styleAsSVG(style), body))) |
| 273 |
} else { |
| 274 |
transform := fmt.Sprintf(` transform="rotate(%0.2f,%d,%d)"`, RadiansToDegrees(*c.textTheta), x, y) |
| 275 |
c.w.Write([]byte(fmt.Sprintf(`<text x="%d" y="%d" %s%s>%s</text>`, x, y, c.styleAsSVG(style), transform, body))) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func (c *canvas) Circle(x, y, r int, style Style) { |
| 280 |
c.w.Write([]byte(fmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" %s/>`, x, y, r, c.styleAsSVG(style)))) |
| 281 |
} |
| 282 |
|
| 283 |
func (c *canvas) End() { |
| 284 |
c.w.Write([]byte("</svg>")) |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
func (c *canvas) getStrokeDashArray(s Style) string { |
| 289 |
if len(s.StrokeDashArray) > 0 { |
| 290 |
var values []string |
| 291 |
for _, v := range s.StrokeDashArray { |
| 292 |
values = append(values, fmt.Sprintf("%0.1f", v)) |
| 293 |
} |
| 294 |
return "stroke-dasharray=\"" + strings.Join(values, ", ") + "\"" |
| 295 |
} |
| 296 |
return "" |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
func (c *canvas) getFontFace(s Style) string { |
| 301 |
family := "sans-serif" |
| 302 |
if s.GetFont() != nil { |
| 303 |
name := s.GetFont().Name(truetype.NameIDFontFamily) |
| 304 |
if len(name) != 0 { |
| 305 |
family = fmt.Sprintf(`'%s',%s`, name, family) |
| 306 |
} |
| 307 |
} |
| 308 |
return fmt.Sprintf("font-family:%s", family) |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
func (c *canvas) styleAsSVG(s Style) string { |
| 313 |
sw := s.StrokeWidth |
| 314 |
sc := s.StrokeColor |
| 315 |
fc := s.FillColor |
| 316 |
fs := s.FontSize |
| 317 |
fnc := s.FontColor |
| 318 |
|
| 319 |
if s.ClassName != "" { |
| 320 |
var classes []string |
| 321 |
classes = append(classes, s.ClassName) |
| 322 |
if !sc.IsZero() { |
| 323 |
classes = append(classes, "stroke") |
| 324 |
} |
| 325 |
if !fc.IsZero() { |
| 326 |
classes = append(classes, "fill") |
| 327 |
} |
| 328 |
if fs != 0 || s.Font != nil { |
| 329 |
classes = append(classes, "text") |
| 330 |
} |
| 331 |
|
| 332 |
return fmt.Sprintf("class=\"%s\"", strings.Join(classes, " ")) |
| 333 |
} |
| 334 |
|
| 335 |
var pieces []string |
| 336 |
|
| 337 |
if sw != 0 { |
| 338 |
pieces = append(pieces, "stroke-width:"+fmt.Sprintf("%d", int(sw))) |
| 339 |
} else { |
| 340 |
pieces = append(pieces, "stroke-width:0") |
| 341 |
} |
| 342 |
|
| 343 |
if !sc.IsZero() { |
| 344 |
pieces = append(pieces, "stroke:"+sc.String()) |
| 345 |
} else { |
| 346 |
pieces = append(pieces, "stroke:none") |
| 347 |
} |
| 348 |
|
| 349 |
if !fnc.IsZero() { |
| 350 |
pieces = append(pieces, "fill:"+fnc.String()) |
| 351 |
} else if !fc.IsZero() { |
| 352 |
pieces = append(pieces, "fill:"+fc.String()) |
| 353 |
} else { |
| 354 |
pieces = append(pieces, "fill:none") |
| 355 |
} |
| 356 |
|
| 357 |
if fs != 0 { |
| 358 |
pieces = append(pieces, "font-size:"+fmt.Sprintf("%.1fpx", drawing.PointsToPixels(c.dpi, fs))) |
| 359 |
} |
| 360 |
|
| 361 |
if s.Font != nil { |
| 362 |
pieces = append(pieces, c.getFontFace(s)) |
| 363 |
} |
| 364 |
return fmt.Sprintf("style=\"%s\"", strings.Join(pieces, ";")) |
| 365 |
} |