Debian Codegraph @

src/lottie/lottiemodel.h

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