mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-10 14:41:50 +00:00
loader svg: code cleanup
Use array instead of individual dynamic array implementation. This also fixes wrong polygon & polyline data access during copy & paste...
This commit is contained in:
parent
95ebbe4339
commit
d72d6aa51f
3 changed files with 21 additions and 50 deletions
|
@ -1611,39 +1611,11 @@ static SvgNode* _createEllipseNode(SvgLoaderData* loader, SvgNode* parent, const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool _attrParsePolygonPoints(const char* str, float** points, int* ptCount)
|
static bool _attrParsePolygonPoints(const char* str, SvgPolygonNode* polygon)
|
||||||
{
|
{
|
||||||
float tmp[50];
|
|
||||||
int tmpCount = 0;
|
|
||||||
int count = 0;
|
|
||||||
float num;
|
float num;
|
||||||
float *pointArray = nullptr, *tmpArray;
|
while (_parseNumber(&str, &num)) polygon->pts.push(num);
|
||||||
|
|
||||||
while (_parseNumber(&str, &num)) {
|
|
||||||
tmp[tmpCount++] = num;
|
|
||||||
if (tmpCount == 50) {
|
|
||||||
tmpArray = (float*)realloc(pointArray, (count + tmpCount) * sizeof(float));
|
|
||||||
if (!tmpArray) goto error_alloc;
|
|
||||||
pointArray = tmpArray;
|
|
||||||
memcpy(&pointArray[count], tmp, tmpCount * sizeof(float));
|
|
||||||
count += tmpCount;
|
|
||||||
tmpCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tmpCount > 0) {
|
|
||||||
tmpArray = (float*)realloc(pointArray, (count + tmpCount) * sizeof(float));
|
|
||||||
if (!tmpArray) goto error_alloc;
|
|
||||||
pointArray = tmpArray;
|
|
||||||
memcpy(&pointArray[count], tmp, tmpCount * sizeof(float));
|
|
||||||
count += tmpCount;
|
|
||||||
}
|
|
||||||
*ptCount = count;
|
|
||||||
*points = pointArray;
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error_alloc:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1660,7 +1632,7 @@ static bool _attrParsePolygonNode(void* data, const char* key, const char* value
|
||||||
else polygon = &(node->node.polyline);
|
else polygon = &(node->node.polyline);
|
||||||
|
|
||||||
if (!strcmp(key, "points")) {
|
if (!strcmp(key, "points")) {
|
||||||
return _attrParsePolygonPoints(value, &polygon->points, &polygon->pointsCount);
|
return _attrParsePolygonPoints(value, polygon);
|
||||||
} else if (!strcmp(key, "style")) {
|
} else if (!strcmp(key, "style")) {
|
||||||
return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader);
|
return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader);
|
||||||
} else if (!strcmp(key, "clip-path")) {
|
} else if (!strcmp(key, "clip-path")) {
|
||||||
|
@ -2936,16 +2908,14 @@ static void _copyAttr(SvgNode* to, const SvgNode* from)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polygon: {
|
case SvgNodeType::Polygon: {
|
||||||
if ((to->node.polygon.pointsCount = from->node.polygon.pointsCount)) {
|
if (to->node.polygon.pts.count = from->node.polygon.pts.count) {
|
||||||
to->node.polygon.points = (float*)malloc(to->node.polygon.pointsCount * sizeof(float));
|
to->node.polygon.pts = from->node.polygon.pts;
|
||||||
memcpy(to->node.polygon.points, from->node.polygon.points, to->node.polygon.pointsCount * sizeof(float));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polyline: {
|
case SvgNodeType::Polyline: {
|
||||||
if ((to->node.polyline.pointsCount = from->node.polyline.pointsCount)) {
|
if (to->node.polyline.pts.count = from->node.polyline.pts.count) {
|
||||||
to->node.polyline.points = (float*)malloc(to->node.polyline.pointsCount * sizeof(float));
|
to->node.polyline.pts = from->node.polyline.pts;
|
||||||
memcpy(to->node.polyline.points, from->node.polyline.points, to->node.polyline.pointsCount * sizeof(float));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -3236,7 +3206,7 @@ static void _inefficientNodeCheck(TVG_UNUSED SvgNode* node)
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polygon:
|
case SvgNodeType::Polygon:
|
||||||
case SvgNodeType::Polyline: {
|
case SvgNodeType::Polyline: {
|
||||||
if (node->node.polygon.pointsCount < 2) TVGLOG("SVG", "Inefficient elements used [Invalid Polygon][Node Type : %s]", type);
|
if (node->node.polygon.pts.count < 2) TVGLOG("SVG", "Inefficient elements used [Invalid Polygon][Node Type : %s]", type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Circle: {
|
case SvgNodeType::Circle: {
|
||||||
|
@ -3392,11 +3362,11 @@ static void _freeNode(SvgNode* node)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polygon: {
|
case SvgNodeType::Polygon: {
|
||||||
free(node->node.polygon.points);
|
free(node->node.polygon.pts.data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polyline: {
|
case SvgNodeType::Polyline: {
|
||||||
free(node->node.polyline.points);
|
free(node->node.polyline.pts.data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Doc: {
|
case SvgNodeType::Doc: {
|
||||||
|
|
|
@ -353,8 +353,7 @@ struct SvgPathNode
|
||||||
|
|
||||||
struct SvgPolygonNode
|
struct SvgPolygonNode
|
||||||
{
|
{
|
||||||
int pointsCount;
|
Array<float> pts;
|
||||||
float* points;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SvgClipNode
|
struct SvgClipNode
|
||||||
|
|
|
@ -402,19 +402,21 @@ static bool _appendShape(SvgNode* node, Shape* shape, const Box& vBox, const str
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polygon: {
|
case SvgNodeType::Polygon: {
|
||||||
if (node->node.polygon.pointsCount < 2) break;
|
if (node->node.polygon.pts.count < 2) break;
|
||||||
shape->moveTo(node->node.polygon.points[0], node->node.polygon.points[1]);
|
auto pts = node->node.polygon.pts.data;
|
||||||
for (int i = 2; i < node->node.polygon.pointsCount - 1; i += 2) {
|
shape->moveTo(pts[0], pts[1]);
|
||||||
shape->lineTo(node->node.polygon.points[i], node->node.polygon.points[i + 1]);
|
for (pts += 2; pts < node->node.polygon.pts.end(); pts += 2) {
|
||||||
|
shape->lineTo(pts[0], pts[1]);
|
||||||
}
|
}
|
||||||
shape->close();
|
shape->close();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SvgNodeType::Polyline: {
|
case SvgNodeType::Polyline: {
|
||||||
if (node->node.polygon.pointsCount < 2) break;
|
if (node->node.polyline.pts.count < 2) break;
|
||||||
shape->moveTo(node->node.polygon.points[0], node->node.polygon.points[1]);
|
auto pts = node->node.polyline.pts.data;
|
||||||
for (int i = 2; i < node->node.polygon.pointsCount - 1; i += 2) {
|
shape->moveTo(pts[0], pts[1]);
|
||||||
shape->lineTo(node->node.polygon.points[i], node->node.polygon.points[i + 1]);
|
for (pts += 2; pts < node->node.polyline.pts.end(); pts += 2) {
|
||||||
|
shape->lineTo(pts[0], pts[1]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue