forked from tdewolff/canvas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latex_bin.go
182 lines (159 loc) · 4.57 KB
/
latex_bin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package canvas
// TODO: make LaTeX work for WASM target?
import (
"bytes"
"crypto/md5"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"os/exec"
"path"
"strings"
"github.com/tdewolff/parse/v2"
"github.com/tdewolff/parse/v2/strconv"
"github.com/tdewolff/parse/v2/xml"
)
var execCommand = exec.Command
var tempDir = path.Join(os.TempDir(), "tdewolff-canvas")
// ParseLaTeX parses a LaTeX formatted string into a path. It requires latex and dvisvgm to be installed on the machine.
// The content is surrounded by:
// \documentclass{article}
// \begin{document}
// \thispagestyle{empty}
// {{input}}
// \end{document}
func ParseLaTeX(s string) (*Path, error) {
if err := os.MkdirAll(tempDir, os.ModePerm); err != nil {
panic(err)
}
defer os.RemoveAll(tempDir)
hash := fmt.Sprintf("%x", md5.Sum([]byte(s)))
// fast track to cached paths
b, err := ioutil.ReadFile(path.Join(tempDir, hash))
if err == nil {
p, err := ParseSVG(string(b))
if err == nil {
return p, nil
}
}
document := `\documentclass{article}
\begin{document}
\thispagestyle{empty}
` + s + `
\end{document}`
stdout := &bytes.Buffer{}
cmd := execCommand("latex", "-jobname="+hash, "-halt-on-error")
cmd.Dir = tempDir
cmd.Stdin = strings.NewReader(document)
cmd.Stdout = stdout
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("%w: %s", err, stdout.String())
}
stdout.Reset()
cmd = execCommand("dvisvgm", "--no-fonts", hash+".dvi")
cmd.Dir = tempDir
cmd.Stdout = stdout
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("%w: %s", err, stdout.String())
}
r, err := os.Open(path.Join(tempDir, hash+".svg"))
if err != nil {
return nil, err
}
defer r.Close()
svgPaths := map[string]*Path{}
x0, y0 := math.Inf(1), math.Inf(1)
height := 0.0
p := &Path{}
l := xml.NewLexer(parse.NewInput(r))
for {
tt, _ := l.Next()
switch tt {
case xml.ErrorToken:
if l.Err() != io.EOF {
return nil, l.Err()
}
p = p.Transform(Identity.Translate(-x0, y0+height).ReflectY())
//_ = ioutil.WriteFile(path.Join(tempDir, hash), []byte(p.String()), 0644)
return p, nil
case xml.StartTagToken:
tag := string(l.Text())
attrs := map[string][]byte{}
for {
ttAttr, _ := l.Next()
if ttAttr != xml.AttributeToken {
break
}
val := l.AttrVal()
if len(val) > 1 && (val[0] == '\'' || val[0] == '"') && val[0] == val[len(val)-1] {
val = val[1 : len(val)-1]
}
attrs[string(l.Text())] = val
}
if tag == "svg" {
var n int
height, n = strconv.ParseFloat(attrs["height"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid height attribute on svg tag")
}
} else if tag == "path" {
id, ok := attrs["id"]
if !ok {
return nil, errors.New("unexpected SVG format: expected id attribute on path tag")
}
d, ok := attrs["d"]
if !ok {
return nil, errors.New("unexpected SVG format: expected d attribute on path tag")
}
svgPath, err := ParseSVG(string(d))
if err != nil {
return nil, err
}
svgPaths[string(id)] = svgPath
} else if tag == "use" {
x, n := strconv.ParseFloat(attrs["x"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid x attribute on use tag")
}
y, n := strconv.ParseFloat(attrs["y"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid y attribute on use tag")
}
id, ok := attrs["xlink:href"]
if !ok || len(id) == 0 || id[0] != '#' {
return nil, errors.New("unexpected SVG format: expected valid xlink:href attribute on use tag")
}
svgPath, ok := svgPaths[string(id[1:])]
if !ok {
return nil, errors.New("unexpected SVG format: xlink:href does not point to existing path")
}
p = p.Append(svgPath.Translate(x, y))
x0 = math.Min(x0, x)
y0 = math.Min(y0, y)
} else if tag == "rect" {
x, n := strconv.ParseFloat(attrs["x"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid x attribute on rect tag")
}
y, n := strconv.ParseFloat(attrs["y"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid y attribute on rect tag")
}
w, n := strconv.ParseFloat(attrs["width"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid width attribute on rect tag")
}
h, n := strconv.ParseFloat(attrs["height"])
if n == 0 {
return nil, errors.New("unexpected SVG format: expected valid height attribute on rect tag")
}
p = p.Append(Rectangle(w, h).Translate(x, y))
x0 = math.Min(x0, x)
y0 = math.Min(y0, y)
}
}
}
}