lottie: generalize parent context access in parsing.

ensure parent context is generally accessible,
not limited to gradients.
This commit is contained in:
Hermet Park 2024-04-15 21:54:47 +09:00
parent 8488c629eb
commit 1ccf14808b
2 changed files with 27 additions and 5 deletions

View file

@ -256,7 +256,7 @@ void LottieParser::getValue(ColorStop& color)
{ {
if (peekType() == kArrayType) enterArray(); if (peekType() == kArrayType) enterArray();
color.input = new Array<float>(context.gradient->colorStops.count); color.input = new Array<float>(static_cast<LottieGradient*>(context.parent)->colorStops.count);
while (nextArrayValue()) color.input->push(getFloat()); while (nextArrayValue()) color.input->push(getFloat());
} }
@ -496,6 +496,8 @@ LottieRect* LottieParser::parseRect()
auto rect = new LottieRect; auto rect = new LottieRect;
if (!rect) return nullptr; if (!rect) return nullptr;
context.parent = rect;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("s")) parseProperty(rect->size); if (KEY_AS("s")) parseProperty(rect->size);
else if (KEY_AS("p")) parseProperty(rect->position); else if (KEY_AS("p")) parseProperty(rect->position);
@ -514,6 +516,8 @@ LottieEllipse* LottieParser::parseEllipse()
auto ellipse = new LottieEllipse; auto ellipse = new LottieEllipse;
if (!ellipse) return nullptr; if (!ellipse) return nullptr;
context.parent = ellipse;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) ellipse->name = getStringCopy(); if (KEY_AS("nm")) ellipse->name = getStringCopy();
else if (KEY_AS("p")) parseProperty(ellipse->position); else if (KEY_AS("p")) parseProperty(ellipse->position);
@ -531,6 +535,8 @@ LottieTransform* LottieParser::parseTransform(bool ddd)
auto transform = new LottieTransform; auto transform = new LottieTransform;
if (!transform) return nullptr; if (!transform) return nullptr;
context.parent = transform;
if (ddd) { if (ddd) {
transform->rotationEx = new LottieTransform::RotationEx; transform->rotationEx = new LottieTransform::RotationEx;
TVGLOG("LOTTIE", "3D transform(ddd) is not totally compatible."); TVGLOG("LOTTIE", "3D transform(ddd) is not totally compatible.");
@ -574,6 +580,8 @@ LottieSolidFill* LottieParser::parseSolidFill()
auto fill = new LottieSolidFill; auto fill = new LottieSolidFill;
if (!fill) return nullptr; if (!fill) return nullptr;
context.parent = fill;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) fill->name = getStringCopy(); if (KEY_AS("nm")) fill->name = getStringCopy();
else if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(fill->color, fill); else if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(fill->color, fill);
@ -613,6 +621,8 @@ LottieSolidStroke* LottieParser::parseSolidStroke()
auto stroke = new LottieSolidStroke; auto stroke = new LottieSolidStroke;
if (!stroke) return nullptr; if (!stroke) return nullptr;
context.parent = stroke;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(stroke->color, stroke); if (KEY_AS("c")) parseProperty<LottieProperty::Type::Color>(stroke->color, stroke);
else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(stroke->opacity, stroke); else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(stroke->opacity, stroke);
@ -668,6 +678,8 @@ LottiePolyStar* LottieParser::parsePolyStar()
auto star = new LottiePolyStar; auto star = new LottiePolyStar;
if (!star) return nullptr; if (!star) return nullptr;
context.parent = star;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) star->name = getStringCopy(); if (KEY_AS("nm")) star->name = getStringCopy();
else if (KEY_AS("p")) parseProperty(star->position); else if (KEY_AS("p")) parseProperty(star->position);
@ -691,6 +703,8 @@ LottieRoundedCorner* LottieParser::parseRoundedCorner()
auto corner = new LottieRoundedCorner; auto corner = new LottieRoundedCorner;
if (!corner) return nullptr; if (!corner) return nullptr;
context.parent = corner;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) corner->name = getStringCopy(); if (KEY_AS("nm")) corner->name = getStringCopy();
else if (KEY_AS("r")) parseProperty(corner->radius); else if (KEY_AS("r")) parseProperty(corner->radius);
@ -704,8 +718,6 @@ LottieRoundedCorner* LottieParser::parseRoundedCorner()
void LottieParser::parseGradient(LottieGradient* gradient, const char* key) void LottieParser::parseGradient(LottieGradient* gradient, const char* key)
{ {
context.gradient = gradient;
if (KEY_AS("t")) gradient->id = getInt(); if (KEY_AS("t")) gradient->id = getInt();
else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(gradient->opacity, gradient); else if (KEY_AS("o")) parseProperty<LottieProperty::Type::Opacity>(gradient->opacity, gradient);
else if (KEY_AS("g")) else if (KEY_AS("g"))
@ -730,6 +742,8 @@ LottieGradientFill* LottieParser::parseGradientFill()
auto fill = new LottieGradientFill; auto fill = new LottieGradientFill;
if (!fill) return nullptr; if (!fill) return nullptr;
context.parent = fill;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) fill->name = getStringCopy(); if (KEY_AS("nm")) fill->name = getStringCopy();
else if (KEY_AS("r")) fill->rule = getFillRule(); else if (KEY_AS("r")) fill->rule = getFillRule();
@ -748,6 +762,8 @@ LottieGradientStroke* LottieParser::parseGradientStroke()
auto stroke = new LottieGradientStroke; auto stroke = new LottieGradientStroke;
if (!stroke) return nullptr; if (!stroke) return nullptr;
context.parent = stroke;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) stroke->name = getStringCopy(); if (KEY_AS("nm")) stroke->name = getStringCopy();
else if (KEY_AS("lc")) stroke->cap = getStrokeCap(); else if (KEY_AS("lc")) stroke->cap = getStrokeCap();
@ -769,6 +785,8 @@ LottieTrimpath* LottieParser::parseTrimpath()
auto trim = new LottieTrimpath; auto trim = new LottieTrimpath;
if (!trim) return nullptr; if (!trim) return nullptr;
context.parent = trim;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) trim->name = getStringCopy(); if (KEY_AS("nm")) trim->name = getStringCopy();
else if (KEY_AS("s")) parseProperty(trim->start); else if (KEY_AS("s")) parseProperty(trim->start);
@ -789,6 +807,8 @@ LottieRepeater* LottieParser::parseRepeater()
auto repeater = new LottieRepeater; auto repeater = new LottieRepeater;
if (!repeater) return nullptr; if (!repeater) return nullptr;
context.parent = repeater;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
if (KEY_AS("nm")) repeater->name = getStringCopy(); if (KEY_AS("nm")) repeater->name = getStringCopy();
else if (KEY_AS("c")) parseProperty(repeater->copies); else if (KEY_AS("c")) parseProperty(repeater->copies);
@ -1286,17 +1306,19 @@ bool LottieParser::apply(LottieSlot* slot)
switch (slot->type) { switch (slot->type) {
case LottieProperty::Type::ColorStop: { case LottieProperty::Type::ColorStop: {
obj = new LottieGradient; obj = new LottieGradient;
context.gradient = static_cast<LottieGradient*>(obj); context.parent = obj;
parseSlotProperty(static_cast<LottieGradient*>(obj)->colorStops); parseSlotProperty(static_cast<LottieGradient*>(obj)->colorStops);
break; break;
} }
case LottieProperty::Type::Color: { case LottieProperty::Type::Color: {
obj = new LottieSolid; obj = new LottieSolid;
context.parent = obj;
parseSlotProperty(static_cast<LottieSolid*>(obj)->color); parseSlotProperty(static_cast<LottieSolid*>(obj)->color);
break; break;
} }
case LottieProperty::Type::TextDoc: { case LottieProperty::Type::TextDoc: {
obj = new LottieText; obj = new LottieText;
context.parent = obj;
parseSlotProperty(static_cast<LottieText*>(obj)->doc); parseSlotProperty(static_cast<LottieText*>(obj)->doc);
break; break;
} }

View file

@ -111,7 +111,7 @@ private:
//Current parsing context //Current parsing context
struct Context { struct Context {
LottieLayer* layer = nullptr; LottieLayer* layer = nullptr;
LottieGradient* gradient = nullptr; LottieObject* parent = nullptr;
} context; } context;
}; };