Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / modules / mod_svg / svg_parser.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file svg_parser.cpp
3 **      \brief Implementation of the Svg parser
4 **      \brief Based on SVG XML specification 1.1
5 **      \brief See: http://www.w3.org/TR/xml11/ for deatils
6 **
7 **      $Id:$
8 **
9 **      \legal
10 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
11 **      Copyright (c) 2008 Chris Moore
12 **      Copyright (c) 2009 Carlos A. Sosa Navarro
13 **
14 **      This package is free software; you can redistribute it and/or
15 **      modify it under the terms of the GNU General Public License as
16 **      published by the Free Software Foundation; either version 2 of
17 **      the License, or (at your option) any later version.
18 **
19 **      This package is distributed in the hope that it will be useful,
20 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 **      General Public License for more details.
23 **      \endlegal
24 */
25 /* ========================================================================= */
26
27 /* === H E A D E R S ======================================================= */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <iostream>
34 #include "svg_parser.h"
35
36 /* === U S I N G =========================================================== */
37
38 using namespace synfig;
39
40 /* === G L O B A L S ======================================================= */
41
42 /* === P R O C E D U R E S ================================================= */
43
44 Canvas::Handle
45 synfig::open_svg(std::string _filepath,String &errors, String &warnings){
46         Canvas::Handle canvas;
47         Svg_parser parser;
48         try
49         {
50                 canvas=parser.load_svg_canvas(_filepath,errors,warnings);
51                 //canvas->set_id(parser.get_id());
52         }catch(...){
53                 std::cout<<"error"<<std::endl;
54         }
55         return canvas;
56 }
57
58 Canvas::Handle
59 Svg_parser::load_svg_canvas(std::string _filepath,String &errors, String &warnings){
60         filepath = _filepath;
61         #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
62         try{
63         #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
64                 //load parser
65                 parser.set_substitute_entities();
66                 parser.parse_file(filepath);
67                 //set_id(filepath);
68                 if(parser){
69                         const xmlpp::Node* pNode = parser.get_document()->get_root_node();
70                         parser_node(pNode);
71                 }
72         #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
73         }catch(const std::exception& ex){
74         std::cout << "Exception caught: " << ex.what() << std::endl;
75         }
76         #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
77         Canvas::Handle canvas;
78         if(nodeRoot){
79                 //canvas=synfig::open_canvas(nodeRoot,_filepath,errors,warnings);
80                 canvas=synfig::open_canvas(nodeRoot,errors,warnings);
81         }
82         return canvas;
83 }
84
85 Svg_parser::Svg_parser(){
86         uid=0;
87         kux=60;
88         set_canvas=0;//we must run parser_canvas method
89         // 0.5 in gamma parameter of color correct layer is 1/0.5 = 2 (thinking) it must be 2.2!!!!
90         gamma.set_gamma(2.2);
91 }
92 /*
93 String
94 Svg_parser::get_id(){
95         if(!id_name.empty()) return id_name;
96         return "id_arbitrario";
97 }
98 void
99 Svg_parser::set_id(String source){
100         const char bad_chars[]=" :#@$^&()*";
101         int inicio=     source.find_last_of('/')+1;
102         int fin=        source.find_last_of('.');
103         String x=source.substr(inicio,fin-inicio);
104         if(!x.empty()){
105                 for(unsigned int i=0;i<sizeof(bad_chars);i++){
106                         unsigned int pos=x.find_first_of(bad_chars[i]);
107                         if(pos!=String::npos)
108                                 x.erase(pos,1);
109                 }
110         }
111         if(!x.empty()){
112                 id_name=x;
113         }else{
114                 id_name="id_arbitrario";
115         }
116 }
117 */
118 //UPDATE
119
120 void
121 Svg_parser::parser_node(const xmlpp::Node* node){
122         const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
123         const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
124         const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
125
126         if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
127         return;
128
129         Glib::ustring nodename = node->get_name();
130         if(!nodeText && !nodeComment && !nodename.empty()){
131                 if(nodename.compare("svg")==0){
132                         parser_svg (node);
133                 }else if(nodename.compare("namedview")==0){
134                         parser_canvas(node);
135                 }else if(nodename.compare("defs")==0){
136                         parser_defs (node);
137                 }else if(nodename.compare("g")==0){
138                         if(set_canvas==0) parser_canvas (node);
139                         parser_layer (node,nodeRoot->add_child("layer"),"",NULL);
140                         return;
141                 }else if(nodename.compare("rect")==0){
142                         if(set_canvas==0) parser_canvas (node);
143                         parser_rect(node,nodeRoot,"",NULL);
144                 }else if(nodename.compare("polygon")==0){
145                         if(set_canvas==0) parser_canvas (node);
146                         parser_polygon(node,nodeRoot,"",NULL);
147                 }else if(nodename.compare("path")==0){
148                         if(set_canvas==0) parser_canvas (node);
149                         parser_path (node,nodeRoot,"",NULL);
150                 }
151         }
152         if(!nodeContent){
153         xmlpp::Node::NodeList list = node->get_children();
154         for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
155                 parser_node(*iter); //recursive
156         }
157         }
158 }
159
160 //parser elements
161 void
162 Svg_parser::parser_svg (const xmlpp::Node* node){
163         //printf("un dia en algun lugar de la tierra media\n");
164         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
165                 width   =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("width")));
166                 height  =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("height")));
167                 docname=nodeElement->get_attribute_value("docname","");
168         }
169 }
170 void
171 Svg_parser::parser_canvas (const xmlpp::Node* node){
172         //printf("el campo de batalla parecia un lienzo de pintura\n");
173         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
174                 if(width.compare("")==0){
175                         width=nodeElement->get_attribute_value("width","");
176                 }
177                 if(height.compare("")==0){
178                         height=nodeElement->get_attribute_value("height","");
179                 }
180                 if(width.compare("")==0 && height.compare("")!=0){
181                         width=height;
182                 }
183                 if(width.compare("")!=0 && height.compare("")==0){
184                         height=width;
185                 }
186                 if(height.compare("")==0 && width.compare("")==0){
187                         width="1024";
188                         height="768";
189                 }
190                 //build
191                 nodeRoot=document.create_root_node("canvas", "", "");
192                 nodeRoot->set_attribute("version","0.5");
193                 nodeRoot->set_attribute("width",width);
194                 nodeRoot->set_attribute("height",height);
195                 nodeRoot->set_attribute("xres","2834.645752");
196                 nodeRoot->set_attribute("yres","2834.645752");
197                 float view_x;
198                 float view_y;
199                 view_x=atof(width.c_str())/kux;
200                 view_y=atof(height.c_str())/kux;
201                 view_x=view_x/2.0;
202                 view_y=view_y/2.0;
203                 char attr_view_box[60];
204                 sprintf(attr_view_box,"%f %f %f %f",-1.0*view_x,view_y,view_x,-1.0*view_y);
205                 nodeRoot->set_attribute("view-box",attr_view_box);
206                 ox=atof(width.c_str() )/2;
207                 oy=atof(height.c_str())/2;
208                 nodeRoot->set_attribute("antialias","1");
209                 nodeRoot->set_attribute("fps","24.000");
210                 nodeRoot->set_attribute("begin-time","0f");
211                 nodeRoot->set_attribute("end-time","5s");
212                 nodeRoot->set_attribute("bgcolor","0.500000 0.500000 0.500000 1.000000");
213                 //nodeRoot->add_child("name")->set_child_text("Synfig Animation 1");
214                 if(!id_name.empty()) nodeRoot->add_child("name")->set_child_text(id_name);
215                 else nodeRoot->add_child("name")->set_child_text("Synfig Animation 1");
216         }
217         set_canvas=1;
218         AdjustPointUrl ();
219 }
220
221 void
222 Svg_parser::parser_rect(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
223         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
224                 Glib::ustring rect_id           =nodeElement->get_attribute_value("id");
225                 Glib::ustring rect_style        =nodeElement->get_attribute_value("style");
226                 Glib::ustring rect_x            =nodeElement->get_attribute_value("x");
227                 Glib::ustring rect_y            =nodeElement->get_attribute_value("y");
228                 Glib::ustring rect_width        =nodeElement->get_attribute_value("width");
229                 Glib::ustring rect_height       =nodeElement->get_attribute_value("height");
230                 //style
231                 String fill                     =loadAttribute("fill",rect_style,parent_style,"none");
232                 String fill_opacity =loadAttribute("fill-opacity",rect_style,parent_style,"1");
233                 String opacity          =loadAttribute("opacity",rect_style,parent_style,"1");
234                 //matrix
235                 //it's some complicated
236
237                 //build
238
239                 int typeFill=0;
240                 if(fill.compare(0,3,"url")==0){
241                         typeFill=2;
242                         root=nodeStartBasicLayer(root->add_child("layer"));
243                 }
244                 xmlpp::Element *child_rect=root->add_child("layer");
245                 child_rect->set_attribute("type","rectangle");
246                 child_rect->set_attribute("active","true");
247                 child_rect->set_attribute("version","0.2");
248                 child_rect->set_attribute("desc",rect_id);
249
250                 build_real(child_rect->add_child("param"),"z_depth",0.0);
251                 build_real(child_rect->add_child("param"),"amount",1.0);
252                 build_integer(child_rect->add_child("param"),"blend_method",0);
253                 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
254
255                 float auxx=atof(rect_x.c_str());
256                 float auxy=atof(rect_y.c_str());
257                 coor2vect(&auxx,&auxy);
258                 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
259                 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
260                 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
261                 coor2vect(&auxx,&auxy);
262                 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
263                 if(typeFill==2){
264                         build_url (root->add_child("layer"),fill,mtx_parent);
265                 }
266         }
267 }
268 void
269 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
270         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
271                 Glib::ustring label             =nodeElement->get_attribute_value("label");
272                 Glib::ustring style             =nodeElement->get_attribute_value("style");
273                 Glib::ustring fill              =nodeElement->get_attribute_value("fill");
274                 Glib::ustring transform =nodeElement->get_attribute_value("transform");
275
276                 String layer_style;
277                 if(!style.empty()){
278                         layer_style=style;
279                 }else if(!fill.empty()){
280                         layer_style.append("fill:");
281                         layer_style.append(fill);
282                 }else if(!parent_style.empty()){
283                         layer_style=parent_style;
284                 }
285                 Matrix* mtx=NULL;
286                 if(!transform.empty())
287                         mtx=build_transform (transform);
288                 if(mtx_parent){
289                         if(mtx)
290                                 composeMatrix(&mtx,mtx_parent,mtx);
291                         else
292                                 mtx=newMatrix (mtx_parent);
293                 }
294                 //build
295                 root->set_attribute("type","PasteCanvas");
296                 root->set_attribute("active","true");
297                 root->set_attribute("version","0.1");
298                 if(!label.empty())      root->set_attribute("desc",label);
299                 else            root->set_attribute("desc","unknow layer");
300
301                 build_real(root->add_child("param"),"z_depth",0.0);
302                 build_real(root->add_child("param"),"amount",1.0);
303                 build_integer(root->add_child("param"),"blend_method",0);
304                 build_vector (root->add_child("param"),"origin",0,0);
305
306                 //printf(" atributos canvas ");
307                 //canvas
308                 xmlpp::Element *child_canvas=root->add_child("param");
309                 child_canvas->set_attribute("name","canvas");
310                 child_canvas=child_canvas->add_child("canvas");
311                 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
312                 if(!nodeContent){
313                 xmlpp::Node::NodeList list = node->get_children();
314                 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
315                                 Glib::ustring name =(*iter)->get_name();
316                                 if(name.compare("g")==0){
317                                         parser_layer (*iter,child_canvas->add_child("layer"),layer_style,mtx);
318                                 }else if(name.compare("path")==0){
319                                         parser_path (*iter,child_canvas,layer_style,mtx);
320                                 }else if(name.compare("polygon")==0){
321                                         parser_polygon (*iter,child_canvas,layer_style,mtx);
322                                 }else if(name.compare("rect")==0){
323                                         parser_rect (*iter,child_canvas,layer_style,mtx);
324                                 }
325                 }
326                 }
327         }
328 }
329 void
330 Svg_parser::parser_polygon(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
331         //printf("sus escudos parecian rombos y sus naves unos triangulos\n");
332         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
333                 //load sub-attributes
334                 Glib::ustring polygon_style                     =nodeElement->get_attribute_value("style");
335                 Glib::ustring polygon_id                        =nodeElement->get_attribute_value("id");
336                 Glib::ustring polygon_points            =nodeElement->get_attribute_value("points");
337                 Glib::ustring polygon_transform         =nodeElement->get_attribute_value("transform");
338                 Glib::ustring polygon_fill                      =nodeElement->get_attribute_value("fill");
339                 String fill                     =loadAttribute("fill",polygon_style,parent_style,polygon_fill,"none");
340                 String fill_rule        =loadAttribute("fill-rule",polygon_style,parent_style,"evenodd");
341                 String fill_opacity     =loadAttribute("fill-opacity",polygon_style,parent_style,"1");
342                 String opacity          =loadAttribute("opacity",polygon_style,parent_style,"1");
343
344                 //transforms
345                 Matrix* mtx=NULL;
346                 if(!polygon_transform.empty())
347                         mtx=build_transform (polygon_transform);
348                 if(mtx_parent){
349                         if(mtx)
350                                 composeMatrix(&mtx,mtx_parent,mtx);
351                         else
352                                 mtx=newMatrix (mtx_parent);
353                 }
354                 //points
355                 if(polygon_points.empty())
356                         return;
357                 std::list<Vertice*> k;
358                 std::vector<String> tokens=get_tokens_path (polygon_points);
359                 unsigned int i;
360                 float ax,ay; ax=ay=0;
361                 for(i=0;i<tokens.size();i++){
362                         ax=atof(tokens.at(i).data());
363                         i++; if(tokens.at(i).compare(",")==0) i++;
364                         ay=atof(tokens.at(i).data());
365                         //mtx
366                         if(mtx) transformPoint2D(mtx,&ax,&ay);
367                         //adjust
368                         coor2vect(&ax,&ay);
369                         //save
370                         k.push_back(nuevoVertice(ax,ay));
371                 }
372                 //escritura
373                 xmlpp::Element *child_polygon;
374
375                 //gradient
376                 int typeFill=0;
377                 if(fill.compare(0,3,"url")==0){
378                         typeFill=2;//gradient
379                         root=nodeStartBasicLayer(root->add_child("layer"));
380                 }
381                 child_polygon=root->add_child("layer");
382                 child_polygon->set_attribute("type","polygon");
383                 child_polygon->set_attribute("active","true");
384                 child_polygon->set_attribute("version","0.1");
385                 child_polygon->set_attribute("desc",polygon_id);
386                 build_param (child_polygon->add_child("param"),"z_depth","real","0.0000000000");
387                 build_param (child_polygon->add_child("param"),"amount","real","1.0000000000");
388                 build_param (child_polygon->add_child("param"),"blend_method","integer","0");
389                 build_color (child_polygon->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
390                 build_vector(child_polygon->add_child("param"),"offset",0,0);
391                 build_param (child_polygon->add_child("param"),"invert","bool","false");
392                 build_param (child_polygon->add_child("param"),"antialias","bool","true");
393                 build_param (child_polygon->add_child("param"),"feather","real","0.0000000000");
394                 build_param (child_polygon->add_child("param"),"blurtype","integer","1");
395                 if(fill_rule.compare("evenodd")==0) build_param (child_polygon->add_child("param"),"winding_style","integer","1");
396                 else build_param (child_polygon->add_child("param"),"winding_style","integer","0");
397                 build_points (child_polygon->add_child("param"),k);
398
399                 if(typeFill==2){
400                         build_url(root->add_child("layer"),fill,mtx);
401                 }
402         }
403 }
404 void
405 Svg_parser::parser_path(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
406         //printf("pensamos que atacarian de frente pero hicieron una curva\n");
407         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
408                 //load sub-attributes
409                 Glib::ustring path_style                =nodeElement->get_attribute_value("style");
410                 Glib::ustring path_id                   =nodeElement->get_attribute_value("id");
411                 Glib::ustring path_d                    =nodeElement->get_attribute_value("d");
412                 Glib::ustring path_transform    =nodeElement->get_attribute_value("transform");
413                 Glib::ustring path_fill                 =nodeElement->get_attribute_value("fill");
414
415                 String fill                             =loadAttribute("fill",path_style,parent_style,path_fill,"none");
416                 String fill_rule                =loadAttribute("fill-rule",path_style,parent_style,"evenodd");
417                 String stroke                   =loadAttribute("stroke",path_style,parent_style,"none");
418                 String stroke_width             =loadAttribute("stroke-width",path_style,parent_style,"1px");
419                 String stroke_linecap   =loadAttribute("stroke-linecap",path_style,parent_style,"butt");
420                 String stroke_linejoin  =loadAttribute("stroke-linejoin",path_style,parent_style,"miter");
421                 String stroke_opacity   =loadAttribute("stroke-opacity",path_style,parent_style,"1");
422                 String fill_opacity             =loadAttribute("fill-opacity",path_style,parent_style,"1");
423                 String opacity                  =loadAttribute("opacity",path_style,parent_style,"1");
424
425                 //resolve transformations
426                 Matrix* mtx=NULL;
427                 if(!path_transform.empty())
428                         mtx=build_transform (path_transform);
429                 if(mtx_parent){
430                         if(mtx)
431                                 composeMatrix(&mtx,mtx_parent,mtx);
432                         else
433                                 mtx=newMatrix(mtx_parent);
434                 }
435                 //parser path_d attribute, this is obviously important
436                 std::list<std::list<Vertice*> > k;
437                 k=parser_path_d (path_d,mtx);
438
439                 //escribir
440                 int typeFill=0; //nothing
441                 int typeStroke=0;//nothing
442                 //Fill
443                 if(fill.compare("none")!=0){
444                         typeFill=1; //simple
445                 }
446                 if(typeFill==1 && fill.compare(0,3,"url")==0){
447                         typeFill=2;     //gradient
448                 }
449                 //Stroke
450                 if(stroke.compare("none")!=0){
451                         typeStroke=1; //simple
452                 }
453                 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
454                         typeStroke=2;   //gradient
455                 }
456                 String bline_id;
457                 String offset_id;
458                 int n=k.size();
459                 if(n!=1){ //if n is > than 1 then we must create a paste canvas for all paths
460                         root=nodeStartBasicLayer(root->add_child("layer"));
461                 }
462                 std::list<std::list<Vertice*> >::iterator aux = k.begin();
463                 for (; aux!=k.end(); aux++){
464                         if(typeFill!=0 && typeStroke!=0){
465                                 bline_id=new_guid();
466                                 offset_id=new_guid();
467                         }
468                         if(typeFill==1 || typeFill==2){//region layer
469                                 xmlpp::Element *child_fill=root;
470                                 if(n==1 && typeFill==2){//open gradient or url (fill)
471                                         child_fill=nodeStartBasicLayer(root->add_child("layer"));
472                                 }
473                                 xmlpp::Element *child_region=child_fill->add_child("layer");
474                                 child_region->set_attribute("type","region");
475                                 child_region->set_attribute("active","true");
476                                 child_region->set_attribute("version","0.1");
477                                 child_region->set_attribute("desc",path_id);
478                                 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
479                                 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
480                                 build_param (child_region->add_child("param"),"blend_method","integer","0");
481                                 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
482                                 if(offset_id.empty())   build_vector (child_region->add_child("param"),"offset",0,0);
483                                 else    build_vector (child_region->add_child("param"),"offset",0,0,offset_id);
484                                 build_param (child_region->add_child("param"),"invert","bool","false");
485                                 build_param (child_region->add_child("param"),"antialias","bool","true");
486                                 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
487                                 build_param (child_region->add_child("param"),"blurtype","integer","1");
488                                 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
489                                 else build_param (child_region->add_child("param"),"winding_style","integer","0");
490
491                                 build_bline (child_region->add_child("param"),*aux,loop,bline_id);
492
493                                 if(n==1 && typeFill==2){ //gradient in onto mode (fill)
494                                         build_url(child_fill->add_child("layer"),fill,mtx);
495                                 }
496                         }
497
498                         if(typeStroke==1 || typeStroke==2){     //layer outline
499                                 xmlpp::Element *child_stroke=root;
500                                 if(n==1 && typeStroke==2){//open gradient in straigth onto (stroke)
501                                         child_stroke=nodeStartBasicLayer(root->add_child("layer"));
502                                 }
503                                 xmlpp::Element *child_outline=child_stroke->add_child("layer");
504                                 child_outline->set_attribute("type","outline");
505                                 child_outline->set_attribute("active","true");
506                                 child_outline->set_attribute("version","0.2");
507                                 child_outline->set_attribute("desc",path_id);
508                                 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
509                                 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
510                                 build_param (child_outline->add_child("param"),"blend_method","integer","0");
511                                 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
512                                 if(offset_id.empty()) build_vector (child_outline->add_child("param"),"offset",0,0);
513                                 else build_vector (child_outline->add_child("param"),"offset",0,0,offset_id);
514                                 build_param (child_outline->add_child("param"),"invert","bool","false");
515                                 build_param (child_outline->add_child("param"),"antialias","bool","true");
516                                 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
517                                 build_param (child_outline->add_child("param"),"blurtype","integer","1");
518                                 //outline in nonzero
519                                 build_param (child_outline->add_child("param"),"winding_style","integer","0");
520
521                                 build_bline (child_outline->add_child("param"),*aux,loop,bline_id);
522
523                                 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
524                                 build_param (child_outline->add_child("param"),"width","real",stroke_width);
525                                 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
526                                 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
527                                 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
528                                 if(stroke_linecap.compare("butt")==0){
529                                         build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
530                                         build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
531                                 }else{
532                                         build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
533                                         build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
534                                 }
535                                 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
536                                 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
537
538                                 if(n==1 && typeStroke==2){ //gradient in onto mode (stroke)
539                                         build_url(child_stroke->add_child("layer"),stroke,mtx);
540                                 }
541                         }
542                 }
543                 if(n!=1){//only fill for several canvas in one path
544                         if(typeFill==2){
545                                 build_url(root->add_child("layer"),fill,mtx);
546                         }
547                 }
548         }
549 }
550
551 std::vector<String>
552 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
553         std::vector<String> tokens;
554         String buffer;
555         int e=0;
556         unsigned int i=0;
557         char a;
558         while(i<path.size()){
559                 a=path.at(i);
560                 switch(e){
561                         case 0: //initial state
562                                         if(a=='m'){ e=1; i++;}
563                                         else if(a=='c'){ e= 2; i++;}
564                                         else if(a=='q'){ e= 3; i++;}
565                                         else if(a=='t'){ e= 4; i++;}
566                                         else if(a=='a'){ e= 5; i++;}
567                                         else if(a=='l'){ e= 6; i++;}
568                                         else if(a=='v'){ e= 7; i++;}
569                                         else if(a=='h'){ e= 8; i++;}
570                                         else if(a=='M'){ e= 9; i++;}
571                                         else if(a=='C'){ e=10; i++;}
572                                         else if(a=='Q'){ e=11; i++;}
573                                         else if(a=='T'){ e=12; i++;}
574                                         else if(a=='A'){ e=13; i++;}
575                                         else if(a=='L'){ e=14; i++;}
576                                         else if(a=='V'){ e=15; i++;}
577                                         else if(a=='H'){ e=16; i++;}
578                                         else if(a=='z' || a=='Z'){ e=17; i++;}
579                                         else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
580                                         else if(a==','){ e=19; i++;}
581                                         else if(a==' '){i++;}
582                                         break;
583                         //relative
584                         case 1 : tokens.push_back("m"); e=0; break;//move
585                         case 2 : tokens.push_back("c"); e=0; break;//curve
586                         case 3 : tokens.push_back("q"); e=0; break;//quadratic
587                         case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
588                         case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
589                         case 6 : tokens.push_back("l"); e=0; break;//line to
590                         case 7 : tokens.push_back("v"); e=0; break;//vertical
591                         case 8 : tokens.push_back("h"); e=0; break;//horizontal
592                         //absolute
593                         case 9 : tokens.push_back("M"); e=0; break;
594                         case 10: tokens.push_back("C"); e=0; break;
595                         case 11: tokens.push_back("Q"); e=0; break;
596                         case 12: tokens.push_back("T"); e=0; break;
597                         case 13: tokens.push_back("A"); e=0; break;
598                         case 14: tokens.push_back("L"); e=0; break;
599                         case 15: tokens.push_back("V"); e=0; break;
600                         case 16: tokens.push_back("H"); e=0; break;
601
602                         case 17: tokens.push_back("z"); e=0; break;//loop
603                         case 18: if(a=='-'||a=='.'|| isdigit (a)){
604                                                 buffer.append(path.substr(i,1));i++;
605                                         }else{
606                                                 e=20;
607                                         }
608                                         break;
609                         case 19: tokens.push_back(","); e=0; break;
610                         case 20: tokens.push_back(buffer);
611                                         buffer.clear();
612                                         e=0; break;
613                         default: break;
614                 }
615         }
616         switch(e){//last element
617                 case 1 : tokens.push_back("m"); break;
618                 case 2 : tokens.push_back("c"); break;
619                 case 3 : tokens.push_back("q"); break;
620                 case 4 : tokens.push_back("t"); break;
621                 case 5 : tokens.push_back("a"); break;
622                 case 6 : tokens.push_back("l"); break;
623                 case 7 : tokens.push_back("v"); break;
624                 case 8 : tokens.push_back("h"); break;
625                 case 9 : tokens.push_back("M"); break;
626                 case 10: tokens.push_back("C"); break;
627                 case 11: tokens.push_back("Q"); break;
628                 case 12: tokens.push_back("T"); break;
629                 case 13: tokens.push_back("A"); break;
630                 case 14: tokens.push_back("L"); break;
631                 case 15: tokens.push_back("V"); break;
632                 case 16: tokens.push_back("H"); break;
633                 case 17: tokens.push_back("z"); break;
634                 case 18: tokens.push_back(buffer); break;
635                 case 19: tokens.push_back(","); break;
636                 case 20: tokens.push_back(buffer); break;
637                 default: break;
638         }
639         return tokens;
640 }
641
642 std::list<std::list<Vertice*> >
643 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
644         std::list<std::list<Vertice*> > k;
645         std::list<Vertice*> k1;
646         float ax,ay,tgx,tgy,tgx2,tgy2;//each method
647         float actual_x=0,actual_y=0;//for relative methods;
648         loop=false;
649         unsigned int i;
650         std::vector<String> tokens=get_tokens_path(path_d);
651         for(i=0;i<tokens.size();i++){
652                 if(tokens.at(i).compare("M")==0){//absolute move to
653                         if(!k1.empty())
654                                 k.push_front(k1);
655                         k1.clear();
656                         //read
657                         i++; ax=atof(tokens.at(i).data());
658                         i++; if(tokens.at(i).compare(",")==0) i++;
659                         ay=atof(tokens.at(i).data());
660                         actual_x=ax;
661                         actual_y=ay;
662                         //operate and save
663                         if(mtx) transformPoint2D(mtx,&ax,&ay);
664                         coor2vect(&ax,&ay);
665                         k1.push_back(nuevoVertice (ax,ay)); //first element
666                         setSplit(k1.back(),TRUE);
667                 }else if(tokens.at(i).compare("C")==0){ //absolute curve
668                         //tg2
669                         i++; tgx2=atof(tokens.at(i).data());
670                         i++; if(tokens.at(i).compare(",")==0) i++;
671                         tgy2=atof(tokens.at(i).data());
672                         //tg1
673                         i++; tgx=atof(tokens.at(i).data());
674                         i++; if(tokens.at(i).compare(",")==0) i++;
675                         tgy=atof(tokens.at(i).data());
676                         //point
677                         i++; ax=atof(tokens.at(i).data());
678                         i++; if(tokens.at(i).compare(",")==0) i++;
679                         ay=atof(tokens.at(i).data());
680                         actual_x=ax;
681                         actual_y=ay;
682                         //mtx
683                         if(mtx){
684                                 transformPoint2D(mtx,&tgx2,&tgy2);
685                                 transformPoint2D(mtx,&ax,&ay);
686                                 transformPoint2D(mtx,&tgx,&tgy);
687                         }
688                         //ajust
689                         coor2vect(&tgx2,&tgy2);
690                         coor2vect(&ax,&ay);
691                         coor2vect(&tgx,&tgy);
692                         //save
693                         setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
694                         if(isFirst(k1.front(),ax,ay)){
695                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
696                         }else{
697                                 k1.push_back(nuevoVertice (ax,ay));
698                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
699                                 setSplit(k1.back(),TRUE);
700                         }
701                 }else if(tokens.at(i).compare("Q")==0){ //absolute quadractic curve
702                         //tg1 and tg2
703                         i++; tgx=ax=atof(tokens.at(i).data());
704                         i++; if(tokens.at(i).compare(",")==0) i++;
705                         tgy=ay=atof(tokens.at(i).data());
706                         //point
707                         i++; ax=atof(tokens.at(i).data());
708                         i++; if(tokens.at(i).compare(",")==0) i++;
709                         ay=atof(tokens.at(i).data());
710                         actual_x=ax;
711                         actual_y=ay;
712                         //mtx
713                         if(mtx){
714                                 transformPoint2D(mtx,&ax,&ay);
715                                 transformPoint2D(mtx,&tgx,&tgy);
716                         }
717                         //adjust
718                         coor2vect(&ax,&ay);
719                         coor2vect(&tgx,&tgy);
720                         //save
721                         setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
722                         setSplit(k1.back(),FALSE);
723                         k1.push_back(nuevoVertice (ax,ay));
724                         setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
725                 }else if(tokens.at(i).compare("L")==0){ //absolute line to
726                         //point
727                         i++; ax=atof(tokens.at(i).data());
728                         i++; if(tokens.at(i).compare(",")==0) i++;
729                         ay=atof(tokens.at(i).data());
730                         actual_x=ax;
731                         actual_y=ay;
732                         //mtx
733                         if(mtx) transformPoint2D(mtx,&ax,&ay);
734                         //adjust
735                         coor2vect(&ax,&ay);
736                         //save
737                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
738                         if(isFirst(k1.front(),ax,ay)){
739                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
740                         }else{
741                                 k1.push_back(nuevoVertice(ax,ay));
742                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
743                         }
744                 }else if(tokens.at(i).compare("l")==0){//relative line to
745                         //point read
746                         i++; ax=atof(tokens.at(i).data());
747                         i++; if(tokens.at(i).compare(",")==0) i++;
748                         ay=atof(tokens.at(i).data());
749                         //relative
750                         ax=actual_x+ax;
751                         ay=actual_y+ay;
752                         actual_x=ax;
753                         actual_y=ay;
754                         //mtx
755                         if(mtx) transformPoint2D(mtx,&ax,&ay);
756                         //adjust
757                         coor2vect(&ax,&ay);
758                         //save
759                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
760                         if(isFirst(k1.front(),ax,ay)){
761                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
762                         }else{
763                                 k1.push_back(nuevoVertice(ax,ay));
764                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
765                         }
766                 }else if(tokens.at(i).compare("H")==0){//absolute horizontal move
767                         //the same that L but only Horizontal movement
768                         //point
769                         i++; ax=atof(tokens.at(i).data());
770                         ay=actual_y;
771                         actual_x=ax;
772                         actual_y=ay;
773                         //mtx
774                         if(mtx) transformPoint2D(mtx,&ax,&ay);
775                         //adjust
776                         coor2vect(&ax,&ay);
777                         //save
778                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
779                         if(isFirst(k1.front(),ax,ay)){
780                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
781                         }else{
782                                 k1.push_back(nuevoVertice(ax,ay));
783                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
784                         }
785                 }else if(tokens.at(i).compare("h")==0){//horizontal relative
786                         i++; ax=atof(tokens.at(i).data());
787                         ax=actual_x+ax;
788                         ay=actual_y;
789                         actual_x=ax;
790                         actual_y=ay;
791                         //mtx
792                         if(mtx) transformPoint2D(mtx,&ax,&ay);
793                         //adjust
794                         coor2vect(&ax,&ay);
795                         //save
796                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
797                         if(isFirst(k1.front(),ax,ay)){
798                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
799                         }else{
800                                 k1.push_back(nuevoVertice(ax,ay));
801                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
802                         }
803                 }else if(tokens.at(i).compare("V")==0){//vertical absolute
804                         //point
805                         i++; ay=atof(tokens.at(i).data());
806                         ax=actual_x;
807                         actual_x=ax;
808                         actual_y=ay;
809                         //mtx
810                         if(mtx) transformPoint2D(mtx,&ax,&ay);
811                         //adjust
812                         coor2vect(&ax,&ay);
813                         //save
814                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
815                         if(isFirst(k1.front(),ax,ay)){
816                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
817                         }else{
818                                 k1.push_back(nuevoVertice(ax,ay));
819                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
820                         }
821                 }else if(tokens.at(i).compare("v")==0){//relative
822                         //point
823                         i++; ay=atof(tokens.at(i).data());
824                         ax=actual_x;
825                         ay=actual_y+ay;
826                         actual_x=ax;
827                         actual_y=ay;
828                         //mtx
829                         if(mtx) transformPoint2D(mtx,&ax,&ay);
830                         //adjust
831                         coor2vect(&ax,&ay);
832                         //save
833                         setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
834                         if(isFirst(k1.front(),ax,ay)){
835                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
836                         }else{
837                                 k1.push_back(nuevoVertice(ax,ay));
838                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
839                         }
840                 }else if(tokens.at(i).compare("T")==0){// I don't know what does it
841                 }else if(tokens.at(i).compare("A")==0){//elliptic arc
842
843                         //isn't complete support, is only for circles
844
845                         //this curve have 6 parameters
846                         //radio
847                         float radio_x,radio_y;
848                         float angle;
849                         bool sweep,large;
850                         //radio
851                         i++; radio_x=atof(tokens.at(i).data());
852                         i++; if(tokens.at(i).compare(",")==0) i++;
853                         radio_y=atof(tokens.at(i).data());
854                         //angle
855                         i++; angle=atof(tokens.at(i).data());
856                         //flags
857                         i++; large=atoi(tokens.at(i).data());
858                         i++; sweep=atoi(tokens.at(i).data());
859                         //point
860                         i++; ax=atof(tokens.at(i).data());
861                         i++; if(tokens.at(i).compare(",")==0) i++;
862                         ay=atof(tokens.at(i).data());
863                         //how to draw?
864                         if(!large && !sweep){
865                                 //points
866                                 tgx2 = actual_x + radio_x*0.5;
867                                 tgy2 = actual_y ;
868                                 tgx  = ax;
869                                 tgy  = ay + radio_y*0.5;
870                                 actual_x=ax;
871                                 actual_y=ay;
872                                 //transformations
873                                 if(mtx){
874                                         transformPoint2D(mtx,&tgx2,&tgy2);
875                                         transformPoint2D(mtx,&ax,&ay);
876                                         transformPoint2D(mtx,&tgx,&tgy);
877                                 }
878                                 //adjust
879                                 coor2vect(&tgx2,&tgy2);
880                                 coor2vect(&ax,&ay);
881                                 coor2vect(&tgx,&tgy);
882                                 //save
883                                 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
884                                 if(isFirst(k1.front(),ax,ay)){
885                                         setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
886                                 }else{
887                                         k1.push_back(nuevoVertice (ax,ay));
888                                         setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
889                                         setSplit(k1.back(),TRUE);
890                                 }
891                         }else if(!large &&  sweep){
892                                 //points
893                                 tgx2 = actual_x;
894                                 tgy2 = actual_y + radio_y*0.5;
895                                 tgx  = ax + radio_x*0.5;
896                                 tgy  = ay ;
897                                 actual_x=ax;
898                                 actual_y=ay;
899                                 //transformations
900                                 if(mtx){
901                                         transformPoint2D(mtx,&tgx2,&tgy2);
902                                         transformPoint2D(mtx,&ax,&ay);
903                                         transformPoint2D(mtx,&tgx,&tgy);
904                                 }
905                                 //adjust
906                                 coor2vect(&tgx2,&tgy2);
907                                 coor2vect(&ax,&ay);
908                                 coor2vect(&tgx,&tgy);
909                                 //save
910                                 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
911                                 if(isFirst(k1.front(),ax,ay)){
912                                         setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
913                                 }else{
914                                         k1.push_back(nuevoVertice (ax,ay));
915                                         setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
916                                         setSplit(k1.back(),TRUE);
917                                 }
918                         }else if( large && !sweep){//rare
919                                 //this need more than one vertex
920                         }else if( large &&  sweep){//circles in inkscape are made with this kind of arc
921                                 if(actual_y==ay){//circles
922                                         //intermediate point
923                                         int sense=1;
924                                         if(actual_x>ax) sense =-1;
925                                         float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
926                                         in_x = (actual_x+ax)/2;
927                                         in_y = actual_y - sense*radio_y;
928                                         in_tgx1 = in_x - sense*(radio_x*0.5);
929                                         in_tgx2 = in_x + sense*(radio_x*0.5);
930                                         in_tgy1 = in_y;
931                                         in_tgy2 = in_y;
932                                         //start/end points
933                                         tgx2=actual_x;
934                                         tgy2=ay - sense*(radio_y*0.5);
935                                         tgx =ax;
936                                         tgy =ay - sense*(radio_y*0.5);
937
938                                         actual_x=ax;
939                                         actual_y=ay;
940                                         //transformations
941                                         if(mtx){
942                                                 transformPoint2D(mtx,&tgx2,&tgy2);
943                                                 transformPoint2D(mtx,&tgx ,&tgy );
944                                                 transformPoint2D(mtx,&ax,&ay);
945
946                                                 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
947                                                 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
948                                                 transformPoint2D(mtx,&in_x,&in_y);
949                                         }
950                                         //adjust
951                                         coor2vect(&tgx2 , &tgy2);
952                                         coor2vect(&ax   , &ay  );
953                                         coor2vect(&tgx  , &tgy );
954
955                                         coor2vect(&in_tgx2 , &in_tgy2);
956                                         coor2vect(&in_tgx1 , &in_tgy1);
957                                         coor2vect(&in_x    , &in_y   );
958
959                                         //save the last tg2
960                                         setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
961                                         //save the intermediate point
962                                         k1.push_back(nuevoVertice (in_x,in_y));
963                                         setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
964                                         setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
965                                         setSplit(k1.back(),TRUE); //this could be changed
966                                         //save the new point
967                                         if(isFirst(k1.front(),ax,ay)){
968                                                 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
969                                         }else{
970                                                 k1.push_back(nuevoVertice (ax,ay));
971                                                 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
972                                                 setSplit(k1.back(),TRUE);
973                                         }
974                                 }
975                         }
976                 }else if(tokens.at(i).compare("z")==0){
977                         loop=true;
978                 }else{
979                         std::cout<<"don't supported: "<<tokens.at(i)<<std::endl;
980                 }
981         }
982         if(!k1.empty())
983                 k.push_front(k1); //last element
984         return k;
985 }
986
987 void
988 Svg_parser::parser_defs(const xmlpp::Node* node){
989         const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
990         if(!nodeContent){
991                 xmlpp::Node::NodeList list = node->get_children();
992                 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
993                         Glib::ustring name =(*iter)->get_name();
994                         if(name.compare("linearGradient")==0){
995                                 parser_linearGradient(*iter);
996                         }else if(name.compare("radialGradient")==0){
997                                 parser_radialGradient(*iter);
998                         }
999                 }
1000         }
1001 }
1002 void
1003 Svg_parser::AdjustPointUrl(){
1004 /*
1005         if(!lg.empty()){//linealgradient
1006                 std::list<LinearGradient*>::iterator aux=lg.begin();
1007                 while(aux!=lg.end()){
1008                         LinearGradient* auxlg=*aux;
1009                         coor2vect (&auxlg->x1,&auxlg->y1);
1010                         coor2vect (&auxlg->x2,&auxlg->y2);
1011                         aux++;
1012                 }
1013         }
1014         if(!rg.empty()){//radialgradient
1015                 std::list<RadialGradient*>::iterator aux=rg.begin();
1016                 while(aux!=rg.end()){
1017                         RadialGradient* auxrg=*aux;
1018                         coor2vect (&auxrg->cx,&auxrg->cy);
1019                         auxrg->r= auxrg->r/kux;
1020                         aux++;
1021                 }
1022         }
1023 */
1024 }
1025 std::list<ColorStop*>*
1026 Svg_parser::find_colorStop(String name){
1027         if(!name.empty()){
1028                 if(lg.empty()&& rg.empty())
1029                         return NULL;
1030
1031                 String buscar= name;
1032                 if(buscar.at(0)=='#') buscar.erase(0,1);
1033                 else return NULL;
1034                 std::list<LinearGradient*>::iterator aux=lg.begin();
1035                 while(aux!=lg.end()){//only find into linear gradients
1036                         if(buscar.compare((*aux)->name)==0){
1037                                 return (*aux)->stops;
1038                         }
1039                         aux++;
1040                 }
1041         }
1042         return NULL;
1043 }
1044 void
1045 Svg_parser::build_url(xmlpp::Element* root, String name,Matrix *mtx){
1046         if(!name.empty()){
1047                 if(lg.empty()&& rg.empty())
1048                         root->get_parent()->remove_child(root);
1049
1050                 int inicio=name.find_first_of("#")+1;
1051                 int fin=name.find_first_of(")");
1052                 String buscar= name.substr(inicio,fin-inicio);
1053                 bool encontro=false;
1054                 if(!lg.empty()){
1055                         std::list<LinearGradient*>::iterator aux=lg.begin();
1056                         while(aux!=lg.end()){
1057                                 if(buscar.compare((*aux)->name)==0){
1058                                         build_linearGradient (root,*aux,mtx);
1059                                         encontro=true;
1060                                 }
1061                                 aux++;
1062                         }
1063                 }
1064                 if(!encontro && !rg.empty()){
1065                         std::list<RadialGradient*>::iterator aux=rg.begin();
1066                         while(aux!=rg.end()){
1067                                 if(buscar.compare((*aux)->name)==0){
1068                                         build_radialGradient (root,*aux,mtx);
1069                                         encontro=true;
1070                                 }
1071                                 aux++;
1072                         }
1073                 }
1074                 if(!encontro)
1075                         root->get_parent()->remove_child(root);
1076         }else{
1077                 root->get_parent()->remove_child(root);
1078         }
1079 }
1080 void
1081 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
1082         std::list<ColorStop*>::iterator aux_stop=stops->begin();
1083         while(aux_stop!=stops->end()){
1084                 xmlpp::Element *child=root->add_child("color");
1085                 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
1086                 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
1087                 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
1088                 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
1089                 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
1090                 aux_stop++;
1091         }
1092 }
1093 void
1094 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
1095         if(data){
1096                 root->set_attribute("type","linear_gradient");
1097                 root->set_attribute("active","true");
1098                 root->set_attribute("desc","Gradient004");
1099                 build_param (root->add_child("param"),"z_depth","real","0");
1100                 build_param (root->add_child("param"),"amount","real","1");
1101                 //straight onto
1102                 build_param (root->add_child("param"),"blend_method","integer","21");
1103                 float x1,y1,x2,y2;
1104                 x1=data->x1;
1105                 y1=data->y1;
1106                 x2=data->x2;
1107                 y2=data->y2;
1108                 if(mtx){
1109                         transformPoint2D(mtx,&x1,&y1);
1110                         transformPoint2D(mtx,&x2,&y2);
1111                 }
1112                 coor2vect (&x1,&y1);
1113                 coor2vect (&x2,&y2);
1114
1115                 build_vector (root->add_child("param"),"p1",x1,y1);
1116                 build_vector (root->add_child("param"),"p2",x2,y2);
1117                 //gradient link
1118                 xmlpp::Element *child=root->add_child("param");
1119                 child->set_attribute("name","gradient");
1120                 build_stop_color (child->add_child("gradient"),data->stops);
1121                 build_param (root->add_child("param"),"loop","bool","false");
1122                 build_param (root->add_child("param"),"zigzag","bool","false");
1123         }
1124 }
1125 void
1126 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1127 //not completed
1128         if(data){
1129                 root->set_attribute("type","radial_gradient");
1130                 root->set_attribute("active","true");
1131                 build_param (root->add_child("param"),"z_depth","real","0");
1132                 build_param (root->add_child("param"),"amount","real","1");
1133                 //straight onto
1134                 build_param (root->add_child("param"),"blend_method","integer","21");
1135                 //gradient link
1136                 xmlpp::Element *child=root->add_child("param");
1137                 child->set_attribute("name","gradient");
1138                 build_stop_color (child->add_child("gradient"),data->stops);
1139                 //here the center point and radio
1140                 float cx=data->cx;
1141                 float cy=data->cy;
1142                 float r =data->r;
1143                 //transform
1144                 if(mtx){
1145                         transformPoint2D(mtx,&cx,&cy);
1146                 }
1147                 //adjust
1148                 coor2vect (&cx,&cy);
1149                 r=r/kux;
1150                 build_vector (root->add_child("param"),"center",cx,cy);
1151                 build_param (root->add_child("param"),"radius","real",r);
1152
1153                 build_param (root->add_child("param"),"loop","bool","false");
1154                 build_param (root->add_child("param"),"zigzag","bool","false");
1155         }
1156 }
1157
1158 void
1159 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1160         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1161                 Glib::ustring id        =nodeElement->get_attribute_value("id");
1162                 float x1                        =atof(nodeElement->get_attribute_value("x1").data());
1163                 float y1                        =atof(nodeElement->get_attribute_value("y1").data());
1164                 float x2                        =atof(nodeElement->get_attribute_value("x2").data());
1165                 float y2                        =atof(nodeElement->get_attribute_value("y2").data());
1166                 Glib::ustring link      =nodeElement->get_attribute_value("href");
1167
1168                 std::list<ColorStop*> *stops;
1169                 if(!link.empty()){
1170                         stops=find_colorStop (link);
1171                 }else{
1172                         //color stops
1173                         stops=new std::list<ColorStop*>();
1174                         const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1175                         if(!nodeContent){
1176                         xmlpp::Node::NodeList list = node->get_children();
1177                         for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1178                                         Glib::ustring name =(*iter)->get_name();
1179                                         if(name.compare("stop")==0){
1180                                                 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1181                                                 Glib::ustring style     =nodeIter->get_attribute_value("style");
1182                                                 float offset=atof(nodeIter->get_attribute_value("offset").data());
1183                                                 String stop_color;
1184                                                 String opacity;
1185                                                 if(!style.empty()){
1186                                                         extractSubAttribute (style,"stop-color",&stop_color);
1187                                                         extractSubAttribute (style,"stop-opacity",&opacity);
1188                                                 }
1189                                                 if(opacity.empty()) opacity="1";
1190                                                 if(stop_color.empty()) stop_color="#000000";//black for default :S
1191                                                 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1192                                         }
1193                         }
1194                         }
1195                 }
1196                 if(stops)
1197                         lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops));
1198         }
1199 }
1200
1201 void
1202 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1203         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1204                 Glib::ustring id        =nodeElement->get_attribute_value("id");
1205                 float cx                        =atof(nodeElement->get_attribute_value("cx").data());
1206                 float cy                        =atof(nodeElement->get_attribute_value("cy").data());
1207                 float r                         =atof(nodeElement->get_attribute_value("r").data());
1208                 Glib::ustring link      =nodeElement->get_attribute_value("href");//basic
1209                 std::list<ColorStop*> *stops=NULL;
1210                 if(!link.empty()){
1211                         //inkscape always use link, i dont need parser stops here, but it's posible
1212                         stops=find_colorStop (link);
1213                 }
1214                 if(stops)
1215                         rg.push_back(newRadialGradient(id,cx,cy,r,stops));
1216         }
1217 }
1218
1219 ColorStop*
1220 Svg_parser::newColorStop(String color,float opacity,float pos){
1221         ColorStop* _stop;
1222         _stop=(ColorStop*)malloc(sizeof(ColorStop));
1223         float r=getRed(color);
1224         float g=getGreen(color);
1225         float b=getBlue(color);
1226         float a=opacity;
1227         Color ret=adjustGamma(r/255,g/255,b/255,a);
1228         _stop->r=ret.get_r();
1229         _stop->g=ret.get_g();
1230         _stop->b=ret.get_b();
1231         _stop->a=ret.get_a();
1232         _stop->pos=pos;
1233         return _stop;
1234 }
1235 Color
1236 Svg_parser::adjustGamma(float r,float g,float b,float a){
1237         Color ret(r,g,b,a);
1238         if(gamma.get_gamma_r()!=1.0){
1239                 if(ret.get_r() < 0)
1240                         ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1241                 else
1242                         ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1243         }
1244         if(gamma.get_gamma_g()!=1.0){
1245                 if(ret.get_g() < 0)
1246                         ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1247                 else
1248                         ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1249         }
1250         if(gamma.get_gamma_b()!=1.0){
1251                 if(ret.get_b() < 0)
1252                         ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1253                 else
1254                         ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1255         }
1256         return ret;
1257 }
1258
1259 LinearGradient*
1260 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops){
1261         LinearGradient* data;
1262         data=(LinearGradient*)malloc(sizeof(LinearGradient));
1263         sprintf(data->name,"%s",name.data());
1264         data->x1=x1;
1265         data->y1=y1;
1266         data->x2=x2;
1267         data->y2=y2;
1268         data->stops=stops;
1269         return data;
1270 }
1271
1272 RadialGradient*
1273 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops){
1274         RadialGradient* data;
1275         data=(RadialGradient*)malloc(sizeof(RadialGradient));
1276         sprintf(data->name,"%s",name.data());
1277         data->cx=cx;
1278         data->cy=cy;
1279         data->r=r;
1280         data->stops=stops;
1281         return data;
1282 }
1283
1284 //builds
1285 void
1286 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1287         root->set_attribute("type","colorcorrect");
1288         root->set_attribute("active","true");
1289         root->set_attribute("version","0.1");
1290         root->set_attribute("desc","Gamma");
1291         build_real (root->add_child("param"),"gamma",gamma);
1292 }
1293 Matrix*
1294 Svg_parser::build_transform(const String transform){
1295         Matrix* a=NULL;
1296         String tf(transform);
1297         removeIntoS(&tf);
1298         std::vector<String> tokens=tokenize(tf," ");
1299         std::vector<String>::iterator aux=tokens.begin();
1300         while(aux!=tokens.end()){
1301                 if((*aux).compare(0,9,"translate")==0){
1302                         float dx,dy;
1303                         int inicio,fin;
1304                         inicio  =(*aux).find_first_of("(")+1;
1305                         fin             =(*aux).find_first_of(",");
1306                         dx              =atof((*aux).substr(inicio,fin-inicio).data());
1307                         inicio  =(*aux).find_first_of(",")+1;
1308                         fin             =(*aux).size()-1;
1309                         dy              =atof((*aux).substr(inicio,fin-inicio).data());
1310                         if(matrixVacia(a))
1311                                 a=newMatrix(1,0,0,1,dx,dy);
1312                         else
1313                                 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1314                 }else if((*aux).compare(0,5,"scale")==0){
1315                         if(matrixVacia(a))
1316                                 a=newMatrix(1,0,0,1,0,0);
1317                 }else if((*aux).compare(0,6,"rotate")==0){
1318                         float angle,seno,coseno;
1319                         int inicio,fin;
1320                         inicio  =(*aux).find_first_of("(")+1;
1321                         fin             =(*aux).size()-1;
1322                         angle=getRadian (atof((*aux).substr(inicio,fin-inicio).data()));
1323                         seno   =sin(angle);
1324                         coseno =cos(angle);
1325                         if(matrixVacia(a))
1326                                 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1327                         else
1328                                 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1329                 }else if((*aux).compare(0,6,"matrix")==0){
1330                         int inicio      =(*aux).find_first_of('(')+1;
1331                         int fin         =(*aux).find_first_of(')');
1332                         if(matrixVacia(a))
1333                                 a=newMatrix((*aux).substr(inicio,fin-inicio));
1334                         else
1335                                 multiplyMatrix(&a,newMatrix((*aux).substr(inicio,fin-inicio)));
1336                 }else{
1337                         a=newMatrix(1,0,0,1,0,0);
1338                 }
1339                 aux++;
1340         }
1341         return a;
1342 }
1343
1344 void
1345 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1346         root->set_attribute("type","translate");
1347         root->set_attribute("active","true");
1348         root->set_attribute("version","0.1");
1349         build_vector (root->add_child("param"),"origin",dx,dy);
1350 }
1351 void
1352 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1353         root->set_attribute("type","rotate");
1354         root->set_attribute("active","true");
1355         root->set_attribute("version","0.1");
1356         build_vector (root->add_child("param"),"origin",dx,dy);
1357         build_real   (root->add_child("param"),"amount",angle);
1358 }
1359 void
1360 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1361         root->set_attribute("name","vector_list");
1362         xmlpp::Element *child=root->add_child("dynamic_list");
1363         child->set_attribute("type","vector");
1364         std::list<Vertice*>::iterator aux = p.begin();
1365         while(aux!=p.end()){
1366                 xmlpp::Element *child_entry=child->add_child("entry");
1367                 xmlpp::Element *child_vector=child_entry->add_child("vector");
1368                 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1369                 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1370                 aux++;
1371         }
1372 }
1373 void
1374 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1375         xmlpp::Element *child_comp=root->add_child("composite");
1376         child_comp->set_attribute("type","bline_point");
1377         build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1378         build_param (child_comp->add_child("width"),"","real","1.0000000000");
1379         build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1380         if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1381         else build_param (child_comp->add_child("split"),"","bool","false");
1382         //tangente 1
1383         xmlpp::Element *child_t1=child_comp->add_child("t1");
1384         xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1385         child_rc->set_attribute("type","vector");
1386         build_param (child_rc->add_child("radius"),"","real",p->radio1);
1387         build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1388         //tangente 2
1389         xmlpp::Element *child_t2=child_comp->add_child("t2");
1390         xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1391         child_rc2->set_attribute("type","vector");
1392         build_param (child_rc2->add_child("radius"),"","real",p->radio2);
1393         build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1394
1395 }
1396 void
1397 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1398         root->set_attribute("name","bline");
1399         xmlpp::Element *child=root->add_child("bline");
1400         child->set_attribute("type","bline_point");
1401         if(loop)
1402                 child->set_attribute("loop","true");
1403         else
1404                 child->set_attribute("loop","false");
1405         if(!blineguid.empty())  child->set_attribute("guid",blineguid);
1406         std::list<Vertice*>::iterator aux = p.begin();
1407         while(aux!=p.end()){
1408                 if(*aux) build_vertice (child->add_child("entry"),*aux);
1409                 aux++;
1410         }
1411 }
1412
1413 void
1414 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1415         if(!type.empty() && !value.empty()){
1416                 if(!name.empty())       root->set_attribute("name",name);
1417                 xmlpp::Element *child=root->add_child(type);
1418                 child->set_attribute("value",value);
1419         }else{
1420                 root->get_parent()->remove_child(root);
1421         }
1422 }
1423 void
1424 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1425         if(!type.empty()){
1426                 if(!name.empty()) root->set_attribute("name",name);
1427                 xmlpp::Element *child=root->add_child(type);
1428                 child->set_attribute("value",etl::strprintf ("%f",value));
1429         }else{
1430                 root->get_parent()->remove_child(root);
1431         }
1432 }
1433 void
1434 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1435         if(!type.empty()){
1436                         if(!name.empty()) root->set_attribute("name",name);
1437                         xmlpp::Element *child=root->add_child(type);
1438                         char *enteroc=new char[10];
1439                         sprintf(enteroc,"%d",value);
1440                         child->set_attribute("value",enteroc);
1441                         delete [] enteroc;
1442         }else{
1443                 root->get_parent()->remove_child(root);
1444         }
1445 }
1446
1447 void
1448 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1449         if(name.compare("")!=0) root->set_attribute("name",name);
1450         xmlpp::Element *child=root->add_child("integer");
1451         char *enteroc=new char[10];
1452         sprintf(enteroc,"%d",value);
1453         child->set_attribute("value",enteroc);
1454 }
1455 void
1456 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1457         if(name.compare("")!=0) root->set_attribute("name",name);
1458         xmlpp::Element *child=root->add_child("real");
1459         char *realc=new char[20];
1460         sprintf(realc,"%f",value);
1461         child->set_attribute("value",realc);
1462 }
1463
1464 void
1465 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1466         if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1467                 root->get_parent()->remove_child(root);
1468                 printf("Color aborted\n");
1469                 return;
1470         }
1471         Color ret=adjustGamma(r/255,g/255,b/255,a);
1472
1473         root->set_attribute("name","color");
1474         xmlpp::Element *child=root->add_child("color");
1475         child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1476         child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1477         child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1478         child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1479 }
1480 void
1481 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1482
1483         if(name.compare("")!=0) root->set_attribute("name",name);
1484         xmlpp::Element *child=root->add_child("vector");
1485         child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1486         child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1487
1488 }
1489 void
1490 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1491         if(name.compare("")!=0) root->set_attribute("name",name);
1492         xmlpp::Element *child=root->add_child("vector");
1493         if(!guid.empty()) child->set_attribute("guid",guid);
1494         child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1495         child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1496 }
1497
1498 xmlpp::Element*
1499 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1500         root->set_attribute("type","PasteCanvas");
1501         root->set_attribute("active","true");
1502         root->set_attribute("version","0.1");
1503         root->set_attribute("desc","Composite");
1504         build_param (root->add_child("param"),"z_depth","real","0");
1505         build_param (root->add_child("param"),"amount","real","1");
1506         build_param (root->add_child("param"),"blend_method","integer","0");
1507         build_vector (root->add_child("param"),"origin",0,0);
1508         xmlpp::Element *child=root->add_child("param");
1509         child->set_attribute("name","canvas");
1510         return child->add_child("canvas");
1511 }
1512
1513 //metodos extras
1514 void
1515 Svg_parser::coor2vect(float *x,float *y){
1516         float sx, sy;
1517         sx=*x;
1518         sy=*y;
1519         sy= atof(height.c_str())-sy;
1520         sx= sx - ox;
1521         sy= sy - oy;
1522         sx= sx / kux;
1523         sy= sy / kux;
1524         *x=sx; *y=sy;
1525 }
1526
1527 void
1528 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1529         float rd=0,ag=0;
1530         float d1x,d1y,d2x,d2y,dx,dy;
1531         d1x=p1x*60;
1532         d1y=p1y*60;
1533         d2x=p2x*60;
1534         d2y=p2y*60;
1535         dx=d2x-d1x;
1536         dy=d2y-d1y;
1537         dx=dx*3;
1538         dy=dy*3;
1539         dx=dx/60;
1540         dy=dy/60;
1541         rd=sqrt(dx*dx + dy*dy);
1542         if(dx>0 && dy>0){
1543                 ag=PI + atan(dy/dx);
1544         }else if(dx>0 && dy<0){
1545                 ag=PI + atan(dy/dx);
1546         }else if(dx<0 && dy<0){
1547                 ag=atan(dy/dx);
1548         }else if(dx<0 && dy>0){
1549                 ag= 2*PI+atan(dy/dx);
1550         }else if(dx==0 && dy>0){
1551                 ag=-1*PI/2;
1552         }else if(dx==0 && dy<0){
1553                 ag=PI/2;
1554         }else if(dx==0 && dy==0){
1555                 ag=0;
1556         }else if(dx<0 && dy==0){
1557                 ag=0;
1558         }else if(dx>0 && dy==0){
1559                 ag=PI;
1560         }
1561         ag= (ag*180)/PI;
1562         p->radio1=rd;
1563         p->angle1=ag;
1564 }
1565 void
1566 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1567         float rd=0,ag=0;
1568         float d1x,d1y,d2x,d2y,dx,dy;
1569         d1x=p1x*60;
1570         d1y=p1y*60;
1571         d2x=p2x*60;
1572         d2y=p2y*60;
1573         dx=d2x-d1x;
1574         dy=d2y-d1y;
1575         dx=dx*3;
1576         dy=dy*3;
1577         dx=dx/60;
1578         dy=dy/60;
1579
1580         rd=sqrt(dx*dx + dy*dy);
1581         if(dx>0 && dy>0){
1582                 ag=PI + atan(dy/dx);
1583         //      printf("caso 180-270\n");
1584         }else if(dx>0 && dy<0){
1585                 ag=PI + atan(dy/dx);
1586         //      printf("caso 90-180\n");
1587         }else if(dx<0 && dy<0){
1588                 ag=atan(dy/dx);
1589         //      printf("caso 0-90\n");
1590         }else if(dx<0 && dy>0){
1591                 ag= 2*PI+atan(dy/dx);
1592         //      printf("caso 270-360\n");
1593         }else if(dx==0 && dy>0){
1594                 ag=-1*PI/2;
1595         }else if(dx==0 && dy<0){
1596                 ag=PI/2;
1597         }else if(dx==0 && dy==0){
1598                 ag=0;
1599         }else if(dx<0 && dy==0){
1600                 ag=0;
1601         }else if(dx>0 && dy==0){
1602                 ag=PI;
1603         }
1604         ag= (ag*180)/PI;
1605         ag=ag-180;
1606         p->radio2=rd;
1607         p->angle2=ag;
1608 }
1609
1610 void
1611 Svg_parser::setSplit(Vertice* p,bool val){
1612         if(p!=NULL){
1613                 p->split=val;
1614         }
1615 }
1616 int
1617 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1618         if(nodo->x==a && nodo->y==b)
1619                 return 1;
1620         return 0;
1621 }
1622
1623 Vertice*
1624 Svg_parser::nuevoVertice(float x,float y){
1625         Vertice* nuevo;
1626         nuevo=(Vertice*)malloc(sizeof(Vertice));
1627         nuevo->x=x;
1628         nuevo->y=y;
1629         nuevo->radio1=nuevo->radio2=nuevo->angle1=nuevo->angle2=0;
1630         return nuevo;
1631 }
1632
1633 int
1634 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1635         int encontro=0;
1636         if(!attribute.empty()){
1637                 String str(attribute);
1638                 removeS(&str);
1639                 std::vector<String> tokens=tokenize(str,";");
1640                 std::vector<String>::iterator aux=tokens.begin();
1641                 while(aux!=tokens.end()){
1642                         int medio= (*aux).find_first_of(":");
1643                         if((*aux).substr(0,medio).compare(name)==0){
1644                                 int fin=(*aux).size();
1645                                 *value=(*aux).substr(medio+1,fin-medio);
1646                                 return 1;
1647                         }
1648                         aux++;
1649                 }
1650         }
1651         return encontro;
1652 }
1653 String
1654 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1655         String value;
1656         int fnd=0;
1657         if(!path_style.empty())
1658                 fnd=extractSubAttribute(path_style,name,&value);
1659         if(fnd==0){
1660                 if(!master_style.empty())
1661                         fnd=extractSubAttribute(master_style,name,&value);
1662                 if(fnd==0)
1663                         value=defaultVal;
1664         }
1665         return value;
1666 }
1667 String
1668 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1669         String value;
1670         int fnd=0;
1671         if(!path_style.empty())
1672                 fnd=extractSubAttribute(path_style,name,&value);
1673         if(fnd==0 && !master_style.empty())
1674                         fnd=extractSubAttribute(master_style,name,&value);
1675         if(fnd==0){
1676                 if(!subattribute.empty())
1677                         value=subattribute;
1678                 else
1679                         value=defaultVal;
1680         }
1681         return value;
1682 }
1683
1684 int
1685 Svg_parser::randomLetter(){
1686         int a=rand()%2;
1687         if(a) return (49 + rand()%9);
1688         else return  (65 + rand()%24);
1689 }
1690
1691 int
1692 Svg_parser::getRed(String hex){
1693         if(hex.at(0)=='#'){
1694                 return hextodec(hex.substr(1,2));
1695         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1696                 int inicio=hex.find_first_of("(")+1;
1697                 int fin =hex.find_last_of(")");
1698                 String aux=tokenize(hex.substr(inicio,fin-inicio),",").at(0);
1699                 return atoi(aux.data());
1700         }
1701         return 0;
1702 }
1703 int
1704 Svg_parser::getGreen(String hex){
1705         if(hex.at(0)=='#'){
1706                 return hextodec(hex.substr(3,2));
1707         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1708                 int inicio=hex.find_first_of("(")+1;
1709                 int fin =hex.find_last_of(")");
1710                 String aux=tokenize(hex.substr(inicio,fin-inicio),",").at(1);
1711                 return atoi(aux.data());
1712         }
1713         return 0;
1714 }
1715 int
1716 Svg_parser::getBlue(String hex){
1717         if(hex.at(0)=='#'){
1718                 return hextodec(hex.substr(5,2));
1719         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1720                 int inicio=hex.find_first_of("(")+1;
1721                 int fin =hex.find_last_of(")");
1722                 String aux=tokenize(hex.substr(inicio,fin-inicio),",").at(2);
1723                 return atoi(aux.data());
1724         }
1725         return 0;
1726 }
1727 int
1728 Svg_parser::hextodec(String hex){
1729         int result=0;
1730         if(!hex.empty()){
1731                 int top=hex.size();
1732                 int ihex[top];
1733                 int i=0;
1734                 while(i<top){
1735                         if(hex.at(i)=='0')
1736                                 ihex[i]=0;
1737                         else if(hex.at(i)=='1')
1738                                 ihex[i]=1;
1739                         else if(hex.at(i)=='2')
1740                                 ihex[i]=2;
1741                         else if(hex.at(i)=='3')
1742                                 ihex[i]=3;
1743                         else if(hex.at(i)=='4')
1744                                 ihex[i]=4;
1745                         else if(hex.at(i)=='5')
1746                                 ihex[i]=5;
1747                         else if(hex.at(i)=='6')
1748                                 ihex[i]=6;
1749                         else if(hex.at(i)=='7')
1750                                 ihex[i]=7;
1751                         else if(hex.at(i)=='8')
1752                                 ihex[i]=8;
1753                         else if(hex.at(i)=='9')
1754                                 ihex[i]=9;
1755                         else if(hex.at(i)=='a')
1756                                 ihex[i]=10;
1757                         else if(hex.at(i)=='b')
1758                                 ihex[i]=11;
1759                         else if(hex.at(i)=='c')
1760                                 ihex[i]=12;
1761                         else if(hex.at(i)=='d')
1762                                 ihex[i]=13;
1763                         else if(hex.at(i)=='e')
1764                                 ihex[i]=14;
1765                         else if(hex.at(i)=='f')
1766                                 ihex[i]=15;
1767                         else
1768                                 return 0;
1769                         i++;
1770                 }
1771                 i=0;
1772                 while(i<top){
1773                         result+=pow(16,i)*ihex[top-i-1];
1774                         i++;
1775                 }
1776         }
1777         return result;
1778 }
1779
1780 float
1781 Svg_parser::getDimension(const String ac){
1782         if(ac.empty()){
1783                 return 0;
1784         }
1785         int length=ac.size();
1786         float af=0;
1787         if(isdigit(ac.at(length-1))){
1788                 af=atof(ac.data());
1789         }else if(ac.at(length-1)=='%'){
1790                         return 1024;
1791         }else{
1792                 String mtc=ac.substr(length-2,length);
1793                 String nmc=ac.substr(0,length-2);
1794                 if(mtc.compare("px")==0){
1795                         af=atof(nmc.data());
1796                 }else if(mtc.compare("pt")==0){
1797                         af=atof(nmc.data())*1.25;
1798                 }else if(mtc.compare("em")==0){
1799                         af=atof(nmc.data())*16;
1800                 }else if(mtc.compare("mm")==0){
1801                         af=atof(nmc.data())*3.54;
1802                 }else if(mtc.compare("pc")==0){
1803                         af=atof(nmc.data())*15;
1804                 }else if(mtc.compare("cm")==0){
1805                         af=atof(nmc.data())*35.43;
1806                 }else if(mtc.compare("in")==0){
1807                         af=atof(nmc.data())*90;
1808                 }else{
1809                         return 1024;
1810                 }
1811         }
1812         return af;
1813 }
1814 //matrix operations
1815 Matrix*
1816 Svg_parser::newMatrix(Matrix *a){
1817         Matrix* data;
1818         data=(Matrix*)malloc(sizeof(Matrix));
1819         data->a=a->a;           data->b=a->b;           data->c=a->c;
1820         data->d=a->d;           data->e=a->e;           data->f=a->f;
1821         return data;
1822 }
1823 Matrix*
1824 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1825         Matrix* data;
1826         data=(Matrix*)malloc(sizeof(Matrix));
1827         data->a=a;              data->b=b;              data->c=c;
1828         data->d=d;              data->e=e;              data->f=f;
1829         return data;
1830 }
1831 Matrix*
1832 Svg_parser::newMatrix(const String mvector){
1833         if(!mvector.empty()){
1834                 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1835                 std::vector<String> tokens=tokenize(mvector,",");
1836                 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1837                 data->a=atof(tokens.at(0).data());
1838                 data->b=atof(tokens.at(1).data());
1839                 data->c=atof(tokens.at(2).data());
1840                 data->d=atof(tokens.at(3).data());
1841                 data->e=atof(tokens.at(4).data());
1842                 data->f=atof(tokens.at(5).data());
1843                 return data;
1844         }else{
1845                 return newMatrix(1,0,0,1,0,0);
1846         }
1847 }
1848 void
1849 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1850         float auxa,auxb;
1851         auxa=0;
1852         auxb=0;
1853         auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1854         auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1855         *a=auxa;
1856         *b=auxb;
1857         return;
1858 }
1859 void
1860 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1861         Matrix* aux=newMatrix(0,0,0,0,0,0);
1862         aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1863         aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1864         aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1865         aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1866         aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1867         aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1868         *mtx=aux;
1869 }
1870 void
1871 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1872         Matrix* aux=newMatrix(0,0,0,0,0,0);
1873         aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1874         aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1875         aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1876         aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1877         aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1878         aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1879         (*mtx1)->a=aux->a;
1880         (*mtx1)->b=aux->b;
1881         (*mtx1)->c=aux->c;
1882         (*mtx1)->d=aux->d;
1883         (*mtx1)->e=aux->e;
1884         (*mtx1)->f=aux->f;
1885 }
1886 bool
1887 Svg_parser::matrixVacia(Matrix *mtx){
1888         if(mtx == NULL) return true;
1889         return false;
1890 }
1891
1892 float
1893 Svg_parser::getRadian(float sexa){
1894         return (sexa*2*PI)/360;
1895 }
1896 void
1897 Svg_parser::removeS(String *input){
1898         for(unsigned int i=0;i<input->size();i++){
1899                 if(input->at(i)==' '){
1900                         input->erase(i,1);
1901                 }
1902         }
1903 }
1904 void
1905 Svg_parser::removeIntoS(String *input){
1906         bool into=false;
1907         for(unsigned int i=0;i<input->size();i++){
1908                 if(input->at(i)=='('){
1909                         into=true;
1910                 }else if(input->at(i)==')'){
1911                         into=false;
1912                 }else if(into && input->at(i)==' '){
1913                         input->erase(i,1);
1914                 }
1915         }
1916 }
1917 std::vector<String>
1918 Svg_parser::tokenize(const String& str,const String& delimiters){
1919         std::vector<String> tokens;
1920         String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1921         String::size_type pos = str.find_first_of(delimiters, lastPos);
1922         while (String::npos != pos || String::npos != lastPos){
1923                 tokens.push_back(str.substr(lastPos, pos - lastPos));
1924                 lastPos = str.find_first_not_of(delimiters, pos);
1925                 pos = str.find_first_of(delimiters, lastPos);
1926         }
1927         return tokens;
1928 }
1929 String
1930 Svg_parser::new_guid(){
1931         uid++;
1932         return GUID::hasher(uid).get_string();
1933 }
1934