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