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