svg.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package svg
  2. import "encoding/xml"
  3. import "fmt"
  4. type CoreAttributes struct {
  5. Chardata string `xml:",chardata"`
  6. ID string `xml:"id,attr"`
  7. }
  8. type Stop struct {
  9. CoreAttributes
  10. Style Style `xml:"style,attr"`
  11. Offset string `xml:"offset,attr"`
  12. }
  13. type LinearGradient struct {
  14. CoreAttributes
  15. Href string `xml:"href,attr"`
  16. X1 string `xml:"x1,attr"`
  17. Y1 string `xml:"y1,attr"`
  18. X2 string `xml:"x2,attr"`
  19. Y2 string `xml:"y2,attr"`
  20. GradientUnits string `xml:"gradientUnits,attr"`
  21. Stop []Stop `xml:"stop"`
  22. }
  23. type RadialGradient struct {
  24. CoreAttributes
  25. Href string `xml:"href,attr"`
  26. Cx string `xml:"cx,attr"`
  27. Cy string `xml:"cy,attr"`
  28. Fx string `xml:"fx,attr"`
  29. Fy string `xml:"fy,attr"`
  30. R string `xml:"r,attr"`
  31. GradientTransform string `xml:"gradientTransform,attr"`
  32. GradientUnits string `xml:"gradientUnits,attr"`
  33. Stop []Stop `xml:"stop"`
  34. }
  35. type Defs struct {
  36. CoreAttributes
  37. LinearGradient []LinearGradient `xml:"linearGradient"`
  38. RadialGradient []RadialGradient `xml:"radialGradient"`
  39. Drawable []Drawable
  40. }
  41. type Ellipse struct {
  42. CoreAttributes
  43. Style string `xml:"style,attr"`
  44. Cx string `xml:"cx,attr"`
  45. Cy string `xml:"cy,attr"`
  46. Rx string `xml:"rx,attr"`
  47. Ry string `xml:"ry,attr"`
  48. }
  49. type Text struct {
  50. CoreAttributes
  51. Space string `xml:"space,attr"`
  52. Style string `xml:"style,attr"`
  53. X string `xml:"x,attr"`
  54. Y string `xml:"y,attr"`
  55. Tspan struct {
  56. CoreAttributes
  57. Style string `xml:"style,attr"`
  58. X string `xml:"x,attr"`
  59. Y string `xml:"y,attr"`
  60. } `xml:"tspan"`
  61. }
  62. type Rect struct {
  63. Text string `xml:",chardata"`
  64. Style string `xml:"style,attr"`
  65. ID string `xml:"id,attr"`
  66. Width string `xml:"width,attr"`
  67. Height string `xml:"height,attr"`
  68. X string `xml:"x,attr"`
  69. Y string `xml:"y,attr"`
  70. }
  71. type Drawable struct {
  72. *Rect
  73. *Ellipse
  74. *Text
  75. }
  76. func (d *Drawable) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
  77. var err error
  78. switch start.Name.Local {
  79. case "rect":
  80. r := Rect{}
  81. err = dec.DecodeElement(&r, &start)
  82. d.Rect = &r
  83. case "ellipse":
  84. r := Ellipse{}
  85. err = dec.DecodeElement(&r, &start)
  86. d.Ellipse = &r
  87. case "text":
  88. r := Text{}
  89. err = dec.DecodeElement(&r, &start)
  90. d.Text = &r
  91. default:
  92. return fmt.Errorf("drawable not supported: %s", start)
  93. }
  94. return err
  95. }
  96. type G struct {
  97. CoreAttributes
  98. Drawable []Drawable `xml:",any"`
  99. }
  100. type SVG struct {
  101. CoreAttributes
  102. XMLName xml.Name `xml:"svg"`
  103. Width Length `xml:"width,attr"`
  104. Height Length `xml:"height,attr"`
  105. ViewBox string `xml:"viewBox,attr"`
  106. Version string `xml:"version,attr"`
  107. Xlink string `xml:"xlink,attr"`
  108. Xmlns string `xml:"xmlns,attr"`
  109. SVG string `xml:"svg,attr"`
  110. Defs []Defs `xml:"defs"`
  111. G G `xml:"g"`
  112. }