Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions modules/svg/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ thirdparty_obj = []
thirdparty_dir = "#thirdparty/thorvg/"
thirdparty_sources = [
# common
"src/common/tvgBezier.cpp",
"src/common/tvgCompressor.cpp",
"src/common/tvgLines.cpp",
"src/common/tvgMath.cpp",
"src/common/tvgStr.cpp",
# SVG parser
Expand All @@ -27,9 +27,16 @@ thirdparty_sources = [
"src/loaders/external_png/tvgPngLoader.cpp",
"src/loaders/jpg/tvgJpgd.cpp",
"src/loaders/jpg/tvgJpgLoader.cpp",
"src/loaders/lottie/tvgLottieAnimation.cpp",
"src/loaders/lottie/tvgLottieBuilder.cpp",
"src/loaders/lottie/tvgLottieInterpolator.cpp",
"src/loaders/lottie/tvgLottieLoader.cpp",
"src/loaders/lottie/tvgLottieModel.cpp",
"src/loaders/lottie/tvgLottieParser.cpp",
"src/loaders/lottie/tvgLottieParserHandler.cpp",
# renderer common
"src/renderer/tvgAccessor.cpp",
# "src/renderer/tvgAnimation.cpp",
"src/renderer/tvgAnimation.cpp",
"src/renderer/tvgCanvas.cpp",
"src/renderer/tvgFill.cpp",
# "src/renderer/tvgGlCanvas.cpp",
Expand Down Expand Up @@ -75,6 +82,7 @@ env_thirdparty.Prepend(
thirdparty_dir + "src/loaders/raw",
thirdparty_dir + "src/loaders/external_png",
thirdparty_dir + "src/loaders/jpg",
thirdparty_dir + "src/loaders/lottie",
]
)

Expand Down
185 changes: 185 additions & 0 deletions modules/svg/lottie_sheet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/**************************************************************************/
/* lottie_sheet.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 "lottie_sheet.h"

#include "core/os/memory.h"
#include "core/variant/variant.h"

#include <thorvg.h>

void LottieSheet::_load_data(String p_string, float p_scale) {
ERR_FAIL_COND_MSG(Math::is_zero_approx(p_scale), "LottieSheet: Can't load Lottie with a scale of 0.");

tvg::Result result = picture->load(p_string.utf8(), p_string.utf8().size(), "lottie", true);
if (result != tvg::Result::Success) {
return;
}
float fw, fh;
picture->size(&fw, &fh);

uint32_t width = MAX(1, round(fw * p_scale));
uint32_t height = MAX(1, round(fh * p_scale));

const uint32_t max_dimension = 16384;
if (width > max_dimension || height > max_dimension) {
WARN_PRINT(vformat(
String::utf8("LottieSheet: Target canvas dimensions %d×%d (with scale %.2f) exceed the max supported dimensions %d×%d. The target canvas will be scaled down."),
width, height, p_scale, max_dimension, max_dimension));
width = MIN(width, max_dimension);
height = MIN(height, max_dimension);
}

picture->size(width, height);
this->width = width;
this->height = height;
image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);
// Note: memalloc here, be sure to memfree before any return.
buffer = (uint32_t *)(buffer == nullptr ? memalloc(sizeof(uint32_t) * width * height) : memrealloc(buffer, sizeof(uint32_t) * width * height));
}

Ref<LottieSheet> LottieSheet::load_json(Ref<JSON> p_json, float p_scale) {
String data = p_json->get_parsed_text();
if (data.is_empty()) {
data = p_json->to_string();
}
Ref<LottieSheet> ret = memnew(LottieSheet);
ret->_load_data(data, p_scale);
ret->json = p_json;
return ret;
}

Ref<LottieSheet> LottieSheet::load_string(String p_string, float p_scale) {
Ref<LottieSheet> ret = memnew(LottieSheet);
ret->_load_data(p_string, p_scale);
ret->json->parse(p_string, true);
return ret;
}

void LottieSheet::update_frame(float frame) {
tvg::Result res = animation->frame(frame);
if (res == tvg::Result::Success) {
sw_canvas->update(picture);
}

res = sw_canvas->target(buffer, width, width, height, tvg::SwCanvas::ARGB8888S);
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieSheet: Couldn't set target on ThorVG canvas.");
}

res = sw_canvas->push(tvg::cast(picture));
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieSheet: Couldn't insert ThorVG picture on canvas.");
}

res = sw_canvas->draw();
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieSheet: Couldn't draw ThorVG pictures on canvas.");
}

res = sw_canvas->sync();
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieSheet: Couldn't sync ThorVG canvas.");
}

Vector<uint8_t> image_data;
image_data.resize(width * height * sizeof(uint32_t));

for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
uint32_t n = buffer[y * width + x];
const size_t offset = sizeof(uint32_t) * width * y + sizeof(uint32_t) * x;
image_data.write[offset + 0] = (n >> 16) & 0xff;
image_data.write[offset + 1] = (n >> 8) & 0xff;
image_data.write[offset + 2] = n & 0xff;
image_data.write[offset + 3] = (n >> 24) & 0xff;
}
}

res = sw_canvas->clear(true);

image->set_data(width, height, false, Image::FORMAT_RGBA8, image_data);
}

Ref<Image> LottieSheet::get_image() { return image; };

Ref<Image> LottieSheet::get_frame_image(float frame) {
update_frame(frame);
return image;
};

Vector2i LottieSheet::get_image_size() {
return Vector2i(width, height);
}

float LottieSheet::get_total_frame() { return animation->totalFrame(); };

float LottieSheet::get_duration() { return animation->duration(); };

Ref<JSON> LottieSheet::get_json() { return json; }

void LottieSheet::set_json(Ref<JSON> p_json) {
String data = p_json->get_parsed_text();
if (data.is_empty()) {
data = p_json->to_string();
}
_load_data(data, scale);
json = p_json;
}

float LottieSheet::get_scale() { return scale; };
void LottieSheet::set_scale(float p_scale) {
String data = json->get_parsed_text();
if (data.is_empty()) {
data = json->to_string();
}
_load_data(data, scale);
scale = p_scale;
};

LottieSheet::~LottieSheet() { memfree(buffer); }

void LottieSheet::_bind_methods() {
ClassDB::bind_static_method("LottieSheet", D_METHOD("load_string", "p_string", "p_scale"), &LottieSheet::load_string, DEFVAL(1));
ClassDB::bind_static_method("LottieSheet", D_METHOD("load_json", "p_json", "p_scale"), &LottieSheet::load_json, DEFVAL(1));
ClassDB::bind_method(D_METHOD("get_json"), &LottieSheet::get_json);
ClassDB::bind_method(D_METHOD("set_json", "p_json"), &LottieSheet::set_json);
ClassDB::bind_method(D_METHOD("get_scale"), &LottieSheet::get_scale);
ClassDB::bind_method(D_METHOD("set_scale", "p_scale"), &LottieSheet::set_scale);
ClassDB::bind_method(D_METHOD("update_frame", "frame"), &LottieSheet::update_frame);
ClassDB::bind_method(D_METHOD("get_image"), &LottieSheet::get_image);
ClassDB::bind_method(D_METHOD("get_frame_image", "frame"), &LottieSheet::get_frame_image);
ClassDB::bind_method(D_METHOD("get_image_size"), &LottieSheet::get_image_size);
ClassDB::bind_method(D_METHOD("get_total_frame"), &LottieSheet::get_total_frame);
ClassDB::bind_method(D_METHOD("get_duration"), &LottieSheet::get_duration);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "json"), "set_json", "get_json");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scale"), "set_scale", "get_scale");
}
74 changes: 74 additions & 0 deletions modules/svg/lottie_sheet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**************************************************************************/
/* lottie_sheet.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/

#ifndef LOTTIE_SHEET_H
#define LOTTIE_SHEET_H

#include "core/io/json.h"
#include "core/io/resource.h"

#include <thorvg.h>

class LottieSheet : public Resource {
GDCLASS(LottieSheet, Resource);

std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
std::unique_ptr<tvg::Animation> animation = tvg::Animation::gen();
tvg::Picture *picture = animation->picture();
Ref<Image> image;
uint32_t *buffer = nullptr;
Ref<JSON> json = memnew(JSON);

float scale;
uint32_t width, height;

void _load_data(String p_string, float p_scale);

protected:
static void _bind_methods();

public:
static Ref<LottieSheet> load_string(String p_string, float p_scale = 1);
static Ref<LottieSheet> load_json(Ref<JSON> p_json, float p_scale = 1);

Ref<JSON> get_json();
void set_json(Ref<JSON> p_json);
float get_scale();
void set_scale(float p_scale);
void update_frame(float frame);
Ref<Image> get_image();
Ref<Image> get_frame_image(float frame);
Vector2i get_image_size();
float get_total_frame();
float get_duration();
~LottieSheet();
};

#endif // LOTTIE_SHEET_H
2 changes: 2 additions & 0 deletions modules/svg/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "register_types.h"

#include "image_loader_svg.h"
#include "lottie_sheet.h"

#include <thorvg.h>

Expand All @@ -55,6 +56,7 @@ void initialize_svg_module(ModuleInitializationLevel p_level) {

image_loader_svg.instantiate();
ImageLoader::add_image_format_loader(image_loader_svg);
ClassDB::register_class<LottieSheet>();
}

void uninitialize_svg_module(ModuleInitializationLevel p_level) {
Expand Down
8 changes: 5 additions & 3 deletions thirdparty/thorvg/AUTHORS
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Hermet Park <hermet@lottiefiles.com>
Hermet Park <hermet@lottiefiles.com>, <chuneon.park@samsung.com>
Prudhvi Raj Vasireddi <prudhvi.raj@samsung.com>
Junsu Choi <jsuya.choi@samsung.com>
Pranay Samanta <pranay.ks@samsung.com>
Mateusz Palkowski <m.palkowski@samsung.com>
Subhransu Mohanty <sub.mohanty@samsung.com>
Mira Grudzinska <veleveta@gmail.com>
Mira Grudzinska <veleveta@gmail.com>, <m.grudzinska@samsung.com>
Michal Szczecinski <m.szczecinsk@partner.samsung.com>
Shinwoo Kim <cinoo.kim@samsung.com>
Piotr Kalota <p.kalota@samsung.com>
Expand All @@ -18,10 +18,12 @@ Rémi Verschelde <rverschelde@gmail.com>
Martin Liska <mliksa@suse.cz>
Vincenzo Pupillo <vincenzo.pupillo@unimi.it>
EunSik Jeong <rinechran@outlook.jp>
Samsung Electronics Co., Ltd
Rafał Mikrut <mikrutrafal@protonmail.com>
Martin Capitanio <capnm@capitanio.org>
RuiwenTang <tangruiwen1989@gmail.com>
YouJin Lee <ol-of@naver.com>
SergeyLebedkin <sergii@lottiefiles.com>
Jinny You <jinny@lottiefiles.com>
Nattu Adnan <nattu@reallynattu.com>
Gabor Kiss-Vamosi <kisvegabor@gmail.com>
Lorcán Mc Donagh <lorcan@lmdsp.com>
3 changes: 2 additions & 1 deletion thirdparty/thorvg/inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#define THORVG_SVG_LOADER_SUPPORT
#define THORVG_PNG_LOADER_SUPPORT
#define THORVG_JPG_LOADER_SUPPORT
#define THORVG_LOTTIE_LOADER_SUPPORT
#define THORVG_THREAD_SUPPORT

// For internal debugging:
//#define THORVG_LOG_ENABLED

#define THORVG_VERSION_STRING "0.12.10"
#define THORVG_VERSION_STRING "0.13.2"
#endif
Loading