Debian Codegraph @

thirdparty/thorvg/src/loaders/svg/tvgSvgPath.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 /*
24 * Copyright notice for the EFL:
25
26 * Copyright (C) EFL developers (see AUTHORS)
27
28 * All rights reserved.
29
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
32
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38
39 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
45 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
46 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
47 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
48 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 */
50
51 #define _USE_MATH_DEFINES //Math Constants are not defined in Standard C/C++.
52
53 #include <cstring>
54 #include <ctype.h>
55 #include "tvgMath.h"
56 #include "tvgShape.h"
57 #include "tvgSvgLoaderCommon.h"
58 #include "tvgSvgPath.h"
59 #include "tvgStr.h"
60
61 /************************************************************************/
62 /* Internal Class Implementation */
63 /************************************************************************/
64
65 static char* _skipComma(const char* content)
66 {
67 while (*content && isspace(*content)) {
68 content++;
69 }
70 if (*content == ',') return (char*)content + 1;
71 return (char*)content;
72 }
73
74
75 static bool _parseNumber(char** content, float* number)
76 {
77 char* end = NULL;
78 *number = strToFloat(*content, &end);
79 //If the start of string is not number
80 if ((*content) == end) return false;
81 //Skip comma if any
82 *content = _skipComma(end);
83 return true;
84 }
85
86
87 static bool _parseFlag(char** content, int* number)
88 {
89 char* end = NULL;
90 if (*(*content) != '0' && *(*content) != '1') return false;
91 *number = *(*content) - '0';
92 *content += 1;
93 end = *content;
94 *content = _skipComma(end);
95
96 return true;
97 }
98
99
100 void _pathAppendArcTo(Array<PathCommand>* cmds, Array<Point>* pts, Point* cur, Point* curCtl, float x, float y, float rx, float ry, float angle, bool largeArc, bool sweep)
101 {
102 float cxp, cyp, cx, cy;
103 float sx, sy;
104 float cosPhi, sinPhi;
105 float dx2, dy2;
106 float x1p, y1p;
107 float x1p2, y1p2;
108 float rx2, ry2;
109 float lambda;
110 float c;
111 float at;
112 float theta1, deltaTheta;
113 float nat;
114 float delta, bcp;
115 float cosPhiRx, cosPhiRy;
116 float sinPhiRx, sinPhiRy;
117 float cosTheta1, sinTheta1;
118 int segments;
119
120 //Some helpful stuff is available here:
121 //http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
122 sx = cur->x;
123 sy = cur->y;
124
125 //Correction of out-of-range radii, see F6.6.1 (step 2)
126 rx = fabsf(rx);
127 ry = fabsf(ry);
128
129 angle = deg2rad(angle);
130 cosPhi = cosf(angle);
131 sinPhi = sinf(angle);
132 dx2 = (sx - x) / 2.0f;
133 dy2 = (sy - y) / 2.0f;
134 x1p = cosPhi * dx2 + sinPhi * dy2;
135 y1p = cosPhi * dy2 - sinPhi * dx2;
136 x1p2 = x1p * x1p;
137 y1p2 = y1p * y1p;
138 rx2 = rx * rx;
139 ry2 = ry * ry;
140 lambda = (x1p2 / rx2) + (y1p2 / ry2);
141
142 //Correction of out-of-range radii, see F6.6.2 (step 4)
143 if (lambda > 1.0f) {
144 //See F6.6.3
145 float lambdaRoot = sqrtf(lambda);
146
147 rx *= lambdaRoot;
148 ry *= lambdaRoot;
149 //Update rx2 and ry2
150 rx2 = rx * rx;
151 ry2 = ry * ry;
152 }
153
154 c = (rx2 * ry2) - (rx2 * y1p2) - (ry2 * x1p2);
155
156 //Check if there is no possible solution
157 //(i.e. we can't do a square root of a negative value)
158 if (c < 0.0f) {
159 //Scale uniformly until we have a single solution
160 //(see F6.2) i.e. when c == 0.0
161 float scale = sqrtf(1.0f - c / (rx2 * ry2));
162 rx *= scale;
163 ry *= scale;
164 //Update rx2 and ry2
165 rx2 = rx * rx;
166 ry2 = ry * ry;
167
168 //Step 2 (F6.5.2) - simplified since c == 0.0
169 cxp = 0.0f;
170 cyp = 0.0f;
171 //Step 3 (F6.5.3 first part) - simplified since cxp and cyp == 0.0
172 cx = 0.0f;
173 cy = 0.0f;
174 } else {
175 //Complete c calculation
176 c = sqrtf(c / ((rx2 * y1p2) + (ry2 * x1p2)));
177 //Inverse sign if Fa == Fs
178 if (largeArc == sweep) c = -c;
179
180 //Step 2 (F6.5.2)
181 cxp = c * (rx * y1p / ry);
182 cyp = c * (-ry * x1p / rx);
183
184 //Step 3 (F6.5.3 first part)
185 cx = cosPhi * cxp - sinPhi * cyp;
186 cy = sinPhi * cxp + cosPhi * cyp;
187 }
188
189 //Step 3 (F6.5.3 second part) we now have the center point of the ellipse
190 cx += (sx + x) / 2.0f;
191 cy += (sy + y) / 2.0f;
192
193 //Step 4 (F6.5.4)
194 //We dont' use arccos (as per w3c doc), see
195 //http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
196 //Note: atan2 (0.0, 1.0) == 0.0
197 at = tvg::atan2(((y1p - cyp) / ry), ((x1p - cxp) / rx));
198 theta1 = (at < 0.0f) ? 2.0f * MATH_PI + at : at;
199
200 nat = tvg::atan2(((-y1p - cyp) / ry), ((-x1p - cxp) / rx));
201 deltaTheta = (nat < at) ? 2.0f * MATH_PI - at + nat : nat - at;
202
203 if (sweep) {
204 //Ensure delta theta < 0 or else add 360 degrees
205 if (deltaTheta < 0.0f) deltaTheta += 2.0f * MATH_PI;
206 } else {
207 //Ensure delta theta > 0 or else substract 360 degrees
208 if (deltaTheta > 0.0f) deltaTheta -= 2.0f * MATH_PI;
209 }
210
211 //Add several cubic bezier to approximate the arc
212 //(smaller than 90 degrees)
213 //We add one extra segment because we want something
214 //Smaller than 90deg (i.e. not 90 itself)
215 segments = static_cast<int>(fabsf(deltaTheta / MATH_PI2) + 1.0f);
216 delta = deltaTheta / segments;
217
218 //http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13)
219 bcp = 4.0f / 3.0f * (1.0f - cosf(delta / 2.0f)) / sinf(delta / 2.0f);
220
221 cosPhiRx = cosPhi * rx;
222 cosPhiRy = cosPhi * ry;
223 sinPhiRx = sinPhi * rx;
224 sinPhiRy = sinPhi * ry;
225
226 cosTheta1 = cosf(theta1);
227 sinTheta1 = sinf(theta1);
228
229 for (int i = 0; i < segments; ++i) {
230 //End angle (for this segment) = current + delta
231 float c1x, c1y, ex, ey, c2x, c2y;
232 float theta2 = theta1 + delta;
233 float cosTheta2 = cosf(theta2);
234 float sinTheta2 = sinf(theta2);
235 Point p[3];
236
237 //First control point (based on start point sx,sy)
238 c1x = sx - bcp * (cosPhiRx * sinTheta1 + sinPhiRy * cosTheta1);
239 c1y = sy + bcp * (cosPhiRy * cosTheta1 - sinPhiRx * sinTheta1);
240
241 //End point (for this segment)
242 ex = cx + (cosPhiRx * cosTheta2 - sinPhiRy * sinTheta2);
243 ey = cy + (sinPhiRx * cosTheta2 + cosPhiRy * sinTheta2);
244
245 //Second control point (based on end point ex,ey)
246 c2x = ex + bcp * (cosPhiRx * sinTheta2 + sinPhiRy * cosTheta2);
247 c2y = ey + bcp * (sinPhiRx * sinTheta2 - cosPhiRy * cosTheta2);
248 cmds->push(PathCommand::CubicTo);
249 p[0] = {c1x, c1y};
250 p[1] = {c2x, c2y};
251 p[2] = {ex, ey};
252 pts->push(p[0]);
253 pts->push(p[1]);
254 pts->push(p[2]);
255 *curCtl = p[1];
256 *cur = p[2];
257
258 //Next start point is the current end point (same for angle)
259 sx = ex;
260 sy = ey;
261 theta1 = theta2;
262 //Avoid recomputations
263 cosTheta1 = cosTheta2;
264 sinTheta1 = sinTheta2;
265 }
266 }
267
268 static int _numberCount(char cmd)
269 {
270 int count = 0;
271 switch (cmd) {
272 case 'M':
273 case 'm':
274 case 'L':
275 case 'l':
276 case 'T':
277 case 't': {
278 count = 2;
279 break;
280 }
281 case 'C':
282 case 'c':
283 case 'E':
284 case 'e': {
285 count = 6;
286 break;
287 }
288 case 'H':
289 case 'h':
290 case 'V':
291 case 'v': {
292 count = 1;
293 break;
294 }
295 case 'S':
296 case 's':
297 case 'Q':
298 case 'q': {
299 count = 4;
300 break;
301 }
302 case 'A':
303 case 'a': {
304 count = 7;
305 break;
306 }
307 default:
308 break;
309 }
310 return count;
311 }
312
313
314 static bool _processCommand(Array<PathCommand>* cmds, Array<Point>* pts, char cmd, float* arr, int count, Point* cur, Point* curCtl, Point* startPoint, bool *isQuadratic, bool* closed)
315 {
316 switch (cmd) {
317 case 'm':
318 case 'l':
319 case 'c':
320 case 's':
321 case 'q':
322 case 't': {
323 for (int i = 0; i < count - 1; i += 2) {
324 arr[i] = arr[i] + cur->x;
325 arr[i + 1] = arr[i + 1] + cur->y;
326 }
327 break;
328 }
329 case 'h': {
330 arr[0] = arr[0] + cur->x;
331 break;
332 }
333 case 'v': {
334 arr[0] = arr[0] + cur->y;
335 break;
336 }
337 case 'a': {
338 arr[5] = arr[5] + cur->x;
339 arr[6] = arr[6] + cur->y;
340 break;
341 }
342 default: {
343 break;
344 }
345 }
346
347 switch (cmd) {
348 case 'm':
349 case 'M': {
350 Point p = {arr[0], arr[1]};
351 cmds->push(PathCommand::MoveTo);
352 pts->push(p);
353 *cur = {arr[0], arr[1]};
354 *startPoint = {arr[0], arr[1]};
355 break;
356 }
357 case 'l':
358 case 'L': {
359 Point p = {arr[0], arr[1]};
360 cmds->push(PathCommand::LineTo);
361 pts->push(p);
362 *cur = {arr[0], arr[1]};
363 break;
364 }
365 case 'c':
366 case 'C': {
367 Point p[3];
368 cmds->push(PathCommand::CubicTo);
369 p[0] = {arr[0], arr[1]};
370 p[1] = {arr[2], arr[3]};
371 p[2] = {arr[4], arr[5]};
372 pts->push(p[0]);
373 pts->push(p[1]);
374 pts->push(p[2]);
375 *curCtl = p[1];
376 *cur = p[2];
377 *isQuadratic = false;
378 break;
379 }
380 case 's':
381 case 'S': {
382 Point p[3], ctrl;
383 if ((cmds->count > 1) && (cmds->last() == PathCommand::CubicTo) &&
384 !(*isQuadratic)) {
385 ctrl.x = 2 * cur->x - curCtl->x;
386 ctrl.y = 2 * cur->y - curCtl->y;
387 } else {
388 ctrl = *cur;
389 }
390 cmds->push(PathCommand::CubicTo);
391 p[0] = ctrl;
392 p[1] = {arr[0], arr[1]};
393 p[2] = {arr[2], arr[3]};
394 pts->push(p[0]);
395 pts->push(p[1]);
396 pts->push(p[2]);
397 *curCtl = p[1];
398 *cur = p[2];
399 *isQuadratic = false;
400 break;
401 }
402 case 'q':
403 case 'Q': {
404 Point p[3];
405 float ctrl_x0 = (cur->x + 2 * arr[0]) * (1.0f / 3.0f);
406 float ctrl_y0 = (cur->y + 2 * arr[1]) * (1.0f / 3.0f);
407 float ctrl_x1 = (arr[2] + 2 * arr[0]) * (1.0f / 3.0f);
408 float ctrl_y1 = (arr[3] + 2 * arr[1]) * (1.0f / 3.0f);
409 cmds->push(PathCommand::CubicTo);
410 p[0] = {ctrl_x0, ctrl_y0};
411 p[1] = {ctrl_x1, ctrl_y1};
412 p[2] = {arr[2], arr[3]};
413 pts->push(p[0]);
414 pts->push(p[1]);
415 pts->push(p[2]);
416 *curCtl = {arr[0], arr[1]};
417 *cur = p[2];
418 *isQuadratic = true;
419 break;
420 }
421 case 't':
422 case 'T': {
423 Point p[3], ctrl;
424 if ((cmds->count > 1) && (cmds->last() == PathCommand::CubicTo) &&
425 *isQuadratic) {
426 ctrl.x = 2 * cur->x - curCtl->x;
427 ctrl.y = 2 * cur->y - curCtl->y;
428 } else {
429 ctrl = *cur;
430 }
431 float ctrl_x0 = (cur->x + 2 * ctrl.x) * (1.0f / 3.0f);
432 float ctrl_y0 = (cur->y + 2 * ctrl.y) * (1.0f / 3.0f);
433 float ctrl_x1 = (arr[0] + 2 * ctrl.x) * (1.0f / 3.0f);
434 float ctrl_y1 = (arr[1] + 2 * ctrl.y) * (1.0f / 3.0f);
435 cmds->push(PathCommand::CubicTo);
436 p[0] = {ctrl_x0, ctrl_y0};
437 p[1] = {ctrl_x1, ctrl_y1};
438 p[2] = {arr[0], arr[1]};
439 pts->push(p[0]);
440 pts->push(p[1]);
441 pts->push(p[2]);
442 *curCtl = {ctrl.x, ctrl.y};
443 *cur = p[2];
444 *isQuadratic = true;
445 break;
446 }
447 case 'h':
448 case 'H': {
449 Point p = {arr[0], cur->y};
450 cmds->push(PathCommand::LineTo);
451 pts->push(p);
452 cur->x = arr[0];
453 break;
454 }
455 case 'v':
456 case 'V': {
457 Point p = {cur->x, arr[0]};
458 cmds->push(PathCommand::LineTo);
459 pts->push(p);
460 cur->y = arr[0];
461 break;
462 }
463 case 'z':
464 case 'Z': {
465 cmds->push(PathCommand::Close);
466 *cur = *startPoint;
467 *closed = true;
468 break;
469 }
470 case 'a':
471 case 'A': {
472 if (tvg::zero(arr[0]) || tvg::zero(arr[1])) {
473 Point p = {arr[5], arr[6]};
474 cmds->push(PathCommand::LineTo);
475 pts->push(p);
476 *cur = {arr[5], arr[6]};
477 } else if (!tvg::equal(cur->x, arr[5]) || !tvg::equal(cur->y, arr[6])) {
478 _pathAppendArcTo(cmds, pts, cur, curCtl, arr[5], arr[6], fabsf(arr[0]), fabsf(arr[1]), arr[2], arr[3], arr[4]);
479 *cur = *curCtl = {arr[5], arr[6]};
480 *isQuadratic = false;
481 }
482 break;
483 }
484 default: {
485 return false;
486 }
487 }
488 return true;
489 }
490
491
492 static char* _nextCommand(char* path, char* cmd, float* arr, int* count, bool* closed)
493 {
494 int large, sweep;
495
496 path = _skipComma(path);
497 if (isalpha(*path)) {
498 *cmd = *path;
499 path++;
500 *count = _numberCount(*cmd);
501 } else {
502 if (*cmd == 'm') *cmd = 'l';
503 else if (*cmd == 'M') *cmd = 'L';
504 else {
505 if (*closed) return nullptr;
506 }
507 }
508 if (*count == 7) {
509 //Special case for arc command
510 if (_parseNumber(&path, &arr[0])) {
511 if (_parseNumber(&path, &arr[1])) {
512 if (_parseNumber(&path, &arr[2])) {
513 if (_parseFlag(&path, &large)) {
514 if (_parseFlag(&path, &sweep)) {
515 if (_parseNumber(&path, &arr[5])) {
516 if (_parseNumber(&path, &arr[6])) {
517 arr[3] = (float)large;
518 arr[4] = (float)sweep;
519 return path;
520 }
521 }
522 }
523 }
524 }
525 }
526 }
527 *count = 0;
528 return NULL;
529 }
530 for (int i = 0; i < *count; i++) {
531 if (!_parseNumber(&path, &arr[i])) {
532 *count = 0;
533 return NULL;
534 }
535 path = _skipComma(path);
536 }
537 return path;
538 }
539
540
541 /************************************************************************/
542 /* External Class Implementation */
543 /************************************************************************/
544
545
546 bool svgPathToShape(const char* svgPath, Shape* shape)
547 {
548 float numberArray[7];
549 int numberCount = 0;
550 Point cur = { 0, 0 };
551 Point curCtl = { 0, 0 };
552 Point startPoint = { 0, 0 };
553 char cmd = 0;
554 bool isQuadratic = false;
555 bool closed = false;
556 char* path = (char*)svgPath;
557
558 auto& pts = P(shape)->rs.path.pts;
559 auto& cmds = P(shape)->rs.path.cmds;
560 auto lastCmds = cmds.count;
561
562 while ((path[0] != '\0')) {
563 path = _nextCommand(path, &cmd, numberArray, &numberCount, &closed);
564 if (!path) break;
565 closed = false;
566 if (!_processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl, &startPoint, &isQuadratic, &closed)) break;
567 }
568
569 if (cmds.count > lastCmds && cmds[lastCmds] != PathCommand::MoveTo) return false;
570 return true;
571 }