mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-14 20:14:37 +00:00
loaders svg: code refactoring
++ clean code.
This commit is contained in:
parent
6239eca92f
commit
605a3bf175
3 changed files with 25 additions and 56 deletions
|
@ -858,8 +858,7 @@ static void _handleTransformAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node
|
||||||
static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
||||||
{
|
{
|
||||||
SvgStyleProperty* style = node->style;
|
SvgStyleProperty* style = node->style;
|
||||||
style->comp.flags = (SvgCompositeFlags)((int)style->comp.flags | (int)SvgCompositeFlags::ClipPath);
|
style->comp.method = CompositeMethod::ClipPath;
|
||||||
|
|
||||||
int len = strlen(value);
|
int len = strlen(value);
|
||||||
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
||||||
}
|
}
|
||||||
|
@ -867,8 +866,7 @@ static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node,
|
||||||
static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
||||||
{
|
{
|
||||||
SvgStyleProperty* style = node->style;
|
SvgStyleProperty* style = node->style;
|
||||||
style->comp.flags = (SvgCompositeFlags)((int)style->comp.flags | (int)SvgCompositeFlags::AlphaMask);
|
style->comp.method = CompositeMethod::AlphaMask;
|
||||||
|
|
||||||
int len = strlen(value);
|
int len = strlen(value);
|
||||||
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,11 +62,6 @@ enum class SvgLengthType
|
||||||
In,
|
In,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class SvgCompositeFlags
|
|
||||||
{
|
|
||||||
ClipPath = 0x01,
|
|
||||||
AlphaMask = 0x02,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class SvgFillFlags
|
enum class SvgFillFlags
|
||||||
{
|
{
|
||||||
|
@ -227,7 +222,7 @@ struct SvgGradientStop
|
||||||
|
|
||||||
struct SvgComposite
|
struct SvgComposite
|
||||||
{
|
{
|
||||||
SvgCompositeFlags flags;
|
CompositeMethod method; //TODO: Currently support either one method
|
||||||
string *url;
|
string *url;
|
||||||
SvgNode* node;
|
SvgNode* node;
|
||||||
};
|
};
|
||||||
|
|
|
@ -203,6 +203,7 @@ void _appendChildShape(SvgNode* node, Shape* shape, float vx, float vy, float vw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, float vh)
|
void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, float vh)
|
||||||
{
|
{
|
||||||
SvgStyleProperty* style = node->style;
|
SvgStyleProperty* style = node->style;
|
||||||
|
@ -243,8 +244,9 @@ void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, floa
|
||||||
vg->stroke(style->stroke.width);
|
vg->stroke(style->stroke.width);
|
||||||
vg->stroke(style->stroke.cap);
|
vg->stroke(style->stroke.cap);
|
||||||
vg->stroke(style->stroke.join);
|
vg->stroke(style->stroke.join);
|
||||||
if (style->stroke.dash.array.count > 0)
|
if (style->stroke.dash.array.count > 0) {
|
||||||
vg->stroke(style->stroke.dash.array.data, style->stroke.dash.array.count);
|
vg->stroke(style->stroke.dash.array.data, style->stroke.dash.array.count);
|
||||||
|
}
|
||||||
|
|
||||||
//If stroke property is nullptr then do nothing
|
//If stroke property is nullptr then do nothing
|
||||||
if (style->stroke.paint.none) {
|
if (style->stroke.paint.none) {
|
||||||
|
@ -256,34 +258,20 @@ void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, floa
|
||||||
} else if (style->stroke.paint.curColor) {
|
} else if (style->stroke.paint.curColor) {
|
||||||
//Apply the current style color
|
//Apply the current style color
|
||||||
vg->stroke(style->r, style->g, style->b, style->stroke.opacity);
|
vg->stroke(style->r, style->g, style->b, style->stroke.opacity);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//Apply the stroke color
|
//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.r, style->stroke.paint.g, style->stroke.paint.b, style->stroke.opacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Apply composite node
|
//Apply composite node
|
||||||
if (style->comp.node) {
|
if (style->comp.node && (style->comp.method != CompositeMethod::None)) {
|
||||||
//Composite ClipPath
|
auto compNode = style->comp.node;
|
||||||
if (((int)style->comp.flags & (int)SvgCompositeFlags::ClipPath)) {
|
if (compNode->child.count > 0) {
|
||||||
auto compNode = style->comp.node;
|
auto comp = Shape::gen();
|
||||||
if (compNode->child.count > 0) {
|
auto child = compNode->child.data;
|
||||||
auto comp = Shape::gen();
|
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
||||||
auto child = compNode->child.data;
|
if (style->comp.method == CompositeMethod::ClipPath) comp->fill(0, 0, 0, 255);
|
||||||
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
vg->composite(move(comp), style->comp.method);
|
||||||
comp->fill(0, 0, 0, 255);
|
|
||||||
vg->composite(move(comp), CompositeMethod::ClipPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Composite Alpha Mask
|
|
||||||
if (((int)style->comp.flags & (int)SvgCompositeFlags::AlphaMask)) {
|
|
||||||
auto compNode = style->comp.node;
|
|
||||||
if (compNode->child.count > 0) {
|
|
||||||
auto comp = Shape::gen();
|
|
||||||
auto child = compNode->child.data;
|
|
||||||
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
|
||||||
vg->composite(move(comp), CompositeMethod::AlphaMask);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,8 +291,9 @@ bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, flo
|
||||||
switch (node->type) {
|
switch (node->type) {
|
||||||
case SvgNodeType::Path: {
|
case SvgNodeType::Path: {
|
||||||
if (node->node.path.path) {
|
if (node->node.path.path) {
|
||||||
if (svgPathToTvgPath(node->node.path.path->c_str(), cmds, pts))
|
if (svgPathToTvgPath(node->node.path.path->c_str(), cmds, pts)) {
|
||||||
shape->appendPath(cmds.data, cmds.count, pts.data, pts.count);
|
shape->appendPath(cmds.data, cmds.count, pts.data, pts.count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -368,27 +357,14 @@ unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, flo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Apply composite node
|
//Apply composite node
|
||||||
if (node->style->comp.node) {
|
if (node->style->comp.node && (node->style->comp.method != CompositeMethod::None)) {
|
||||||
//Composite ClipPath
|
auto compNode = node->style->comp.node;
|
||||||
if (((int)node->style->comp.flags & (int)SvgCompositeFlags::ClipPath)) {
|
if (compNode->child.count > 0) {
|
||||||
auto compNode = node->style->comp.node;
|
auto comp = Shape::gen();
|
||||||
if (compNode->child.count > 0) {
|
auto child = compNode->child.data;
|
||||||
auto comp = Shape::gen();
|
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
||||||
auto child = compNode->child.data;
|
if (node->style->comp.method == CompositeMethod::ClipPath) comp->fill(0, 0, 0, 255);
|
||||||
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
scene->composite(move(comp), node->style->comp.method);
|
||||||
comp->fill(0, 0, 0, 255);
|
|
||||||
scene->composite(move(comp), CompositeMethod::ClipPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Composite AlphaMask
|
|
||||||
if (((int)node->style->comp.flags & (int)SvgCompositeFlags::AlphaMask)) {
|
|
||||||
auto compNode = node->style->comp.node;
|
|
||||||
if (compNode->child.count > 0) {
|
|
||||||
auto comp = Shape::gen();
|
|
||||||
auto child = compNode->child.data;
|
|
||||||
for (uint32_t i = 0; i < compNode->child.count; ++i, ++child) _appendChildShape(*child, comp.get(), vx, vy, vw, vh);
|
|
||||||
scene->composite(move(comp), CompositeMethod::AlphaMask);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scene->opacity(node->style->opacity);
|
scene->opacity(node->style->opacity);
|
||||||
|
@ -412,7 +388,7 @@ unique_ptr<Scene> _buildRoot(const SvgNode* node, float vx, float vy, float vw,
|
||||||
viewBoxClip->fill(0, 0, 0, 255);
|
viewBoxClip->fill(0, 0, 0, 255);
|
||||||
|
|
||||||
auto compositeLayer = Scene::gen();
|
auto compositeLayer = Scene::gen();
|
||||||
compositeLayer->composite(move(viewBoxClip), tvg::CompositeMethod::ClipPath);
|
compositeLayer->composite(move(viewBoxClip), CompositeMethod::ClipPath);
|
||||||
compositeLayer->push(move(docNode));
|
compositeLayer->push(move(docNode));
|
||||||
|
|
||||||
root = Scene::gen();
|
root = Scene::gen();
|
||||||
|
|
Loading…
Add table
Reference in a new issue