svg_icon.go 1.2 KB

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