b6275383005c60567061eec18d5d75e5c0d5ed43
[platform/core/graphics/tizenvg.git] / src / loaders / svg / tvgSvgLoaderCommon.h
1 /*
2  * Copyright (c) 2020-2021 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 struct SvgNode;
29 struct SvgStyleGradient;
30
31 //NOTE: Please update simpleXmlNodeTypeToString() as well.
32 enum class SvgNodeType
33 {
34     Doc,
35     G,
36     Defs,
37     Animation,
38     Arc,
39     Circle,
40     Ellipse,
41     Image,
42     Line,
43     Path,
44     Polygon,
45     Polyline,
46     Rect,
47     Text,
48     TextArea,
49     Tspan,
50     Use,
51     Video,
52     ClipPath,
53     Mask,
54     Unknown
55 };
56
57 /*
58 // TODO - remove?
59 enum class SvgLengthType
60 {
61     Percent,
62     Px,
63     Pc,
64     Pt,
65     Mm,
66     Cm,
67     In,
68 };
69 */
70
71 enum class SvgFillFlags
72 {
73     Paint = 0x01,
74     Opacity = 0x02,
75     Gradient = 0x04,
76     FillRule = 0x08,
77     ClipPath = 0x16
78 };
79
80 enum class SvgStrokeFlags
81 {
82     Paint = 0x1,
83     Opacity = 0x2,
84     Gradient = 0x4,
85     Scale = 0x8,
86     Width = 0x10,
87     Cap = 0x20,
88     Join = 0x40,
89     Dash = 0x80,
90 };
91
92 enum class SvgGradientType
93 {
94     Linear,
95     Radial
96 };
97
98 enum class SvgStyleFlags
99 {
100     Color = 0x01,
101     Fill = 0x02,
102     FillRule = 0x04,
103     FillOpacity = 0x08,
104     Opacity = 0x010,
105     Stroke = 0x20,
106     StrokeWidth = 0x40,
107     StrokeLineJoin = 0x80,
108     StrokeLineCap = 0x100,
109     StrokeOpacity = 0x200,
110     StrokeDashArray = 0x400,
111     Transform = 0x800,
112     ClipPath = 0x1000,
113     Mask = 0x2000,
114     Display = 0x4000
115 };
116
117 enum class SvgStopStyleFlags
118 {
119     StopDefault = 0x0,
120     StopOpacity = 0x01,
121     StopColor = 0x02
122 };
123
124 enum class SvgFillRule
125 {
126     Winding = 0,
127     OddEven = 1
128 };
129
130 //Length type to recalculate %, pt, pc, mm, cm etc
131 enum class SvgParserLengthType
132 {
133     Vertical,
134     Horizontal,
135     //In case of, for example, radius of radial gradient
136     Other
137 };
138
139 struct SvgDocNode
140 {
141     float w;
142     float h;
143     float vx;
144     float vy;
145     float vw;
146     float vh;
147     SvgNode* defs;
148     bool preserveAspect;
149 };
150
151 struct SvgGNode
152 {
153 };
154
155 struct SvgDefsNode
156 {
157     Array<SvgStyleGradient*> gradients;
158 };
159
160 struct SvgUseNode
161 {
162     float x, y, w, h;
163 };
164
165
166 struct SvgEllipseNode
167 {
168     float cx;
169     float cy;
170     float rx;
171     float ry;
172 };
173
174 struct SvgCircleNode
175 {
176     float cx;
177     float cy;
178     float r;
179 };
180
181 struct SvgRectNode
182 {
183     float x;
184     float y;
185     float w;
186     float h;
187     float rx;
188     float ry;
189     bool hasRx;
190     bool hasRy;
191 };
192
193 struct SvgLineNode
194 {
195     float x1;
196     float y1;
197     float x2;
198     float y2;
199 };
200
201 struct SvgImageNode
202 {
203     float x, y, w, h;
204     char* href;
205 };
206
207 struct SvgPathNode
208 {
209     char* path;
210 };
211
212 struct SvgPolygonNode
213 {
214     int pointsCount;
215     float* points;
216 };
217
218 struct SvgCompositeNode
219 {
220     bool userSpace;
221 };
222
223 struct SvgLinearGradient
224 {
225     float x1;
226     float y1;
227     float x2;
228     float y2;
229     bool isX1Percentage;
230     bool isY1Percentage;
231     bool isX2Percentage;
232     bool isY2Percentage;
233 };
234
235 struct SvgRadialGradient
236 {
237     float cx;
238     float cy;
239     float fx;
240     float fy;
241     float r;
242     bool isCxPercentage;
243     bool isCyPercentage;
244     bool isFxPercentage;
245     bool isFyPercentage;
246     bool isRPercentage;
247 };
248
249 struct SvgComposite
250 {
251     char *url;
252     SvgNode* node;
253     bool applying;              //flag for checking circular dependency.
254 };
255
256 struct SvgColor
257 {
258     uint8_t r;
259     uint8_t g;
260     uint8_t b;
261 };
262
263 struct SvgPaint
264 {
265     SvgStyleGradient* gradient;
266     char *url;
267     SvgColor color;
268     bool none;
269     bool curColor;
270 };
271
272 struct SvgDash
273 {
274     Array<float> array;
275 };
276
277 struct SvgStyleGradient
278 {
279     SvgGradientType type = SvgGradientType::Linear;
280     char *id = nullptr;
281     char *ref = nullptr;
282     FillSpread spread = FillSpread::Pad;
283     SvgRadialGradient* radial = nullptr;
284     SvgLinearGradient* linear = nullptr;
285     Matrix* transform = nullptr;
286     Array<Fill::ColorStop> stops;
287     bool userSpace = false;
288
289     ~SvgStyleGradient()
290     {
291         stops.reset();
292         free(transform);
293         free(radial);
294         free(linear);
295         free(ref);
296         free(id);
297     }
298 };
299
300 struct SvgStyleFill
301 {
302     SvgFillFlags flags;
303     SvgPaint paint;
304     int opacity;
305     FillRule fillRule;
306 };
307
308 struct SvgStyleStroke
309 {
310     SvgStrokeFlags flags;
311     SvgPaint paint;
312     int opacity;
313     float scale;
314     float width;
315     float centered;
316     StrokeCap cap;
317     StrokeJoin join;
318     SvgDash dash;
319     int dashCount;
320 };
321
322 struct SvgStyleProperty
323 {
324     SvgStyleFill fill;
325     SvgStyleStroke stroke;
326     SvgComposite clipPath;
327     SvgComposite mask;
328     int opacity;
329     SvgColor color;
330     bool curColorSet;
331     SvgStyleFlags flags;
332 };
333
334 struct SvgNode
335 {
336     SvgNodeType type;
337     SvgNode* parent;
338     Array<SvgNode*> child;
339     char *id;
340     SvgStyleProperty *style;
341     Matrix* transform;
342     union {
343         SvgGNode g;
344         SvgDocNode doc;
345         SvgDefsNode defs;
346         SvgUseNode use;
347         SvgCircleNode circle;
348         SvgEllipseNode ellipse;
349         SvgPolygonNode polygon;
350         SvgPolygonNode polyline;
351         SvgRectNode rect;
352         SvgPathNode path;
353         SvgLineNode line;
354         SvgImageNode image;
355         SvgCompositeNode comp;
356     } node;
357     bool display;
358     ~SvgNode();
359 };
360
361 struct SvgParser
362 {
363     SvgNode* node;
364     SvgStyleGradient* styleGrad;
365     Fill::ColorStop gradStop;
366     SvgStopStyleFlags flags;
367     struct
368     {
369         int x, y;
370         uint32_t w, h;
371     } global;
372     struct
373     {
374         bool parsedFx;
375         bool parsedFy;
376     } gradient;
377 };
378
379 struct SvgNodeIdPair
380 {
381     SvgNode* node;
382     char *id;
383 };
384
385 struct SvgLoaderData
386 {
387     Array<SvgNode *> stack = {nullptr, 0, 0};
388     SvgNode* doc = nullptr;
389     SvgNode* def = nullptr;
390     Array<SvgStyleGradient*> gradients;
391     SvgStyleGradient* latestGradient = nullptr; //For stops
392     SvgParser* svgParse = nullptr;
393     Array<SvgNodeIdPair> cloneNodes;
394     int level = 0;
395     bool result = false;
396 };
397
398 /*
399  * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtof-strtod-l-wcstod-wcstod-l?view=vs-2017
400  *
401  * src should be one of the following form :
402  *
403  * [whitespace] [sign] {digits [radix digits] | radix digits} [{e | E} [sign] digits]
404  * [whitespace] [sign] {INF | INFINITY}
405  * [whitespace] [sign] NAN [sequence]
406  *
407  * No hexadecimal form supported
408  * no sequence supported after NAN
409  */
410 float customStrtof(const char *nptr, char **endptr);
411
412 #endif