lottie: add support for image size

The width and height of the image must
be specified in the Lottie file and must
be taken into account during rendering.
This commit is contained in:
Mira Grudzinska 2024-07-08 13:23:14 +02:00 committed by Hermet Park
parent 329ab9ef4a
commit 423e37d4d5
4 changed files with 12 additions and 3 deletions

View file

@ -1042,6 +1042,7 @@ static void _updateImage(LottieGroup* layer)
TaskScheduler::async(true); TaskScheduler::async(true);
PP(image->picture)->ref(); PP(image->picture)->ref();
image->picture->size(image->width, image->height);
} }
if (image->refCnt == 1) layer->scene->push(tvg::cast(image->picture)); if (image->refCnt == 1) layer->scene->push(tvg::cast(image->picture));

View file

@ -458,6 +458,8 @@ struct LottieImage : LottieObject
char* mimeType = nullptr; char* mimeType = nullptr;
uint32_t size = 0; uint32_t size = 0;
uint16_t refCnt = 0; //refernce count uint16_t refCnt = 0; //refernce count
float width = 0.0f;
float height = 0.0f;
~LottieImage(); ~LottieImage();

View file

@ -899,7 +899,7 @@ void LottieParser::parseObject(Array<LottieObject*>& parent)
} }
LottieImage* LottieParser::parseImage(const char* data, const char* subPath, bool embedded) LottieImage* LottieParser::parseImage(const char* data, const char* subPath, bool embedded, float width, float height)
{ {
//Used for Image Asset //Used for Image Asset
auto image = new LottieImage; auto image = new LottieImage;
@ -922,6 +922,8 @@ LottieImage* LottieParser::parseImage(const char* data, const char* subPath, boo
snprintf(image->path, len, "%s/%s%s", dirName, subPath, data); snprintf(image->path, len, "%s/%s%s", dirName, subPath, data);
} }
image->width = width;
image->height = height;
image->prepare(); image->prepare();
return image; return image;
@ -938,6 +940,8 @@ LottieObject* LottieParser::parseAsset()
//Used for Image Asset //Used for Image Asset
const char* data = nullptr; const char* data = nullptr;
const char* subPath = nullptr; const char* subPath = nullptr;
float width = 0.0f;
float height = 0.0f;
auto embedded = false; auto embedded = false;
while (auto key = nextObjectKey()) { while (auto key = nextObjectKey()) {
@ -952,10 +956,12 @@ LottieObject* LottieParser::parseAsset()
else if (KEY_AS("layers")) obj = parseLayers(); else if (KEY_AS("layers")) obj = parseLayers();
else if (KEY_AS("u")) subPath = getString(); else if (KEY_AS("u")) subPath = getString();
else if (KEY_AS("p")) data = getString(); else if (KEY_AS("p")) data = getString();
else if (KEY_AS("w")) width = getFloat();
else if (KEY_AS("h")) height = getFloat();
else if (KEY_AS("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, width, height);
if (obj) obj->id = id; if (obj) obj->id = id;
return obj; return obj;
} }

View file

@ -74,7 +74,7 @@ private:
LottieObject* parseObject(); LottieObject* parseObject();
LottieObject* parseAsset(); LottieObject* parseAsset();
LottieImage* parseImage(const char* data, const char* subPath, bool embedded); LottieImage* parseImage(const char* data, const char* subPath, bool embedded, float width, float height);
LottieLayer* parseLayer(); LottieLayer* parseLayer();
LottieObject* parseGroup(); LottieObject* parseGroup();
LottieRect* parseRect(); LottieRect* parseRect();