svg_loader: code refactoring

use color structure to keep neat & compact code.
This commit is contained in:
Hermet Park 2021-06-30 13:58:58 +09:00 committed by Hermet Park
parent 8dc5d5204b
commit 3e7888b538
3 changed files with 19 additions and 24 deletions

View file

@ -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;

View file

@ -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;
};

View file

@ -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);