| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
| 17 | */ |
| 18 |
| 19 | #ifndef _RLOTTIE_H_ |
| 20 | #define _RLOTTIE_H_ |
| 21 |
| 22 | #include <future> |
| 23 | #include <vector> |
| 24 | #include <memory> |
| 25 | #include <map> |
| 26 |
| 27 | #ifdef _WIN32 |
| 28 | #ifdef LOT_BUILD |
| 29 | #ifdef DLL_EXPORT |
| 30 | #define LOT_EXPORT __declspec(dllexport) |
| 31 | #else |
| 32 | #define LOT_EXPORT |
| 33 | #endif |
| 34 | #else |
| 35 | #define LOT_EXPORT __declspec(dllimport) |
| 36 | #endif |
| 37 | #else |
| 38 | #ifdef __GNUC__ |
| 39 | #if __GNUC__ >= 4 |
| 40 | #define LOT_EXPORT __attribute__((visibility("default"))) |
| 41 | #else |
| 42 | #define LOT_EXPORT |
| 43 | #endif |
| 44 | #else |
| 45 | #define LOT_EXPORT |
| 46 | #endif |
| 47 | #endif |
| 48 |
| 49 | class AnimationImpl; |
| 50 | struct LOTNode; |
| 51 | struct LOTLayerNode; |
| 52 |
| 53 | namespace rlottie { |
| 54 |
| 55 | enum class FitzModifier { |
| 56 | None, |
| 57 | Type12, |
| 58 | Type3, |
| 59 | Type4, |
| 60 | Type5, |
| 61 | Type6 |
| 62 | }; |
| 63 |
| 64 | struct Color { |
| 65 | Color() = default; |
| 66 | Color(float r, float g , float b):_r(r), _g(g), _b(b){} |
| 67 | float r() const {return _r;} |
| 68 | float g() const {return _g;} |
| 69 | float b() const {return _b;} |
| 70 | private: |
| 71 | float _r{0}; |
| 72 | float _g{0}; |
| 73 | float _b{0}; |
| 74 | }; |
| 75 |
| 76 | struct Size { |
| 77 | Size() = default; |
| 78 | Size(float w, float h):_w(w), _h(h){} |
| 79 | float w() const {return _w;} |
| 80 | float h() const {return _h;} |
| 81 | private: |
| 82 | float _w{0}; |
| 83 | float _h{0}; |
| 84 | }; |
| 85 |
| 86 | struct Point { |
| 87 | Point() = default; |
| 88 | Point(float x, float y):_x(x), _y(y){} |
| 89 | float x() const {return _x;} |
| 90 | float y() const {return _y;} |
| 91 | private: |
| 92 | float _x{0}; |
| 93 | float _y{0}; |
| 94 | }; |
| 95 |
| 96 | struct FrameInfo { |
| 97 | explicit FrameInfo(uint32_t frame): _frameNo(frame){} |
| 98 | uint32_t curFrame() const {return _frameNo;} |
| 99 | private: |
| 100 | uint32_t _frameNo; |
| 101 | }; |
| 102 |
| 103 | enum class Property { |
| 104 | Color, /*!< Color property of Fill object , value type is rlottie::Color */ |
| 105 | FillOpacity, /*!< Opacity property of Fill object , value type is float [ 0 .. 100] */ |
| 106 | StrokeOpacity, /*!< Opacity property of Stroke object , value type is float [ 0 .. 100] */ |
| 107 | StrokeWidth, /*!< stroke with property of Stroke object , value type is float */ |
| 108 | TrAnchor, /*!< Transform Anchor property of Layer and Group object , value type is rlottie::Point */ |
| 109 | TrPosition, /*!< Transform Position property of Layer and Group object , value type is rlottie::Point */ |
| 110 | TrScale, /*!< Transform Scale property of Layer and Group object , value type is rlottie::Size. range[0 ..100] */ |
| 111 | TrRotation, /*!< Transform Scale property of Layer and Group object , value type is float. range[0 .. 360] in degrees*/ |
| 112 | TrOpacity /*!< Transform Opacity property of Layer and Group object , value type is float [ 0 .. 100] */ |
| 113 | }; |
| 114 |
| 115 | struct Color_Type{}; |
| 116 | struct Point_Type{}; |
| 117 | struct Size_Type{}; |
| 118 | struct Float_Type{}; |
| 119 | template <typename T> struct MapType; |
| 120 |
| 121 | class LOT_EXPORT Surface { |
| 122 | public: |
| 123 | /** |
| 124 | * @brief Surface object constructor. |
| 125 | * |
| 126 | * @param[in] buffer surface buffer. |
| 127 | * @param[in] width surface width. |
| 128 | * @param[in] height surface height. |
| 129 | * @param[in] bytesPerLine number of bytes in a surface scanline. |
| 130 | * |
| 131 | * @note Default surface format is ARGB32_Premultiplied. |
| 132 | * |
| 133 | * @internal |
| 134 | */ |
| 135 | Surface(uint32_t *buffer, size_t width, size_t height, size_t bytesPerLine); |
| 136 |
| 137 | /** |
| 138 | * @brief Sets the Draw Area available on the Surface. |
| 139 | * |
| 140 | * Lottie will use the draw region size to generate frame image |
| 141 | * and will update only the draw rgion of the surface. |
| 142 | * |
| 143 | * @param[in] x region area x position. |
| 144 | * @param[in] y region area y position. |
| 145 | * @param[in] width region area width. |
| 146 | * @param[in] height region area height. |
| 147 | * |
| 148 | * @note Default surface format is ARGB32_Premultiplied. |
| 149 | * @note Default draw region area is [ 0 , 0, surface width , surface height] |
| 150 | * |
| 151 | * @internal |
| 152 | */ |
| 153 | void setDrawRegion(size_t x, size_t y, size_t width, size_t height); |
| 154 |
| 155 | /** |
| 156 | * @brief Returns width of the surface. |
| 157 | * |
| 158 | * @return surface width |
| 159 | * |
| 160 | * @internal |
| 161 | * |
| 162 | */ |
| 163 | size_t width() const {return mWidth;} |
| 164 |
| 165 | /** |
| 166 | * @brief Returns height of the surface. |
| 167 | * |
| 168 | * @return surface height |
| 169 | * |
| 170 | * @internal |
| 171 | */ |
| 172 | size_t height() const {return mHeight;} |
| 173 |
| 174 | /** |
| 175 | * @brief Returns number of bytes in the surface scanline. |
| 176 | * |
| 177 | * @return number of bytes in scanline. |
| 178 | * |
| 179 | * @internal |
| 180 | */ |
| 181 | size_t bytesPerLine() const {return mBytesPerLine;} |
| 182 |
| 183 | /** |
| 184 | * @brief Returns buffer attached tp the surface. |
| 185 | * |
| 186 | * @return buffer attaced to the Surface. |
| 187 | * |
| 188 | * @internal |
| 189 | */ |
| 190 | uint32_t *buffer() const {return mBuffer;} |
| 191 |
| 192 | /** |
| 193 | * @brief Returns drawable area width of the surface. |
| 194 | * |
| 195 | * @return drawable area width |
| 196 | * |
| 197 | * @note Default value is width() of the surface |
| 198 | * |
| 199 | * @internal |
| 200 | * |
| 201 | */ |
| 202 | size_t drawRegionWidth() const {return mDrawArea.w;} |
| 203 |
| 204 | /** |
| 205 | * @brief Returns drawable area height of the surface. |
| 206 | * |
| 207 | * @return drawable area height |
| 208 | * |
| 209 | * @note Default value is height() of the surface |
| 210 | * |
| 211 | * @internal |
| 212 | */ |
| 213 | size_t drawRegionHeight() const {return mDrawArea.h;} |
| 214 |
| 215 | /** |
| 216 | * @brief Returns drawable area's x position of the surface. |
| 217 | * |
| 218 | * @return drawable area's x potition. |
| 219 | * |
| 220 | * @note Default value is 0 |
| 221 | * |
| 222 | * @internal |
| 223 | */ |
| 224 | size_t drawRegionPosX() const {return mDrawArea.x;} |
| 225 |
| 226 | /** |
| 227 | * @brief Returns drawable area's y position of the surface. |
| 228 | * |
| 229 | * @return drawable area's y potition. |
| 230 | * |
| 231 | * @note Default value is 0 |
| 232 | * |
| 233 | * @internal |
| 234 | */ |
| 235 | size_t drawRegionPosY() const {return mDrawArea.y;} |
| 236 |
| 237 | /** |
| 238 | * @brief Default constructor. |
| 239 | */ |
| 240 | Surface() = default; |
| 241 | private: |
| 242 | uint32_t *mBuffer{nullptr}; |
| 243 | size_t mWidth{0}; |
| 244 | size_t mHeight{0}; |
| 245 | size_t mBytesPerLine{0}; |
| 246 | struct { |
| 247 | size_t x{0}; |
| 248 | size_t y{0}; |
| 249 | size_t w{0}; |
| 250 | size_t h{0}; |
| 251 | }mDrawArea; |
| 252 | }; |
| 253 |
| 254 | using LayerInfoList = std::vector<std::tuple<std::string, int , int>>; |
| 255 |
| 256 | class LOT_EXPORT Animation { |
| 257 | public: |
| 258 |
| 259 | /** |
| 260 | * @brief Constructs an animation object from file path. |
| 261 | * |
| 262 | * @param[in] path Lottie resource file path |
| 263 | * |
| 264 | * @return Animation object that can render the contents of the |
| 265 | * Lottie resource represented by file path. |
| 266 | * |
| 267 | * @internal |
| 268 | */ |
| 269 | static std::unique_ptr<Animation> |
| 270 | loadFromFile(const std::string &path, std::map<int32_t, int32_t> *colorReplacement, FitzModifier fitzModifier); |
| 271 |
| 272 | /** |
| 273 | * @brief Constructs an animation object from JSON string data. |
| 274 | * |
| 275 | * @param[in] jsonData The JSON string data. |
| 276 | * @param[in] key the string that will be used to cache the JSON string data. |
| 277 | * @param[in] resourcePath the path will be used to search for external resource. |
| 278 | * |
| 279 | * @return Animation object that can render the contents of the |
| 280 | * Lottie resource represented by JSON string data. |
| 281 | * |
| 282 | * @internal |
| 283 | */ |
| 284 | static std::unique_ptr<Animation> |
| 285 | loadFromData(std::string jsonData, const std::string &key, std::map<int32_t, int32_t> *colorReplacement, FitzModifier fitzModifier = FitzModifier::None, const std::string &resourcePath=""); |
| 286 |
| 287 | /** |
| 288 | * @brief Returns default framerate of the Lottie resource. |
| 289 | * |
| 290 | * @return framerate of the Lottie resource |
| 291 | * |
| 292 | * @internal |
| 293 | * |
| 294 | */ |
| 295 | double frameRate() const; |
| 296 |
| 297 | /** |
| 298 | * @brief Returns total number of frames present in the Lottie resource. |
| 299 | * |
| 300 | * @return frame count of the Lottie resource. |
| 301 | * |
| 302 | * @note frame number starts with 0. |
| 303 | * |
| 304 | * @internal |
| 305 | */ |
| 306 | size_t totalFrame() const; |
| 307 |
| 308 | /** |
| 309 | * @brief Returns default viewport size of the Lottie resource. |
| 310 | * |
| 311 | * @param[out] width default width of the viewport. |
| 312 | * @param[out] height default height of the viewport. |
| 313 | * |
| 314 | * @internal |
| 315 | * |
| 316 | */ |
| 317 | void size(size_t &width, size_t &height) const; |
| 318 |
| 319 | /** |
| 320 | * @brief Returns total animation duration of Lottie resource in second. |
| 321 | * it uses totalFrame() and frameRate() to calculate the duration. |
| 322 | * duration = totalFrame() / frameRate(). |
| 323 | * |
| 324 | * @return total animation duration in second. |
| 325 | * @retval 0 if the Lottie resource has no animation. |
| 326 | * |
| 327 | * @see totalFrame() |
| 328 | * @see frameRate() |
| 329 | * |
| 330 | * @internal |
| 331 | */ |
| 332 | double duration() const; |
| 333 |
| 334 | /** |
| 335 | * @brief Returns frame number for a given position. |
| 336 | * this function helps to map the position value retuned |
| 337 | * by the animator to a frame number in side the Lottie resource. |
| 338 | * frame_number = lerp(start_frame, endframe, pos); |
| 339 | * |
| 340 | * @param[in] pos normalized position value [0 ... 1] |
| 341 | * |
| 342 | * @return frame numer maps to the position value [startFrame .... endFrame] |
| 343 | * |
| 344 | * @internal |
| 345 | */ |
| 346 | size_t frameAtPos(double pos); |
| 347 |
| 348 | /** |
| 349 | * @brief Renders the content to surface synchronously. |
| 350 | * for performance use the async rendering @see render |
| 351 | * |
| 352 | * @param[in] frameNo Content corresponds to the @p frameNo needs to be drawn |
| 353 | * @param[in] surface Surface in which content will be drawn |
| 354 | * |
| 355 | * @internal |
| 356 | */ |
| 357 | void renderSync(size_t frameNo, Surface &surface, bool clear, bool* result); |
| 358 |
| 359 | /** |
| 360 | * @brief Returns root layer of the composition updated with |
| 361 | * content of the Lottie resource at frame number @p frameNo. |
| 362 | * |
| 363 | * @param[in] frameNo Content corresponds to the @p frameNo needs to be extracted. |
| 364 | * @param[in] width content viewbox width |
| 365 | * @param[in] height content viewbox height |
| 366 | * |
| 367 | * @return Root layer node. |
| 368 | * |
| 369 | * @internal |
| 370 | */ |
| 371 | const LOTLayerNode * renderTree(size_t frameNo, size_t width, size_t height) const; |
| 372 |
| 373 | /** |
| 374 | * @brief Returns Layer information{name, inFrame, outFrame} of all the child layers of the composition. |
| 375 | * |
| 376 | * |
| 377 | * @return List of Layer Information of the Composition. |
| 378 | * |
| 379 | * @see LayerInfoList |
| 380 | * @internal |
| 381 | */ |
| 382 | const LayerInfoList& layers() const; |
| 383 |
| 384 | /** |
| 385 | * @brief Sets property value for the specified {@link KeyPath}. This {@link KeyPath} can resolve |
| 386 | * to multiple contents. In that case, the callback's value will apply to all of them. |
| 387 | * |
| 388 | * Keypath should conatin object names separated by (.) and can handle globe(**) or wildchar(*). |
| 389 | * |
| 390 | * @usage |
| 391 | * To change fillcolor property of fill1 object in the layer1->group1->fill1 hirarchy to RED color |
| 392 | * |
| 393 | * player->setValue<rlottie::Property::FillColor>("layer1.group1.fill1", rlottie::Color(1, 0, 0); |
| 394 | * |
| 395 | * if all the color property inside group1 needs to be changed to GREEN color |
| 396 | * |
| 397 | * player->setValue<rlottie::Property::FillColor>("**.group1.**", rlottie::Color(0, 1, 0); |
| 398 | * |
| 399 | * @internal |
| 400 | */ |
| 401 | template<Property prop, typename AnyValue> |
| 402 | void setValue(const std::string &keypath, AnyValue value) |
| 403 | { |
| 404 | setValue(MapType<std::integral_constant<Property, prop>>{}, prop, keypath, value); |
| 405 | } |
| 406 |
| 407 | /** |
| 408 | * @brief default destructor |
| 409 | * |
| 410 | * @internal |
| 411 | */ |
| 412 | ~Animation(); |
| 413 |
| 414 | std::map<int32_t, int32_t> *colorMap{nullptr}; |
| 415 | void resetCurrentFrame(); |
| 416 | private: |
| 417 | void setValue(Color_Type, Property, const std::string &, Color); |
| 418 | void setValue(Float_Type, Property, const std::string &, float); |
| 419 | void setValue(Size_Type, Property, const std::string &, Size); |
| 420 | void setValue(Point_Type, Property, const std::string &, Point); |
| 421 |
| 422 | void setValue(Color_Type, Property, const std::string &, std::function<Color(const FrameInfo &)> &&); |
| 423 | void setValue(Float_Type, Property, const std::string &, std::function<float(const FrameInfo &)> &&); |
| 424 | void setValue(Size_Type, Property, const std::string &, std::function<Size(const FrameInfo &)> &&); |
| 425 | void setValue(Point_Type, Property, const std::string &, std::function<Point(const FrameInfo &)> &&); |
| 426 | /** |
| 427 | * @brief default constructor |
| 428 | * |
| 429 | * @internal |
| 430 | */ |
| 431 | Animation(); |
| 432 |
| 433 | std::unique_ptr<AnimationImpl> d; |
| 434 | }; |
| 435 |
| 436 | //Map Property to Value type |
| 437 | template<> struct MapType<std::integral_constant<Property, Property::Color>>: Color_Type{}; |
| 438 | template<> struct MapType<std::integral_constant<Property, Property::FillOpacity>>: Float_Type{}; |
| 439 | template<> struct MapType<std::integral_constant<Property, Property::StrokeOpacity>>: Float_Type{}; |
| 440 | template<> struct MapType<std::integral_constant<Property, Property::StrokeWidth>>: Float_Type{}; |
| 441 | template<> struct MapType<std::integral_constant<Property, Property::TrRotation>>: Float_Type{}; |
| 442 | template<> struct MapType<std::integral_constant<Property, Property::TrOpacity>>: Float_Type{}; |
| 443 | template<> struct MapType<std::integral_constant<Property, Property::TrAnchor>>: Point_Type{}; |
| 444 | template<> struct MapType<std::integral_constant<Property, Property::TrPosition>>: Point_Type{}; |
| 445 | template<> struct MapType<std::integral_constant<Property, Property::TrScale>>: Size_Type{}; |
| 446 |
| 447 |
| 448 | } // namespace lotplayer |
| 449 |
| 450 | #endif // _RLOTTIE_H_ |
| 451 |