lottie: corrected an omission in overriding the default slot

issue: https://github.com/thorvg/thorvg/issues/2953
This commit is contained in:
Hermet Park 2024-11-18 16:23:23 +09:00
parent fcf080cc4e
commit 88098f547f
7 changed files with 108 additions and 85 deletions

View file

@ -44,7 +44,7 @@ void LottieLoader::run(unsigned tid)
comp = parser.comp;
}
if (parser.slots) {
override(parser.slots, false);
override(parser.slots, true);
parser.slots = nullptr;
}
builder->build(comp);
@ -290,7 +290,7 @@ Paint* LottieLoader::paint()
}
bool LottieLoader::override(const char* slots, bool copy)
bool LottieLoader::override(const char* slots, bool byDefault)
{
if (!ready() || comp->slots.count == 0) return false;
@ -299,7 +299,7 @@ bool LottieLoader::override(const char* slots, bool copy)
//override slots
if (slots) {
//Copy the input data because the JSON parser will encode the data immediately.
auto temp = copy ? strdup(slots) : slots;
auto temp = byDefault ? slots : strdup(slots);
//parsing slot json
LottieParser parser(temp, dirName);
@ -309,7 +309,7 @@ bool LottieLoader::override(const char* slots, bool copy)
while (auto sid = parser.sid(idx == 0)) {
for (auto s = comp->slots.begin(); s < comp->slots.end(); ++s) {
if (strcmp((*s)->sid, sid)) continue;
if (!parser.apply(*s)) success = false;
if (!parser.apply(*s, byDefault)) success = false;
break;
}
++idx;

View file

@ -57,7 +57,7 @@ public:
bool resize(Paint* paint, float w, float h) override;
bool read() override;
Paint* paint() override;
bool override(const char* slot, bool copy = true);
bool override(const char* slot, bool byDefault = false);
//Frame Controls
bool frame(float no) override;

View file

@ -78,56 +78,43 @@ void LottieSlot::reset()
}
void LottieSlot::assign(LottieObject* target)
void LottieSlot::assign(LottieObject* target, bool byDefault)
{
auto copy = !overridden && !byDefault;
//apply slot object to all targets
for (auto pair = pairs.begin(); pair < pairs.end(); ++pair) {
//backup the original properties before overwriting
switch (type) {
case LottieProperty::Type::Opacity: {
if (!overridden) {
pair->prop = new LottieOpacity;
*static_cast<LottieOpacity*>(pair->prop) = static_cast<LottieSolid*>(pair->obj)->opacity;
}
pair->obj->override(&static_cast<LottieSolid*>(target)->opacity);
if (copy) pair->prop = new LottieOpacity(static_cast<LottieSolid*>(pair->obj)->opacity);
pair->obj->override(&static_cast<LottieSolid*>(target)->opacity, byDefault);
break;
}
case LottieProperty::Type::Color: {
if (!overridden) {
pair->prop = new LottieColor;
*static_cast<LottieColor*>(pair->prop) = static_cast<LottieSolid*>(pair->obj)->color;
}
pair->obj->override(&static_cast<LottieSolid*>(target)->color);
if (copy) pair->prop = new LottieColor(static_cast<LottieSolid*>(pair->obj)->color);
pair->obj->override(&static_cast<LottieSolid*>(target)->color, byDefault);
break;
}
case LottieProperty::Type::ColorStop: {
if (!overridden) {
pair->prop = new LottieColorStop;
*static_cast<LottieColorStop*>(pair->prop) = static_cast<LottieGradient*>(pair->obj)->colorStops;
}
pair->obj->override(&static_cast<LottieGradient*>(target)->colorStops);
if (copy) pair->prop = new LottieColorStop(static_cast<LottieGradient*>(pair->obj)->colorStops);
pair->obj->override(&static_cast<LottieGradient*>(target)->colorStops, byDefault);
break;
}
case LottieProperty::Type::TextDoc: {
if (!overridden) {
pair->prop = new LottieTextDoc;
*static_cast<LottieTextDoc*>(pair->prop) = static_cast<LottieText*>(pair->obj)->doc;
}
pair->obj->override(&static_cast<LottieText*>(target)->doc);
if (copy) pair->prop = new LottieTextDoc(static_cast<LottieText*>(pair->obj)->doc);
pair->obj->override(&static_cast<LottieText*>(target)->doc, byDefault);
break;
}
case LottieProperty::Type::Image: {
if (!overridden) {
pair->prop = new LottieBitmap;
*static_cast<LottieBitmap*>(pair->prop) = static_cast<LottieImage*>(pair->obj)->data;
}
pair->obj->override(&static_cast<LottieImage*>(target)->data);
if (copy) pair->prop = new LottieBitmap(static_cast<LottieImage*>(pair->obj)->data);
pair->obj->override(&static_cast<LottieImage*>(target)->data, byDefault);
break;
}
default: break;
}
}
overridden = true;
if (!byDefault) overridden = true;
}

View file

@ -142,7 +142,7 @@ struct LottieObject
{
}
virtual void override(LottieProperty* prop)
virtual void override(LottieProperty* prop, bool byDefault = false)
{
TVGERR("LOTTIE", "Unsupported slot type");
}
@ -262,10 +262,11 @@ struct LottieText : LottieObject, LottieRenderPooler<tvg::Shape>
LottieObject::type = LottieObject::Text;
}
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault = false) override
{
this->doc = *static_cast<LottieTextDoc*>(prop);
this->prepare();
if (byDefault) doc.release();
doc = *static_cast<LottieTextDoc*>(prop);
prepare();
}
LottieProperty* property(uint16_t ix) override
@ -533,10 +534,11 @@ struct LottieSolidStroke : LottieSolid, LottieStroke
return LottieSolid::property(ix);
}
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault) override
{
this->color = *static_cast<LottieColor*>(prop);
this->prepare();
if (byDefault) color.release();
color = *static_cast<LottieColor*>(prop);
prepare();
}
};
@ -548,11 +550,16 @@ struct LottieSolidFill : LottieSolid
LottieObject::type = LottieObject::SolidFill;
}
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault) override
{
if (prop->type == LottieProperty::Type::Opacity) this->opacity = *static_cast<LottieOpacity*>(prop);
else if (prop->type == LottieProperty::Type::Color) this->color = *static_cast<LottieColor*>(prop);
this->prepare();
if (prop->type == LottieProperty::Type::Opacity) {
if (byDefault) opacity.release();
opacity = *static_cast<LottieOpacity*>(prop);
} else if (prop->type == LottieProperty::Type::Color) {
if (byDefault) color.release();
color = *static_cast<LottieColor*>(prop);
}
prepare();
}
FillRule rule = FillRule::Winding;
@ -610,10 +617,11 @@ struct LottieGradientFill : LottieGradient
LottieGradient::prepare();
}
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault) override
{
this->colorStops = *static_cast<LottieColorStop*>(prop);
this->prepare();
if (byDefault) colorStops.release();
colorStops = *static_cast<LottieColorStop*>(prop);
prepare();
}
FillRule rule = FillRule::Winding;
@ -639,10 +647,11 @@ struct LottieGradientStroke : LottieGradient, LottieStroke
return LottieGradient::property(ix);
}
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault = false) override
{
this->colorStops = *static_cast<LottieColorStop*>(prop);
this->prepare();
if (byDefault) colorStops.release();
colorStops = *static_cast<LottieColorStop*>(prop);
prepare();
}
};
@ -651,10 +660,11 @@ struct LottieImage : LottieObject, LottieRenderPooler<tvg::Picture>
{
LottieBitmap data;
void override(LottieProperty* prop) override
void override(LottieProperty* prop, bool byDefault = false) override
{
this->data = *static_cast<LottieBitmap*>(prop);
this->prepare();
if (byDefault) data.release();
data = *static_cast<LottieBitmap*>(prop);
prepare();
}
void prepare();
@ -825,7 +835,7 @@ struct LottieSlot
LottieProperty* prop;
};
void assign(LottieObject* target);
void assign(LottieObject* target, bool byDefault);
void reset();
LottieSlot(char* sid, LottieObject* obj, LottieProperty::Type type) : sid(sid), type(type)

