lottie/loader: support external image from memeory lottie

I've added new parameter, const string& resourcePath, to load external image on lottie.

Result load(const char* data, uint32_t size, const string& mimeType, bool copy, const string& resourcePath)

Note: tvgLoadModule will have new overrided method `open`, not to effect to other changes except animation.

Issue: #1793
This commit is contained in:
Jinny You 2023-11-22 17:47:57 +09:00 committed by Hermet Park
parent ba2a2f02cf
commit 132be91de4
23 changed files with 32 additions and 27 deletions

View file

@ -1236,6 +1236,7 @@ public:
* @param[in] size The size in bytes of the memory occupied by the @p data.
* @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "lottie", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one.
* @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not.
* @param[in] resourcePath Directory path to load external images.
*
* @retval Result::Success When succeed.
* @retval Result::InvalidArguments In case no data are provided or the @p size is zero or less.
@ -1247,7 +1248,7 @@ public:
* @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out.
* @since 0.5
*/
Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept;
Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false, const std::string& resourcePath = "") noexcept;
/**
* @brief Resizes the picture content to the given width and height.

View file

@ -100,7 +100,7 @@ finalize:
}
bool JpgLoader::open(const char* data, uint32_t size, bool copy)
bool JpgLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
clear();

View file

@ -36,7 +36,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;

View file

@ -60,7 +60,7 @@ bool PngLoader::open(const string& path)
return true;
}
bool PngLoader::open(const char* data, uint32_t size, bool copy)
bool PngLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
image->opaque = NULL;

View file

@ -34,7 +34,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;

View file

@ -107,7 +107,7 @@ finalize:
}
bool WebpLoader::open(const char* data, uint32_t size, bool copy)
bool WebpLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
clear();

View file

@ -34,7 +34,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;

View file

@ -76,7 +76,7 @@ bool JpgLoader::open(const string& path)
}
bool JpgLoader::open(const char* data, uint32_t size, bool copy)
bool JpgLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
clear();

View file

@ -43,7 +43,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;

View file

@ -206,7 +206,7 @@ bool LottieLoader::header()
}
bool LottieLoader::open(const char* data, uint32_t size, bool copy)
bool LottieLoader::open(const char* data, uint32_t size, bool copy, const std::string& resourcePath)
{
clear();
@ -226,6 +226,10 @@ bool LottieLoader::open(const char* data, uint32_t size, bool copy)
this->size = size;
this->copy = copy;
if (!resourcePath.empty()) {
this->dirName = strdup(resourcePath.c_str());
}
return header();
}

View file

@ -51,7 +51,7 @@ public:
//Lottie Loaders
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const std::string& resourcePath) override;
bool resize(Paint* paint, float w, float h) override;
bool read() override;
bool close() override;

View file

@ -867,9 +867,9 @@ LottieImage* LottieParser::parseImage(const char* key)
image->size = b64Decode(b64Data, length, &image->b64Data);
//external image resource
} else {
auto len = strlen(dirName) + strlen(subPath) + strlen(data) + 1;
auto len = strlen(dirName) + strlen(subPath) + strlen(data) + 2;
image->path = static_cast<char*>(malloc(len));
snprintf(image->path, len, "%s%s%s", dirName, subPath, data);
snprintf(image->path, len, "%s/%s%s", dirName, subPath, data);
}
image->prepare();

View file

@ -117,7 +117,7 @@ finalize:
}
bool PngLoader::open(const char* data, uint32_t size, bool copy)
bool PngLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
clear();

View file

@ -45,7 +45,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;

View file

@ -3671,7 +3671,7 @@ bool SvgLoader::header()
}
bool SvgLoader::open(const char* data, uint32_t size, bool copy)
bool SvgLoader::open(const char* data, uint32_t size, bool copy, const string& resourcePath)
{
clear();

View file

@ -44,7 +44,7 @@ public:
using LoadModule::open;
bool open(const string& path) override;
bool open(const char* data, uint32_t size, bool copy) override;
bool open(const char* data, uint32_t size, bool copy, const string& resourcePath) override;
bool resize(Paint* paint, float w, float h) override;
bool read() override;
bool close() override;

View file

@ -148,7 +148,7 @@ bool TvgLoader::open(const string &path)
}
bool TvgLoader::open(const char *data, uint32_t size, bool copy)
bool TvgLoader::open(const char *data, uint32_t size, bool copy,const string& resourcePath)
{
clear();

View file

@ -46,7 +46,7 @@ public:
using LoadModule::open;
bool open(const string &path) override;
bool open(const char *data, uint32_t size, bool copy) override;
bool open(const char *data, uint32_t size, bool copy, const string& resourcePath) override;
bool read() override;
bool close() override;
bool resize(Paint* paint, float w, float h) override;

View file

@ -37,7 +37,7 @@ public:
virtual ~LoadModule() {}
virtual bool open(const string& path) { return false; }
virtual bool open(const char* data, uint32_t size, bool copy) { return false; }
virtual bool open(const char* data, uint32_t size, bool copy, const string& resourcePath = "") { return false; }
//Override this if the vector-format has own resizing policy.
virtual bool resize(Paint* paint, float w, float h) { return false; }

View file

@ -212,12 +212,12 @@ shared_ptr<LoadModule> LoaderMgr::loader(const string& path, bool* invalid)
}
shared_ptr<LoadModule> LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy)
shared_ptr<LoadModule> LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy, const string& resourcePath)
{
//Try with the given MimeType
if (!mimeType.empty()) {
if (auto loader = _findByType(mimeType)) {
if (loader->open(data, size, copy)) {
if (loader->open(data, size, copy, resourcePath)) {
return shared_ptr<LoadModule>(loader);
} else {
TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported.", mimeType.c_str());

View file

@ -30,7 +30,7 @@ struct LoaderMgr
static bool init();
static bool term();
static shared_ptr<LoadModule> loader(const string& path, bool* invalid);
static shared_ptr<LoadModule> loader(const char* data, uint32_t size, const string& mimeType, bool copy);
static shared_ptr<LoadModule> loader(const char* data, uint32_t size, const string& mimeType, bool copy, const string& resourcePath);
static shared_ptr<LoadModule> loader(const uint32_t* data, uint32_t w, uint32_t h, bool premultiplied, bool copy);
};

View file

@ -156,11 +156,11 @@ Result Picture::load(const std::string& path) noexcept
}
Result Picture::load(const char* data, uint32_t size, const string& mimeType, bool copy) noexcept
Result Picture::load(const char* data, uint32_t size, const string& mimeType, bool copy, const string& resourcePath) noexcept
{
if (!data || size <= 0) return Result::InvalidArguments;
return pImpl->load(data, size, mimeType, copy);
return pImpl->load(data, size, mimeType, copy, resourcePath);
}

View file

@ -162,11 +162,11 @@ struct Picture::Impl
return Result::Success;
}
Result load(const char* data, uint32_t size, const string& mimeType, bool copy)
Result load(const char* data, uint32_t size, const string& mimeType, bool copy, const string& resourcePath)
{
if (paint || surface) return Result::InsufficientCondition;
if (loader) loader->close();
loader = LoaderMgr::loader(data, size, mimeType, copy);
loader = LoaderMgr::loader(data, size, mimeType, copy, resourcePath);
if (!loader) return Result::NonSupport;
if (!loader->read()) return Result::Unknown;
w = loader->w;