| 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 LOTModel_H |
| 20 | #define LOTModel_H |
| 21 |
| 22 | #include<vector> |
| 23 | #include<memory> |
| 24 | #include<unordered_map> |
| 25 | #include<map> |
| 26 | #include <algorithm> |
| 27 | #include"vpoint.h" |
| 28 | #include"vrect.h" |
| 29 | #include"vinterpolator.h" |
| 30 | #include"vmatrix.h" |
| 31 | #include"vbezier.h" |
| 32 | #include"vbrush.h" |
| 33 | #include"vpath.h" |
| 34 |
| 35 | V_USE_NAMESPACE |
| 36 |
| 37 | class LOTCompositionData; |
| 38 | class LOTLayerData; |
| 39 | class LOTTransformData; |
| 40 | class LOTShapeGroupData; |
| 41 | class LOTShapeData; |
| 42 | class LOTRectData; |
| 43 | class LOTEllipseData; |
| 44 | class LOTTrimData; |
| 45 | class LOTRepeaterData; |
| 46 | class LOTFillData; |
| 47 | class LOTStrokeData; |
| 48 | class LOTGroupData; |
| 49 | class LOTGFillData; |
| 50 | class LOTGStrokeData; |
| 51 | class LottieShapeData; |
| 52 | class LOTPolystarData; |
| 53 | class LOTMaskData; |
| 54 |
| 55 | enum class MatteType: uchar |
| 56 | { |
| 57 | None = 0, |
| 58 | Alpha = 1, |
| 59 | AlphaInv, |
| 60 | Luma, |
| 61 | LumaInv |
| 62 | }; |
| 63 |
| 64 | enum class LayerType: uchar |
| 65 | { |
| 66 | Precomp = 0, |
| 67 | Solid = 1, |
| 68 | Image = 2, |
| 69 | Null = 3, |
| 70 | Shape = 4, |
| 71 | Text = 5 |
| 72 | }; |
| 73 |
| 74 | class LottieColor |
| 75 | { |
| 76 | public: |
| 77 | LottieColor() = default; |
| 78 | LottieColor(float red, float green, float blue, std::map<int32_t, int32_t> *colorReplacement) { |
| 79 | r = red; |
| 80 | g = green; |
| 81 | b = blue; |
| 82 | colorMap = colorReplacement; |
| 83 | }; |
| 84 | VColor toColor(float a=1) { |
| 85 | float r1, g1, b1; |
| 86 | getColorReplacement(colorMap, *this, r1, g1, b1); |
| 87 | return VColor((255 * r1), (255 * g1), (255 * b1), (255 * a)); |
| 88 | }; |
| 89 | friend inline LottieColor operator+(const LottieColor &c1, const LottieColor &c2); |
| 90 | friend inline LottieColor operator-(const LottieColor &c1, const LottieColor &c2); |
| 91 | friend inline void getColorReplacement(std::map<int32_t, int32_t> *colorMap, const LottieColor &c, float &r, float &g, float &b); |
| 92 | public: |
| 93 | std::map<int32_t, int32_t> *colorMap{nullptr}; |
| 94 | float r{1}; |
| 95 | float g{1}; |
| 96 | float b{1}; |
| 97 | }; |
| 98 |
| 99 | inline void getColorReplacement(std::map<int32_t, int32_t> *colorMap, const LottieColor &c, float &r, float &g, float &b) { |
| 100 | if (colorMap != nullptr && !colorMap->empty()) { |
| 101 | int32_t rr = (int32_t) round(c.r * 255); |
| 102 | int32_t gg = (int32_t) round(c.g * 255); |
| 103 | int32_t bb = (int32_t) round(c.b * 255); |
| 104 | int32_t cc = (int32_t) (((bb & 0xff) << 16) | ((gg & 0xff) << 8) | (rr & 0xff)); |
| 105 | std::map<int32_t, int32_t>::iterator iter = colorMap->find(cc); |
| 106 | if (iter != colorMap->end()) { |
| 107 | cc = iter->second; |
| 108 | r = ((cc) & 0xff) / 255.0f; |
| 109 | g = ((cc >> 8) & 0xff) / 255.0f; |
| 110 | b = ((cc >> 16) & 0xff) / 255.0f; |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | r = c.r; |
| 115 | g = c.g; |
| 116 | b = c.b; |
| 117 | } |
| 118 |
| 119 | inline LottieColor operator-(const LottieColor &c1, const LottieColor &c2) |
| 120 | { |
| 121 | float r1, g1, b1; |
| 122 | float r2, g2, b2; |
| 123 | getColorReplacement(c1.colorMap, c1, r1, g1, b1); |
| 124 | getColorReplacement(c2.colorMap, c2, r2, g2, b2); |
| 125 | return LottieColor(r1 - r2, g1 - g2, b1 - b2, nullptr); |
| 126 | } |
| 127 | inline LottieColor operator+(const LottieColor &c1, const LottieColor &c2) |
| 128 | { |
| 129 | float r1, g1, b1; |
| 130 | float r2, g2, b2; |
| 131 | getColorReplacement(c1.colorMap, c1, r1, g1, b1); |
| 132 | getColorReplacement(c2.colorMap, c2, r2, g2, b2); |
| 133 | return LottieColor(r1 + r2, g1 + g2, b1 + b2, nullptr); |
| 134 | } |
| 135 |
| 136 | inline const LottieColor operator*(const LottieColor &c, float m) |
| 137 | { |
| 138 | float r1, g1, b1; |
| 139 | getColorReplacement(c.colorMap, c, r1, g1, b1); |
| 140 | return LottieColor(r1 * m, g1 * m, b1 * m, nullptr); |
| 141 | } |
| 142 |
| 143 | inline const LottieColor operator*(float m, const LottieColor &c) |
| 144 | { |
| 145 | float r1, g1, b1; |
| 146 | getColorReplacement(c.colorMap, c, r1, g1, b1); |
| 147 | return LottieColor(r1 * m, g1 * m, b1 * m, nullptr); |
| 148 | } |
| 149 |
| 150 | class LottieShapeData |
| 151 | { |
| 152 | public: |
| 153 | void reserve(int size) { |
| 154 | mPoints.reserve(mPoints.size() + size); |
| 155 | } |
| 156 | void toPath(VPath& path) { |
| 157 | path.reset(); |
| 158 |
| 159 | if (mPoints.empty()) return; |
| 160 |
| 161 | int size = mPoints.size(); |
| 162 | const VPointF *points = mPoints.data(); |
| 163 | /* reserve exact memory requirement at once |
| 164 | * ptSize = size + 1(size + close) |
| 165 | * elmSize = size/3 cubic + 1 move + 1 close |
| 166 | */ |
| 167 | path.reserve(size + 1 , size/3 + 2); |
| 168 | path.moveTo(points[0]); |
| 169 | for (int i = 1 ; i < size; i+=3) { |
| 170 | path.cubicTo(points[i], points[i+1], points[i+2]); |
| 171 | } |
| 172 | if (mClosed) |
| 173 | path.close(); |
| 174 | } |
| 175 | public: |
| 176 | std::vector<VPointF> mPoints; |
| 177 | bool mClosed = false; /* "c" */ |
| 178 | }; |
| 179 |
| 180 |
| 181 |
| 182 | template<typename T> |
| 183 | inline T lerp(const T& start, const T& end, float t) |
| 184 | { |
| 185 | return start + t * (end - start); |
| 186 | } |
| 187 |
| 188 | inline LottieShapeData lerp(const LottieShapeData& start, const LottieShapeData& end, float t) |
| 189 | { |
| 190 | // Usal case both start and end path has same size |
| 191 | // In case its different then truncate the larger path and do the interpolation. |
| 192 | LottieShapeData result; |
| 193 | auto size = std::min(start.mPoints.size(), end.mPoints.size()); |
| 194 | result.reserve(size); |
| 195 | for (unsigned int i = 0 ; i < size; i++) { |
| 196 | result.mPoints.push_back(start.mPoints[i] + t * (end.mPoints[i] - start.mPoints[i])); |
| 197 | } |
| 198 | return result; |
| 199 | } |
| 200 |
| 201 | template <typename T> |
| 202 | struct LOTKeyFrameValue |
| 203 | { |
| 204 | T mStartValue; |
| 205 | T mEndValue; |
| 206 | T value(float t) const { |
| 207 | return lerp(mStartValue, mEndValue, t); |
| 208 | } |
| 209 | float angle(float ) const { return 0;} |
| 210 | }; |
| 211 |
| 212 | template <> |
| 213 | struct LOTKeyFrameValue<VPointF> |
| 214 | { |
| 215 | VPointF mStartValue; |
| 216 | VPointF mEndValue; |
| 217 | VPointF mInTangent; |
| 218 | VPointF mOutTangent; |
| 219 | bool mPathKeyFrame = false; |
| 220 |
| 221 | VPointF value(float t) const { |
| 222 | if (mPathKeyFrame) { |
| 223 | /* |
| 224 | * position along the path calcualated |
| 225 | * using bezier at progress length (t * bezlen) |
| 226 | */ |
| 227 | VBezier b = VBezier::fromPoints(mStartValue, mStartValue + mOutTangent, |
| 228 | mEndValue + mInTangent, mEndValue); |
| 229 | return b.pointAt(b.tAtLength(t * b.length())); |
| 230 |
| 231 | } else { |
| 232 | return lerp(mStartValue, mEndValue, t); |
| 233 | } |
| 234 | } |
| 235 |
| 236 | float angle(float t) const { |
| 237 | if (mPathKeyFrame) { |
| 238 | VBezier b = VBezier::fromPoints(mStartValue, mStartValue + mOutTangent, |
| 239 | mEndValue + mInTangent, mEndValue); |
| 240 | return b.angleAt(b.tAtLength(t * b.length())); |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | }; |
| 245 |
| 246 |
| 247 | template<typename T> |
| 248 | class LOTKeyFrame |
| 249 | { |
| 250 | public: |
| 251 | float progress(int frameNo) const { |
| 252 | return mInterpolator ? mInterpolator->value((frameNo - mStartFrame) / (mEndFrame - mStartFrame)) : 0; |
| 253 | } |
| 254 | T value(int frameNo) const { |
| 255 | return mValue.value(progress(frameNo)); |
| 256 | } |
| 257 | float angle(int frameNo) const { |
| 258 | return mValue.angle(progress(frameNo)); |
| 259 | } |
| 260 |
| 261 | public: |
| 262 | float mStartFrame{0}; |
| 263 | float mEndFrame{0}; |
| 264 | std::shared_ptr<VInterpolator> mInterpolator; |
| 265 | LOTKeyFrameValue<T> mValue; |
| 266 | }; |
| 267 |
| 268 | template<typename T> |
| 269 | class LOTAnimInfo |
| 270 | { |
| 271 | public: |
| 272 | T value(int frameNo) const { |
| 273 | if (!mKeyFrames.empty() && mKeyFrames.front().mStartFrame >= frameNo) { |
| 274 | return mKeyFrames.front().mValue.mStartValue; |
| 275 | } |
| 276 | if (!mKeyFrames.empty() && mKeyFrames.back().mEndFrame <= frameNo) { |
| 277 | return mKeyFrames.back().mValue.mEndValue; |
| 278 | } |
| 279 |
| 280 | for (const auto &keyFrame : mKeyFrames) { |
| 281 | if (frameNo >= keyFrame.mStartFrame && frameNo < keyFrame.mEndFrame) { |
| 282 | return keyFrame.value(frameNo); |
| 283 | } |
| 284 | } |
| 285 | return T(); |
| 286 | } |
| 287 |
| 288 | float angle(int frameNo) const { |
| 289 | if (mKeyFrames.empty() || (mKeyFrames.front().mStartFrame >= frameNo) || (mKeyFrames.back().mEndFrame <= frameNo) ) |
| 290 | return 0; |
| 291 |
| 292 | for(const auto &keyFrame : mKeyFrames) { |
| 293 | if (frameNo >= keyFrame.mStartFrame && frameNo < keyFrame.mEndFrame) |
| 294 | return keyFrame.angle(frameNo); |
| 295 | } |
| 296 | return 0; |
| 297 | } |
| 298 |
| 299 | bool changed(int prevFrame, int curFrame) const { |
| 300 | if (mKeyFrames.empty()) { |
| 301 | return false; |
| 302 | } |
| 303 | int first = mKeyFrames.front().mStartFrame; |
| 304 | int last = mKeyFrames.back().mEndFrame; |
| 305 |
| 306 | if ((first > prevFrame && first > curFrame) || |
| 307 | (last < prevFrame && last < curFrame)) { |
| 308 | return false; |
| 309 | } |
| 310 |
| 311 | return true; |
| 312 | } |
| 313 |
| 314 | public: |
| 315 | std::vector<LOTKeyFrame<T>> mKeyFrames; |
| 316 | }; |
| 317 |
| 318 | template<typename T> |
| 319 | class LOTAnimatable |
| 320 | { |
| 321 | public: |
| 322 | LOTAnimatable() { construct(impl.mValue, {}); } |
| 323 | explicit LOTAnimatable(T value) { construct(impl.mValue, std::move(value)); } |
| 324 |
| 325 | const LOTAnimInfo<T>& animation() const {return *(impl.mAnimInfo.get());} |
| 326 | const T& value() const {return impl.mValue;} |
| 327 |
| 328 | LOTAnimInfo<T>& animation() |
| 329 | { |
| 330 | if (mStatic) { |
| 331 | destroy(); |
| 332 | construct(impl.mAnimInfo, std::make_unique<LOTAnimInfo<T>>()); |
| 333 | mStatic = false; |
| 334 | } |
| 335 | return *(impl.mAnimInfo.get()); |
| 336 | } |
| 337 |
| 338 | T& value() |
| 339 | { |
| 340 | assert(mStatic); |
| 341 | return impl.mValue; |
| 342 | } |
| 343 |
| 344 | // delete special member functions |
| 345 | LOTAnimatable(const LOTAnimatable &) = delete; |
| 346 | LOTAnimatable(LOTAnimatable &&) = delete; |
| 347 | LOTAnimatable& operator=(const LOTAnimatable&) = delete; |
| 348 | LOTAnimatable& operator=(LOTAnimatable&&) = delete; |
| 349 |
| 350 | ~LOTAnimatable() {destroy();} |
| 351 |
| 352 | bool isStatic() const {return mStatic;} |
| 353 |
| 354 | T value(int frameNo) const { |
| 355 | return isStatic() ? value() : animation().value(frameNo); |
| 356 | } |
| 357 |
| 358 | float angle(int frameNo) const { |
| 359 | return isStatic() ? 0 : animation().angle(frameNo); |
| 360 | } |
| 361 |
| 362 | bool changed(int prevFrame, int curFrame) const { |
| 363 | return isStatic() ? false : animation().changed(prevFrame, curFrame); |
| 364 | } |
| 365 | private: |
| 366 | template <typename Tp> |
| 367 | void construct(Tp& member, Tp&& val) |
| 368 | { |
| 369 | new (&member) Tp(std::move(val)); |
| 370 | } |
| 371 |
| 372 | void destroy() { |
| 373 | if (mStatic) { |
| 374 | impl.mValue.~T(); |
| 375 | } else { |
| 376 | using std::unique_ptr; |
| 377 | impl.mAnimInfo.~unique_ptr<LOTAnimInfo<T>>(); |
| 378 | } |
| 379 | } |
| 380 | union details { |
| 381 | std::unique_ptr<LOTAnimInfo<T>> mAnimInfo; |
| 382 | T mValue; |
| 383 | details(){} |
| 384 | ~details(){} |
| 385 | }impl; |
| 386 | bool mStatic{true}; |
| 387 | }; |
| 388 |
| 389 | enum class LottieBlendMode: uchar |
| 390 | { |
| 391 | Normal = 0, |
| 392 | Multiply = 1, |
| 393 | Screen = 2, |
| 394 | OverLay = 3 |
| 395 | }; |
| 396 |
| 397 | class LOTDataVisitor; |
| 398 | class LOTData |
| 399 | { |
| 400 | public: |
| 401 | enum class Type :short { |
| 402 | Composition = 1, |
| 403 | Layer, |
| 404 | ShapeGroup, |
| 405 | Transform, |
| 406 | Fill, |
| 407 | Stroke, |
| 408 | GFill, |
| 409 | GStroke, |
| 410 | Rect, |
| 411 | Ellipse, |
| 412 | Shape, |
| 413 | Polystar, |
| 414 | Trim, |
| 415 | Repeater |
| 416 | }; |
| 417 | explicit LOTData(LOTData::Type type): mType(type){} |
| 418 | inline LOTData::Type type() const {return mType;} |
| 419 | bool isStatic() const{return mStatic;} |
| 420 | void setStatic(bool value) {mStatic = value;} |
| 421 | bool hidden() const {return mHidden;} |
| 422 | void setHidden(bool value) {mHidden = value;} |
| 423 | void setName(const char *str) {mName = str;} |
| 424 | const std::string& name() const{ return mName;} |
| 425 | private: |
| 426 | std::string mName; |
| 427 | bool mStatic{true}; |
| 428 | bool mHidden{false}; |
| 429 | LOTData::Type mType; |
| 430 | }; |
| 431 |
| 432 | class LOTGroupData: public LOTData |
| 433 | { |
| 434 | public: |
| 435 | explicit LOTGroupData(LOTData::Type type):LOTData(type){} |
| 436 | public: |
| 437 | std::vector<std::shared_ptr<LOTData>> mChildren; |
| 438 | std::shared_ptr<LOTTransformData> mTransform; |
| 439 | }; |
| 440 |
| 441 | class LOTShapeGroupData : public LOTGroupData |
| 442 | { |
| 443 | public: |
| 444 | LOTShapeGroupData():LOTGroupData(LOTData::Type::ShapeGroup){} |
| 445 | }; |
| 446 |
| 447 | class LOTLayerData; |
| 448 | struct LOTAsset |
| 449 | { |
| 450 | enum class Type : unsigned char{ |
| 451 | Precomp, |
| 452 | Image, |
| 453 | Char |
| 454 | }; |
| 455 | bool isStatic() const {return mStatic;} |
| 456 | void setStatic(bool value) {mStatic = value;} |
| 457 | VBitmap bitmap() const {return mBitmap;} |
| 458 | void loadImageData(std::string data); |
| 459 | void loadImagePath(std::string Path); |
| 460 | Type mAssetType{Type::Precomp}; |
| 461 | bool mStatic{true}; |
| 462 | std::string mRefId; // ref id |
| 463 | std::vector<std::shared_ptr<LOTData>> mLayers; |
| 464 | // image asset data |
| 465 | int mWidth{0}; |
| 466 | int mHeight{0}; |
| 467 | VBitmap mBitmap; |
| 468 | }; |
| 469 |
| 470 | struct LOT3DData |
| 471 | { |
| 472 | LOTAnimatable<float> mRx{0}; |
| 473 | LOTAnimatable<float> mRy{0}; |
| 474 | LOTAnimatable<float> mRz{0}; |
| 475 | }; |
| 476 |
| 477 | struct TransformData |
| 478 | { |
| 479 | VMatrix matrix(int frameNo, bool autoOrient = false) const; |
| 480 | float opacity(int frameNo) const { return mOpacity.value(frameNo)/100.0f; } |
| 481 | bool isStatic() const { return mStatic;} |
| 482 | std::unique_ptr<LOT3DData> m3D; |
| 483 | LOTAnimatable<float> mRotation{0}; /* "r" */ |
| 484 | LOTAnimatable<VPointF> mScale{{100, 100}}; /* "s" */ |
| 485 | LOTAnimatable<VPointF> mPosition; /* "p" */ |
| 486 | LOTAnimatable<float> mX{0}; |
| 487 | LOTAnimatable<float> mY{0}; |
| 488 | LOTAnimatable<VPointF> mAnchor; /* "a" */ |
| 489 | LOTAnimatable<float> mOpacity{100}; /* "o" */ |
| 490 | bool mSeparate{false}; |
| 491 | bool mStatic{false}; |
| 492 | }; |
| 493 |
| 494 | class LOTTransformData : public LOTData |
| 495 | { |
| 496 | public: |
| 497 | LOTTransformData():LOTData(LOTData::Type::Transform){} |
| 498 | void set(std::unique_ptr<TransformData> data) |
| 499 | { |
| 500 | setStatic(data->isStatic()); |
| 501 | if (isStatic()) { |
| 502 | new (&impl.mStaticData) static_data(data->matrix(0), data->opacity(0)); |
| 503 | } else { |
| 504 | new (&impl.mData) std::unique_ptr<TransformData>(std::move(data)); |
| 505 | } |
| 506 | } |
| 507 | VMatrix matrix(int frameNo, bool autoOrient = false) const |
| 508 | { |
| 509 | if (isStatic()) return impl.mStaticData.mMatrix; |
| 510 | return impl.mData->matrix(frameNo, autoOrient); |
| 511 | } |
| 512 | float opacity(int frameNo) const |
| 513 | { |
| 514 | if (isStatic()) return impl.mStaticData.mOpacity; |
| 515 | return impl.mData->opacity(frameNo); |
| 516 | } |
| 517 |
| 518 | LOTTransformData& operator=(LOTTransformData&&) = delete; |
| 519 | ~LOTTransformData() {destroy();} |
| 520 |
| 521 | private: |
| 522 | void destroy() { |
| 523 | if (isStatic()) { |
| 524 | impl.mStaticData.~static_data(); |
| 525 | } else { |
| 526 | using std::unique_ptr; |
| 527 | impl.mData.~unique_ptr<TransformData>(); |
| 528 | } |
| 529 | } |
| 530 | struct static_data { |
| 531 | static_data(VMatrix &&m, float opacity): |
| 532 | mOpacity(opacity), mMatrix(std::move(m)){} |
| 533 | float mOpacity; |
| 534 | VMatrix mMatrix; |
| 535 | }; |
| 536 | union details { |
| 537 | std::unique_ptr<TransformData> mData; |
| 538 | static_data mStaticData; |
| 539 | details(){} |
| 540 | ~details(){} |
| 541 | }impl; |
| 542 | }; |
| 543 |
| 544 | struct ExtraLayerData |
| 545 | { |
| 546 | LottieColor mSolidColor; |
| 547 | std::string mPreCompRefId; |
| 548 | LOTAnimatable<float> mTimeRemap; /* "tm" */ |
| 549 | LOTCompositionData *mCompRef{nullptr}; |
| 550 | std::shared_ptr<LOTAsset> mAsset; |
| 551 | std::vector<std::shared_ptr<LOTMaskData>> mMasks; |
| 552 | }; |
| 553 |
| 554 | class LOTLayerData : public LOTGroupData |
| 555 | { |
| 556 | public: |
| 557 | LOTLayerData():LOTGroupData(LOTData::Type::Layer){} |
| 558 | bool hasPathOperator() const noexcept {return mHasPathOperator;} |
| 559 | bool hasGradient() const noexcept {return mHasGradient;} |
| 560 | bool hasMask() const noexcept {return mHasMask;} |
| 561 | bool hasRepeater() const noexcept {return mHasRepeater;} |
| 562 | int id() const noexcept{ return mId;} |
| 563 | int parentId() const noexcept{ return mParentId;} |
| 564 | bool hasParent() const noexcept {return mParentId != -1;} |
| 565 | int inFrame() const noexcept{return mInFrame;} |
| 566 | int outFrame() const noexcept{return mOutFrame;} |
| 567 | int startFrame() const noexcept{return mStartFrame;} |
| 568 | LottieColor solidColor() const noexcept{return mExtra->mSolidColor;} |
| 569 | bool autoOrient() const noexcept{return mAutoOrient;} |
| 570 | int timeRemap(int frameNo) const; |
| 571 | VSize layerSize() const {return mLayerSize;} |
| 572 | bool precompLayer() const {return mLayerType == LayerType::Precomp;} |
| 573 | VMatrix matrix(int frameNo) const |
| 574 | { |
| 575 | return mTransform ? mTransform->matrix(frameNo, autoOrient()) : VMatrix{}; |
| 576 | } |
| 577 | float opacity(int frameNo) const |
| 578 | { |
| 579 | return mTransform ? mTransform->opacity(frameNo) : 1.0f; |
| 580 | } |
| 581 | LOTAsset* asset() const |
| 582 | { |
| 583 | return (mExtra && mExtra->mAsset) ? mExtra->mAsset.get() : nullptr; |
| 584 | } |
| 585 | public: |
| 586 | ExtraLayerData* extra() |
| 587 | { |
| 588 | if (!mExtra) mExtra = std::make_unique<ExtraLayerData>(); |
| 589 | return mExtra.get(); |
| 590 | } |
| 591 | MatteType mMatteType{MatteType::None}; |
| 592 | LayerType mLayerType{LayerType::Null}; |
| 593 | LottieBlendMode mBlendMode{LottieBlendMode::Normal}; |
| 594 | bool mHasPathOperator{false}; |
| 595 | bool mHasMask{false}; |
| 596 | bool mHasRepeater{false}; |
| 597 | bool mHasGradient{false}; |
| 598 | bool mAutoOrient{false}; |
| 599 | VSize mLayerSize; |
| 600 | int mParentId{-1}; // Lottie the id of the parent in the composition |
| 601 | int mId{-1}; // Lottie the group id used for parenting. |
| 602 | float mTimeStreatch{1.0f}; |
| 603 | int mInFrame{0}; |
| 604 | int mOutFrame{0}; |
| 605 | int mStartFrame{0}; |
| 606 | std::unique_ptr<ExtraLayerData> mExtra{nullptr}; |
| 607 | }; |
| 608 |
| 609 | using LayerInfo = std::tuple<std::string, int , int>; |
| 610 |
| 611 | class LOTCompositionData : public LOTData |
| 612 | { |
| 613 | public: |
| 614 | LOTCompositionData():LOTData(LOTData::Type::Composition){} |
| 615 | const std::vector<LayerInfo> &layerInfoList() const { return mLayerInfoList;} |
| 616 | double duration() const { |
| 617 | return frameDuration() / frameRate(); // in second |
| 618 | } |
| 619 | size_t frameAtPos(double pos) const { |
| 620 | if (pos < 0) pos = 0; |
| 621 | if (pos > 1) pos = 1; |
| 622 | return pos * frameDuration(); |
| 623 | } |
| 624 | long frameAtTime(double timeInSec) const { |
| 625 | return frameAtPos(timeInSec / duration()); |
| 626 | } |
| 627 | size_t totalFrame() const {return mEndFrame - mStartFrame;} |
| 628 | long frameDuration() const {return mEndFrame - mStartFrame -1;} |
| 629 | float frameRate() const {return mFrameRate;} |
| 630 | long startFrame() const {return mStartFrame;} |
| 631 | long endFrame() const {return mEndFrame;} |
| 632 | VSize size() const {return mSize;} |
| 633 | void processRepeaterObjects(); |
| 634 | public: |
| 635 | std::string mVersion; |
| 636 | VSize mSize; |
| 637 | long mStartFrame{0}; |
| 638 | long mEndFrame{0}; |
| 639 | float mFrameRate{60}; |
| 640 | LottieBlendMode mBlendMode{LottieBlendMode::Normal}; |
| 641 | std::shared_ptr<LOTLayerData> mRootLayer; |
| 642 | std::unordered_map<std::string, |
| 643 | std::shared_ptr<LOTAsset>> mAssets; |
| 644 |
| 645 | std::vector<LayerInfo> mLayerInfoList; |
| 646 |
| 647 | }; |
| 648 |
| 649 | /** |
| 650 | * TimeRemap has the value in time domain(in sec) |
| 651 | * To get the proper mapping first we get the mapped time at the current frame Number |
| 652 | * then we need to convert mapped time to frame number using the composition time line |
| 653 | * Ex: at frame 10 the mappend time is 0.5(500 ms) which will be convert to frame number |
| 654 | * 30 if the frame rate is 60. or will result to frame number 15 if the frame rate is 30. |
| 655 | */ |
| 656 | inline int LOTLayerData::timeRemap(int frameNo) const |
| 657 | { |
| 658 | /* |
| 659 | * only consider startFrame() when there is no timeRemap. |
| 660 | * when a layer has timeremap bodymovin updates the startFrame() |
| 661 | * of all child layer so we don't have to take care of it. |
| 662 | */ |
| 663 | if (!mExtra || mExtra->mTimeRemap.isStatic()) |
| 664 | frameNo = frameNo - startFrame(); |
| 665 | else |
| 666 | frameNo = mExtra->mCompRef->frameAtTime(mExtra->mTimeRemap.value(frameNo)); |
| 667 | /* Apply time streatch if it has any. |
| 668 | * Time streatch is just a factor by which the animation will speedup or slow |
| 669 | * down with respect to the overal animation. |
| 670 | * Time streach factor is already applied to the layers inFrame and outFrame. |
| 671 | * @TODO need to find out if timestreatch also affects the in and out frame of the |
| 672 | * child layers or not. */ |
| 673 | return frameNo / mTimeStreatch; |
| 674 | } |
| 675 |
| 676 | class LOTFillData : public LOTData |
| 677 | { |
| 678 | public: |
| 679 | LOTFillData():LOTData(LOTData::Type::Fill){} |
| 680 | LottieColor color(int frameNo) const {return mColor.value(frameNo);} |
| 681 | float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;} |
| 682 | FillRule fillRule() const {return mFillRule;} |
| 683 | public: |
| 684 | FillRule mFillRule{FillRule::Winding}; /* "r" */ |
| 685 | LOTAnimatable<LottieColor> mColor; /* "c" */ |
| 686 | LOTAnimatable<float> mOpacity{100}; /* "o" */ |
| 687 | bool mEnabled{true}; /* "fillEnabled" */ |
| 688 | }; |
| 689 |
| 690 | struct LOTDashProperty |
| 691 | { |
| 692 | LOTAnimatable<float> mDashArray[5]; /* "d" "g" "o"*/ |
| 693 | int mDashCount{0}; |
| 694 | bool mStatic{true}; |
| 695 | }; |
| 696 |
| 697 | class LOTStrokeData : public LOTData |
| 698 | { |
| 699 | public: |
| 700 | LOTStrokeData():LOTData(LOTData::Type::Stroke){} |
| 701 | LottieColor color(int frameNo) const {return mColor.value(frameNo);} |
| 702 | float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;} |
| 703 | float strokeWidth(int frameNo) const {return mWidth.value(frameNo);} |
| 704 | CapStyle capStyle() const {return mCapStyle;} |
| 705 | JoinStyle joinStyle() const {return mJoinStyle;} |
| 706 | float meterLimit() const{return mMeterLimit;} |
| 707 | bool hasDashInfo() const { return !(mDash.mDashCount == 0);} |
| 708 | int getDashInfo(int frameNo, float *array) const; |
| 709 | public: |
| 710 | LOTAnimatable<LottieColor> mColor; /* "c" */ |
| 711 | LOTAnimatable<float> mOpacity{100}; /* "o" */ |
| 712 | LOTAnimatable<float> mWidth{0}; /* "w" */ |
| 713 | CapStyle mCapStyle{CapStyle::Flat}; /* "lc" */ |
| 714 | JoinStyle mJoinStyle{JoinStyle::Miter}; /* "lj" */ |
| 715 | float mMeterLimit{0}; /* "ml" */ |
| 716 | LOTDashProperty mDash; |
| 717 | bool mEnabled{true}; /* "fillEnabled" */ |
| 718 | }; |
| 719 |
| 720 | class LottieGradient |
| 721 | { |
| 722 | public: |
| 723 | friend inline LottieGradient operator+(const LottieGradient &g1, const LottieGradient &g2); |
| 724 | friend inline LottieGradient operator-(const LottieGradient &g1, const LottieGradient &g2); |
| 725 | friend inline LottieGradient operator*(float m, const LottieGradient &g); |
| 726 | public: |
| 727 | std::vector<float> mGradient; |
| 728 | }; |
| 729 |
| 730 | inline LottieGradient operator+(const LottieGradient &g1, const LottieGradient &g2) |
| 731 | { |
| 732 | if (g1.mGradient.size() != g2.mGradient.size()) |
| 733 | return g1; |
| 734 |
| 735 | LottieGradient newG; |
| 736 | newG.mGradient = g1.mGradient; |
| 737 |
| 738 | auto g2It = g2.mGradient.begin(); |
| 739 | for(auto &i : newG.mGradient) { |
| 740 | i = i + *g2It; |
| 741 | g2It++; |
| 742 | } |
| 743 |
| 744 | return newG; |
| 745 | } |
| 746 |
| 747 | inline LottieGradient operator-(const LottieGradient &g1, const LottieGradient &g2) |
| 748 | { |
| 749 | if (g1.mGradient.size() != g2.mGradient.size()) |
| 750 | return g1; |
| 751 | LottieGradient newG; |
| 752 | newG.mGradient = g1.mGradient; |
| 753 |
| 754 | auto g2It = g2.mGradient.begin(); |
| 755 | for(auto &i : newG.mGradient) { |
| 756 | i = i - *g2It; |
| 757 | g2It++; |
| 758 | } |
| 759 |
| 760 | return newG; |
| 761 | } |
| 762 |
| 763 | inline LottieGradient operator*(float m, const LottieGradient &g) |
| 764 | { |
| 765 | LottieGradient newG; |
| 766 | newG.mGradient = g.mGradient; |
| 767 |
| 768 | for(auto &i : newG.mGradient) { |
| 769 | i = i * m; |
| 770 | } |
| 771 | return newG; |
| 772 | } |
| 773 |
| 774 |
| 775 |
| 776 | class LOTGradient : public LOTData |
| 777 | { |
| 778 | public: |
| 779 | explicit LOTGradient(LOTData::Type type):LOTData(type){} |
| 780 | inline float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0;} |
| 781 | void update(std::unique_ptr<VGradient> &grad, int frameNo); |
| 782 |
| 783 | private: |
| 784 | void populate(VGradientStops &stops, int frameNo); |
| 785 | public: |
| 786 | int mGradientType{1}; /* "t" Linear=1 , Radial = 2*/ |
| 787 | LOTAnimatable<VPointF> mStartPoint; /* "s" */ |
| 788 | LOTAnimatable<VPointF> mEndPoint; /* "e" */ |
| 789 | LOTAnimatable<float> mHighlightLength{0}; /* "h" */ |
| 790 | LOTAnimatable<float> mHighlightAngle{0}; /* "a" */ |
| 791 | LOTAnimatable<float> mOpacity{100}; /* "o" */ |
| 792 | LOTAnimatable<LottieGradient> mGradient; /* "g" */ |
| 793 | int mColorPoints{-1}; |
| 794 | bool mEnabled{true}; /* "fillEnabled" */ |
| 795 | }; |
| 796 |
| 797 | class LOTGFillData : public LOTGradient |
| 798 | { |
| 799 | public: |
| 800 | LOTGFillData():LOTGradient(LOTData::Type::GFill){} |
| 801 | FillRule fillRule() const {return mFillRule;} |
| 802 | public: |
| 803 | FillRule mFillRule{FillRule::Winding}; /* "r" */ |
| 804 | }; |
| 805 |
| 806 | class LOTGStrokeData : public LOTGradient |
| 807 | { |
| 808 | public: |
| 809 | LOTGStrokeData():LOTGradient(LOTData::Type::GStroke){} |
| 810 | float width(int frameNo) const {return mWidth.value(frameNo);} |
| 811 | CapStyle capStyle() const {return mCapStyle;} |
| 812 | JoinStyle joinStyle() const {return mJoinStyle;} |
| 813 | float meterLimit() const{return mMeterLimit;} |
| 814 | bool hasDashInfo() const { return !(mDash.mDashCount == 0);} |
| 815 | int getDashInfo(int frameNo, float *array) const; |
| 816 | public: |
| 817 | LOTAnimatable<float> mWidth; /* "w" */ |
| 818 | CapStyle mCapStyle{CapStyle::Flat}; /* "lc" */ |
| 819 | JoinStyle mJoinStyle{JoinStyle::Miter}; /* "lj" */ |
| 820 | float mMeterLimit{0}; /* "ml" */ |
| 821 | LOTDashProperty mDash; |
| 822 | }; |
| 823 |
| 824 | class LOTPath : public LOTData |
| 825 | { |
| 826 | public: |
| 827 | explicit LOTPath(LOTData::Type type):LOTData(type){} |
| 828 | VPath::Direction direction() { if (mDirection == 3) return VPath::Direction::CCW; |
| 829 | else return VPath::Direction::CW;} |
| 830 | public: |
| 831 | int mDirection{1}; |
| 832 | }; |
| 833 |
| 834 | class LOTShapeData : public LOTPath |
| 835 | { |
| 836 | public: |
| 837 | LOTShapeData():LOTPath(LOTData::Type::Shape){} |
| 838 | void process(); |
| 839 | public: |
| 840 | LOTAnimatable<LottieShapeData> mShape; |
| 841 | }; |
| 842 |
| 843 | class LOTMaskData |
| 844 | { |
| 845 | public: |
| 846 | enum class Mode { |
| 847 | None, |
| 848 | Add, |
| 849 | Substarct, |
| 850 | Intersect, |
| 851 | Difference |
| 852 | }; |
| 853 | float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;} |
| 854 | bool isStatic() const {return mIsStatic;} |
| 855 | public: |
| 856 | LOTAnimatable<LottieShapeData> mShape; |
| 857 | LOTAnimatable<float> mOpacity{100}; |
| 858 | bool mInv{false}; |
| 859 | bool mIsStatic{true}; |
| 860 | LOTMaskData::Mode mMode; |
| 861 | }; |
| 862 |
| 863 | class LOTRectData : public LOTPath |
| 864 | { |
| 865 | public: |
| 866 | LOTRectData():LOTPath(LOTData::Type::Rect){} |
| 867 | public: |
| 868 | LOTAnimatable<VPointF> mPos; |
| 869 | LOTAnimatable<VPointF> mSize; |
| 870 | LOTAnimatable<float> mRound{0}; |
| 871 | }; |
| 872 |
| 873 | class LOTEllipseData : public LOTPath |
| 874 | { |
| 875 | public: |
| 876 | LOTEllipseData():LOTPath(LOTData::Type::Ellipse){} |
| 877 | public: |
| 878 | LOTAnimatable<VPointF> mPos; |
| 879 | LOTAnimatable<VPointF> mSize; |
| 880 | }; |
| 881 |
| 882 | class LOTPolystarData : public LOTPath |
| 883 | { |
| 884 | public: |
| 885 | enum class PolyType { |
| 886 | Star = 1, |
| 887 | Polygon = 2 |
| 888 | }; |
| 889 | LOTPolystarData():LOTPath(LOTData::Type::Polystar){} |
| 890 | public: |
| 891 | LOTPolystarData::PolyType mType{PolyType::Polygon}; |
| 892 | LOTAnimatable<VPointF> mPos; |
| 893 | LOTAnimatable<float> mPointCount{0}; |
| 894 | LOTAnimatable<float> mInnerRadius{0}; |
| 895 | LOTAnimatable<float> mOuterRadius{0}; |
| 896 | LOTAnimatable<float> mInnerRoundness{0}; |
| 897 | LOTAnimatable<float> mOuterRoundness{0}; |
| 898 | LOTAnimatable<float> mRotation{0}; |
| 899 | }; |
| 900 |
| 901 | class LOTTrimData : public LOTData |
| 902 | { |
| 903 | public: |
| 904 | struct Segment { |
| 905 | float start{0}; |
| 906 | float end{0}; |
| 907 | Segment() {} |
| 908 | Segment(float s, float e):start(s), end(e) {} |
| 909 | }; |
| 910 | enum class TrimType { |
| 911 | Simultaneously, |
| 912 | Individually |
| 913 | }; |
| 914 | LOTTrimData():LOTData(LOTData::Type::Trim){} |
| 915 | /* |
| 916 | * if start > end vector trims the path as a loop ( 2 segment) |
| 917 | * if start < end vector trims the path without loop ( 1 segment). |
| 918 | * if no offset then there is no loop. |
| 919 | */ |
| 920 | Segment segment(int frameNo) const { |
| 921 | float start = mStart.value(frameNo)/100.0f; |
| 922 | float end = mEnd.value(frameNo)/100.0f; |
| 923 | float offset = fmod(mOffset.value(frameNo), 360.0f)/ 360.0f; |
| 924 |
| 925 | float diff = fabs(start - end); |
| 926 | if (vCompare(diff, 0.0f)) return Segment(0, 0); |
| 927 | if (vCompare(diff, 1.0f)) return Segment(0, 1); |
| 928 |
| 929 | if (offset > 0) { |
| 930 | start += offset; |
| 931 | end += offset; |
| 932 | if (start <= 1 && end <=1) { |
| 933 | return noloop(start, end); |
| 934 | } else if (start > 1 && end > 1) { |
| 935 | return noloop(start - 1, end - 1); |
| 936 | } else { |
| 937 | if (start > 1) return loop(start - 1 , end); |
| 938 | else return loop(start , end - 1); |
| 939 | } |
| 940 | } else { |
| 941 | start += offset; |
| 942 | end += offset; |
| 943 | if (start >= 0 && end >= 0) { |
| 944 | return noloop(start, end); |
| 945 | } else if (start < 0 && end < 0) { |
| 946 | return noloop(1 + start, 1 + end); |
| 947 | } else { |
| 948 | if (start < 0) return loop(1 + start, end); |
| 949 | else return loop(start , 1 + end); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | LOTTrimData::TrimType type() const {return mTrimType;} |
| 954 | private: |
| 955 | Segment noloop(float start, float end) const{ |
| 956 | assert(start >= 0); |
| 957 | assert(end >= 0); |
| 958 | Segment s; |
| 959 | s.start = std::min(start, end); |
| 960 | s.end = std::max(start, end); |
| 961 | return s; |
| 962 | } |
| 963 | Segment loop(float start, float end) const{ |
| 964 | assert(start >= 0); |
| 965 | assert(end >= 0); |
| 966 | Segment s; |
| 967 | s.start = std::max(start, end); |
| 968 | s.end = std::min(start, end); |
| 969 | return s; |
| 970 | } |
| 971 | public: |
| 972 | LOTAnimatable<float> mStart{0}; |
| 973 | LOTAnimatable<float> mEnd{0}; |
| 974 | LOTAnimatable<float> mOffset{0}; |
| 975 | LOTTrimData::TrimType mTrimType{TrimType::Simultaneously}; |
| 976 | }; |
| 977 |
| 978 | class LOTRepeaterTransform |
| 979 | { |
| 980 | public: |
| 981 | VMatrix matrix(int frameNo, float multiplier) const; |
| 982 | float startOpacity(int frameNo) const { return mStartOpacity.value(frameNo)/100;} |
| 983 | float endOpacity(int frameNo) const { return mEndOpacity.value(frameNo)/100;} |
| 984 | bool isStatic() const |
| 985 | { |
| 986 | return mRotation.isStatic() && |
| 987 | mScale.isStatic() && |
| 988 | mPosition.isStatic() && |
| 989 | mAnchor.isStatic() && |
| 990 | mStartOpacity.isStatic() && |
| 991 | mEndOpacity.isStatic(); |
| 992 | } |
| 993 | public: |
| 994 | LOTAnimatable<float> mRotation{0}; /* "r" */ |
| 995 | LOTAnimatable<VPointF> mScale{{100, 100}}; /* "s" */ |
| 996 | LOTAnimatable<VPointF> mPosition; /* "p" */ |
| 997 | LOTAnimatable<VPointF> mAnchor; /* "a" */ |
| 998 | LOTAnimatable<float> mStartOpacity{100}; /* "so" */ |
| 999 | LOTAnimatable<float> mEndOpacity{100}; /* "eo" */ |
| 1000 | }; |
| 1001 |
| 1002 | class LOTRepeaterData : public LOTData |
| 1003 | { |
| 1004 | public: |
| 1005 | LOTRepeaterData():LOTData(LOTData::Type::Repeater){} |
| 1006 | LOTShapeGroupData *content() const { return mContent ? mContent.get() : nullptr; } |
| 1007 | void setContent(std::shared_ptr<LOTShapeGroupData> content) {mContent = std::move(content);} |
| 1008 | int maxCopies() const { return int(mMaxCopies);} |
| 1009 | float copies(int frameNo) const {return mCopies.value(frameNo);} |
| 1010 | float offset(int frameNo) const {return mOffset.value(frameNo);} |
| 1011 | public: |
| 1012 | std::shared_ptr<LOTShapeGroupData> mContent{nullptr}; |
| 1013 | LOTRepeaterTransform mTransform; |
| 1014 | LOTAnimatable<float> mCopies{0}; |
| 1015 | LOTAnimatable<float> mOffset{0}; |
| 1016 | float mMaxCopies{0.0}; |
| 1017 | }; |
| 1018 |
| 1019 | class LOTModel |
| 1020 | { |
| 1021 | public: |
| 1022 | bool isStatic() const {return mRoot->isStatic();} |
| 1023 | double duration() const {return mRoot->duration();} |
| 1024 | size_t totalFrame() const {return mRoot->totalFrame();} |
| 1025 | size_t frameDuration() const {return mRoot->frameDuration();} |
| 1026 | size_t frameRate() const {return mRoot->frameRate();} |
| 1027 | size_t startFrame() const {return mRoot->startFrame();} |
| 1028 | size_t endFrame() const {return mRoot->endFrame();} |
| 1029 | size_t frameAtPos(double pos) const {return mRoot->frameAtPos(pos);} |
| 1030 | const std::vector<LayerInfo> &layerInfoList() const { return mRoot->layerInfoList();} |
| 1031 | public: |
| 1032 | std::shared_ptr<LOTCompositionData> mRoot; |
| 1033 | }; |
| 1034 |
| 1035 | #endif // LOTModel_H |
| 1036 |