svg path: changes in processing cmds T,Q,S

Change-Id: I979bb8cbcbdd4fd1374205feac6acedc58bdd3e1
This commit is contained in:
Mira Grudzinska 2020-08-16 15:23:42 +02:00 committed by Hermet Park
parent 60f2ece182
commit 5905c0c323

View file

@ -239,7 +239,9 @@ static int _numberCount(char cmd)
case 'M': case 'M':
case 'm': case 'm':
case 'L': case 'L':
case 'l': { case 'l':
case 'T':
case 't': {
count = 2; count = 2;
break; break;
} }
@ -260,9 +262,7 @@ static int _numberCount(char cmd)
case 'S': case 'S':
case 's': case 's':
case 'Q': case 'Q':
case 'q': case 'q': {
case 'T':
case 't': {
count = 4; count = 4;
break; break;
} }
@ -278,7 +278,7 @@ static int _numberCount(char cmd)
} }
static void _processCommand(vector<PathCommand>* cmds, vector<Point>* pts, char cmd, float* arr, int count, Point* cur, Point* curCtl) static void _processCommand(vector<PathCommand>* cmds, vector<Point>* pts, char cmd, float* arr, int count, Point* cur, Point* curCtl, bool *isQuadratic)
{ {
int i; int i;
switch (cmd) { switch (cmd) {
@ -341,12 +341,14 @@ static void _processCommand(vector<PathCommand>* cmds, vector<Point>* pts, char
pts->push_back(p[2]); pts->push_back(p[2]);
*curCtl = p[1]; *curCtl = p[1];
*cur = p[2]; *cur = p[2];
*isQuadratic = false;
break; break;
} }
case 's': case 's':
case 'S': { case 'S': {
Point p[3], ctrl; Point p[3], ctrl;
if ((cmds->size() > 1) && (cmds->at(cmds->size() - 1) == PathCommand::CubicTo)) { if ((cmds->size() > 1) && (cmds->at(cmds->size() - 1) == PathCommand::CubicTo) &&
!(*isQuadratic)) {
ctrl.x = 2 * cur->x - curCtl->x; ctrl.x = 2 * cur->x - curCtl->x;
ctrl.y = 2 * cur->y - curCtl->y; ctrl.y = 2 * cur->y - curCtl->y;
} else { } else {
@ -361,6 +363,7 @@ static void _processCommand(vector<PathCommand>* cmds, vector<Point>* pts, char
pts->push_back(p[2]); pts->push_back(p[2]);
*curCtl = p[1]; *curCtl = p[1];
*cur = p[2]; *cur = p[2];
*isQuadratic = false;
break; break;
} }
case 'q': case 'q':
@ -377,16 +380,35 @@ static void _processCommand(vector<PathCommand>* cmds, vector<Point>* pts, char
pts->push_back(p[0]); pts->push_back(p[0]);
pts->push_back(p[1]); pts->push_back(p[1]);
pts->push_back(p[2]); pts->push_back(p[2]);
*curCtl = p[1]; *curCtl = {arr[0], arr[1]};
*cur = p[2]; *cur = p[2];
*isQuadratic = true;
break; break;
} }
case 't': case 't':
case 'T': { case 'T': {
Point p = {arr[0], arr[1]}; Point p[3], ctrl;
cmds->push_back(PathCommand::MoveTo); if ((cmds->size() > 1) && (cmds->at(cmds->size() - 1) == PathCommand::CubicTo) &&
pts->push_back(p); *isQuadratic) {
*cur = {arr[0] ,arr[1]}; ctrl.x = 2 * cur->x - curCtl->x;
ctrl.y = 2 * cur->y - curCtl->y;
} else {
ctrl = *cur;
}
float ctrl_x0 = (cur->x + 2 * ctrl.x) * (1.0 / 3.0);
float ctrl_y0 = (cur->y + 2 * ctrl.y) * (1.0 / 3.0);
float ctrl_x1 = (arr[0] + 2 * ctrl.x) * (1.0 / 3.0);
float ctrl_y1 = (arr[1] + 2 * ctrl.y) * (1.0 / 3.0);
cmds->push_back(PathCommand::CubicTo);
p[0] = {ctrl_x0, ctrl_y0};
p[1] = {ctrl_x1, ctrl_y1};
p[2] = {arr[0], arr[1]};
pts->push_back(p[0]);
pts->push_back(p[1]);
pts->push_back(p[2]);
*curCtl = {ctrl.x, ctrl.y};
*cur = p[2];
*isQuadratic = true;
break; break;
} }
case 'h': case 'h':
@ -479,6 +501,7 @@ tuple<vector<PathCommand>, vector<Point>> svgPathToTvgPath(const char* svgPath)
Point cur = { 0, 0 }; Point cur = { 0, 0 };
Point curCtl = { 0, 0 }; Point curCtl = { 0, 0 };
char cmd = 0; char cmd = 0;
bool isQuadratic = false;
char* path = (char*)svgPath; char* path = (char*)svgPath;
char* curLocale; char* curLocale;
@ -489,7 +512,7 @@ tuple<vector<PathCommand>, vector<Point>> svgPathToTvgPath(const char* svgPath)
while ((path[0] != '\0')) { while ((path[0] != '\0')) {
path = _nextCommand(path, &cmd, numberArray, &numberCount); path = _nextCommand(path, &cmd, numberArray, &numberCount);
if (!path) break; if (!path) break;
_processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl); _processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl, &isQuadratic);
} }
setlocale(LC_NUMERIC, curLocale); setlocale(LC_NUMERIC, curLocale);