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