Debian Codegraph @

thirdparty/msdfgen/core/shape-description.cpp

raw symbols SCIP
1
2 #ifndef _CRT_SECURE_NO_WARNINGS
3 #define _CRT_SECURE_NO_WARNINGS
4 #endif
5
6 #include "shape-description.h"
7
8 #include <cstdlib>
9
10 namespace msdfgen {
11
12 int readCharF(FILE *input) {
13 int c = '\0';
14 do {
15 c = fgetc(input);
16 } while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
17 return c;
18 }
19
20 int readCharS(const char **input) {
21 int c = '\0';
22 do {
23 c = *(*input)++;
24 } while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
25 if (!c) {
26 --c;
27 return EOF;
28 }
29 return c;
30 }
31
32 int readCoordF(FILE *input, Point2 &coord) {
33 return fscanf(input, "%lf , %lf", &coord.x, &coord.y);
34 }
35
36 int readCoordS(const char **input, Point2 &coord) {
37 char *end = NULL;
38 coord.x = strtod(*input, &end);
39 if (end <= *input)
40 return 0;
41 *input = end;
42 while (**input == ' ' || **input == '\t' || **input == '\n' || **input == '\r')
43 ++*input;
44 if (**input != ',')
45 return 1;
46 ++*input;
47 coord.y = strtod(*input, &end);
48 if (end <= *input)
49 return 1;
50 *input = end;
51 return 2;
52 }
53
54 bool matchStringS(const char **input, const char *str) {
55 const char *cur = *input;
56 while (*cur && *str && *cur == *str)
57 ++cur, ++str;
58 if (!*str) {
59 *input = cur;
60 return true;
61 }
62 return false;
63 }
64
65 static bool writeCoord(FILE *output, Point2 coord) {
66 fprintf(output, "%.12g, %.12g", coord.x, coord.y);
67 return true;
68 }
69
70 template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
71 static int readControlPoints(T *input, Point2 *output) {
72 int result = readCoord(input, output[0]);
73 if (result == 2) {
74 switch (readChar(input)) {
75 case ')':
76 return 1;
77 case ';':
78 break;
79 default:
80 return -1;
81 }
82 result = readCoord(input, output[1]);
83 if (result == 2 && readChar(input) == ')')
84 return 2;
85 } else if (result != 1 && readChar(input) == ')')
86 return 0;
87 return -1;
88 }
89
90 template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
91 static bool readContour(T *input, Contour &output, const Point2 *first, int terminator, bool &colorsSpecified) {
92 Point2 p[4], start;
93 if (first)
94 p[0] = *first;
95 else {
96 int result = readCoord(input, p[0]);
97 if (result != 2)
98 return result != 1 && readChar(input) == terminator;
99 }
100 start = p[0];
101 int c = '\0';
102 while ((c = readChar(input)) != terminator) {
103 if (c != ';')
104 return false;
105 EdgeColor color = WHITE;
106 int result = readCoord(input, p[1]);
107 if (result == 2) {
108 output.addEdge(EdgeHolder(p[0], p[1], color));
109 p[0] = p[1];
110 continue;
111 } else if (result == 1)
112 return false;
113 else {
114 int controlPoints = 0;
115 switch ((c = readChar(input))) {
116 case '#':
117 output.addEdge(EdgeHolder(p[0], start, color));
118 p[0] = start;
119 continue;
120 case ';':
121 goto FINISH_EDGE;
122 case '(':
123 goto READ_CONTROL_POINTS;
124 case 'C': case 'c':
125 color = CYAN;
126 colorsSpecified = true;
127 break;
128 case 'M': case 'm':
129 color = MAGENTA;
130 colorsSpecified = true;
131 break;
132 case 'Y': case 'y':
133 color = YELLOW;
134 colorsSpecified = true;
135 break;
136 case 'W': case 'w':
137 color = WHITE;
138 colorsSpecified = true;
139 break;
140 default:
141 return c == terminator;
142 }
143 switch (readChar(input)) {
144 case ';':
145 goto FINISH_EDGE;
146 case '(':
147 READ_CONTROL_POINTS:
148 if ((controlPoints = readControlPoints<T, readChar, readCoord>(input, p+1)) < 0)
149 return false;
150 break;
151 default:
152 return false;
153 }
154 if (readChar(input) != ';')
155 return false;
156 FINISH_EDGE:
157 result = readCoord(input, p[1+controlPoints]);
158 if (result != 2) {
159 if (result == 1)
160 return false;
161 else {
162 if (readChar(input) == '#')
163 p[1+controlPoints] = start;
164 else
165 return false;
166 }
167 }
168 switch (controlPoints) {
169 case 0:
170 output.addEdge(EdgeHolder(p[0], p[1], color));
171 p[0] = p[1];
172 continue;
173 case 1:
174 output.addEdge(EdgeHolder(p[0], p[1], p[2], color));
175 p[0] = p[2];
176 continue;
177 case 2:
178 output.addEdge(EdgeHolder(p[0], p[1], p[2], p[3], color));
179 p[0] = p[3];
180 continue;
181 }
182 }
183 }
184 return true;
185 }
186
187 bool readShapeDescription(FILE *input, Shape &output, bool *colorsSpecified) {
188 bool locColorsSpec = false;
189 output.contours.clear();
190 output.setYAxisOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION);
191 Point2 p;
192 int result = readCoordF(input, p);
193 if (result == 2) {
194 return readContour<FILE, readCharF, readCoordF>(input, output.addContour(), &p, EOF, locColorsSpec);
195 } else if (result == 1)
196 return false;
197 else {
198 int c = readCharF(input);
199 if (c == '@') {
200 char after = '\0';
201 if (fscanf(input, "y-%c", &after) == 1 && (after == 'u' || after == 'd')) {
202 switch (after) {
203 case 'u':
204 output.setYAxisOrientation(Y_UPWARD);
205 break;
206 case 'd':
207 output.setYAxisOrientation(Y_DOWNWARD);
208 break;
209 }
210 if (fscanf(input, after == 'u' ? "p%c" : "own%c", &after) != 1)
211 return feof(input) != 0;
212 } else if (fscanf(input, "invert-y%c", &after) == 1)
213 output.inverseYAxis = true;
214 else
215 return feof(input) != 0;
216 c = after;
217 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
218 c = readCharF(input);
219 }
220 for (; c == '{'; c = readCharF(input))
221 if (!readContour<FILE, readCharF, readCoordF>(input, output.addContour(), NULL, '}', locColorsSpec))
222 return false;
223 if (colorsSpecified)
224 *colorsSpecified = locColorsSpec;
225 return c == EOF && feof(input);
226 }
227 }
228
229 bool readShapeDescription(const char *input, Shape &output, bool *colorsSpecified) {
230 bool locColorsSpec = false;
231 output.contours.clear();
232 output.setYAxisOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION);
233 Point2 p;
234 int result = readCoordS(&input, p);
235 if (result == 2) {
236 return readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), &p, EOF, locColorsSpec);
237 } else if (result == 1)
238 return false;
239 else {
240 int c = readCharS(&input);
241 if (c == '@') {
242 if (matchStringS(&input, "y-down"))
243 output.setYAxisOrientation(Y_DOWNWARD);
244 else if (matchStringS(&input, "y-up"))
245 output.setYAxisOrientation(Y_UPWARD);
246 else if (matchStringS(&input, "invert-y"))
247 output.inverseYAxis = true;
248 else
249 return false;
250 c = readCharS(&input);
251 }
252 for (; c == '{'; c = readCharS(&input))
253 if (!readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), NULL, '}', locColorsSpec))
254 return false;
255 if (colorsSpecified)
256 *colorsSpecified = locColorsSpec;
257 return c == EOF;
258 }
259 }
260
261 static bool isColored(const Shape &shape) {
262 for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
263 for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
264 if ((*edge)->color != WHITE)
265 return true;
266 return false;
267 }
268
269 bool writeShapeDescription(FILE *output, const Shape &shape) {
270 if (!shape.validate())
271 return false;
272 bool writeColors = isColored(shape);
273 switch (shape.getYAxisOrientation()) {
274 case Y_UPWARD:
275 fprintf(output, "@y-up\n");
276 break;
277 case Y_DOWNWARD:
278 fprintf(output, "@y-down\n");
279 break;
280 }
281 for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
282 fprintf(output, "{\n");
283 if (!contour->edges.empty()) {
284 for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
285 char colorCode = '\0';
286 if (writeColors) {
287 switch ((*edge)->color) {
288 case YELLOW: colorCode = 'y'; break;
289 case MAGENTA: colorCode = 'm'; break;
290 case CYAN: colorCode = 'c'; break;
291 case WHITE: colorCode = 'w'; break;
292 default:;
293 }
294 }
295 const Point2 *p = (*edge)->controlPoints();
296 switch ((*edge)->type()) {
297 case (int) LinearSegment::EDGE_TYPE:
298 fprintf(output, "\t");
299 writeCoord(output, p[0]);
300 fprintf(output, ";\n");
301 if (colorCode)
302 fprintf(output, "\t\t%c;\n", colorCode);
303 break;
304 case (int) QuadraticSegment::EDGE_TYPE:
305 fprintf(output, "\t");
306 writeCoord(output, p[0]);
307 fprintf(output, ";\n\t\t");
308 if (colorCode)
309 fprintf(output, "%c", colorCode);
310 fprintf(output, "(");
311 writeCoord(output, p[1]);
312 fprintf(output, ");\n");
313 break;
314 case (int) CubicSegment::EDGE_TYPE:
315 fprintf(output, "\t");
316 writeCoord(output, p[0]);
317 fprintf(output, ";\n\t\t");
318 if (colorCode)
319 fprintf(output, "%c", colorCode);
320 fprintf(output, "(");
321 writeCoord(output, p[1]);
322 fprintf(output, "; ");
323 writeCoord(output, p[2]);
324 fprintf(output, ");\n");
325 break;
326 }
327 }
328 fprintf(output, "\t#\n");
329 }
330 fprintf(output, "}\n");
331 }
332 return true;
333 }
334
335 }