Debian Codegraph @

src/lottie/lottiemodel.h

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