summaryrefslogtreecommitdiff
path: root/ssg/src/lottie/lottiecomposition.h
blob: 870625946ab66a2aded6095da2ac0e0f7a2e296d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef LOTTIECOMPOSITION_H
#define LOTTIECOMPOSITION_H

#include"rapidjson/document.h"
#include"sgrect.h"
#include"sgpoint.h"
#include"sgdebug.h"
#include<vector>

#include"lottiemodel.h"

// Format Indipendent class to generate data after parsing svg file

struct FIDefNode;
struct FIGradinet;
struct FIColor
{
    int red;
    int green;
    int blue;
};

struct FIPaint
{
   FIColor       mColor;
   bool          mNone;
   bool          mCurColor;
   FIGradinet   *mGradient;
   std::string   mRef;
};

struct FIFill
{
    enum class Mode {
        OddEven,
        Winding
    };
    enum class Flag {
        Paint    = 0x1,
        Opacity  = 0x2,
        Gradient = 0x4,
        FillRule = 0x8
    };
    FIFill::Flag   mFlag;
    FIPaint        mPaint;
    int            mOpacity;
    FIFill::Mode   mMode;
};

struct FIStroke
{
    struct Dash {

    };
    enum class Cap {
      Butt,
      Round,
      Square
    };
    enum class Join {
      Bevel,
      Round,
      Meter
    };
    enum class Flag {
        Paint    = 0x1,
        Opacity  = 0x2,
        Gradient = 0x4,
        Scale    = 0x8,
        Width    = 0x10,
        Cap      = 0x20,
        Join     = 0x40,
        Dash     = 0x80
    };
   FIStroke::Flag       mFlags;
   FIPaint              mPaint;
   int                  mOpacity;
   float                mScale;
   float                mWidth;
   float                mCentered;
   FIStroke::Cap        mCap;
   FIStroke::Join       mJoin;
   FIStroke::Dash      *mDash;
};

class FINodeProperty
{
    FIFill     mfill;
    FIStroke   mstroke;
};
struct FINode
{
    ~FINode(){}
    FINode(FINode *parent = nullptr) {
        if (parent) {
            mParent = parent;
            mParent->mChildren.push_back(this);
        }
    }
    enum class Type {
        Doc,
        Group,
        Def,
        Ellipse,
        Circle,
        Rect,
        Line,
        Path
    };
    FINode::Type           mType;
    FINode                *mParent;
    std::string            name;
    FINodeProperty        *property;
    std::vector<FINode *>  mChildren;

};

struct FIGroupNode : public FINode
{
    FIGroupNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Group;}
    int                    mParentId; // Lottie the id of the parent in the composition
    int                    mId;  // Lottie the group id  used for parenting.
    int                    mGroupType; //lottie layer type  (solid/shape/precomp)
};

struct FIDocNode : public FINode
{
    FIDocNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Doc;}
    SGRect      mBound;
    FIDefNode  *mDef;
    bool        mpreserveAspect = false;
    bool        mAnimation = false;
    float       mStartTime = 0;
    float       mEndTime = 0;
    float       mFrameRate;
};

struct FIEllipseNode : public FINode
{
    FIEllipseNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Ellipse;}
    SGPointF mCenter;
    float    mRadiusX;
    float    mRadiusY;
};

struct FICircleNode : public FINode
{
    FICircleNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Circle;}
    SGPointF mCenter;
    float    mRadius;
};

struct FIRectNode : public FINode
{
    FIRectNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Rect;}
    SGRect  mRect;
    float   mRadiusX;
    float   mRadiusY;
};

template<bool sizeAnim>
struct LNode : public FINode
{
    LottieIntProperty<sizeAnim> mSizeProperty;
};

struct FILineNode : public FINode
{
    FILineNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Line;}
    SGPointF mStartPt;
    SGPointF mEndPt;
};

struct FIPathNode : public FINode
{
    FIPathNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Path;}
    std::vector<float> mPoints;
};

struct FIDefNode : public FINode
{
    FIDefNode(FINode *parent = nullptr):FINode(parent) { mType = FINode::Type::Def;}
    std::vector<FIGradinet> mGradients;
};

struct FIGradinet
{
    enum class Type {
        Linear,
        Radial
    };
    enum class Spread {
        Pad,
        Repeat,
        Reflect
    };

   FIGradinet::Type     mType;
   std::string          mId;
   bool                 mUserSpace;
   FIGradinet::Spread   mSpread;
   std::vector<float>   mStops;
};

struct FILinearGradinet : FIGradinet
{
    FILinearGradinet()
    {
        mType = FIGradinet::Type::Linear;
    }
    SGPointF mStartPt;
    SGPointF mEndPt;
};

struct FIRadialGradinet : FIGradinet
{
    FIRadialGradinet()
    {
        mType = FIGradinet::Type::Radial;
    }
    SGPointF mCenter;
    SGPointF mFocal;
    float    mRadius;
};


using namespace rapidjson;

class LottieCompositionData;
class LottieComposition
{
public:
    static LottieComposition fromJsonSync(JsonDoc &json);
    ~LottieComposition();
    LottieComposition(const LottieComposition &path);
    LottieComposition(LottieComposition &&other);
    LottieComposition &operator=(const LottieComposition &);
    LottieComposition &operator=(LottieComposition &&other);

    long getStartFrame() const;
    long getEndFrame() const;
    SGRect getBounds() const;
    float getDpScale() const;
    int getMajorVersion() const;
    int getMinorVersion() const;
    int getPatchVersion() const;
    long getDuration() const;

    friend SGDebug& operator<<(SGDebug& os, const LottieComposition& o);
private:
    void cleanUp(LottieCompositionData *x);
    LottieComposition();
    friend class LottieCompositionData;
    LottieCompositionData *d;
};

inline SGDebug& operator<<(SGDebug& os, const LottieComposition& o)
{
    os<<"{startFrame:"<<o.getStartFrame()<<", endFrame: "<<o.getEndFrame()<<", Bound :"<<o.getBounds()<<", Duration:"<<o.getDuration()<<"}";
    return os;
}

#endif // LOTTIECOMPOSITION_H