1 /*
2  * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved.
3 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10 
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #ifndef _TVG_LOTTIE_PARSER_H_
27 #define _TVG_LOTTIE_PARSER_H_
28 
29 #include "tvgCommon.h"
30 #include "tvgLottieParserHandler.h"
31 #include "tvgLottieProperty.h"
32 
33 struct LottieParser : LookaheadParserHandler
34 {
35 public:
LottieParserLottieParser36     LottieParser(const char *str, const char* dirName) : LookaheadParserHandler(str)
37     {
38         this->dirName = dirName;
39     }
40 
41     bool parse();
42     bool apply(LottieSlot* slot);
43     const char* sid(bool first = false);
44 
45     LottieComposition* comp = nullptr;
46     const char* dirName = nullptr;       //base resource directory
47 
48 private:
49     RGB24 getColor(const char *str);
50     CompositeMethod getMatteType();
51     FillRule getFillRule();
52     StrokeCap getStrokeCap();
53     StrokeJoin getStrokeJoin();
54     CompositeMethod getMaskMethod(bool inversed);
55     LottieInterpolator* getInterpolator(const char* key, Point& in, Point& out);
56     LottieEffect* getEffect(int type);
57 
58     void getInterpolatorPoint(Point& pt);
59     void getPathSet(LottiePathSet& path);
60     void getLayerSize(float& val);
61     void getValue(TextDocument& doc);
62     void getValue(PathSet& path);
63     void getValue(Array<Point>& pts);
64     void getValue(ColorStop& color);
65     void getValue(float& val);
66     void getValue(uint8_t& val);
67     void getValue(int8_t& val);
68     void getValue(RGB24& color);
69     bool getValue(Point& pt);
70 
71     template<typename T> bool parseTangent(const char *key, LottieVectorFrame<T>& value);
72     template<typename T> bool parseTangent(const char *key, LottieScalarFrame<T>& value);
73     template<typename T> void parseKeyFrame(T& prop);
74     template<typename T> void parsePropertyInternal(T& prop);
75     template<LottieProperty::Type type = LottieProperty::Type::Invalid, typename T> void parseProperty(T& prop, LottieObject* obj = nullptr);
76     template<LottieProperty::Type type = LottieProperty::Type::Invalid, typename T> void parseSlotProperty(T& prop);
77 
78     LottieObject* parseObject();
79     LottieObject* parseAsset();
80     LottieImage* parseImage(const char* data, const char* subPath, bool embedded, float width, float height);
81     LottieLayer* parseLayer(LottieLayer* precomp);
82     LottieObject* parseGroup();
83     LottieRect* parseRect();
84     LottieEllipse* parseEllipse();
85     LottieSolidFill* parseSolidFill();
86     LottieTransform* parseTransform(bool ddd = false);
87     LottieSolidStroke* parseSolidStroke();
88     LottieGradientStroke* parseGradientStroke();
89     LottiePath* parsePath();
90     LottiePolyStar* parsePolyStar();
91     LottieRoundedCorner* parseRoundedCorner();
92     LottieGradientFill* parseGradientFill();
93     LottieLayer* parseLayers(LottieLayer* root);
94     LottieMask* parseMask();
95     LottieTrimpath* parseTrimpath();
96     LottieRepeater* parseRepeater();
97     LottieOffsetPath* parseOffsetPath();
98     LottieFont* parseFont();
99     LottieMarker* parseMarker();
100 
101     void parseGaussianBlur(LottieGaussianBlur* effect);
102 
103     bool parseDirection(LottieShape* shape, const char* key);
104     bool parseCommon(LottieObject* obj, const char* key);
105     void parseObject(Array<LottieObject*>& parent);
106     void parseShapes(Array<LottieObject*>& parent);
107     void parseText(Array<LottieObject*>& parent);
108     void parseMasks(LottieLayer* layer);
109     void parseEffects(LottieLayer* layer);
110     void parseTimeRemap(LottieLayer* layer);
111     void parseStrokeDash(LottieStroke* stroke);
112     void parseGradient(LottieGradient* gradient, const char* key);
113     void parseTextRange(LottieText* text);
114     void parseAssets();
115     void parseFonts();
116     void parseChars(Array<LottieGlyph*>& glyphs);
117     void parseMarkers();
118     void parseEffect(LottieEffect* effect);
119     void postProcess(Array<LottieGlyph*>& glyphs);
120 
121     //Current parsing context
122     struct Context {
123         LottieLayer* layer = nullptr;
124         LottieObject* parent = nullptr;
125     } context;
126 };
127 
128 #endif //_TVG_LOTTIE_PARSER_H_
129 
130 #endif /* LV_USE_THORVG_INTERNAL */
131 
132