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){
138 if(set_canvas==0) parser_canvas (node);
139 parser_graphics(node,nodeRoot,"",NULL);
140 if(nodename.compare("g")==0) return;
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
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","");
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","");
166 if(height.compare("")==0){
167 height=nodeElement->get_attribute_value("height","");
169 if(width.compare("")==0 && height.compare("")!=0){
172 if(width.compare("")!=0 && height.compare("")==0){
175 if(height.compare("")==0 && width.compare("")==0){
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");
188 view_x=atof(width.c_str())/kux;
189 view_y=atof(height.c_str())/kux;
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");
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
215 // 2: when necessary, including for linking
217 #define SVG_SEP_TRANSFORMS 1
218 #define SVG_RESOLVE_BLINE 1
219 #define SVG_CONVERT_PATHS 1
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");
230 //resolve transformations
232 if(!transform.empty())
233 mtx=build_transform (transform);
234 if (SVG_SEP_TRANSFORMS)
238 composeMatrix(&mtx,mtx_parent,mtx);
240 mtx=newMatrix(mtx_parent);
243 if(nodename.compare("g")==0){
244 parser_layer (node,root->add_child("layer"),parent_style,mtx);
248 Glib::ustring obj_style =nodeElement->get_attribute_value("style");
249 Glib::ustring obj_fill =nodeElement->get_attribute_value("fill");
252 String fill =loadAttribute("fill",obj_style,parent_style,obj_fill,"none");
253 String fill_rule =loadAttribute("fill-rule",obj_style,parent_style,"evenodd");
254 String stroke =loadAttribute("stroke",obj_style,parent_style,"none");
255 String stroke_width =loadAttribute("stroke-width",obj_style,parent_style,"1px");
256 String stroke_linecap =loadAttribute("stroke-linecap",obj_style,parent_style,"butt");
257 String stroke_linejoin =loadAttribute("stroke-linejoin",obj_style,parent_style,"miter");
258 String stroke_opacity =loadAttribute("stroke-opacity",obj_style,parent_style,"1");
259 String fill_opacity =loadAttribute("fill-opacity",obj_style,parent_style,"1");
260 String opacity =loadAttribute("opacity",obj_style,parent_style,"1");
264 int typeFill=0; //nothing
266 if(fill.compare("none")!=0){
269 if(typeFill==1 && fill.compare(0,3,"url")==0){
270 typeFill=2; //gradient
273 int typeStroke=0;//nothing
275 if(stroke.compare("none")!=0){
276 typeStroke=1; //simple
278 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
279 typeStroke=2; //gradient
282 xmlpp::Element* child_fill = root; //TODO: child_filter
283 xmlpp::Element* child_stroke = root;
284 if(typeFill!=0 && typeStroke!=0){
285 child_fill=child_stroke=nodeStartBasicLayer(root->add_child("layer"));
288 child_fill=nodeStartBasicLayer(child_fill->add_child("layer"));
291 child_stroke=nodeStartBasicLayer(child_stroke->add_child("layer"));
293 if ((SVG_CONVERT_PATHS == 1 && typeFill!=0) || (SVG_CONVERT_PATHS == 2 && typeFill!=0 && typeStroke==0)) {
295 if(nodename.compare("rect")==0){
296 rect_simple(nodeElement,child_fill,fill,fill_opacity,opacity);
298 build_url (child_fill->add_child("layer"),fill,mtx);
300 typeFill=0; //set fill to zero so as not to create a fill layer in the paths section
304 //=======================================================================
305 std::list<std::list<Vertice*> > k;
306 if (nodename.compare("path")==0 || nodename.compare("polygon")==0 || typeStroke!=0 || SVG_CONVERT_PATHS==3) {
307 //if we are creating a bline
309 //First, create the list of vertices
310 if(nodename.compare("path")==0){
311 k=parser_path_d (nodeElement->get_attribute_value("d"),mtx);
312 } else if(nodename.compare("polygon")==0){
313 k=parser_polygon_path (nodeElement->get_attribute_value("points"),mtx);
319 for (int i=0;i<n;i++){
320 bline_id[i]=new_guid();
321 offset_id[i]=new_guid();
324 std::list<std::list<Vertice*> >::iterator aux = k.begin();
325 if(typeFill!=0){//region layer
326 for (int i=0; aux!=k.end(); aux++){
327 xmlpp::Element *child_region=child_fill->add_child("layer");
328 child_region->set_attribute("type","region");
329 child_region->set_attribute("active","true");
330 child_region->set_attribute("version","0.1");
331 child_region->set_attribute("desc",id);
332 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
333 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
334 build_param (child_region->add_child("param"),"blend_method","integer","0");
335 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
336 build_vector (child_region->add_child("param"),"offset",0,0,offset_id[i]);
337 build_param (child_region->add_child("param"),"invert","bool","false");
338 build_param (child_region->add_child("param"),"antialias","bool","true");
339 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
340 build_param (child_region->add_child("param"),"blurtype","integer","1");
341 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
342 else build_param (child_region->add_child("param"),"winding_style","integer","0");
344 build_bline (child_region->add_child("param"),*aux,loop,bline_id[i]);
348 if(typeFill==2){ //gradient in onto mode (fill)
349 build_url(child_fill->add_child("layer"),fill,mtx);
352 if(typeStroke!=0){//outline layer
354 for (aux=k.begin(); aux!=k.end(); aux++){
355 xmlpp::Element *child_outline=child_stroke->add_child("layer");
356 child_outline->set_attribute("type","outline");
357 child_outline->set_attribute("active","true");
358 child_outline->set_attribute("version","0.2");
359 child_outline->set_attribute("desc",id);
360 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
361 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
362 build_param (child_outline->add_child("param"),"blend_method","integer","0");
363 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
364 build_vector (child_outline->add_child("param"),"offset",0,0,offset_id[i]);
365 build_param (child_outline->add_child("param"),"invert","bool","false");
366 build_param (child_outline->add_child("param"),"antialias","bool","true");
367 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
368 build_param (child_outline->add_child("param"),"blurtype","integer","1");
370 build_param (child_outline->add_child("param"),"winding_style","integer","0");
372 build_bline (child_outline->add_child("param"),*aux,loop,bline_id[i]);
374 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
375 build_param (child_outline->add_child("param"),"width","real",stroke_width);
376 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
377 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
378 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
379 if(stroke_linecap.compare("butt")==0){
380 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
381 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
383 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
384 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
386 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
387 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
393 if(typeStroke==2){ //gradient in onto mode (stroke)
394 build_url(child_stroke->add_child("layer"),stroke,mtx);
403 Svg_parser::rect_simple(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
404 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
405 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
406 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
407 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
408 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
410 xmlpp::Element *child_rect=root->add_child("layer");
411 child_rect->set_attribute("type","rectangle");
412 child_rect->set_attribute("active","true");
413 child_rect->set_attribute("version","0.2");
414 child_rect->set_attribute("desc",rect_id);
416 build_real(child_rect->add_child("param"),"z_depth",0.0);
417 build_real(child_rect->add_child("param"),"amount",1.0);
418 build_integer(child_rect->add_child("param"),"blend_method",0);
419 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
421 float auxx=atof(rect_x.c_str());
422 float auxy=atof(rect_y.c_str());
423 coor2vect(&auxx,&auxy);
424 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
425 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
426 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
427 coor2vect(&auxx,&auxy);
428 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
434 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
435 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
436 Glib::ustring label =nodeElement->get_attribute_value("label");
437 Glib::ustring style =nodeElement->get_attribute_value("style");
438 Glib::ustring fill =nodeElement->get_attribute_value("fill");
443 }else if(!fill.empty()){
444 layer_style.append("fill:");
445 layer_style.append(fill);
446 }else if(!parent_style.empty()){
447 layer_style=parent_style;
450 root->set_attribute("type","PasteCanvas");
451 root->set_attribute("active","true");
452 root->set_attribute("version","0.1");
453 if(!label.empty()) root->set_attribute("desc",label);
454 else root->set_attribute("desc","Inline Canvas");
456 build_real(root->add_child("param"),"z_depth",0.0);
457 build_real(root->add_child("param"),"amount",1.0);
458 build_integer(root->add_child("param"),"blend_method",0);
459 build_vector (root->add_child("param"),"origin",0,0);
461 //printf(" canvas attributes ");
463 xmlpp::Element *child_canvas=root->add_child("param");
464 child_canvas->set_attribute("name","canvas");
465 child_canvas=child_canvas->add_child("canvas");
466 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
468 xmlpp::Node::NodeList list = node->get_children();
469 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
470 Glib::ustring name =(*iter)->get_name();
471 parser_graphics (*iter,child_canvas,layer_style,mtx);
477 std::list<std::list<Vertice*> >
478 Svg_parser::parser_polygon_path(Glib::ustring polygon_points, Matrix* mtx){
479 std::list<std::list<Vertice*> > k0;
480 if(polygon_points.empty())
482 std::list<Vertice*> k;
483 std::vector<String> tokens=get_tokens_path (polygon_points);
485 float ax,ay; ax=ay=0;
486 for(i=0;i<tokens.size();i++){
487 ax=atof(tokens.at(i).data());
488 i++; if(tokens.at(i).compare(",")==0) i++;
489 ay=atof(tokens.at(i).data());
491 if(mtx) transformPoint2D(mtx,&ax,&ay);
495 k.push_back(newVertice(ax,ay));
502 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
503 std::vector<String> tokens;
508 while(i<path.size()){
511 case 0: //initial state
512 if(a=='m'){ e=1; i++;}
513 else if(a=='c'){ e= 2; i++;}
514 else if(a=='q'){ e= 3; i++;}
515 else if(a=='t'){ e= 4; i++;}
516 else if(a=='a'){ e= 5; i++;}
517 else if(a=='l'){ e= 6; i++;}
518 else if(a=='v'){ e= 7; i++;}
519 else if(a=='h'){ e= 8; i++;}
520 else if(a=='M'){ e= 9; i++;}
521 else if(a=='C'){ e=10; i++;}
522 else if(a=='Q'){ e=11; i++;}
523 else if(a=='T'){ e=12; i++;}
524 else if(a=='A'){ e=13; i++;}
525 else if(a=='L'){ e=14; i++;}
526 else if(a=='V'){ e=15; i++;}
527 else if(a=='H'){ e=16; i++;}
528 else if(a=='z' || a=='Z'){ e=17; i++;}
529 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
530 else if(a==','){ e=19; i++;}
531 else if(a==' '){i++;}
534 case 1 : tokens.push_back("m"); e=0; break;//move
535 case 2 : tokens.push_back("c"); e=0; break;//curve
536 case 3 : tokens.push_back("q"); e=0; break;//quadratic
537 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
538 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
539 case 6 : tokens.push_back("l"); e=0; break;//line to
540 case 7 : tokens.push_back("v"); e=0; break;//vertical
541 case 8 : tokens.push_back("h"); e=0; break;//horizontal
543 case 9 : tokens.push_back("M"); e=0; break;
544 case 10: tokens.push_back("C"); e=0; break;
545 case 11: tokens.push_back("Q"); e=0; break;
546 case 12: tokens.push_back("T"); e=0; break;
547 case 13: tokens.push_back("A"); e=0; break;
548 case 14: tokens.push_back("L"); e=0; break;
549 case 15: tokens.push_back("V"); e=0; break;
550 case 16: tokens.push_back("H"); e=0; break;
552 case 17: tokens.push_back("z"); e=0; break;//loop
553 case 18: if(a=='-'||a=='.'|| isdigit (a)){
554 buffer.append(path.substr(i,1));i++;
559 case 19: tokens.push_back(","); e=0; break;
560 case 20: tokens.push_back(buffer);
566 switch(e){//last element
567 case 1 : tokens.push_back("m"); break;
568 case 2 : tokens.push_back("c"); break;
569 case 3 : tokens.push_back("q"); break;
570 case 4 : tokens.push_back("t"); break;
571 case 5 : tokens.push_back("a"); break;
572 case 6 : tokens.push_back("l"); break;
573 case 7 : tokens.push_back("v"); break;
574 case 8 : tokens.push_back("h"); break;
575 case 9 : tokens.push_back("M"); break;
576 case 10: tokens.push_back("C"); break;
577 case 11: tokens.push_back("Q"); break;
578 case 12: tokens.push_back("T"); break;
579 case 13: tokens.push_back("A"); break;
580 case 14: tokens.push_back("L"); break;
581 case 15: tokens.push_back("V"); break;
582 case 16: tokens.push_back("H"); break;
583 case 17: tokens.push_back("z"); break;
584 case 18: tokens.push_back(buffer); break;
585 case 19: tokens.push_back(","); break;
586 case 20: tokens.push_back(buffer); break;
592 std::list<std::list<Vertice*> >
593 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
594 std::list<std::list<Vertice*> > k;
595 std::list<Vertice*> k1;
596 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
597 float actual_x=0,actual_y=0;//for relative methods;
600 std::vector<String> tokens=get_tokens_path(path_d);
601 for(i=0;i<tokens.size();i++){
602 if(tokens.at(i).compare("M")==0){//absolute move to
607 i++; ax=atof(tokens.at(i).data());
608 i++; if(tokens.at(i).compare(",")==0) i++;
609 ay=atof(tokens.at(i).data());
613 if(mtx) transformPoint2D(mtx,&ax,&ay);
615 k1.push_back(newVertice (ax,ay)); //first element
616 setSplit(k1.back(),TRUE);
617 }else if(tokens.at(i).compare("C")==0){ //absolute curve
619 i++; tgx2=atof(tokens.at(i).data());
620 i++; if(tokens.at(i).compare(",")==0) i++;
621 tgy2=atof(tokens.at(i).data());
623 i++; tgx=atof(tokens.at(i).data());
624 i++; if(tokens.at(i).compare(",")==0) i++;
625 tgy=atof(tokens.at(i).data());
627 i++; ax=atof(tokens.at(i).data());
628 i++; if(tokens.at(i).compare(",")==0) i++;
629 ay=atof(tokens.at(i).data());
634 transformPoint2D(mtx,&tgx2,&tgy2);
635 transformPoint2D(mtx,&ax,&ay);
636 transformPoint2D(mtx,&tgx,&tgy);
639 coor2vect(&tgx2,&tgy2);
641 coor2vect(&tgx,&tgy);
643 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
644 if(isFirst(k1.front(),ax,ay)){
645 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
647 k1.push_back(newVertice (ax,ay));
648 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
649 setSplit(k1.back(),TRUE);
651 }else if(tokens.at(i).compare("Q")==0){ //absolute quadractic curve
653 i++; tgx=ax=atof(tokens.at(i).data());
654 i++; if(tokens.at(i).compare(",")==0) i++;
655 tgy=ay=atof(tokens.at(i).data());
657 i++; ax=atof(tokens.at(i).data());
658 i++; if(tokens.at(i).compare(",")==0) i++;
659 ay=atof(tokens.at(i).data());
664 transformPoint2D(mtx,&ax,&ay);
665 transformPoint2D(mtx,&tgx,&tgy);
669 coor2vect(&tgx,&tgy);
671 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
672 setSplit(k1.back(),FALSE);
673 k1.push_back(newVertice (ax,ay));
674 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
675 }else if(tokens.at(i).compare("L")==0){ //absolute line to
677 i++; ax=atof(tokens.at(i).data());
678 i++; if(tokens.at(i).compare(",")==0) i++;
679 ay=atof(tokens.at(i).data());
683 if(mtx) transformPoint2D(mtx,&ax,&ay);
687 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
688 if(isFirst(k1.front(),ax,ay)){
689 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
691 k1.push_back(newVertice(ax,ay));
692 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
694 }else if(tokens.at(i).compare("l")==0){//relative line to
696 i++; ax=atof(tokens.at(i).data());
697 i++; if(tokens.at(i).compare(",")==0) i++;
698 ay=atof(tokens.at(i).data());
705 if(mtx) transformPoint2D(mtx,&ax,&ay);
709 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
710 if(isFirst(k1.front(),ax,ay)){
711 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
713 k1.push_back(newVertice(ax,ay));
714 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
716 }else if(tokens.at(i).compare("H")==0){//absolute horizontal move
717 //the same that L but only Horizontal movement
719 i++; ax=atof(tokens.at(i).data());
724 if(mtx) transformPoint2D(mtx,&ax,&ay);
728 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
729 if(isFirst(k1.front(),ax,ay)){
730 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
732 k1.push_back(newVertice(ax,ay));
733 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
735 }else if(tokens.at(i).compare("h")==0){//horizontal relative
736 i++; ax=atof(tokens.at(i).data());
742 if(mtx) transformPoint2D(mtx,&ax,&ay);
746 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
747 if(isFirst(k1.front(),ax,ay)){
748 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
750 k1.push_back(newVertice(ax,ay));
751 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
753 }else if(tokens.at(i).compare("V")==0){//vertical absolute
755 i++; ay=atof(tokens.at(i).data());
760 if(mtx) transformPoint2D(mtx,&ax,&ay);
764 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
765 if(isFirst(k1.front(),ax,ay)){
766 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
768 k1.push_back(newVertice(ax,ay));
769 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
771 }else if(tokens.at(i).compare("v")==0){//relative
773 i++; ay=atof(tokens.at(i).data());
779 if(mtx) transformPoint2D(mtx,&ax,&ay);
783 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
784 if(isFirst(k1.front(),ax,ay)){
785 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
787 k1.push_back(newVertice(ax,ay));
788 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
790 }else if(tokens.at(i).compare("T")==0){// I don't know what does it
791 }else if(tokens.at(i).compare("A")==0){//elliptic arc
793 //isn't complete support, is only for circles
795 //this curve have 6 parameters
797 float radio_x,radio_y;
801 i++; radio_x=atof(tokens.at(i).data());
802 i++; if(tokens.at(i).compare(",")==0) i++;
803 radio_y=atof(tokens.at(i).data());
805 i++; angle=atof(tokens.at(i).data());
807 i++; large=atoi(tokens.at(i).data());
808 i++; sweep=atoi(tokens.at(i).data());
810 i++; ax=atof(tokens.at(i).data());
811 i++; if(tokens.at(i).compare(",")==0) i++;
812 ay=atof(tokens.at(i).data());
814 if(!large && !sweep){
816 tgx2 = actual_x + radio_x*0.5;
819 tgy = ay + radio_y*0.5;
824 transformPoint2D(mtx,&tgx2,&tgy2);
825 transformPoint2D(mtx,&ax,&ay);
826 transformPoint2D(mtx,&tgx,&tgy);
829 coor2vect(&tgx2,&tgy2);
831 coor2vect(&tgx,&tgy);
833 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
834 if(isFirst(k1.front(),ax,ay)){
835 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
837 k1.push_back(newVertice (ax,ay));
838 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
839 setSplit(k1.back(),TRUE);
841 }else if(!large && sweep){
844 tgy2 = actual_y + radio_y*0.5;
845 tgx = ax + radio_x*0.5;
851 transformPoint2D(mtx,&tgx2,&tgy2);
852 transformPoint2D(mtx,&ax,&ay);
853 transformPoint2D(mtx,&tgx,&tgy);
856 coor2vect(&tgx2,&tgy2);
858 coor2vect(&tgx,&tgy);
860 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
861 if(isFirst(k1.front(),ax,ay)){
862 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
864 k1.push_back(newVertice (ax,ay));
865 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
866 setSplit(k1.back(),TRUE);
868 }else if( large && !sweep){//rare
869 //this need more than one vertex
870 }else if( large && sweep){//circles in inkscape are made with this kind of arc
871 if(actual_y==ay){//circles
874 if(actual_x>ax) sense =-1;
875 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
876 in_x = (actual_x+ax)/2;
877 in_y = actual_y - sense*radio_y;
878 in_tgx1 = in_x - sense*(radio_x*0.5);
879 in_tgx2 = in_x + sense*(radio_x*0.5);
884 tgy2=ay - sense*(radio_y*0.5);
886 tgy =ay - sense*(radio_y*0.5);
892 transformPoint2D(mtx,&tgx2,&tgy2);
893 transformPoint2D(mtx,&tgx ,&tgy );
894 transformPoint2D(mtx,&ax,&ay);
896 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
897 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
898 transformPoint2D(mtx,&in_x,&in_y);
901 coor2vect(&tgx2 , &tgy2);
902 coor2vect(&ax , &ay );
903 coor2vect(&tgx , &tgy );
905 coor2vect(&in_tgx2 , &in_tgy2);
906 coor2vect(&in_tgx1 , &in_tgy1);
907 coor2vect(&in_x , &in_y );
910 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
911 //save the intermediate point
912 k1.push_back(newVertice (in_x,in_y));
913 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
914 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
915 setSplit(k1.back(),TRUE); //this could be changed
917 if(isFirst(k1.front(),ax,ay)){
918 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
920 k1.push_back(newVertice (ax,ay));
921 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
922 setSplit(k1.back(),TRUE);
926 }else if(tokens.at(i).compare("z")==0){
929 std::cout<<"don't supported: "<<tokens.at(i)<<std::endl;
933 k.push_front(k1); //last element
938 Svg_parser::parser_defs(const xmlpp::Node* node){
939 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
941 xmlpp::Node::NodeList list = node->get_children();
942 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
943 Glib::ustring name =(*iter)->get_name();
944 if(name.compare("linearGradient")==0){
945 parser_linearGradient(*iter);
946 }else if(name.compare("radialGradient")==0){
947 parser_radialGradient(*iter);
953 Svg_parser::AdjustPointUrl(){
955 if(!lg.empty()){//linealgradient
956 std::list<LinearGradient*>::iterator aux=lg.begin();
957 while(aux!=lg.end()){
958 LinearGradient* auxlg=*aux;
959 coor2vect (&auxlg->x1,&auxlg->y1);
960 coor2vect (&auxlg->x2,&auxlg->y2);
964 if(!rg.empty()){//radialgradient
965 std::list<RadialGradient*>::iterator aux=rg.begin();
966 while(aux!=rg.end()){
967 RadialGradient* auxrg=*aux;
968 coor2vect (&auxrg->cx,&auxrg->cy);
969 auxrg->r= auxrg->r/kux;
975 std::list<ColorStop*>*
976 Svg_parser::find_colorStop(String name){
978 if(lg.empty()&& rg.empty())
982 if(find.at(0)=='#') find.erase(0,1);
984 std::list<LinearGradient*>::iterator aux=lg.begin();
985 while(aux!=lg.end()){//only find into linear gradients
986 if(find.compare((*aux)->name)==0){
987 return (*aux)->stops;
995 Svg_parser::build_url(xmlpp::Element* root, String name,Matrix *mtx){
997 if(lg.empty()&& rg.empty())
998 root->get_parent()->remove_child(root);
1000 int start=name.find_first_of("#")+1;
1001 int end=name.find_first_of(")");
1002 String find= name.substr(start,end-start);
1003 bool encounter=false;
1005 std::list<LinearGradient*>::iterator aux=lg.begin();
1006 while(aux!=lg.end()){
1007 if(find.compare((*aux)->name)==0){
1008 build_linearGradient (root,*aux,mtx);
1014 if(!encounter && !rg.empty()){
1015 std::list<RadialGradient*>::iterator aux=rg.begin();
1016 while(aux!=rg.end()){
1017 if(find.compare((*aux)->name)==0){
1018 build_radialGradient (root,*aux,mtx);
1025 root->get_parent()->remove_child(root);
1027 root->get_parent()->remove_child(root);
1031 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
1032 std::list<ColorStop*>::iterator aux_stop=stops->begin();
1033 while(aux_stop!=stops->end()){
1034 xmlpp::Element *child=root->add_child("color");
1035 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
1036 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
1037 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
1038 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
1039 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
1044 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
1046 root->set_attribute("type","linear_gradient");
1047 root->set_attribute("active","true");
1048 root->set_attribute("desc","Gradient004");
1049 build_param (root->add_child("param"),"z_depth","real","0");
1050 build_param (root->add_child("param"),"amount","real","1");
1052 build_param (root->add_child("param"),"blend_method","integer","21");
1059 transformPoint2D(mtx,&x1,&y1);
1060 transformPoint2D(mtx,&x2,&y2);
1062 coor2vect (&x1,&y1);
1063 coor2vect (&x2,&y2);
1065 build_vector (root->add_child("param"),"p1",x1,y1);
1066 build_vector (root->add_child("param"),"p2",x2,y2);
1068 xmlpp::Element *child=root->add_child("param");
1069 child->set_attribute("name","gradient");
1070 build_stop_color (child->add_child("gradient"),data->stops);
1071 build_param (root->add_child("param"),"loop","bool","false");
1072 build_param (root->add_child("param"),"zigzag","bool","false");
1076 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1079 root->set_attribute("type","radial_gradient");
1080 root->set_attribute("active","true");
1081 build_param (root->add_child("param"),"z_depth","real","0");
1082 build_param (root->add_child("param"),"amount","real","1");
1084 build_param (root->add_child("param"),"blend_method","integer","21");
1086 xmlpp::Element *child=root->add_child("param");
1087 child->set_attribute("name","gradient");
1088 build_stop_color (child->add_child("gradient"),data->stops);
1089 //here the center point and radio
1095 transformPoint2D(mtx,&cx,&cy);
1098 coor2vect (&cx,&cy);
1100 build_vector (root->add_child("param"),"center",cx,cy);
1101 build_param (root->add_child("param"),"radius","real",r);
1103 build_param (root->add_child("param"),"loop","bool","false");
1104 build_param (root->add_child("param"),"zigzag","bool","false");
1109 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1110 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1111 Glib::ustring id =nodeElement->get_attribute_value("id");
1112 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1113 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1114 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1115 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1116 Glib::ustring link =nodeElement->get_attribute_value("href");
1118 std::list<ColorStop*> *stops;
1120 stops=find_colorStop (link);
1123 stops=new std::list<ColorStop*>();
1124 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1126 xmlpp::Node::NodeList list = node->get_children();
1127 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1128 Glib::ustring name =(*iter)->get_name();
1129 if(name.compare("stop")==0){
1130 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1131 Glib::ustring style =nodeIter->get_attribute_value("style");
1132 float offset=atof(nodeIter->get_attribute_value("offset").data());
1136 extractSubAttribute (style,"stop-color",&stop_color);
1137 extractSubAttribute (style,"stop-opacity",&opacity);
1139 if(opacity.empty()) opacity="1";
1140 if(stop_color.empty()) stop_color="#000000";//black for default :S
1141 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1147 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops));
1152 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1153 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1154 Glib::ustring id =nodeElement->get_attribute_value("id");
1155 float cx =atof(nodeElement->get_attribute_value("cx").data());
1156 float cy =atof(nodeElement->get_attribute_value("cy").data());
1157 float r =atof(nodeElement->get_attribute_value("r").data());
1158 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1159 std::list<ColorStop*> *stops=NULL;
1161 //inkscape always use link, i dont need parser stops here, but it's posible
1162 stops=find_colorStop (link);
1165 rg.push_back(newRadialGradient(id,cx,cy,r,stops));
1170 Svg_parser::newColorStop(String color,float opacity,float pos){
1172 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1173 float r=getRed(color);
1174 float g=getGreen(color);
1175 float b=getBlue(color);
1177 Color ret=adjustGamma(r/255,g/255,b/255,a);
1178 _stop->r=ret.get_r();
1179 _stop->g=ret.get_g();
1180 _stop->b=ret.get_b();
1181 _stop->a=ret.get_a();
1186 Svg_parser::adjustGamma(float r,float g,float b,float a){
1188 if(gamma.get_gamma_r()!=1.0){
1190 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1192 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1194 if(gamma.get_gamma_g()!=1.0){
1196 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1198 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1200 if(gamma.get_gamma_b()!=1.0){
1202 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1204 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1210 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops){
1211 LinearGradient* data;
1212 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1213 sprintf(data->name,"%s",name.data());
1223 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops){
1224 RadialGradient* data;
1225 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1226 sprintf(data->name,"%s",name.data());
1236 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1237 root->set_attribute("type","colorcorrect");
1238 root->set_attribute("active","true");
1239 root->set_attribute("version","0.1");
1240 root->set_attribute("desc","Gamma");
1241 build_real (root->add_child("param"),"gamma",gamma);
1244 Svg_parser::build_transform(const String transform){
1246 String tf(transform);
1248 std::vector<String> tokens=tokenize(tf," ");
1249 std::vector<String>::iterator aux=tokens.begin();
1250 while(aux!=tokens.end()){
1251 if((*aux).compare(0,9,"translate")==0){
1254 start =(*aux).find_first_of("(")+1;
1255 end =(*aux).find_first_of(",");
1256 dx =atof((*aux).substr(start,end-start).data());
1257 start =(*aux).find_first_of(",")+1;
1258 end =(*aux).size()-1;
1259 dy =atof((*aux).substr(start,end-start).data());
1261 a=newMatrix(1,0,0,1,dx,dy);
1263 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1264 }else if((*aux).compare(0,5,"scale")==0){
1266 a=newMatrix(1,0,0,1,0,0);
1267 }else if((*aux).compare(0,6,"rotate")==0){
1268 float angle,seno,coseno;
1270 start =(*aux).find_first_of("(")+1;
1271 end =(*aux).size()-1;
1272 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1276 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1278 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1279 }else if((*aux).compare(0,6,"matrix")==0){
1280 int start =(*aux).find_first_of('(')+1;
1281 int end =(*aux).find_first_of(')');
1283 a=newMatrix((*aux).substr(start,end-start));
1285 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1287 a=newMatrix(1,0,0,1,0,0);
1295 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1296 root->set_attribute("type","translate");
1297 root->set_attribute("active","true");
1298 root->set_attribute("version","0.1");
1299 build_vector (root->add_child("param"),"origin",dx,dy);
1302 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1303 root->set_attribute("type","rotate");
1304 root->set_attribute("active","true");
1305 root->set_attribute("version","0.1");
1306 build_vector (root->add_child("param"),"origin",dx,dy);
1307 build_real (root->add_child("param"),"amount",angle);
1310 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1311 root->set_attribute("name","vector_list");
1312 xmlpp::Element *child=root->add_child("dynamic_list");
1313 child->set_attribute("type","vector");
1314 std::list<Vertice*>::iterator aux = p.begin();
1315 while(aux!=p.end()){
1316 xmlpp::Element *child_entry=child->add_child("entry");
1317 xmlpp::Element *child_vector=child_entry->add_child("vector");
1318 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1319 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1324 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1325 xmlpp::Element *child_comp=root->add_child("composite");
1326 child_comp->set_attribute("type","bline_point");
1327 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1328 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1329 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1330 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1331 else build_param (child_comp->add_child("split"),"","bool","false");
1333 xmlpp::Element *child_t1=child_comp->add_child("t1");
1334 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1335 child_rc->set_attribute("type","vector");
1336 build_param (child_rc->add_child("radius"),"","real",p->radio1);
1337 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1339 xmlpp::Element *child_t2=child_comp->add_child("t2");
1340 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1341 child_rc2->set_attribute("type","vector");
1342 build_param (child_rc2->add_child("radius"),"","real",p->radio2);
1343 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1347 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1348 root->set_attribute("name","bline");
1349 xmlpp::Element *child=root->add_child("bline");
1350 child->set_attribute("type","bline_point");
1352 child->set_attribute("loop","true");
1354 child->set_attribute("loop","false");
1355 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1356 std::list<Vertice*>::iterator aux = p.begin();
1357 while(aux!=p.end()){
1358 if(*aux) build_vertice (child->add_child("entry"),*aux);
1364 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1365 if(!type.empty() && !value.empty()){
1366 if(!name.empty()) root->set_attribute("name",name);
1367 xmlpp::Element *child=root->add_child(type);
1368 child->set_attribute("value",value);
1370 root->get_parent()->remove_child(root);
1374 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1376 if(!name.empty()) root->set_attribute("name",name);
1377 xmlpp::Element *child=root->add_child(type);
1378 child->set_attribute("value",etl::strprintf ("%f",value));
1380 root->get_parent()->remove_child(root);
1384 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1386 if(!name.empty()) root->set_attribute("name",name);
1387 xmlpp::Element *child=root->add_child(type);
1388 char *enteroc=new char[10];
1389 sprintf(enteroc,"%d",value);
1390 child->set_attribute("value",enteroc);
1393 root->get_parent()->remove_child(root);
1398 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1399 if(name.compare("")!=0) root->set_attribute("name",name);
1400 xmlpp::Element *child=root->add_child("integer");
1401 char *enteroc=new char[10];
1402 sprintf(enteroc,"%d",value);
1403 child->set_attribute("value",enteroc);
1406 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1407 if(name.compare("")!=0) root->set_attribute("name",name);
1408 xmlpp::Element *child=root->add_child("real");
1409 char *realc=new char[20];
1410 sprintf(realc,"%f",value);
1411 child->set_attribute("value",realc);
1415 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1416 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1417 root->get_parent()->remove_child(root);
1418 printf("Color aborted\n");
1421 Color ret=adjustGamma(r/255,g/255,b/255,a);
1423 root->set_attribute("name","color");
1424 xmlpp::Element *child=root->add_child("color");
1425 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1426 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1427 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1428 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1431 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1433 if(name.compare("")!=0) root->set_attribute("name",name);
1434 xmlpp::Element *child=root->add_child("vector");
1435 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1436 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1440 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1441 if(name.compare("")!=0) root->set_attribute("name",name);
1442 xmlpp::Element *child=root->add_child("vector");
1443 if(!guid.empty()) child->set_attribute("guid",guid);
1444 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1445 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1449 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1450 root->set_attribute("type","PasteCanvas");
1451 root->set_attribute("active","true");
1452 root->set_attribute("version","0.1");
1453 root->set_attribute("desc","Composite");
1454 build_param (root->add_child("param"),"z_depth","real","0");
1455 build_param (root->add_child("param"),"amount","real","1");
1456 build_param (root->add_child("param"),"blend_method","integer","0");
1457 build_vector (root->add_child("param"),"origin",0,0);
1458 xmlpp::Element *child=root->add_child("param");
1459 child->set_attribute("name","canvas");
1460 return child->add_child("canvas");
1465 Svg_parser::coor2vect(float *x,float *y){
1469 sy= atof(height.c_str())-sy;
1478 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1480 float d1x,d1y,d2x,d2y,dx,dy;
1491 rd=sqrt(dx*dx + dy*dy);
1493 ag=PI + atan(dy/dx);
1494 }else if(dx>0 && dy<0){
1495 ag=PI + atan(dy/dx);
1496 }else if(dx<0 && dy<0){
1498 }else if(dx<0 && dy>0){
1499 ag= 2*PI+atan(dy/dx);
1500 }else if(dx==0 && dy>0){
1502 }else if(dx==0 && dy<0){
1504 }else if(dx==0 && dy==0){
1506 }else if(dx<0 && dy==0){
1508 }else if(dx>0 && dy==0){
1516 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1518 float d1x,d1y,d2x,d2y,dx,dy;
1530 rd=sqrt(dx*dx + dy*dy);
1532 ag=PI + atan(dy/dx);
1533 // printf("case 180-270\n");
1534 }else if(dx>0 && dy<0){
1535 ag=PI + atan(dy/dx);
1536 // printf("case 90-180\n");
1537 }else if(dx<0 && dy<0){
1539 // printf("case 0-90\n");
1540 }else if(dx<0 && dy>0){
1541 ag= 2*PI+atan(dy/dx);
1542 // printf("case 270-360\n");
1543 }else if(dx==0 && dy>0){
1545 }else if(dx==0 && dy<0){
1547 }else if(dx==0 && dy==0){
1549 }else if(dx<0 && dy==0){
1551 }else if(dx>0 && dy==0){
1561 Svg_parser::setSplit(Vertice* p,bool val){
1567 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1568 if(nodo->x==a && nodo->y==b)
1574 Svg_parser::newVertice(float x,float y){
1576 vert=(Vertice*)malloc(sizeof(Vertice));
1579 vert->radio1=vert->radio2=vert->angle1=vert->angle2=0;
1584 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1586 if(!attribute.empty()){
1587 String str(attribute);
1589 std::vector<String> tokens=tokenize(str,";");
1590 std::vector<String>::iterator aux=tokens.begin();
1591 while(aux!=tokens.end()){
1592 int mid= (*aux).find_first_of(":");
1593 if((*aux).substr(0,mid).compare(name)==0){
1594 int end=(*aux).size();
1595 *value=(*aux).substr(mid+1,end-mid);
1604 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1607 if(!path_style.empty())
1608 fnd=extractSubAttribute(path_style,name,&value);
1610 if(!master_style.empty())
1611 fnd=extractSubAttribute(master_style,name,&value);
1618 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1621 if(!path_style.empty())
1622 fnd=extractSubAttribute(path_style,name,&value);
1623 if(fnd==0 && !master_style.empty())
1624 fnd=extractSubAttribute(master_style,name,&value);
1626 if(!subattribute.empty())
1635 Svg_parser::randomLetter(){
1637 if(a) return (49 + rand()%9);
1638 else return (65 + rand()%24);
1642 Svg_parser::getRed(String hex){
1644 return hextodec(hex.substr(1,2));
1645 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1646 int start=hex.find_first_of("(")+1;
1647 int end =hex.find_last_of(")");
1648 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1649 return atoi(aux.data());
1654 Svg_parser::getGreen(String hex){
1656 return hextodec(hex.substr(3,2));
1657 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1658 int start=hex.find_first_of("(")+1;
1659 int end =hex.find_last_of(")");
1660 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1661 return atoi(aux.data());
1666 Svg_parser::getBlue(String hex){
1668 return hextodec(hex.substr(5,2));
1669 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1670 int start=hex.find_first_of("(")+1;
1671 int end =hex.find_last_of(")");
1672 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1673 return atoi(aux.data());
1678 Svg_parser::hextodec(String hex){
1687 else if(hex.at(i)=='1')
1689 else if(hex.at(i)=='2')
1691 else if(hex.at(i)=='3')
1693 else if(hex.at(i)=='4')
1695 else if(hex.at(i)=='5')
1697 else if(hex.at(i)=='6')
1699 else if(hex.at(i)=='7')
1701 else if(hex.at(i)=='8')
1703 else if(hex.at(i)=='9')
1705 else if(hex.at(i)=='a')
1707 else if(hex.at(i)=='b')
1709 else if(hex.at(i)=='c')
1711 else if(hex.at(i)=='d')
1713 else if(hex.at(i)=='e')
1715 else if(hex.at(i)=='f')
1723 result+=pow(16,i)*ihex[top-i-1];
1731 Svg_parser::getDimension(const String ac){
1735 int length=ac.size();
1737 if(isdigit(ac.at(length-1))){
1739 }else if(ac.at(length-1)=='%'){
1742 String mtc=ac.substr(length-2,length);
1743 String nmc=ac.substr(0,length-2);
1744 if(mtc.compare("px")==0){
1745 af=atof(nmc.data());
1746 }else if(mtc.compare("pt")==0){
1747 af=atof(nmc.data())*1.25;
1748 }else if(mtc.compare("em")==0){
1749 af=atof(nmc.data())*16;
1750 }else if(mtc.compare("mm")==0){
1751 af=atof(nmc.data())*3.54;
1752 }else if(mtc.compare("pc")==0){
1753 af=atof(nmc.data())*15;
1754 }else if(mtc.compare("cm")==0){
1755 af=atof(nmc.data())*35.43;
1756 }else if(mtc.compare("in")==0){
1757 af=atof(nmc.data())*90;
1766 Svg_parser::newMatrix(Matrix *a){
1768 data=(Matrix*)malloc(sizeof(Matrix));
1769 data->a=a->a; data->b=a->b; data->c=a->c;
1770 data->d=a->d; data->e=a->e; data->f=a->f;
1774 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1776 data=(Matrix*)malloc(sizeof(Matrix));
1777 data->a=a; data->b=b; data->c=c;
1778 data->d=d; data->e=e; data->f=f;
1782 Svg_parser::newMatrix(const String mvector){
1783 if(!mvector.empty()){
1784 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1785 std::vector<String> tokens=tokenize(mvector,",");
1786 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1787 data->a=atof(tokens.at(0).data());
1788 data->b=atof(tokens.at(1).data());
1789 data->c=atof(tokens.at(2).data());
1790 data->d=atof(tokens.at(3).data());
1791 data->e=atof(tokens.at(4).data());
1792 data->f=atof(tokens.at(5).data());
1795 return newMatrix(1,0,0,1,0,0);
1799 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1803 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1804 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1810 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1811 Matrix* aux=newMatrix(0,0,0,0,0,0);
1812 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1813 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1814 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1815 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1816 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1817 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1821 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1822 Matrix* aux=newMatrix(0,0,0,0,0,0);
1823 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1824 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1825 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1826 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1827 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1828 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1837 Svg_parser::matrixIsNull(Matrix *mtx){
1838 if(mtx == NULL) return true;
1843 Svg_parser::getRadian(float sexa){
1844 return (sexa*2*PI)/360;
1847 Svg_parser::removeS(String *input){
1848 for(unsigned int i=0;i<input->size();i++){
1849 if(input->at(i)==' '){
1855 Svg_parser::removeIntoS(String *input){
1857 for(unsigned int i=0;i<input->size();i++){
1858 if(input->at(i)=='('){
1860 }else if(input->at(i)==')'){
1862 }else if(into && input->at(i)==' '){
1868 Svg_parser::tokenize(const String& str,const String& delimiters){
1869 std::vector<String> tokens;
1870 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1871 String::size_type pos = str.find_first_of(delimiters, lastPos);
1872 while (String::npos != pos || String::npos != lastPos){
1873 tokens.push_back(str.substr(lastPos, pos - lastPos));
1874 lastPos = str.find_first_not_of(delimiters, pos);
1875 pos = str.find_first_of(delimiters, lastPos);
1880 Svg_parser::new_guid(){
1882 return GUID::hasher(uid).get_string();