View file

@ -1441,7 +1441,7 @@ const char* LottieParser::sid(bool first)
}
bool LottieParser::apply(LottieSlot* slot)
bool LottieParser::apply(LottieSlot* slot, bool byDefault)
{
enterObject();
@ -1486,7 +1486,7 @@ bool LottieParser::apply(LottieSlot* slot)
if (!obj || Invalid()) return false;
slot->assign(obj);
slot->assign(obj, byDefault);
delete(obj);

View file

@ -36,7 +36,7 @@ public:
}
bool parse();
bool apply(LottieSlot* slot);
bool apply(LottieSlot* slot, bool byDefault);
const char* sid(bool first = false);
void captureSlots(const char* key);
template<LottieProperty::Type type = LottieProperty::Type::Invalid> void registerSlot(LottieObject* obj, char* sid);

View file

@ -261,6 +261,11 @@ struct LottieGenericProperty : LottieProperty
LottieGenericProperty(T v) : value(v) {}
LottieGenericProperty() {}
LottieGenericProperty(const LottieGenericProperty<T>& rhs)
{
*this = rhs;
}
~LottieGenericProperty()
{
release();
@ -329,13 +334,13 @@ struct LottieGenericProperty : LottieProperty
return operator()(frameNo);
}
LottieGenericProperty<T>& operator=(const LottieGenericProperty<T>& other)
LottieGenericProperty<T>& operator=(const LottieGenericProperty<T>& rhs)
{
//shallow copy, used for slot overriding
if (other.frames) {
frames = other.frames;
const_cast<LottieGenericProperty<T>&>(other).frames = nullptr;
} else value = other.value;
if (rhs.frames) {
frames = rhs.frames;
const_cast<LottieGenericProperty<T>&>(rhs).frames = nullptr;
} else value = rhs.value;
return *this;
}
@ -504,6 +509,13 @@ struct LottieColorStop : LottieProperty
uint16_t count = 0; //colorstop count
bool populated = false;
LottieColorStop() {}
LottieColorStop(const LottieColorStop& rhs)
{
*this = rhs;
}
~LottieColorStop()
{
release();
@ -610,18 +622,18 @@ struct LottieColorStop : LottieProperty
return fill->colorStops(result.data, count);
}
LottieColorStop& operator=(const LottieColorStop& other)
LottieColorStop& operator=(const LottieColorStop& rhs)
{
//shallow copy, used for slot overriding
if (other.frames) {
frames = other.frames;
const_cast<LottieColorStop&>(other).frames = nullptr;
if (rhs.frames) {
frames = rhs.frames;
const_cast<LottieColorStop&>(rhs).frames = nullptr;
} else {
value = other.value;
const_cast<LottieColorStop&>(other).value = {nullptr, nullptr};
value = rhs.value;
const_cast<LottieColorStop&>(rhs).value = {nullptr, nullptr};
}
populated = other.populated;
count = other.count;
populated = rhs.populated;
count = rhs.count;
return *this;
}
@ -737,6 +749,13 @@ struct LottieTextDoc : LottieProperty
Array<LottieScalarFrame<TextDocument>>* frames = nullptr;
TextDocument value;
LottieTextDoc() {}
LottieTextDoc(const LottieTextDoc& rhs)
{
*this = rhs;
}
~LottieTextDoc()
{
release();
@ -810,16 +829,16 @@ struct LottieTextDoc : LottieProperty
return frame->value;
}
LottieTextDoc& operator=(const LottieTextDoc& other)
LottieTextDoc& operator=(const LottieTextDoc& rhs)
{
//shallow copy, used for slot overriding
if (other.frames) {
frames = other.frames;
const_cast<LottieTextDoc&>(other).frames = nullptr;
if (rhs.frames) {
frames = rhs.frames;
const_cast<LottieTextDoc&>(rhs).frames = nullptr;
} else {
value = other.value;
const_cast<LottieTextDoc&>(other).value.text = nullptr;
const_cast<LottieTextDoc&>(other).value.name = nullptr;
value = rhs.value;
const_cast<LottieTextDoc&>(rhs).value.text = nullptr;
const_cast<LottieTextDoc&>(rhs).value.name = nullptr;
}
return *this;
}
@ -839,6 +858,13 @@ struct LottieBitmap : LottieProperty
float width = 0.0f;
float height = 0.0f;
LottieBitmap() {}
LottieBitmap(const LottieBitmap& rhs)
{
*this = rhs;
}
~LottieBitmap()
{
release();
@ -857,18 +883,18 @@ struct LottieBitmap : LottieProperty
uint32_t nearest(float time) override { return 0; }
float frameNo(int32_t key) override { return 0; }
LottieBitmap& operator=(const LottieBitmap& other)
LottieBitmap& operator=(const LottieBitmap& rhs)
{
//shallow copy, used for slot overriding
if (other.mimeType) b64Data = other.b64Data;
else path = other.path;
mimeType = other.mimeType;
size = other.size;
width = other.width;
height = other.height;
if (rhs.mimeType) b64Data = rhs.b64Data;
else path = rhs.path;
mimeType = rhs.mimeType;
size = rhs.size;
width = rhs.width;
height = rhs.height;
const_cast<LottieBitmap&>(other).b64Data = nullptr;
const_cast<LottieBitmap&>(other).mimeType = nullptr;
const_cast<LottieBitmap&>(rhs).b64Data = nullptr;
const_cast<LottieBitmap&>(rhs).mimeType = nullptr;
return *this;
}
};