Debian Codegraph @

src/lottie/lottiemodel.h

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