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