svg_loader: code refactoring.

clean up about logging before replacing it with TVGLOG()
This commit is contained in:
Hermet Park 2021-07-23 14:46:31 +09:00 committed by Hermet Park
parent 02b0f6cfc9
commit 5024e7f952
4 changed files with 76 additions and 65 deletions

View file

@ -2484,49 +2484,50 @@ static void _styleInherit(SvgStyleProperty* child, const SvgStyleProperty* paren
}
static void _inefficientNodeCheck(TVG_UNUSED SvgNode* node){
#ifdef THORVG_LOG_ENABLED
static void _inefficientNodeCheck(SvgNode* node){
if (!node->display && node->type != SvgNodeType::ClipPath) printf("SVG: Inefficient elements used [Display is none][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->style->opacity == 0) printf("SVG: Inefficient elements used [Opacity is zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->style->fill.opacity == 0 && node->style->stroke.opacity == 0) printf("SVG: Inefficient elements used [Fill opacity and stroke opacity are zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
auto type = simpleXmlNodeTypeToString(node->type);
if (!node->display && node->type != SvgNodeType::ClipPath) printf("SVG: Inefficient elements used [Display is none][Node Type : %s]\n", type);
if (node->style->opacity == 0) printf("SVG: Inefficient elements used [Opacity is zero][Node Type : %s]\n", type);
if (node->style->fill.opacity == 0 && node->style->stroke.opacity == 0) printf("SVG: Inefficient elements used [Fill opacity and stroke opacity are zero][Node Type : %s]\n", type);
switch (node->type) {
case SvgNodeType::Path: {
if (!node->node.path.path || node->node.path.path->empty()) printf("SVG: Inefficient elements used [Empty path][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (!node->node.path.path || node->node.path.path->empty()) printf("SVG: Inefficient elements used [Empty path][Node Type : %s]\n", type);
break;
}
case SvgNodeType::Ellipse: {
if (node->node.ellipse.rx == 0 && node->node.ellipse.ry == 0) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->node.ellipse.rx == 0 && node->node.ellipse.ry == 0) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", type);
break;
}
case SvgNodeType::Polygon:
case SvgNodeType::Polyline: {
if (node->node.polygon.pointsCount < 2) printf("SVG: Inefficient elements used [Invalid Polygon][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->node.polygon.pointsCount < 2) printf("SVG: Inefficient elements used [Invalid Polygon][Node Type : %s]\n", type);
break;
}
case SvgNodeType::Circle: {
if (node->node.circle.r == 0) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->node.circle.r == 0) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", type);
break;
}
case SvgNodeType::Rect: {
if (node->node.rect.w == 0 && node->node.rect.h) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->node.rect.w == 0 && node->node.rect.h) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", type);
break;
}
case SvgNodeType::Line: {
if (node->node.line.x1 == node->node.line.x2 && node->node.line.y1 == node->node.line.y2) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", simpleXmlNodeTypeToString(node->type).c_str());
if (node->node.line.x1 == node->node.line.x2 && node->node.line.y1 == node->node.line.y2) printf("SVG: Inefficient elements used [Size is zero][Node Type : %s]\n", type);
break;
}
default: break;
}
}
#endif
}
static void _updateStyle(SvgNode* node, SvgStyleProperty* parentStyle)
{
_styleInherit(node->style, parentStyle);
#ifdef THORVG_LOG_ENABLED
_inefficientNodeCheck(node);
#endif
auto child = node->child.data;
for (uint32_t i = 0; i < node->child.count; ++i, ++child) {

View file

@ -28,6 +28,7 @@
struct SvgNode;
struct SvgStyleGradient;
//NOTE: Please update simpleXmlNodeTypeToString() as well.
enum class SvgNodeType
{
Doc,

View file

@ -36,50 +36,6 @@
/************************************************************************/
#ifdef THORVG_LOG_ENABLED
#include <stdio.h>
string simpleXmlNodeTypeToString(SvgNodeType type)
{
switch (type) {
case SvgNodeType::Doc: return "Svg";
case SvgNodeType::G: return "G";
case SvgNodeType::Defs: return "Defs";
case SvgNodeType::Animation: return "Animation";
case SvgNodeType::Arc: return "Arc";
case SvgNodeType::Circle: return "Circle";
case SvgNodeType::Ellipse: return "Ellipse";
case SvgNodeType::Image: return "Image";
case SvgNodeType::Line: return "Line";
case SvgNodeType::Path: return "Path";
case SvgNodeType::Polygon: return "Polygon";
case SvgNodeType::Polyline: return "Polyline";
case SvgNodeType::Rect: return "Rect";
case SvgNodeType::Text: return "Text";
case SvgNodeType::TextArea: return "TextArea";
case SvgNodeType::Tspan: return "Tspan";
case SvgNodeType::Use: return "Use";
case SvgNodeType::Video: return "Video";
case SvgNodeType::ClipPath: return "ClipPath";
case SvgNodeType::Mask: return "Mask";
default: return "Unknown";
}
return "Unknown";
}
bool isIgnoreUnsupportedLogElements(const char* tagName)
{
const auto elementsNum = 1;
const char* const elements[] = { "title" };
for (unsigned int i = 0; i < elementsNum; ++i) {
if (!strncmp(tagName, elements[i], strlen(tagName))) {
return true;
}
}
return false;
}
bool _isIgnoreUnsupportedLogAttributes(const char* tagAttribute, const char* tagValue)
{
const auto attributesNum = 6;
@ -109,9 +65,9 @@ bool _isIgnoreUnsupportedLogAttributes(const char* tagAttribute, const char* tag
}
return false;
}
#endif
static const char* _simpleXmlFindWhiteSpace(const char* itr, const char* itrEnd)
{
for (; itr < itrEnd; itr++) {
@ -253,6 +209,55 @@ static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char*
/* External Class Implementation */
/************************************************************************/
const char* simpleXmlNodeTypeToString(TVG_UNUSED SvgNodeType type)
{
#ifdef THORVG_LOG_ENABLED
static const char* TYPE_NAMES[] = {
"Svg",
"G",
"Defs",
"Animation",
"Arc",
"Circle",
"Ellipse",
"Image",
"Line",
"Path",
"Polygon",
"Polyline",
"Rect",
"Text",
"TextArea",
"Tspan",
"Use",
"Video",
"ClipPath",
"Mask",
"Unknown",
};
return TYPE_NAMES[(int) type];
#endif
return nullptr;
}
bool isIgnoreUnsupportedLogElements(TVG_UNUSED const char* tagName)
{
#ifdef THORVG_LOG_ENABLED
const auto elementsNum = 1;
const char* const elements[] = { "title" };
for (unsigned int i = 0; i < elementsNum; ++i) {
if (!strncmp(tagName, elements[i], strlen(tagName))) {
return true;
}
}
return false;
#else
return true;
#endif
}
bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
{
@ -313,7 +318,11 @@ bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttr
#ifdef THORVG_LOG_ENABLED
if (!func((void*)data, tmpBuf, tval)) {
if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) printf("SVG: Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]\n", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type).c_str(), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id->c_str() : "NO_ID", tmpBuf, tval ? tval : "NONE");
if (!_isIgnoreUnsupportedLogAttributes(tmpBuf, tval)) {
auto type = simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type);
auto id = ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id->c_str() : "NO_ID";
printf("SVG: Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]\n", type, id, tmpBuf, tval ? tval : "NONE");
}
}
#else
func((void*)data, tmpBuf, tval);
@ -510,7 +519,11 @@ bool simpleXmlParseW3CAttribute(const char* buf, simpleXMLAttributeCb func, cons
#ifdef THORVG_LOG_ENABLED
if (!func((void*)data, key, val)) {
if (!_isIgnoreUnsupportedLogAttributes(key, val)) printf("SVG: Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]\n", simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type).c_str(), ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id->c_str() : "NO_ID", key, val ? val : "NONE");
if (!_isIgnoreUnsupportedLogAttributes(key, val)) {
auto type = simpleXmlNodeTypeToString(((SvgLoaderData*)data)->svgParse->node->type);
auto id = ((SvgLoaderData*)data)->svgParse->node->id ? ((SvgLoaderData*)data)->svgParse->node->id->c_str() : "NO_ID";
printf("SVG: Unsupported attributes used [Elements type: %s][Id : %s][Attribute: %s][Value: %s]\n", type, id, key, val ? val : "NONE");
}
}
#else
func((void*)data, key, val);

View file

@ -51,11 +51,7 @@ bool simpleXmlParseAttributes(const char* buf, unsigned buflen, simpleXMLAttribu
bool simpleXmlParse(const char* buf, unsigned buflen, bool strip, simpleXMLCb func, const void* data);
bool simpleXmlParseW3CAttribute(const char* buf, simpleXMLAttributeCb func, const void* data);
const char *simpleXmlFindAttributesTag(const char* buf, unsigned buflen);
#ifdef THORVG_LOG_ENABLED
string simpleXmlNodeTypeToString(SvgNodeType type);
bool isIgnoreUnsupportedLogElements(const char* tagName);
#endif
const char* simpleXmlNodeTypeToString(SvgNodeType type);
#endif //_TVG_SIMPLE_XML_PARSER_H_