-
Notifications
You must be signed in to change notification settings - Fork 101
/
sample01e.cpp
272 lines (228 loc) · 6.8 KB
/
sample01e.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <rapidxml_ns/rapidxml_ns.hpp>
#include <svgpp/policy/xml/rapidxml_ns.hpp>
#include <svgpp/svgpp.hpp>
using namespace svgpp;
typedef rapidxml_ns::xml_node<> const * xml_element_t;
class BaseContext
{
public:
void on_exit_element()
{}
void transform_matrix(const boost::array<double, 6> & matrix)
{}
void set_viewport(double viewport_x, double viewport_y, double viewport_width, double viewport_height)
{}
void set_viewbox_size(double viewbox_width, double viewbox_height)
{}
void disable_rendering()
{}
};
class ShapeContext: public BaseContext
{
public:
ShapeContext(BaseContext const & parent)
{}
void path_move_to(double x, double y, tag::coordinate::absolute)
{}
void path_line_to(double x, double y, tag::coordinate::absolute)
{}
void path_cubic_bezier_to(
double x1, double y1,
double x2, double y2,
double x, double y,
tag::coordinate::absolute)
{}
void path_quadratic_bezier_to(
double x1, double y1,
double x, double y,
tag::coordinate::absolute)
{}
void path_elliptical_arc_to(
double rx, double ry, double x_axis_rotation,
bool large_arc_flag, bool sweep_flag,
double x, double y,
tag::coordinate::absolute)
{}
void path_close_subpath()
{}
void path_exit()
{}
};
class UseContext: public BaseContext
{
public:
UseContext(BaseContext const & parent)
{}
boost::optional<double> const & width() const { return width_; }
boost::optional<double> const & height() const { return height_; }
template<class IRI>
void set(tag::attribute::xlink::href, tag::iri_fragment, IRI const & fragment)
{ fragment_id_.assign(boost::begin(fragment), boost::end(fragment)); }
template<class IRI>
void set(tag::attribute::xlink::href, IRI const & fragment)
{ std::cerr << "External references aren't supported\n"; }
void set(tag::attribute::x, double val)
{ x_ = val; }
void set(tag::attribute::y, double val)
{ y_ = val; }
void set(tag::attribute::width, double val)
{ width_ = val; }
void set(tag::attribute::height, double val)
{ height_ = val; }
void on_exit_element();
private:
std::string fragment_id_;
double x_, y_;
boost::optional<double> width_, height_;
};
class ReferencedSymbolOrSvgContext:
public BaseContext
{
public:
ReferencedSymbolOrSvgContext(UseContext & referencing)
: BaseContext(referencing)
, referencing_(referencing)
{
}
void get_reference_viewport_size(double & width, double & height)
{
if (referencing_.width())
width = *referencing_.width();
if (referencing_.height())
height = *referencing_.height();
}
private:
UseContext & referencing_;
};
struct ChildContextFactories
{
template<class ParentContext, class ElementTag, class Enable = void>
struct apply
{
// Default definition handles "svg" and "g" elements
typedef factory::context::on_stack<BaseContext> type;
};
};
// This specialization handles all shape elements (elements from traits::shape_elements sequence)
template<class ElementTag>
struct ChildContextFactories::apply<BaseContext, ElementTag,
typename boost::enable_if<boost::mpl::has_key<traits::shape_elements, ElementTag> >::type>
{
typedef factory::context::on_stack<ShapeContext> type;
};
template<>
struct ChildContextFactories::apply<BaseContext, tag::element::use_>
{
typedef factory::context::on_stack<UseContext> type;
};
// Elements referenced by 'use' element
template<>
struct ChildContextFactories::apply<UseContext, tag::element::svg, void>
{
typedef factory::context::on_stack<ReferencedSymbolOrSvgContext> type;
};
template<>
struct ChildContextFactories::apply<UseContext, tag::element::symbol, void>
{
typedef factory::context::on_stack<ReferencedSymbolOrSvgContext> type;
};
template<class ElementTag>
struct ChildContextFactories::apply<UseContext, ElementTag, void>: ChildContextFactories::apply<BaseContext, ElementTag>
{};
template<class ElementTag>
struct ChildContextFactories::apply<ReferencedSymbolOrSvgContext, ElementTag, void>:
ChildContextFactories::apply<BaseContext, ElementTag>
{};
typedef
boost::mpl::set<
// SVG Structural Elements
tag::element::svg,
tag::element::g,
tag::element::use_,
// SVG Shape Elements
tag::element::circle,
tag::element::ellipse,
tag::element::line,
tag::element::path,
tag::element::polygon,
tag::element::polyline,
tag::element::rect
>::type processed_elements_t;
// This cryptic code just merges predefined sequences traits::shapes_attributes_by_element
// and traits::viewport_attributes with tag::attribute::transform and tag::attribute::xlink::href
// attributes into single MPL sequence
typedef
boost::mpl::fold<
boost::mpl::protect<
boost::mpl::joint_view<
traits::shapes_attributes_by_element,
traits::viewport_attributes
>
>,
boost::mpl::set<
tag::attribute::transform,
boost::mpl::pair<tag::element::use_, tag::attribute::xlink::href>
>::type,
boost::mpl::insert<boost::mpl::_1, boost::mpl::_2>
>::type processed_attributes_t;
typedef
document_traversal<
processed_elements<processed_elements_t>,
processed_attributes<processed_attributes_t>,
viewport_policy<policy::viewport::as_transform>,
context_factories<ChildContextFactories>
> document_traversal_t;
void loadSvg(xml_element_t xml_root_element)
{
BaseContext context;
document_traversal_t::load_document(xml_root_element, context);
}
xml_element_t FindCurrentDocumentElementById(std::string const &)
{
// To be implemented.
return NULL;
}
void UseContext::on_exit_element()
{
if (xml_element_t element = FindCurrentDocumentElementById(fragment_id_))
{
// TODO: Check for cyclic references
// TODO: Apply translate transform (x_, y_)
document_traversal_t::load_referenced_element<
referencing_element<tag::element::use_>,
expected_elements<traits::reusable_elements>,
processed_elements<
boost::mpl::insert<processed_elements_t, tag::element::symbol>::type
>
>::load(element, *this);
}
else
std::cerr << "Element referenced by 'use' not found\n";
}
int main()
{
#define TEXT(x) #x
char text[] =
TEXT(<svg width="10cm" height="3cm" viewBox="0 0 100 30" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">)
TEXT( <defs>)
TEXT( <rect id="MyRect" width="60" height="10"/>)
TEXT( </defs>)
TEXT( <rect x=".1" y=".1" width="99.8" height="29.8"/>)
TEXT( <use x="20" y="10" xlink:href="#MyRect"/>)
TEXT(</svg>);
rapidxml_ns::xml_document<> doc; // character type defaults to char
try
{
doc.parse<0>(text);
if (rapidxml_ns::xml_node<> * svg_element = doc.first_node("svg"))
{
loadSvg(svg_element);
}
}
catch (std::exception const & e)
{
std::cerr << "Error loading SVG: " << e.what() << std::endl;
return 1;
}
return 0;
}