From 05018631957450633bb9664df97dcaab274b7981 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Wed, 3 Nov 2021 01:46:28 +0100 Subject: [PATCH] svg_loader: mask node loaded as a scene Since the mask is a container element (in opposite to the clipPath), it has to be loaded as a scene. --- src/loaders/svg/tvgSvgSceneBuilder.cpp | 29 ++++++++++---------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/loaders/svg/tvgSvgSceneBuilder.cpp b/src/loaders/svg/tvgSvgSceneBuilder.cpp index f36d9df2..3826afe6 100644 --- a/src/loaders/svg/tvgSvgSceneBuilder.cpp +++ b/src/loaders/svg/tvgSvgSceneBuilder.cpp @@ -57,7 +57,7 @@ #include static bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh); -static unique_ptr _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath); +static unique_ptr _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath, bool mask); /************************************************************************/ /* Internal Class Implementation */ @@ -255,19 +255,12 @@ static void _applyComposition(Paint* paint, const SvgNode* node, float vx, float if (compNode && compNode->child.count > 0) { node->style->mask.applying = true; - auto comp = Shape::gen(); - comp->fill(255, 255, 255, 255); - if (node->transform) comp->transform(*node->transform); - - auto child = compNode->child.data; - auto valid = false; //Composite only when valid shapes are existed - - for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) { - if (_appendChildShape(*child, comp.get(), vx, vy, vw, vh)) valid = true; + auto comp = _sceneBuildHelper(compNode, vx, vy, vw, vh, "", true); + if (comp) { + if (node->transform) comp->transform(*node->transform); + paint->composite(move(comp), CompositeMethod::AlphaMask); } - if (valid) paint->composite(move(comp), CompositeMethod::AlphaMask); - node->style->mask.applying = false; } } @@ -553,7 +546,7 @@ static unique_ptr _imageBuildHelper(SvgNode* node, float vx, float vy, static unique_ptr _useBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath) { - auto scene = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath); + auto scene = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath, false); if (node->node.use.x != 0.0f || node->node.use.y != 0.0f) { scene->translate(node->node.use.x, node->node.use.y); } @@ -564,9 +557,9 @@ static unique_ptr _useBuildHelper(const SvgNode* node, float vx, float vy } -static unique_ptr _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath) +static unique_ptr _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh, const string& svgPath, bool mask) { - if (_isGroupType(node->type)) { + if (_isGroupType(node->type) || mask) { auto scene = Scene::gen(); if (node->transform) scene->transform(*node->transform); @@ -577,11 +570,11 @@ static unique_ptr _sceneBuildHelper(const SvgNode* node, float vx, float if ((*child)->type == SvgNodeType::Use) scene->push(_useBuildHelper(*child, vx, vy, vw, vh, svgPath)); else - scene->push(_sceneBuildHelper(*child, vx, vy, vw, vh, svgPath)); + scene->push(_sceneBuildHelper(*child, vx, vy, vw, vh, svgPath, false)); } else if ((*child)->type == SvgNodeType::Image) { auto image = _imageBuildHelper(*child, vx, vy, vw, vh, svgPath); if (image) scene->push(move(image)); - } else { + } else if ((*child)->type != SvgNodeType::Mask) { auto shape = _shapeBuildHelper(*child, vx, vy, vw, vh); if (shape) scene->push(move(shape)); } @@ -603,7 +596,7 @@ unique_ptr svgSceneBuild(SvgNode* node, float vx, float vy, float vw, flo { if (!node || (node->type != SvgNodeType::Doc)) return nullptr; - auto docNode = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath); + auto docNode = _sceneBuildHelper(node, vx, vy, vw, vh, svgPath, false); if (fabsf(w - vw) > FLT_EPSILON || fabsf(h - vh) > FLT_EPSILON) { auto sx = w / vw;