log SvgLoader: Enhance log message
[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 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     Mask,
51     //Custome_command,   //Not support
52     Unknown
53 };
54
55 enum class SvgLengthType
56 {
57     Percent,
58     Px,
59     Pc,
60     Pt,
61     Mm,
62     Cm,
63     In,
64 };
65
66
67 enum class SvgFillFlags
68 {
69     Paint = 0x01,
70     Opacity = 0x02,
71     Gradient = 0x04,
72     FillRule = 0x08,
73     ClipPath = 0x16
74 };
75
76 enum class SvgStrokeFlags
77 {
78     Paint = 0x1,
79     Opacity = 0x2,
80     Gradient = 0x4,
81     Scale = 0x8,
82     Width = 0x10,
83     Cap = 0x20,
84     Join = 0x40,
85     Dash = 0x80,
86 };
87
88 enum class SvgGradientType
89 {
90     Linear,
91     Radial
92 };
93
94 enum class SvgStyleType
95 {
96     Quality,
97     Fill,
98     ViewportFill,
99     Font,
100     Stroke,
101     SolidColor,
102     Gradient,
103     Transform,
104     Opacity,
105     CompOp
106 };
107
108 enum class SvgFillRule
109 {
110     Winding = 0,
111     OddEven = 1
112 };
113
114 //Length type to recalculate %, pt, pc, mm, cm etc
115 enum class SvgParserLengthType
116 {
117     Vertical,
118     Horizontal,
119     //In case of, for example, radius of radial gradient
120     Other
121 };
122
123 struct SvgNode;
124 struct SvgStyleGradient;
125
126
127 struct SvgDocNode
128 {
129     float w;
130     float h;
131     float vx;
132     float vy;
133     float vw;
134     float vh;
135     SvgNode* defs;
136     bool preserveAspect;
137 };
138
139 struct SvgGNode
140 {
141 };
142
143 struct SvgDefsNode
144 {
145     Array<SvgStyleGradient*> gradients;
146 };
147
148 struct SvgArcNode
149 {
150 };
151
152 struct SvgEllipseNode
153 {
154     float cx;
155     float cy;
156     float rx;
157     float ry;
158 };
159
160 struct SvgCircleNode
161 {
162     float cx;
163     float cy;
164     float r;
165 };
166
167 struct SvgRectNode
168 {
169     float x;
170     float y;
171     float w;
172     float h;
173     float rx;
174     float ry;
175     bool hasRx;
176     bool hasRy;
177 };
178
179 struct SvgLineNode
180 {
181     float x1;
182     float y1;
183     float x2;
184     float y2;
185 };
186
187 struct SvgPathNode
188 {
189     string* path;
190 };
191
192 struct SvgPolygonNode
193 {
194     int pointsCount;
195     float* points;
196 };
197
198 struct SvgLinearGradient
199 {
200     float x1;
201     float y1;
202     float x2;
203     float y2;
204 };
205
206 struct SvgRadialGradient
207 {
208     float cx;
209     float cy;
210     float fx;
211     float fy;
212     float r;
213 };
214
215 struct SvgGradientStop
216 {
217     float offset;
218     uint8_t r;
219     uint8_t g;
220     uint8_t b;
221     uint8_t a;
222 };
223
224 struct SvgComposite
225 {
226     CompositeMethod method;     //TODO: Currently support either one method
227     string *url;
228     SvgNode* node;
229 };
230
231 struct SvgPaint
232 {
233     SvgStyleGradient* gradient;
234     string *url;
235     uint8_t r;
236     uint8_t g;
237     uint8_t b;
238     bool none;
239     bool curColor;
240 };
241
242 struct SvgDash
243 {
244     Array<float> array;
245 };
246
247 struct SvgStyleGradient
248 {
249     SvgGradientType type;
250     string *id;
251     string *ref;
252     FillSpread spread;
253     SvgRadialGradient* radial;
254     SvgLinearGradient* linear;
255     Matrix* transform;
256     Array<Fill::ColorStop *> stops;
257     bool userSpace;
258     bool usePercentage;
259 };
260
261 struct SvgStyleFill
262 {
263     SvgFillFlags flags;
264     SvgPaint paint;
265     int opacity;
266     SvgFillRule fillRule;
267 };
268
269 struct SvgStyleStroke
270 {
271     SvgStrokeFlags flags;
272     SvgPaint paint;
273     int opacity;
274     float scale;
275     float width;
276     float centered;
277     StrokeCap cap;
278     StrokeJoin join;
279     SvgDash dash;
280     int dashCount;
281 };
282
283 struct SvgStyleProperty
284 {
285     SvgStyleFill fill;
286     SvgStyleStroke stroke;
287     SvgComposite comp;
288     int opacity;
289     uint8_t r;
290     uint8_t g;
291     uint8_t b;
292 };
293
294 struct SvgNode
295 {
296     SvgNodeType type;
297     SvgNode* parent;
298     Array<SvgNode*> child;
299     string *id;
300     SvgStyleProperty *style;
301     Matrix* transform;
302     union {
303         SvgGNode g;
304         SvgDocNode doc;
305         SvgDefsNode defs;
306         SvgArcNode arc;
307         SvgCircleNode circle;
308         SvgEllipseNode ellipse;
309         SvgPolygonNode polygon;
310         SvgPolygonNode polyline;
311         SvgRectNode rect;
312         SvgPathNode path;
313         SvgLineNode line;
314     } node;
315     bool display;
316 };
317
318 struct SvgParser
319 {
320     SvgNode* node;
321     SvgStyleGradient* styleGrad;
322     Fill::ColorStop* gradStop;
323     struct
324     {
325         int x, y;
326         uint32_t w, h;
327     } global;
328     struct
329     {
330         bool parsedFx;
331         bool parsedFy;
332     } gradient;
333 };
334
335 struct SvgLoaderData
336 {
337     Array<SvgNode *> stack = {nullptr, 0, 0};
338     SvgNode* doc = nullptr;
339     SvgNode* def = nullptr;
340     Array<SvgStyleGradient*> gradients;
341     SvgStyleGradient* latestGradient = nullptr; //For stops
342     SvgParser* svgParse = nullptr;
343     int level = 0;
344     bool result = false;
345 };
346
347 /*
348  * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtof-strtod-l-wcstod-wcstod-l?view=vs-2017
349  *
350  * src should be one of the following form :
351  *
352  * [whitespace] [sign] {digits [radix digits] | radix digits} [{e | E} [sign] digits]
353  * [whitespace] [sign] {INF | INFINITY}
354  * [whitespace] [sign] NAN [sequence]
355  *
356  * No hexadecimal form supported
357  * no sequence supported after NAN
358  */
359 float customStrtof(const char *nptr, char **endptr);
360
361 #endif