lottie: code refactoring.

make it neat and clean.
This commit is contained in:
Hermet Park 2024-04-15 17:42:18 +09:00
parent bfed28e86e
commit c7d51ad0bb

View file

@ -30,6 +30,7 @@
/* Internal Class Implementation */ /* Internal Class Implementation */
/************************************************************************/ /************************************************************************/
#define KEY_AS(name) !strcmp(key, name)
static char* _int2str(int num) static char* _int2str(int num)
@ -160,19 +161,19 @@ void LottieParser::getValue(TextDocument& doc)
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "s")) doc.size = getFloat(); if (KEY_AS("s")) doc.size = getFloat();
else if (!strcmp(key, "f")) doc.name = getStringCopy(); else if (KEY_AS("f")) doc.name = getStringCopy();
else if (!strcmp(key, "t")) doc.text = getStringCopy(); else if (KEY_AS("t")) doc.text = getStringCopy();
else if (!strcmp(key, "j")) doc.justify = getInt(); else if (KEY_AS("j")) doc.justify = getInt();
else if (!strcmp(key, "tr")) doc.tracking = getInt(); else if (KEY_AS("tr")) doc.tracking = getInt();
else if (!strcmp(key, "lh")) doc.height = getFloat(); else if (KEY_AS("lh")) doc.height = getFloat();
else if (!strcmp(key, "ls")) doc.shift = getFloat(); else if (KEY_AS("ls")) doc.shift = getFloat();
else if (!strcmp(key, "fc")) getValue(doc.color); else if (KEY_AS("fc")) getValue(doc.color);
else if (!strcmp(key, "ps")) getValue(doc.bbox.pos); else if (KEY_AS("ps")) getValue(doc.bbox.pos);
else if (!strcmp(key, "sz")) getValue(doc.bbox.size); else if (KEY_AS("sz")) getValue(doc.bbox.size);
else if (!strcmp(key, "sc")) getValue(doc.stroke.color); else if (KEY_AS("sc")) getValue(doc.stroke.color);
else if (!strcmp(key, "sw")) doc.stroke.width = getFloat(); else if (KEY_AS("sw")) doc.stroke.width = getFloat();
else if (!strcmp(key, "of")) doc.stroke.render = getBool(); else if (KEY_AS("of")) doc.stroke.render = getBool();
else skip(key); else skip(key);
} }
} }
@ -190,10 +191,10 @@ void LottieParser::getValue(PathSet& path)
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "i")) getValue(ins); if (KEY_AS("i")) getValue(ins);
else if (!strcmp(key, "o")) getValue(outs); else if (KEY_AS("o")) getValue(outs);
else if (!strcmp(key, "v")) getValue(pts); else if (KEY_AS("v")) getValue(pts);
else if (!strcmp(key, "c")) closed = getBool(); else if (KEY_AS("c")) closed = getBool();
else skip(key); else skip(key);
} }
@ -332,8 +333,8 @@ void LottieParser::getInperpolatorPoint(Point& pt)
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "x")) getValue(pt.x); if (KEY_AS("x")) getValue(pt.x);
else if (!strcmp(key, "y")) getValue(pt.y); else if (KEY_AS("y")) getValue(pt.y);
} }
} }
@ -342,7 +343,7 @@ template<typename T>
void LottieParser::parseSlotProperty(T& prop) void LottieParser::parseSlotProperty(T& prop)
{ {
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "p")) parseProperty(prop); if (KEY_AS("p")) parseProperty(prop);
else skip(key); else skip(key);
} }
} }
@ -351,10 +352,10 @@ void LottieParser::parseSlotProperty(T& prop)
template<typename T> template<typename T>
bool LottieParser::parseTangent(const char *key, LottieVectorFrame<T>& value) bool LottieParser::parseTangent(const char *key, LottieVectorFrame<T>& value)
{ {
if (!strcmp(key, "ti")) { if (KEY_AS("ti")) {
value.hasTangent = true; value.hasTangent = true;
getValue(value.inTangent); getValue(value.inTangent);
} else if (!strcmp(key, "to")) { } else if (KEY_AS("to")) {
value.hasTangent = true; value.hasTangent = true;
getValue(value.outTangent); getValue(value.outTangent);
} else return false; } else return false;
@ -408,12 +409,12 @@ void LottieParser::parseKeyFrame(T& prop)
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "i")) { if (KEY_AS("i")) {
interpolator = true; interpolator = true;
getInperpolatorPoint(inTangent); getInperpolatorPoint(inTangent);
} else if (!strcmp(key, "o")) { } else if (KEY_AS("o")) {
getInperpolatorPoint(outTangent); getInperpolatorPoint(outTangent);
} else if (!strcmp(key, "n")) { } else if (KEY_AS("n")) {
if (peekType() == kStringType) { if (peekType() == kStringType) {
interpolatorKey = getString(); interpolatorKey = getString();
} else { } else {
@ -423,18 +424,18 @@ void LottieParser::parseKeyFrame(T& prop)
else skip(nullptr); else skip(nullptr);
} }
} }
} else if (!strcmp(key, "t")) { } else if (KEY_AS("t")) {
frame.no = getFloat(); frame.no = getFloat();
} else if (!strcmp(key, "s")) { } else if (KEY_AS("s")) {
getValue(frame.value); getValue(frame.value);
} else if (!strcmp(key, "e")) { } else if (KEY_AS("e")) {
//current end frame and the next start frame is duplicated, //current end frame and the next start frame is duplicated,
//We propagate the end value to the next frame to avoid having duplicated values. //We propagate the end value to the next frame to avoid having duplicated values.
auto& frame2 = prop.nextFrame(); auto& frame2 = prop.nextFrame();
getValue(frame2.value); getValue(frame2.value);
} else if (parseTangent(key, frame)) { } else if (parseTangent(key, frame)) {
continue; continue;
} else if (!strcmp(key, "h")) { } else if (KEY_AS("h")) {
frame.hold = getInt(); frame.hold = getInt();
} else skip(key); } else skip(key);
} }
@ -475,8 +476,8 @@ void LottieParser::parseProperty(T& prop, LottieObject* obj)
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "k")) parsePropertyInternal(prop); if (KEY_AS("k")) parsePropertyInternal(prop);
else if (obj && !strcmp(key, "sid")) { else if (obj && KEY_AS("sid")) {
auto sid = getStringCopy(); auto sid = getStringCopy();
//append object if the slot already exists. //append object if the slot already exists.
for (auto slot = comp->slots.begin(); slot < comp->slots.end(); ++slot) { for (auto slot = comp->slots.begin(); slot < comp->slots.end(); ++slot) {
@ -496,11 +497,11 @@ LottieRect* LottieParser::parseRect()
if (!rect) return nullptr; if (!rect) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "s")) parseProperty(rect->size); if (KEY_AS("s")) parseProperty(rect->size);
else if (!strcmp(key, "p")) parseProperty(rect->position); else if (KEY_AS("p")) parseProperty(rect->position);
else if (!strcmp(key, "r")) parseProperty(rect->radius); else if (KEY_AS("r")) parseProperty(rect->radius);
else if (!strcmp(key, "nm")) rect->name = getStringCopy(); else if (KEY_AS("nm")) rect->name = getStringCopy();
else if (!strcmp(key, "hd")) rect->hidden = getBool(); else if (KEY_AS("hd")) rect->hidden = getBool();
else skip(key); else skip(key);
} }
rect->prepare(); rect->prepare();
@ -514,10 +515,10 @@ LottieEllipse* LottieParser::parseEllipse()
if (!ellipse) return nullptr; if (!ellipse) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) ellipse->name = getStringCopy(); if (KEY_AS("nm")) ellipse->name = getStringCopy();
else if (!strcmp(key, "p")) parseProperty(ellipse->position); else if (KEY_AS("p")) parseProperty(ellipse->position);
else if (!strcmp(key, "s")) parseProperty(ellipse->size); else if (KEY_AS("s")) parseProperty(ellipse->size);
else if (!strcmp(key, "hd")) ellipse->hidden = getBool(); else if (KEY_AS("hd")) ellipse->hidden = getBool();
else skip(key); else skip(key);
} }
ellipse->prepare(); ellipse->prepare();
@ -536,31 +537,31 @@ LottieTransform* LottieParser::parseTransform(bool ddd)
} }
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "p")) if (KEY_AS("p"))
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "k")) parsePropertyInternal(transform->position); if (KEY_AS("k")) parsePropertyInternal(transform->position);
else if (!strcmp(key, "s")) { else if (KEY_AS("s")) {
if (getBool()) transform->coords = new LottieTransform::SeparateCoord; if (getBool()) transform->coords = new LottieTransform::SeparateCoord;
//check separateCoord to figure out whether "x(expression)" / "x(coord)" //check separateCoord to figure out whether "x(expression)" / "x(coord)"
} else if (transform->coords && !strcmp(key, "x")) { } else if (transform->coords && KEY_AS("x")) {
parseProperty(transform->coords->x); parseProperty(transform->coords->x);
} else if (transform->coords && !strcmp(key, "y")) { } else if (transform->coords && KEY_AS("y")) {
parseProperty(transform->coords->y); parseProperty(transform->coords->y);
} else skip(key); } else skip(key);
} }
} }
else if (!strcmp(key, "a")) parseProperty(transform->anchor); else if (KEY_AS("a")) parseProperty(transform->anchor);
else if (!strcmp(key, "s")) parseProperty(transform->scale); else if (KEY_AS("s")) parseProperty(transform->scale);
else if (!strcmp(key, "r")) parseProperty(transform->rotation); else if (KEY_AS("r")) parseProperty(transform->rotation);
else if (!strcmp(key, "o")) parseProperty(transform->opacity); else if (KEY_AS("o")) parseProperty(transform->opacity);
else if (transform->rotationEx && !strcmp(key, "rx")) parseProperty(transform->rotationEx->x); else if (transform->rotationEx && KEY_AS("rx")) parseProperty(transform->rotationEx->x);
else if (transform->rotationEx && !strcmp(key, "ry")) parseProperty(transform->rotationEx->y); else if (transform->rotationEx && KEY_AS("ry")) parseProperty(transform->rotationEx->y);
else if (transform->rotationEx && !strcmp(key, "rz")) parseProperty(transform->rotation); else if (transform->rotationEx && KEY_AS("rz")) parseProperty(transform->rotation);
else if (!strcmp(key, "nm")) transform->name = getStringCopy(); else if (KEY_AS("nm")) transform->name = getStringCopy();
//else if (!strcmp(key, "sk")) //TODO: skew //else if (KEY_AS("sk")) //TODO: skew
//else if (!strcmp(key, "sa")) //TODO: skew axis //else if (KEY_AS("sa")) //TODO: skew axis
else skip(key); else skip(key);
} }
transform->prepare(); transform->prepare();
@ -574,12 +575,12 @@ LottieSolidFill* LottieParser::parseSolidFill()
if (!fill) return nullptr; if (!fill) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) fill->name = getStringCopy(); if (KEY_AS("nm")) fill->name = getStringCopy();
else if (!strcmp(key, "c")) parseProperty<LottieProperty::Type::Color>(fill->color, fill); else if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(fill->color, fill);
else if (!strcmp(key, "o")) parseProperty<LottieProperty::Type::Opacity>(fill->opacity, fill); else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(fill->opacity, fill);
else if (!strcmp(key, "fillEnabled")) fill->hidden |= !getBool(); else if (KEY_AS("fillEnabled")) fill->hidden |= !getBool();
else if (!strcmp(key, "r")) fill->rule = getFillRule(); else if (KEY_AS("r")) fill->rule = getFillRule();
else if (!strcmp(key, "hd")) fill->hidden = getBool(); else if (KEY_AS("hd")) fill->hidden = getBool();
else skip(key); else skip(key);
} }
fill->prepare(); fill->prepare();
@ -594,12 +595,12 @@ void LottieParser::parseStrokeDash(LottieStroke* stroke)
enterObject(); enterObject();
int idx = 0; int idx = 0;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "n")) { if (KEY_AS("n")) {
auto style = getString(); auto style = getString();
if (!strcmp("o", style)) idx = 0; //offset if (!strcmp("o", style)) idx = 0; //offset
else if (!strcmp("d", style)) idx = 1; //dash else if (!strcmp("d", style)) idx = 1; //dash
else if (!strcmp("g", style)) idx = 2; //gap else if (!strcmp("g", style)) idx = 2; //gap
} else if (!strcmp(key, "v")) { } else if (KEY_AS("v")) {
parseProperty(stroke->dash(idx)); parseProperty(stroke->dash(idx));
} else skip(key); } else skip(key);
} }
@ -613,16 +614,16 @@ LottieSolidStroke* LottieParser::parseSolidStroke()
if (!stroke) return nullptr; if (!stroke) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "c")) parseProperty<LottieProperty::Type::Color>(stroke->color, stroke); if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(stroke->color, stroke);
else if (!strcmp(key, "o")) parseProperty<LottieProperty::Type::Opacity>(stroke->opacity, stroke); else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(stroke->opacity, stroke);
else if (!strcmp(key, "w")) parseProperty<LottieProperty::Type::Float>(stroke->width, stroke); else if (KEY_AS("w")) parseProperty<LottieProperty::Type::Float>(stroke->width, stroke);
else if (!strcmp(key, "lc")) stroke->cap = getStrokeCap(); else if (KEY_AS("lc")) stroke->cap = getStrokeCap();
else if (!strcmp(key, "lj")) stroke->join = getStrokeJoin(); else if (KEY_AS("lj")) stroke->join = getStrokeJoin();
else if (!strcmp(key, "ml")) stroke->miterLimit = getFloat(); else if (KEY_AS("ml")) stroke->miterLimit = getFloat();
else if (!strcmp(key, "nm")) stroke->name = getStringCopy(); else if (KEY_AS("nm")) stroke->name = getStringCopy();
else if (!strcmp(key, "hd")) stroke->hidden = getBool(); else if (KEY_AS("hd")) stroke->hidden = getBool();
else if (!strcmp(key, "fillEnabled")) stroke->hidden |= !getBool(); else if (KEY_AS("fillEnabled")) stroke->hidden |= !getBool();
else if (!strcmp(key, "d")) parseStrokeDash(stroke); else if (KEY_AS("d")) parseStrokeDash(stroke);
else skip(key); else skip(key);
} }
stroke->prepare(); stroke->prepare();
@ -634,7 +635,7 @@ LottieSolidStroke* LottieParser::parseSolidStroke()
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "k")) { if (KEY_AS("k")) {
if (peekType() == kArrayType) { if (peekType() == kArrayType) {
enterArray(); enterArray();
while (nextArrayValue()) parseKeyFrame(path); while (nextArrayValue()) parseKeyFrame(path);
@ -652,9 +653,9 @@ LottiePath* LottieParser::parsePath()
if (!path) return nullptr; if (!path) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) path->name = getStringCopy(); if (KEY_AS("nm")) path->name = getStringCopy();
else if (!strcmp(key, "ks")) getPathSet(path->pathset); else if (KEY_AS("ks")) getPathSet(path->pathset);
else if (!strcmp(key, "hd")) path->hidden = getBool(); else if (KEY_AS("hd")) path->hidden = getBool();
else skip(key); else skip(key);
} }
path->prepare(); path->prepare();
@ -668,16 +669,16 @@ LottiePolyStar* LottieParser::parsePolyStar()
if (!star) return nullptr; if (!star) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) star->name = getStringCopy(); if (KEY_AS("nm")) star->name = getStringCopy();
else if (!strcmp(key, "p")) parseProperty(star->position); else if (KEY_AS("p")) parseProperty(star->position);
else if (!strcmp(key, "pt")) parseProperty(star->ptsCnt); else if (KEY_AS("pt")) parseProperty(star->ptsCnt);
else if (!strcmp(key, "ir")) parseProperty(star->innerRadius); else if (KEY_AS("ir")) parseProperty(star->innerRadius);
else if (!strcmp(key, "is")) parseProperty(star->innerRoundness); else if (KEY_AS("is")) parseProperty(star->innerRoundness);
else if (!strcmp(key, "or")) parseProperty(star->outerRadius); else if (KEY_AS("or")) parseProperty(star->outerRadius);
else if (!strcmp(key, "os")) parseProperty(star->outerRoundness); else if (KEY_AS("os")) parseProperty(star->outerRoundness);
else if (!strcmp(key, "r")) parseProperty(star->rotation); else if (KEY_AS("r")) parseProperty(star->rotation);
else if (!strcmp(key, "sy")) star->type = (LottiePolyStar::Type) getInt(); else if (KEY_AS("sy")) star->type = (LottiePolyStar::Type) getInt();
else if (!strcmp(key, "hd")) star->hidden = getBool(); else if (KEY_AS("hd")) star->hidden = getBool();
else skip(key); else skip(key);
} }
star->prepare(); star->prepare();
@ -691,9 +692,9 @@ LottieRoundedCorner* LottieParser::parseRoundedCorner()
if (!corner) return nullptr; if (!corner) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) corner->name = getStringCopy(); if (KEY_AS("nm")) corner->name = getStringCopy();
else if (!strcmp(key, "r")) parseProperty(corner->radius); else if (KEY_AS("r")) parseProperty(corner->radius);
else if (!strcmp(key, "hd")) corner->hidden = getBool(); else if (KEY_AS("hd")) corner->hidden = getBool();
else skip(key); else skip(key);
} }
corner->prepare(); corner->prepare();
@ -705,21 +706,21 @@ void LottieParser::parseGradient(LottieGradient* gradient, const char* key)
{ {
context.gradient = gradient; context.gradient = gradient;
if (!strcmp(key, "t")) gradient->id = getInt(); if (KEY_AS("t")) gradient->id = getInt();
else if (!strcmp(key, "o")) parseProperty<LottieProperty::Type::Opacity>(gradient->opacity, gradient); else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(gradient->opacity, gradient);
else if (!strcmp(key, "g")) else if (KEY_AS("g"))
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "p")) gradient->colorStops.count = getInt(); if (KEY_AS("p")) gradient->colorStops.count = getInt();
else if (!strcmp(key, "k")) parseProperty<LottieProperty::Type::ColorStop>(gradient->colorStops, gradient); else if (KEY_AS("k")) parseProperty<LottieProperty::Type::ColorStop>(gradient->colorStops, gradient);
else skip(key); else skip(key);
} }
} }
else if (!strcmp(key, "s")) parseProperty<LottieProperty::Type::Point>(gradient->start, gradient); else if (KEY_AS("s")) parseProperty<LottieProperty::Type::Point>(gradient->start, gradient);
else if (!strcmp(key, "e")) parseProperty<LottieProperty::Type::Point>(gradient->end, gradient); else if (KEY_AS("e")) parseProperty<LottieProperty::Type::Point>(gradient->end, gradient);
else if (!strcmp(key, "h")) parseProperty<LottieProperty::Type::Float>(gradient->height, gradient); else if (KEY_AS("h")) parseProperty<LottieProperty::Type::Float>(gradient->height, gradient);
else if (!strcmp(key, "a")) parseProperty<LottieProperty::Type::Float>(gradient->angle, gradient); else if (KEY_AS("a")) parseProperty<LottieProperty::Type::Float>(gradient->angle, gradient);
else skip(key); else skip(key);
} }
@ -730,9 +731,9 @@ LottieGradientFill* LottieParser::parseGradientFill()
if (!fill) return nullptr; if (!fill) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) fill->name = getStringCopy(); if (KEY_AS("nm")) fill->name = getStringCopy();
else if (!strcmp(key, "r")) fill->rule = getFillRule(); else if (KEY_AS("r")) fill->rule = getFillRule();
else if (!strcmp(key, "hd")) fill->hidden = getBool(); else if (KEY_AS("hd")) fill->hidden = getBool();
else parseGradient(fill, key); else parseGradient(fill, key);
} }
@ -748,13 +749,13 @@ LottieGradientStroke* LottieParser::parseGradientStroke()
if (!stroke) return nullptr; if (!stroke) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) stroke->name = getStringCopy(); if (KEY_AS("nm")) stroke->name = getStringCopy();
else if (!strcmp(key, "lc")) stroke->cap = getStrokeCap(); else if (KEY_AS("lc")) stroke->cap = getStrokeCap();
else if (!strcmp(key, "lj")) stroke->join = getStrokeJoin(); else if (KEY_AS("lj")) stroke->join = getStrokeJoin();
else if (!strcmp(key, "ml")) stroke->miterLimit = getFloat(); else if (KEY_AS("ml")) stroke->miterLimit = getFloat();
else if (!strcmp(key, "hd")) stroke->hidden = getBool(); else if (KEY_AS("hd")) stroke->hidden = getBool();
else if (!strcmp(key, "w")) parseProperty(stroke->width); else if (KEY_AS("w")) parseProperty(stroke->width);
else if (!strcmp(key, "d")) parseStrokeDash(stroke); else if (KEY_AS("d")) parseStrokeDash(stroke);
else parseGradient(stroke, key); else parseGradient(stroke, key);
} }
stroke->prepare(); stroke->prepare();
@ -769,12 +770,12 @@ LottieTrimpath* LottieParser::parseTrimpath()
if (!trim) return nullptr; if (!trim) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) trim->name = getStringCopy(); if (KEY_AS("nm")) trim->name = getStringCopy();
else if (!strcmp(key, "s")) parseProperty(trim->start); else if (KEY_AS("s")) parseProperty(trim->start);
else if (!strcmp(key, "e")) parseProperty(trim->end); else if (KEY_AS("e")) parseProperty(trim->end);
else if (!strcmp(key, "o")) parseProperty(trim->offset); else if (KEY_AS("o")) parseProperty(trim->offset);
else if (!strcmp(key, "m")) trim->type = static_cast<LottieTrimpath::Type>(getInt()); else if (KEY_AS("m")) trim->type = static_cast<LottieTrimpath::Type>(getInt());
else if (!strcmp(key, "hd")) trim->hidden = getBool(); else if (KEY_AS("hd")) trim->hidden = getBool();
else skip(key); else skip(key);
} }
trim->prepare(); trim->prepare();
@ -789,24 +790,24 @@ LottieRepeater* LottieParser::parseRepeater()
if (!repeater) return nullptr; if (!repeater) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) repeater->name = getStringCopy(); if (KEY_AS("nm")) repeater->name = getStringCopy();
else if (!strcmp(key, "c")) parseProperty(repeater->copies); else if (KEY_AS("c")) parseProperty(repeater->copies);
else if (!strcmp(key, "o")) parseProperty(repeater->offset); else if (KEY_AS("o")) parseProperty(repeater->offset);
else if (!strcmp(key, "m")) repeater->inorder = getInt(); else if (KEY_AS("m")) repeater->inorder = getInt();
else if (!strcmp(key, "tr")) else if (KEY_AS("tr"))
{ {
enterObject(); enterObject();
while (auto key2 = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key2, "a")) parseProperty(repeater->anchor); if (KEY_AS("a")) parseProperty(repeater->anchor);
else if (!strcmp(key2, "p")) parseProperty(repeater->position); else if (KEY_AS("p")) parseProperty(repeater->position);
else if (!strcmp(key2, "r")) parseProperty(repeater->rotation); else if (KEY_AS("r")) parseProperty(repeater->rotation);
else if (!strcmp(key2, "s")) parseProperty(repeater->scale); else if (KEY_AS("s")) parseProperty(repeater->scale);
else if (!strcmp(key2, "so")) parseProperty(repeater->startOpacity); else if (KEY_AS("so")) parseProperty(repeater->startOpacity);
else if (!strcmp(key2, "eo")) parseProperty(repeater->endOpacity); else if (KEY_AS("eo")) parseProperty(repeater->endOpacity);
else skip(key2); else skip(key);
} }
} }
else if (!strcmp(key, "hd")) repeater->hidden = getBool(); else if (KEY_AS("hd")) repeater->hidden = getBool();
else skip(key); else skip(key);
} }
repeater->prepare(); repeater->prepare();
@ -846,7 +847,7 @@ void LottieParser::parseObject(Array<LottieObject*>& parent)
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "ty")) { if (KEY_AS("ty")) {
if (auto child = parseObject()) { if (auto child = parseObject()) {
if (child->hidden) delete(child); if (child->hidden) delete(child);
else parent.push(child); else parent.push(child);
@ -898,7 +899,7 @@ LottieObject* LottieParser::parseAsset()
auto embedded = false; auto embedded = false;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "id")) if (KEY_AS("id"))
{ {
if (peekType() == kStringType) { if (peekType() == kStringType) {
id = getStringCopy(); id = getStringCopy();
@ -906,10 +907,10 @@ LottieObject* LottieParser::parseAsset()
id = _int2str(getInt()); id = _int2str(getInt());
} }
} }
else if (!strcmp(key, "layers")) obj = parseLayers(); else if (KEY_AS("layers")) obj = parseLayers();
else if (!strcmp(key, "u")) subPath = getString(); else if (KEY_AS("u")) subPath = getString();
else if (!strcmp(key, "p")) data = getString(); else if (KEY_AS("p")) data = getString();
else if (!strcmp(key, "e")) embedded = getInt(); else if (KEY_AS("e")) embedded = getInt();
else skip(key); else skip(key);
} }
if (data) obj = parseImage(data, subPath, embedded); if (data) obj = parseImage(data, subPath, embedded);
@ -926,11 +927,11 @@ LottieFont* LottieParser::parseFont()
auto font = new LottieFont; auto font = new LottieFont;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "fName")) font->name = getStringCopy(); if (KEY_AS("fName")) font->name = getStringCopy();
else if (!strcmp(key, "fFamily")) font->family = getStringCopy(); else if (KEY_AS("fFamily")) font->family = getStringCopy();
else if (!strcmp(key, "fStyle")) font->style = getStringCopy(); else if (KEY_AS("fStyle")) font->style = getStringCopy();
else if (!strcmp(key, "ascent")) font->ascent = getFloat(); else if (KEY_AS("ascent")) font->ascent = getFloat();
else if (!strcmp(key, "origin")) font->origin = (LottieFont::Origin) getInt(); else if (KEY_AS("origin")) font->origin = (LottieFont::Origin) getInt();
else skip(key); else skip(key);
} }
return font; return font;
@ -954,9 +955,9 @@ LottieMarker* LottieParser::parseMarker()
auto marker = new LottieMarker; auto marker = new LottieMarker;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "cm")) marker->name = getStringCopy(); if (KEY_AS("cm")) marker->name = getStringCopy();
else if (!strcmp(key, "tm")) marker->time = getFloat(); else if (KEY_AS("tm")) marker->time = getFloat();
else if (!strcmp(key, "dr")) marker->duration = getFloat(); else if (KEY_AS("dr")) marker->duration = getFloat();
else skip(key); else skip(key);
} }
@ -979,16 +980,16 @@ void LottieParser::parseChars(Array<LottieGlyph*>& glyphes)
//a new glyph //a new glyph
auto glyph = new LottieGlyph; auto glyph = new LottieGlyph;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp("ch", key)) glyph->code = getStringCopy(); if (KEY_AS("ch")) glyph->code = getStringCopy();
else if (!strcmp("size", key)) glyph->size = static_cast<uint16_t>(getFloat()); else if (KEY_AS("size")) glyph->size = static_cast<uint16_t>(getFloat());
else if (!strcmp("style", key)) glyph->style = getStringCopy(); else if (KEY_AS("style")) glyph->style = getStringCopy();
else if (!strcmp("w", key)) glyph->width = getFloat(); else if (KEY_AS("w")) glyph->width = getFloat();
else if (!strcmp("fFamily", key)) glyph->family = getStringCopy(); else if (KEY_AS("fFamily")) glyph->family = getStringCopy();
else if (!strcmp("data", key)) else if (KEY_AS("data"))
{ //glyph shapes { //glyph shapes
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "shapes")) parseShapes(glyph->children); if (KEY_AS("shapes")) parseShapes(glyph->children);
} }
} else skip(key); } else skip(key);
} }
@ -1001,7 +1002,7 @@ void LottieParser::parseFonts()
{ {
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp("list", key)) { if (KEY_AS("list")) {
enterArray(); enterArray();
while (nextArrayValue()) { while (nextArrayValue()) {
comp->fonts.push(parseFont()); comp->fonts.push(parseFont());
@ -1017,9 +1018,9 @@ LottieObject* LottieParser::parseGroup()
if (!group) return nullptr; if (!group) return nullptr;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "nm")) { if (KEY_AS("nm")) {
group->name = getStringCopy(); group->name = getStringCopy();
} else if (!strcmp(key, "it")) { } else if (KEY_AS("it")) {
enterArray(); enterArray();
while (nextArrayValue()) parseObject(group->children); while (nextArrayValue()) parseObject(group->children);
} else skip(key); } else skip(key);
@ -1058,12 +1059,12 @@ void LottieParser::parseShapes(Array<LottieObject*>& parent)
direction = 0; direction = 0;
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "it")) { if (KEY_AS("it")) {
enterArray(); enterArray();
while (nextArrayValue()) parseObject(parent); while (nextArrayValue()) parseObject(parent);
} else if (!strcmp(key, "d")) { } else if (KEY_AS("d")) {
direction = getDirection(); direction = getDirection();
} else if (!strcmp(key, "ty")) { } else if (KEY_AS("ty")) {
if (auto child = parseObject()) { if (auto child = parseObject()) {
if (child->hidden) delete(child); if (child->hidden) delete(child);
else parent.push(child); else parent.push(child);
@ -1080,14 +1081,14 @@ void LottieParser::parseTextRange(LottieText* text)
enterArray(); enterArray();
while (nextArrayValue()) { while (nextArrayValue()) {
enterObject(); enterObject();
while (auto key2 = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key2, "a")) { //text style if (KEY_AS("a")) { //text style
enterObject(); enterObject();
while (auto key3 = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key3, "t")) parseProperty(text->spacing); if (KEY_AS("t")) parseProperty(text->spacing);
else skip(key3); else skip(key);
} }
} else skip(key2); } else skip(key);
} }
} }
} }
@ -1100,10 +1101,10 @@ void LottieParser::parseText(Array<LottieObject*>& parent)
auto text = new LottieText; auto text = new LottieText;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "d")) parseProperty<LottieProperty::Type::TextDoc>(text->doc, text); if (KEY_AS("d")) parseProperty<LottieProperty::Type::TextDoc>(text->doc, text);
else if (!strcmp(key, "a")) parseTextRange(text); else if (KEY_AS("a")) parseTextRange(text);
//else if (!strcmp(key, "p")) TVGLOG("LOTTIE", "Text Follow Path (p) is not supported"); //else if (KEY_AS("p")) TVGLOG("LOTTIE", "Text Follow Path (p) is not supported");
//else if (!strcmp(key, "m")) TVGLOG("LOTTIE", "Text Alignment Option (m) is not supported"); //else if (KEY_AS("m")) TVGLOG("LOTTIE", "Text Alignment Option (m) is not supported");
else skip(key); else skip(key);
} }
@ -1131,10 +1132,10 @@ LottieMask* LottieParser::parseMask()
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "inv")) mask->inverse = getBool(); if (KEY_AS("inv")) mask->inverse = getBool();
else if (!strcmp(key, "mode")) mask->method = getMaskMethod(mask->inverse); else if (KEY_AS("mode")) mask->method = getMaskMethod(mask->inverse);
else if (!strcmp(key, "pt")) getPathSet(mask->pathset); else if (KEY_AS("pt")) getPathSet(mask->pathset);
else if (!strcmp(key, "o")) parseProperty(mask->opacity); else if (KEY_AS("o")) parseProperty(mask->opacity);
else skip(key); else skip(key);
} }
@ -1165,34 +1166,34 @@ LottieLayer* LottieParser::parseLayer()
enterObject(); enterObject();
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "ddd")) ddd = getInt(); //3d layer if (KEY_AS("ddd")) ddd = getInt(); //3d layer
else if (!strcmp(key, "ind")) layer->id = getInt(); else if (KEY_AS("ind")) layer->id = getInt();
else if (!strcmp(key, "ty")) layer->type = (LottieLayer::Type) getInt(); else if (KEY_AS("ty")) layer->type = (LottieLayer::Type) getInt();
else if (!strcmp(key, "nm")) layer->name = getStringCopy(); else if (KEY_AS("nm")) layer->name = getStringCopy();
else if (!strcmp(key, "sr")) layer->timeStretch = getFloat(); else if (KEY_AS("sr")) layer->timeStretch = getFloat();
else if (!strcmp(key, "ks")) else if (KEY_AS("ks"))
{ {
enterObject(); enterObject();
layer->transform = parseTransform(ddd); layer->transform = parseTransform(ddd);
} }
else if (!strcmp(key, "ao")) layer->autoOrient = getInt(); else if (KEY_AS("ao")) layer->autoOrient = getInt();
else if (!strcmp(key, "shapes")) parseShapes(layer->children); else if (KEY_AS("shapes")) parseShapes(layer->children);
else if (!strcmp(key, "ip")) layer->inFrame = getFloat(); else if (KEY_AS("ip")) layer->inFrame = getFloat();
else if (!strcmp(key, "op")) layer->outFrame = getFloat(); else if (KEY_AS("op")) layer->outFrame = getFloat();
else if (!strcmp(key, "st")) layer->startFrame = getFloat(); else if (KEY_AS("st")) layer->startFrame = getFloat();
else if (!strcmp(key, "bm")) layer->blendMethod = getBlendMethod(); else if (KEY_AS("bm")) layer->blendMethod = getBlendMethod();
else if (!strcmp(key, "parent")) layer->pid = getInt(); else if (KEY_AS("parent")) layer->pid = getInt();
else if (!strcmp(key, "tm")) parseTimeRemap(layer); else if (KEY_AS("tm")) parseTimeRemap(layer);
else if (!strcmp(key, "w") || !strcmp(key, "sw")) getLayerSize(layer->w); else if (KEY_AS("w") || KEY_AS("sw")) getLayerSize(layer->w);
else if (!strcmp(key, "h") || !strcmp(key, "sh")) getLayerSize(layer->h); else if (KEY_AS("h") || KEY_AS("sh")) getLayerSize(layer->h);
else if (!strcmp(key, "sc")) layer->color = getColor(getString()); else if (KEY_AS("sc")) layer->color = getColor(getString());
else if (!strcmp(key, "tt")) layer->matte.type = getMatteType(); else if (KEY_AS("tt")) layer->matte.type = getMatteType();
else if (!strcmp(key, "masksProperties")) parseMasks(layer); else if (KEY_AS("masksProperties")) parseMasks(layer);
else if (!strcmp(key, "hd")) layer->hidden = getBool(); else if (KEY_AS("hd")) layer->hidden = getBool();
else if (!strcmp(key, "refId")) layer->refId = getStringCopy(); else if (KEY_AS("refId")) layer->refId = getStringCopy();
else if (!strcmp(key, "td")) layer->matteSrc = getInt(); //used for matte layer else if (KEY_AS("td")) layer->matteSrc = getInt(); //used for matte layer
else if (!strcmp(key, "t")) parseText(layer->children); else if (KEY_AS("t")) parseText(layer->children);
else if (!strcmp(key, "ef")) else if (KEY_AS("ef"))
{ {
TVGERR("LOTTIE", "layer effect(ef) is not supported!"); TVGERR("LOTTIE", "layer effect(ef) is not supported!");
skip(key); skip(key);
@ -1326,18 +1327,18 @@ bool LottieParser::parse()
Array<LottieGlyph*> glyphes; Array<LottieGlyph*> glyphes;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (!strcmp(key, "v")) comp->version = getStringCopy(); if (KEY_AS("v")) comp->version = getStringCopy();
else if (!strcmp(key, "fr")) comp->frameRate = getFloat(); else if (KEY_AS("fr")) comp->frameRate = getFloat();
else if (!strcmp(key, "ip")) comp->startFrame = getFloat(); else if (KEY_AS("ip")) comp->startFrame = getFloat();
else if (!strcmp(key, "op")) comp->endFrame = getFloat(); else if (KEY_AS("op")) comp->endFrame = getFloat();
else if (!strcmp(key, "w")) comp->w = getFloat(); else if (KEY_AS("w")) comp->w = getFloat();
else if (!strcmp(key, "h")) comp->h = getFloat(); else if (KEY_AS("h")) comp->h = getFloat();
else if (!strcmp(key, "nm")) comp->name = getStringCopy(); else if (KEY_AS("nm")) comp->name = getStringCopy();
else if (!strcmp(key, "assets")) parseAssets(); else if (KEY_AS("assets")) parseAssets();
else if (!strcmp(key, "layers")) comp->root = parseLayers(); else if (KEY_AS("layers")) comp->root = parseLayers();
else if (!strcmp(key, "fonts")) parseFonts(); else if (KEY_AS("fonts")) parseFonts();
else if (!strcmp(key, "chars")) parseChars(glyphes); else if (KEY_AS("chars")) parseChars(glyphes);
else if (!strcmp(key, "markers")) parseMarkers(); else if (KEY_AS("markers")) parseMarkers();
else skip(key); else skip(key);
} }