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