2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #if defined _WIN32 || defined __CYGWIN__
32 #define RLOTTIE_API __declspec(dllexport)
34 #define RLOTTIE_API __declspec(dllimport)
38 #define RLOTTIE_API __attribute__ ((visibility ("default")))
51 * @brief Configures rlottie model cache policy.
53 * Provides Library level control to configure model cache
54 * policy. Setting it to 0 will disable
55 * the cache as well as flush all the previously cached content.
57 * @param[in] cacheSize Maximum Model Cache size.
59 * @note to disable Caching configure with 0 size.
60 * @note to flush the current Cache content configure it with 0 and
61 * then reconfigure with the new size.
65 RLOTTIE_API void configureModelCacheSize(size_t cacheSize);
69 Color(float r, float g , float b):_r(r), _g(g), _b(b){}
70 float r() const {return _r;}
71 float g() const {return _g;}
72 float b() const {return _b;}
81 Size(float w, float h):_w(w), _h(h){}
82 float w() const {return _w;}
83 float h() const {return _h;}
91 Point(float x, float y):_x(x), _y(y){}
92 float x() const {return _x;}
93 float y() const {return _y;}
100 explicit FrameInfo(uint32_t frame): _frameNo(frame){}
101 uint32_t curFrame() const {return _frameNo;}
106 enum class Property {
107 FillColor, /*!< Color property of Fill object , value type is rlottie::Color */
108 FillOpacity, /*!< Opacity property of Fill object , value type is float [ 0 .. 100] */
109 StrokeColor, /*!< Color property of Stroke object , value type is rlottie::Color */
110 StrokeOpacity, /*!< Opacity property of Stroke object , value type is float [ 0 .. 100] */
111 StrokeWidth, /*!< stroke with property of Stroke object , value type is float */
112 TrAnchor, /*!< Transform Anchor property of Layer and Group object , value type is rlottie::Point */
113 TrPosition, /*!< Transform Position property of Layer and Group object , value type is rlottie::Point */
114 TrScale, /*!< Transform Scale property of Layer and Group object , value type is rlottie::Size. range[0 ..100] */
115 TrRotation, /*!< Transform Scale property of Layer and Group object , value type is float. range[0 .. 360] in degrees*/
116 TrOpacity /*!< Transform Opacity property of Layer and Group object , value type is float [ 0 .. 100] */
123 template <typename T> struct MapType;
125 class RLOTTIE_API Surface {
128 * @brief Surface object constructor.
130 * @param[in] buffer surface buffer.
131 * @param[in] width surface width.
132 * @param[in] height surface height.
133 * @param[in] bytesPerLine number of bytes in a surface scanline.
135 * @note Default surface format is ARGB32_Premultiplied.
139 Surface(uint32_t *buffer, size_t width, size_t height, size_t bytesPerLine);
142 * @brief Sets the Draw Area available on the Surface.
144 * Lottie will use the draw region size to generate frame image
145 * and will update only the draw rgion of the surface.
147 * @param[in] x region area x position.
148 * @param[in] y region area y position.
149 * @param[in] width region area width.
150 * @param[in] height region area height.
152 * @note Default surface format is ARGB32_Premultiplied.
153 * @note Default draw region area is [ 0 , 0, surface width , surface height]
157 void setDrawRegion(size_t x, size_t y, size_t width, size_t height);
160 * @brief Returns width of the surface.
162 * @return surface width
167 size_t width() const {return mWidth;}
170 * @brief Returns height of the surface.
172 * @return surface height
176 size_t height() const {return mHeight;}
179 * @brief Returns number of bytes in the surface scanline.
181 * @return number of bytes in scanline.
185 size_t bytesPerLine() const {return mBytesPerLine;}
188 * @brief Returns buffer attached tp the surface.
190 * @return buffer attaced to the Surface.
194 uint32_t *buffer() const {return mBuffer;}
197 * @brief Returns drawable area width of the surface.
199 * @return drawable area width
201 * @note Default value is width() of the surface
206 size_t drawRegionWidth() const {return mDrawArea.w;}
209 * @brief Returns drawable area height of the surface.
211 * @return drawable area height
213 * @note Default value is height() of the surface
217 size_t drawRegionHeight() const {return mDrawArea.h;}
220 * @brief Returns drawable area's x position of the surface.
222 * @return drawable area's x potition.
224 * @note Default value is 0
228 size_t drawRegionPosX() const {return mDrawArea.x;}
231 * @brief Returns drawable area's y position of the surface.
233 * @return drawable area's y potition.
235 * @note Default value is 0
239 size_t drawRegionPosY() const {return mDrawArea.y;}
242 * @brief Default constructor.
246 uint32_t *mBuffer{nullptr};
249 size_t mBytesPerLine{0};
258 using MarkerList = std::vector<std::tuple<std::string, int , int>>;
260 * @brief https://helpx.adobe.com/after-effects/using/layer-markers-composition-markers.html
261 * Markers exported form AE are used to describe a segmnet of an animation {comment/tag , startFrame, endFrame}
262 * Marker can be use to devide a resource in to separate animations by tagging the segment with comment string ,
263 * start frame and duration of that segment.
266 using LayerInfoList = std::vector<std::tuple<std::string, int , int>>;
269 using ColorFilter = std::function<void(float &r , float &g, float &b)>;
271 class RLOTTIE_API Animation {
275 * @brief Constructs an animation object from file path.
277 * @param[in] path Lottie resource file path
278 * @param[in] cachePolicy whether to cache or not the model data.
279 * use only when need to explicit disabl caching for a
280 * particular resource. To disable caching at library level
281 * use @see configureModelCacheSize() instead.
283 * @return Animation object that can render the contents of the
284 * Lottie resource represented by file path.
288 static std::unique_ptr<Animation>
289 loadFromFile(const std::string &path, bool cachePolicy=true);
292 * @brief Constructs an animation object from JSON string data.
294 * @param[in] jsonData The JSON string data.
295 * @param[in] key the string that will be used to cache the JSON string data.
296 * @param[in] resourcePath the path will be used to search for external resource.
297 * @param[in] cachePolicy whether to cache or not the model data.
298 * use only when need to explicit disabl caching for a
299 * particular resource. To disable caching at library level
300 * use @see configureModelCacheSize() instead.
302 * @return Animation object that can render the contents of the
303 * Lottie resource represented by JSON string data.
307 static std::unique_ptr<Animation>
308 loadFromData(std::string jsonData, const std::string &key,
309 const std::string &resourcePath="", bool cachePolicy=true);
312 * @brief Constructs an animation object from JSON string data and update.
313 * the color properties using ColorFilter.
315 * @param[in] jsonData The JSON string data.
316 * @param[in] resourcePath the path will be used to search for external resource.
317 * @param[in] filter The color filter that will be applied for each color property
318 * found during parsing.
320 * @return Animation object that can render the contents of the
321 * Lottie resource represented by JSON string data.
325 static std::unique_ptr<Animation>
326 loadFromData(std::string jsonData, std::string resourcePath, ColorFilter filter);
329 * @brief Returns default framerate of the Lottie resource.
331 * @return framerate of the Lottie resource
336 double frameRate() const;
339 * @brief Returns total number of frames present in the Lottie resource.
341 * @return frame count of the Lottie resource.
343 * @note frame number starts with 0.
347 size_t totalFrame() const;
350 * @brief Returns default viewport size of the Lottie resource.
352 * @param[out] width default width of the viewport.
353 * @param[out] height default height of the viewport.
358 void size(size_t &width, size_t &height) const;
361 * @brief Returns total animation duration of Lottie resource in second.
362 * it uses totalFrame() and frameRate() to calculate the duration.
363 * duration = totalFrame() / frameRate().
365 * @return total animation duration in second.
366 * @retval 0 if the Lottie resource has no animation.
373 double duration() const;
376 * @brief Returns frame number for a given position.
377 * this function helps to map the position value retuned
378 * by the animator to a frame number in side the Lottie resource.
379 * frame_number = lerp(start_frame, endframe, pos);
381 * @param[in] pos normalized position value [0 ... 1]
383 * @return frame numer maps to the position value [startFrame .... endFrame]
387 size_t frameAtPos(double pos);
390 * @brief Renders the content to surface Asynchronously.
391 * it gives a future in return to get the result of the
392 * rendering at a future point.
393 * To get best performance user has to start rendering as soon as
394 * it finds that content at {frameNo} has to be rendered and get the
395 * result from the future at the last moment when the surface is needed
396 * to draw into the screen.
399 * @param[in] frameNo Content corresponds to the @p frameNo needs to be drawn
400 * @param[in] surface Surface in which content will be drawn
401 * @param[in] keepAspectRatio whether to keep the aspect ratio while scaling the content.
403 * @return future that will hold the result when rendering finished.
405 * for Synchronus rendering @see renderSync
410 std::future<Surface> render(size_t frameNo, Surface surface, bool keepAspectRatio=true);
413 * @brief Renders the content to surface synchronously.
414 * for performance use the async rendering @see render
416 * @param[in] frameNo Content corresponds to the @p frameNo needs to be drawn
417 * @param[in] surface Surface in which content will be drawn
418 * @param[in] keepAspectRatio whether to keep the aspect ratio while scaling the content.
422 void renderSync(size_t frameNo, Surface surface, bool keepAspectRatio=true);
425 * @brief Returns root layer of the composition updated with
426 * content of the Lottie resource at frame number @p frameNo.
428 * @param[in] frameNo Content corresponds to the @p frameNo needs to be extracted.
429 * @param[in] width content viewbox width
430 * @param[in] height content viewbox height
432 * @return Root layer node.
436 const LOTLayerNode * renderTree(size_t frameNo, size_t width, size_t height) const;
439 * @brief Returns Composition Markers.
442 * @return returns MarkerList of the Composition.
447 const MarkerList& markers() const;
450 * @brief Returns Layer information{name, inFrame, outFrame} of all the child layers of the composition.
453 * @return List of Layer Information of the Composition.
458 const LayerInfoList& layers() const;
461 * @brief Sets property value for the specified {@link KeyPath}. This {@link KeyPath} can resolve
462 * to multiple contents. In that case, the callback's value will apply to all of them.
464 * Keypath should conatin object names separated by (.) and can handle globe(**) or wildchar(*).
467 * To change fillcolor property of fill1 object in the layer1->group1->fill1 hirarchy to RED color
469 * player->setValue<rlottie::Property::FillColor>("layer1.group1.fill1", rlottie::Color(1, 0, 0);
471 * if all the color property inside group1 needs to be changed to GREEN color
473 * player->setValue<rlottie::Property::FillColor>("**.group1.**", rlottie::Color(0, 1, 0);
477 template<Property prop, typename AnyValue>
478 void setValue(const std::string &keypath, AnyValue value)
480 setValue(MapType<std::integral_constant<Property, prop>>{}, prop, keypath, value);
484 * @brief default destructor
491 void setValue(Color_Type, Property, const std::string &, Color);
492 void setValue(Float_Type, Property, const std::string &, float);
493 void setValue(Size_Type, Property, const std::string &, Size);
494 void setValue(Point_Type, Property, const std::string &, Point);
496 void setValue(Color_Type, Property, const std::string &, std::function<Color(const FrameInfo &)> &&);
497 void setValue(Float_Type, Property, const std::string &, std::function<float(const FrameInfo &)> &&);
498 void setValue(Size_Type, Property, const std::string &, std::function<Size(const FrameInfo &)> &&);
499 void setValue(Point_Type, Property, const std::string &, std::function<Point(const FrameInfo &)> &&);
501 * @brief default constructor
507 std::unique_ptr<AnimationImpl> d;
510 //Map Property to Value type
511 template<> struct MapType<std::integral_constant<Property, Property::FillColor>>: Color_Type{};
512 template<> struct MapType<std::integral_constant<Property, Property::StrokeColor>>: Color_Type{};
513 template<> struct MapType<std::integral_constant<Property, Property::FillOpacity>>: Float_Type{};
514 template<> struct MapType<std::integral_constant<Property, Property::StrokeOpacity>>: Float_Type{};
515 template<> struct MapType<std::integral_constant<Property, Property::StrokeWidth>>: Float_Type{};
516 template<> struct MapType<std::integral_constant<Property, Property::TrRotation>>: Float_Type{};
517 template<> struct MapType<std::integral_constant<Property, Property::TrOpacity>>: Float_Type{};
518 template<> struct MapType<std::integral_constant<Property, Property::TrAnchor>>: Point_Type{};
519 template<> struct MapType<std::integral_constant<Property, Property::TrPosition>>: Point_Type{};
520 template<> struct MapType<std::integral_constant<Property, Property::TrScale>>: Size_Type{};
523 } // namespace lotplayer
525 #endif // _RLOTTIE_H_