svg_loader XmlParser: code refactoring.
[platform/core/graphics/tizenvg.git] / src / loaders / svg / tvgSvgLoaderCommon.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. 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 #ifndef _TVG_SVG_LOADER_COMMON_H_
23 #define _TVG_SVG_LOADER_COMMON_H_
24
25 #include "tvgCommon.h"
26 #include "tvgArray.h"
27
28 enum class SvgNodeType
29 {
30     Doc,
31     G,
32     Defs,
33     //Switch,  //Not support
34     Animation,
35     Arc,
36     Circle,
37     Ellipse,
38     Image,
39     Line,
40     Path,
41     Polygon,
42     Polyline,
43     Rect,
44     Text,
45     TextArea,
46     Tspan,
47     Use,
48     Video,
49     ClipPath,
50     //Custome_command,   //Not support
51     Unknown
52 };
53
54 enum class SvgLengthType
55 {
56     Percent,
57     Px,
58     Pc,
59     Pt,
60     Mm,
61     Cm,
62     In,
63 };
64
65 enum class SvgCompositeFlags
66 {
67     ClipPath = 0x01,
68 };
69
70 enum class SvgFillFlags
71 {
72     Paint = 0x01,
73     Opacity = 0x02,
74     Gradient = 0x04,
75     FillRule = 0x08,
76     ClipPath = 0x16
77 };
78
79 enum class SvgStrokeFlags
80 {
81     Paint = 0x1,
82     Opacity = 0x2,
83     Gradient = 0x4,
84     Scale = 0x8,
85     Width = 0x10,
86     Cap = 0x20,
87     Join = 0x40,
88     Dash = 0x80,
89 };
90
91 enum class SvgGradientType
92 {
93     Linear,
94     Radial
95 };
96
97 enum class SvgStyleType
98 {
99     Quality,
100     Fill,
101     ViewportFill,
102     Font,
103     Stroke,
104     SolidColor,
105     Gradient,
106     Transform,
107     Opacity,
108     CompOp
109 };
110
111 enum class SvgFillRule
112 {
113     Winding = 0,
114     OddEven = 1
115 };
116
117 //Length type to recalculate %, pt, pc, mm, cm etc
118 enum class SvgParserLengthType
119 {
120     Vertical,
121     Horizontal,
122     //In case of, for example, radius of radial gradient
123     Other
124 };
125
126 struct SvgNode;
127 struct SvgStyleGradient;
128
129
130 struct SvgDocNode
131 {
132     float w;
133     float h;
134     float vx;
135     float vy;
136     float vw;
137     float vh;
138     SvgNode* defs;
139     bool preserveAspect;
140 };
141
142 struct SvgGNode
143 {
144 };
145
146 struct SvgDefsNode
147 {
148     Array<SvgStyleGradient*> gradients;
149 };
150
151 struct SvgArcNode
152 {
153 };
154
155 struct SvgEllipseNode
156 {
157     float cx;
158     float cy;
159     float rx;
160     float ry;
161 };
162
163 struct SvgCircleNode
164 {
165     float cx;
166     float cy;
167     float r;
168 };
169
170 struct SvgRectNode
171 {
172     float x;
173     float y;
174     float w;
175     float h;
176     float rx;
177     float ry;
178     bool hasRx;
179     bool hasRy;
180 };
181
182 struct SvgLineNode
183 {
184     float x1;
185     float y1;
186     float x2;
187     float y2;
188 };
189
190 struct SvgPathNode
191 {
192     string* path;
193 };
194
195 struct SvgPolygonNode
196 {
197     int pointsCount;
198     float* points;
199 };
200
201 struct SvgLinearGradient
202 {
203     float x1;
204     float y1;
205     float x2;
206     float y2;
207 };
208
209 struct SvgRadialGradient
210 {
211     float cx;
212     float cy;
213     float fx;
214     float fy;
215     float r;
216 };
217
218 struct SvgGradientStop
219 {
220     float offset;
221     uint8_t r;
222     uint8_t g;
223     uint8_t b;
224     uint8_t a;
225 };
226
227 struct SvgComposite
228 {
229     SvgCompositeFlags flags;
230     string *url;
231     SvgNode* node;
232 };
233
234 struct SvgPaint
235 {
236     SvgStyleGradient* gradient;
237     string *url;
238     uint8_t r;
239     uint8_t g;
240     uint8_t b;
241     bool none;
242     bool curColor;
243 };
244
245 struct SvgDash
246 {
247     Array<float> array;
248 };
249
250 struct SvgStyleGradient
251 {
252     SvgGradientType type;
253     string *id;
254     string *ref;
255     FillSpread spread;
256     SvgRadialGradient* radial;
257     SvgLinearGradient* linear;
258     Matrix* transform;
259     Array<Fill::ColorStop *> stops;
260     bool userSpace;
261     bool usePercentage;
262 };
263
264 struct SvgStyleFill
265 {
266     SvgFillFlags flags;
267     SvgPaint paint;
268     int opacity;
269     SvgFillRule fillRule;
270 };
271
272 struct SvgStyleStroke
273 {
274     SvgStrokeFlags flags;
275     SvgPaint paint;
276     int opacity;
277     float scale;
278     float width;
279     float centered;
280     StrokeCap cap;
281     StrokeJoin join;
282     SvgDash dash;
283     int dashCount;
284 };
285
286 struct SvgStyleProperty
287 {
288     SvgStyleFill fill;
289     SvgStyleStroke stroke;
290     SvgComposite comp;
291     int opacity;
292     uint8_t r;
293     uint8_t g;
294     uint8_t b;
295 };
296
297 struct SvgNode
298 {
299     SvgNodeType type;
300     SvgNode* parent;
301     Array<SvgNode*> child;
302     string *id;
303     SvgStyleProperty *style;
304     Matrix* transform;
305     union {
306         SvgGNode g;
307         SvgDocNode doc;
308         SvgDefsNode defs;
309         SvgArcNode arc;
310         SvgCircleNode circle;
311         SvgEllipseNode ellipse;
312         SvgPolygonNode polygon;
313         SvgPolygonNode polyline;
314         SvgRectNode rect;
315         SvgPathNode path;
316         SvgLineNode line;
317     } node;
318     bool display;
319 };
320
321 struct SvgParser
322 {
323     SvgNode* node;
324     SvgStyleGradient* styleGrad;
325     Fill::ColorStop* gradStop;
326     struct
327     {
328         int x, y;
329         uint32_t w, h;
330     } global;
331     struct
332     {
333         bool parsedFx;
334         bool parsedFy;
335     } gradient;
336 };
337
338 struct SvgLoaderData
339 {
340     Array<SvgNode *> stack = {nullptr, 0, 0};
341     SvgNode* doc = nullptr;
342     SvgNode* def = nullptr;
343     Array<SvgStyleGradient*> gradients;
344     SvgStyleGradient* latestGradient = nullptr; //For stops
345     SvgParser* svgParse = nullptr;
346     int level = 0;
347     bool result = false;
348 };
349
350 #endif