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