typedef SvgNode* (*FactoryMethod)(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func);
typedef SvgStyleGradient* (*GradientFactoryMethod)(SvgLoaderData* loader, const char* buf, unsigned bufLength);
+static void _postponeCloneNode(Array<SvgNodeIdPair>* nodes, SvgNode *node, char* id);
static char* _skipSpace(const char* str, const char* end)
{
}
+//TODO: check SVG2 standard - should the geometric properties be copied?
static void _copyCssStyleAttr(SvgNode* to, const SvgNode* from)
{
//Copy matrix attribute
if (*cssClass && value) free(*cssClass);
*cssClass = _copyId(value);
- //TODO: works only if style was defined before it is used
+ bool cssClassFound = false;
+
+ //css styling: tag.name has higher priority than .name
if (auto cssNode = _findCssStyleNode(loader->cssStyle, *cssClass, node->type)) {
- //TODO: check SVG2 standard - should the geometric properties be copied?
+ cssClassFound = true;
_copyCssStyleAttr(node, cssNode);
}
if (auto cssNode = _findCssStyleNode(loader->cssStyle, *cssClass)) {
+ cssClassFound = true;
_copyCssStyleAttr(node, cssNode);
}
+
+ if (!cssClassFound) _postponeCloneNode(&loader->cloneCssStyleNodes, node, *cssClass);
}
}
-static void _postponeCloneNode(SvgLoaderData* loader, SvgNode *node, char* id)
+static void _postponeCloneNode(Array<SvgNodeIdPair>* nodes, SvgNode *node, char* id)
{
- loader->cloneNodes.push({node, id});
+ nodes->push({node, id});
}
//some svg export software include <defs> element at the end of the file
//if so the 'from' element won't be found now and we have to repeat finding
//after the whole file is parsed
- _postponeCloneNode(loader, node, id);
+ _postponeCloneNode(&loader->cloneNodes, node, id);
}
} else {
return _attrParseGNode(data, key, value);
}
+static void _clonePostponedCssStyleNodes(Array<SvgNodeIdPair>* cloneCssStyleNodes, SvgNode* cssStyle)
+{
+ for (uint32_t i = 0; i < cloneCssStyleNodes->count; ++i) {
+ auto nodeIdPair = cloneCssStyleNodes->data[i];
+
+ //css styling: tag.name has higher priority than .name
+ if (auto cssNode = _findCssStyleNode(cssStyle, nodeIdPair.id, nodeIdPair.node->type)) {
+ _copyCssStyleAttr(nodeIdPair.node, cssNode);
+ }
+ if (auto cssNode = _findCssStyleNode(cssStyle, nodeIdPair.id)) {
+ _copyCssStyleAttr(nodeIdPair.node, cssNode);
+ }
+ }
+}
+
+
static void _freeNodeStyle(SvgStyleProperty* style)
{
if (!style) return;
if (loaderData.gradients.count > 0) _updateGradient(loaderData.doc, &loaderData.gradients);
if (defs) _updateGradient(loaderData.doc, &defs->node.defs.gradients);
+ if (loaderData.cloneCssStyleNodes.count > 0) _clonePostponedCssStyleNodes(&loaderData.cloneCssStyleNodes, loaderData.cssStyle);
//TODO: defs should be updated as well?
if (loaderData.cssStyle) _updateCssStyle(loaderData.doc, loaderData.cssStyle);
}