-
Notifications
You must be signed in to change notification settings - Fork 4
/
wrapper.go
109 lines (94 loc) · 2.71 KB
/
wrapper.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package inkscape
import (
"bytes"
"errors"
"fmt"
"image"
"image/png"
"os"
"os/exec"
)
var defaultCommand = "inkscape"
// Client is an implementation of an Inkscape CLI wrapper.
// It assumes a locally available inkscape command can be run.
type Client struct {
// the name/path for the inkscape CLI command
// If left empty, the default inkscape command will be used.
Command string
}
// IsAvailable checks whether the inkscape command is locally available.
// Provides feature detection for a sort of progressive enhancement.
func (c Client) IsAvailable() bool {
_, err := exec.LookPath(c.cmd())
return err == nil
}
// SVGToPNG rasterizes the SVG at the specified path to PNG format.
func (c Client) SVGToPNG(path string, width, height int) (image.Image, error) {
if height < 1 {
return nil, errors.New("invalid height")
}
if width < 1 {
return nil, errors.New("invalid width")
}
_, err := os.Stat(path)
if err != nil {
return nil, err
}
cmd := exec.Command(c.cmd(), path, "-w", fmt.Sprint(width), "-h", fmt.Sprint(height), "--export-type=png", "--export-filename=-")
stdoutData := bytes.NewBuffer(nil)
stderrData := bytes.NewBuffer(nil)
cmd.Stdout = stdoutData
cmd.Stderr = stderrData
err = cmd.Start()
if err != nil {
return nil, describeError(err, stderrData.String())
}
err = cmd.Wait()
if err != nil {
return nil, describeError(err, stderrData.String())
}
// if we get no bytes on stdout, that means something went wrong
if stdoutData.Len() == 0 {
return nil, errors.New("error processing SVG")
}
img, err := png.Decode(stdoutData)
return img, err
}
// SVGStringToPNG rasterizes the provided SVG text to PNG format.
func (c Client) SVGStringToPNG(svgText string, width, height int) (image.Image, error) {
if height < 1 {
return nil, errors.New("invalid height")
}
if width < 1 {
return nil, errors.New("invalid width")
}
cmd := exec.Command(c.cmd(), "--pipe", "-w", fmt.Sprint(width), "-h", fmt.Sprint(height), "--export-type=png", "--export-filename=-")
stdoutData := bytes.NewBuffer(nil)
stderrData := bytes.NewBuffer(nil)
cmd.Stdout = stdoutData
cmd.Stderr = stderrData
cmd.Stdin = bytes.NewBufferString(svgText)
err := cmd.Start()
if err != nil {
return nil, describeError(err, stderrData.String())
}
err = cmd.Wait()
if err != nil {
return nil, describeError(err, stderrData.String())
}
// if we get no bytes on stdout, that means something went wrong
if stdoutData.Len() == 0 {
return nil, errors.New("error processing SVG")
}
img, err := png.Decode(stdoutData)
return img, err
}
func (c Client) cmd() string {
if c.Command == "" {
return defaultCommand
}
return c.Command
}
func describeError(err error, info string) error {
return fmt.Errorf("%v: %s", err, info)
}