Browse Source

Add style type with primitive parsing.

Beoran 2 years ago
parent
commit
07628e2849
4 changed files with 40 additions and 2 deletions
  1. 2 1
      README.md
  2. 5 0
      go.mod
  3. 33 0
      svg/style.go
  4. 0 1
      svg/svg.go

+ 2 - 1
README.md

@@ -1,3 +1,4 @@
 # ebisvg
 
-SVG loader and renderer for Ebiten.
+SVG 1.1 loader and renderer for Ebiten. Also accepts Inkscape SVG.
+Support for features varies.

+ 5 - 0
go.mod

@@ -1,3 +1,8 @@
 module src.eruta.nl/beoran/ebisvg
 
 go 1.16
+
+require (
+	github.com/hajimehoshi/ebiten/v2 v2.1.7 // indirect
+	golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
+)

+ 33 - 0
svg/style.go

@@ -0,0 +1,33 @@
+package svg
+
+import "encoding/xml"
+import "strings"
+
+type Style map[string]string
+
+func ParseStyle(str string) (Style, error) {
+	res := make(Style)
+	rules := strings.Split(";", str)
+	for _, rule := range rules {
+		parts := strings.SplitN(rule, ":", 2)
+		key := parts[0]
+		value := "true"
+		if len(parts) > 1 {
+			value = parts[1]
+		}
+		res[key] = value
+	}
+	return res, nil
+}
+
+func (s *Style) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
+	var value string
+	// Read tag content into value
+	d.DecodeElement(&value, &start)
+	style, err := ParseStyle(value)
+	if err != nil {
+		return err
+	}
+	*s = style
+	return nil
+}

+ 0 - 1
svg/svg.go

@@ -16,7 +16,6 @@ type Docname string
 type Sodipodi string
 type G string
 type Space string
-type Style string
 type Box string
 type Xlink string
 type L string