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