-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathconverters.go
176 lines (161 loc) · 4.2 KB
/
converters.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
// Copyright 2015 The draw2d Authors. All rights reserved.
// created: 16/12/2017 by Drahoslav Bednářpackage draw2dsvg
package draw2dsvg
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/llgcode/draw2d"
"image"
"image/color"
"image/png"
"math"
"strconv"
"strings"
)
func toSvgRGBA(c color.Color) string {
r, g, b, a := c.RGBA()
r, g, b, a = r>>8, g>>8, b>>8, a>>8
if a == 255 {
return optiSprintf("#%02X%02X%02X", r, g, b)
}
return optiSprintf("rgba(%v,%v,%v,%f)", r, g, b, float64(a)/255)
}
func toSvgLength(l float64) string {
if math.IsInf(l, 1) {
return "100%"
}
return optiSprintf("%f", l)
}
func toSvgArray(nums []float64) string {
arr := make([]string, len(nums))
for i, num := range nums {
arr[i] = optiSprintf("%f", num)
}
return strings.Join(arr, ",")
}
func toSvgFillRule(rule draw2d.FillRule) string {
return map[draw2d.FillRule]string{
draw2d.FillRuleEvenOdd: "evenodd",
draw2d.FillRuleWinding: "nonzero",
}[rule]
}
func toSvgPathDesc(p *draw2d.Path) string {
parts := make([]string, len(p.Components))
ps := p.Points
for i, cmp := range p.Components {
switch cmp {
case draw2d.MoveToCmp:
parts[i] = optiSprintf("M %f,%f", ps[0], ps[1])
ps = ps[2:]
case draw2d.LineToCmp:
parts[i] = optiSprintf("L %f,%f", ps[0], ps[1])
ps = ps[2:]
case draw2d.QuadCurveToCmp:
parts[i] = optiSprintf("Q %f,%f %f,%f", ps[0], ps[1], ps[2], ps[3])
ps = ps[4:]
case draw2d.CubicCurveToCmp:
parts[i] = optiSprintf("C %f,%f %f,%f %f,%f", ps[0], ps[1], ps[2], ps[3], ps[4], ps[5])
ps = ps[6:]
case draw2d.ArcToCmp:
cx, cy := ps[0], ps[1] // center
rx, ry := ps[2], ps[3] // radii
fi := ps[4] + ps[5] // startAngle + angle
// compute endpoint
sinfi, cosfi := math.Sincos(fi)
nom := math.Hypot(ry*cosfi, rx*sinfi)
x := cx + (rx*ry*cosfi)/nom
y := cy + (rx*ry*sinfi)/nom
// compute large and sweep flags
large := 0
sweep := 0
if math.Abs(ps[5]) > math.Pi {
large = 1
}
if !math.Signbit(ps[5]) {
sweep = 1
}
// dirty hack to ensure whole arc is drawn
// if start point equals end point
if sweep == 1 {
x += 0.01 * sinfi
y += 0.01 * -cosfi
} else {
x += 0.01 * sinfi
y += 0.01 * cosfi
}
// rx ry x-axis-rotation large-arc-flag sweep-flag x y
parts[i] = optiSprintf("A %f %f %v %v %v %F %F",
rx, ry, 0, large, sweep, x, y,
)
ps = ps[6:]
case draw2d.CloseCmp:
parts[i] = "Z"
}
}
return strings.Join(parts, " ")
}
func toSvgTransform(mat draw2d.Matrix) string {
if mat.IsIdentity() {
return ""
}
if mat.IsTranslation() {
x, y := mat.GetTranslation()
return optiSprintf("translate(%f,%f)", x, y)
}
return optiSprintf("matrix(%f,%f,%f,%f,%f,%f)",
mat[0], mat[1], mat[2], mat[3], mat[4], mat[5],
)
}
func imageToSvgHref(image image.Image) string {
out := "data:image/png;base64,"
pngBuf := &bytes.Buffer{}
png.Encode(pngBuf, image)
out += base64.RawStdEncoding.EncodeToString(pngBuf.Bytes())
return out
}
// Do the same thing as fmt.Sprintf
// except it uses the optimal precition for floats: (0-3) for f and (0-6) for F
// eg.:
// optiSprintf("%f", 3.0) => fmt.Sprintf("%.0f", 3.0)
// optiSprintf("%f", 3.33) => fmt.Sprintf("%.2f", 3.33)
// optiSprintf("%f", 3.3001) => fmt.Sprintf("%.1f", 3.3001)
// optiSprintf("%f", 3.333333333333333) => fmt.Sprintf("%.3f", 3.333333333333333)
// optiSprintf("%F", 3.333333333333333) => fmt.Sprintf("%.6f", 3.333333333333333)
func optiSprintf(format string, a ...interface{}) string {
chunks := strings.Split(format, "%")
newChunks := make([]string, len(chunks))
for i, chunk := range chunks {
if i != 0 {
verb := chunk[0]
if verb == 'f' || verb == 'F' {
num := a[i-1].(float64)
p := strconv.Itoa(getPrec(num, verb == 'F'))
chunk = strings.Replace(chunk, string(verb), "."+p+"f", 1)
}
}
newChunks[i] = chunk
}
format = strings.Join(newChunks, "%")
return fmt.Sprintf(format, a...)
}
// TODO needs test, since it is not quiet right
func getPrec(num float64, better bool) int {
num = math.Abs(num)
max := 3
eps := 0.0005
if better {
max = 6
eps = 0.0000005
}
prec := 0
for math.Mod(num, 1) > eps {
num *= 10
eps *= 10
prec++
}
if max < prec {
return max
}
return prec
}