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