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