-
Notifications
You must be signed in to change notification settings - Fork 38
/
svg_icon.go
37 lines (32 loc) · 1.17 KB
/
svg_icon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2017 The oksvg Authors. All rights reserved.
// created: 2/12/2017 by S.R.Wiley
//
// utils.go implements translation of an SVG2.0 path into a rasterx Path.
package oksvg
import (
"github.com/srwiley/rasterx"
)
// SvgIcon holds data from parsed SVGs.
type SvgIcon struct {
ViewBox struct{ X, Y, W, H float64 }
Titles []string // Title elements collect here
Descriptions []string // Description elements collect here
Grads map[string]*rasterx.Gradient
Defs map[string][]definition
SVGPaths []SvgPath
Transform rasterx.Matrix2D
classes map[string]styleAttribute
}
// Draw the compiled SVG icon into the GraphicContext.
// All elements should be contained by the Bounds rectangle of the SvgIcon.
func (s *SvgIcon) Draw(r *rasterx.Dasher, opacity float64) {
for _, svgp := range s.SVGPaths {
svgp.DrawTransformed(r, opacity, s.Transform)
}
}
// SetTarget sets the Transform matrix to draw within the bounds of the rectangle arguments
func (s *SvgIcon) SetTarget(x, y, w, h float64) {
scaleW := w / s.ViewBox.W
scaleH := h / s.ViewBox.H
s.Transform = rasterx.Identity.Translate(x-s.ViewBox.X, y-s.ViewBox.Y).Scale(scaleW, scaleH)
}