Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
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
12 : all 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
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef CORE_VG_SPVECTORPATHDATA_H_
24 : #define CORE_VG_SPVECTORPATHDATA_H_
25 :
26 : #include "SPColor.h"
27 : #include "SPGeometry.h"
28 : #include "SPMat4.h"
29 : #include "SPTessLine.h"
30 : #include "SPMemory.h"
31 :
32 : namespace STAPPLER_VERSIONIZED stappler::vg {
33 :
34 : using namespace geom;
35 :
36 : union CommandData {
37 : struct {
38 : float x;
39 : float y;
40 : } p;
41 : struct {
42 : float v;
43 : bool a;
44 : bool b;
45 : } f;
46 :
47 24631744 : CommandData(float x, float y) { p.x = x; p.y = y; }
48 172810 : CommandData(float r, bool a, bool b) { f.v = r; f.a = a; f.b = b; }
49 25 : CommandData() { p = {0.0f, 0.0f}; }
50 : };
51 :
52 : enum class Command : uint8_t { // use hint to decode data from `_points` vector
53 : MoveTo, // (x, y)
54 : LineTo, // (x, y)
55 : QuadTo, // (x1, y1) (x2, y2)
56 : CubicTo, // (x1, y1) (x2, y2) (x3, y3)
57 : ArcTo, // (rx, ry), (x, y), (rotation, largeFlag, sweepFlag)
58 : ClosePath, // nothing
59 : };
60 :
61 : struct PathParams {
62 : Mat4 transform;
63 : Color4B fillColor = Color4B(255, 255, 255, 255);
64 : Color4B strokeColor = Color4B(255, 255, 255, 255);
65 : DrawStyle style = DrawStyle::Fill;
66 : float strokeWidth = 1.0f;
67 :
68 : Winding winding = Winding::NonZero;
69 : LineCup lineCup = LineCup::Butt;
70 : LineJoin lineJoin = LineJoin::Miter;
71 : float miterLimit = 4.0f;
72 : bool isAntialiased = true;
73 : };
74 :
75 : struct PathWriter;
76 :
77 : template <typename Interface>
78 : struct PathData : Interface::AllocBaseType {
79 : template <typename Value>
80 : using Vector = typename Interface::template VectorType<Value>;
81 :
82 : Vector<CommandData> points;
83 : Vector<Command> commands;
84 : PathParams params;
85 :
86 549278 : PathData() = default;
87 1025 : PathData(const PathData &) = default;
88 75 : PathData &operator=(const PathData &) = default;
89 :
90 548353 : PathData(PathData &&) = default;
91 50 : PathData &operator=(PathData &&) = default;
92 :
93 : void clear();
94 :
95 : PathWriter getWriter();
96 :
97 : template <typename OutInterface>
98 : auto encode() const -> typename OutInterface::BytesType;
99 :
100 : template <typename OutInterface>
101 : auto toString(bool newline = false) const -> typename OutInterface::StringType;
102 : };
103 :
104 : struct PathWriter {
105 : VectorAdapter<CommandData> points;
106 : VectorAdapter<Command> commands;
107 :
108 : PathWriter(PathData<mem_std::Interface> &);
109 : PathWriter(PathData<mem_pool::Interface> &);
110 :
111 1100 : PathWriter() = default;
112 : PathWriter(const PathWriter &) = default;
113 : PathWriter &operator=(const PathWriter &) = default;
114 :
115 : PathWriter(PathWriter &&) = default;
116 : PathWriter &operator=(PathWriter &&) = default;
117 :
118 : explicit operator bool () const;
119 :
120 : bool empty() const;
121 :
122 : void reserve(size_t);
123 :
124 : bool readFromPathString(StringView);
125 : bool readFromFileContent(StringView);
126 : bool readFromFile(StringView);
127 : bool readFromBytes(BytesView);
128 :
129 : PathWriter &moveTo(float x, float y);
130 : PathWriter &moveTo(const Vec2 &point);
131 : PathWriter &lineTo(float x, float y);
132 : PathWriter &lineTo(const Vec2 &point);
133 : PathWriter &quadTo(float x1, float y1, float x2, float y2);
134 : PathWriter &quadTo(const Vec2& p1, const Vec2& p2);
135 : PathWriter &cubicTo(float x1, float y1, float x2, float y2, float x3, float y3);
136 : PathWriter &cubicTo(const Vec2& p1, const Vec2& p2, const Vec2& p3);
137 :
138 : PathWriter &arcTo(float rx, float ry, float rotation, bool largeFlag, bool sweepFlag, float x, float y);
139 : PathWriter &arcTo(const Vec2 & r, float rotation, bool largeFlag, bool sweepFlag, const Vec2 &target);
140 : PathWriter &closePath();
141 : PathWriter &addRect(const Rect& rect);
142 : PathWriter &addRect(const Rect& rect, float rx, float ry);
143 : PathWriter &addRect(float x, float y, float width, float height);
144 : PathWriter &addOval(const Rect& oval);
145 : PathWriter &addCircle(float x, float y, float radius);
146 : PathWriter &addEllipse(float x, float y, float rx, float ry);
147 : PathWriter &addArc(const Rect& oval, float startAngleInRadians, float sweepAngleInRadians);
148 : PathWriter &addRect(float x, float y, float width, float height, float rx, float ry);
149 :
150 : bool addPath(const PathData<memory::StandartInterface> &);
151 : bool addPath(const PathData<memory::PoolInterface> &);
152 : bool addPath(BytesView);
153 : bool addPath(StringView);
154 : };
155 :
156 :
157 : }
158 :
159 : #endif /* CORE_VG_SPVECTORPATHDATA_H_ */
|