From 3e7888b5387a57568c558afc59526d5e672e1859 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 30 Jun 2021 13:58:58 +0900 Subject: [PATCH] svg_loader: code refactoring use color structure to keep neat & compact code. --- src/loaders/svg/tvgSvgLoader.cpp | 20 ++++++-------------- src/loaders/svg/tvgSvgLoaderCommon.h | 15 +++++++++------ src/loaders/svg/tvgSvgSceneBuilder.cpp | 8 ++++---- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/loaders/svg/tvgSvgLoader.cpp b/src/loaders/svg/tvgSvgLoader.cpp index 5e6c1349..a7e173c6 100644 --- a/src/loaders/svg/tvgSvgLoader.cpp +++ b/src/loaders/svg/tvgSvgLoader.cpp @@ -774,7 +774,7 @@ static void _handlePaintAttr(SvgPaint* paint, const char* value) paint->curColor = true; return; } - _toColor(value, &paint->r, &paint->g, &paint->b, &paint->url); + _toColor(value, &paint->color.r, &paint->color.g, &paint->color.b, &paint->url); } @@ -782,7 +782,7 @@ static void _handleColorAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, co { SvgStyleProperty* style = node->style; style->curColorSet = true; - _toColor(value, &style->r, &style->g, &style->b, nullptr); + _toColor(value, &style->color.r, &style->color.g, &style->color.b, nullptr); } @@ -2286,16 +2286,12 @@ static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* paren //Inherit the property of parent if not present in child. //Fill if (!((int)child->fill.flags & (int)SvgFillFlags::Paint)) { - child->fill.paint.r = parent->fill.paint.r; - child->fill.paint.g = parent->fill.paint.g; - child->fill.paint.b = parent->fill.paint.b; + child->fill.paint.color = parent->fill.paint.color; child->fill.paint.none = parent->fill.paint.none; child->fill.paint.curColor = parent->fill.paint.curColor; if (parent->fill.paint.url) child->fill.paint.url = _copyId(parent->fill.paint.url->c_str()); } else if (child->fill.paint.curColor && !child->curColorSet) { - child->r = parent->r; - child->g = parent->g; - child->b = parent->b; + child->color = parent->color; } if (!((int)child->fill.flags & (int)SvgFillFlags::Opacity)) { child->fill.opacity = parent->fill.opacity; @@ -2305,16 +2301,12 @@ static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* paren } //Stroke if (!((int)child->stroke.flags & (int)SvgStrokeFlags::Paint)) { - child->stroke.paint.r = parent->stroke.paint.r; - child->stroke.paint.g = parent->stroke.paint.g; - child->stroke.paint.b = parent->stroke.paint.b; + child->stroke.paint.color = parent->stroke.paint.color; child->stroke.paint.none = parent->stroke.paint.none; child->stroke.paint.curColor = parent->stroke.paint.curColor; child->stroke.paint.url = parent->stroke.paint.url ? _copyId(parent->stroke.paint.url->c_str()) : nullptr; } else if (child->stroke.paint.curColor && !child->curColorSet) { - child->r = parent->r; - child->g = parent->g; - child->b = parent->b; + child->color = parent->color; } if (!((int)child->stroke.flags & (int)SvgStrokeFlags::Opacity)) { child->stroke.opacity = parent->stroke.opacity; diff --git a/src/loaders/svg/tvgSvgLoaderCommon.h b/src/loaders/svg/tvgSvgLoaderCommon.h index f0a1985f..7c1416e4 100644 --- a/src/loaders/svg/tvgSvgLoaderCommon.h +++ b/src/loaders/svg/tvgSvgLoaderCommon.h @@ -219,13 +219,18 @@ struct SvgComposite SvgNode* node; }; +struct SvgColor +{ + uint8_t r; + uint8_t g; + uint8_t b; +}; + struct SvgPaint { SvgStyleGradient* gradient; string *url; - uint8_t r; - uint8_t g; - uint8_t b; + SvgColor color; bool none; bool curColor; }; @@ -277,9 +282,7 @@ struct SvgStyleProperty SvgStyleStroke stroke; SvgComposite comp; int opacity; - uint8_t r; - uint8_t g; - uint8_t b; + SvgColor color; bool curColorSet; }; diff --git a/src/loaders/svg/tvgSvgSceneBuilder.cpp b/src/loaders/svg/tvgSvgSceneBuilder.cpp index 28f96ece..4766b0cf 100644 --- a/src/loaders/svg/tvgSvgSceneBuilder.cpp +++ b/src/loaders/svg/tvgSvgSceneBuilder.cpp @@ -219,10 +219,10 @@ static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float v } } else if (style->fill.paint.curColor) { //Apply the current style color - vg->fill(style->r, style->g, style->b, style->fill.opacity); + vg->fill(style->color.r, style->color.g, style->color.b, style->fill.opacity); } else { //Apply the fill color - vg->fill(style->fill.paint.r, style->fill.paint.g, style->fill.paint.b, style->fill.opacity); + vg->fill(style->fill.paint.color.r, style->fill.paint.color.g, style->fill.paint.color.b, style->fill.opacity); } //Apply the fill rule @@ -258,10 +258,10 @@ static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float v //TODO: Apply the color pointed by url } else if (style->stroke.paint.curColor) { //Apply the current style color - vg->stroke(style->r, style->g, style->b, style->stroke.opacity); + vg->stroke(style->color.r, style->color.g, style->color.b, style->stroke.opacity); } else { //Apply the stroke color - vg->stroke(style->stroke.paint.r, style->stroke.paint.g, style->stroke.paint.b, style->stroke.opacity); + vg->stroke(style->stroke.paint.color.r, style->stroke.paint.color.g, style->stroke.paint.color.b, style->stroke.opacity); } _applyComposition(vg, node, vx, vy, vw, vh);