mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
loaders svg: code refactoring
clean up code and remove unnecessary internal class. this also helps to reduce binary size by 5.5kb no logical changes.
This commit is contained in:
parent
adf1312a5e
commit
e1eb98af79
7 changed files with 63 additions and 53 deletions
|
@ -19,14 +19,13 @@
|
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
#include <fstream>
|
||||
#include <string.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include "tvgLoaderMgr.h"
|
||||
#include "tvgXmlParser.h"
|
||||
#include "tvgSvgLoader.h"
|
||||
#include "tvgSvgSceneBuilder.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
|
@ -855,6 +854,7 @@ static void _handleTransformAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node
|
|||
node->transform = _parseTransformationMatrix(value);
|
||||
}
|
||||
|
||||
|
||||
static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
||||
{
|
||||
SvgStyleProperty* style = node->style;
|
||||
|
@ -866,6 +866,7 @@ static void _handleClipPathAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node,
|
|||
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
||||
}
|
||||
|
||||
|
||||
static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
||||
{
|
||||
SvgStyleProperty* style = node->style;
|
||||
|
@ -877,6 +878,7 @@ static void _handleMaskAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, con
|
|||
if (len >= 3 && !strncmp(value, "url", 3)) style->comp.url = _idFromUrl((const char*)(value + 3));
|
||||
}
|
||||
|
||||
|
||||
static void _handleDisplayAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node, const char* value)
|
||||
{
|
||||
//TODO : The display attribute can have various values as well as "none".
|
||||
|
@ -890,12 +892,7 @@ static void _handleDisplayAttr(TVG_UNUSED SvgLoaderData* loader, SvgNode* node,
|
|||
|
||||
typedef void (*styleMethod)(SvgLoaderData* loader, SvgNode* node, const char* value);
|
||||
|
||||
|
||||
#define STYLE_DEF(Name, Name1) \
|
||||
{ \
|
||||
#Name, sizeof(#Name), _handle##Name1##Attr \
|
||||
}
|
||||
|
||||
#define STYLE_DEF(Name, Name1) { #Name, sizeof(#Name), _handle##Name1##Attr }
|
||||
|
||||
static constexpr struct
|
||||
{
|
||||
|
@ -2589,7 +2586,7 @@ void SvgLoader::run(unsigned tid)
|
|||
_updateComposite(loaderData.doc, loaderData.doc);
|
||||
if (defs) _updateComposite(loaderData.doc, defs);
|
||||
}
|
||||
root = builder.build(loaderData.doc);
|
||||
root = svgSceneBuild(loaderData.doc);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#define _TVG_SVG_LOADER_H_
|
||||
|
||||
#include "tvgTaskScheduler.h"
|
||||
#include "tvgSvgSceneBuilder.h"
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
|
||||
class SvgLoader : public Loader, public Task
|
||||
{
|
||||
|
@ -33,7 +33,6 @@ public:
|
|||
uint32_t size = 0;
|
||||
|
||||
SvgLoaderData loaderData;
|
||||
SvgSceneBuilder builder;
|
||||
unique_ptr<Scene> root;
|
||||
|
||||
SvgLoader();
|
||||
|
|
|
@ -19,12 +19,14 @@
|
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
#include "tvgSvgPath.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
static char* _skipComma(const char* content)
|
||||
{
|
||||
while (*content && isspace(*content)) {
|
||||
|
@ -491,6 +493,11 @@ static char* _nextCommand(char* path, char* cmd, float* arr, int* count)
|
|||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
|
||||
bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point>& pts)
|
||||
{
|
||||
float numberArray[7];
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#ifndef _TVG_SVG_PATH_H_
|
||||
#define _TVG_SVG_PATH_H_
|
||||
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
#include <tvgCommon.h>
|
||||
|
||||
bool svgPathToTvgPath(const char* svgPath, Array<PathCommand>& cmds, Array<Point>& pts);
|
||||
|
||||
|
|
|
@ -20,19 +20,24 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
#include "tvgSvgSceneBuilder.h"
|
||||
#include "tvgSvgPath.h"
|
||||
|
||||
bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh);
|
||||
static bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh);
|
||||
|
||||
bool _isGroupType(SvgNodeType type)
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
static inline bool _isGroupType(SvgNodeType type)
|
||||
{
|
||||
if (type == SvgNodeType::Doc || type == SvgNodeType::G || type == SvgNodeType::ClipPath) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
unique_ptr<LinearGradient> _applyLinearGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh)
|
||||
|
||||
static unique_ptr<LinearGradient> _applyLinearGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh)
|
||||
{
|
||||
Fill::ColorStop* stops;
|
||||
int stopCount = 0;
|
||||
|
@ -112,7 +117,7 @@ unique_ptr<LinearGradient> _applyLinearGradientProperty(SvgStyleGradient* g, con
|
|||
}
|
||||
|
||||
|
||||
unique_ptr<RadialGradient> _applyRadialGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh)
|
||||
static unique_ptr<RadialGradient> _applyRadialGradientProperty(SvgStyleGradient* g, const Shape* vg, float rx, float ry, float rw, float rh)
|
||||
{
|
||||
Fill::ColorStop *stops;
|
||||
int stopCount = 0;
|
||||
|
@ -194,7 +199,8 @@ unique_ptr<RadialGradient> _applyRadialGradientProperty(SvgStyleGradient* g, con
|
|||
return fillGrad;
|
||||
}
|
||||
|
||||
void _appendChildShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh)
|
||||
|
||||
static void _appendChildShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh)
|
||||
{
|
||||
_appendShape(node, shape, vx, vy, vw, vh);
|
||||
if (node->child.count > 0) {
|
||||
|
@ -204,7 +210,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)
|
||||
static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, float vh)
|
||||
{
|
||||
SvgStyleProperty* style = node->style;
|
||||
|
||||
|
@ -276,14 +282,16 @@ void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float vw, floa
|
|||
}
|
||||
}
|
||||
|
||||
unique_ptr<Shape> _shapeBuildHelper(SvgNode* node, float vx, float vy, float vw, float vh)
|
||||
|
||||
static unique_ptr<Shape> _shapeBuildHelper(SvgNode* node, float vx, float vy, float vw, float vh)
|
||||
{
|
||||
auto shape = Shape::gen();
|
||||
if (_appendShape(node, shape.get(), vx, vy, vw, vh)) return shape;
|
||||
else return nullptr;
|
||||
}
|
||||
|
||||
bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh)
|
||||
|
||||
static bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, float vh)
|
||||
{
|
||||
Array<PathCommand> cmds;
|
||||
Array<Point> pts;
|
||||
|
@ -340,7 +348,8 @@ bool _appendShape(SvgNode* node, Shape* shape, float vx, float vy, float vw, flo
|
|||
return true;
|
||||
}
|
||||
|
||||
unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh)
|
||||
|
||||
static unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, float vw, float vh)
|
||||
{
|
||||
if (_isGroupType(node->type)) {
|
||||
auto scene = Scene::gen();
|
||||
|
@ -374,8 +383,20 @@ unique_ptr<Scene> _sceneBuildHelper(const SvgNode* node, float vx, float vy, flo
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
unique_ptr<Scene> _buildRoot(const SvgNode* node, float vx, float vy, float vw, float vh)
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
unique_ptr<Scene> svgSceneBuild(SvgNode* node)
|
||||
{
|
||||
if (!node || (node->type != SvgNodeType::Doc)) return nullptr;
|
||||
|
||||
auto vx = node->node.doc.vx;
|
||||
auto vy = node->node.doc.vy;
|
||||
auto vw = node->node.doc.vw;
|
||||
auto vh = node->node.doc.vh;
|
||||
|
||||
unique_ptr<Scene> root;
|
||||
auto docNode = _sceneBuildHelper(node, vx, vy, vw, vh);
|
||||
float x, y, w, h;
|
||||
|
@ -397,21 +418,4 @@ unique_ptr<Scene> _buildRoot(const SvgNode* node, float vx, float vy, float vw,
|
|||
root = move(docNode);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
SvgSceneBuilder::SvgSceneBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SvgSceneBuilder::~SvgSceneBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unique_ptr<Scene> SvgSceneBuilder::build(SvgNode* node)
|
||||
{
|
||||
if (!node || (node->type != SvgNodeType::Doc)) return nullptr;
|
||||
|
||||
return _buildRoot(node, node->node.doc.vx, node->node.doc.vy, node->node.doc.vw, node->node.doc.vh);
|
||||
}
|
||||
}
|
|
@ -23,15 +23,8 @@
|
|||
#ifndef _TVG_SVG_SCENE_BUILDER_H_
|
||||
#define _TVG_SVG_SCENE_BUILDER_H_
|
||||
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
#include "tvgCommon.h"
|
||||
|
||||
class SvgSceneBuilder
|
||||
{
|
||||
public:
|
||||
SvgSceneBuilder();
|
||||
~SvgSceneBuilder();
|
||||
|
||||
unique_ptr<Scene> build(SvgNode* node);
|
||||
};
|
||||
unique_ptr<Scene> svgSceneBuild(SvgNode* node);
|
||||
|
||||
#endif //_TVG_SVG_SCENE_BUILDER_H_
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include <ctype.h>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <malloc.h>
|
||||
#else
|
||||
|
@ -30,6 +31,10 @@
|
|||
|
||||
#include "tvgXmlParser.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
#ifdef THORVG_LOG_ENABLED
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -199,6 +204,11 @@ static const char* _simpleXmlFindDoctypeChildEndTag(const char* itr, const char*
|
|||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
|
||||
bool simpleXmlParseAttributes(const char* buf, unsigned bufLength, simpleXMLAttributeCb func, const void* data)
|
||||
{
|
||||
const char *itr = buf, *itrEnd = buf + bufLength;
|
||||
|
|
Loading…
Add table
Reference in a new issue