svg.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package svg
  2. import "encoding/xml"
  3. import "fmt"
  4. import "github.com/hajimehoshi/ebiten/v2"
  5. type CoreAttributes struct {
  6. Chardata string `xml:",chardata"`
  7. ID string `xml:"id,attr"`
  8. }
  9. type Stop struct {
  10. CoreAttributes
  11. Style Style `xml:"style,attr"`
  12. Offset string `xml:"offset,attr"`
  13. }
  14. type LinearGradient struct {
  15. CoreAttributes
  16. Href string `xml:"href,attr"`
  17. Linear
  18. GradientUnits string `xml:"gradientUnits,attr"`
  19. Stop []Stop `xml:"stop"`
  20. }
  21. type RadialGradient struct {
  22. CoreAttributes
  23. Href string `xml:"href,attr"`
  24. CxCy
  25. FxFy
  26. R string `xml:"r,attr"`
  27. GradientTransform string `xml:"gradientTransform,attr"`
  28. GradientUnits string `xml:"gradientUnits,attr"`
  29. Stop []Stop `xml:"stop"`
  30. }
  31. type Defs struct {
  32. CoreAttributes
  33. LinearGradient []LinearGradient `xml:"linearGradient"`
  34. RadialGradient []RadialGradient `xml:"radialGradient"`
  35. Drawable []Drawable
  36. }
  37. type Ellipse struct {
  38. CoreAttributes
  39. Style Style `xml:"style,attr"`
  40. CxCy
  41. Rx string `xml:"rx,attr"`
  42. Ry string `xml:"ry,attr"`
  43. }
  44. type CxCy struct {
  45. Cx Length `xml:"cx,attr"`
  46. Cy Length `xml:"cy,attr"`
  47. }
  48. type FxFy struct {
  49. Fx Length `xml:"fx,attr"`
  50. Fy Length `xml:"fy,attr"`
  51. }
  52. type XY struct {
  53. X Length `xml:"x,attr"`
  54. Y Length `xml:"y,attr"`
  55. }
  56. type X1Y1 struct {
  57. X1 Length `xml:"x1,attr"`
  58. Y1 Length `xml:"y1,attr"`
  59. }
  60. type X2Y2 struct {
  61. X2 Length `xml:"x2,attr"`
  62. Y2 Length `xml:"y2,attr"`
  63. }
  64. type Linear struct {
  65. X1Y1
  66. X2Y2
  67. }
  68. type Tspan struct {
  69. CoreAttributes
  70. Style Style `xml:"style,attr"`
  71. XY
  72. }
  73. type Text struct {
  74. CoreAttributes
  75. Space string `xml:"space,attr"`
  76. Style Style `xml:"style,attr"`
  77. XY
  78. Tspan Tspan `xml:"tspan"`
  79. }
  80. type Rect struct {
  81. CoreAttributes
  82. Style Style `xml:"style,attr"`
  83. XY
  84. Width string `xml:"width,attr"`
  85. Height string `xml:"height,attr"`
  86. }
  87. func (r Rect) Draw(target *ebiten.Image, options *DrawOptions) {
  88. p := vector.Path{}
  89. fo := vector.FillOptions{Color: r.ToColor("color")}
  90. p.MoveTo(r.X, r.Y).LineTo(r.X, r.Y+r.Width).
  91. LineTo(r.X+r.Height, r.Y+r.Width).LineTo(r.X+r.Height, r.Y).
  92. p.LineTo(r.X, r.Y).Fill(target, fo)
  93. }
  94. type DrawOptions struct {
  95. X float32
  96. Y float32
  97. }
  98. type Drawer interface {
  99. Draw(target *ebiten.Image, options *DrawOptions)
  100. }
  101. type Drawable struct {
  102. Drawer
  103. }
  104. func (d *Drawable) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
  105. var err error
  106. switch start.Name.Local {
  107. case "rect":
  108. r := Rect{}
  109. err = dec.DecodeElement(&r, &start)
  110. d.Rect = &r
  111. case "ellipse":
  112. r := Ellipse{}
  113. err = dec.DecodeElement(&r, &start)
  114. d.Ellipse = &r
  115. case "text":
  116. r := Text{}
  117. err = dec.DecodeElement(&r, &start)
  118. d.Text = &r
  119. default:
  120. return fmt.Errorf("drawable not supported: %s", start)
  121. }
  122. return err
  123. }
  124. type G struct {
  125. CoreAttributes
  126. Drawable []Drawable `xml:",any"`
  127. }
  128. type SVG struct {
  129. CoreAttributes
  130. XMLName xml.Name `xml:"svg"`
  131. Width Length `xml:"width,attr"`
  132. Height Length `xml:"height,attr"`
  133. ViewBox string `xml:"viewBox,attr"`
  134. Version string `xml:"version,attr"`
  135. Xlink string `xml:"xlink,attr"`
  136. Xmlns string `xml:"xmlns,attr"`
  137. SVG string `xml:"svg,attr"`
  138. Defs []Defs `xml:"defs"`
  139. G G `xml:"g"`
  140. }