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