loader/svg: optimize the path conversion.

Push the path data into a given shape directly.
This will skip unnecessary memory copies.

Issue: https://github.com/thorvg/thorvg/issues/1848
This commit is contained in:
Hermet Park 2023-12-20 18:19:05 +09:00
parent a59950ea97
commit bcdf877339
3 changed files with 11 additions and 6 deletions

View file

@ -53,6 +53,7 @@
#include <cstring>
#include <math.h>
#include <ctype.h>
#include "tvgShape.h"
#include "tvgSvgLoaderCommon.h"
#include "tvgSvgPath.h"
#include "tvgStr.h"
@ -534,7 +535,7 @@ static char* _nextCommand(char* path, char* cmd, float* arr, int* count)
/************************************************************************/
bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point>& pts)
bool svgPathToShape(const char* svgPath, Shape* shape)
{
float numberArray[7];
int numberCount = 0;
@ -545,11 +546,16 @@ bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point
bool isQuadratic = false;
char* path = (char*)svgPath;
auto& pts = P(shape)->rs.path.pts;
auto& cmds = P(shape)->rs.path.cmds;
auto lastCmds = cmds.count;
while ((path[0] != '\0')) {
path = _nextCommand(path, &cmd, numberArray, &numberCount);
if (!path) break;
if (!_processCommand(&cmds, &pts, cmd, numberArray, numberCount, &cur, &curCtl, &startPoint, &isQuadratic)) break;
}
if (cmds.count > lastCmds && cmds[lastCmds] != PathCommand::MoveTo) return false;
return true;
}

View file

@ -25,6 +25,6 @@
#include <tvgCommon.h>
bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point>& pts);
bool svgPathToShape(const char* svgPath, Shape* shape);
#endif //_TVG_SVG_PATH_H_

View file

@ -396,10 +396,9 @@ static bool _recognizeShape(SvgNode* node, Shape* shape)
switch (node->type) {
case SvgNodeType::Path: {
if (node->node.path.path) {
Array<PathCommand> cmds;
Array<Point> pts;
if (svgPathToTvgPath(node->node.path.path, cmds, pts)) {
shape->appendPath(cmds.data, cmds.count, pts.data, pts.count);
if (!svgPathToShape(node->node.path.path, shape)) {
TVGERR("SVG", "Invalid path information.");
return false;
}
}
break;