forked from blucz/Vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapetool
More file actions
executable file
·209 lines (190 loc) · 5.03 KB
/
shapetool
File metadata and controls
executable file
·209 lines (190 loc) · 5.03 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
ORIGINX=0.5
ORIGINY=0.5
if [ x$2 == x ]; then
echo "usage: $0 shapename filename.svg [originx] [originy]"
echo
echo ' This program takes in an SVG file (see notes about the SVG file'
echo ' below) and outputs a C data structure that can be passed to '
echo ' vector_shape_draw_shape()'
echo
echo ' The origin x and y is numerical value for where in the drawing the'
echo ' center should be. If not specified, the default is 0.5 for both.'
echo
echo ' You want to get a SVG file with 1 <path d="........."/> in it. Only'
echo ' lines are supported, no curves. The easiest way to draw that is to'
echo ' open Adobe Illustrator, draw what you want using the pen tool, then'
echo ' select all your segments. Go to the menu Object > Compound Paths'
echo ' and select "Make". Then File > Save As..., pick SVG, and on the next'
echo ' popup, pick a profile of SVG 1.0.'
echo
exit 1
fi
if [ x$3 != x ]; then ORIGINX=$3; fi
if [ x$4 != x ]; then ORIGINY=$4; fi
cat $2 | tr -d \\n | sed -e '
s// /g
s/.*\(<path[^>]*>\).*/\1/
s/,/ /g
s/-/ -/g
s/[ ][ ]*/ /g
s/.*d="\([^"]*\)".*/\1/
s/\([A-Za-z]\)/\
\1 /g
' | awk -v originx=$ORIGINX -v originy=$ORIGINY -v name=$1 '
BEGIN {
cpx = 0
cpy = 0
icpx = 0
icpy = 0
cn = 0
drawn = 0
addn = 0
}
function add(v, x, y) {
# print v " " x " " y
data[addn, 1] = v
data[addn, 2] = x
data[addn, 3] = y
addn++
}
{
# print $0
# skip blank lines
if ($0 == "") {
next
} else if ($1 == "M" || $1 == "m") {
if (drawn == 1) {
add("end_draw", 0, 0)
drawn = 0
icpx = 0
icpy = 0
}
cn = 0
x = cpx
y = cpy
for (i = 2; i <= NF; i += 2) {
if ($1 == "m") {
x += $i
y += $(i+1)
} else {
x = $i
y = $(i+1)
}
}
# print "move " x " " y
cpx = x
cpy = y
} else if ($1 == "L" || $1 == "l") {
if (drawn == 0) add("begin_draw", cpx, cpy)
x = cpx
y = cpy
for (i = 2; i <= NF; i += 2) {
if ($1 == "l") {
x += $i
y += $(i+1)
} else {
x = $i
y = $(i+1)
}
add("draw_to", x, y)
}
cpx = x
cpy = y
drawn = 1
} else if ($1 == "H" || $1 == "h") {
if (drawn == 0) add("begin_draw", cpx, cpy)
x = cpx
y = cpy
for (i = 2; i <= NF; i += 1) {
if ($1 == "h") {
x += $i
} else {
x = $i
}
add("draw_to", x, y)
}
cpx = x
cpy = y
drawn = 1
} else if ($1 == "V" || $1 == "v") {
if (drawn == 0) add("begin_draw", cpx, cpy)
x = cpx
y = cpy
for (i = 2; i <= NF; i += 1) {
if ($1 == "v") {
y += $i
} else {
y = $i
}
add("draw_to", x, y)
}
cpx = x
cpy = y
drawn = 1
} else if ($1 == "Z" || $1 == "z") {
if (cpx != icpx || cpy != icpy)
add("draw_to", icpx, icpy)
} else {
print "Unknown SVG path command. Only M,L,V,H,Z are supported. Command encountered is: " $1
exit 1
}
if (cn == 0) {
icpx = cpx
icpy = cpy
}
cn++
}
END {
if (drawn == 1) {
add("end_draw", 0, 0)
}
minx = "unset"
miny = "unset"
maxx = "unset"
maxy = "unset"
cnt = 0
total = 0
begin = 0
for (i = 0; i < addn; i++) {
if (data[i, 1] != "end_draw") {
if (minx == "unset" || minx > data[i, 2]) minx = data[i, 2]
if (miny == "unset" || miny > data[i, 3]) miny = data[i, 3]
if (maxx == "unset" || maxx < data[i, 2]) maxx = data[i, 2]
if (maxy == "unset" || maxy < data[i, 3]) maxy = data[i, 3]
cnt++
total++
} else {
data[begin, 4] = cnt
cnt = 0
begin = i+1
}
}
# print "minx " minx
# print "maxx " maxx
# print "miny " miny
# print "maxy " maxy
width = maxx - minx
height = maxy - miny
for (i = 0; i < addn; i++) {
if (data[i, 1] != "end_draw") {
data[i, 2] = (data[i, 2] - minx) / width - originx
data[i, 3] = (data[i, 3] - miny) / height - originy
}
}
print "double " name "[] = {"
print " /* original size: " width "x" height ", aspect ratio: " width / height " */"
print " " total ", /* total vertex count. */"
for (i = 0; i < addn; i++) {
if (data[i, 1] == "begin_draw") {
print " " data[i,4] ", /* segment vertex count */"
print " " data[i,2] ", " data[i,3] ","
} else if (data[i, 1] == "end_draw") {
print " /* eos */"
} else {
print " " data[i,2] ", " data[i,3] ","
}
}
print " /* -- end of drawing -- */"
print "};"
}'