Debian Codegraph @

src/lottie/lottiemodel.h

raw ◀ ▶
Source reconstructed at patch state 6 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 private:
464 static constexpr unsigned char maxShortStringLength = 14;
465 void setShortString(bool value) {mData._shortString = value;}
466 bool shortString() const {return mData._shortString;}
467 struct Data{
468 char _buffer[maxShortStringLength];
469 LOTData::Type _type;
470 bool _static : 1;
471 bool _hidden : 1;
472 bool _shortString : 1;
473 };
474 union {
475 Data mData;
476 char *mPtr{nullptr};
477 };
478 };
479
480 class LOTGroupData: public LOTData
481 {
482 public:
483 explicit LOTGroupData(LOTData::Type type):LOTData(type){}
484 public:
485 std::vector<LOTData *> mChildren;
486 LOTTransformData *mTransform{nullptr};
487 };
488
489 class LOTShapeGroupData : public LOTGroupData
490 {
491 public:
492 LOTShapeGroupData():LOTGroupData(LOTData::Type::ShapeGroup){}
493 };
494
495 class LOTLayerData;
496 struct LOTAsset
497 {
498 enum class Type : unsigned char{
499 Precomp,
500 Image,
501 Char
502 };
503 bool isStatic() const {return mStatic;}
504 void setStatic(bool value) {mStatic = value;}
505 VBitmap bitmap() const {return mBitmap;}
506 void loadImageData(std::string data);
507 void loadImagePath(std::string Path);
508 Type mAssetType{Type::Precomp};
509 bool mStatic{true};
510 std::string mRefId; // ref id
511 std::vector<LOTData *> mLayers;
512 // image asset data
513 int mWidth{0};
514 int mHeight{0};
515 VBitmap mBitmap;
516 };
517
518 struct TransformDataExtra
519 {
520 LOTAnimatable<float> m3DRx{0};
521 LOTAnimatable<float> m3DRy{0};
522 LOTAnimatable<float> m3DRz{0};
523 LOTAnimatable<float> mSeparateX{0};
524 LOTAnimatable<float> mSeparateY{0};
525 bool mSeparate{false};
526 bool m3DData{false};
527 };
528
529 struct TransformData
530 {
531 VMatrix matrix(int frameNo, bool autoOrient = false) const;
532 float opacity(int frameNo) const { return mOpacity.value(frameNo)/100.0f; }
533 void createExtraData()
534 {
535 if (!mExtra) mExtra = std::make_unique<TransformDataExtra>();
536 }
537 LOTAnimatable<float> mRotation{0}; /* "r" */
538 LOTAnimatable<VPointF> mScale{{100, 100}}; /* "s" */
539 LOTAnimatable<VPointF> mPosition; /* "p" */
540 LOTAnimatable<VPointF> mAnchor; /* "a" */
541 LOTAnimatable<float> mOpacity{100}; /* "o" */
542 std::unique_ptr<TransformDataExtra> mExtra;
543 };
544
545 class LOTTransformData : public LOTData
546 {
547 public:
548 LOTTransformData():LOTData(LOTData::Type::Transform){}
549 void set(TransformData* data, bool staticFlag)
550 {
551 setStatic(staticFlag);
552 if (isStatic()) {
553 new (&impl.mStaticData) static_data(data->matrix(0), data->opacity(0));
554 } else {
555 impl.mData = data;
556 }
557 }
558 VMatrix matrix(int frameNo, bool autoOrient = false) const
559 {
560 if (isStatic()) return impl.mStaticData.mMatrix;
561 return impl.mData->matrix(frameNo, autoOrient);
562 }
563 float opacity(int frameNo) const
564 {
565 if (isStatic()) return impl.mStaticData.mOpacity;
566 return impl.mData->opacity(frameNo);
567 }
568 LOTTransformData(const LOTTransformData&) = delete;
569 LOTTransformData(LOTTransformData&&) = delete;
570 LOTTransformData& operator=(LOTTransformData&) = delete;
571 LOTTransformData& operator=(LOTTransformData&&) = delete;
572 ~LOTTransformData() {destroy();}
573
574 private:
575 void destroy() {
576 if (isStatic()) {
577 impl.mStaticData.~static_data();
578 }
579 }
580 struct static_data {
581 static_data(VMatrix &&m, float opacity):
582 mOpacity(opacity), mMatrix(std::move(m)){}
583 float mOpacity;
584 VMatrix mMatrix;
585 };
586 union details {
587 TransformData *mData{nullptr};
588 static_data mStaticData;
589 details(){};
590 details(const details&) = delete;
591 details(details&&) = delete;
592 details& operator=(details&&) = delete;
593 details& operator=(const details&) = delete;
594 ~details(){};
595 }impl;
596 };
597
598 struct ExtraLayerData
599 {
600 LottieColor mSolidColor;
601 std::string mPreCompRefId;
602 LOTAnimatable<float> mTimeRemap; /* "tm" */
603 LOTCompositionData *mCompRef{nullptr};
604 LOTAsset *mAsset{nullptr};
605 std::vector<LOTMaskData *> mMasks;
606 };
607
608 class LOTLayerData : public LOTGroupData
609 {
610 public:
611 LOTLayerData():LOTGroupData(LOTData::Type::Layer){}
612 bool hasPathOperator() const noexcept {return mHasPathOperator;}
613 bool hasGradient() const noexcept {return mHasGradient;}
614 bool hasMask() const noexcept {return mHasMask;}
615 bool hasRepeater() const noexcept {return mHasRepeater;}
616 int id() const noexcept{ return mId;}
617 int parentId() const noexcept{ return mParentId;}
618 bool hasParent() const noexcept {return mParentId != -1;}
619 int inFrame() const noexcept{return mInFrame;}
620 int outFrame() const noexcept{return mOutFrame;}
621 int startFrame() const noexcept{return mStartFrame;}
622 LottieColor solidColor() const noexcept
623 {
624 return mExtra ? mExtra->mSolidColor : LottieColor();
625 }
626 bool autoOrient() const noexcept{return mAutoOrient;}
627 int timeRemap(int frameNo) const;
628 VSize layerSize() const {return mLayerSize;}
629 bool precompLayer() const {return mLayerType == LayerType::Precomp;}
630 VMatrix matrix(int frameNo) const
631 {
632 return mTransform ? mTransform->matrix(frameNo, autoOrient()) : VMatrix{};
633 }
634 float opacity(int frameNo) const
635 {
636 return mTransform ? mTransform->opacity(frameNo) : 1.0f;
637 }
638 LOTAsset* asset() const {return mExtra ? mExtra->mAsset : nullptr;}
639 public:
640 ExtraLayerData* extra()
641 {
642 if (!mExtra) mExtra = std::make_unique<ExtraLayerData>();
643 return mExtra.get();
644 }
645 MatteType mMatteType{MatteType::None};
646 LayerType mLayerType{LayerType::Null};
647 LottieBlendMode mBlendMode{LottieBlendMode::Normal};
648 bool mHasPathOperator{false};
649 bool mHasMask{false};
650 bool mHasRepeater{false};
651 bool mHasGradient{false};
652 bool mAutoOrient{false};
653 VSize mLayerSize;
654 int mParentId{-1}; // Lottie the id of the parent in the composition
655 int mId{-1}; // Lottie the group id used for parenting.
656 float mTimeStreatch{1.0f};
657 int mInFrame{0};
658 int mOutFrame{0};
659 int mStartFrame{0};
660 std::unique_ptr<ExtraLayerData> mExtra{nullptr};
661 };
662
663 using Marker = std::tuple<std::string, int , int>;
664 using LayerInfo = Marker;
665
666 class LOTCompositionData : public LOTData
667 {
668 public:
669 LOTCompositionData():LOTData(LOTData::Type::Composition){}
670 std::vector<LayerInfo> layerInfoList() const;
671 const std::vector<Marker> &markers() const { return mMarkers;}
672 double duration() const {
673 return frameDuration() / frameRate(); // in second
674 }
675 size_t frameAtPos(double pos) const {
676 if (pos < 0) pos = 0;
677 if (pos > 1) pos = 1;
678 return size_t(pos * frameDuration());
679 }
680 long frameAtTime(double timeInSec) const {
681 return long(frameAtPos(timeInSec / duration()));
682 }
683 size_t totalFrame() const {return mEndFrame - mStartFrame;}
684 long frameDuration() const {return mEndFrame - mStartFrame -1;}
685 float frameRate() const {return mFrameRate;}
686 long startFrame() const {return mStartFrame;}
687 long endFrame() const {return mEndFrame;}
688 VSize size() const {return mSize;}
689 void processRepeaterObjects();
690 void updateStats();
691 public:
692 std::string mVersion;
693 VSize mSize;
694 long mStartFrame{0};
695 long mEndFrame{0};
696 float mFrameRate{60};
697 LottieBlendMode mBlendMode{LottieBlendMode::Normal};
698 LOTLayerData *mRootLayer{nullptr};
699 std::unordered_map<std::string,
700 LOTAsset*> mAssets;
701
702 std::vector<Marker> mMarkers;
703 VArenaAlloc mArenaAlloc{2048};
704 LOTModelStat mStats;
705 };
706
707 /**
708 * TimeRemap has the value in time domain(in sec)
709 * To get the proper mapping first we get the mapped time at the current frame Number
710 * then we need to convert mapped time to frame number using the composition time line
711 * Ex: at frame 10 the mappend time is 0.5(500 ms) which will be convert to frame number
712 * 30 if the frame rate is 60. or will result to frame number 15 if the frame rate is 30.
713 */
714 inline int LOTLayerData::timeRemap(int frameNo) const
715 {
716 /*
717 * only consider startFrame() when there is no timeRemap.
718 * when a layer has timeremap bodymovin updates the startFrame()
719 * of all child layer so we don't have to take care of it.
720 */
721 if (!mExtra || mExtra->mTimeRemap.isStatic())
722 frameNo = frameNo - startFrame();
723 else
724 frameNo = mExtra->mCompRef->frameAtTime(mExtra->mTimeRemap.value(frameNo));
725 /* Apply time streatch if it has any.
726 * Time streatch is just a factor by which the animation will speedup or slow
727 * down with respect to the overal animation.
728 * Time streach factor is already applied to the layers inFrame and outFrame.
729 * @TODO need to find out if timestreatch also affects the in and out frame of the
730 * child layers or not. */
731 return int(frameNo / mTimeStreatch);
732 }
733
734 class LOTFillData : public LOTData
735 {
736 public:
737 LOTFillData():LOTData(LOTData::Type::Fill){}
738 LottieColor color(int frameNo) const {return mColor.value(frameNo);}
739 float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;}
740 FillRule fillRule() const {return mFillRule;}
741 public:
742 FillRule mFillRule{FillRule::Winding}; /* "r" */
743 bool mEnabled{true}; /* "fillEnabled" */
744 LOTAnimatable<LottieColor> mColor; /* "c" */
745 LOTAnimatable<float> mOpacity{100}; /* "o" */
746 };
747
748 struct LOTDashProperty
749 {
750 std::vector<LOTAnimatable<float>> mData;
751 bool empty() const {return mData.empty();}
752 size_t size() const {return mData.size();}
753 bool isStatic() const {
754 for(const auto &elm : mData)
755 if (!elm.isStatic()) return false;
756 return true;
757 }
758 void getDashInfo(int frameNo, std::vector<float>& result) const;
759 };
760
761 class LOTStrokeData : public LOTData
762 {
763 public:
764 LOTStrokeData():LOTData(LOTData::Type::Stroke){}
765 LottieColor color(int frameNo) const {return mColor.value(frameNo);}
766 float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;}
767 float strokeWidth(int frameNo) const {return mWidth.value(frameNo);}
768 CapStyle capStyle() const {return mCapStyle;}
769 JoinStyle joinStyle() const {return mJoinStyle;}
770 float miterLimit() const{return mMiterLimit;}
771 bool hasDashInfo() const {return !mDash.empty();}
772 void getDashInfo(int frameNo, std::vector<float>& result) const
773 {
774 return mDash.getDashInfo(frameNo, result);
775 }
776 public:
777 LOTAnimatable<LottieColor> mColor; /* "c" */
778 LOTAnimatable<float> mOpacity{100}; /* "o" */
779 LOTAnimatable<float> mWidth{0}; /* "w" */
780 CapStyle mCapStyle{CapStyle::Flat}; /* "lc" */
781 JoinStyle mJoinStyle{JoinStyle::Miter}; /* "lj" */
782 float mMiterLimit{0}; /* "ml" */
783 LOTDashProperty mDash;
784 bool mEnabled{true}; /* "fillEnabled" */
785 };
786
787 class LottieGradient
788 {
789 public:
790 friend inline LottieGradient operator+(const LottieGradient &g1, const LottieGradient &g2);
791 friend inline LottieGradient operator-(const LottieGradient &g1, const LottieGradient &g2);
792 friend inline LottieGradient operator*(float m, const LottieGradient &g);
793 public:
794 std::vector<float> mGradient;
795 };
796
797 inline LottieGradient operator+(const LottieGradient &g1, const LottieGradient &g2)
798 {
799 if (g1.mGradient.size() != g2.mGradient.size())
800 return g1;
801
802 LottieGradient newG;
803 newG.mGradient = g1.mGradient;
804
805 auto g2It = g2.mGradient.begin();
806 for(auto &i : newG.mGradient) {
807 i = i + *g2It;
808 g2It++;
809 }
810
811 return newG;
812 }
813
814 inline LottieGradient operator-(const LottieGradient &g1, const LottieGradient &g2)
815 {
816 if (g1.mGradient.size() != g2.mGradient.size())
817 return g1;
818 LottieGradient newG;
819 newG.mGradient = g1.mGradient;
820
821 auto g2It = g2.mGradient.begin();
822 for(auto &i : newG.mGradient) {
823 i = i - *g2It;
824 g2It++;
825 }
826
827 return newG;
828 }
829
830 inline LottieGradient operator*(float m, const LottieGradient &g)
831 {
832 LottieGradient newG;
833 newG.mGradient = g.mGradient;
834
835 for(auto &i : newG.mGradient) {
836 i = i * m;
837 }
838 return newG;
839 }
840
841
842
843 class LOTGradient : public LOTData
844 {
845 public:
846 explicit LOTGradient(LOTData::Type type):LOTData(type){}
847 inline float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;}
848 void update(std::unique_ptr<VGradient> &grad, int frameNo);
849
850 private:
851 void populate(VGradientStops &stops, int frameNo);
852 public:
853 int mGradientType{1}; /* "t" Linear=1 , Radial = 2*/
854 LOTAnimatable<VPointF> mStartPoint; /* "s" */
855 LOTAnimatable<VPointF> mEndPoint; /* "e" */
856 LOTAnimatable<float> mHighlightLength{0}; /* "h" */
857 LOTAnimatable<float> mHighlightAngle{0}; /* "a" */
858 LOTAnimatable<float> mOpacity{100}; /* "o" */
859 LOTAnimatable<LottieGradient> mGradient; /* "g" */
860 int mColorPoints{-1};
861 bool mEnabled{true}; /* "fillEnabled" */
862 };
863
864 class LOTGFillData : public LOTGradient
865 {
866 public:
867 LOTGFillData():LOTGradient(LOTData::Type::GFill){}
868 FillRule fillRule() const {return mFillRule;}
869 public:
870 FillRule mFillRule{FillRule::Winding}; /* "r" */
871 };
872
873 class LOTGStrokeData : public LOTGradient
874 {
875 public:
876 LOTGStrokeData():LOTGradient(LOTData::Type::GStroke){}
877 float width(int frameNo) const {return mWidth.value(frameNo);}
878 CapStyle capStyle() const {return mCapStyle;}
879 JoinStyle joinStyle() const {return mJoinStyle;}
880 float miterLimit() const{return mMiterLimit;}
881 bool hasDashInfo() const {return !mDash.empty();}
882 void getDashInfo(int frameNo, std::vector<float>& result) const
883 {
884 return mDash.getDashInfo(frameNo, result);
885 }
886 public:
887 LOTAnimatable<float> mWidth; /* "w" */
888 CapStyle mCapStyle{CapStyle::Flat}; /* "lc" */
889 JoinStyle mJoinStyle{JoinStyle::Miter}; /* "lj" */
890 float mMiterLimit{0}; /* "ml" */
891 LOTDashProperty mDash;
892 };
893
894 class LOTPath : public LOTData
895 {
896 public:
897 explicit LOTPath(LOTData::Type type):LOTData(type){}
898 VPath::Direction direction() {
899 return (mDirection == 3) ?
900 VPath::Direction::CCW : VPath::Direction::CW;
901 }
902 public:
903 int mDirection{1};
904 };
905
906 class LOTShapeData : public LOTPath
907 {
908 public:
909 LOTShapeData():LOTPath(LOTData::Type::Shape){}
910 public:
911 LOTAnimatableShape mShape;
912 };
913
914 class LOTMaskData
915 {
916 public:
917 enum class Mode {
918 None,
919 Add,
920 Substarct,
921 Intersect,
922 Difference
923 };
924 float opacity(int frameNo) const {return mOpacity.value(frameNo)/100.0f;}
925 bool isStatic() const {return mIsStatic;}
926 public:
927 LOTAnimatableShape mShape;
928 LOTAnimatable<float> mOpacity{100};
929 bool mInv{false};
930 bool mIsStatic{true};
931 LOTMaskData::Mode mMode;
932 };
933
934 class LOTRectData : public LOTPath
935 {
936 public:
937 LOTRectData():LOTPath(LOTData::Type::Rect){}
938 public:
939 LOTAnimatable<VPointF> mPos;
940 LOTAnimatable<VPointF> mSize;
941 LOTAnimatable<float> mRound{0};
942 };
943
944 class LOTEllipseData : public LOTPath
945 {
946 public:
947 LOTEllipseData():LOTPath(LOTData::Type::Ellipse){}
948 public:
949 LOTAnimatable<VPointF> mPos;
950 LOTAnimatable<VPointF> mSize;
951 };
952
953 class LOTPolystarData : public LOTPath
954 {
955 public:
956 enum class PolyType {
957 Star = 1,
958 Polygon = 2
959 };
960 LOTPolystarData():LOTPath(LOTData::Type::Polystar){}
961 public:
962 LOTPolystarData::PolyType mPolyType{PolyType::Polygon};
963 LOTAnimatable<VPointF> mPos;
964 LOTAnimatable<float> mPointCount{0};
965 LOTAnimatable<float> mInnerRadius{0};
966 LOTAnimatable<float> mOuterRadius{0};
967 LOTAnimatable<float> mInnerRoundness{0};
968 LOTAnimatable<float> mOuterRoundness{0};
969 LOTAnimatable<float> mRotation{0};
970 };
971
972 class LOTTrimData : public LOTData
973 {
974 public:
975 struct Segment {
976 float start{0};
977 float end{0};
978 Segment() = default;
979 explicit Segment(float s, float e):start(s), end(e) {}
980 };
981 enum class TrimType {
982 Simultaneously,
983 Individually
984 };
985 LOTTrimData():LOTData(LOTData::Type::Trim){}
986 /*
987 * if start > end vector trims the path as a loop ( 2 segment)
988 * if start < end vector trims the path without loop ( 1 segment).
989 * if no offset then there is no loop.
990 */
991 Segment segment(int frameNo) const {
992 float start = mStart.value(frameNo)/100.0f;
993 float end = mEnd.value(frameNo)/100.0f;
994 float offset = std::fmod(mOffset.value(frameNo), 360.0f)/ 360.0f;
995
996 float diff = std::abs(start - end);
997 if (vCompare(diff, 0.0f)) return Segment(0, 0);
998 if (vCompare(diff, 1.0f)) return Segment(0, 1);
999
1000 if (offset > 0) {
1001 start += offset;
1002 end += offset;
1003 if (start <= 1 && end <=1) {
1004 return noloop(start, end);
1005 } else if (start > 1 && end > 1) {
1006 return noloop(start - 1, end - 1);
1007 } else {
1008 return (start > 1) ?
1009 loop(start - 1 , end) : loop(start , end - 1);
1010 }
1011 } else {
1012 start += offset;
1013 end += offset;
1014 if (start >= 0 && end >= 0) {
1015 return noloop(start, end);
1016 } else if (start < 0 && end < 0) {
1017 return noloop(1 + start, 1 + end);
1018 } else {
1019 return (start < 0) ?
1020 loop(1 + start, end) : loop(start , 1 + end);
1021 }
1022 }
1023 }
1024 LOTTrimData::TrimType type() const {return mTrimType;}
1025 private:
1026 Segment noloop(float start, float end) const{
1027 assert(start >= 0);
1028 assert(end >= 0);
1029 Segment s;
1030 s.start = std::min(start, end);
1031 s.end = std::max(start, end);
1032 return s;
1033 }
1034 Segment loop(float start, float end) const{
1035 assert(start >= 0);
1036 assert(end >= 0);
1037 Segment s;
1038 s.start = std::max(start, end);
1039 s.end = std::min(start, end);
1040 return s;
1041 }
1042 public:
1043 LOTAnimatable<float> mStart{0};
1044 LOTAnimatable<float> mEnd{0};
1045 LOTAnimatable<float> mOffset{0};
1046 LOTTrimData::TrimType mTrimType{TrimType::Simultaneously};
1047 };
1048
1049 class LOTRepeaterTransform
1050 {
1051 public:
1052 VMatrix matrix(int frameNo, float multiplier) const;
1053 float startOpacity(int frameNo) const { return mStartOpacity.value(frameNo)/100;}
1054 float endOpacity(int frameNo) const { return mEndOpacity.value(frameNo)/100;}
1055 bool isStatic() const
1056 {
1057 return mRotation.isStatic() &&
1058 mScale.isStatic() &&
1059 mPosition.isStatic() &&
1060 mAnchor.isStatic() &&
1061 mStartOpacity.isStatic() &&
1062 mEndOpacity.isStatic();
1063 }
1064 public:
1065 LOTAnimatable<float> mRotation{0}; /* "r" */
1066 LOTAnimatable<VPointF> mScale{{100, 100}}; /* "s" */
1067 LOTAnimatable<VPointF> mPosition; /* "p" */
1068 LOTAnimatable<VPointF> mAnchor; /* "a" */
1069 LOTAnimatable<float> mStartOpacity{100}; /* "so" */
1070 LOTAnimatable<float> mEndOpacity{100}; /* "eo" */
1071 };
1072
1073 class LOTRepeaterData : public LOTData
1074 {
1075 public:
1076 LOTRepeaterData():LOTData(LOTData::Type::Repeater){}
1077 LOTShapeGroupData *content() const { return mContent ? mContent : nullptr; }
1078 void setContent(LOTShapeGroupData *content) {mContent = content;}
1079 int maxCopies() const { return int(mMaxCopies);}
1080 float copies(int frameNo) const {return mCopies.value(frameNo);}
1081 float offset(int frameNo) const {return mOffset.value(frameNo);}
1082 bool processed() const {return mProcessed;}
1083 void markProcessed() {mProcessed = true;}
1084 public:
1085 LOTShapeGroupData* mContent{nullptr};
1086 LOTRepeaterTransform mTransform;
1087 LOTAnimatable<float> mCopies{0};
1088 LOTAnimatable<float> mOffset{0};
1089 float mMaxCopies{0.0};
1090 bool mProcessed{false};
1091 };
1092
1093 class LOTModel
1094 {
1095 public:
1096 bool isStatic() const {return mRoot->isStatic();}
1097 VSize size() const {return mRoot->size();}
1098 double duration() const {return mRoot->duration();}
1099 size_t totalFrame() const {return mRoot->totalFrame();}
1100 size_t frameDuration() const {return mRoot->frameDuration();}
1101 double frameRate() const {return mRoot->frameRate();}
1102 size_t startFrame() const {return mRoot->startFrame();}
1103 size_t endFrame() const {return mRoot->endFrame();}
1104 size_t frameAtPos(double pos) const {return mRoot->frameAtPos(pos);}
1105 std::vector<LayerInfo> layerInfoList() const { return mRoot->layerInfoList();}
1106 const std::vector<Marker> &markers() const { return mRoot->markers();}
1107 public:
1108 std::shared_ptr<LOTCompositionData> mRoot;
1109 };
1110
1111 #endif // LOTModel_H