1 /*
2  * Copyright (c) 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_EXPRESSIONS_H_
27 #define _TVG_LOTTIE_EXPRESSIONS_H_
28 
29 #include "tvgCommon.h"
30 #include "tvgLottieCommon.h"
31 
32 struct LottieExpression;
33 struct LottieComposition;
34 struct LottieLayer;
35 struct LottieRoundnessModifier;
36 struct LottieOffsetModifier;
37 
38 #ifdef THORVG_LOTTIE_EXPRESSIONS_SUPPORT
39 
40 #include "jerryscript.h"
41 
42 
43 struct LottieExpressions
44 {
45 public:
46     template<typename Property, typename NumType>
resultLottieExpressions47     bool result(float frameNo, NumType& out, LottieExpression* exp)
48     {
49         auto bm_rt = evaluate(frameNo, exp);
50         if (jerry_value_is_undefined(bm_rt)) return false;
51 
52         if (jerry_value_is_number(bm_rt)) {
53             out = (NumType) jerry_value_as_number(bm_rt);
54         } else if (auto prop = static_cast<Property*>(jerry_object_get_native_ptr(bm_rt, nullptr))) {
55             out = (*prop)(frameNo);
56         }
57         jerry_value_free(bm_rt);
58         return true;
59     }
60 
61     template<typename Property>
resultLottieExpressions62     bool result(float frameNo, Point& out, LottieExpression* exp)
63     {
64         auto bm_rt = evaluate(frameNo, exp);
65         if (jerry_value_is_undefined(bm_rt)) return false;
66 
67         if (auto prop = static_cast<Property*>(jerry_object_get_native_ptr(bm_rt, nullptr))) {
68             out = (*prop)(frameNo);
69         } else {
70             auto x = jerry_object_get_index(bm_rt, 0);
71             auto y = jerry_object_get_index(bm_rt, 1);
72             out.x = jerry_value_as_number(x);
73             out.y = jerry_value_as_number(y);
74             jerry_value_free(x);
75             jerry_value_free(y);
76         }
77         jerry_value_free(bm_rt);
78         return true;
79     }
80 
81     template<typename Property>
resultLottieExpressions82     bool result(float frameNo, RGB24& out, LottieExpression* exp)
83     {
84         auto bm_rt = evaluate(frameNo, exp);
85         if (jerry_value_is_undefined(bm_rt)) return false;
86 
87         if (auto color = static_cast<Property*>(jerry_object_get_native_ptr(bm_rt, nullptr))) {
88             out = (*color)(frameNo);
89         } else {
90             auto r = jerry_object_get_index(bm_rt, 0);
91             auto g = jerry_object_get_index(bm_rt, 1);
92             auto b = jerry_object_get_index(bm_rt, 2);
93             out.rgb[0] = REMAP255(jerry_value_as_number(r));
94             out.rgb[1] = REMAP255(jerry_value_as_number(g));
95             out.rgb[2] = REMAP255(jerry_value_as_number(b));
96             jerry_value_free(r);
97             jerry_value_free(g);
98             jerry_value_free(b);
99         }
100         jerry_value_free(bm_rt);
101         return true;
102     }
103 
104     template<typename Property>
resultLottieExpressions105     bool result(float frameNo, Fill* fill, LottieExpression* exp)
106     {
107         auto bm_rt = evaluate(frameNo, exp);
108         if (jerry_value_is_undefined(bm_rt)) return false;
109 
110         if (auto colorStop = static_cast<Property*>(jerry_object_get_native_ptr(bm_rt, nullptr))) {
111             (*colorStop)(frameNo, fill, this);
112         }
113         jerry_value_free(bm_rt);
114         return true;
115     }
116 
117     template<typename Property>
resultLottieExpressions118     bool result(float frameNo, Array<PathCommand>& cmds, Array<Point>& pts, Matrix* transform, const LottieRoundnessModifier* roundness, const LottieOffsetModifier* offsetPath, LottieExpression* exp)
119     {
120         auto bm_rt = evaluate(frameNo, exp);
121         if (jerry_value_is_undefined(bm_rt)) return false;
122 
123         if (auto pathset = static_cast<Property*>(jerry_object_get_native_ptr(bm_rt, nullptr))) {
124             (*pathset)(frameNo, cmds, pts, transform, roundness, offsetPath);
125         }
126         jerry_value_free(bm_rt);
127         return true;
128     }
129 
130     void update(float curTime);
131 
132     //singleton (no thread safety)
133     static LottieExpressions* instance();
134     static void retrieve(LottieExpressions* instance);
135 
136 private:
137     LottieExpressions();
138     ~LottieExpressions();
139 
140     jerry_value_t evaluate(float frameNo, LottieExpression* exp);
141     jerry_value_t buildGlobal();
142 
143     void buildComp(LottieComposition* comp, float frameNo, LottieExpression* exp);
144     void buildComp(jerry_value_t context, float frameNo, LottieLayer* comp, LottieExpression* exp);
145     void buildGlobal(LottieExpression* exp);
146 
147     //global object, attributes, methods
148     jerry_value_t global;
149     jerry_value_t comp;
150     jerry_value_t thisComp;
151     jerry_value_t thisLayer;
152     jerry_value_t thisProperty;
153 };
154 
155 #else
156 
157 struct LottieExpressions
158 {
resultLottieExpressions159     template<typename Property, typename NumType> bool result(TVG_UNUSED float, TVG_UNUSED NumType&, TVG_UNUSED LottieExpression*) { return false; }
resultLottieExpressions160     template<typename Property> bool result(TVG_UNUSED float, TVG_UNUSED Point&, LottieExpression*) { return false; }
resultLottieExpressions161     template<typename Property> bool result(TVG_UNUSED float, TVG_UNUSED RGB24&, TVG_UNUSED LottieExpression*) { return false; }
resultLottieExpressions162     template<typename Property> bool result(TVG_UNUSED float, TVG_UNUSED Fill*, TVG_UNUSED LottieExpression*) { return false; }
resultLottieExpressions163     template<typename Property> bool result(TVG_UNUSED float, TVG_UNUSED Array<PathCommand>&, TVG_UNUSED Array<Point>&, TVG_UNUSED Matrix* transform, TVG_UNUSED const LottieRoundnessModifier*, TVG_UNUSED const LottieOffsetModifier*, TVG_UNUSED LottieExpression*) { return false; }
updateLottieExpressions164     void update(TVG_UNUSED float) {}
instanceLottieExpressions165     static LottieExpressions* instance() { return nullptr; }
retrieveLottieExpressions166     static void retrieve(TVG_UNUSED LottieExpressions* instance) {}
167 };
168 
169 #endif //THORVG_LOTTIE_EXPRESSIONS_SUPPORT
170 
171 #endif //_TVG_LOTTIE_EXPRESSIONS_H_
172 
173 #endif /* LV_USE_THORVG_INTERNAL */
174 
175