svg_loader: css style node introduced 12/289012/1
authorMira Grudzinska <m.grudzinska@samsung.com>
Sun, 9 Jan 2022 20:31:24 +0000 (21:31 +0100)
committerMichal Szczecinski <mihashco89@gmail.com>
Mon, 27 Feb 2023 08:45:28 +0000 (09:45 +0100)
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.

Change-Id: I53e95dadfbf8a1a48903bc9f07bd2cbec82a4eda

src/loaders/svg/tvgSvgLoader.cpp
src/loaders/svg/tvgSvgLoaderCommon.h

index 7def62d..2a9cba5 100644 (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;
index 04f5b34..41c4184 100644 (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;