summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsubhransu mohanty <sub.mohanty@samsung.com>2017-12-14 18:09:49 +0900
committersubhransu mohanty <sub.mohanty@samsung.com>2018-04-12 13:51:52 +0900
commit721f5f1a5f655d4854ebd9c776221322a15c14b1 (patch)
tree284102feef8785272ba529a5420cce40a2f967fb
parent322da5c1f1595762f4dedf5564492933e33a5e0d (diff)
ssg: added lottie model class
-rw-r--r--ssg/include/sgpoint.h4
-rw-r--r--ssg/src/lottie/jsontest.cpp156
-rw-r--r--ssg/src/lottie/lottiecomposition.h8
-rw-r--r--ssg/src/lottie/lottiemodel.h113
4 files changed, 278 insertions, 3 deletions
diff --git a/ssg/include/sgpoint.h b/ssg/include/sgpoint.h
index d00bbec3ae..90a9205dc3 100644
--- a/ssg/include/sgpoint.h
+++ b/ssg/include/sgpoint.h
@@ -10,6 +10,8 @@ public:
10 constexpr inline SGPointF(double x, double y) noexcept :mx(x), my(y){} 10 constexpr inline SGPointF(double x, double y) noexcept :mx(x), my(y){}
11 constexpr inline double x() const noexcept {return mx;} 11 constexpr inline double x() const noexcept {return mx;}
12 constexpr inline double y() const noexcept {return my;} 12 constexpr inline double y() const noexcept {return my;}
13 inline void setX(float x) {mx = x;}
14 inline void setY(float y) {my = y;}
13 inline SGPointF &operator+=(const SGPointF &p) noexcept; 15 inline SGPointF &operator+=(const SGPointF &p) noexcept;
14 inline SGPointF &operator-=(const SGPointF &p) noexcept; 16 inline SGPointF &operator-=(const SGPointF &p) noexcept;
15private: 17private:
@@ -30,6 +32,8 @@ public:
30 constexpr inline SGPoint(int x, int y) noexcept :mx(x), my(y){} 32 constexpr inline SGPoint(int x, int y) noexcept :mx(x), my(y){}
31 constexpr inline int x() const noexcept {return mx;} 33 constexpr inline int x() const noexcept {return mx;}
32 constexpr inline int y() const noexcept {return my;} 34 constexpr inline int y() const noexcept {return my;}
35 inline void setX(int x) {mx = x;}
36 inline void setY(int y) {my = y;}
33 inline SGPoint &operator+=(const SGPoint &p) noexcept; 37 inline SGPoint &operator+=(const SGPoint &p) noexcept;
34 inline SGPoint &operator-=(const SGPoint &p) noexcept; 38 inline SGPoint &operator-=(const SGPoint &p) noexcept;
35 constexpr inline bool operator==(const SGPoint &o) const; 39 constexpr inline bool operator==(const SGPoint &o) const;
diff --git a/ssg/src/lottie/jsontest.cpp b/ssg/src/lottie/jsontest.cpp
index 43fd408d36..c49a630cfa 100644
--- a/ssg/src/lottie/jsontest.cpp
+++ b/ssg/src/lottie/jsontest.cpp
@@ -2,6 +2,7 @@
2#include "rapidjson/document.h" 2#include "rapidjson/document.h"
3#include <iostream> 3#include <iostream>
4#include "lottiecomposition.h" 4#include "lottiecomposition.h"
5#include "lottiemodel.h"
5 6
6RAPIDJSON_DIAG_PUSH 7RAPIDJSON_DIAG_PUSH
7#ifdef __GNUC__ 8#ifdef __GNUC__
@@ -102,6 +103,34 @@ void LookaheadParserHandler::ParseNext() {
102 } 103 }
103} 104}
104 105
106template <typename T>
107struct LottiePropertyHelper
108{
109 LottiePropertyHelper(const T &value):mAnimation(false), mStatic(value){}
110 bool mAnimation;
111 LottieProperty<T, false> mStatic;
112 LottieProperty<T,true> mDyanmic;
113};
114
115struct LottieFloatPropertyHelper
116{
117 LottieFloatPropertyHelper(float initialValue):
118 mAnimation(false), mStatic(initialValue){}
119 bool mAnimation;
120 LottieFloatProperty<false> mStatic;
121 LottieFloatProperty<true> mDyanmic;
122};
123
124struct LottiePointFPropertyHelper
125{
126 LottiePointFPropertyHelper(const SGPointF &initialValue):
127 mAnimation(false), mStatic(initialValue){}
128 bool mAnimation;
129 LottiePointFProperty<false> mStatic;
130 LottiePointFProperty<true> mDyanmic;
131};
132
133
105class LottieParser : protected LookaheadParserHandler { 134class LottieParser : protected LookaheadParserHandler {
106public: 135public:
107 LottieParser(char* str) : LookaheadParserHandler(str) {} 136 LottieParser(char* str) : LookaheadParserHandler(str) {}
@@ -132,6 +161,13 @@ public:
132 void parseItems1(FINode *layer); 161 void parseItems1(FINode *layer);
133 void parseGroupItem(FINode *group); 162 void parseGroupItem(FINode *group);
134 void parseEllipse(FINode *parent); 163 void parseEllipse(FINode *parent);
164 void parseRectObject();
165 void parseArrayValue(SGPointF &pt);
166 void parseArrayValue(float &val);
167 template<typename T>
168 void parseKeyFrame(LottieProperty<T,true> &obj);
169 template<typename T>
170 void parseProperty(LottiePropertyHelper<T> &obj);
135 171
136protected: 172protected:
137 void SkipOut(int depth); 173 void SkipOut(int depth);
@@ -444,10 +480,11 @@ void LottieParser::parseItems(FINode *parent)
444 while (const char* key = NextObjectKey()) { 480 while (const char* key = NextObjectKey()) {
445 if (0 == strcmp(key, "ty")) { 481 if (0 == strcmp(key, "ty")) {
446 const char *type = GetString(); 482 const char *type = GetString();
483 sgDebug<<"Shape type :"<<type;
447 if (0 == strcmp(type, "gr")) { 484 if (0 == strcmp(type, "gr")) {
448 nodeType = FINode::Type::Group; 485 nodeType = FINode::Type::Group;
449 } else if (0 == strcmp(type, "rc")) { 486 } else if (0 == strcmp(type, "rc")) {
450 nodeType = FINode::Type::Rect; 487 parseRectObject();
451 } else if (0 == strcmp(type, "el")) { 488 } else if (0 == strcmp(type, "el")) {
452 parseEllipse(parent); 489 parseEllipse(parent);
453 } else if (0 == strcmp(type, "sh")) { 490 } else if (0 == strcmp(type, "sh")) {
@@ -471,6 +508,111 @@ void LottieParser::parseItems(FINode *parent)
471 sgDebug<<"EXIT Parse item"; 508 sgDebug<<"EXIT Parse item";
472} 509}
473 510
511/*
512 * https://github.com/airbnb/lottie-web/blob/master/docs/json/shapes/rect.json
513 */
514void LottieParser::parseRectObject()
515{
516 LottiePropertyHelper<float> roundness(0);
517 while (const char* key = NextObjectKey()) {
518 if (0 == strcmp(key, "p")) {
519 Skip(key);
520 } else if (0 == strcmp(key, "s")) {
521 Skip(key);
522 } else if (0 == strcmp(key, "r")) {
523 parseProperty(roundness);
524 } else if (0 == strcmp(key, "d")) {
525 Skip(key);
526 } else {
527 Skip(key);
528 }
529 }
530}
531
532void LottieParser::parseArrayValue(SGPointF &pt)
533{
534 float val[10];
535 int i=0;
536 while (NextArrayValue()) {
537 val[i++] = GetDouble();
538 }
539 sgDebug<<"Value parsed as point / size"<<i;
540
541 pt.setX(val[0]);
542 pt.setX(val[1]);
543}
544
545void LottieParser::parseArrayValue(float &val)
546{
547 sgDebug<<"Value parsed as single val";
548 val = GetDouble();
549}
550
551template<typename T>
552void LottieParser::parseKeyFrame(LottieProperty<T,true> &obj)
553{
554 EnterObject();
555 LottieKeyFrame<T> keyframe;
556 while (const char* key = NextObjectKey()) {
557 if (0 == strcmp(key, "i")) {
558 sgDebug<<"i";
559 Skip(key);
560 } else if (0 == strcmp(key, "i")) {
561 sgDebug<<"o";
562 Skip(key);
563 } else if (0 == strcmp(key, "n")) {
564 sgDebug<<"n";
565 Skip(key);
566 } else if (0 == strcmp(key, "t")) {
567 sgDebug<<"t";
568 Skip(key);
569 } else if (0 == strcmp(key, "s")) {
570 if (PeekType() == kArrayType)
571 EnterArray();
572 parseArrayValue(keyframe.mStartValue);
573 sgDebug<<" S consumed";
574 } else if (0 == strcmp(key, "e")) {
575 if (PeekType() == kArrayType)
576 EnterArray();
577 parseArrayValue(keyframe.mEndValue);
578 sgDebug<<"E consumed";
579 } else {
580 Skip(key);
581 }
582 }
583 obj.mKeyFrames.push_back(keyframe);
584}
585
586template<typename T>
587void LottieParser::parseProperty(LottiePropertyHelper<T> &obj)
588{
589 EnterObject();
590 while (const char* key = NextObjectKey()) {
591 if (0 == strcmp(key, "a")) {
592 obj.mAnimation = GetBool();
593 sgDebug<<"animation property :"<< obj.mAnimation;
594 } else if (0 == strcmp(key, "k")) {
595 RAPIDJSON_ASSERT(PeekType() == kArrayType);
596 EnterArray();
597 while (NextArrayValue()) {
598 // for key frame
599 if (PeekType() == kObjectType) {
600 obj.mAnimation = true;
601 parseKeyFrame(obj.mDyanmic);
602 } else if (PeekType() == kNumberType) {
603 parseArrayValue(obj.mStatic.mValue);
604 } else {
605 sgDebug<<"Something is really wrong here ++++++++";
606 Skip(nullptr);
607 }
608 }
609 } else {
610 sgDebug<<"PointFProperty ignored :";
611 Skip(key);
612 }
613 }
614}
615
474void LottieParser::parseGroupItem(FINode *parent) 616void LottieParser::parseGroupItem(FINode *parent)
475{ 617{
476 FINode *group = new FIGroupNode(parent); 618 FINode *group = new FIGroupNode(parent);
@@ -483,9 +625,17 @@ void LottieParser::parseGroupItem(FINode *parent)
483 625
484void LottieParser::parseEllipse(FINode *parent) 626void LottieParser::parseEllipse(FINode *parent)
485{ 627{
486 FIEllipseNode *ellipse = new FIEllipseNode(parent); 628 sgDebug<<"parse el item :";
629 LottiePropertyHelper<SGPointF> pos = LottiePropertyHelper<SGPointF>(SGPointF());
630 LottiePropertyHelper<SGPointF> size = LottiePropertyHelper<SGPointF>(SGPointF());
487 while (const char* key = NextObjectKey()) { 631 while (const char* key = NextObjectKey()) {
488 Skip(key); 632 if (0 == strcmp(key, "p")) {
633 parseProperty(pos);
634 } else if (0 == strcmp(key, "s")) {
635 parseProperty(size);
636 } else {
637 Skip(key);
638 }
489 } 639 }
490} 640}
491 641
diff --git a/ssg/src/lottie/lottiecomposition.h b/ssg/src/lottie/lottiecomposition.h
index f24d13566d..870625946a 100644
--- a/ssg/src/lottie/lottiecomposition.h
+++ b/ssg/src/lottie/lottiecomposition.h
@@ -7,6 +7,8 @@
7#include"sgdebug.h" 7#include"sgdebug.h"
8#include<vector> 8#include<vector>
9 9
10#include"lottiemodel.h"
11
10// Format Indipendent class to generate data after parsing svg file 12// Format Indipendent class to generate data after parsing svg file
11 13
12struct FIDefNode; 14struct FIDefNode;
@@ -156,6 +158,12 @@ struct FIRectNode : public FINode
156 float mRadiusY; 158 float mRadiusY;
157}; 159};
158 160
161template<bool sizeAnim>
162struct LNode : public FINode
163{
164 LottieIntProperty<sizeAnim> mSizeProperty;
165};
166
159struct FILineNode : public FINode 167struct FILineNode : public FINode
160{ 168{
161 FILineNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Line;} 169 FILineNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Line;}
diff --git a/ssg/src/lottie/lottiemodel.h b/ssg/src/lottie/lottiemodel.h
new file mode 100644
index 0000000000..0825b7cbd7
--- /dev/null
+++ b/ssg/src/lottie/lottiemodel.h
@@ -0,0 +1,113 @@
1#ifndef LOTTIEMODEL_H
2#define LOTTIEMODEL_H
3
4#include<vector>
5#include"sgpoint.h"
6
7template<typename T>
8class LottieKeyFrame
9{
10public:
11 T mStartValue;
12 T mEndValue;
13 int mStartFrame;
14 int mEndFrame;
15};
16
17template<typename T, bool animation = true>
18class LottieProperty
19{
20public:
21 LottieProperty(){}
22 constexpr bool isStatic() const {return false;}
23public:
24 std::vector<LottieKeyFrame<T>> mKeyFrames;
25};
26
27template<typename T>
28class LottieProperty<T, false>
29{
30public:
31 LottieProperty(){}
32 LottieProperty<T,false>(T initialvalue): mValue(initialvalue){}
33 constexpr bool isStatic() const {return true;}
34public:
35 T mValue;
36};
37
38// Template aliasing for easy of use.
39template<bool animation>
40using LottieIntProperty = LottieProperty<int, animation>;
41
42template<bool animation>
43using LottieFloatProperty = LottieProperty<float, animation>;
44
45template<bool animation>
46using LottiePointFProperty = LottieProperty<SGPointF, animation>;
47
48class LottieObject
49{
50public:
51 enum class Type {
52 Composition,
53 Layer,
54 Group,
55 Fill,
56 Stroke,
57 GFill,
58 GStroke,
59 Rect,
60 Ellipse,
61 Shape,
62 Star
63 };
64
65 bool isStatic();
66public:
67 LottieObject::Type mType;
68};
69
70class LottieGroup : public LottieObject
71{
72public:
73 std::vector<LottieObject *> mChildren;
74 int mParentId; // Lottie the id of the parent in the composition
75 int mId; // Lottie the group id used for parenting.
76};
77
78//class LottieComposition : public LottieGroupObject
79//{
80//public:
81
82//};
83
84class LottieLayer : public LottieGroup
85{
86public:
87 int mlayerType; //lottie layer type (solid/shape/precomp)
88};
89
90class LottieMatrix : public LottieGroup
91{
92public:
93 LottieFloatProperty<true> mRotation; /* "r" */
94 LottiePointFProperty<true> mScale; /* "s" */
95 LottiePointFProperty<true> mPosition; /* "p" */
96 LottiePointFProperty<true> mAnchor; /* "a" */
97 LottieFloatProperty<true> mOpacity; /* "o" */
98 LottiePointFProperty<true> mSkew; /* "sk" */
99 LottieFloatProperty<true> mSkewAxis; /* "sa" */
100};
101
102template<bool pos, bool size, bool roundness>
103class LottieRect : public LottieObject
104{
105public:
106 LottiePointFProperty<pos> mPos;
107 LottiePointFProperty<size> mSize;
108 LottieFloatProperty<roundness> mRound;
109};
110
111
112
113#endif // LOTTIEMODEL_H