Repository navigation
Expand file tree
/
Copy pathreadSVG.cpp
More file actions
319 lines (267 loc) · 14.1 KB
/
Copy pathreadSVG.cpp
File metadata and controls
319 lines (267 loc) · 14.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <iostream>
#include "SVGElements.hpp"
#include "external/tinyxml2/tinyxml2.h"
#include <sstream>
using namespace std;
using namespace tinyxml2;
namespace svg
{
// Function declaration - definition later in the code
void readElements(XMLElement *root, XMLElement *elem,vector<SVGElement *>& svg_elements, vector<pair<string,Point>> &transformations);
void use(XMLElement *root, XMLElement *elem, string id ,vector<SVGElement *>& svg_elements, vector<pair<string,Point>> &transformations);
//checks if the transformation has got an origin
Point checktransformorigin(string & ori);
void readSVG(const string& svg_file, Point& dimensions, vector<SVGElement *>& svg_elements)
{
XMLDocument doc;
XMLError r = doc.LoadFile(svg_file.c_str());
if (r != XML_SUCCESS)
{
throw runtime_error("Unable to load " + svg_file);
}
XMLElement *xml_elem = doc.RootElement();
dimensions.x = xml_elem->IntAttribute("width");
dimensions.y = xml_elem->IntAttribute("height");
vector<pair<string,Point>> transformations;
readElements(xml_elem, doc.RootElement(),svg_elements,transformations);
}
void readElements(XMLElement *root, XMLElement *elem,vector<SVGElement *>& svg_elements, vector<pair<string,Point>> &transformations){
// HANDLE GROUPS
if (string(elem->Name())=="g")
{
if (elem->Attribute("transform")!=0){
if (elem->Attribute("transform-origin")!=0){
string ori = elem->Attribute("transform-origin");
transformations.push_back(pair<string,Point>(string(elem->Attribute("transform")),checktransformorigin(ori)));
}
else{
Point o = {0,0};
transformations.push_back(pair<string,Point>(elem->Attribute("transform"),o));
}
//Recursive function call for the group (to be done)
for (XMLElement *child = elem->FirstChildElement(); child != nullptr; child = child->NextSiblingElement())
{
readElements(root, child, svg_elements, transformations);
}
transformations.pop_back();
}
else{
for (XMLElement *child = elem->FirstChildElement(); child != nullptr; child = child->NextSiblingElement())
{
readElements(root, child, svg_elements, transformations);
}
}
}
else{
if (string(elem->Name())=="ellipse"){
//Define an Ellipse
Point p = {elem->IntAttribute("cx"), elem->IntAttribute("cy")};
Point radius = {elem->IntAttribute("rx"), elem->IntAttribute("ry")};
Ellipse* objeto = new Ellipse(parse_color(elem->Attribute("fill")), p, radius);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0){
Point o = {0,0};
if (elem->Attribute("transform-origin")!=0)
{
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori));
} else{
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
else if (string(elem->Name())=="circle"){
//Define a Circle
Point p = {elem->IntAttribute("cx"), elem->IntAttribute("cy")};
Point r = {elem->IntAttribute("r"),elem->IntAttribute("r")};
Circle* objeto = new Circle(parse_color(elem->Attribute("fill")), p, r);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0)
{
if (elem->Attribute("transform-origin")!=0)
{
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori));
} else{
Point o = {0,0};
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
else if (string(elem->Name())=="rect")
{
//Define a Rectangle
Point p = {elem->IntAttribute("x"), elem->IntAttribute("y")};
Rectangle* objeto = new Rectangle(parse_color(string(elem->Attribute("fill"))), elem->IntAttribute("height"), elem->IntAttribute("width"), p);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0)
{
if (elem->Attribute("transform-origin")!=0)
{
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori));
} else{
Point o = {0,0};
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
else if (string(elem->Name())=="polyline")
{
//Define a Polyline
string points = elem->Attribute("points");
vector <Point> cluster = {};
Point ponto;
for(char &c: points){
if (c==',' ){c=' ';}
}
istringstream turf(points);
while(turf){
turf>>ponto.x;
turf>>ponto.y;
cluster.push_back(ponto);
}
Polyline* objeto = new Polyline(parse_color(elem->Attribute("stroke")), cluster);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0)
{
if (elem->Attribute("transform-origin")!=0)
{
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori) );
} else{
Point o = {0,0};
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
else if (string(elem->Name())=="line")
{
//Define a Line
Point start = {elem->IntAttribute("x1"),elem->IntAttribute("y1")};
Point end = {elem->IntAttribute("x2"),elem->IntAttribute("y2")};
Line *objeto = new Line(parse_color(elem->Attribute("stroke")), end, start);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0)
{
if (elem->Attribute("transform-origin")!=0){
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori) );
}
else{
Point o = {0,0};
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
else if (string(elem->Name())=="polygon")
{
//Define a Polygon
string points = elem->Attribute("points");
vector <Point> poly = {};
Point ponto;
for(char &c: points){
if (c==',' ){c=' ';}
}
istringstream turf(points);
while(turf){
turf>>ponto.x;
turf>>ponto.y;
poly.push_back(ponto);
}
Polygon* objeto = new Polygon(parse_color(elem->Attribute("fill")), poly);
//Check if any transformation is applied, and transform if needed
if (elem->Attribute("transform")!=0)
{
if (elem->Attribute("transform-origin")!=0)
{
string ori = elem->Attribute("transform-origin");
objeto->transform(string(elem->Attribute("transform")), checktransformorigin(ori));
} else{
Point o = {0,0};
objeto->transform(string(elem->Attribute("transform")),o);
}
}
//Apply group transformations
for (vector<pair<string, Point>>::reverse_iterator trans = transformations.rbegin(); trans!=transformations.rend(); trans++){
objeto->transform(trans->first,trans->second);
}
// Push_Back the final object with all transformations applied
svg_elements.push_back(objeto);
}
//HANDLE USE's
else if (string(elem->Name())=="use")
{
string ref = elem->Attribute("href");
string id = ref.substr(1);
vector<pair<string,Point>> t_temp = transformations; //Create a temporary transformations vector to pass the group transformations applied to the use
//Check if <use> has any transformation, and store it in vector
if (elem->Attribute("transform")!=0){
if (elem->Attribute("transform-origin")!=0){
string ori = elem->Attribute("transform-origin");
t_temp.push_back(pair<string,Point>(elem->Attribute("transform"),checktransformorigin(ori)));
}
else{
Point o = {0,0};
t_temp.push_back(pair<string,Point>(elem->Attribute("transform"),o));
}
}
use(root,root,id,svg_elements,t_temp);
}
for (XMLElement *child = elem->FirstChildElement(); child != nullptr; child = child->NextSiblingElement()){
readElements(root, child, svg_elements, transformations);
}
}
}
void use(XMLElement *root, XMLElement *elem, string id ,vector<SVGElement *>& svg_elements, vector<pair<string,Point>> &transformations){
if (elem->Attribute("id")!=0){
string i = elem->Attribute("id");
if (i==id){
readElements(root,elem,svg_elements,transformations);
}
}
for (XMLElement *child = elem->FirstChildElement(); child != nullptr; child = child->NextSiblingElement()){
use(root, child, id, svg_elements, transformations);
}
}
Point checktransformorigin(string & ori){
Point o;
for(char &c: ori){
if (c==','||c=='(' ||c==')' ){c=' ';}
}
istringstream cogumelo(ori);
cogumelo>>o.x;
cogumelo>>o.y;
return o;
}
}