-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
shape-description.cpp
303 lines (287 loc) · 10 KB
/
shape-description.cpp
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
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "shape-description.h"
#include <cstdlib>
namespace msdfgen {
int readCharF(FILE *input) {
int c = '\0';
do {
c = fgetc(input);
} while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
return c;
}
int readCharS(const char **input) {
int c = '\0';
do {
c = *(*input)++;
} while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
if (!c) {
--c;
return EOF;
}
return c;
}
int readCoordF(FILE *input, Point2 &coord) {
return fscanf(input, "%lf , %lf", &coord.x, &coord.y);
}
int readCoordS(const char **input, Point2 &coord) {
char *end = NULL;
coord.x = strtod(*input, &end);
if (end <= *input)
return 0;
*input = end;
while (**input == ' ' || **input == '\t' || **input == '\n' || **input == '\r')
++*input;
if (**input != ',')
return 1;
++*input;
coord.y = strtod(*input, &end);
if (end <= *input)
return 1;
*input = end;
return 2;
}
static bool writeCoord(FILE *output, Point2 coord) {
fprintf(output, "%.12g, %.12g", coord.x, coord.y);
return true;
}
template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
static int readControlPoints(T *input, Point2 *output) {
int result = readCoord(input, output[0]);
if (result == 2) {
switch (readChar(input)) {
case ')':
return 1;
case ';':
break;
default:
return -1;
}
result = readCoord(input, output[1]);
if (result == 2 && readChar(input) == ')')
return 2;
} else if (result != 1 && readChar(input) == ')')
return 0;
return -1;
}
template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
static bool readContour(T *input, Contour &output, const Point2 *first, int terminator, bool &colorsSpecified) {
Point2 p[4], start;
if (first)
p[0] = *first;
else {
int result = readCoord(input, p[0]);
if (result != 2)
return result != 1 && readChar(input) == terminator;
}
start = p[0];
int c = '\0';
while ((c = readChar(input)) != terminator) {
if (c != ';')
return false;
EdgeColor color = WHITE;
int result = readCoord(input, p[1]);
if (result == 2) {
output.addEdge(EdgeHolder(p[0], p[1], color));
p[0] = p[1];
continue;
} else if (result == 1)
return false;
else {
int controlPoints = 0;
switch ((c = readChar(input))) {
case '#':
output.addEdge(EdgeHolder(p[0], start, color));
p[0] = start;
continue;
case ';':
goto FINISH_EDGE;
case '(':
goto READ_CONTROL_POINTS;
case 'C': case 'c':
color = CYAN;
colorsSpecified = true;
break;
case 'M': case 'm':
color = MAGENTA;
colorsSpecified = true;
break;
case 'Y': case 'y':
color = YELLOW;
colorsSpecified = true;
break;
case 'W': case 'w':
color = WHITE;
colorsSpecified = true;
break;
default:
return c == terminator;
}
switch (readChar(input)) {
case ';':
goto FINISH_EDGE;
case '(':
READ_CONTROL_POINTS:
if ((controlPoints = readControlPoints<T, readChar, readCoord>(input, p+1)) < 0)
return false;
break;
default:
return false;
}
if (readChar(input) != ';')
return false;
FINISH_EDGE:
result = readCoord(input, p[1+controlPoints]);
if (result != 2) {
if (result == 1)
return false;
else {
if (readChar(input) == '#')
p[1+controlPoints] = start;
else
return false;
}
}
switch (controlPoints) {
case 0:
output.addEdge(EdgeHolder(p[0], p[1], color));
p[0] = p[1];
continue;
case 1:
output.addEdge(EdgeHolder(p[0], p[1], p[2], color));
p[0] = p[2];
continue;
case 2:
output.addEdge(EdgeHolder(p[0], p[1], p[2], p[3], color));
p[0] = p[3];
continue;
}
}
}
return true;
}
bool readShapeDescription(FILE *input, Shape &output, bool *colorsSpecified) {
bool locColorsSpec = false;
output.contours.clear();
output.inverseYAxis = false;
Point2 p;
int result = readCoordF(input, p);
if (result == 2) {
return readContour<FILE, readCharF, readCoordF>(input, output.addContour(), &p, EOF, locColorsSpec);
} else if (result == 1)
return false;
else {
int c = readCharF(input);
if (c == '@') {
char after = '\0';
if (fscanf(input, "invert-y%c", &after) != 1)
return feof(input) != 0;
output.inverseYAxis = true;
c = after;
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
c = readCharF(input);
}
for (; c == '{'; c = readCharF(input))
if (!readContour<FILE, readCharF, readCoordF>(input, output.addContour(), NULL, '}', locColorsSpec))
return false;
if (colorsSpecified)
*colorsSpecified = locColorsSpec;
return c == EOF && feof(input);
}
}
bool readShapeDescription(const char *input, Shape &output, bool *colorsSpecified) {
bool locColorsSpec = false;
output.contours.clear();
output.inverseYAxis = false;
Point2 p;
int result = readCoordS(&input, p);
if (result == 2) {
return readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), &p, EOF, locColorsSpec);
} else if (result == 1)
return false;
else {
int c = readCharS(&input);
if (c == '@') {
for (int i = 0; i < (int) sizeof("invert-y")-1; ++i)
if (input[i] != "invert-y"[i])
return false;
output.inverseYAxis = true;
input += sizeof("invert-y")-1;
c = readCharS(&input);
}
for (; c == '{'; c = readCharS(&input))
if (!readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), NULL, '}', locColorsSpec))
return false;
if (colorsSpecified)
*colorsSpecified = locColorsSpec;
return c == EOF;
}
}
static bool isColored(const Shape &shape) {
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
if ((*edge)->color != WHITE)
return true;
return false;
}
bool writeShapeDescription(FILE *output, const Shape &shape) {
if (!shape.validate())
return false;
bool writeColors = isColored(shape);
if (shape.inverseYAxis)
fprintf(output, "@invert-y\n");
for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
fprintf(output, "{\n");
if (!contour->edges.empty()) {
for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
char colorCode = '\0';
if (writeColors) {
switch ((*edge)->color) {
case YELLOW: colorCode = 'y'; break;
case MAGENTA: colorCode = 'm'; break;
case CYAN: colorCode = 'c'; break;
case WHITE: colorCode = 'w'; break;
default:;
}
}
const Point2 *p = (*edge)->controlPoints();
switch ((*edge)->type()) {
case (int) LinearSegment::EDGE_TYPE:
fprintf(output, "\t");
writeCoord(output, p[0]);
fprintf(output, ";\n");
if (colorCode)
fprintf(output, "\t\t%c;\n", colorCode);
break;
case (int) QuadraticSegment::EDGE_TYPE:
fprintf(output, "\t");
writeCoord(output, p[0]);
fprintf(output, ";\n\t\t");
if (colorCode)
fprintf(output, "%c", colorCode);
fprintf(output, "(");
writeCoord(output, p[1]);
fprintf(output, ");\n");
break;
case (int) CubicSegment::EDGE_TYPE:
fprintf(output, "\t");
writeCoord(output, p[0]);
fprintf(output, ";\n\t\t");
if (colorCode)
fprintf(output, "%c", colorCode);
fprintf(output, "(");
writeCoord(output, p[1]);
fprintf(output, "; ");
writeCoord(output, p[2]);
fprintf(output, ");\n");
break;
}
}
fprintf(output, "\t#\n");
}
fprintf(output, "}\n");
}
return true;
}
}