2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
23 #define _USE_MATH_DEFINES //Math Constants are not defined in Standard C/C++.
28 #include "tvgSvgLoaderCommon.h"
29 #include "tvgSvgPath.h"
30 #include "tvgSvgUtil.h"
32 /************************************************************************/
33 /* Internal Class Implementation */
34 /************************************************************************/
36 static char* _skipComma(const char* content)
38 while (*content && isspace(*content)) {
41 if (*content == ',') return (char*)content + 1;
42 return (char*)content;
46 static bool _parseNumber(char** content, float* number)
49 *number = svgUtilStrtof(*content, &end);
50 //If the start of string is not number
51 if ((*content) == end) return false;
53 *content = _skipComma(end);
58 static bool _parseFlag(char** content, int* number)
61 *number = strtol(*content, &end, 10);
62 //If the start of string is not number or a number was a float
63 if ((*content) == end || *end == '.') return false;
64 //If a flag has a different value than 0 or 1
65 if (*number != 0 && *number != 1) return false;
66 *content = _skipComma(end);
70 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)
72 float cxp, cyp, cx, cy;
82 float theta1, deltaTheta;
85 float cosPhiRx, cosPhiRy;
86 float sinPhiRx, sinPhiRy;
87 float cosTheta1, sinTheta1;
90 //Some helpful stuff is available here:
91 //http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
95 //If start and end points are identical, then no arc is drawn
96 if ((fabsf(x - sx) < (1.0f / 256.0f)) && (fabsf(y - sy) < (1.0f / 256.0f))) return;
98 //Correction of out-of-range radii, see F6.6.1 (step 2)
102 angle = angle * M_PI / 180.0f;
103 cosPhi = cosf(angle);
104 sinPhi = sinf(angle);
105 dx2 = (sx - x) / 2.0f;
106 dy2 = (sy - y) / 2.0f;
107 x1p = cosPhi * dx2 + sinPhi * dy2;
108 y1p = cosPhi * dy2 - sinPhi * dx2;
113 lambda = (x1p2 / rx2) + (y1p2 / ry2);
115 //Correction of out-of-range radii, see F6.6.2 (step 4)
118 float lambdaRoot = sqrt(lambda);
127 c = (rx2 * ry2) - (rx2 * y1p2) - (ry2 * x1p2);
129 //Check if there is no possible solution
130 //(i.e. we can't do a square root of a negative value)
132 //Scale uniformly until we have a single solution
133 //(see F6.2) i.e. when c == 0.0
134 float scale = sqrt(1.0f - c / (rx2 * ry2));
141 //Step 2 (F6.5.2) - simplified since c == 0.0
144 //Step 3 (F6.5.3 first part) - simplified since cxp and cyp == 0.0
148 //Complete c calculation
149 c = sqrt(c / ((rx2 * y1p2) + (ry2 * x1p2)));
150 //Inverse sign if Fa == Fs
151 if (largeArc == sweep) c = -c;
154 cxp = c * (rx * y1p / ry);
155 cyp = c * (-ry * x1p / rx);
157 //Step 3 (F6.5.3 first part)
158 cx = cosPhi * cxp - sinPhi * cyp;
159 cy = sinPhi * cxp + cosPhi * cyp;
162 //Step 3 (F6.5.3 second part) we now have the center point of the ellipse
163 cx += (sx + x) / 2.0f;
164 cy += (sy + y) / 2.0f;
167 //We dont' use arccos (as per w3c doc), see
168 //http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
169 //Note: atan2 (0.0, 1.0) == 0.0
170 at = atan2(((y1p - cyp) / ry), ((x1p - cxp) / rx));
171 theta1 = (at < 0.0f) ? 2.0f * M_PI + at : at;
173 nat = atan2(((-y1p - cyp) / ry), ((-x1p - cxp) / rx));
174 deltaTheta = (nat < at) ? 2.0f * M_PI - at + nat : nat - at;
177 //Ensure delta theta < 0 or else add 360 degrees
178 if (deltaTheta < 0.0f) deltaTheta += 2.0f * M_PI;
180 //Ensure delta theta > 0 or else substract 360 degrees
181 if (deltaTheta > 0.0f) deltaTheta -= 2.0f * M_PI;
184 //Add several cubic bezier to approximate the arc
185 //(smaller than 90 degrees)
186 //We add one extra segment because we want something
187 //Smaller than 90deg (i.e. not 90 itself)
188 segments = (int)(fabsf(deltaTheta / float(M_PI_2))) + 1.0f;
189 delta = deltaTheta / segments;
191 //http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13)
192 bcp = 4.0f / 3.0f * (1.0f - cos(delta / 2.0f)) / sin(delta / 2.0f);
194 cosPhiRx = cosPhi * rx;
195 cosPhiRy = cosPhi * ry;
196 sinPhiRx = sinPhi * rx;
197 sinPhiRy = sinPhi * ry;
199 cosTheta1 = cos(theta1);
200 sinTheta1 = sin(theta1);
202 for (int i = 0; i < segments; ++i) {
203 //End angle (for this segment) = current + delta
204 float c1x, c1y, ex, ey, c2x, c2y;
205 float theta2 = theta1 + delta;
206 float cosTheta2 = cos(theta2);
207 float sinTheta2 = sin(theta2);
210 //First control point (based on start point sx,sy)
211 c1x = sx - bcp * (cosPhiRx * sinTheta1 + sinPhiRy * cosTheta1);
212 c1y = sy + bcp * (cosPhiRy * cosTheta1 - sinPhiRx * sinTheta1);
214 //End point (for this segment)
215 ex = cx + (cosPhiRx * cosTheta2 - sinPhiRy * sinTheta2);
216 ey = cy + (sinPhiRx * cosTheta2 + cosPhiRy * sinTheta2);
218 //Second control point (based on end point ex,ey)
219 c2x = ex + bcp * (cosPhiRx * sinTheta2 + sinPhiRy * cosTheta2);
220 c2y = ey + bcp * (sinPhiRx * sinTheta2 - cosPhiRy * cosTheta2);
221 cmds->push(PathCommand::CubicTo);
231 //Next start point is the current end point (same for angle)
235 //Avoid recomputations
236 cosTheta1 = cosTheta2;
237 sinTheta1 = sinTheta2;
241 static int _numberCount(char cmd)
287 static void _processCommand(Array<PathCommand>* cmds, Array<Point>* pts, char cmd, float* arr, int count, Point* cur, Point* curCtl, Point* startPoint, bool *isQuadratic)
296 for (int i = 0; i < count - 1; i += 2) {
297 arr[i] = arr[i] + cur->x;
298 arr[i + 1] = arr[i + 1] + cur->y;
303 arr[0] = arr[0] + cur->x;
307 arr[0] = arr[0] + cur->y;
311 arr[5] = arr[5] + cur->x;
312 arr[6] = arr[6] + cur->y;
323 Point p = {arr[0], arr[1]};
324 cmds->push(PathCommand::MoveTo);
326 *cur = {arr[0], arr[1]};
327 *startPoint = {arr[0], arr[1]};
332 Point p = {arr[0], arr[1]};
333 cmds->push(PathCommand::LineTo);
335 *cur = {arr[0], arr[1]};
341 cmds->push(PathCommand::CubicTo);
342 p[0] = {arr[0], arr[1]};
343 p[1] = {arr[2], arr[3]};
344 p[2] = {arr[4], arr[5]};
350 *isQuadratic = false;
356 if ((cmds->count > 1) && (cmds->data[cmds->count - 1] == PathCommand::CubicTo) &&
358 ctrl.x = 2 * cur->x - curCtl->x;
359 ctrl.y = 2 * cur->y - curCtl->y;
363 cmds->push(PathCommand::CubicTo);
365 p[1] = {arr[0], arr[1]};
366 p[2] = {arr[2], arr[3]};
372 *isQuadratic = false;
378 float ctrl_x0 = (cur->x + 2 * arr[0]) * (1.0 / 3.0);
379 float ctrl_y0 = (cur->y + 2 * arr[1]) * (1.0 / 3.0);
380 float ctrl_x1 = (arr[2] + 2 * arr[0]) * (1.0 / 3.0);
381 float ctrl_y1 = (arr[3] + 2 * arr[1]) * (1.0 / 3.0);
382 cmds->push(PathCommand::CubicTo);
383 p[0] = {ctrl_x0, ctrl_y0};
384 p[1] = {ctrl_x1, ctrl_y1};
385 p[2] = {arr[2], arr[3]};
389 *curCtl = {arr[0], arr[1]};
397 if ((cmds->count > 1) && (cmds->data[cmds->count - 1] == PathCommand::CubicTo) &&
399 ctrl.x = 2 * cur->x - curCtl->x;
400 ctrl.y = 2 * cur->y - curCtl->y;
404 float ctrl_x0 = (cur->x + 2 * ctrl.x) * (1.0 / 3.0);
405 float ctrl_y0 = (cur->y + 2 * ctrl.y) * (1.0 / 3.0);
406 float ctrl_x1 = (arr[0] + 2 * ctrl.x) * (1.0 / 3.0);
407 float ctrl_y1 = (arr[1] + 2 * ctrl.y) * (1.0 / 3.0);
408 cmds->push(PathCommand::CubicTo);
409 p[0] = {ctrl_x0, ctrl_y0};
410 p[1] = {ctrl_x1, ctrl_y1};
411 p[2] = {arr[0], arr[1]};
415 *curCtl = {ctrl.x, ctrl.y};
422 Point p = {arr[0], cur->y};
423 cmds->push(PathCommand::LineTo);
430 Point p = {cur->x, arr[0]};
431 cmds->push(PathCommand::LineTo);
438 cmds->push(PathCommand::Close);
444 _pathAppendArcTo(cmds, pts, cur, curCtl, arr[5], arr[6], arr[0], arr[1], arr[2], arr[3], arr[4]);
445 *cur = *curCtl = {arr[5], arr[6]};
446 *isQuadratic = false;
456 static char* _nextCommand(char* path, char* cmd, float* arr, int* count)
460 path = _skipComma(path);
461 if (isalpha(*path)) {
464 *count = _numberCount(*cmd);
466 if (*cmd == 'm') *cmd = 'l';
467 else if (*cmd == 'M') *cmd = 'L';
470 //Special case for arc command
471 if (_parseNumber(&path, &arr[0])) {
472 if (_parseNumber(&path, &arr[1])) {
473 if (_parseNumber(&path, &arr[2])) {
474 if (_parseFlag(&path, &large)) {
475 if (_parseFlag(&path, &sweep)) {
476 if (_parseNumber(&path, &arr[5])) {
477 if (_parseNumber(&path, &arr[6])) {
491 for (int i = 0; i < *count; i++) {
492 if (!_parseNumber(&path, &arr[i])) {
496 path = _skipComma(path);
502 /************************************************************************/
503 /* External Class Implementation */
504 /************************************************************************/
507 bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point>& pts)
509 float numberArray[7];
511 Point cur = { 0, 0 };
512 Point curCtl = { 0, 0 };
513 Point startPoint = { 0, 0 };
515 bool isQuadratic = false;
516 char* path = (char*)svgPath;
519 curLocale = setlocale(LC_NUMERIC, NULL);
520 if (curLocale) curLocale = strdup(curLocale);
521 setlocale(LC_NUMERIC, "POSIX");
523 while ((path[0] != '\0')) {
524 path = _nextCommand(path, &cmd, numberArray, &numberCount);
526 _processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl, &startPoint, &isQuadratic);
529 setlocale(LC_NUMERIC, curLocale);
530 if (curLocale) free(curLocale);