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