mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 05:33:36 +00:00
loader lottie: added empty interface body
This marks the first step towards implementing the Lottie feature.
This commit is contained in:
parent
7c58d7afaa
commit
60f81b7da7
8 changed files with 328 additions and 17 deletions
21
meson.build
21
meson.build
|
@ -49,6 +49,11 @@ if get_option('loaders').contains('webp_beta') == true
|
||||||
config_h.set10('THORVG_WEBP_LOADER_SUPPORT', true)
|
config_h.set10('THORVG_WEBP_LOADER_SUPPORT', true)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if get_option('loaders').contains('lottie_beta') == true
|
||||||
|
config_h.set10('THORVG_LOTTIE_LOADER_SUPPORT', true)
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
#Savers
|
#Savers
|
||||||
all_savers = false
|
all_savers = false
|
||||||
|
|
||||||
|
@ -119,13 +124,14 @@ Summary:
|
||||||
Loader (PNG): @8@
|
Loader (PNG): @8@
|
||||||
Loader (JPG): @9@
|
Loader (JPG): @9@
|
||||||
Loader (WEBP_BETA): @10@
|
Loader (WEBP_BETA): @10@
|
||||||
Saver (TVG): @11@
|
Loader (LOTTIE_BETA): @11@
|
||||||
CAPI Binding: @12@
|
Saver (TVG): @12@
|
||||||
Log Message: @13@
|
CAPI Binding: @13@
|
||||||
Tests: @14@
|
Log Message: @14@
|
||||||
Examples: @15@
|
Tests: @15@
|
||||||
Tool (Svg2Tvg): @16@
|
Examples: @16@
|
||||||
Tool (Svg2Png): @17@
|
Tool (Svg2Tvg): @17@
|
||||||
|
Tool (Svg2Png): @18@
|
||||||
|
|
||||||
'''.format(
|
'''.format(
|
||||||
meson.project_version(),
|
meson.project_version(),
|
||||||
|
@ -139,6 +145,7 @@ Summary:
|
||||||
all_loaders or get_option('loaders').contains('png'),
|
all_loaders or get_option('loaders').contains('png'),
|
||||||
all_loaders or get_option('loaders').contains('jpg'),
|
all_loaders or get_option('loaders').contains('jpg'),
|
||||||
get_option('loaders').contains('webp_beta'),
|
get_option('loaders').contains('webp_beta'),
|
||||||
|
get_option('loaders').contains('lottie_beta'),
|
||||||
all_savers or get_option('savers').contains('tvg'),
|
all_savers or get_option('savers').contains('tvg'),
|
||||||
get_option('bindings').contains('capi'),
|
get_option('bindings').contains('capi'),
|
||||||
get_option('log'),
|
get_option('log'),
|
||||||
|
|
|
@ -6,7 +6,7 @@ option('engines',
|
||||||
|
|
||||||
option('loaders',
|
option('loaders',
|
||||||
type: 'array',
|
type: 'array',
|
||||||
choices: ['', 'tvg', 'svg', 'png', 'jpg', 'webp_beta','all'],
|
choices: ['', 'tvg', 'svg', 'png', 'jpg', 'webp_beta', 'lottie_beta', 'all'],
|
||||||
value: ['svg', 'tvg'],
|
value: ['svg', 'tvg'],
|
||||||
description: 'Enable File Loaders in thorvg ("all" does not include "*_beta".)')
|
description: 'Enable File Loaders in thorvg ("all" does not include "*_beta".)')
|
||||||
|
|
||||||
|
|
9
src/loaders/lottie/meson.build
Normal file
9
src/loaders/lottie/meson.build
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
source_file = [
|
||||||
|
'tvgLottieLoader.h',
|
||||||
|
'tvgLottieLoader.cpp',
|
||||||
|
]
|
||||||
|
|
||||||
|
subloader_dep += [declare_dependency(
|
||||||
|
include_directories : include_directories('.'),
|
||||||
|
sources : source_file
|
||||||
|
)]
|
294
src/loaders/lottie/tvgLottieLoader.cpp
Normal file
294
src/loaders/lottie/tvgLottieLoader.cpp
Normal file
|
@ -0,0 +1,294 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 the ThorVG project. All rights reserved.
|
||||||
|
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include "tvgLoader.h"
|
||||||
|
#include "tvgLottieLoader.h"
|
||||||
|
|
||||||
|
/************************************************************************/
|
||||||
|
/* Internal Class Implementation */
|
||||||
|
/************************************************************************/
|
||||||
|
|
||||||
|
static bool _checkDotLottie(const char *str)
|
||||||
|
{
|
||||||
|
//check the .Lottie signature.
|
||||||
|
if (str[0] == 0x50 && str[1] == 0x4B && str[2] == 0x03 && str[3] == 0x04) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int _str2int(const char* str, int len)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
for(int i = 0; i < len; ++i) {
|
||||||
|
ret = ret * 10 + (str[i] - '0');
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int _str2float(const char* str, int len)
|
||||||
|
{
|
||||||
|
//strndup()
|
||||||
|
auto tmp = (char*)malloc(len + 1);
|
||||||
|
if (!tmp) return 0;
|
||||||
|
tmp[len] = '\0';
|
||||||
|
memcpy(tmp, str, len);
|
||||||
|
|
||||||
|
auto ret = strtof(tmp, nullptr);
|
||||||
|
free(tmp);
|
||||||
|
return lround(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LottieLoader::clear()
|
||||||
|
{
|
||||||
|
//TODO: Clear all used resources
|
||||||
|
if (copy) free((char*)content);
|
||||||
|
size = 0;
|
||||||
|
content = nullptr;
|
||||||
|
copy = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************************/
|
||||||
|
/* External Class Implementation */
|
||||||
|
/************************************************************************/
|
||||||
|
|
||||||
|
LottieLoader::LottieLoader()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LottieLoader::~LottieLoader()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LottieLoader::run(unsigned tid)
|
||||||
|
{
|
||||||
|
/* TODO: Compose current frame of Lottie Scene tree
|
||||||
|
The result should be assigned to "this->root" */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::header()
|
||||||
|
{
|
||||||
|
//Quickly validate the given Lottie file without parsing in order to get the animation info.
|
||||||
|
auto p = content;
|
||||||
|
|
||||||
|
uint32_t depth = 0;
|
||||||
|
|
||||||
|
while (*p != '\0') {
|
||||||
|
if (*p == '{') {
|
||||||
|
++depth;
|
||||||
|
++p;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (*p == '}') {
|
||||||
|
--depth;
|
||||||
|
++p;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (depth != 1) {
|
||||||
|
++p;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//version.
|
||||||
|
if (!strncmp(p, "\"v\":", 4)) {
|
||||||
|
p += 4;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//framerate
|
||||||
|
if (!strncmp(p, "\"fr\":", 5)) {
|
||||||
|
p += 5;
|
||||||
|
auto e = strstr(p, ",");
|
||||||
|
if (!e) e = strstr(p, "}");
|
||||||
|
frameRate = _str2float(p, e - p);
|
||||||
|
p = e;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//width
|
||||||
|
if (!strncmp(p, "\"w\":", 4)) {
|
||||||
|
p += 4;
|
||||||
|
auto e = strstr(p, ",");
|
||||||
|
if (!e) e = strstr(p, "}");
|
||||||
|
w = static_cast<float>(_str2int(p, e - p));
|
||||||
|
p = e;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//height
|
||||||
|
if (!strncmp(p, "\"h\":", 4)) {
|
||||||
|
p += 4;
|
||||||
|
auto e = strstr(p, ",");
|
||||||
|
if (!e) e = strstr(p, "}");
|
||||||
|
h = static_cast<float>(_str2int(p, e - p));
|
||||||
|
p = e;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
TVGLOG("LOTTIE", "info: fr = %d, size = %d x %d", frameRate, (int)w, (int)h);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::open(const char* data, uint32_t size, bool copy)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
|
||||||
|
//If the format is dotLottie
|
||||||
|
auto dotLottie = _checkDotLottie(data);
|
||||||
|
if (dotLottie) {
|
||||||
|
TVGLOG("LOTTIE", "Requested .Lottie Format, Not Supported yet.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (copy) {
|
||||||
|
content = (char*)malloc(size);
|
||||||
|
if (!content) return false;
|
||||||
|
memcpy((char*)content, data, size);
|
||||||
|
} else content = data;
|
||||||
|
|
||||||
|
this->size = size;
|
||||||
|
this->copy = copy;
|
||||||
|
|
||||||
|
return header();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::open(const string& path)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
|
||||||
|
auto f = fopen(path.c_str(), "r");
|
||||||
|
if (!f) return false;
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
|
||||||
|
size = ftell(f);
|
||||||
|
if (size == 0) {
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto content = (char*)(malloc(sizeof(char) * size + 1));
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
auto ret = fread(content, sizeof(char), size, f);
|
||||||
|
if (ret < size) {
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
content[size] = '\0';
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
//If the format is dotLottie
|
||||||
|
auto dotLottie = _checkDotLottie(content);
|
||||||
|
if (dotLottie) {
|
||||||
|
TVGLOG("LOTTIE", "Requested .Lottie Format, Not Supported yet.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->content = content;
|
||||||
|
this->copy = true;
|
||||||
|
|
||||||
|
return header();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::resize(Paint* paint, float w, float h)
|
||||||
|
{
|
||||||
|
if (!paint) return false;
|
||||||
|
|
||||||
|
auto sx = w / this->w;
|
||||||
|
auto sy = h / this->h;
|
||||||
|
Matrix m = {sx, 0, 0, 0, sy, 0, 0, 0, 1};
|
||||||
|
paint->transform(m);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::read()
|
||||||
|
{
|
||||||
|
if (!content || size == 0) return false;
|
||||||
|
|
||||||
|
//the loading has been already completed in header()
|
||||||
|
if (root) return true;
|
||||||
|
|
||||||
|
TaskScheduler::request(this);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::close()
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
clear();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unique_ptr<Paint> LottieLoader::paint()
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
return std::move(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool LottieLoader::frame(uint32_t frameNo)
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t LottieLoader::totalFrame()
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t LottieLoader::curFrame()
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
return frameNo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float LottieLoader::duration()
|
||||||
|
{
|
||||||
|
this->done();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -27,12 +27,13 @@
|
||||||
#include "tvgFrameModule.h"
|
#include "tvgFrameModule.h"
|
||||||
#include "tvgTaskScheduler.h"
|
#include "tvgTaskScheduler.h"
|
||||||
|
|
||||||
class LottieLoader : public LoadModule, public FrameModule, public Task
|
class LottieLoader : public FrameModule, public Task
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
string filePath; //lottie file path
|
|
||||||
const char* content = nullptr; //lottie file data
|
const char* content = nullptr; //lottie file data
|
||||||
uint32_t size = 0; //lottie data size
|
uint32_t size = 0; //lottie data size
|
||||||
|
uint32_t frameRate;
|
||||||
|
uint32_t frameNo = 0; //current frame number
|
||||||
|
|
||||||
unique_ptr<Scene> root; //current motion frame
|
unique_ptr<Scene> root; //current motion frame
|
||||||
|
|
||||||
|
@ -50,13 +51,11 @@ public:
|
||||||
bool close() override;
|
bool close() override;
|
||||||
unique_ptr<Paint> paint() override;
|
unique_ptr<Paint> paint() override;
|
||||||
|
|
||||||
bool animatable() override { return true; }
|
|
||||||
|
|
||||||
//Frame Controls
|
//Frame Controls
|
||||||
bool frame(uint32_t frameNo) override;
|
bool frame(uint32_t frameNo) override;
|
||||||
uint32_t totalFrame() override;
|
uint32_t totalFrame() override;
|
||||||
uint32_t curFrame() override;
|
uint32_t curFrame() override;
|
||||||
double duration() override;
|
float duration() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool header();
|
bool header();
|
||||||
|
|
|
@ -8,6 +8,10 @@ if all_loaders or get_option('loaders').contains('svg') == true
|
||||||
subdir('svg')
|
subdir('svg')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if get_option('loaders').contains('lottie_beta') == true
|
||||||
|
subdir('lottie')
|
||||||
|
endif
|
||||||
|
|
||||||
if all_loaders or get_option('loaders').contains('png') == true
|
if all_loaders or get_option('loaders').contains('png') == true
|
||||||
if get_option('static') == true
|
if get_option('static') == true
|
||||||
subdir('png')
|
subdir('png')
|
||||||
|
|
|
@ -3682,6 +3682,5 @@ bool SvgLoader::close()
|
||||||
unique_ptr<Paint> SvgLoader::paint()
|
unique_ptr<Paint> SvgLoader::paint()
|
||||||
{
|
{
|
||||||
this->done();
|
this->done();
|
||||||
if (root) return std::move(root);
|
return std::move(root);
|
||||||
else return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,6 +228,5 @@ void TvgLoader::run(unsigned tid)
|
||||||
unique_ptr<Paint> TvgLoader::paint()
|
unique_ptr<Paint> TvgLoader::paint()
|
||||||
{
|
{
|
||||||
this->done();
|
this->done();
|
||||||
if (root) return std::move(root);
|
return std::move(root);
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue