-
Notifications
You must be signed in to change notification settings - Fork 11
/
svg-path.c
229 lines (192 loc) · 4.62 KB
/
svg-path.c
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
#include "svg-path.h"
#include "kvec.h"
#include <ctype.h>
#include <setjmp.h>
struct ParseData
{
const char *str;
jmp_buf buf;
};
static void wsp(struct ParseData *d)
{
for (;;)
{
if (isspace(*d->str))
d->str++;
else
break;
}
}
static void comma(struct ParseData *d)
{
wsp(d);
if (*d->str == ',')
d->str++;
else
return;
wsp(d);
}
static float number(struct ParseData *d)
{
if (isspace(*d->str))
longjmp(d->buf, 1);
char *end = NULL;
float result = (float)strtod(d->str, &end);
if (d->str == end)
longjmp(d->buf, 1);
d->str = end;
return result;
}
static int hasNumber(struct ParseData *d)
{
if (isspace(*d->str))
return 0;
char *end = NULL;
strtod(d->str, &end);
return d->str != end;
}
static int hasComma(struct ParseData *d)
{
const char *save = d->str;
wsp(d);
int result = (*d->str == ',');
d->str = save;
return result;
}
typedef kvec_t(float) kvec_float_t;
static void numbers(struct ParseData *d, int alignment, int multiple, kvec_float_t *v)
{
int i;
wsp(d);
for (;;)
{
for (i = 0; i < alignment; ++i)
{
if (i == 0)
{
kv_push_back(*v, number(d));
}
else
{
comma(d);
kv_push_back(*v, number(d));
}
}
if (!multiple)
break;
if (hasComma(d))
{
comma(d);
continue;
}
wsp(d);
if (!hasNumber(d))
break;
}
}
typedef kvec_t(unsigned char) kvec_uchar_t;
static void parseHelper(struct ParseData *d, kvec_uchar_t *commands, kvec_float_t *coords, int alignment, int multiple)
{
int i;
size_t j;
char command;
kvec_float_t n;
command = *d->str;
d->str++;
kv_init(n);
numbers(d, alignment, multiple, &n);
if (multiple)
{
for (j = 0; j < kv_size(n); j += alignment)
{
kv_push_back(*commands, command);
for (i = 0; i < alignment; ++i)
kv_push_back(*coords, kv_a(n, j + i));
}
}
else
{
kv_push_back(*commands, command);
for (i = 0; i < alignment; ++i)
kv_push_back(*coords, kv_a(n, i));
}
kv_free(n);
}
static void parse(struct ParseData *d, kvec_uchar_t *commands, kvec_float_t *coords)
{
for (;;)
{
wsp(d);
if (*d->str == 0)
break;
switch (*d->str)
{
case 'M':
case 'm':
parseHelper(d, commands, coords, 2, 0);
break;
case 'L':
parseHelper(d, commands, coords, 2, 1);
break;
case 'Q':
parseHelper(d, commands, coords, 4, 1);
break;
case 'C':
parseHelper(d, commands, coords, 6, 1);
break;
case 'Z':
case 'z':
d->str++;
kv_push_back(*commands, 'Z');
break;
default:
longjmp(d->buf, 1);
break;
}
}
}
struct PrSvgPath *prParseSvgPath(size_t length, const char *pathString)
{
struct ParseData d;
kvec_uchar_t commands;
kvec_float_t coords;
kv_init(commands);
kv_init(coords);
d.str = pathString;
if (!setjmp(d.buf))
parse(&d, &commands, &coords);
else
return NULL;
struct PrSvgPath *svgPath = (struct PrSvgPath*)malloc(sizeof(struct PrSvgPath));
if (kv_empty(commands))
{
svgPath->numCommands = 0;
svgPath->commands = NULL;
}
else
{
svgPath->numCommands = kv_size(commands);
svgPath->commands = (unsigned char*)malloc(svgPath->numCommands * sizeof(unsigned char));
memcpy(svgPath->commands, kv_data(commands), svgPath->numCommands * sizeof(unsigned char));
}
if (kv_empty(coords))
{
svgPath->numCoords = 0;
svgPath->coords = NULL;
}
else
{
svgPath->numCoords = kv_size(coords);
svgPath->coords = (float*)malloc(svgPath->numCoords * sizeof(float));
memcpy(svgPath->coords, kv_data(coords), svgPath->numCoords * sizeof(float));
}
return svgPath;
}
void prFreeSvgPath(struct PrSvgPath *svgPath)
{
if (svgPath == NULL)
return;
free(svgPath->coords);
free(svgPath->commands);
free(svgPath);
}