-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransform-vector-font.sh
executable file
·331 lines (314 loc) · 9.47 KB
/
transform-vector-font.sh
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
# Copyright (C) 2012-2017 Jacob R. Lifshay
# This file is part of Voxels.
#
# Voxels is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Voxels is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Voxels; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
not_running_bash=true
if [[ "123" =~ "123" ]]; then
if [[ "${BASH_REMATCH[0]}" == "123" ]]; then
not_running_bash=false
fi
fi
if "$not_running_bash"; then
echo "this program needs to be run in bash" >&2
exit 1
fi
zeros="0000000"
function parse_float()
{
local v="$1" retval=0 decimals
if [[ "$v" =~ ^('-'?[0-9][0-9]*)('.'([0-9][0-9]*)'f'?)?$ ]]; then
decimals="${BASH_REMATCH[3]}$zeros"
v="${BASH_REMATCH[1]}${decimals:0:${#zeros}}"
echo $((v))
else
return 1
fi
return 0
}
function output_float()
{
local v decimals
[[ "$1" =~ ^'-'?[0-9]+$ ]] || return 1
((v = $1))
if (( v < 0 )); then
(( v = -v ))
echo -n '-'
fi
decimals="$((v % 1$zeros))"
if (( decimals )); then
decimals="${zeros:${#decimals}}$decimals"
[[ "$decimals" =~ ^([0-9]*[1-9])?'0'*$ ]]
decimals="${BASH_REMATCH[1]}"
echo "$((v / 1$zeros)).${decimals}f"
else
echo "$((v / 1$zeros))"
fi
return 0
}
function parse_line()
{
local line="$1" x y kind
if [[ "$line" =~ ^[[:space:]]*'{'[[:space:]]*('-'?[0-9][0-9.f]*)[[:space:]]*','[[:space:]]*('-'?[0-9][0-9.f]*)[[:space:]]*','[[:space:]]*'VectorFontVertex'[[:space:]]*'::'[[:space:]]*('StartLoop'|'Vertex'|'EndLoop'|'EndGlyph')[[:space:]]*'}'[[:space:]]*(','[[:space:]]*)?$ ]]; then
x="${BASH_REMATCH[1]}"
y="${BASH_REMATCH[2]}"
kind="${BASH_REMATCH[3]}"
#echo "x='$x' y='$y' kind='$kind'" >&2
case "$kind" in
StartLoop)
line="S `parse_float "$x"`" || return 1
line="$line `parse_float "$y"`" || return 1
;;
Vertex)
line="V `parse_float "$x"`" || return 1
line="$line `parse_float "$y"`" || return 1
;;
EndLoop)
line="E `parse_float "$x"`" || return 1
line="$line `parse_float "$y"`" || return 1
;;
EndGlyph)
line=";"
;;
*)
return 1
;;
esac
echo "$line"
else
return 1
fi
return 0
}
function output_line()
{
local indent=" "
[[ "$1" =~ ^(.)(' '('-'?[0-9]+)' '('-'?[0-9]+))?$ ]] || return 1
local kind="${BASH_REMATCH[1]}" x="${BASH_REMATCH[3]}" y="${BASH_REMATCH[4]}"
case "$kind" in
S)
x="`output_float "$x"`" || return 1
y="`output_float "$y"`" || return 1
echo "$indent{$x, $y, VectorFontVertex::StartLoop},"
;;
V)
x="`output_float "$x"`" || return 1
y="`output_float "$y"`" || return 1
echo "$indent{$x, $y, VectorFontVertex::Vertex},"
;;
E)
x="`output_float "$x"`" || return 1
y="`output_float "$y"`" || return 1
echo "$indent{$x, $y, VectorFontVertex::EndLoop},"
;;
\;)
echo "$indent{0, 0, VectorFontVertex::EndGlyph},"
;;
*)
return 1
;;
esac
return 0
}
function set_line_kind()
{
[[ "$1" =~ ^(.)(' '('-'?[0-9]+)' '('-'?[0-9]+))?$ ]] || return 1
local kind="$2" x="${BASH_REMATCH[3]}" y="${BASH_REMATCH[4]}"
echo "$kind $x $y"
return 0
}
function transform_line()
{
if ! [[ "$1" =~ ^(.)(' '('-'?[0-9]+)' '('-'?[0-9]+))?$ ]]; then
echo "parse failed">&2
echo -n "transform_line" >&2
printf " '%s'" "$@">&2
echo >&2
return 1
fi
local kind="${BASH_REMATCH[1]}" x="${BASH_REMATCH[3]}" y="${BASH_REMATCH[4]}" tform="$2" t
case "$kind" in
S|V|E)
case "$tform" in
mirror_x)
(( $# == 3 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( x = 2 * $3 - x ))
;;
mirror_y)
(( $# == 3 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( y = 2 * $3 - y ))
;;
rotate_cw)
(( $# == 4 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( t = x - $3 + $4 ))
(( x = $4 - y + $3 ))
(( y = t ))
;;
rotate_ccw)
(( $# == 4 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( t = y - $4 + $3 ))
(( y = $3 - x + $4 ))
(( x = t ))
;;
rotate_180)
(( $# == 4 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( x = 2 * $3 - x ))
(( y = 2 * $4 - y ))
;;
translate)
(( $# == 4 )) || { echo "invalid arguments to $tform" >&2; return 1; }
(( x += $3 ))
(( y += $4 ))
;;
reverse)
(( $# == 2 )) || { echo "invalid arguments to $tform" >&2; return 1; }
;;
*)
echo "transform $tform not implemented" >&2
return 1
esac
echo "$kind $x $y"
;;
\;)
echo ";"
;;
*)
echo "invalid line" >&2
return 1
;;
esac
return 0
}
function transform_lines()
{
local lines
mapfile -t lines
local i
for((i=0;i<${#lines[@]};i++)); do
lines[i]="`transform_line "${lines[i]}" "$@"`" || return 1
done
local loopStart="-1" kind j k temp kKind
if [[ "$1" == "reverse" ]]; then
for((i=0;i<${#lines[@]};i++)); do
[[ "${lines[i]}" =~ ^(.)(' '('-'?[0-9]+)' '('-'?[0-9]+))?$ ]] || return 1
kind="${BASH_REMATCH[1]}"
case "$kind" in
S)
if [[ "$loopStart" != "-1" ]]; then
echo "unexpected start loop" >&2
return 1
fi
loopStart="$i"
;;
V)
if [[ "$loopStart" == "-1" ]]; then
echo "unexpected vertex" >&2
return 1
fi
;;
E)
if [[ "$loopStart" == "-1" ]]; then
echo "unexpected end loop" >&2
return 1
fi
for((j=loopStart+1,k=i;j<k;j++,k--)); do
temp="`set_line_kind "${lines[j]}" "V"`" || return 1
lines[j]="`set_line_kind "${lines[k]}" "V"`" || return 1
lines[k]="$temp"
done
lines[i]="`set_line_kind "${lines[i]}" "E"`" || return 1
loopStart="-1"
;;
\;)
if [[ "$loopStart" != "-1" ]]; then
echo "unexpected end glyph" >&2
return 1
fi
;;
*)
return 1
;;
esac
done
if [[ "$loopStart" != "-1" ]]; then
echo "unexpected eof" >&2
return 1
fi
fi
printf "%s\n" "${lines[@]}"
return 0
}
function parse_lines()
{
local lines line
mapfile -t lines
for line in "${lines[@]}"; do
parse_line "$line" || return 1
done
return 0
}
function output_lines()
{
local lines line
mapfile -t lines
for line in "${lines[@]}"; do
output_line "$line" || return 1
done
return 0
}
function handle_failure()
{
echo "failure" "$@" >&2
exit 1
}
commands=()
valid_commands=()
valid_commands+=("mirror_x <x> mirror across the line x=<x>")
valid_commands+=("mirror_y <y> mirror across the line y=<y>")
valid_commands+=("rotate_cw <x> <y> rotate 90 degrees counterclockwise around the point (<x>, <y>)")
valid_commands+=("rotate_ccw <x> <y> rotate 90 degrees clockwise around the point (<x>, <y>)")
valid_commands+=("rotate_180 <x> <y> rotate 180 degrees around the point (<x>, <y>)")
valid_commands+=("translate <x> <y> translate by (<x>, <y>)")
valid_commands+=("reverse reverse the order of vertices in the polygons")
for arg in "$@"; do
if [[ "$arg" =~ ^'-'?[0-9]+('.'[0-9]*'f'?)?$ ]]; then
if (( ${#commands[@]} == 0)); then
echo "number before first command">&2
exit 1
fi
arg="`parse_float "$arg"`" || handle_failure parse_float
commands[${#commands[@]} - 1]="${commands[${#commands[@]} - 1]} $arg"
elif [[ "$arg" =~ ^[a-zA-Z_][a-zA-Z_0-9]*$ ]]; then
commands+=("$arg")
elif [[ "$arg" =~ ^('-h'|'--help') ]]; then
echo "usage: $0 [<command> [<command> [...]]]"
echo "valid commands:"
for i in "${valid_commands[@]}"; do
echo "$i"
done
exit 0
else
echo "invalid argument: '$arg'" >&2
exit 1
fi
done
lines=("`parse_lines`") || handle_failure parse_lines
for tform in "${commands[@]}" ; do
lines="`echo "$lines" | transform_lines $tform`" || handle_failure transform_lines "$tform"
done
echo "$lines" | output_lines