svg_loader: css style node introduced

For now it is assumed that only one style element is defined
in an svg file, although it can be easily changed if needed.
The style node will be used to define the style applied to a node
of a given type or in a case when a class attrib was used.
This commit is contained in:
Mira Grudzinska 2022-01-09 21:31:24 +01:00 committed by Hermet Park
parent 828a15cfdb
commit 0f51d798ce
2 changed files with 38 additions and 2 deletions

View file

@ -1102,6 +1102,21 @@ static bool _attrParseMaskNode(void* data, const char* key, const char* value)
}
static bool _attrParseCssStyleNode(void* data, const char* key, const char* value)
{
SvgLoaderData* loader = (SvgLoaderData*)data;
SvgNode* node = loader->svgParse->node;
if (!strcmp(key, "id")) {
if (node->id && value) free(node->id);
node->id = _copyId(value);
} else {
return _parseStyleAttr(loader, key, value, false);
}
return true;
}
static SvgNode* _createNode(SvgNode* parent, SvgNodeType type)
{
SvgNode* node = (SvgNode*)calloc(1, sizeof(SvgNode));
@ -1226,6 +1241,17 @@ static SvgNode* _createClipPathNode(SvgLoaderData* loader, SvgNode* parent, cons
return loader->svgParse->node;
}
static SvgNode* _createCssStyleNode(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength)
{
loader->svgParse->node = _createNode(parent, SvgNodeType::CssStyle);
if (!loader->svgParse->node) return nullptr;
simpleXmlParseAttributes(buf, bufLength, _attrParseCssStyleNode, loader);
return loader->svgParse->node;
}
static bool _attrParsePathNode(void* data, const char* key, const char* value)
{
SvgLoaderData* loader = (SvgLoaderData*)data;
@ -2096,7 +2122,8 @@ static constexpr struct
{"g", sizeof("g"), _createGNode},
{"svg", sizeof("svg"), _createSvgNode},
{"mask", sizeof("mask"), _createMaskNode},
{"clipPath", sizeof("clipPath"), _createClipPathNode}
{"clipPath", sizeof("clipPath"), _createClipPathNode},
{"style", sizeof("style"), _createCssStyleNode}
};
@ -2526,7 +2553,8 @@ static constexpr struct
{"svg", sizeof("svg")},
{"defs", sizeof("defs")},
{"mask", sizeof("mask")},
{"clipPath", sizeof("clipPath")}
{"clipPath", sizeof("clipPath")},
{"style", sizeof("style")}
};
@ -2585,6 +2613,7 @@ static void _svgLoaderParserXmlOpen(SvgLoaderData* loader, const char* content,
if (loader->stack.count > 0) parent = loader->stack.data[loader->stack.count - 1];
else parent = loader->doc;
node = method(loader, parent, attrs, attrsLength);
if (!strcmp(tagName, "style")) loader->cssStyle = node;
}
if (!node) return;

View file

@ -51,6 +51,7 @@ enum class SvgNodeType
Video,
ClipPath,
Mask,
CssStyle,
Unknown
};
@ -233,6 +234,10 @@ struct SvgMaskNode
bool userSpace;
};
struct SvgCssStyleNode
{
};
struct SvgLinearGradient
{
float x1;
@ -368,6 +373,7 @@ struct SvgNode
SvgImageNode image;
SvgMaskNode mask;
SvgClipNode clip;
SvgCssStyleNode cssStyle;
} node;
bool display;
~SvgNode();
@ -402,6 +408,7 @@ struct SvgLoaderData
Array<SvgNode *> stack = {nullptr, 0, 0};
SvgNode* doc = nullptr;
SvgNode* def = nullptr;
SvgNode* cssStyle = nullptr;
Array<SvgStyleGradient*> gradients;
SvgStyleGradient* latestGradient = nullptr; //For stops
SvgParser* svgParse = nullptr;