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