forked from sammycage/lunasvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.h
222 lines (190 loc) · 4.37 KB
/
element.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef ELEMENT_H
#define ELEMENT_H
#include <memory>
#include <list>
#include "property.h"
#include "lunasvg.h"
namespace lunasvg {
enum class ElementID {
Unknown = 0,
Star,
Circle,
ClipPath,
Defs,
Ellipse,
G,
Image,
Line,
LinearGradient,
Marker,
Mask,
Path,
Pattern,
Polygon,
Polyline,
RadialGradient,
Rect,
SolidColor,
Stop,
Style,
Svg,
Symbol,
Text,
TSpan,
Use
};
enum class PropertyID {
Unknown = 0,
Class,
Clip_Path,
Clip_Rule,
ClipPathUnits,
Color,
Cx,
Cy,
D,
Display,
Fill,
Fill_Opacity,
Fill_Rule,
Fx,
Fy,
GradientTransform,
GradientUnits,
Height,
Href,
Id,
Marker_End,
Marker_Mid,
Marker_Start,
MarkerHeight,
MarkerUnits,
MarkerWidth,
Mask,
MaskContentUnits,
MaskUnits,
Offset,
Opacity,
Orient,
Overflow,
PatternContentUnits,
PatternTransform,
PatternUnits,
Points,
PreserveAspectRatio,
R,
RefX,
RefY,
Rx,
Ry,
Solid_Color,
Solid_Opacity,
SpreadMethod,
Stop_Color,
Stop_Opacity,
Stroke,
Stroke_Dasharray,
Stroke_Dashoffset,
Stroke_Linecap,
Stroke_Linejoin,
Stroke_Miterlimit,
Stroke_Opacity,
Stroke_Width,
Style,
Transform,
ViewBox,
Visibility,
Width,
X,
X1,
X2,
Y,
Y1,
Y2
};
ElementID elementid(const std::string& name);
PropertyID csspropertyid(const std::string& name);
PropertyID propertyid(const std::string& name);
struct Property {
int specificity;
PropertyID id;
std::string value;
};
using PropertyList = std::vector<Property>;
template<typename T, typename... Args>
inline std::unique_ptr<T> makeUnique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
class LayoutObject;
class LayoutContainer;
class LayoutContext;
class Element;
class Node {
public:
Node() = default;
virtual ~Node() = default;
virtual bool isText() const { return false; }
virtual bool isPaint() const { return false; }
virtual bool isGeometry() const { return false; }
virtual void layout(LayoutContext*, LayoutContainer*) {}
virtual std::unique_ptr<Node> clone() const = 0;
void setParent(Element* parent) { m_parent = parent; }
Element* parent() const { return m_parent; }
LayoutObject* box() const { return m_box; }
void setBox(LayoutObject* box) { m_box = box; }
private:
Element* m_parent = nullptr;
LayoutObject* m_box = nullptr;
};
class TextNode final : public Node {
public:
TextNode() = default;
bool isText() const final { return true; }
std::unique_ptr<Node> clone() const final;
void setText(std::string text) { m_text = std::move(text); }
const std::string& text() const { return m_text; }
private:
std::string m_text;
};
using NodeList = std::list<std::unique_ptr<Node>>;
class Element : public Node {
public:
static std::unique_ptr<Element> create(ElementID id);
void set(PropertyID id, const std::string& value, int specificity);
const std::string& get(PropertyID id) const;
const std::string& find(PropertyID id) const;
bool has(PropertyID id) const;
const PropertyList& properties() const { return m_properties; }
void setPropertyList(PropertyList properties) { m_properties = std::move(properties); }
Element* previousElement() const;
Element* nextElement() const;
Node* addChild(std::unique_ptr<Node> child);
void layoutChildren(LayoutContext* context, LayoutContainer* current);
Rect currentViewport() const;
ElementID id() const { return m_id; }
const NodeList& children() const { return m_children; }
virtual void build(const Document* document);
template<typename T>
void transverse(T callback) {
if(!callback(this))
return;
for(auto& child : m_children) {
if(child->isText()) {
if(!callback(child.get()))
return;
continue;
}
auto element = static_cast<Element*>(child.get());
element->transverse(callback);
}
}
std::unique_ptr<Node> clone() const final;
protected:
Element(ElementID id);
ElementID m_id;
NodeList m_children;
PropertyList m_properties;
};
} // namespace lunasvg
#endif // ELEMENT_H