Debian Codegraph @

thirdparty/thorvg/src/loaders/svg/tvgSvgLoaderCommon.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 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 #ifndef _TVG_SVG_LOADER_COMMON_H_
24 #define _TVG_SVG_LOADER_COMMON_H_
25
26 #include "tvgCommon.h"
27 #include "tvgArray.h"
28 #include "tvgInlist.h"
29
30 struct SvgNode;
31 struct SvgStyleGradient;
32
33 //NOTE: Please update simpleXmlNodeTypeToString() as well.
34 enum class SvgNodeType
35 {
36 Doc,
37 G,
38 Defs,
39 Animation,
40 Arc,
41 Circle,
42 Ellipse,
43 Image,
44 Line,
45 Path,
46 Polygon,
47 Polyline,
48 Rect,
49 Text,
50 TextArea,
51 Tspan,
52 Use,
53 Video,
54 ClipPath,
55 Mask,
56 CssStyle,
57 Symbol,
58 Unknown
59 };
60
61 /*
62 // TODO - remove?
63 enum class SvgLengthType
64 {
65 Percent,
66 Px,
67 Pc,
68 Pt,
69 Mm,
70 Cm,
71 In,
72 };
73 */
74
75 enum class SvgFillFlags
76 {
77 Paint = 0x01,
78 Opacity = 0x02,
79 Gradient = 0x04,
80 FillRule = 0x08,
81 ClipPath = 0x16
82 };
83
84 constexpr bool operator &(SvgFillFlags a, SvgFillFlags b)
85 {
86 return int(a) & int(b);
87 }
88
89 constexpr SvgFillFlags operator |(SvgFillFlags a, SvgFillFlags b)
90 {
91 return SvgFillFlags(int(a) | int(b));
92 }
93
94 enum class SvgStrokeFlags
95 {
96 Paint = 0x1,
97 Opacity = 0x2,
98 Gradient = 0x4,
99 Scale = 0x8,
100 Width = 0x10,
101 Cap = 0x20,
102 Join = 0x40,
103 Dash = 0x80,
104 Miterlimit = 0x100,
105 DashOffset = 0x200
106 };
107
108 constexpr bool operator &(SvgStrokeFlags a, SvgStrokeFlags b)
109 {
110 return int(a) & int(b);
111 }
112
113 constexpr SvgStrokeFlags operator |(SvgStrokeFlags a, SvgStrokeFlags b)
114 {
115 return SvgStrokeFlags(int(a) | int(b));
116 }
117
118
119 enum class SvgGradientType
120 {
121 Linear,
122 Radial
123 };
124
125 enum class SvgStyleFlags
126 {
127 Color = 0x01,
128 Fill = 0x02,
129 FillRule = 0x04,
130 FillOpacity = 0x08,
131 Opacity = 0x010,
132 Stroke = 0x20,
133 StrokeWidth = 0x40,
134 StrokeLineJoin = 0x80,
135 StrokeLineCap = 0x100,
136 StrokeOpacity = 0x200,
137 StrokeDashArray = 0x400,
138 Transform = 0x800,
139 ClipPath = 0x1000,
140 Mask = 0x2000,
141 MaskType = 0x4000,
142 Display = 0x8000,
143 PaintOrder = 0x10000,
144 StrokeMiterlimit = 0x20000,
145 StrokeDashOffset = 0x40000,
146 };
147
148 constexpr bool operator &(SvgStyleFlags a, SvgStyleFlags b)
149 {
150 return int(a) & int(b);
151 }
152
153 constexpr SvgStyleFlags operator |(SvgStyleFlags a, SvgStyleFlags b)
154 {
155 return SvgStyleFlags(int(a) | int(b));
156 }
157
158 enum class SvgStopStyleFlags
159 {
160 StopDefault = 0x0,
161 StopOpacity = 0x01,
162 StopColor = 0x02
163 };
164
165 constexpr bool operator &(SvgStopStyleFlags a, SvgStopStyleFlags b)
166 {
167 return int(a) & int(b);
168 }
169
170 constexpr SvgStopStyleFlags operator |(SvgStopStyleFlags a, SvgStopStyleFlags b)
171 {
172 return SvgStopStyleFlags(int(a) | int(b));
173 }
174
175 enum class SvgGradientFlags
176 {
177 None = 0x0,
178 GradientUnits = 0x1,
179 SpreadMethod = 0x2,
180 X1 = 0x4,
181 X2 = 0x8,
182 Y1 = 0x10,
183 Y2 = 0x20,
184 Cx = 0x40,
185 Cy = 0x80,
186 R = 0x100,
187 Fx = 0x200,
188 Fy = 0x400,
189 Fr = 0x800
190 };
191
192 constexpr bool operator &(SvgGradientFlags a, SvgGradientFlags b)
193 {
194 return int(a) & int(b);
195 }
196
197 constexpr SvgGradientFlags operator |(SvgGradientFlags a, SvgGradientFlags b)
198 {
199 return SvgGradientFlags(int(a) | int(b));
200 }
201
202 enum class SvgFillRule
203 {
204 Winding = 0,
205 OddEven = 1
206 };
207
208 enum class SvgMaskType
209 {
210 Luminance = 0,
211 Alpha
212 };
213
214 //Length type to recalculate %, pt, pc, mm, cm etc
215 enum class SvgParserLengthType
216 {
217 Vertical,
218 Horizontal,
219 Diagonal,
220 //In case of, for example, radius of radial gradient
221 Other
222 };
223
224 enum class SvgViewFlag
225 {
226 None = 0x0,
227 Width = 0x01, //viewPort width
228 Height = 0x02, //viewPort height
229 Viewbox = 0x04, //viewBox x,y,w,h - used only if all 4 are correctly set
230 WidthInPercent = 0x08,
231 HeightInPercent = 0x10
232 };
233
234 constexpr bool operator &(SvgViewFlag a, SvgViewFlag b)
235 {
236 return static_cast<int>(a) & static_cast<int>(b);
237 }
238
239 constexpr SvgViewFlag operator |(SvgViewFlag a, SvgViewFlag b)
240 {
241 return SvgViewFlag(int(a) | int(b));
242 }
243
244 constexpr SvgViewFlag operator ^(SvgViewFlag a, SvgViewFlag b)
245 {
246 return SvgViewFlag(int(a) ^ int(b));
247 }
248
249 enum class AspectRatioAlign
250 {
251 None,
252 XMinYMin,
253 XMidYMin,
254 XMaxYMin,
255 XMinYMid,
256 XMidYMid,
257 XMaxYMid,
258 XMinYMax,
259 XMidYMax,
260 XMaxYMax
261 };
262
263 enum class AspectRatioMeetOrSlice
264 {
265 Meet,
266 Slice
267 };
268
269 struct SvgDocNode
270 {
271 float w; //unit: point or in percentage see: SvgViewFlag
272 float h; //unit: point or in percentage see: SvgViewFlag
273 float vx;
274 float vy;
275 float vw;
276 float vh;
277 SvgViewFlag viewFlag;
278 SvgNode* defs;
279 SvgNode* style;
280 AspectRatioAlign align;
281 AspectRatioMeetOrSlice meetOrSlice;
282 };
283
284 struct SvgGNode
285 {
286 };
287
288 struct SvgDefsNode
289 {
290 Array<SvgStyleGradient*> gradients;
291 };
292
293 struct SvgSymbolNode
294 {
295 float w, h;
296 float vx, vy, vw, vh;
297 AspectRatioAlign align;
298 AspectRatioMeetOrSlice meetOrSlice;
299 bool overflowVisible;
300 bool hasViewBox;
301 bool hasWidth;
302 bool hasHeight;
303 };
304
305 struct SvgUseNode
306 {
307 float x, y, w, h;
308 bool isWidthSet;
309 bool isHeightSet;
310 SvgNode* symbol;
311 };
312
313 struct SvgEllipseNode
314 {
315 float cx;
316 float cy;
317 float rx;
318 float ry;
319 };
320
321 struct SvgCircleNode
322 {
323 float cx;
324 float cy;
325 float r;
326 };
327
328 struct SvgRectNode
329 {
330 float x;
331 float y;
332 float w;
333 float h;
334 float rx;
335 float ry;
336 bool hasRx;
337 bool hasRy;
338 };
339
340 struct SvgLineNode
341 {
342 float x1;
343 float y1;
344 float x2;
345 float y2;
346 };
347
348 struct SvgImageNode
349 {
350 float x, y, w, h;
351 char* href;
352 };
353
354 struct SvgPathNode
355 {
356 char* path;
357 };
358
359 struct SvgPolygonNode
360 {
361 Array<float> pts;
362 };
363
364 struct SvgClipNode
365 {
366 bool userSpace;
367 };
368
369 struct SvgMaskNode
370 {
371 SvgMaskType type;
372 bool userSpace;
373 };
374
375 struct SvgCssStyleNode
376 {
377 };
378
379 struct SvgTextNode
380 {
381 char* text;
382 char* fontFamily;
383 float x, y;
384 float fontSize;
385 };
386
387 struct SvgLinearGradient
388 {
389 float x1;
390 float y1;
391 float x2;
392 float y2;
393 bool isX1Percentage;
394 bool isY1Percentage;
395 bool isX2Percentage;
396 bool isY2Percentage;
397 };
398
399 struct SvgRadialGradient
400 {
401 float cx;
402 float cy;
403 float fx;
404 float fy;
405 float r;
406 float fr;
407 bool isCxPercentage;
408 bool isCyPercentage;
409 bool isFxPercentage;
410 bool isFyPercentage;
411 bool isRPercentage;
412 bool isFrPercentage;
413 };
414
415 struct SvgComposite
416 {
417 char *url;
418 SvgNode* node;
419 bool applying; //flag for checking circular dependency.
420 };
421
422 struct SvgColor
423 {
424 uint8_t r;
425 uint8_t g;
426 uint8_t b;
427 };
428
429 struct SvgPaint
430 {
431 SvgStyleGradient* gradient;
432 char *url;
433 SvgColor color;
434 bool none;
435 bool curColor;
436 };
437
438 struct SvgDash
439 {
440 Array<float> array;
441 float offset;
442 };
443
444 struct SvgStyleGradient
445 {
446 SvgGradientType type;
447 char* id;
448 char* ref;
449 FillSpread spread;
450 SvgRadialGradient* radial;
451 SvgLinearGradient* linear;
452 Matrix* transform;
453 Array<Fill::ColorStop> stops;
454 SvgGradientFlags flags;
455 bool userSpace;
456
457 void clear()
458 {
459 stops.reset();
460 free(transform);
461 free(radial);
462 free(linear);
463 free(ref);
464 free(id);
465 }
466 };
467
468 struct SvgStyleFill
469 {
470 SvgFillFlags flags;
471 SvgPaint paint;
472 int opacity;
473 FillRule fillRule;
474 };
475
476 struct SvgStyleStroke
477 {
478 SvgStrokeFlags flags;
479 SvgPaint paint;
480 int opacity;
481 float scale;
482 float width;
483 float centered;
484 StrokeCap cap;
485 StrokeJoin join;
486 float miterlimit;
487 SvgDash dash;
488 };
489
490 struct SvgStyleProperty
491 {
492 SvgStyleFill fill;
493 SvgStyleStroke stroke;
494 SvgComposite clipPath;
495 SvgComposite mask;
496 int opacity;
497 SvgColor color;
498 char* cssClass;
499 SvgStyleFlags flags;
500 SvgStyleFlags flagsImportance; //indicates the importance of the flag - if set, higher priority is applied (https://drafts.csswg.org/css-cascade-4/#importance)
501 bool curColorSet;
502 bool paintOrder; //true if default (fill, stroke), false otherwise
503 bool display;
504 };
505
506 struct SvgNode
507 {
508 SvgNodeType type;
509 SvgNode* parent;
510 Array<SvgNode*> child;
511 char *id;
512 SvgStyleProperty *style;
513 Matrix* transform;
514 union {
515 SvgGNode g;
516 SvgDocNode doc;
517 SvgDefsNode defs;
518 SvgUseNode use;
519 SvgCircleNode circle;
520 SvgEllipseNode ellipse;
521 SvgPolygonNode polygon;
522 SvgPolygonNode polyline;
523 SvgRectNode rect;
524 SvgPathNode path;
525 SvgLineNode line;
526 SvgImageNode image;
527 SvgMaskNode mask;
528 SvgClipNode clip;
529 SvgCssStyleNode cssStyle;
530 SvgSymbolNode symbol;
531 SvgTextNode text;
532 } node;
533 ~SvgNode();
534 };
535
536 struct SvgParser
537 {
538 SvgNode* node;
539 SvgStyleGradient* styleGrad;
540 Fill::ColorStop gradStop;
541 SvgStopStyleFlags flags;
542 struct
543 {
544 float x, y, w, h;
545 } global;
546 struct
547 {
548 bool parsedFx;
549 bool parsedFy;
550 } gradient;
551 };
552
553 struct SvgNodeIdPair
554 {
555 INLIST_ITEM(SvgNodeIdPair);
556 SvgNodeIdPair(SvgNode* n, char* i) : node{n}, id{i} {}
557 SvgNode* node;
558 char *id;
559 };
560
561 struct FontFace
562 {
563 char* name = nullptr;
564 char* src = nullptr;
565 size_t srcLen = 0;
566 char* decoded = nullptr;
567 };
568
569 enum class OpenedTagType : uint8_t
570 {
571 Other = 0,
572 Style,
573 Text
574 };
575
576 struct SvgLoaderData
577 {
578 Array<SvgNode*> stack;
579 SvgNode* doc = nullptr;
580 SvgNode* def = nullptr; //also used to store nested graphic nodes
581 SvgNode* cssStyle = nullptr;
582 Array<SvgStyleGradient*> gradients;
583 Array<SvgStyleGradient*> gradientStack; //For stops
584 SvgParser* svgParse = nullptr;
585 Inlist<SvgNodeIdPair> cloneNodes;
586 Array<SvgNodeIdPair> nodesToStyle;
587 Array<char*> images; //embedded images
588 Array<FontFace> fonts;
589 int level = 0;
590 bool result = false;
591 OpenedTagType openedTag = OpenedTagType::Other;
592 SvgNode* currentGraphicsNode = nullptr;
593 };
594
595 struct Box
596 {
597 float x, y, w, h;
598 };
599
600 #endif