123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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"`
- }
|