mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 05:33:36 +00:00
svg_loader: Fix <stop> being registered in closed latestGradient
If stop is declared immediately after Gradient is closed, the stop is registered in latestGradient. Change latestGradient to a Stack and add a stop only to the last added gradient. When that gradient is closed, no more stops should be registered for that gradient. And gradients do not allow nested declarations, so only the earliest declared Gradient is valid.
This commit is contained in:
parent
d7db54ea5e
commit
fda24c3f59
2 changed files with 24 additions and 17 deletions
|
@ -3368,6 +3368,13 @@ static void _svgLoaderParserXmlClose(SvgLoaderData* loader, const char* content,
|
|||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < sizeof(gradientTags) / sizeof(gradientTags[0]); i++) {
|
||||
if (!strncmp(tagName, gradientTags[i].tag, sz)) {
|
||||
loader->gradientStack.pop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < sizeof(graphicsTags) / sizeof(graphicsTags[0]); i++) {
|
||||
if (!strncmp(tagName, graphicsTags[i].tag, sz)) {
|
||||
loader->currentGraphicsNode = nullptr;
|
||||
|
@ -3439,7 +3446,6 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
|
|||
if (node->type != SvgNodeType::Defs || !empty) {
|
||||
loader->stack.push(node);
|
||||
}
|
||||
loader->latestGradient = nullptr;
|
||||
} else if ((method = _findGraphicsFactory(tagName))) {
|
||||
if (loader->stack.count > 0) parent = loader->stack.last();
|
||||
else parent = loader->doc;
|
||||
|
@ -3450,10 +3456,11 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
|
|||
loader->stack.push(defs);
|
||||
loader->currentGraphicsNode = node;
|
||||
}
|
||||
loader->latestGradient = nullptr;
|
||||
} else if ((gradientMethod = _findGradientFactory(tagName))) {
|
||||
SvgStyleGradient* gradient;
|
||||
gradient = gradientMethod(loader, attrs, attrsLength);
|
||||
//Gradients do not allow nested declarations, so only the earliest declared Gradient is valid.
|
||||
if (loader->gradientStack.count == 0) {
|
||||
//FIXME: The current parsing structure does not distinguish end tags.
|
||||
// There is no way to know if the currently parsed gradient is in defs.
|
||||
// If a gradient is declared outside of defs after defs is set, it is included in the gradients of defs.
|
||||
|
@ -3465,9 +3472,10 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
|
|||
} else {
|
||||
loader->gradients.push(gradient);
|
||||
}
|
||||
loader->latestGradient = gradient;
|
||||
}
|
||||
if (!empty) loader->gradientStack.push(gradient);
|
||||
} else if (!strcmp(tagName, "stop")) {
|
||||
if (!loader->latestGradient) {
|
||||
if (loader->gradientStack.count == 0) {
|
||||
TVGLOG("SVG", "Stop element is used outside of the Gradient element");
|
||||
return;
|
||||
}
|
||||
|
@ -3475,9 +3483,8 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
|
|||
loader->svgParse->gradStop = {0.0f, 0, 0, 0, 255};
|
||||
loader->svgParse->flags = SvgStopStyleFlags::StopDefault;
|
||||
simpleXmlParseAttributes(attrs, attrsLength, _attrParseStops, loader);
|
||||
loader->latestGradient->stops.push(loader->svgParse->gradStop);
|
||||
loader->gradientStack.last()->stops.push(loader->svgParse->gradStop);
|
||||
} else {
|
||||
loader->latestGradient = nullptr;
|
||||
if (!isIgnoreUnsupportedLogElements(tagName)) TVGLOG("SVG", "Unsupported elements used [Elements: %s]", tagName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -577,7 +577,7 @@ struct SvgLoaderData
|
|||
SvgNode* def = nullptr; //also used to store nested graphic nodes
|
||||
SvgNode* cssStyle = nullptr;
|
||||
Array<SvgStyleGradient*> gradients;
|
||||
SvgStyleGradient* latestGradient = nullptr; //For stops
|
||||
Array<SvgStyleGradient*> gradientStack; //For stops
|
||||
SvgParser* svgParse = nullptr;
|
||||
Array<SvgNodeIdPair> cloneNodes;
|
||||
Array<SvgNodeIdPair> nodesToStyle;
|
||||
|
|
Loading…
Add table
Reference in a new issue