mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-09 14:13:43 +00:00
tvg format: code refactoring
use SIZE() macro for consistency.
This commit is contained in:
parent
87c6ce1f7c
commit
4bdf346dbb
3 changed files with 32 additions and 31 deletions
|
@ -34,6 +34,7 @@ using TvgBinTag = TvgBinByte;
|
||||||
using TvgBinFlag = TvgBinByte;
|
using TvgBinFlag = TvgBinByte;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Header
|
//Header
|
||||||
#define TVG_HEADER_SIGNATURE "ThorVG"
|
#define TVG_HEADER_SIGNATURE "ThorVG"
|
||||||
#define TVG_HEADER_SIGNATURE_LENGTH 6
|
#define TVG_HEADER_SIGNATURE_LENGTH 6
|
||||||
|
|
|
@ -42,6 +42,7 @@ struct TvgBinBlock
|
||||||
#define _read_tvg_ui32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
|
#define _read_tvg_ui32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
|
||||||
#define _read_tvg_float(dst, src) memcpy(dst, (src), sizeof(float))
|
#define _read_tvg_float(dst, src) memcpy(dst, (src), sizeof(float))
|
||||||
|
|
||||||
|
|
||||||
static Paint* _parsePaint(TvgBinBlock block);
|
static Paint* _parsePaint(TvgBinBlock block);
|
||||||
|
|
||||||
static bool _paintProperty(TvgBinBlock block)
|
static bool _paintProperty(TvgBinBlock block)
|
||||||
|
@ -80,11 +81,11 @@ static bool _readTvgHeader(const char **ptr, float* w, float* h)
|
||||||
|
|
||||||
//View width
|
//View width
|
||||||
if (w) _read_tvg_float(w, *ptr);
|
if (w) _read_tvg_float(w, *ptr);
|
||||||
*ptr += sizeof(float);
|
*ptr += SIZE(float);
|
||||||
|
|
||||||
//View height
|
//View height
|
||||||
if (h) _read_tvg_float(h, *ptr);
|
if (h) _read_tvg_float(h, *ptr);
|
||||||
*ptr += sizeof(float);
|
*ptr += SIZE(float);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -96,7 +97,7 @@ static bool _parseCmpTarget(const char *ptr, const char *end, Paint *paint)
|
||||||
if (block.end > end) return false;
|
if (block.end > end) return false;
|
||||||
|
|
||||||
if (block.type != TVG_TAG_PAINT_CMP_METHOD) return false;
|
if (block.type != TVG_TAG_PAINT_CMP_METHOD) return false;
|
||||||
if (block.length != sizeof(TvgBinFlag)) return false;
|
if (block.length != SIZE(TvgBinFlag)) return false;
|
||||||
|
|
||||||
CompositeMethod cmpMethod;
|
CompositeMethod cmpMethod;
|
||||||
|
|
||||||
|
@ -131,14 +132,14 @@ static bool _parsePaintProperty(TvgBinBlock block, Paint *paint)
|
||||||
{
|
{
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
case TVG_TAG_PAINT_OPACITY: {
|
case TVG_TAG_PAINT_OPACITY: {
|
||||||
if (block.length != sizeof(uint8_t)) return false;
|
if (block.length != SIZE(uint8_t)) return false;
|
||||||
paint->opacity(*block.data);
|
paint->opacity(*block.data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case TVG_TAG_PAINT_TRANSFORM: {
|
case TVG_TAG_PAINT_TRANSFORM: {
|
||||||
if (block.length != sizeof(Matrix)) return false;
|
if (block.length != SIZE(Matrix)) return false;
|
||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
memcpy(&matrix, block.data, sizeof(Matrix));
|
memcpy(&matrix, block.data, SIZE(Matrix));
|
||||||
if (paint->transform(matrix) != Result::Success) return false;
|
if (paint->transform(matrix) != Result::Success) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +158,7 @@ static bool _parseScene(TvgBinBlock block, Paint *paint)
|
||||||
|
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
case TVG_FLAG_SCENE_RESERVEDCNT: {
|
case TVG_FLAG_SCENE_RESERVEDCNT: {
|
||||||
if (block.length != sizeof(uint32_t)) return false;
|
if (block.length != SIZE(uint32_t)) return false;
|
||||||
uint32_t reservedCnt;
|
uint32_t reservedCnt;
|
||||||
_read_tvg_ui32(&reservedCnt, block.data);
|
_read_tvg_ui32(&reservedCnt, block.data);
|
||||||
scene->reserve(reservedCnt);
|
scene->reserve(reservedCnt);
|
||||||
|
@ -181,14 +182,14 @@ static bool _parseShapePath(const char *ptr, const char *end, Shape *shape)
|
||||||
//Shape Path
|
//Shape Path
|
||||||
uint32_t cmdCnt, ptsCnt;
|
uint32_t cmdCnt, ptsCnt;
|
||||||
_read_tvg_ui32(&cmdCnt, ptr);
|
_read_tvg_ui32(&cmdCnt, ptr);
|
||||||
ptr += sizeof(uint32_t);
|
ptr += SIZE(uint32_t);
|
||||||
_read_tvg_ui32(&ptsCnt, ptr);
|
_read_tvg_ui32(&ptsCnt, ptr);
|
||||||
ptr += sizeof(uint32_t);
|
ptr += SIZE(uint32_t);
|
||||||
|
|
||||||
const PathCommand* cmds = (PathCommand*) ptr;
|
const PathCommand* cmds = (PathCommand*) ptr;
|
||||||
ptr += sizeof(PathCommand) * cmdCnt;
|
ptr += SIZE(PathCommand) * cmdCnt;
|
||||||
const Point* pts = (Point*) ptr;
|
const Point* pts = (Point*) ptr;
|
||||||
ptr += sizeof(Point) * ptsCnt;
|
ptr += SIZE(Point) * ptsCnt;
|
||||||
|
|
||||||
if (ptr > end) return false;
|
if (ptr > end) return false;
|
||||||
|
|
||||||
|
@ -207,15 +208,15 @@ static bool _parseShapeFill(const char *ptr, const char *end, Fill **fillOutside
|
||||||
|
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
case TVG_TAG_FILL_RADIAL_GRADIENT: {
|
case TVG_TAG_FILL_RADIAL_GRADIENT: {
|
||||||
if (block.length != 3 * sizeof(float)) return false;
|
if (block.length != 3 * SIZE(float)) return false;
|
||||||
|
|
||||||
auto ptr = block.data;
|
auto ptr = block.data;
|
||||||
float x, y, radius;
|
float x, y, radius;
|
||||||
|
|
||||||
_read_tvg_float(&x, ptr);
|
_read_tvg_float(&x, ptr);
|
||||||
ptr += sizeof(float);
|
ptr += SIZE(float);
|
||||||
_read_tvg_float(&y, ptr);
|
_read_tvg_float(&y, ptr);
|
||||||
ptr += sizeof(float);
|
ptr += SIZE(float);
|
||||||
_read_tvg_float(&radius, ptr);
|
_read_tvg_float(&radius, ptr);
|
||||||
|
|
||||||
auto fillGradRadial = RadialGradient::gen();
|
auto fillGradRadial = RadialGradient::gen();
|
||||||
|
@ -224,17 +225,17 @@ static bool _parseShapeFill(const char *ptr, const char *end, Fill **fillOutside
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TVG_TAG_FILL_LINEAR_GRADIENT: {
|
case TVG_TAG_FILL_LINEAR_GRADIENT: {
|
||||||
if (block.length != 4 * sizeof(float)) return false;
|
if (block.length != 4 * SIZE(float)) return false;
|
||||||
|
|
||||||
auto ptr = block.data;
|
auto ptr = block.data;
|
||||||
float x1, y1, x2, y2;
|
float x1, y1, x2, y2;
|
||||||
|
|
||||||
_read_tvg_float(&x1, ptr);
|
_read_tvg_float(&x1, ptr);
|
||||||
ptr += sizeof(float);
|
ptr += SIZE(float);
|
||||||
_read_tvg_float(&y1, ptr);
|
_read_tvg_float(&y1, ptr);
|
||||||
ptr += sizeof(float);
|
ptr += SIZE(float);
|
||||||
_read_tvg_float(&x2, ptr);
|
_read_tvg_float(&x2, ptr);
|
||||||
ptr += sizeof(float);
|
ptr += SIZE(float);
|
||||||
_read_tvg_float(&y2, ptr);
|
_read_tvg_float(&y2, ptr);
|
||||||
|
|
||||||
auto fillGradLinear = LinearGradient::gen();
|
auto fillGradLinear = LinearGradient::gen();
|
||||||
|
@ -244,7 +245,7 @@ static bool _parseShapeFill(const char *ptr, const char *end, Fill **fillOutside
|
||||||
}
|
}
|
||||||
case TVG_TAG_FILL_FILLSPREAD: {
|
case TVG_TAG_FILL_FILLSPREAD: {
|
||||||
if (!fillGrad) return false;
|
if (!fillGrad) return false;
|
||||||
if (block.length != sizeof(TvgBinFlag)) return false;
|
if (block.length != SIZE(TvgBinFlag)) return false;
|
||||||
switch (*block.data) {
|
switch (*block.data) {
|
||||||
case TVG_FLAG_FILL_FILLSPREAD_PAD: {
|
case TVG_FLAG_FILL_FILLSPREAD_PAD: {
|
||||||
fillGrad->spread(FillSpread::Pad);
|
fillGrad->spread(FillSpread::Pad);
|
||||||
|
@ -290,9 +291,9 @@ static bool _parseShapeStrokeDashPattern(const char *ptr, const char *end, Shape
|
||||||
{
|
{
|
||||||
uint32_t dashPatternCnt;
|
uint32_t dashPatternCnt;
|
||||||
_read_tvg_ui32(&dashPatternCnt, ptr);
|
_read_tvg_ui32(&dashPatternCnt, ptr);
|
||||||
ptr += sizeof(uint32_t);
|
ptr += SIZE(uint32_t);
|
||||||
const float* dashPattern = (float*) ptr;
|
const float* dashPattern = (float*) ptr;
|
||||||
ptr += sizeof(float) * dashPatternCnt;
|
ptr += SIZE(float) * dashPatternCnt;
|
||||||
|
|
||||||
if (ptr > end) return false;
|
if (ptr > end) return false;
|
||||||
|
|
||||||
|
@ -309,7 +310,7 @@ static bool _parseShapeStroke(const char *ptr, const char *end, Shape *shape)
|
||||||
|
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
case TVG_TAG_SHAPE_STROKE_CAP: {
|
case TVG_TAG_SHAPE_STROKE_CAP: {
|
||||||
if (block.length != sizeof(TvgBinFlag)) return false;
|
if (block.length != SIZE(TvgBinFlag)) return false;
|
||||||
switch (*block.data) {
|
switch (*block.data) {
|
||||||
case TVG_FLAG_SHAPE_STROKE_CAP_SQUARE:
|
case TVG_FLAG_SHAPE_STROKE_CAP_SQUARE:
|
||||||
shape->stroke(StrokeCap::Square);
|
shape->stroke(StrokeCap::Square);
|
||||||
|
@ -324,7 +325,7 @@ static bool _parseShapeStroke(const char *ptr, const char *end, Shape *shape)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TVG_TAG_SHAPE_STROKE_JOIN: {
|
case TVG_TAG_SHAPE_STROKE_JOIN: {
|
||||||
if (block.length != sizeof(TvgBinFlag)) return false;
|
if (block.length != SIZE(TvgBinFlag)) return false;
|
||||||
switch (*block.data) {
|
switch (*block.data) {
|
||||||
case TVG_FLAG_SHAPE_STROKE_JOIN_BEVEL:
|
case TVG_FLAG_SHAPE_STROKE_JOIN_BEVEL:
|
||||||
shape->stroke(StrokeJoin::Bevel);
|
shape->stroke(StrokeJoin::Bevel);
|
||||||
|
@ -339,7 +340,7 @@ static bool _parseShapeStroke(const char *ptr, const char *end, Shape *shape)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TVG_TAG_SHAPE_STROKE_WIDTH: {
|
case TVG_TAG_SHAPE_STROKE_WIDTH: {
|
||||||
if (block.length != sizeof(float)) return false;
|
if (block.length != SIZE(float)) return false;
|
||||||
float width;
|
float width;
|
||||||
_read_tvg_float(&width, block.data);
|
_read_tvg_float(&width, block.data);
|
||||||
shape->stroke(width);
|
shape->stroke(width);
|
||||||
|
@ -392,7 +393,7 @@ static bool _parseShape(TvgBinBlock block, Paint* paint)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TVG_TAG_SHAPE_FILLRULE: {
|
case TVG_TAG_SHAPE_FILLRULE: {
|
||||||
if (block.length != sizeof(TvgBinFlag)) return false;
|
if (block.length != SIZE(TvgBinFlag)) return false;
|
||||||
switch (*block.data) {
|
switch (*block.data) {
|
||||||
case TVG_FLAG_SHAPE_FILLRULE_WINDING:
|
case TVG_FLAG_SHAPE_FILLRULE_WINDING:
|
||||||
shape->fill(FillRule::Winding);
|
shape->fill(FillRule::Winding);
|
||||||
|
@ -415,18 +416,18 @@ static bool _parsePicture(TvgBinBlock block, Paint* paint)
|
||||||
|
|
||||||
switch (block.type) {
|
switch (block.type) {
|
||||||
case TVG_TAG_PICTURE_RAW_IMAGE: {
|
case TVG_TAG_PICTURE_RAW_IMAGE: {
|
||||||
if (block.length < 2 * sizeof(uint32_t)) return false;
|
if (block.length < 2 * SIZE(uint32_t)) return false;
|
||||||
|
|
||||||
auto ptr = block.data;
|
auto ptr = block.data;
|
||||||
uint32_t w, h;
|
uint32_t w, h;
|
||||||
|
|
||||||
_read_tvg_ui32(&w, ptr);
|
_read_tvg_ui32(&w, ptr);
|
||||||
ptr += sizeof(uint32_t);
|
ptr += SIZE(uint32_t);
|
||||||
_read_tvg_ui32(&h, ptr);
|
_read_tvg_ui32(&h, ptr);
|
||||||
ptr += sizeof(uint32_t);
|
ptr += SIZE(uint32_t);
|
||||||
|
|
||||||
auto size = w * h * sizeof(uint32_t);
|
auto size = w * h * SIZE(uint32_t);
|
||||||
if (block.length != 2 * sizeof(uint32_t) + size) return false;
|
if (block.length != 2 * SIZE(uint32_t) + size) return false;
|
||||||
|
|
||||||
picture->load((uint32_t*) ptr, w, h, true);
|
picture->load((uint32_t*) ptr, w, h, true);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
*/
|
*/
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include "tvgSaveModule.h"
|
#include "tvgSaveModule.h"
|
||||||
#include "tvgTvgSaver.h"
|
#include "tvgTvgSaver.h"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue