forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.go
56 lines (48 loc) · 1.61 KB
/
svg.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"encoding/xml"
"io"
"regexp"
"strconv"
"github.com/pkg/errors"
)
type SVGInfo struct {
Width int
Height int
}
func parseSVG(svgReader io.Reader) (SVGInfo, error) {
var parsedSVG struct {
Width string `xml:"width,attr,omitempty"`
Height string `xml:"height,attr,omitempty"`
ViewBox string `xml:"viewBox,attr,omitempty"`
}
svgInfo := SVGInfo{
Width: 0,
Height: 0,
}
viewBoxPattern := regexp.MustCompile("^([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)$")
dimensionPattern := regexp.MustCompile("(?i)^([0-9]+)(?:px)?$")
// decode provided SVG
if err := xml.NewDecoder(svgReader).Decode(&parsedSVG); err != nil {
return svgInfo, err
}
// prefer viewbox for SVG dimensions over width/height
if viewBoxMatches := viewBoxPattern.FindStringSubmatch(parsedSVG.ViewBox); len(viewBoxMatches) == 5 {
svgInfo.Width, _ = strconv.Atoi(viewBoxMatches[3])
svgInfo.Height, _ = strconv.Atoi(viewBoxMatches[4])
} else if len(parsedSVG.Width) > 0 && len(parsedSVG.Height) > 0 {
widthMatches := dimensionPattern.FindStringSubmatch(parsedSVG.Width)
heightMatches := dimensionPattern.FindStringSubmatch(parsedSVG.Height)
if len(widthMatches) == 2 && len(heightMatches) == 2 {
svgInfo.Width, _ = strconv.Atoi(widthMatches[1])
svgInfo.Height, _ = strconv.Atoi(heightMatches[1])
}
}
// if width and/or height are still zero, create new error
if svgInfo.Width == 0 || svgInfo.Height == 0 {
return svgInfo, errors.New("unable to extract SVG dimensions")
}
return svgInfo, nil
}