package svg import "encoding/xml" import "fmt" import "github.com/hajimehoshi/ebiten/v2" type CoreAttributes struct { Chardata string `xml:",chardata"` ID string `xml:"id,attr"` } type Stop struct { CoreAttributes Style Style `xml:"style,attr"` Offset string `xml:"offset,attr"` } type LinearGradient struct { CoreAttributes Href string `xml:"href,attr"` Linear GradientUnits string `xml:"gradientUnits,attr"` Stop []Stop `xml:"stop"` } type RadialGradient struct { CoreAttributes Href string `xml:"href,attr"` CxCy FxFy R string `xml:"r,attr"` GradientTransform string `xml:"gradientTransform,attr"` GradientUnits string `xml:"gradientUnits,attr"` Stop []Stop `xml:"stop"` } type Defs struct { CoreAttributes LinearGradient []LinearGradient `xml:"linearGradient"` RadialGradient []RadialGradient `xml:"radialGradient"` Drawable []Drawable } type Ellipse struct { CoreAttributes Style Style `xml:"style,attr"` CxCy Rx string `xml:"rx,attr"` Ry string `xml:"ry,attr"` } type CxCy struct { Cx Length `xml:"cx,attr"` Cy Length `xml:"cy,attr"` } type FxFy struct { Fx Length `xml:"fx,attr"` Fy Length `xml:"fy,attr"` } type XY struct { X Length `xml:"x,attr"` Y Length `xml:"y,attr"` } type X1Y1 struct { X1 Length `xml:"x1,attr"` Y1 Length `xml:"y1,attr"` } type X2Y2 struct { X2 Length `xml:"x2,attr"` Y2 Length `xml:"y2,attr"` } type Linear struct { X1Y1 X2Y2 } type Tspan struct { CoreAttributes Style Style `xml:"style,attr"` XY } type Text struct { CoreAttributes Space string `xml:"space,attr"` Style Style `xml:"style,attr"` XY Tspan Tspan `xml:"tspan"` } type Rect struct { CoreAttributes Style Style `xml:"style,attr"` XY Width string `xml:"width,attr"` Height string `xml:"height,attr"` } func (r Rect) Draw(target *ebiten.Image, options *DrawOptions) { p := vector.Path{} fo := vector.FillOptions{Color: r.ToColor("color")} p.MoveTo(r.X, r.Y).LineTo(r.X, r.Y+r.Width). LineTo(r.X+r.Height, r.Y+r.Width).LineTo(r.X+r.Height, r.Y). p.LineTo(r.X, r.Y).Fill(target, fo) } type DrawOptions struct { X float32 Y float32 } type Drawer interface { Draw(target *ebiten.Image, options *DrawOptions) } type Drawable struct { Drawer } func (d *Drawable) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error { var err error switch start.Name.Local { case "rect": r := Rect{} err = dec.DecodeElement(&r, &start) d.Rect = &r case "ellipse": r := Ellipse{} err = dec.DecodeElement(&r, &start) d.Ellipse = &r case "text": r := Text{} err = dec.DecodeElement(&r, &start) d.Text = &r default: return fmt.Errorf("drawable not supported: %s", start) } return err } type G struct { CoreAttributes Drawable []Drawable `xml:",any"` } type SVG struct { CoreAttributes XMLName xml.Name `xml:"svg"` Width Length `xml:"width,attr"` Height Length `xml:"height,attr"` ViewBox string `xml:"viewBox,attr"` Version string `xml:"version,attr"` Xlink string `xml:"xlink,attr"` Xmlns string `xml:"xmlns,attr"` SVG string `xml:"svg,attr"` Defs []Defs `xml:"defs"` G G `xml:"g"` }