svg_loader: code refactoring.

follow strict coding-style.

no logic changes.
This commit is contained in:
Hermet Park 2022-02-08 18:43:03 +09:00
parent 5f40449c3d
commit ff3ebd9abd
3 changed files with 45 additions and 50 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
* Copyright (c) 2022 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -20,14 +20,13 @@
* SOFTWARE.
*/
#include <cstring>
#include "tvgSvgCssStyle.h"
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static void _cssStyleCopy(SvgStyleProperty* to, const SvgStyleProperty* from)
static void _copyStyle(SvgStyleProperty* to, const SvgStyleProperty* from)
{
if (from == nullptr) return;
//Copy the properties of 'from' only if they were explicitly set (not the default ones).
@ -108,7 +107,7 @@ static void _cssStyleCopy(SvgStyleProperty* to, const SvgStyleProperty* from)
/* External Class Implementation */
/************************************************************************/
void copyCssStyleAttr(SvgNode* to, const SvgNode* from)
void cssCopyStyleAttr(SvgNode* to, const SvgNode* from)
{
//Copy matrix attribute
if (from->transform && !((int)to->style->flags & (int)SvgStyleFlags::Transform)) {
@ -119,19 +118,19 @@ void copyCssStyleAttr(SvgNode* to, const SvgNode* from)
}
}
//Copy style attribute
_cssStyleCopy(to->style, from->style);
_copyStyle(to->style, from->style);
if (from->style->clipPath.url) to->style->clipPath.url = strdup(from->style->clipPath.url);
if (from->style->mask.url) to->style->mask.url = strdup(from->style->mask.url);
}
SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title, SvgNodeType type)
SvgNode* cssFindStyleNode(const SvgNode* style, const char* title, SvgNodeType type)
{
if (!cssStyle) return nullptr;
if (!style) return nullptr;
auto child = cssStyle->child.data;
for (uint32_t i = 0; i < cssStyle->child.count; ++i, ++child) {
auto child = style->child.data;
for (uint32_t i = 0; i < style->child.count; ++i, ++child) {
if ((*child)->type == type) {
if ((!title && !(*child)->id) || (title && (*child)->id && !strcmp((*child)->id, title))) return (*child);
}
@ -140,12 +139,12 @@ SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title, SvgNodeTyp
}
SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title)
SvgNode* cssFindStyleNode(const SvgNode* style, const char* title)
{
if (!cssStyle) return nullptr;
if (!style) return nullptr;
auto child = cssStyle->child.data;
for (uint32_t i = 0; i < cssStyle->child.count; ++i, ++child) {
auto child = style->child.data;
for (uint32_t i = 0; i < style->child.count; ++i, ++child) {
if ((*child)->type == SvgNodeType::CssStyle) {
if ((title && (*child)->id && !strcmp((*child)->id, title))) return (*child);
}
@ -154,35 +153,34 @@ SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title)
}
void updateCssStyle(SvgNode* doc, SvgNode* cssStyle)
void cssUpdateStyle(SvgNode* doc, SvgNode* style)
{
if (doc->child.count > 0) {
auto child = doc->child.data;
for (uint32_t i = 0; i < doc->child.count; ++i, ++child) {
if (auto cssNode = findCssStyleNode(cssStyle, nullptr, (*child)->type)) {
copyCssStyleAttr(*child, cssNode);
if (auto cssNode = cssFindStyleNode(style, nullptr, (*child)->type)) {
cssCopyStyleAttr(*child, cssNode);
}
if (auto cssNode = findCssStyleNode(cssStyle, nullptr)) {
copyCssStyleAttr(*child, cssNode);
if (auto cssNode = cssFindStyleNode(style, nullptr)) {
cssCopyStyleAttr(*child, cssNode);
}
updateCssStyle(*child, cssStyle);
cssUpdateStyle(*child, style);
}
}
}
void stylePostponedNodes(Array<SvgNodeIdPair>* nodesToStyle, SvgNode* cssStyle)
void cssApplyStyleToPostponeds(Array<SvgNodeIdPair>& postponeds, SvgNode* style)
{
for (uint32_t i = 0; i < nodesToStyle->count; ++i) {
auto nodeIdPair = nodesToStyle->data[i];
for (uint32_t i = 0; i < postponeds.count; ++i) {
auto nodeIdPair = postponeds.data[i];
//css styling: tag.name has higher priority than .name
if (auto cssNode = findCssStyleNode(cssStyle, nodeIdPair.id, nodeIdPair.node->type)) {
copyCssStyleAttr(nodeIdPair.node, cssNode);
if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id, nodeIdPair.node->type)) {
cssCopyStyleAttr(nodeIdPair.node, cssNode);
}
if (auto cssNode = findCssStyleNode(cssStyle, nodeIdPair.id)) {
copyCssStyleAttr(nodeIdPair.node, cssNode);
if (auto cssNode = cssFindStyleNode(style, nodeIdPair.id)) {
cssCopyStyleAttr(nodeIdPair.node, cssNode);
}
}
}
}

View file

@ -25,10 +25,10 @@
#include "tvgSvgLoaderCommon.h"
void copyCssStyleAttr(SvgNode* to, const SvgNode* from);
SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title, SvgNodeType type);
SvgNode* findCssStyleNode(const SvgNode* cssStyle, const char* title);
void updateCssStyle(SvgNode* doc, SvgNode* cssStyle);
void stylePostponedNodes(Array<SvgNodeIdPair>* nodesToStyle, SvgNode* cssStyle);
void cssCopyStyleAttr(SvgNode* to, const SvgNode* from);
SvgNode* cssFindStyleNode(const SvgNode* style, const char* title, SvgNodeType type);
SvgNode* cssFindStyleNode(const SvgNode* style, const char* title);
void cssUpdateStyle(SvgNode* doc, SvgNode* style);
void cssApplyStyleToPostponeds(Array<SvgNodeIdPair>& postponeds, SvgNode* style);
#endif //_TVG_SVG_CSS_STYLE_H_

View file

@ -80,9 +80,6 @@ typedef bool (*parseAttributes)(const char* buf, unsigned bufLength, simpleXMLAt
typedef SvgNode* (*FactoryMethod)(SvgLoaderData* loader, SvgNode* parent, const char* buf, unsigned bufLength, parseAttributes func);
typedef SvgStyleGradient* (*GradientFactoryMethod)(SvgLoaderData* loader, const char* buf, unsigned bufLength);
static void _postponeCloneNode(Array<SvgNodeIdPair>* nodes, SvgNode *node, char* id);
static char* _skipSpace(const char* str, const char* end)
{
while (((end && str < end) || (!end && *str != '\0')) && isspace(*str)) {
@ -737,6 +734,12 @@ error:
}
static void _postpone(Array<SvgNodeIdPair>& nodes, SvgNode *node, char* id)
{
nodes.push({node, id});
}
/*
// TODO - remove?
static constexpr struct
@ -960,16 +963,16 @@ static void _handleCssClassAttr(SvgLoaderData* loader, SvgNode* node, const char
bool cssClassFound = false;
//css styling: tag.name has higher priority than .name
if (auto cssNode = findCssStyleNode(loader->cssStyle, *cssClass, node->type)) {
if (auto cssNode = cssFindStyleNode(loader->cssStyle, *cssClass, node->type)) {
cssClassFound = true;
copyCssStyleAttr(node, cssNode);
cssCopyStyleAttr(node, cssNode);
}
if (auto cssNode = findCssStyleNode(loader->cssStyle, *cssClass)) {
if (auto cssNode = cssFindStyleNode(loader->cssStyle, *cssClass)) {
cssClassFound = true;
copyCssStyleAttr(node, cssNode);
cssCopyStyleAttr(node, cssNode);
}
if (!cssClassFound) _postponeCloneNode(&loader->nodesToStyle, node, *cssClass);
if (!cssClassFound) _postpone(loader->nodesToStyle, node, *cssClass);
}
@ -2031,12 +2034,6 @@ static void _cloneNode(SvgNode* from, SvgNode* parent, int depth)
}
static void _postponeCloneNode(Array<SvgNodeIdPair>* nodes, SvgNode *node, char* id)
{
nodes->push({node, id});
}
static void _clonePostponedNodes(Array<SvgNodeIdPair>* cloneNodes, SvgNode* doc)
{
for (uint32_t i = 0; i < cloneNodes->count; ++i) {
@ -2091,7 +2088,7 @@ static bool _attrParseUseNode(void* data, const char* key, const char* value)
//some svg export software include <defs> element at the end of the file
//if so the 'from' element won't be found now and we have to repeat finding
//after the whole file is parsed
_postponeCloneNode(&loader->cloneNodes, node, id);
_postpone(loader->cloneNodes, node, id);
}
} else {
return _attrParseGNode(data, key, value);
@ -3047,8 +3044,8 @@ void SvgLoader::run(unsigned tid)
if (loaderData.doc) {
auto defs = loaderData.doc->node.doc.defs;
if (loaderData.nodesToStyle.count > 0) stylePostponedNodes(&loaderData.nodesToStyle, loaderData.cssStyle);
if (loaderData.cssStyle) updateCssStyle(loaderData.doc, loaderData.cssStyle);
if (loaderData.nodesToStyle.count > 0) cssApplyStyleToPostponeds(loaderData.nodesToStyle, loaderData.cssStyle);
if (loaderData.cssStyle) cssUpdateStyle(loaderData.doc, loaderData.cssStyle);
_updateComposite(loaderData.doc, loaderData.doc);
if (defs) _updateComposite(loaderData.doc, defs);