Bugfixing
[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"));
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"));
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"));
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","Gradient004");
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                 build_stop_color (child_stops->add_child("gradient"),data->stops);
1047                 build_param (gradient->add_child("param"),"loop","bool","false");
1048                 build_param (gradient->add_child("param"),"zigzag","bool","false");
1049         }
1050 }
1051 void
1052 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1053         if(data){
1054                 xmlpp::Element* gradient;
1055
1056                 if (mtx || data->transform) {
1057                         xmlpp::Element* layer=root->add_child("layer");
1058
1059                         layer->set_attribute("type","PasteCanvas");
1060                         layer->set_attribute("active","true");
1061                         layer->set_attribute("version","0.1");
1062                         layer->set_attribute("desc","Composite");
1063                         build_param (layer->add_child("param"),"z_depth","real","0");
1064                         build_param (layer->add_child("param"),"amount","real","1");
1065                         build_param (layer->add_child("param"),"blend_method","integer","21"); //straight onto
1066                         build_vector (layer->add_child("param"),"origin",0,0);
1067                         xmlpp::Element *child=layer->add_child("param");
1068                         child->set_attribute("name","canvas");
1069                         xmlpp::Element* child_layer=child->add_child("canvas");
1070
1071                         gradient=child_layer->add_child("layer");
1072                         build_param (gradient->add_child("param"),"blend_method","integer","0"); //composite
1073                         Matrix *mtx2=NULL;
1074                         if (mtx && data->transform){
1075                                 composeMatrix(&mtx2,mtx,data->transform);
1076                         }else if (mtx){
1077                                 mtx2=mtx;
1078                         }else if (data->transform){
1079                                 mtx2=data->transform;
1080                         }
1081                         build_transform(child_layer,mtx2);
1082                         
1083                 }else {
1084                         gradient=root->add_child("layer");
1085                         build_param (gradient->add_child("param"),"blend_method","integer","21"); //straight onto
1086                 }               
1087
1088                 gradient->set_attribute("type","radial_gradient");
1089                 gradient->set_attribute("active","true");
1090                 build_param (gradient->add_child("param"),"z_depth","real","0");
1091                 build_param (gradient->add_child("param"),"amount","real","1");
1092                 //gradient link
1093                 xmlpp::Element *child_stops=gradient->add_child("param");
1094                 child_stops->set_attribute("name","gradient");
1095                 build_stop_color (child_stops->add_child("gradient"),data->stops);
1096
1097                 //here the center point and radius
1098                 float cx=data->cx;
1099                 float cy=data->cy;
1100                 float r =data->r;
1101
1102                 //adjust
1103                 coor2vect (&cx,&cy);
1104                 r=r/kux;
1105                 build_vector (gradient->add_child("param"),"center",cx,cy);
1106                 build_param (gradient->add_child("param"),"radius","real",r);
1107
1108                 build_param (gradient->add_child("param"),"loop","bool","false");
1109                 build_param (gradient->add_child("param"),"zigzag","bool","false");
1110         }
1111 }
1112
1113 void
1114 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1115         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1116                 Glib::ustring id        =nodeElement->get_attribute_value("id");
1117                 float x1                        =atof(nodeElement->get_attribute_value("x1").data());
1118                 float y1                        =atof(nodeElement->get_attribute_value("y1").data());
1119                 float x2                        =atof(nodeElement->get_attribute_value("x2").data());
1120                 float y2                        =atof(nodeElement->get_attribute_value("y2").data());
1121                 Glib::ustring link      =nodeElement->get_attribute_value("href");
1122                 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1123
1124                 //resolve transformations
1125                 Matrix* mtx=NULL;
1126                 if(!transform.empty())
1127                         mtx=parser_transform (transform);
1128
1129                 std::list<ColorStop*> *stops;
1130                 if(!link.empty()){
1131                         stops=find_colorStop (link);
1132                 }else{
1133                         //color stops
1134                         stops=new std::list<ColorStop*>();
1135                         const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1136                         if(!nodeContent){
1137                         xmlpp::Node::NodeList list = node->get_children();
1138                         for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1139                                         Glib::ustring name =(*iter)->get_name();
1140                                         if(name.compare("stop")==0){
1141                                                 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1142                                                 Glib::ustring style     =nodeIter->get_attribute_value("style");
1143                                                 float offset=atof(nodeIter->get_attribute_value("offset").data());
1144                                                 String stop_color;
1145                                                 String opacity;
1146                                                 if(!style.empty()){
1147                                                         extractSubAttribute (style,"stop-color",&stop_color);
1148                                                         extractSubAttribute (style,"stop-opacity",&opacity);
1149                                                 }
1150                                                 if(opacity.empty()) opacity="1";
1151                                                 if(stop_color.empty()) stop_color="#000000";//black for default :S
1152                                                 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1153                                         }
1154                         }
1155                         }
1156                 }
1157                 if(stops)
1158                         lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops,mtx));
1159         }
1160 }
1161
1162 void
1163 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1164         if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1165                 Glib::ustring id        =nodeElement->get_attribute_value("id");
1166                 float cx                        =atof(nodeElement->get_attribute_value("cx").data());
1167                 float cy                        =atof(nodeElement->get_attribute_value("cy").data());
1168                 float fx                        =atof(nodeElement->get_attribute_value("fx").data());
1169                 float fy                        =atof(nodeElement->get_attribute_value("fy").data());
1170                 float r                         =atof(nodeElement->get_attribute_value("r").data());
1171                 Glib::ustring link      =nodeElement->get_attribute_value("href");//basic
1172                 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1173
1174                 if (cx!=fx || cy!=fy)
1175                         std::cout<<"SVG Parser: ignoring focus attributes for radial gradient";
1176
1177                 //resolve transformations
1178                 Matrix* mtx=NULL;
1179                 if(!transform.empty())
1180                         mtx=parser_transform (transform);
1181
1182                 std::list<ColorStop*> *stops=NULL;
1183                 if(!link.empty()){
1184                         //inkscape always use link, i dont need parser stops here, but it's posible
1185                         stops=find_colorStop (link);
1186                 }
1187                 if(stops)
1188                         rg.push_back(newRadialGradient(id,cx,cy,r,stops,mtx));
1189         }
1190 }
1191
1192 ColorStop*
1193 Svg_parser::newColorStop(String color,float opacity,float pos){
1194         ColorStop* _stop;
1195         _stop=(ColorStop*)malloc(sizeof(ColorStop));
1196         float r=getRed(color);
1197         float g=getGreen(color);
1198         float b=getBlue(color);
1199         float a=opacity;
1200         Color ret=adjustGamma(r/255,g/255,b/255,a);
1201         _stop->r=ret.get_r();
1202         _stop->g=ret.get_g();
1203         _stop->b=ret.get_b();
1204         _stop->a=ret.get_a();
1205         _stop->pos=pos;
1206         return _stop;
1207 }
1208 Color
1209 Svg_parser::adjustGamma(float r,float g,float b,float a){
1210         Color ret(r,g,b,a);
1211         if(gamma.get_gamma_r()!=1.0){
1212                 if(ret.get_r() < 0)
1213                         ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1214                 else
1215                         ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1216         }
1217         if(gamma.get_gamma_g()!=1.0){
1218                 if(ret.get_g() < 0)
1219                         ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1220                 else
1221                         ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1222         }
1223         if(gamma.get_gamma_b()!=1.0){
1224                 if(ret.get_b() < 0)
1225                         ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1226                 else
1227                         ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1228         }
1229         return ret;
1230 }
1231
1232 LinearGradient*
1233 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform){
1234         LinearGradient* data;
1235         data=(LinearGradient*)malloc(sizeof(LinearGradient));
1236         sprintf(data->name,"%s",name.data());
1237         data->x1=x1;
1238         data->y1=y1;
1239         data->x2=x2;
1240         data->y2=y2;
1241         data->stops=stops;
1242         data->transform=transform;
1243         return data;
1244 }
1245
1246 RadialGradient*
1247 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform){
1248         RadialGradient* data;
1249         data=(RadialGradient*)malloc(sizeof(RadialGradient));
1250         sprintf(data->name,"%s",name.data());
1251         data->cx=cx;
1252         data->cy=cy;
1253         data->r=r;
1254         data->stops=stops;
1255         data->transform=transform;
1256         return data;
1257 }
1258
1259 BLine*
1260 Svg_parser::newBLine(std::list<Vertex*> *points,bool loop){
1261         BLine* data;
1262         data=(BLine*)malloc(sizeof(BLine));
1263         //sprintf(data->name,"%s",name.data());
1264         data->points=new std::list<Vertex*> (*points);
1265         data->loop=loop;
1266         data->bline_id=new String(new_guid());
1267         data->offset_id=new String(new_guid());
1268         return data;
1269 }
1270
1271 void
1272 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1273         root->set_attribute("type","colorcorrect");
1274         root->set_attribute("active","true");
1275         root->set_attribute("version","0.1");
1276         root->set_attribute("desc","Gamma");
1277         build_real (root->add_child("param"),"gamma",gamma);
1278 }
1279
1280 void
1281 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1282         root->set_attribute("type","translate");
1283         root->set_attribute("active","true");
1284         root->set_attribute("version","0.1");
1285         build_vector (root->add_child("param"),"origin",dx,dy);
1286 }
1287 void
1288 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1289         root->set_attribute("type","rotate");
1290         root->set_attribute("active","true");
1291         root->set_attribute("version","0.1");
1292         build_vector (root->add_child("param"),"origin",dx,dy);
1293         build_real   (root->add_child("param"),"amount",angle);
1294 }
1295 void
1296 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertex*> p){
1297         root->set_attribute("name","vector_list");
1298         xmlpp::Element *child=root->add_child("dynamic_list");
1299         child->set_attribute("type","vector");
1300         std::list<Vertex*>::iterator aux = p.begin();
1301         while(aux!=p.end()){
1302                 xmlpp::Element *child_entry=child->add_child("entry");
1303                 xmlpp::Element *child_vector=child_entry->add_child("vector");
1304                 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1305                 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1306                 aux++;
1307         }
1308 }
1309 void
1310 Svg_parser::build_vertex(xmlpp::Element* root , Vertex *p){
1311         xmlpp::Element *child_comp=root->add_child("composite");
1312         child_comp->set_attribute("type","bline_point");
1313         build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1314         build_param (child_comp->add_child("width"),"","real","1.0000000000");
1315         build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1316         if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1317         else build_param (child_comp->add_child("split"),"","bool","false");
1318         //tangent 1
1319         xmlpp::Element *child_t1=child_comp->add_child("t1");
1320         xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1321         child_rc->set_attribute("type","vector");
1322         build_param (child_rc->add_child("radius"),"","real",p->radius1);
1323         build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1324         //tangent 2
1325         xmlpp::Element *child_t2=child_comp->add_child("t2");
1326         xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1327         child_rc2->set_attribute("type","vector");
1328         build_param (child_rc2->add_child("radius"),"","real",p->radius2);
1329         build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1330
1331 }
1332 void
1333 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertex*> p,bool loop,String blineguid){
1334         root->set_attribute("name","bline");
1335         xmlpp::Element *child=root->add_child("bline");
1336         child->set_attribute("type","bline_point");
1337         if(loop)
1338                 child->set_attribute("loop","true");
1339         else
1340                 child->set_attribute("loop","false");
1341         if(!blineguid.empty())  child->set_attribute("guid",blineguid);
1342         std::list<Vertex*>::iterator aux = p.begin();
1343         while(aux!=p.end()){
1344                 if(*aux) build_vertex (child->add_child("entry"),*aux);
1345                 aux++;
1346         }
1347 }
1348
1349 void
1350 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1351         if(!type.empty() && !value.empty()){
1352                 if(!name.empty())       root->set_attribute("name",name);
1353                 xmlpp::Element *child=root->add_child(type);
1354                 child->set_attribute("value",value);
1355         }else{
1356                 root->get_parent()->remove_child(root);
1357         }
1358 }
1359 void
1360 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1361         if(!type.empty()){
1362                 if(!name.empty()) root->set_attribute("name",name);
1363                 xmlpp::Element *child=root->add_child(type);
1364                 child->set_attribute("value",etl::strprintf ("%f",value));
1365         }else{
1366                 root->get_parent()->remove_child(root);
1367         }
1368 }
1369 void
1370 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1371         if(!type.empty()){
1372                         if(!name.empty()) root->set_attribute("name",name);
1373                         xmlpp::Element *child=root->add_child(type);
1374                         char *enteroc=new char[10];
1375                         sprintf(enteroc,"%d",value);
1376                         child->set_attribute("value",enteroc);
1377                         delete [] enteroc;
1378         }else{
1379                 root->get_parent()->remove_child(root);
1380         }
1381 }
1382
1383 void
1384 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1385         if(name.compare("")!=0) root->set_attribute("name",name);
1386         xmlpp::Element *child=root->add_child("integer");
1387         char *enteroc=new char[10];
1388         sprintf(enteroc,"%d",value);
1389         child->set_attribute("value",enteroc);
1390 }
1391 void
1392 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1393         if(name.compare("")!=0) root->set_attribute("name",name);
1394         xmlpp::Element *child=root->add_child("real");
1395         char *realc=new char[20];
1396         sprintf(realc,"%f",value);
1397         child->set_attribute("value",realc);
1398 }
1399
1400 void
1401 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1402         if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1403                 root->get_parent()->remove_child(root);
1404                 printf("Color aborted\n");
1405                 return;
1406         }
1407         Color ret=adjustGamma(r/255,g/255,b/255,a);
1408
1409         root->set_attribute("name","color");
1410         xmlpp::Element *child=root->add_child("color");
1411         child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1412         child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1413         child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1414         child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1415 }
1416 void
1417 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1418
1419         if(name.compare("")!=0) root->set_attribute("name",name);
1420         xmlpp::Element *child=root->add_child("vector");
1421         child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1422         child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1423
1424 }
1425 void
1426 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1427         if(name.compare("")!=0) root->set_attribute("name",name);
1428         xmlpp::Element *child=root->add_child("vector");
1429         if(!guid.empty()) child->set_attribute("guid",guid);
1430         child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1431         child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1432 }
1433
1434 xmlpp::Element*
1435 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1436         root->set_attribute("type","PasteCanvas");
1437         root->set_attribute("active","true");
1438         root->set_attribute("version","0.1");
1439         root->set_attribute("desc","Composite");
1440         build_param (root->add_child("param"),"z_depth","real","0");
1441         build_param (root->add_child("param"),"amount","real","1");
1442         build_param (root->add_child("param"),"blend_method","integer","0");
1443         build_vector (root->add_child("param"),"origin",0,0);
1444         xmlpp::Element *child=root->add_child("param");
1445         child->set_attribute("name","canvas");
1446         return child->add_child("canvas");
1447 }
1448
1449 /* === COORDINATES & TRANSFORMATIONS ======================================= */
1450 void
1451 Svg_parser::coor2vect(float *x,float *y){
1452         float sx, sy;
1453         sx=*x;
1454         sy=*y;
1455         sy= atof(height.c_str())-sy;
1456         sx= sx - ox;
1457         sy= sy - oy;
1458         sx= sx / kux;
1459         sy= sy / kux;
1460         *x=sx; *y=sy;
1461 }
1462
1463 void
1464 Svg_parser::setTg1(Vertex *p,float p1x,float p1y,float p2x,float p2y){
1465         float rd=0,ag=0;
1466         float d1x,d1y,d2x,d2y,dx,dy;
1467         d1x=p1x*60;
1468         d1y=p1y*60;
1469         d2x=p2x*60;
1470         d2y=p2y*60;
1471         dx=d2x-d1x;
1472         dy=d2y-d1y;
1473         dx=dx*3;
1474         dy=dy*3;
1475         dx=dx/60;
1476         dy=dy/60;
1477         rd=sqrt(dx*dx + dy*dy);
1478         if(dx>0 && dy>0){
1479                 ag=PI + atan(dy/dx);
1480         }else if(dx>0 && dy<0){
1481                 ag=PI + atan(dy/dx);
1482         }else if(dx<0 && dy<0){
1483                 ag=atan(dy/dx);
1484         }else if(dx<0 && dy>0){
1485                 ag= 2*PI+atan(dy/dx);
1486         }else if(dx==0 && dy>0){
1487                 ag=-1*PI/2;
1488         }else if(dx==0 && dy<0){
1489                 ag=PI/2;
1490         }else if(dx==0 && dy==0){
1491                 ag=0;
1492         }else if(dx<0 && dy==0){
1493                 ag=0;
1494         }else if(dx>0 && dy==0){
1495                 ag=PI;
1496         }
1497         ag= (ag*180)/PI;
1498         p->radius1=rd;
1499         p->angle1=ag;
1500 }
1501 void
1502 Svg_parser::setTg2(Vertex* p,float p1x,float p1y,float p2x,float p2y){
1503         float rd=0,ag=0;
1504         float d1x,d1y,d2x,d2y,dx,dy;
1505         d1x=p1x*60;
1506         d1y=p1y*60;
1507         d2x=p2x*60;
1508         d2y=p2y*60;
1509         dx=d2x-d1x;
1510         dy=d2y-d1y;
1511         dx=dx*3;
1512         dy=dy*3;
1513         dx=dx/60;
1514         dy=dy/60;
1515
1516         rd=sqrt(dx*dx + dy*dy);
1517         if(dx>0 && dy>0){
1518                 ag=PI + atan(dy/dx);
1519         //      printf("case 180-270\n");
1520         }else if(dx>0 && dy<0){
1521                 ag=PI + atan(dy/dx);
1522         //      printf("case 90-180\n");
1523         }else if(dx<0 && dy<0){
1524                 ag=atan(dy/dx);
1525         //      printf("case 0-90\n");
1526         }else if(dx<0 && dy>0){
1527                 ag= 2*PI+atan(dy/dx);
1528         //      printf("case 270-360\n");
1529         }else if(dx==0 && dy>0){
1530                 ag=-1*PI/2;
1531         }else if(dx==0 && dy<0){
1532                 ag=PI/2;
1533         }else if(dx==0 && dy==0){
1534                 ag=0;
1535         }else if(dx<0 && dy==0){
1536                 ag=0;
1537         }else if(dx>0 && dy==0){
1538                 ag=PI;
1539         }
1540         ag= (ag*180)/PI;
1541         ag=ag-180;
1542         p->radius2=rd;
1543         p->angle2=ag;
1544 }
1545
1546 void
1547 Svg_parser::setSplit(Vertex* p,bool val){
1548         if(p!=NULL){
1549                 p->split=val;
1550         }
1551 }
1552 int
1553 Svg_parser::isFirst(Vertex* nodo,float a, float b){
1554         if(nodo->x==a && nodo->y==b)
1555                 return 1;
1556         return 0;
1557 }
1558
1559 Vertex*
1560 Svg_parser::newVertex(float x,float y){
1561         Vertex* vert;
1562         vert=(Vertex*)malloc(sizeof(Vertex));
1563         vert->x=x;
1564         vert->y=y;
1565         vert->radius1=vert->radius2=vert->angle1=vert->angle2=0;
1566         return vert;
1567 }
1568
1569 //matrices
1570 Matrix*
1571 Svg_parser::parser_transform(const String transform){
1572         Matrix* a=NULL;
1573         String tf(transform);
1574         removeIntoS(&tf);
1575         std::vector<String> tokens=tokenize(tf," ");
1576         std::vector<String>::iterator aux=tokens.begin();
1577         while(aux!=tokens.end()){
1578                 if((*aux).compare(0,9,"translate")==0){
1579                         float dx,dy;
1580                         int start,end;
1581                         start   =(*aux).find_first_of("(")+1;
1582                         end             =(*aux).find_first_of(",");
1583                         dx              =atof((*aux).substr(start,end-start).data());
1584                         start   =(*aux).find_first_of(",")+1;
1585                         end             =(*aux).size()-1;
1586                         dy              =atof((*aux).substr(start,end-start).data());
1587                         if(matrixIsNull(a))
1588                                 a=newMatrix(1,0,0,1,dx,dy);
1589                         else
1590                                 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1591                 }else if((*aux).compare(0,5,"scale")==0){
1592                         if(matrixIsNull(a))
1593                                 a=newMatrix(1,0,0,1,0,0);
1594                 }else if((*aux).compare(0,6,"rotate")==0){
1595                         float angle,seno,coseno;
1596                         int start,end;
1597                         start   =(*aux).find_first_of("(")+1;
1598                         end             =(*aux).size()-1;
1599                         angle=getRadian (atof((*aux).substr(start,end-start).data()));
1600                         seno   =sin(angle);
1601                         coseno =cos(angle);
1602                         if(matrixIsNull(a))
1603                                 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1604                         else
1605                                 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1606                 }else if((*aux).compare(0,6,"matrix")==0){
1607                         int start       =(*aux).find_first_of('(')+1;
1608                         int end         =(*aux).find_first_of(')');
1609                         if(matrixIsNull(a))
1610                                 a=newMatrix((*aux).substr(start,end-start));
1611                         else
1612                                 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1613                 }else{
1614                         a=newMatrix(1,0,0,1,0,0);
1615                 }
1616                 aux++;
1617         }
1618         return a;
1619 }
1620
1621 Matrix*
1622 Svg_parser::newMatrix(Matrix *a){
1623         Matrix* data;
1624         data=(Matrix*)malloc(sizeof(Matrix));
1625         data->a=a->a;           data->b=a->b;           data->c=a->c;
1626         data->d=a->d;           data->e=a->e;           data->f=a->f;
1627         return data;
1628 }
1629 Matrix*
1630 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1631         Matrix* data;
1632         data=(Matrix*)malloc(sizeof(Matrix));
1633         data->a=a;              data->b=b;              data->c=c;
1634         data->d=d;              data->e=e;              data->f=f;
1635         return data;
1636 }
1637 Matrix*
1638 Svg_parser::newMatrix(const String mvector){
1639         if(!mvector.empty()){
1640                 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1641                 std::vector<String> tokens=tokenize(mvector,",");
1642                 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1643                 data->a=atof(tokens.at(0).data());
1644                 data->b=atof(tokens.at(1).data());
1645                 data->c=atof(tokens.at(2).data());
1646                 data->d=atof(tokens.at(3).data());
1647                 data->e=atof(tokens.at(4).data());
1648                 data->f=atof(tokens.at(5).data());
1649                 return data;
1650         }else{
1651                 return newMatrix(1,0,0,1,0,0);
1652         }
1653 }
1654 void
1655 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1656         float auxa,auxb;
1657         auxa=0;
1658         auxb=0;
1659         auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1660         auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1661         *a=auxa;
1662         *b=auxb;
1663         return;
1664 }
1665 void
1666 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1667         Matrix* aux=newMatrix(0,0,0,0,0,0);
1668         aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1669         aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1670         aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1671         aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1672         aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1673         aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1674         *mtx=aux;
1675 }
1676 void
1677 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1678         Matrix* aux=newMatrix(0,0,0,0,0,0);
1679         aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1680         aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1681         aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1682         aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1683         aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1684         aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1685         (*mtx1)->a=aux->a;
1686         (*mtx1)->b=aux->b;
1687         (*mtx1)->c=aux->c;
1688         (*mtx1)->d=aux->d;
1689         (*mtx1)->e=aux->e;
1690         (*mtx1)->f=aux->f;
1691 }
1692 bool
1693 Svg_parser::matrixIsNull(Matrix *mtx){
1694         if(mtx == NULL) return true;
1695         return false;
1696 }
1697
1698 /* === EXTRA METHODS ======================================================= */
1699
1700 int
1701 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1702         int encounter=0;
1703         if(!attribute.empty()){
1704                 String str(attribute);
1705                 removeS(&str);
1706                 std::vector<String> tokens=tokenize(str,";");
1707                 std::vector<String>::iterator aux=tokens.begin();
1708                 while(aux!=tokens.end()){
1709                         int mid= (*aux).find_first_of(":");
1710                         if((*aux).substr(0,mid).compare(name)==0){
1711                                 int end=(*aux).size();
1712                                 *value=(*aux).substr(mid+1,end-mid);
1713                                 return 1;
1714                         }
1715                         aux++;
1716                 }
1717         }
1718         return encounter;
1719 }
1720 String
1721 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1722         String value;
1723         int fnd=0;
1724         if(!path_style.empty())
1725                 fnd=extractSubAttribute(path_style,name,&value);
1726         if(fnd==0){
1727                 if(!master_style.empty())
1728                         fnd=extractSubAttribute(master_style,name,&value);
1729                 if(fnd==0)
1730                         value=defaultVal;
1731         }
1732         return value;
1733 }
1734 String
1735 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1736         String value;
1737         int fnd=0;
1738         if(!path_style.empty())
1739                 fnd=extractSubAttribute(path_style,name,&value);
1740         if(fnd==0 && !master_style.empty())
1741                         fnd=extractSubAttribute(master_style,name,&value);
1742         if(fnd==0){
1743                 if(!subattribute.empty())
1744                         value=subattribute;
1745                 else
1746                         value=defaultVal;
1747         }
1748         return value;
1749 }
1750
1751 std::vector<String>
1752 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
1753         std::vector<String> tokens;
1754         String buffer;
1755         int e=0;
1756         unsigned int i=0;
1757         char a;
1758         while(i<path.size()){
1759                 a=path.at(i);
1760                 switch(e){
1761                         case 0: //initial state
1762                                         if(a=='m'){ e=1; i++;}
1763                                         else if(a=='c'){ e= 2; i++;}
1764                                         else if(a=='q'){ e= 3; i++;}
1765                                         else if(a=='t'){ e= 4; i++;}
1766                                         else if(a=='a'){ e= 5; i++;}
1767                                         else if(a=='l'){ e= 6; i++;}
1768                                         else if(a=='v'){ e= 7; i++;}
1769                                         else if(a=='h'){ e= 8; i++;}
1770                                         else if(a=='M'){ e= 9; i++;}
1771                                         else if(a=='C'){ e=10; i++;}
1772                                         else if(a=='Q'){ e=11; i++;}
1773                                         else if(a=='T'){ e=12; i++;}
1774                                         else if(a=='A'){ e=13; i++;}
1775                                         else if(a=='L'){ e=14; i++;}
1776                                         else if(a=='V'){ e=15; i++;}
1777                                         else if(a=='H'){ e=16; i++;}
1778                                         else if(a=='z' || a=='Z'){ e=17; i++;}
1779                                         else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
1780                                         else if(a==','){ e=19; i++;}
1781                                         else if(a==' '){i++;}
1782                                         break;
1783                         //relative
1784                         case 1 : tokens.push_back("m"); e=0; break;//move
1785                         case 2 : tokens.push_back("c"); e=0; break;//curve
1786                         case 3 : tokens.push_back("q"); e=0; break;//quadratic
1787                         case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
1788                         case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
1789                         case 6 : tokens.push_back("l"); e=0; break;//line to
1790                         case 7 : tokens.push_back("v"); e=0; break;//vertical
1791                         case 8 : tokens.push_back("h"); e=0; break;//horizontal
1792                         //absolute
1793                         case 9 : tokens.push_back("M"); e=0; break;
1794                         case 10: tokens.push_back("C"); e=0; break;
1795                         case 11: tokens.push_back("Q"); e=0; break;
1796                         case 12: tokens.push_back("T"); e=0; break;
1797                         case 13: tokens.push_back("A"); e=0; break;
1798                         case 14: tokens.push_back("L"); e=0; break;
1799                         case 15: tokens.push_back("V"); e=0; break;
1800                         case 16: tokens.push_back("H"); e=0; break;
1801
1802                         case 17: tokens.push_back("z"); e=0; break;//loop
1803                         case 18: if(a=='-'||a=='.'|| isdigit (a)){
1804                                                 buffer.append(path.substr(i,1));i++;
1805                                         }else{
1806                                                 e=20;
1807                                         }
1808                                         break;
1809                         case 19: tokens.push_back(","); e=0; break;
1810                         case 20: tokens.push_back(buffer);
1811                                         buffer.clear();
1812                                         e=0; break;
1813                         default: break;
1814                 }
1815         }
1816         switch(e){//last element
1817                 case 1 : tokens.push_back("m"); break;
1818                 case 2 : tokens.push_back("c"); break;
1819                 case 3 : tokens.push_back("q"); break;
1820                 case 4 : tokens.push_back("t"); break;
1821                 case 5 : tokens.push_back("a"); break;
1822                 case 6 : tokens.push_back("l"); break;
1823                 case 7 : tokens.push_back("v"); break;
1824                 case 8 : tokens.push_back("h"); break;
1825                 case 9 : tokens.push_back("M"); break;
1826                 case 10: tokens.push_back("C"); break;
1827                 case 11: tokens.push_back("Q"); break;
1828                 case 12: tokens.push_back("T"); break;
1829                 case 13: tokens.push_back("A"); break;
1830                 case 14: tokens.push_back("L"); break;
1831                 case 15: tokens.push_back("V"); break;
1832                 case 16: tokens.push_back("H"); break;
1833                 case 17: tokens.push_back("z"); break;
1834                 case 18: tokens.push_back(buffer); break;
1835                 case 19: tokens.push_back(","); break;
1836                 case 20: tokens.push_back(buffer); break;
1837                 default: break;
1838         }
1839         return tokens;
1840 }
1841
1842 int
1843 Svg_parser::randomLetter(){
1844         int a=rand()%2;
1845         if(a) return (49 + rand()%9);
1846         else return  (65 + rand()%24);
1847 }
1848
1849 int
1850 Svg_parser::getRed(String hex){
1851         if(hex.at(0)=='#'){
1852                 return hextodec(hex.substr(1,2));
1853         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1854                 int start=hex.find_first_of("(")+1;
1855                 int end =hex.find_last_of(")");
1856                 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1857                 return atoi(aux.data());
1858         }
1859         return 0;
1860 }
1861 int
1862 Svg_parser::getGreen(String hex){
1863         if(hex.at(0)=='#'){
1864                 return hextodec(hex.substr(3,2));
1865         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1866                 int start=hex.find_first_of("(")+1;
1867                 int end =hex.find_last_of(")");
1868                 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1869                 return atoi(aux.data());
1870         }
1871         return 0;
1872 }
1873 int
1874 Svg_parser::getBlue(String hex){
1875         if(hex.at(0)=='#'){
1876                 return hextodec(hex.substr(5,2));
1877         }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1878                 int start=hex.find_first_of("(")+1;
1879                 int end =hex.find_last_of(")");
1880                 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1881                 return atoi(aux.data());
1882         }
1883         return 0;
1884 }
1885 int
1886 Svg_parser::hextodec(String hex){
1887         int result=0;
1888         if(!hex.empty()){
1889                 int top=hex.size();
1890                 int ihex[top];
1891                 int i=0;
1892                 while(i<top){
1893                         if(hex.at(i)=='0')
1894                                 ihex[i]=0;
1895                         else if(hex.at(i)=='1')
1896                                 ihex[i]=1;
1897                         else if(hex.at(i)=='2')
1898                                 ihex[i]=2;
1899                         else if(hex.at(i)=='3')
1900                                 ihex[i]=3;
1901                         else if(hex.at(i)=='4')
1902                                 ihex[i]=4;
1903                         else if(hex.at(i)=='5')
1904                                 ihex[i]=5;
1905                         else if(hex.at(i)=='6')
1906                                 ihex[i]=6;
1907                         else if(hex.at(i)=='7')
1908                                 ihex[i]=7;
1909                         else if(hex.at(i)=='8')
1910                                 ihex[i]=8;
1911                         else if(hex.at(i)=='9')
1912                                 ihex[i]=9;
1913                         else if(hex.at(i)=='a')
1914                                 ihex[i]=10;
1915                         else if(hex.at(i)=='b')
1916                                 ihex[i]=11;
1917                         else if(hex.at(i)=='c')
1918                                 ihex[i]=12;
1919                         else if(hex.at(i)=='d')
1920                                 ihex[i]=13;
1921                         else if(hex.at(i)=='e')
1922                                 ihex[i]=14;
1923                         else if(hex.at(i)=='f')
1924                                 ihex[i]=15;
1925                         else
1926                                 return 0;
1927                         i++;
1928                 }
1929                 i=0;
1930                 while(i<top){
1931                         result+=pow(16,i)*ihex[top-i-1];
1932                         i++;
1933                 }
1934         }
1935         return result;
1936 }
1937
1938 float
1939 Svg_parser::getDimension(const String ac){
1940         if(ac.empty()){
1941                 return 0;
1942         }
1943         int length=ac.size();
1944         float af=0;
1945         if(isdigit(ac.at(length-1))){
1946                 af=atof(ac.data());
1947         }else if(ac.at(length-1)=='%'){
1948                         return 1024;
1949         }else{
1950                 String mtc=ac.substr(length-2,length);
1951                 String nmc=ac.substr(0,length-2);
1952                 if(mtc.compare("px")==0){
1953                         af=atof(nmc.data());
1954                 }else if(mtc.compare("pt")==0){
1955                         af=atof(nmc.data())*1.25;
1956                 }else if(mtc.compare("em")==0){
1957                         af=atof(nmc.data())*16;
1958                 }else if(mtc.compare("mm")==0){
1959                         af=atof(nmc.data())*3.54;
1960                 }else if(mtc.compare("pc")==0){
1961                         af=atof(nmc.data())*15;
1962                 }else if(mtc.compare("cm")==0){
1963                         af=atof(nmc.data())*35.43;
1964                 }else if(mtc.compare("in")==0){
1965                         af=atof(nmc.data())*90;
1966                 }else{
1967                         return 1024;
1968                 }
1969         }
1970         return af;
1971 }
1972
1973 float
1974 Svg_parser::getRadian(float sexa){
1975         return (sexa*2*PI)/360;
1976 }
1977 void
1978 Svg_parser::removeS(String *input){
1979         for(unsigned int i=0;i<input->size();i++){
1980                 if(input->at(i)==' '){
1981                         input->erase(i,1);
1982                 }
1983         }
1984 }
1985 void
1986 Svg_parser::removeIntoS(String *input){
1987         bool into=false;
1988         for(unsigned int i=0;i<input->size();i++){
1989                 if(input->at(i)=='('){
1990                         into=true;
1991                 }else if(input->at(i)==')'){
1992                         into=false;
1993                 }else if(into && input->at(i)==' '){
1994                         input->erase(i,1);
1995                 }
1996         }
1997 }
1998 std::vector<String>
1999 Svg_parser::tokenize(const String& str,const String& delimiters){
2000         std::vector<String> tokens;
2001         String::size_type lastPos = str.find_first_not_of(delimiters, 0);
2002         String::size_type pos = str.find_first_of(delimiters, lastPos);
2003         while (String::npos != pos || String::npos != lastPos){
2004                 tokens.push_back(str.substr(lastPos, pos - lastPos));
2005                 lastPos = str.find_first_not_of(delimiters, pos);
2006                 pos = str.find_first_of(delimiters, lastPos);
2007         }
2008         return tokens;
2009 }
2010 String
2011 Svg_parser::new_guid(){
2012         uid++;
2013         return GUID::hasher(uid).get_string();
2014 }
2015