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