#ifndef _TVG_BINARY_DESC_H_
#define _TVG_BINARY_DESC_H_
-// now only little endian
-#define _read_tvg_ui16(dst, src) memcpy(dst, (src), sizeof(uint16_t))
-#define _read_tvg_ui32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
-#define _read_tvg_float(dst, src) memcpy(dst, (src), sizeof(float))
-
+using TvgBinByte = uint8_t;
using TvgIndicator = uint8_t;
using ByteCounter = uint32_t;
using TvgFlag = uint8_t;
#define BYTE_COUNTER_SIZE sizeof(ByteCounter)
#define TVG_FLAG_SIZE sizeof(TvgFlag)
-struct tvgBlock
-{
- TvgIndicator type;
- ByteCounter length;
- const char* data;
- const char* end;
-};
-
-
// HEADER
#define TVG_BIN_HEADER_SIGNATURE "ThorVG"
#define TVG_BIN_HEADER_SIGNATURE_LENGTH 6
#ifndef _TVG_SAVER_IMPL_H_
#define _TVG_SAVER_IMPL_H_
-#include "tvgPaint.h"
-#include "tvgBinaryDesc.h"
#include <float.h>
#include <math.h>
#include <stdio.h>
+#include "tvgPaint.h"
+#include "tvgBinaryDesc.h"
+
struct Saver::Impl
{
Paint* paint = nullptr; //TODO: replace with Array
- Array<char> buffer;
+ Array<TvgBinByte> buffer;
~Impl()
{
{
ByteCounter paintDataByteCnt = 0;
- uint8_t opacity = paint->opacity();
+ auto opacity = paint->opacity();
if (opacity < 255) {
paintDataByteCnt += writeMember(TVG_PAINT_OPACITY_INDICATOR, sizeof(opacity), &opacity);
}
- Matrix m = const_cast<Paint*>(paint)->transform();
+ auto m = const_cast<Paint*>(paint)->transform();
if (fabs(m.e11 - 1) > FLT_EPSILON || fabs(m.e12) > FLT_EPSILON || fabs(m.e13) > FLT_EPSILON ||
fabs(m.e21) > FLT_EPSILON || fabs(m.e22 - 1) > FLT_EPSILON || fabs(m.e23) > FLT_EPSILON ||
fabs(m.e31) > FLT_EPSILON || fabs(m.e32) > FLT_EPSILON || fabs(m.e33 - 1) > FLT_EPSILON) {
return paintDataByteCnt;
}
- ByteCounter serializeScene(const Paint* paint)
+ ByteCounter serializeScene(const Scene* scene)
{
- auto scene = static_cast<const Scene*>(paint);
- if (!scene) return 0;
-
- ByteCounter sceneDataByteCnt = 0;
-
writeMemberIndicator(TVG_SCENE_BEGIN_INDICATOR);
skipInBufferMemberDataSize();
- sceneDataByteCnt += serializeChildren(paint);
+ auto sceneDataByteCnt = serializeChildren(scene);
sceneDataByteCnt += serializePaint(scene);
writeMemberDataSizeAt(sceneDataByteCnt);
return TVG_INDICATOR_SIZE + BYTE_COUNTER_SIZE + pathDataByteCnt;
}
- ByteCounter serializeShape(const Paint* paint)
+ ByteCounter serializeShape(const Shape* shape)
{
- auto shape = static_cast<const Shape*>(paint);
- if (!shape) return 0;
-
- ByteCounter shapeDataByteCnt = 0;
-
writeMemberIndicator(TVG_SHAPE_BEGIN_INDICATOR);
skipInBufferMemberDataSize();
- TvgFlag ruleTvgFlag = (shape->fillRule() == FillRule::EvenOdd) ? TVG_SHAPE_FILLRULE_EVENODD_FLAG : TVG_SHAPE_FILLRULE_WINDING_FLAG;
- shapeDataByteCnt += writeMember(TVG_SHAPE_FILLRULE_INDICATOR, TVG_FLAG_SIZE, &ruleTvgFlag);
+ auto ruleTvgFlag = (shape->fillRule() == FillRule::EvenOdd) ? TVG_SHAPE_FILLRULE_EVENODD_FLAG : TVG_SHAPE_FILLRULE_WINDING_FLAG;
+ auto shapeDataByteCnt = writeMember(TVG_SHAPE_FILLRULE_INDICATOR, TVG_FLAG_SIZE, &ruleTvgFlag);
- if (shape->strokeWidth() > 0) {
- shapeDataByteCnt += serializeShapeStroke(shape);
- }
+ if (shape->strokeWidth() > 0) shapeDataByteCnt += serializeShapeStroke(shape);
if (auto fill = shape->fill()) {
shapeDataByteCnt += serializeShapeFill(fill, TVG_SHAPE_FILL_INDICATOR);
}
shapeDataByteCnt += serializeShapePath(shape);
-
- shapeDataByteCnt += serializeChildren(paint);
shapeDataByteCnt += serializePaint(shape);
writeMemberDataSizeAt(shapeDataByteCnt);
return TVG_INDICATOR_SIZE + BYTE_COUNTER_SIZE + shapeDataByteCnt;
}
- ByteCounter serializePicture(const Paint* paint)
+ ByteCounter serializePicture(const Picture* picture)
{
- auto picture = static_cast<const Picture*>(paint);
- if (!picture) return 0;
auto pixels = picture->data();
ByteCounter pictureDataByteCnt = 0;
pictureDataByteCnt += writeMemberData(pixels, pixelsByteCnt);
pictureDataByteCnt += TVG_INDICATOR_SIZE + BYTE_COUNTER_SIZE;
} else {
- pictureDataByteCnt += serializeChildren(paint);
+ pictureDataByteCnt += serializeChildren(picture);
}
pictureDataByteCnt += serializePaint(picture);
ByteCounter serialize(const Paint* paint)
{
if (!paint) return 0;
- ByteCounter dataByteCnt = 0;
switch (paint->id()) {
- case TVG_CLASS_ID_SHAPE: {
- dataByteCnt += serializeShape(paint);
- break;
- }
- case TVG_CLASS_ID_SCENE: {
- dataByteCnt += serializeScene(paint);
- break;
- }
- case TVG_CLASS_ID_PICTURE: {
- dataByteCnt += serializePicture(paint);
- break;
- }
+ case TVG_CLASS_ID_SHAPE: return serializeShape(static_cast<const Shape*>(paint));
+ case TVG_CLASS_ID_SCENE: return serializeScene(static_cast<const Scene*>(paint));
+ case TVG_CLASS_ID_PICTURE: return serializePicture(static_cast<const Picture*>(paint));
}
- return dataByteCnt;
+ return 0;
}
bool save(Paint* paint, const std::string& path)
/* Internal Class Implementation */
/************************************************************************/
+struct TvgBinBlock
+{
+ TvgIndicator type;
+ ByteCounter length;
+ const char* data;
+ const char* end;
+};
+
+#define _read_tvg_ui32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
+#define _read_tvg_float(dst, src) memcpy(dst, (src), sizeof(float))
+
enum class LoaderResult { Success = 0, InvalidType, SizeCorruption, MemoryCorruption, LogicalCorruption };
-static Paint* _parsePaint(tvgBlock block);
+static Paint* _parsePaint(TvgBinBlock block);
-static bool _paintProperty(tvgBlock block)
+static bool _paintProperty(TvgBinBlock block)
{
switch (block.type) {
case TVG_PAINT_OPACITY_INDICATOR:
}
-static tvgBlock _readBlock(const char *ptr)
+static TvgBinBlock _readBlock(const char *ptr)
{
- tvgBlock block;
+ TvgBinBlock block;
block.type = *ptr;
_read_tvg_ui32(&block.length, ptr + TVG_INDICATOR_SIZE);
block.data = ptr + TVG_INDICATOR_SIZE + BYTE_COUNTER_SIZE;
}
-static LoaderResult _parsePaintProperty(tvgBlock block, Paint *paint)
+static LoaderResult _parsePaintProperty(TvgBinBlock block, Paint *paint)
{
switch (block.type) {
case TVG_PAINT_OPACITY_INDICATOR: {
}
-static LoaderResult _parseScene(tvgBlock block, Paint *paint)
+static LoaderResult _parseScene(TvgBinBlock block, Paint *paint)
{
auto scene = static_cast<Scene*>(paint);
}
-static LoaderResult _parseShape(tvgBlock block, Paint* paint)
+static LoaderResult _parseShape(TvgBinBlock block, Paint* paint)
{
auto shape = static_cast<Shape*>(paint);
}
-static LoaderResult _parsePicture(tvgBlock block, Paint* paint)
+static LoaderResult _parsePicture(TvgBinBlock block, Paint* paint)
{
auto picture = static_cast<Picture*>(paint);
}
-static Paint* _parsePaint(tvgBlock baseBlock)
+static Paint* _parsePaint(TvgBinBlock baseBlock)
{
- LoaderResult (*parser)(tvgBlock, Paint*);
+ LoaderResult (*parser)(TvgBinBlock, Paint*);
Paint *paint = nullptr;
switch (baseBlock.type) {