/
transform.go
41 lines (37 loc) · 1.03 KB
/
transform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package mapserver
import (
"fmt"
"log"
"regexp"
"strconv"
)
type transformation struct {
scale float64
rotate float64
hasRotateAnchor bool
rotateAnchor [2]float64
}
var svgTransformRe = regexp.MustCompile(`(\w+)\s*\(((-?\d*\.?\d+),? ?(-?\d*\.?\d+)?,? ?(-?\d*\.?\d+)?)\)`)
func parseTransform(transform string) (transformation, error) {
tr := transformation{}
for _, match := range svgTransformRe.FindAllStringSubmatch(transform, -1) {
switch match[1] {
case "rotate":
if len(match) == 6 {
tr.rotate, _ = strconv.ParseFloat(match[3], 64)
tr.rotateAnchor[0], _ = strconv.ParseFloat(match[4], 64)
tr.rotateAnchor[1], _ = strconv.ParseFloat(match[5], 64)
tr.hasRotateAnchor = true
} else {
tr.rotate, _ = strconv.ParseFloat(match[2], 64)
}
tr.rotate *= -1
case "scale":
tr.scale, _ = strconv.ParseFloat(match[2], 64)
default:
// TODO proper error handling
log.Println(fmt.Errorf("unsupported transform function %q in %q", match[1], transform))
}
}
return tr, nil
}