2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "rlottie_capi.h"
23 using namespace rlottie;
29 struct Lottie_Animation_S
31 std::unique_ptr<Animation> mAnimation;
32 std::future<Surface> mRenderTask;
34 LOTMarkerList *mMarkerList;
37 LOT_EXPORT Lottie_Animation_S *lottie_animation_from_file(const char *path)
39 if (auto animation = Animation::loadFromFile(path) ) {
40 Lottie_Animation_S *handle = new Lottie_Animation_S();
41 handle->mAnimation = std::move(animation);
48 LOT_EXPORT Lottie_Animation_S *lottie_animation_from_data(const char *data, const char *key, const char *resourcePath)
50 if (auto animation = Animation::loadFromData(data, key, resourcePath) ) {
51 Lottie_Animation_S *handle = new Lottie_Animation_S();
52 handle->mAnimation = std::move(animation);
59 LOT_EXPORT void lottie_animation_destroy(Lottie_Animation_S *animation)
62 if (animation->mMarkerList) {
63 for(size_t i = 0; i < animation->mMarkerList->size; i++) {
64 if (animation->mMarkerList->ptr[i].name) free(animation->mMarkerList->ptr[i].name);
66 delete[] animation->mMarkerList->ptr;
67 delete animation->mMarkerList;
70 if (animation->mRenderTask.valid()) {
71 animation->mRenderTask.get();
73 animation->mAnimation = nullptr;
78 LOT_EXPORT void lottie_animation_get_size(const Lottie_Animation_S *animation, size_t *width, size_t *height)
80 if (!animation) return;
82 animation->mAnimation->size(*width, *height);
85 LOT_EXPORT double lottie_animation_get_duration(const Lottie_Animation_S *animation)
87 if (!animation) return 0;
89 return animation->mAnimation->duration();
92 LOT_EXPORT size_t lottie_animation_get_totalframe(const Lottie_Animation_S *animation)
94 if (!animation) return 0;
96 return animation->mAnimation->totalFrame();
100 LOT_EXPORT double lottie_animation_get_framerate(const Lottie_Animation_S *animation)
102 if (!animation) return 0;
104 return animation->mAnimation->frameRate();
107 LOT_EXPORT const LOTLayerNode * lottie_animation_render_tree(Lottie_Animation_S *animation, size_t frame_num, size_t width, size_t height)
109 if (!animation) return nullptr;
111 return animation->mAnimation->renderTree(frame_num, width, height);
115 lottie_animation_get_frame_at_pos(const Lottie_Animation_S *animation, float pos)
117 if (!animation) return 0;
119 return animation->mAnimation->frameAtPos(pos);
123 lottie_animation_render(Lottie_Animation_S *animation,
128 size_t bytes_per_line)
130 if (!animation) return;
132 rlottie::Surface surface(buffer, width, height, bytes_per_line);
133 animation->mAnimation->renderSync(frame_number, surface);
137 lottie_animation_render_async(Lottie_Animation_S *animation,
142 size_t bytes_per_line)
144 if (!animation) return;
146 rlottie::Surface surface(buffer, width, height, bytes_per_line);
147 animation->mRenderTask = animation->mAnimation->render(frame_number, surface);
148 animation->mBufferRef = buffer;
151 LOT_EXPORT uint32_t *
152 lottie_animation_render_flush(Lottie_Animation_S *animation)
154 if (!animation) return nullptr;
156 if (animation->mRenderTask.valid()) {
157 animation->mRenderTask.get();
160 return animation->mBufferRef;
164 lottie_animation_property_override(Lottie_Animation_S *animation,
165 const Lottie_Animation_Property type,
170 va_start(prop, keypath);
171 const int arg_count = [type](){
173 case LOTTIE_ANIMATION_PROPERTY_FILLCOLOR:
174 case LOTTIE_ANIMATION_PROPERTY_STROKECOLOR:
176 case LOTTIE_ANIMATION_PROPERTY_FILLOPACITY:
177 case LOTTIE_ANIMATION_PROPERTY_STROKEOPACITY:
178 case LOTTIE_ANIMATION_PROPERTY_STROKEWIDTH:
179 case LOTTIE_ANIMATION_PROPERTY_TR_ROTATION:
181 case LOTTIE_ANIMATION_PROPERTY_TR_POSITION:
182 case LOTTIE_ANIMATION_PROPERTY_TR_SCALE:
189 for (int i = 0; i < arg_count ; i++) {
190 v[i] = va_arg(prop, double);
195 case LOTTIE_ANIMATION_PROPERTY_FILLCOLOR: {
199 if (r > 1 || r < 0 || g > 1 || g < 0 || b > 1 || b < 0) break;
200 animation->mAnimation->setValue<rlottie::Property::FillColor>(keypath, rlottie::Color(r, g, b));
203 case LOTTIE_ANIMATION_PROPERTY_FILLOPACITY: {
204 double opacity = v[0];
205 if (opacity > 100 || opacity < 0) break;
206 animation->mAnimation->setValue<rlottie::Property::FillOpacity>(keypath, (float)opacity);
209 case LOTTIE_ANIMATION_PROPERTY_STROKECOLOR: {
213 if (r > 1 || r < 0 || g > 1 || g < 0 || b > 1 || b < 0) break;
214 animation->mAnimation->setValue<rlottie::Property::StrokeColor>(keypath, rlottie::Color(r, g, b));
217 case LOTTIE_ANIMATION_PROPERTY_STROKEOPACITY: {
218 double opacity = v[0];
219 if (opacity > 100 || opacity < 0) break;
220 animation->mAnimation->setValue<rlottie::Property::StrokeOpacity>(keypath, (float)opacity);
223 case LOTTIE_ANIMATION_PROPERTY_STROKEWIDTH: {
225 if (width < 0) break;
226 animation->mAnimation->setValue<rlottie::Property::StrokeWidth>(keypath, (float)width);
229 case LOTTIE_ANIMATION_PROPERTY_TR_POSITION: {
232 animation->mAnimation->setValue<rlottie::Property::TrPosition>(keypath, rlottie::Point((float)x, (float)y));
235 case LOTTIE_ANIMATION_PROPERTY_TR_SCALE: {
238 animation->mAnimation->setValue<rlottie::Property::TrScale>(keypath, rlottie::Size((float)w, (float)h));
241 case LOTTIE_ANIMATION_PROPERTY_TR_ROTATION: {
243 animation->mAnimation->setValue<rlottie::Property::TrRotation>(keypath, (float)r);
246 case LOTTIE_ANIMATION_PROPERTY_TR_ANCHOR:
247 case LOTTIE_ANIMATION_PROPERTY_TR_OPACITY:
248 //@TODO handle propery update.
253 LOT_EXPORT const LOTMarkerList*
254 lottie_animation_get_markerlist(Lottie_Animation_S *animation)
256 if (!animation) return nullptr;
258 auto markers = animation->mAnimation->markers();
259 if (markers.size() == 0) return nullptr;
260 if (animation->mMarkerList) return (const LOTMarkerList*)animation->mMarkerList;
262 animation->mMarkerList = new LOTMarkerList();
263 animation->mMarkerList->size = markers.size();
264 animation->mMarkerList->ptr = new LOTMarker[markers.size()]();
266 for(size_t i = 0; i < markers.size(); i++) {
267 animation->mMarkerList->ptr[i].name = strdup(std::get<0>(markers[i]).c_str());
268 animation->mMarkerList->ptr[i].startframe= std::get<1>(markers[i]);
269 animation->mMarkerList->ptr[i].endframe= std::get<2>(markers[i]);
271 return (const LOTMarkerList*)animation->mMarkerList;