Line data Source code
1 : /**
2 : Copyright (c) 2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_VG_SPSVGREADER_H_
25 : #define STAPPLER_VG_SPSVGREADER_H_
26 :
27 : #include "SPHtmlParser.h"
28 : #include "SPVectorPath.h"
29 : #include "SPGeometry.h"
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::vg {
32 :
33 : using Metric = geom::Metric;
34 :
35 : struct SvgTag : public html::Tag<StringView> {
36 1100 : SvgTag(StringView &r) : Tag(r) {
37 1100 : rpath.setFillColor(Color4B::BLACK);
38 1100 : rpath.setStrokeColor(Color4B::BLACK);
39 1100 : }
40 :
41 : enum Shape {
42 : None,
43 : Rect,
44 : Circle,
45 : Ellipse,
46 : Line,
47 : Polyline,
48 : Polygon,
49 : Use,
50 : } shape = None;
51 :
52 : // Coords layouts:
53 : // Rect: x, y, width, height, rx,ry
54 : // Circle: cx, cy, r
55 : // Ellipse: cx, cy, rx, ry
56 : // Line: x1, y1, x2, y2
57 : // Polyline: write directly to path
58 : // Polygon: write directly to path
59 : Mat4 mat = Mat4::INVALID;
60 : StringView id;
61 : StringView ref;
62 : VectorPath rpath;
63 : PathWriter writer;
64 :
65 : VectorPath &getPath();
66 :
67 : PathWriter &getWriter();
68 : };
69 :
70 : struct SvgReader {
71 : using Parser = html::Parser<SvgReader, StringView, SvgTag>;
72 : using Tag = SvgTag;
73 : using StringReader = Parser::StringReader;
74 :
75 : SvgReader();
76 :
77 : void onBeginTag(Parser &p, Tag &tag);
78 : void onEndTag(Parser &p, Tag &tag, bool isClosed);
79 : void onStyleParameter(Tag &tag, StringReader &name, StringReader &value);
80 : void onStyle(Tag &tag, StringReader &value);
81 : void onTagAttribute(Parser &p, Tag &tag, StringReader &name, StringReader &value);
82 : void onPushTag(Parser &p, Tag &tag);
83 : void onPopTag(Parser &p, Tag &tag);
84 : void onInlineTag(Parser &p, Tag &tag);
85 :
86 : bool _defs = false;
87 : float _squareLength = 0.0f;
88 : float _width = 0;
89 : float _height = 0;
90 : uint16_t _nextId = 0;
91 :
92 : Rect _viewBox;
93 : Interface::VectorType<PathXRef> _drawOrder;
94 : Interface::MapType<Interface::StringType, VectorPath> _paths;
95 : };
96 :
97 : }
98 :
99 : #endif /* STAPPLER_VG_SPSVGREADER_H_ */
|