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
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
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.
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.
25 /* ========================================================================= */
27 /* === H E A D E R S ======================================================= */
34 #include "svg_parser.h"
36 /* === U S I N G =========================================================== */
38 using namespace synfig;
40 /* === G L O B A L S ======================================================= */
42 /* === P R O C E D U R E S ================================================= */
45 synfig::open_svg(std::string _filepath,String &errors, String &warnings){
46 Canvas::Handle canvas;
50 canvas=parser.load_svg_canvas(_filepath,errors,warnings);
51 //canvas->set_id(parser.get_id());
53 std::cout<<"error"<<std::endl;
59 Svg_parser::load_svg_canvas(std::string _filepath,String &errors, String &warnings){
61 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
63 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
65 parser.set_substitute_entities();
66 parser.parse_file(filepath);
69 const xmlpp::Node* pNode = parser.get_document()->get_root_node();
72 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
73 }catch(const std::exception& ex){
74 std::cout << "Exception caught: " << ex.what() << std::endl;
76 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
77 Canvas::Handle canvas;
79 //canvas=synfig::open_canvas(nodeRoot,_filepath,errors,warnings);
80 canvas=synfig::open_canvas(nodeRoot,errors,warnings);
85 Svg_parser::Svg_parser(){
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!!!!
95 if(!id_name.empty()) return id_name;
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);
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)
114 id_name="id_arbitrario";
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);
126 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
129 Glib::ustring nodename = node->get_name();
130 if(!nodeText && !nodeComment && !nodename.empty()){
131 if(nodename.compare("svg")==0){
133 }else if(nodename.compare("namedview")==0){
135 }else if(nodename.compare("defs")==0){
137 }else if(nodename.compare("g")==0){
138 if(set_canvas==0) parser_canvas (node);
139 parser_layer (node,nodeRoot->add_child("layer"),"",NULL);
141 }else if(nodename.compare("rect")==0){
142 if(set_canvas==0) parser_canvas (node);
143 parser_rect(node,nodeRoot,"",NULL);
144 }else if(nodename.compare("polygon")==0){
145 if(set_canvas==0) parser_canvas (node);
146 parser_polygon(node,nodeRoot,"",NULL);
147 }else if(nodename.compare("path")==0){
148 if(set_canvas==0) parser_canvas (node);
149 parser_path (node,nodeRoot,"",NULL);
153 xmlpp::Node::NodeList list = node->get_children();
154 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
155 parser_node(*iter); //recursive
162 Svg_parser::parser_svg (const xmlpp::Node* node){
163 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
164 width =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("width")));
165 height =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("height")));
166 docname=nodeElement->get_attribute_value("docname","");
170 Svg_parser::parser_canvas (const xmlpp::Node* node){
171 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
172 if(width.compare("")==0){
173 width=nodeElement->get_attribute_value("width","");
175 if(height.compare("")==0){
176 height=nodeElement->get_attribute_value("height","");
178 if(width.compare("")==0 && height.compare("")!=0){
181 if(width.compare("")!=0 && height.compare("")==0){
184 if(height.compare("")==0 && width.compare("")==0){
189 nodeRoot=document.create_root_node("canvas", "", "");
190 nodeRoot->set_attribute("version","0.5");
191 nodeRoot->set_attribute("width",width);
192 nodeRoot->set_attribute("height",height);
193 nodeRoot->set_attribute("xres","2834.645752");
194 nodeRoot->set_attribute("yres","2834.645752");
197 view_x=atof(width.c_str())/kux;
198 view_y=atof(height.c_str())/kux;
201 char attr_view_box[60];
202 sprintf(attr_view_box,"%f %f %f %f",-1.0*view_x,view_y,view_x,-1.0*view_y);
203 nodeRoot->set_attribute("view-box",attr_view_box);
204 ox=atof(width.c_str() )/2;
205 oy=atof(height.c_str())/2;
206 nodeRoot->set_attribute("antialias","1");
207 nodeRoot->set_attribute("fps","24.000");
208 nodeRoot->set_attribute("begin-time","0f");
209 nodeRoot->set_attribute("end-time","5s");
210 nodeRoot->set_attribute("bgcolor","0.500000 0.500000 0.500000 1.000000");
211 if(!id_name.empty()) nodeRoot->add_child("name")->set_child_text(id_name);
212 else nodeRoot->add_child("name")->set_child_text("Synfig Animation 1");
219 Svg_parser::parser_rect(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
220 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
221 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
222 Glib::ustring rect_style =nodeElement->get_attribute_value("style");
223 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
224 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
225 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
226 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
228 String fill =loadAttribute("fill",rect_style,parent_style,"none");
229 String fill_opacity =loadAttribute("fill-opacity",rect_style,parent_style,"1");
230 String opacity =loadAttribute("opacity",rect_style,parent_style,"1");
232 //it's some complicated
237 if(fill.compare(0,3,"url")==0){
239 root=nodeStartBasicLayer(root->add_child("layer"));
241 xmlpp::Element *child_rect=root->add_child("layer");
242 child_rect->set_attribute("type","rectangle");
243 child_rect->set_attribute("active","true");
244 child_rect->set_attribute("version","0.2");
245 child_rect->set_attribute("desc",rect_id);
247 build_real(child_rect->add_child("param"),"z_depth",0.0);
248 build_real(child_rect->add_child("param"),"amount",1.0);
249 build_integer(child_rect->add_child("param"),"blend_method",0);
250 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
252 float auxx=atof(rect_x.c_str());
253 float auxy=atof(rect_y.c_str());
254 coor2vect(&auxx,&auxy);
255 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
256 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
257 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
258 coor2vect(&auxx,&auxy);
259 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
261 build_url (root->add_child("layer"),fill,mtx_parent);
266 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
267 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
268 Glib::ustring label =nodeElement->get_attribute_value("label");
269 Glib::ustring style =nodeElement->get_attribute_value("style");
270 Glib::ustring fill =nodeElement->get_attribute_value("fill");
271 Glib::ustring transform =nodeElement->get_attribute_value("transform");
276 }else if(!fill.empty()){
277 layer_style.append("fill:");
278 layer_style.append(fill);
279 }else if(!parent_style.empty()){
280 layer_style=parent_style;
283 if(!transform.empty())
284 mtx=build_transform (transform);
287 composeMatrix(&mtx,mtx_parent,mtx);
289 mtx=newMatrix (mtx_parent);
292 root->set_attribute("type","PasteCanvas");
293 root->set_attribute("active","true");
294 root->set_attribute("version","0.1");
295 if(!label.empty()) root->set_attribute("desc",label);
296 else root->set_attribute("desc","unknow layer");
298 build_real(root->add_child("param"),"z_depth",0.0);
299 build_real(root->add_child("param"),"amount",1.0);
300 build_integer(root->add_child("param"),"blend_method",0);
301 build_vector (root->add_child("param"),"origin",0,0);
303 //printf(" canvas attributes ");
305 xmlpp::Element *child_canvas=root->add_child("param");
306 child_canvas->set_attribute("name","canvas");
307 child_canvas=child_canvas->add_child("canvas");
308 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
310 xmlpp::Node::NodeList list = node->get_children();
311 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
312 Glib::ustring name =(*iter)->get_name();
313 if(name.compare("g")==0){
314 parser_layer (*iter,child_canvas->add_child("layer"),layer_style,mtx);
315 }else if(name.compare("path")==0){
316 parser_path (*iter,child_canvas,layer_style,mtx);
317 }else if(name.compare("polygon")==0){
318 parser_polygon (*iter,child_canvas,layer_style,mtx);
319 }else if(name.compare("rect")==0){
320 parser_rect (*iter,child_canvas,layer_style,mtx);
327 Svg_parser::parser_polygon(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
328 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
329 //load sub-attributes
330 Glib::ustring polygon_style =nodeElement->get_attribute_value("style");
331 Glib::ustring polygon_id =nodeElement->get_attribute_value("id");
332 Glib::ustring polygon_points =nodeElement->get_attribute_value("points");
333 Glib::ustring polygon_transform =nodeElement->get_attribute_value("transform");
334 Glib::ustring polygon_fill =nodeElement->get_attribute_value("fill");
335 String fill =loadAttribute("fill",polygon_style,parent_style,polygon_fill,"none");
336 String fill_rule =loadAttribute("fill-rule",polygon_style,parent_style,"evenodd");
337 String fill_opacity =loadAttribute("fill-opacity",polygon_style,parent_style,"1");
338 String opacity =loadAttribute("opacity",polygon_style,parent_style,"1");
342 if(!polygon_transform.empty())
343 mtx=build_transform (polygon_transform);
346 composeMatrix(&mtx,mtx_parent,mtx);
348 mtx=newMatrix (mtx_parent);
351 if(polygon_points.empty())
353 std::list<Vertice*> k;
354 std::vector<String> tokens=get_tokens_path (polygon_points);
356 float ax,ay; ax=ay=0;
357 for(i=0;i<tokens.size();i++){
358 ax=atof(tokens.at(i).data());
359 i++; if(tokens.at(i).compare(",")==0) i++;
360 ay=atof(tokens.at(i).data());
362 if(mtx) transformPoint2D(mtx,&ax,&ay);
366 k.push_back(newVertice(ax,ay));
369 xmlpp::Element *child_polygon;
373 if(fill.compare(0,3,"url")==0){
374 typeFill=2;//gradient
375 root=nodeStartBasicLayer(root->add_child("layer"));
377 child_polygon=root->add_child("layer");
378 child_polygon->set_attribute("type","polygon");
379 child_polygon->set_attribute("active","true");
380 child_polygon->set_attribute("version","0.1");
381 child_polygon->set_attribute("desc",polygon_id);
382 build_param (child_polygon->add_child("param"),"z_depth","real","0.0000000000");
383 build_param (child_polygon->add_child("param"),"amount","real","1.0000000000");
384 build_param (child_polygon->add_child("param"),"blend_method","integer","0");
385 build_color (child_polygon->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
386 build_vector(child_polygon->add_child("param"),"offset",0,0);
387 build_param (child_polygon->add_child("param"),"invert","bool","false");
388 build_param (child_polygon->add_child("param"),"antialias","bool","true");
389 build_param (child_polygon->add_child("param"),"feather","real","0.0000000000");
390 build_param (child_polygon->add_child("param"),"blurtype","integer","1");
391 if(fill_rule.compare("evenodd")==0) build_param (child_polygon->add_child("param"),"winding_style","integer","1");
392 else build_param (child_polygon->add_child("param"),"winding_style","integer","0");
393 build_points (child_polygon->add_child("param"),k);
396 build_url(root->add_child("layer"),fill,mtx);
401 Svg_parser::parser_path(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
402 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
403 //load sub-attributes
404 Glib::ustring path_style =nodeElement->get_attribute_value("style");
405 Glib::ustring path_id =nodeElement->get_attribute_value("id");
406 Glib::ustring path_d =nodeElement->get_attribute_value("d");
407 Glib::ustring path_transform =nodeElement->get_attribute_value("transform");
408 Glib::ustring path_fill =nodeElement->get_attribute_value("fill");
410 String fill =loadAttribute("fill",path_style,parent_style,path_fill,"none");
411 String fill_rule =loadAttribute("fill-rule",path_style,parent_style,"evenodd");
412 String stroke =loadAttribute("stroke",path_style,parent_style,"none");
413 String stroke_width =loadAttribute("stroke-width",path_style,parent_style,"1px");
414 String stroke_linecap =loadAttribute("stroke-linecap",path_style,parent_style,"butt");
415 String stroke_linejoin =loadAttribute("stroke-linejoin",path_style,parent_style,"miter");
416 String stroke_opacity =loadAttribute("stroke-opacity",path_style,parent_style,"1");
417 String fill_opacity =loadAttribute("fill-opacity",path_style,parent_style,"1");
418 String opacity =loadAttribute("opacity",path_style,parent_style,"1");
420 //resolve transformations
422 if(!path_transform.empty())
423 mtx=build_transform (path_transform);
426 composeMatrix(&mtx,mtx_parent,mtx);
428 mtx=newMatrix(mtx_parent);
430 //parser path_d attribute, this is obviously important
431 std::list<std::list<Vertice*> > k;
432 k=parser_path_d (path_d,mtx);
435 int typeFill=0; //nothing
437 if(fill.compare("none")!=0){
440 if(typeFill==1 && fill.compare(0,3,"url")==0){
441 typeFill=2; //gradient
444 int typeStroke=0;//nothing
446 if(stroke.compare("none")!=0){
447 typeStroke=1; //simple
449 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
450 typeStroke=2; //gradient
455 if(n!=1){ //if n is > than 1 then we must create a paste canvas for all paths
456 root=nodeStartBasicLayer(root->add_child("layer"));
458 std::list<std::list<Vertice*> >::iterator aux = k.begin();
459 for (; aux!=k.end(); aux++){
460 if(typeFill!=0 && typeStroke!=0){
462 offset_id=new_guid();
464 if(typeFill==1 || typeFill==2){//region layer
465 xmlpp::Element *child_fill=root;
466 if(n==1 && typeFill==2){//open gradient or url (fill)
467 child_fill=nodeStartBasicLayer(root->add_child("layer"));
469 xmlpp::Element *child_region=child_fill->add_child("layer");
470 child_region->set_attribute("type","region");
471 child_region->set_attribute("active","true");
472 child_region->set_attribute("version","0.1");
473 child_region->set_attribute("desc",path_id);
474 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
475 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
476 build_param (child_region->add_child("param"),"blend_method","integer","0");
477 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
478 if(offset_id.empty()) build_vector (child_region->add_child("param"),"offset",0,0);
479 else build_vector (child_region->add_child("param"),"offset",0,0,offset_id);
480 build_param (child_region->add_child("param"),"invert","bool","false");
481 build_param (child_region->add_child("param"),"antialias","bool","true");
482 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
483 build_param (child_region->add_child("param"),"blurtype","integer","1");
484 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
485 else build_param (child_region->add_child("param"),"winding_style","integer","0");
487 build_bline (child_region->add_child("param"),*aux,loop,bline_id);
489 if(n==1 && typeFill==2){ //gradient in onto mode (fill)
490 build_url(child_fill->add_child("layer"),fill,mtx);
494 if(typeStroke==1 || typeStroke==2){ //layer outline
495 xmlpp::Element *child_stroke=root;
496 if(n==1 && typeStroke==2){//open gradient in straigth onto (stroke)
497 child_stroke=nodeStartBasicLayer(root->add_child("layer"));
499 xmlpp::Element *child_outline=child_stroke->add_child("layer");
500 child_outline->set_attribute("type","outline");
501 child_outline->set_attribute("active","true");
502 child_outline->set_attribute("version","0.2");
503 child_outline->set_attribute("desc",path_id);
504 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
505 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
506 build_param (child_outline->add_child("param"),"blend_method","integer","0");
507 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
508 if(offset_id.empty()) build_vector (child_outline->add_child("param"),"offset",0,0);
509 else build_vector (child_outline->add_child("param"),"offset",0,0,offset_id);
510 build_param (child_outline->add_child("param"),"invert","bool","false");
511 build_param (child_outline->add_child("param"),"antialias","bool","true");
512 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
513 build_param (child_outline->add_child("param"),"blurtype","integer","1");
515 build_param (child_outline->add_child("param"),"winding_style","integer","0");
517 build_bline (child_outline->add_child("param"),*aux,loop,bline_id);
519 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
520 build_param (child_outline->add_child("param"),"width","real",stroke_width);
521 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
522 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
523 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
524 if(stroke_linecap.compare("butt")==0){
525 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
526 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
528 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
529 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
531 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
532 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
534 if(n==1 && typeStroke==2){ //gradient in onto mode (stroke)
535 build_url(child_stroke->add_child("layer"),stroke,mtx);
539 if(n!=1){//only fill for several canvas in one path
541 build_url(root->add_child("layer"),fill,mtx);
548 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
549 std::vector<String> tokens;
554 while(i<path.size()){
557 case 0: //initial state
558 if(a=='m'){ e=1; i++;}
559 else if(a=='c'){ e= 2; i++;}
560 else if(a=='q'){ e= 3; i++;}
561 else if(a=='t'){ e= 4; i++;}
562 else if(a=='a'){ e= 5; i++;}
563 else if(a=='l'){ e= 6; i++;}
564 else if(a=='v'){ e= 7; i++;}
565 else if(a=='h'){ e= 8; i++;}
566 else if(a=='M'){ e= 9; i++;}
567 else if(a=='C'){ e=10; i++;}
568 else if(a=='Q'){ e=11; i++;}
569 else if(a=='T'){ e=12; i++;}
570 else if(a=='A'){ e=13; i++;}
571 else if(a=='L'){ e=14; i++;}
572 else if(a=='V'){ e=15; i++;}
573 else if(a=='H'){ e=16; i++;}
574 else if(a=='z' || a=='Z'){ e=17; i++;}
575 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
576 else if(a==','){ e=19; i++;}
577 else if(a==' '){i++;}
580 case 1 : tokens.push_back("m"); e=0; break;//move
581 case 2 : tokens.push_back("c"); e=0; break;//curve
582 case 3 : tokens.push_back("q"); e=0; break;//quadratic
583 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
584 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
585 case 6 : tokens.push_back("l"); e=0; break;//line to
586 case 7 : tokens.push_back("v"); e=0; break;//vertical
587 case 8 : tokens.push_back("h"); e=0; break;//horizontal
589 case 9 : tokens.push_back("M"); e=0; break;
590 case 10: tokens.push_back("C"); e=0; break;
591 case 11: tokens.push_back("Q"); e=0; break;
592 case 12: tokens.push_back("T"); e=0; break;
593 case 13: tokens.push_back("A"); e=0; break;
594 case 14: tokens.push_back("L"); e=0; break;
595 case 15: tokens.push_back("V"); e=0; break;
596 case 16: tokens.push_back("H"); e=0; break;
598 case 17: tokens.push_back("z"); e=0; break;//loop
599 case 18: if(a=='-'||a=='.'|| isdigit (a)){
600 buffer.append(path.substr(i,1));i++;
605 case 19: tokens.push_back(","); e=0; break;
606 case 20: tokens.push_back(buffer);
612 switch(e){//last element
613 case 1 : tokens.push_back("m"); break;
614 case 2 : tokens.push_back("c"); break;
615 case 3 : tokens.push_back("q"); break;
616 case 4 : tokens.push_back("t"); break;
617 case 5 : tokens.push_back("a"); break;
618 case 6 : tokens.push_back("l"); break;
619 case 7 : tokens.push_back("v"); break;
620 case 8 : tokens.push_back("h"); break;
621 case 9 : tokens.push_back("M"); break;
622 case 10: tokens.push_back("C"); break;
623 case 11: tokens.push_back("Q"); break;
624 case 12: tokens.push_back("T"); break;
625 case 13: tokens.push_back("A"); break;
626 case 14: tokens.push_back("L"); break;
627 case 15: tokens.push_back("V"); break;
628 case 16: tokens.push_back("H"); break;
629 case 17: tokens.push_back("z"); break;
630 case 18: tokens.push_back(buffer); break;
631 case 19: tokens.push_back(","); break;
632 case 20: tokens.push_back(buffer); break;
638 std::list<std::list<Vertice*> >
639 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
640 std::list<std::list<Vertice*> > k;
641 std::list<Vertice*> k1;
642 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
643 float actual_x=0,actual_y=0;//for relative methods;
646 std::vector<String> tokens=get_tokens_path(path_d);
647 for(i=0;i<tokens.size();i++){
648 if(tokens.at(i).compare("M")==0){//absolute move to
653 i++; ax=atof(tokens.at(i).data());
654 i++; if(tokens.at(i).compare(",")==0) i++;
655 ay=atof(tokens.at(i).data());
659 if(mtx) transformPoint2D(mtx,&ax,&ay);
661 k1.push_back(newVertice (ax,ay)); //first element
662 setSplit(k1.back(),TRUE);
663 }else if(tokens.at(i).compare("C")==0){ //absolute curve
665 i++; tgx2=atof(tokens.at(i).data());
666 i++; if(tokens.at(i).compare(",")==0) i++;
667 tgy2=atof(tokens.at(i).data());
669 i++; tgx=atof(tokens.at(i).data());
670 i++; if(tokens.at(i).compare(",")==0) i++;
671 tgy=atof(tokens.at(i).data());
673 i++; ax=atof(tokens.at(i).data());
674 i++; if(tokens.at(i).compare(",")==0) i++;
675 ay=atof(tokens.at(i).data());
680 transformPoint2D(mtx,&tgx2,&tgy2);
681 transformPoint2D(mtx,&ax,&ay);
682 transformPoint2D(mtx,&tgx,&tgy);
685 coor2vect(&tgx2,&tgy2);
687 coor2vect(&tgx,&tgy);
689 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
690 if(isFirst(k1.front(),ax,ay)){
691 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
693 k1.push_back(newVertice (ax,ay));
694 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
695 setSplit(k1.back(),TRUE);
697 }else if(tokens.at(i).compare("Q")==0){ //absolute quadractic curve
699 i++; tgx=ax=atof(tokens.at(i).data());
700 i++; if(tokens.at(i).compare(",")==0) i++;
701 tgy=ay=atof(tokens.at(i).data());
703 i++; ax=atof(tokens.at(i).data());
704 i++; if(tokens.at(i).compare(",")==0) i++;
705 ay=atof(tokens.at(i).data());
710 transformPoint2D(mtx,&ax,&ay);
711 transformPoint2D(mtx,&tgx,&tgy);
715 coor2vect(&tgx,&tgy);
717 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
718 setSplit(k1.back(),FALSE);
719 k1.push_back(newVertice (ax,ay));
720 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
721 }else if(tokens.at(i).compare("L")==0){ //absolute line to
723 i++; ax=atof(tokens.at(i).data());
724 i++; if(tokens.at(i).compare(",")==0) i++;
725 ay=atof(tokens.at(i).data());
729 if(mtx) transformPoint2D(mtx,&ax,&ay);
733 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
734 if(isFirst(k1.front(),ax,ay)){
735 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
737 k1.push_back(newVertice(ax,ay));
738 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
740 }else if(tokens.at(i).compare("l")==0){//relative line to
742 i++; ax=atof(tokens.at(i).data());
743 i++; if(tokens.at(i).compare(",")==0) i++;
744 ay=atof(tokens.at(i).data());
751 if(mtx) transformPoint2D(mtx,&ax,&ay);
755 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
756 if(isFirst(k1.front(),ax,ay)){
757 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
759 k1.push_back(newVertice(ax,ay));
760 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
762 }else if(tokens.at(i).compare("H")==0){//absolute horizontal move
763 //the same that L but only Horizontal movement
765 i++; ax=atof(tokens.at(i).data());
770 if(mtx) transformPoint2D(mtx,&ax,&ay);
774 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
775 if(isFirst(k1.front(),ax,ay)){
776 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
778 k1.push_back(newVertice(ax,ay));
779 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
781 }else if(tokens.at(i).compare("h")==0){//horizontal relative
782 i++; ax=atof(tokens.at(i).data());
788 if(mtx) transformPoint2D(mtx,&ax,&ay);
792 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
793 if(isFirst(k1.front(),ax,ay)){
794 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
796 k1.push_back(newVertice(ax,ay));
797 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
799 }else if(tokens.at(i).compare("V")==0){//vertical absolute
801 i++; ay=atof(tokens.at(i).data());
806 if(mtx) transformPoint2D(mtx,&ax,&ay);
810 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
811 if(isFirst(k1.front(),ax,ay)){
812 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
814 k1.push_back(newVertice(ax,ay));
815 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
817 }else if(tokens.at(i).compare("v")==0){//relative
819 i++; ay=atof(tokens.at(i).data());
825 if(mtx) transformPoint2D(mtx,&ax,&ay);
829 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
830 if(isFirst(k1.front(),ax,ay)){
831 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
833 k1.push_back(newVertice(ax,ay));
834 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
836 }else if(tokens.at(i).compare("T")==0){// I don't know what does it
837 }else if(tokens.at(i).compare("A")==0){//elliptic arc
839 //isn't complete support, is only for circles
841 //this curve have 6 parameters
843 float radio_x,radio_y;
847 i++; radio_x=atof(tokens.at(i).data());
848 i++; if(tokens.at(i).compare(",")==0) i++;
849 radio_y=atof(tokens.at(i).data());
851 i++; angle=atof(tokens.at(i).data());
853 i++; large=atoi(tokens.at(i).data());
854 i++; sweep=atoi(tokens.at(i).data());
856 i++; ax=atof(tokens.at(i).data());
857 i++; if(tokens.at(i).compare(",")==0) i++;
858 ay=atof(tokens.at(i).data());
860 if(!large && !sweep){
862 tgx2 = actual_x + radio_x*0.5;
865 tgy = ay + radio_y*0.5;
870 transformPoint2D(mtx,&tgx2,&tgy2);
871 transformPoint2D(mtx,&ax,&ay);
872 transformPoint2D(mtx,&tgx,&tgy);
875 coor2vect(&tgx2,&tgy2);
877 coor2vect(&tgx,&tgy);
879 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
880 if(isFirst(k1.front(),ax,ay)){
881 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
883 k1.push_back(newVertice (ax,ay));
884 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
885 setSplit(k1.back(),TRUE);
887 }else if(!large && sweep){
890 tgy2 = actual_y + radio_y*0.5;
891 tgx = ax + radio_x*0.5;
897 transformPoint2D(mtx,&tgx2,&tgy2);
898 transformPoint2D(mtx,&ax,&ay);
899 transformPoint2D(mtx,&tgx,&tgy);
902 coor2vect(&tgx2,&tgy2);
904 coor2vect(&tgx,&tgy);
906 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
907 if(isFirst(k1.front(),ax,ay)){
908 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
910 k1.push_back(newVertice (ax,ay));
911 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
912 setSplit(k1.back(),TRUE);
914 }else if( large && !sweep){//rare
915 //this need more than one vertex
916 }else if( large && sweep){//circles in inkscape are made with this kind of arc
917 if(actual_y==ay){//circles
920 if(actual_x>ax) sense =-1;
921 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
922 in_x = (actual_x+ax)/2;
923 in_y = actual_y - sense*radio_y;
924 in_tgx1 = in_x - sense*(radio_x*0.5);
925 in_tgx2 = in_x + sense*(radio_x*0.5);
930 tgy2=ay - sense*(radio_y*0.5);
932 tgy =ay - sense*(radio_y*0.5);
938 transformPoint2D(mtx,&tgx2,&tgy2);
939 transformPoint2D(mtx,&tgx ,&tgy );
940 transformPoint2D(mtx,&ax,&ay);
942 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
943 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
944 transformPoint2D(mtx,&in_x,&in_y);
947 coor2vect(&tgx2 , &tgy2);
948 coor2vect(&ax , &ay );
949 coor2vect(&tgx , &tgy );
951 coor2vect(&in_tgx2 , &in_tgy2);
952 coor2vect(&in_tgx1 , &in_tgy1);
953 coor2vect(&in_x , &in_y );
956 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
957 //save the intermediate point
958 k1.push_back(newVertice (in_x,in_y));
959 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
960 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
961 setSplit(k1.back(),TRUE); //this could be changed
963 if(isFirst(k1.front(),ax,ay)){
964 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
966 k1.push_back(newVertice (ax,ay));
967 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
968 setSplit(k1.back(),TRUE);
972 }else if(tokens.at(i).compare("z")==0){
975 std::cout<<"don't supported: "<<tokens.at(i)<<std::endl;
979 k.push_front(k1); //last element
984 Svg_parser::parser_defs(const xmlpp::Node* node){
985 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
987 xmlpp::Node::NodeList list = node->get_children();
988 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
989 Glib::ustring name =(*iter)->get_name();
990 if(name.compare("linearGradient")==0){
991 parser_linearGradient(*iter);
992 }else if(name.compare("radialGradient")==0){
993 parser_radialGradient(*iter);
999 Svg_parser::AdjustPointUrl(){
1001 if(!lg.empty()){//linealgradient
1002 std::list<LinearGradient*>::iterator aux=lg.begin();
1003 while(aux!=lg.end()){
1004 LinearGradient* auxlg=*aux;
1005 coor2vect (&auxlg->x1,&auxlg->y1);
1006 coor2vect (&auxlg->x2,&auxlg->y2);
1010 if(!rg.empty()){//radialgradient
1011 std::list<RadialGradient*>::iterator aux=rg.begin();
1012 while(aux!=rg.end()){
1013 RadialGradient* auxrg=*aux;
1014 coor2vect (&auxrg->cx,&auxrg->cy);
1015 auxrg->r= auxrg->r/kux;
1021 std::list<ColorStop*>*
1022 Svg_parser::find_colorStop(String name){
1024 if(lg.empty()&& rg.empty())
1028 if(find.at(0)=='#') find.erase(0,1);
1030 std::list<LinearGradient*>::iterator aux=lg.begin();
1031 while(aux!=lg.end()){//only find into linear gradients
1032 if(find.compare((*aux)->name)==0){
1033 return (*aux)->stops;
1041 Svg_parser::build_url(xmlpp::Element* root, String name,Matrix *mtx){
1043 if(lg.empty()&& rg.empty())
1044 root->get_parent()->remove_child(root);
1046 int start=name.find_first_of("#")+1;
1047 int end=name.find_first_of(")");
1048 String find= name.substr(start,end-start);
1049 bool encounter=false;
1051 std::list<LinearGradient*>::iterator aux=lg.begin();
1052 while(aux!=lg.end()){
1053 if(find.compare((*aux)->name)==0){
1054 build_linearGradient (root,*aux,mtx);
1060 if(!encounter && !rg.empty()){
1061 std::list<RadialGradient*>::iterator aux=rg.begin();
1062 while(aux!=rg.end()){
1063 if(find.compare((*aux)->name)==0){
1064 build_radialGradient (root,*aux,mtx);
1071 root->get_parent()->remove_child(root);
1073 root->get_parent()->remove_child(root);
1077 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
1078 std::list<ColorStop*>::iterator aux_stop=stops->begin();
1079 while(aux_stop!=stops->end()){
1080 xmlpp::Element *child=root->add_child("color");
1081 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
1082 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
1083 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
1084 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
1085 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
1090 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
1092 root->set_attribute("type","linear_gradient");
1093 root->set_attribute("active","true");
1094 root->set_attribute("desc","Gradient004");
1095 build_param (root->add_child("param"),"z_depth","real","0");
1096 build_param (root->add_child("param"),"amount","real","1");
1098 build_param (root->add_child("param"),"blend_method","integer","21");
1105 transformPoint2D(mtx,&x1,&y1);
1106 transformPoint2D(mtx,&x2,&y2);
1108 coor2vect (&x1,&y1);
1109 coor2vect (&x2,&y2);
1111 build_vector (root->add_child("param"),"p1",x1,y1);
1112 build_vector (root->add_child("param"),"p2",x2,y2);
1114 xmlpp::Element *child=root->add_child("param");
1115 child->set_attribute("name","gradient");
1116 build_stop_color (child->add_child("gradient"),data->stops);
1117 build_param (root->add_child("param"),"loop","bool","false");
1118 build_param (root->add_child("param"),"zigzag","bool","false");
1122 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1125 root->set_attribute("type","radial_gradient");
1126 root->set_attribute("active","true");
1127 build_param (root->add_child("param"),"z_depth","real","0");
1128 build_param (root->add_child("param"),"amount","real","1");
1130 build_param (root->add_child("param"),"blend_method","integer","21");
1132 xmlpp::Element *child=root->add_child("param");
1133 child->set_attribute("name","gradient");
1134 build_stop_color (child->add_child("gradient"),data->stops);
1135 //here the center point and radio
1141 transformPoint2D(mtx,&cx,&cy);
1144 coor2vect (&cx,&cy);
1146 build_vector (root->add_child("param"),"center",cx,cy);
1147 build_param (root->add_child("param"),"radius","real",r);
1149 build_param (root->add_child("param"),"loop","bool","false");
1150 build_param (root->add_child("param"),"zigzag","bool","false");
1155 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1156 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1157 Glib::ustring id =nodeElement->get_attribute_value("id");
1158 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1159 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1160 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1161 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1162 Glib::ustring link =nodeElement->get_attribute_value("href");
1164 std::list<ColorStop*> *stops;
1166 stops=find_colorStop (link);
1169 stops=new std::list<ColorStop*>();
1170 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1172 xmlpp::Node::NodeList list = node->get_children();
1173 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1174 Glib::ustring name =(*iter)->get_name();
1175 if(name.compare("stop")==0){
1176 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1177 Glib::ustring style =nodeIter->get_attribute_value("style");
1178 float offset=atof(nodeIter->get_attribute_value("offset").data());
1182 extractSubAttribute (style,"stop-color",&stop_color);
1183 extractSubAttribute (style,"stop-opacity",&opacity);
1185 if(opacity.empty()) opacity="1";
1186 if(stop_color.empty()) stop_color="#000000";//black for default :S
1187 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1193 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops));
1198 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1199 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1200 Glib::ustring id =nodeElement->get_attribute_value("id");
1201 float cx =atof(nodeElement->get_attribute_value("cx").data());
1202 float cy =atof(nodeElement->get_attribute_value("cy").data());
1203 float r =atof(nodeElement->get_attribute_value("r").data());
1204 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1205 std::list<ColorStop*> *stops=NULL;
1207 //inkscape always use link, i dont need parser stops here, but it's posible
1208 stops=find_colorStop (link);
1211 rg.push_back(newRadialGradient(id,cx,cy,r,stops));
1216 Svg_parser::newColorStop(String color,float opacity,float pos){
1218 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1219 float r=getRed(color);
1220 float g=getGreen(color);
1221 float b=getBlue(color);
1223 Color ret=adjustGamma(r/255,g/255,b/255,a);
1224 _stop->r=ret.get_r();
1225 _stop->g=ret.get_g();
1226 _stop->b=ret.get_b();
1227 _stop->a=ret.get_a();
1232 Svg_parser::adjustGamma(float r,float g,float b,float a){
1234 if(gamma.get_gamma_r()!=1.0){
1236 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1238 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1240 if(gamma.get_gamma_g()!=1.0){
1242 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1244 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1246 if(gamma.get_gamma_b()!=1.0){
1248 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1250 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1256 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops){
1257 LinearGradient* data;
1258 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1259 sprintf(data->name,"%s",name.data());
1269 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops){
1270 RadialGradient* data;
1271 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1272 sprintf(data->name,"%s",name.data());
1282 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1283 root->set_attribute("type","colorcorrect");
1284 root->set_attribute("active","true");
1285 root->set_attribute("version","0.1");
1286 root->set_attribute("desc","Gamma");
1287 build_real (root->add_child("param"),"gamma",gamma);
1290 Svg_parser::build_transform(const String transform){
1292 String tf(transform);
1294 std::vector<String> tokens=tokenize(tf," ");
1295 std::vector<String>::iterator aux=tokens.begin();
1296 while(aux!=tokens.end()){
1297 if((*aux).compare(0,9,"translate")==0){
1300 start =(*aux).find_first_of("(")+1;
1301 end =(*aux).find_first_of(",");
1302 dx =atof((*aux).substr(start,end-start).data());
1303 start =(*aux).find_first_of(",")+1;
1304 end =(*aux).size()-1;
1305 dy =atof((*aux).substr(start,end-start).data());
1307 a=newMatrix(1,0,0,1,dx,dy);
1309 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1310 }else if((*aux).compare(0,5,"scale")==0){
1312 a=newMatrix(1,0,0,1,0,0);
1313 }else if((*aux).compare(0,6,"rotate")==0){
1314 float angle,seno,coseno;
1316 start =(*aux).find_first_of("(")+1;
1317 end =(*aux).size()-1;
1318 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1322 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1324 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1325 }else if((*aux).compare(0,6,"matrix")==0){
1326 int start =(*aux).find_first_of('(')+1;
1327 int end =(*aux).find_first_of(')');
1329 a=newMatrix((*aux).substr(start,end-start));
1331 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1333 a=newMatrix(1,0,0,1,0,0);
1341 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1342 root->set_attribute("type","translate");
1343 root->set_attribute("active","true");
1344 root->set_attribute("version","0.1");
1345 build_vector (root->add_child("param"),"origin",dx,dy);
1348 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1349 root->set_attribute("type","rotate");
1350 root->set_attribute("active","true");
1351 root->set_attribute("version","0.1");
1352 build_vector (root->add_child("param"),"origin",dx,dy);
1353 build_real (root->add_child("param"),"amount",angle);
1356 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1357 root->set_attribute("name","vector_list");
1358 xmlpp::Element *child=root->add_child("dynamic_list");
1359 child->set_attribute("type","vector");
1360 std::list<Vertice*>::iterator aux = p.begin();
1361 while(aux!=p.end()){
1362 xmlpp::Element *child_entry=child->add_child("entry");
1363 xmlpp::Element *child_vector=child_entry->add_child("vector");
1364 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1365 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1370 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1371 xmlpp::Element *child_comp=root->add_child("composite");
1372 child_comp->set_attribute("type","bline_point");
1373 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1374 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1375 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1376 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1377 else build_param (child_comp->add_child("split"),"","bool","false");
1379 xmlpp::Element *child_t1=child_comp->add_child("t1");
1380 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1381 child_rc->set_attribute("type","vector");
1382 build_param (child_rc->add_child("radius"),"","real",p->radio1);
1383 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1385 xmlpp::Element *child_t2=child_comp->add_child("t2");
1386 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1387 child_rc2->set_attribute("type","vector");
1388 build_param (child_rc2->add_child("radius"),"","real",p->radio2);
1389 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1393 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1394 root->set_attribute("name","bline");
1395 xmlpp::Element *child=root->add_child("bline");
1396 child->set_attribute("type","bline_point");
1398 child->set_attribute("loop","true");
1400 child->set_attribute("loop","false");
1401 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1402 std::list<Vertice*>::iterator aux = p.begin();
1403 while(aux!=p.end()){
1404 if(*aux) build_vertice (child->add_child("entry"),*aux);
1410 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1411 if(!type.empty() && !value.empty()){
1412 if(!name.empty()) root->set_attribute("name",name);
1413 xmlpp::Element *child=root->add_child(type);
1414 child->set_attribute("value",value);
1416 root->get_parent()->remove_child(root);
1420 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1422 if(!name.empty()) root->set_attribute("name",name);
1423 xmlpp::Element *child=root->add_child(type);
1424 child->set_attribute("value",etl::strprintf ("%f",value));
1426 root->get_parent()->remove_child(root);
1430 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1432 if(!name.empty()) root->set_attribute("name",name);
1433 xmlpp::Element *child=root->add_child(type);
1434 char *enteroc=new char[10];
1435 sprintf(enteroc,"%d",value);
1436 child->set_attribute("value",enteroc);
1439 root->get_parent()->remove_child(root);
1444 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1445 if(name.compare("")!=0) root->set_attribute("name",name);
1446 xmlpp::Element *child=root->add_child("integer");
1447 char *enteroc=new char[10];
1448 sprintf(enteroc,"%d",value);
1449 child->set_attribute("value",enteroc);
1452 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1453 if(name.compare("")!=0) root->set_attribute("name",name);
1454 xmlpp::Element *child=root->add_child("real");
1455 char *realc=new char[20];
1456 sprintf(realc,"%f",value);
1457 child->set_attribute("value",realc);
1461 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1462 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1463 root->get_parent()->remove_child(root);
1464 printf("Color aborted\n");
1467 Color ret=adjustGamma(r/255,g/255,b/255,a);
1469 root->set_attribute("name","color");
1470 xmlpp::Element *child=root->add_child("color");
1471 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1472 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1473 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1474 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1477 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1479 if(name.compare("")!=0) root->set_attribute("name",name);
1480 xmlpp::Element *child=root->add_child("vector");
1481 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1482 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1486 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1487 if(name.compare("")!=0) root->set_attribute("name",name);
1488 xmlpp::Element *child=root->add_child("vector");
1489 if(!guid.empty()) child->set_attribute("guid",guid);
1490 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1491 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1495 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1496 root->set_attribute("type","PasteCanvas");
1497 root->set_attribute("active","true");
1498 root->set_attribute("version","0.1");
1499 root->set_attribute("desc","Composite");
1500 build_param (root->add_child("param"),"z_depth","real","0");
1501 build_param (root->add_child("param"),"amount","real","1");
1502 build_param (root->add_child("param"),"blend_method","integer","0");
1503 build_vector (root->add_child("param"),"origin",0,0);
1504 xmlpp::Element *child=root->add_child("param");
1505 child->set_attribute("name","canvas");
1506 return child->add_child("canvas");
1511 Svg_parser::coor2vect(float *x,float *y){
1515 sy= atof(height.c_str())-sy;
1524 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1526 float d1x,d1y,d2x,d2y,dx,dy;
1537 rd=sqrt(dx*dx + dy*dy);
1539 ag=PI + atan(dy/dx);
1540 }else if(dx>0 && dy<0){
1541 ag=PI + atan(dy/dx);
1542 }else if(dx<0 && dy<0){
1544 }else if(dx<0 && dy>0){
1545 ag= 2*PI+atan(dy/dx);
1546 }else if(dx==0 && dy>0){
1548 }else if(dx==0 && dy<0){
1550 }else if(dx==0 && dy==0){
1552 }else if(dx<0 && dy==0){
1554 }else if(dx>0 && dy==0){
1562 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1564 float d1x,d1y,d2x,d2y,dx,dy;
1576 rd=sqrt(dx*dx + dy*dy);
1578 ag=PI + atan(dy/dx);
1579 // printf("case 180-270\n");
1580 }else if(dx>0 && dy<0){
1581 ag=PI + atan(dy/dx);
1582 // printf("case 90-180\n");
1583 }else if(dx<0 && dy<0){
1585 // printf("case 0-90\n");
1586 }else if(dx<0 && dy>0){
1587 ag= 2*PI+atan(dy/dx);
1588 // printf("case 270-360\n");
1589 }else if(dx==0 && dy>0){
1591 }else if(dx==0 && dy<0){
1593 }else if(dx==0 && dy==0){
1595 }else if(dx<0 && dy==0){
1597 }else if(dx>0 && dy==0){
1607 Svg_parser::setSplit(Vertice* p,bool val){
1613 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1614 if(nodo->x==a && nodo->y==b)
1620 Svg_parser::newVertice(float x,float y){
1622 vert=(Vertice*)malloc(sizeof(Vertice));
1625 vert->radio1=vert->radio2=vert->angle1=vert->angle2=0;
1630 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1632 if(!attribute.empty()){
1633 String str(attribute);
1635 std::vector<String> tokens=tokenize(str,";");
1636 std::vector<String>::iterator aux=tokens.begin();
1637 while(aux!=tokens.end()){
1638 int mid= (*aux).find_first_of(":");
1639 if((*aux).substr(0,mid).compare(name)==0){
1640 int end=(*aux).size();
1641 *value=(*aux).substr(mid+1,end-mid);
1650 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1653 if(!path_style.empty())
1654 fnd=extractSubAttribute(path_style,name,&value);
1656 if(!master_style.empty())
1657 fnd=extractSubAttribute(master_style,name,&value);
1664 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1667 if(!path_style.empty())
1668 fnd=extractSubAttribute(path_style,name,&value);
1669 if(fnd==0 && !master_style.empty())
1670 fnd=extractSubAttribute(master_style,name,&value);
1672 if(!subattribute.empty())
1681 Svg_parser::randomLetter(){
1683 if(a) return (49 + rand()%9);
1684 else return (65 + rand()%24);
1688 Svg_parser::getRed(String hex){
1690 return hextodec(hex.substr(1,2));
1691 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1692 int start=hex.find_first_of("(")+1;
1693 int end =hex.find_last_of(")");
1694 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1695 return atoi(aux.data());
1700 Svg_parser::getGreen(String hex){
1702 return hextodec(hex.substr(3,2));
1703 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1704 int start=hex.find_first_of("(")+1;
1705 int end =hex.find_last_of(")");
1706 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1707 return atoi(aux.data());
1712 Svg_parser::getBlue(String hex){
1714 return hextodec(hex.substr(5,2));
1715 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1716 int start=hex.find_first_of("(")+1;
1717 int end =hex.find_last_of(")");
1718 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1719 return atoi(aux.data());
1724 Svg_parser::hextodec(String hex){
1733 else if(hex.at(i)=='1')
1735 else if(hex.at(i)=='2')
1737 else if(hex.at(i)=='3')
1739 else if(hex.at(i)=='4')
1741 else if(hex.at(i)=='5')
1743 else if(hex.at(i)=='6')
1745 else if(hex.at(i)=='7')
1747 else if(hex.at(i)=='8')
1749 else if(hex.at(i)=='9')
1751 else if(hex.at(i)=='a')
1753 else if(hex.at(i)=='b')
1755 else if(hex.at(i)=='c')
1757 else if(hex.at(i)=='d')
1759 else if(hex.at(i)=='e')
1761 else if(hex.at(i)=='f')
1769 result+=pow(16,i)*ihex[top-i-1];
1777 Svg_parser::getDimension(const String ac){
1781 int length=ac.size();
1783 if(isdigit(ac.at(length-1))){
1785 }else if(ac.at(length-1)=='%'){
1788 String mtc=ac.substr(length-2,length);
1789 String nmc=ac.substr(0,length-2);
1790 if(mtc.compare("px")==0){
1791 af=atof(nmc.data());
1792 }else if(mtc.compare("pt")==0){
1793 af=atof(nmc.data())*1.25;
1794 }else if(mtc.compare("em")==0){
1795 af=atof(nmc.data())*16;
1796 }else if(mtc.compare("mm")==0){
1797 af=atof(nmc.data())*3.54;
1798 }else if(mtc.compare("pc")==0){
1799 af=atof(nmc.data())*15;
1800 }else if(mtc.compare("cm")==0){
1801 af=atof(nmc.data())*35.43;
1802 }else if(mtc.compare("in")==0){
1803 af=atof(nmc.data())*90;
1812 Svg_parser::newMatrix(Matrix *a){
1814 data=(Matrix*)malloc(sizeof(Matrix));
1815 data->a=a->a; data->b=a->b; data->c=a->c;
1816 data->d=a->d; data->e=a->e; data->f=a->f;
1820 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1822 data=(Matrix*)malloc(sizeof(Matrix));
1823 data->a=a; data->b=b; data->c=c;
1824 data->d=d; data->e=e; data->f=f;
1828 Svg_parser::newMatrix(const String mvector){
1829 if(!mvector.empty()){
1830 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1831 std::vector<String> tokens=tokenize(mvector,",");
1832 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1833 data->a=atof(tokens.at(0).data());
1834 data->b=atof(tokens.at(1).data());
1835 data->c=atof(tokens.at(2).data());
1836 data->d=atof(tokens.at(3).data());
1837 data->e=atof(tokens.at(4).data());
1838 data->f=atof(tokens.at(5).data());
1841 return newMatrix(1,0,0,1,0,0);
1845 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1849 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1850 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1856 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1857 Matrix* aux=newMatrix(0,0,0,0,0,0);
1858 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1859 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1860 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1861 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1862 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1863 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1867 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1868 Matrix* aux=newMatrix(0,0,0,0,0,0);
1869 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1870 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1871 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1872 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1873 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1874 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1883 Svg_parser::matrixIsNull(Matrix *mtx){
1884 if(mtx == NULL) return true;
1889 Svg_parser::getRadian(float sexa){
1890 return (sexa*2*PI)/360;
1893 Svg_parser::removeS(String *input){
1894 for(unsigned int i=0;i<input->size();i++){
1895 if(input->at(i)==' '){
1901 Svg_parser::removeIntoS(String *input){
1903 for(unsigned int i=0;i<input->size();i++){
1904 if(input->at(i)=='('){
1906 }else if(input->at(i)==')'){
1908 }else if(into && input->at(i)==' '){
1914 Svg_parser::tokenize(const String& str,const String& delimiters){
1915 std::vector<String> tokens;
1916 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1917 String::size_type pos = str.find_first_of(delimiters, lastPos);
1918 while (String::npos != pos || String::npos != lastPos){
1919 tokens.push_back(str.substr(lastPos, pos - lastPos));
1920 lastPos = str.find_first_not_of(delimiters, pos);
1921 pos = str.find_first_of(delimiters, lastPos);
1926 Svg_parser::new_guid(){
1928 return GUID::hasher(uid).get_string();