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 #define SVG_SEP_TRANSFORMS 1
214 #define SVG_RESOLVE_BLINE 1
217 Svg_parser::parser_graphics(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
218 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
219 Glib::ustring nodename = node->get_name();
221 //load sub-attributes
222 Glib::ustring id =nodeElement->get_attribute_value("id");
223 Glib::ustring transform =nodeElement->get_attribute_value("transform");
225 //resolve transformations
227 if(!transform.empty())
228 mtx=build_transform (transform);
229 if (SVG_SEP_TRANSFORMS)
233 composeMatrix(&mtx,mtx_parent,mtx);
235 mtx=newMatrix(mtx_parent);
238 if(nodename.compare("g")==0){
239 parser_layer (node,root->add_child("layer"),parent_style,mtx);
243 Glib::ustring obj_style =nodeElement->get_attribute_value("style");
244 Glib::ustring obj_fill =nodeElement->get_attribute_value("fill");
247 String fill =loadAttribute("fill",obj_style,parent_style,obj_fill,"none");
248 String fill_rule =loadAttribute("fill-rule",obj_style,parent_style,"evenodd");
249 String stroke =loadAttribute("stroke",obj_style,parent_style,"none");
250 String stroke_width =loadAttribute("stroke-width",obj_style,parent_style,"1px");
251 String stroke_linecap =loadAttribute("stroke-linecap",obj_style,parent_style,"butt");
252 String stroke_linejoin =loadAttribute("stroke-linejoin",obj_style,parent_style,"miter");
253 String stroke_opacity =loadAttribute("stroke-opacity",obj_style,parent_style,"1");
254 String fill_opacity =loadAttribute("fill-opacity",obj_style,parent_style,"1");
255 String opacity =loadAttribute("opacity",obj_style,parent_style,"1");
259 int typeFill=0; //nothing
261 if(fill.compare("none")!=0){
264 if(typeFill==1 && fill.compare(0,3,"url")==0){
265 typeFill=2; //gradient
268 int typeStroke=0;//nothing
270 if(stroke.compare("none")!=0){
271 typeStroke=1; //simple
273 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
274 typeStroke=2; //gradient
277 xmlpp::Element* child_layer = nodeStartBasicLayer(root->add_child("layer"));
278 xmlpp::Element* child_fill = child_layer;
279 xmlpp::Element* child_stroke = child_layer;
281 child_fill=nodeStartBasicLayer(child_fill->add_child("layer"));
284 child_stroke=nodeStartBasicLayer(child_stroke->add_child("layer"));
288 if(nodename.compare("rect")==0 && typeFill!=0){
289 rect_simple(nodeElement,child_fill,fill,fill_opacity,opacity);
291 build_url (child_fill->add_child("layer"),fill,NULL);
293 parser_effects(nodeElement,child_layer,parent_style,mtx);
298 //=======================================================================
299 std::list<std::list<Vertice*> > k;
300 //if we are creating a bline
302 //First, create the list of vertices
303 if (SVG_RESOLVE_BLINE) {
304 if(nodename.compare("path")==0){
305 k=parser_path_d (nodeElement->get_attribute_value("d"),mtx);
306 } else if(nodename.compare("polygon")==0){
307 k=parser_polygon_path (nodeElement->get_attribute_value("points"),mtx);
310 if(nodename.compare("path")==0){
311 k=parser_path_d (nodeElement->get_attribute_value("d"),NULL);
312 } else if(nodename.compare("polygon")==0){
313 k=parser_polygon_path (nodeElement->get_attribute_value("points"),NULL);
320 for (int i=0;i<n;i++){
321 bline_id[i]=new_guid();
322 offset_id[i]=new_guid();
325 std::list<std::list<Vertice*> >::iterator aux = k.begin();
326 if(typeFill!=0){//region layer
327 for (int i=0; aux!=k.end(); aux++){
328 xmlpp::Element *child_region=child_fill->add_child("layer");
329 child_region->set_attribute("type","region");
330 child_region->set_attribute("active","true");
331 child_region->set_attribute("version","0.1");
332 child_region->set_attribute("desc",id);
333 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
334 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
335 build_param (child_region->add_child("param"),"blend_method","integer","0");
336 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
337 build_vector (child_region->add_child("param"),"offset",0,0,offset_id[i]);
338 build_param (child_region->add_child("param"),"invert","bool","false");
339 build_param (child_region->add_child("param"),"antialias","bool","true");
340 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
341 build_param (child_region->add_child("param"),"blurtype","integer","1");
342 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
343 else build_param (child_region->add_child("param"),"winding_style","integer","0");
345 build_bline (child_region->add_child("param"),*aux,loop,bline_id[i]);
349 if(typeFill==2){ //gradient in onto mode (fill)
350 if (SVG_RESOLVE_BLINE)
351 build_url(child_fill->add_child("layer"),fill,mtx);
353 build_url(child_fill->add_child("layer"),fill,NULL);
356 if(typeStroke!=0){//outline layer
358 for (aux=k.begin(); aux!=k.end(); aux++){
359 xmlpp::Element *child_outline=child_stroke->add_child("layer");
360 child_outline->set_attribute("type","outline");
361 child_outline->set_attribute("active","true");
362 child_outline->set_attribute("version","0.2");
363 child_outline->set_attribute("desc",id);
364 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
365 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
366 build_param (child_outline->add_child("param"),"blend_method","integer","0");
367 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
368 build_vector (child_outline->add_child("param"),"offset",0,0,offset_id[i]);
369 build_param (child_outline->add_child("param"),"invert","bool","false");
370 build_param (child_outline->add_child("param"),"antialias","bool","true");
371 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
372 build_param (child_outline->add_child("param"),"blurtype","integer","1");
374 build_param (child_outline->add_child("param"),"winding_style","integer","0");
376 build_bline (child_outline->add_child("param"),*aux,loop,bline_id[i]);
378 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
379 build_param (child_outline->add_child("param"),"width","real",stroke_width);
380 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
381 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
382 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
383 if(stroke_linecap.compare("butt")==0){
384 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
385 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
387 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
388 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
390 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
391 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
397 if(typeStroke==2){ //gradient in onto mode (stroke)
398 if (SVG_RESOLVE_BLINE)
399 build_url(child_stroke->add_child("layer"),fill,mtx);
401 build_url(child_stroke->add_child("layer"),fill,NULL);
403 if (SVG_RESOLVE_BLINE)
404 parser_effects(nodeElement,child_layer,parent_style,NULL);
406 parser_effects(nodeElement,child_layer,parent_style,mtx);
413 Svg_parser::rect_simple(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
414 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
415 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
416 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
417 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
418 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
420 xmlpp::Element *child_rect=root->add_child("layer");
421 child_rect->set_attribute("type","rectangle");
422 child_rect->set_attribute("active","true");
423 child_rect->set_attribute("version","0.2");
424 child_rect->set_attribute("desc",rect_id);
426 build_real(child_rect->add_child("param"),"z_depth",0.0);
427 build_real(child_rect->add_child("param"),"amount",1.0);
428 build_integer(child_rect->add_child("param"),"blend_method",0);
429 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
431 float auxx=atof(rect_x.c_str());
432 float auxy=atof(rect_y.c_str());
433 coor2vect(&auxx,&auxy);
434 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
435 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
436 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
437 coor2vect(&auxx,&auxy);
438 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
444 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
445 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
446 Glib::ustring label =nodeElement->get_attribute_value("label");
447 Glib::ustring style =nodeElement->get_attribute_value("style");
448 Glib::ustring fill =nodeElement->get_attribute_value("fill");
453 }else if(!fill.empty()){
454 layer_style.append("fill:");
455 layer_style.append(fill);
456 }else if(!parent_style.empty()){
457 layer_style=parent_style;
460 root->set_attribute("type","PasteCanvas");
461 root->set_attribute("active","true");
462 root->set_attribute("version","0.1");
463 if(!label.empty()) root->set_attribute("desc",label);
464 else root->set_attribute("desc","Inline Canvas");
466 build_real(root->add_child("param"),"z_depth",0.0);
467 build_real(root->add_child("param"),"amount",1.0);
468 build_integer(root->add_child("param"),"blend_method",0);
469 build_vector (root->add_child("param"),"origin",0,0);
471 //printf(" canvas attributes ");
473 xmlpp::Element *child_canvas=root->add_child("param");
474 child_canvas->set_attribute("name","canvas");
475 child_canvas=child_canvas->add_child("canvas");
476 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
478 xmlpp::Node::NodeList list = node->get_children();
479 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
480 Glib::ustring name =(*iter)->get_name();
481 parser_graphics (*iter,child_canvas,layer_style,mtx);
484 if (! SVG_SEP_TRANSFORMS) parser_effects(nodeElement,child_canvas,parent_style,mtx);
488 std::list<std::list<Vertice*> >
489 Svg_parser::parser_polygon_path(Glib::ustring polygon_points, Matrix* mtx){
490 std::list<std::list<Vertice*> > k0;
491 if(polygon_points.empty())
493 std::list<Vertice*> k;
494 std::vector<String> tokens=get_tokens_path (polygon_points);
496 float ax,ay; ax=ay=0;
497 for(i=0;i<tokens.size();i++){
498 ax=atof(tokens.at(i).data());
499 i++; if(tokens.at(i).compare(",")==0) i++;
500 ay=atof(tokens.at(i).data());
502 if(mtx) transformPoint2D(mtx,&ax,&ay);
506 k.push_back(newVertice(ax,ay));
513 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
514 std::vector<String> tokens;
519 while(i<path.size()){
522 case 0: //initial state
523 if(a=='m'){ e=1; i++;}
524 else if(a=='c'){ e= 2; i++;}
525 else if(a=='q'){ e= 3; i++;}
526 else if(a=='t'){ e= 4; i++;}
527 else if(a=='a'){ e= 5; i++;}
528 else if(a=='l'){ e= 6; i++;}
529 else if(a=='v'){ e= 7; i++;}
530 else if(a=='h'){ e= 8; i++;}
531 else if(a=='M'){ e= 9; i++;}
532 else if(a=='C'){ e=10; i++;}
533 else if(a=='Q'){ e=11; i++;}
534 else if(a=='T'){ e=12; i++;}
535 else if(a=='A'){ e=13; i++;}
536 else if(a=='L'){ e=14; i++;}
537 else if(a=='V'){ e=15; i++;}
538 else if(a=='H'){ e=16; i++;}
539 else if(a=='z' || a=='Z'){ e=17; i++;}
540 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
541 else if(a==','){ e=19; i++;}
542 else if(a==' '){i++;}
545 case 1 : tokens.push_back("m"); e=0; break;//move
546 case 2 : tokens.push_back("c"); e=0; break;//curve
547 case 3 : tokens.push_back("q"); e=0; break;//quadratic
548 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
549 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
550 case 6 : tokens.push_back("l"); e=0; break;//line to
551 case 7 : tokens.push_back("v"); e=0; break;//vertical
552 case 8 : tokens.push_back("h"); e=0; break;//horizontal
554 case 9 : tokens.push_back("M"); e=0; break;
555 case 10: tokens.push_back("C"); e=0; break;
556 case 11: tokens.push_back("Q"); e=0; break;
557 case 12: tokens.push_back("T"); e=0; break;
558 case 13: tokens.push_back("A"); e=0; break;
559 case 14: tokens.push_back("L"); e=0; break;
560 case 15: tokens.push_back("V"); e=0; break;
561 case 16: tokens.push_back("H"); e=0; break;
563 case 17: tokens.push_back("z"); e=0; break;//loop
564 case 18: if(a=='-'||a=='.'|| isdigit (a)){
565 buffer.append(path.substr(i,1));i++;
570 case 19: tokens.push_back(","); e=0; break;
571 case 20: tokens.push_back(buffer);
577 switch(e){//last element
578 case 1 : tokens.push_back("m"); break;
579 case 2 : tokens.push_back("c"); break;
580 case 3 : tokens.push_back("q"); break;
581 case 4 : tokens.push_back("t"); break;
582 case 5 : tokens.push_back("a"); break;
583 case 6 : tokens.push_back("l"); break;
584 case 7 : tokens.push_back("v"); break;
585 case 8 : tokens.push_back("h"); break;
586 case 9 : tokens.push_back("M"); break;
587 case 10: tokens.push_back("C"); break;
588 case 11: tokens.push_back("Q"); break;
589 case 12: tokens.push_back("T"); break;
590 case 13: tokens.push_back("A"); break;
591 case 14: tokens.push_back("L"); break;
592 case 15: tokens.push_back("V"); break;
593 case 16: tokens.push_back("H"); break;
594 case 17: tokens.push_back("z"); break;
595 case 18: tokens.push_back(buffer); break;
596 case 19: tokens.push_back(","); break;
597 case 20: tokens.push_back(buffer); break;
603 std::list<std::list<Vertice*> >
604 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
605 std::list<std::list<Vertice*> > k;
606 std::list<Vertice*> k1;
607 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
608 float actual_x=0,actual_y=0;//for relative methods;
611 std::vector<String> tokens=get_tokens_path(path_d);
612 for(i=0;i<tokens.size();i++){
613 if(tokens.at(i).compare("M")==0){//absolute move to
618 i++; ax=atof(tokens.at(i).data());
619 i++; if(tokens.at(i).compare(",")==0) i++;
620 ay=atof(tokens.at(i).data());
624 if(mtx) transformPoint2D(mtx,&ax,&ay);
626 k1.push_back(newVertice (ax,ay)); //first element
627 setSplit(k1.back(),TRUE);
628 }else if(tokens.at(i).compare("C")==0){ //absolute curve
630 i++; tgx2=atof(tokens.at(i).data());
631 i++; if(tokens.at(i).compare(",")==0) i++;
632 tgy2=atof(tokens.at(i).data());
634 i++; tgx=atof(tokens.at(i).data());
635 i++; if(tokens.at(i).compare(",")==0) i++;
636 tgy=atof(tokens.at(i).data());
638 i++; ax=atof(tokens.at(i).data());
639 i++; if(tokens.at(i).compare(",")==0) i++;
640 ay=atof(tokens.at(i).data());
645 transformPoint2D(mtx,&tgx2,&tgy2);
646 transformPoint2D(mtx,&ax,&ay);
647 transformPoint2D(mtx,&tgx,&tgy);
650 coor2vect(&tgx2,&tgy2);
652 coor2vect(&tgx,&tgy);
654 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
655 if(isFirst(k1.front(),ax,ay)){
656 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
658 k1.push_back(newVertice (ax,ay));
659 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
660 setSplit(k1.back(),TRUE);
662 }else if(tokens.at(i).compare("Q")==0){ //absolute quadractic curve
664 i++; tgx=ax=atof(tokens.at(i).data());
665 i++; if(tokens.at(i).compare(",")==0) i++;
666 tgy=ay=atof(tokens.at(i).data());
668 i++; ax=atof(tokens.at(i).data());
669 i++; if(tokens.at(i).compare(",")==0) i++;
670 ay=atof(tokens.at(i).data());
675 transformPoint2D(mtx,&ax,&ay);
676 transformPoint2D(mtx,&tgx,&tgy);
680 coor2vect(&tgx,&tgy);
682 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
683 setSplit(k1.back(),FALSE);
684 k1.push_back(newVertice (ax,ay));
685 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
686 }else if(tokens.at(i).compare("L")==0){ //absolute line to
688 i++; ax=atof(tokens.at(i).data());
689 i++; if(tokens.at(i).compare(",")==0) i++;
690 ay=atof(tokens.at(i).data());
694 if(mtx) transformPoint2D(mtx,&ax,&ay);
698 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
699 if(isFirst(k1.front(),ax,ay)){
700 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
702 k1.push_back(newVertice(ax,ay));
703 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
705 }else if(tokens.at(i).compare("l")==0){//relative line to
707 i++; ax=atof(tokens.at(i).data());
708 i++; if(tokens.at(i).compare(",")==0) i++;
709 ay=atof(tokens.at(i).data());
716 if(mtx) transformPoint2D(mtx,&ax,&ay);
720 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
721 if(isFirst(k1.front(),ax,ay)){
722 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
724 k1.push_back(newVertice(ax,ay));
725 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
727 }else if(tokens.at(i).compare("H")==0){//absolute horizontal move
728 //the same that L but only Horizontal movement
730 i++; ax=atof(tokens.at(i).data());
735 if(mtx) transformPoint2D(mtx,&ax,&ay);
739 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
740 if(isFirst(k1.front(),ax,ay)){
741 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
743 k1.push_back(newVertice(ax,ay));
744 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
746 }else if(tokens.at(i).compare("h")==0){//horizontal relative
747 i++; ax=atof(tokens.at(i).data());
753 if(mtx) transformPoint2D(mtx,&ax,&ay);
757 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
758 if(isFirst(k1.front(),ax,ay)){
759 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
761 k1.push_back(newVertice(ax,ay));
762 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
764 }else if(tokens.at(i).compare("V")==0){//vertical absolute
766 i++; ay=atof(tokens.at(i).data());
771 if(mtx) transformPoint2D(mtx,&ax,&ay);
775 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
776 if(isFirst(k1.front(),ax,ay)){
777 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
779 k1.push_back(newVertice(ax,ay));
780 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
782 }else if(tokens.at(i).compare("v")==0){//relative
784 i++; ay=atof(tokens.at(i).data());
790 if(mtx) transformPoint2D(mtx,&ax,&ay);
794 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
795 if(isFirst(k1.front(),ax,ay)){
796 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
798 k1.push_back(newVertice(ax,ay));
799 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
801 }else if(tokens.at(i).compare("T")==0){// I don't know what does it
802 }else if(tokens.at(i).compare("A")==0){//elliptic arc
804 //isn't complete support, is only for circles
806 //this curve have 6 parameters
808 float radio_x,radio_y;
812 i++; radio_x=atof(tokens.at(i).data());
813 i++; if(tokens.at(i).compare(",")==0) i++;
814 radio_y=atof(tokens.at(i).data());
816 i++; angle=atof(tokens.at(i).data());
818 i++; large=atoi(tokens.at(i).data());
819 i++; sweep=atoi(tokens.at(i).data());
821 i++; ax=atof(tokens.at(i).data());
822 i++; if(tokens.at(i).compare(",")==0) i++;
823 ay=atof(tokens.at(i).data());
825 if(!large && !sweep){
827 tgx2 = actual_x + radio_x*0.5;
830 tgy = ay + radio_y*0.5;
835 transformPoint2D(mtx,&tgx2,&tgy2);
836 transformPoint2D(mtx,&ax,&ay);
837 transformPoint2D(mtx,&tgx,&tgy);
840 coor2vect(&tgx2,&tgy2);
842 coor2vect(&tgx,&tgy);
844 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
845 if(isFirst(k1.front(),ax,ay)){
846 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
848 k1.push_back(newVertice (ax,ay));
849 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
850 setSplit(k1.back(),TRUE);
852 }else if(!large && sweep){
855 tgy2 = actual_y + radio_y*0.5;
856 tgx = ax + radio_x*0.5;
862 transformPoint2D(mtx,&tgx2,&tgy2);
863 transformPoint2D(mtx,&ax,&ay);
864 transformPoint2D(mtx,&tgx,&tgy);
867 coor2vect(&tgx2,&tgy2);
869 coor2vect(&tgx,&tgy);
871 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
872 if(isFirst(k1.front(),ax,ay)){
873 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
875 k1.push_back(newVertice (ax,ay));
876 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
877 setSplit(k1.back(),TRUE);
879 }else if( large && !sweep){//rare
880 //this need more than one vertex
881 }else if( large && sweep){//circles in inkscape are made with this kind of arc
882 if(actual_y==ay){//circles
885 if(actual_x>ax) sense =-1;
886 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
887 in_x = (actual_x+ax)/2;
888 in_y = actual_y - sense*radio_y;
889 in_tgx1 = in_x - sense*(radio_x*0.5);
890 in_tgx2 = in_x + sense*(radio_x*0.5);
895 tgy2=ay - sense*(radio_y*0.5);
897 tgy =ay - sense*(radio_y*0.5);
903 transformPoint2D(mtx,&tgx2,&tgy2);
904 transformPoint2D(mtx,&tgx ,&tgy );
905 transformPoint2D(mtx,&ax,&ay);
907 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
908 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
909 transformPoint2D(mtx,&in_x,&in_y);
912 coor2vect(&tgx2 , &tgy2);
913 coor2vect(&ax , &ay );
914 coor2vect(&tgx , &tgy );
916 coor2vect(&in_tgx2 , &in_tgy2);
917 coor2vect(&in_tgx1 , &in_tgy1);
918 coor2vect(&in_x , &in_y );
921 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
922 //save the intermediate point
923 k1.push_back(newVertice (in_x,in_y));
924 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
925 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
926 setSplit(k1.back(),TRUE); //this could be changed
928 if(isFirst(k1.front(),ax,ay)){
929 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
931 k1.push_back(newVertice (ax,ay));
932 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
933 setSplit(k1.back(),TRUE);
937 }else if(tokens.at(i).compare("z")==0){
940 std::cout<<"don't supported: "<<tokens.at(i)<<std::endl;
944 k.push_front(k1); //last element
949 Svg_parser::parser_effects(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
951 xmlpp::Element *child_transform=root->add_child("layer");
952 child_transform->set_attribute("type","warp");
953 child_transform->set_attribute("active","true");
954 child_transform->set_attribute("version","0.1");
955 child_transform->set_attribute("desc","Transform");
958 x=100;y=100;coor2vect(&x,&y);
959 build_vector (child_transform->add_child("param"),"src_tl",x,y);
961 x=200;y=200;coor2vect(&x,&y);
962 build_vector (child_transform->add_child("param"),"src_br",x,y);
966 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
967 build_vector (child_transform->add_child("param"),"dest_tl",x,y);
970 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
971 build_vector (child_transform->add_child("param"),"dest_tr",x,y);
974 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
975 build_vector (child_transform->add_child("param"),"dest_br",x,y);
978 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
979 build_vector (child_transform->add_child("param"),"dest_bl",x,y);
981 build_param (child_transform->add_child("param"),"clip","bool","false");
982 build_param (child_transform->add_child("param"),"horizon","real","4.0");
988 Svg_parser::parser_defs(const xmlpp::Node* node){
989 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
991 xmlpp::Node::NodeList list = node->get_children();
992 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
993 Glib::ustring name =(*iter)->get_name();
994 if(name.compare("linearGradient")==0){
995 parser_linearGradient(*iter);
996 }else if(name.compare("radialGradient")==0){
997 parser_radialGradient(*iter);
1004 Svg_parser::AdjustPointUrl(){
1006 if(!lg.empty()){//linealgradient
1007 std::list<LinearGradient*>::iterator aux=lg.begin();
1008 while(aux!=lg.end()){
1009 LinearGradient* auxlg=*aux;
1010 coor2vect (&auxlg->x1,&auxlg->y1);
1011 coor2vect (&auxlg->x2,&auxlg->y2);
1015 if(!rg.empty()){//radialgradient
1016 std::list<RadialGradient*>::iterator aux=rg.begin();
1017 while(aux!=rg.end()){
1018 RadialGradient* auxrg=*aux;
1019 coor2vect (&auxrg->cx,&auxrg->cy);
1020 auxrg->r= auxrg->r/kux;
1026 std::list<ColorStop*>*
1027 Svg_parser::find_colorStop(String name){
1029 if(lg.empty()&& rg.empty())
1033 if(find.at(0)=='#') find.erase(0,1);
1035 std::list<LinearGradient*>::iterator aux=lg.begin();
1036 while(aux!=lg.end()){//only find into linear gradients
1037 if(find.compare((*aux)->name)==0){
1038 return (*aux)->stops;
1046 Svg_parser::build_url(xmlpp::Element* root, String name,Matrix *mtx){
1048 if(lg.empty()&& rg.empty())
1049 root->get_parent()->remove_child(root);
1051 int start=name.find_first_of("#")+1;
1052 int end=name.find_first_of(")");
1053 String find= name.substr(start,end-start);
1054 bool encounter=false;
1056 std::list<LinearGradient*>::iterator aux=lg.begin();
1057 while(aux!=lg.end()){
1058 if(find.compare((*aux)->name)==0){
1059 build_linearGradient (root,*aux,mtx);
1065 if(!encounter && !rg.empty()){
1066 std::list<RadialGradient*>::iterator aux=rg.begin();
1067 while(aux!=rg.end()){
1068 if(find.compare((*aux)->name)==0){
1069 build_radialGradient (root,*aux,mtx);
1076 root->get_parent()->remove_child(root);
1078 root->get_parent()->remove_child(root);
1082 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
1083 std::list<ColorStop*>::iterator aux_stop=stops->begin();
1084 while(aux_stop!=stops->end()){
1085 xmlpp::Element *child=root->add_child("color");
1086 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
1087 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
1088 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
1089 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
1090 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
1095 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
1097 root->set_attribute("type","linear_gradient");
1098 root->set_attribute("active","true");
1099 root->set_attribute("desc","Gradient004");
1100 build_param (root->add_child("param"),"z_depth","real","0");
1101 build_param (root->add_child("param"),"amount","real","1");
1103 build_param (root->add_child("param"),"blend_method","integer","21");
1110 transformPoint2D(mtx,&x1,&y1);
1111 transformPoint2D(mtx,&x2,&y2);
1113 coor2vect (&x1,&y1);
1114 coor2vect (&x2,&y2);
1116 build_vector (root->add_child("param"),"p1",x1,y1);
1117 build_vector (root->add_child("param"),"p2",x2,y2);
1119 xmlpp::Element *child=root->add_child("param");
1120 child->set_attribute("name","gradient");
1121 build_stop_color (child->add_child("gradient"),data->stops);
1122 build_param (root->add_child("param"),"loop","bool","false");
1123 build_param (root->add_child("param"),"zigzag","bool","false");
1127 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1130 root->set_attribute("type","radial_gradient");
1131 root->set_attribute("active","true");
1132 build_param (root->add_child("param"),"z_depth","real","0");
1133 build_param (root->add_child("param"),"amount","real","1");
1135 build_param (root->add_child("param"),"blend_method","integer","21");
1137 xmlpp::Element *child=root->add_child("param");
1138 child->set_attribute("name","gradient");
1139 build_stop_color (child->add_child("gradient"),data->stops);
1140 //here the center point and radio
1146 transformPoint2D(mtx,&cx,&cy);
1149 coor2vect (&cx,&cy);
1151 build_vector (root->add_child("param"),"center",cx,cy);
1152 build_param (root->add_child("param"),"radius","real",r);
1154 build_param (root->add_child("param"),"loop","bool","false");
1155 build_param (root->add_child("param"),"zigzag","bool","false");
1160 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1161 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1162 Glib::ustring id =nodeElement->get_attribute_value("id");
1163 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1164 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1165 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1166 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1167 Glib::ustring link =nodeElement->get_attribute_value("href");
1169 std::list<ColorStop*> *stops;
1171 stops=find_colorStop (link);
1174 stops=new std::list<ColorStop*>();
1175 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1177 xmlpp::Node::NodeList list = node->get_children();
1178 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1179 Glib::ustring name =(*iter)->get_name();
1180 if(name.compare("stop")==0){
1181 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1182 Glib::ustring style =nodeIter->get_attribute_value("style");
1183 float offset=atof(nodeIter->get_attribute_value("offset").data());
1187 extractSubAttribute (style,"stop-color",&stop_color);
1188 extractSubAttribute (style,"stop-opacity",&opacity);
1190 if(opacity.empty()) opacity="1";
1191 if(stop_color.empty()) stop_color="#000000";//black for default :S
1192 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1198 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops));
1203 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1204 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1205 Glib::ustring id =nodeElement->get_attribute_value("id");
1206 float cx =atof(nodeElement->get_attribute_value("cx").data());
1207 float cy =atof(nodeElement->get_attribute_value("cy").data());
1208 float r =atof(nodeElement->get_attribute_value("r").data());
1209 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1210 std::list<ColorStop*> *stops=NULL;
1212 //inkscape always use link, i dont need parser stops here, but it's posible
1213 stops=find_colorStop (link);
1216 rg.push_back(newRadialGradient(id,cx,cy,r,stops));
1221 Svg_parser::newColorStop(String color,float opacity,float pos){
1223 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1224 float r=getRed(color);
1225 float g=getGreen(color);
1226 float b=getBlue(color);
1228 Color ret=adjustGamma(r/255,g/255,b/255,a);
1229 _stop->r=ret.get_r();
1230 _stop->g=ret.get_g();
1231 _stop->b=ret.get_b();
1232 _stop->a=ret.get_a();
1237 Svg_parser::adjustGamma(float r,float g,float b,float a){
1239 if(gamma.get_gamma_r()!=1.0){
1241 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1243 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1245 if(gamma.get_gamma_g()!=1.0){
1247 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1249 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1251 if(gamma.get_gamma_b()!=1.0){
1253 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1255 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1261 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops){
1262 LinearGradient* data;
1263 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1264 sprintf(data->name,"%s",name.data());
1274 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops){
1275 RadialGradient* data;
1276 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1277 sprintf(data->name,"%s",name.data());
1287 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1288 root->set_attribute("type","colorcorrect");
1289 root->set_attribute("active","true");
1290 root->set_attribute("version","0.1");
1291 root->set_attribute("desc","Gamma");
1292 build_real (root->add_child("param"),"gamma",gamma);
1295 Svg_parser::build_transform(const String transform){
1297 String tf(transform);
1299 std::vector<String> tokens=tokenize(tf," ");
1300 std::vector<String>::iterator aux=tokens.begin();
1301 while(aux!=tokens.end()){
1302 if((*aux).compare(0,9,"translate")==0){
1305 start =(*aux).find_first_of("(")+1;
1306 end =(*aux).find_first_of(",");
1307 dx =atof((*aux).substr(start,end-start).data());
1308 start =(*aux).find_first_of(",")+1;
1309 end =(*aux).size()-1;
1310 dy =atof((*aux).substr(start,end-start).data());
1312 a=newMatrix(1,0,0,1,dx,dy);
1314 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1315 }else if((*aux).compare(0,5,"scale")==0){
1317 a=newMatrix(1,0,0,1,0,0);
1318 }else if((*aux).compare(0,6,"rotate")==0){
1319 float angle,seno,coseno;
1321 start =(*aux).find_first_of("(")+1;
1322 end =(*aux).size()-1;
1323 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1327 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1329 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1330 }else if((*aux).compare(0,6,"matrix")==0){
1331 int start =(*aux).find_first_of('(')+1;
1332 int end =(*aux).find_first_of(')');
1334 a=newMatrix((*aux).substr(start,end-start));
1336 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1338 a=newMatrix(1,0,0,1,0,0);
1346 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1347 root->set_attribute("type","translate");
1348 root->set_attribute("active","true");
1349 root->set_attribute("version","0.1");
1350 build_vector (root->add_child("param"),"origin",dx,dy);
1353 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1354 root->set_attribute("type","rotate");
1355 root->set_attribute("active","true");
1356 root->set_attribute("version","0.1");
1357 build_vector (root->add_child("param"),"origin",dx,dy);
1358 build_real (root->add_child("param"),"amount",angle);
1361 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1362 root->set_attribute("name","vector_list");
1363 xmlpp::Element *child=root->add_child("dynamic_list");
1364 child->set_attribute("type","vector");
1365 std::list<Vertice*>::iterator aux = p.begin();
1366 while(aux!=p.end()){
1367 xmlpp::Element *child_entry=child->add_child("entry");
1368 xmlpp::Element *child_vector=child_entry->add_child("vector");
1369 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1370 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1375 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1376 xmlpp::Element *child_comp=root->add_child("composite");
1377 child_comp->set_attribute("type","bline_point");
1378 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1379 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1380 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1381 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1382 else build_param (child_comp->add_child("split"),"","bool","false");
1384 xmlpp::Element *child_t1=child_comp->add_child("t1");
1385 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1386 child_rc->set_attribute("type","vector");
1387 build_param (child_rc->add_child("radius"),"","real",p->radio1);
1388 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1390 xmlpp::Element *child_t2=child_comp->add_child("t2");
1391 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1392 child_rc2->set_attribute("type","vector");
1393 build_param (child_rc2->add_child("radius"),"","real",p->radio2);
1394 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1398 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1399 root->set_attribute("name","bline");
1400 xmlpp::Element *child=root->add_child("bline");
1401 child->set_attribute("type","bline_point");
1403 child->set_attribute("loop","true");
1405 child->set_attribute("loop","false");
1406 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1407 std::list<Vertice*>::iterator aux = p.begin();
1408 while(aux!=p.end()){
1409 if(*aux) build_vertice (child->add_child("entry"),*aux);
1415 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1416 if(!type.empty() && !value.empty()){
1417 if(!name.empty()) root->set_attribute("name",name);
1418 xmlpp::Element *child=root->add_child(type);
1419 child->set_attribute("value",value);
1421 root->get_parent()->remove_child(root);
1425 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1427 if(!name.empty()) root->set_attribute("name",name);
1428 xmlpp::Element *child=root->add_child(type);
1429 child->set_attribute("value",etl::strprintf ("%f",value));
1431 root->get_parent()->remove_child(root);
1435 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1437 if(!name.empty()) root->set_attribute("name",name);
1438 xmlpp::Element *child=root->add_child(type);
1439 char *enteroc=new char[10];
1440 sprintf(enteroc,"%d",value);
1441 child->set_attribute("value",enteroc);
1444 root->get_parent()->remove_child(root);
1449 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1450 if(name.compare("")!=0) root->set_attribute("name",name);
1451 xmlpp::Element *child=root->add_child("integer");
1452 char *enteroc=new char[10];
1453 sprintf(enteroc,"%d",value);
1454 child->set_attribute("value",enteroc);
1457 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1458 if(name.compare("")!=0) root->set_attribute("name",name);
1459 xmlpp::Element *child=root->add_child("real");
1460 char *realc=new char[20];
1461 sprintf(realc,"%f",value);
1462 child->set_attribute("value",realc);
1466 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1467 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1468 root->get_parent()->remove_child(root);
1469 printf("Color aborted\n");
1472 Color ret=adjustGamma(r/255,g/255,b/255,a);
1474 root->set_attribute("name","color");
1475 xmlpp::Element *child=root->add_child("color");
1476 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1477 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1478 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1479 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1482 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1484 if(name.compare("")!=0) root->set_attribute("name",name);
1485 xmlpp::Element *child=root->add_child("vector");
1486 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1487 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1491 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1492 if(name.compare("")!=0) root->set_attribute("name",name);
1493 xmlpp::Element *child=root->add_child("vector");
1494 if(!guid.empty()) child->set_attribute("guid",guid);
1495 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1496 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1500 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1501 root->set_attribute("type","PasteCanvas");
1502 root->set_attribute("active","true");
1503 root->set_attribute("version","0.1");
1504 root->set_attribute("desc","Composite");
1505 build_param (root->add_child("param"),"z_depth","real","0");
1506 build_param (root->add_child("param"),"amount","real","1");
1507 build_param (root->add_child("param"),"blend_method","integer","0");
1508 build_vector (root->add_child("param"),"origin",0,0);
1509 xmlpp::Element *child=root->add_child("param");
1510 child->set_attribute("name","canvas");
1511 return child->add_child("canvas");
1516 Svg_parser::coor2vect(float *x,float *y){
1520 sy= atof(height.c_str())-sy;
1529 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1531 float d1x,d1y,d2x,d2y,dx,dy;
1542 rd=sqrt(dx*dx + dy*dy);
1544 ag=PI + atan(dy/dx);
1545 }else if(dx>0 && dy<0){
1546 ag=PI + atan(dy/dx);
1547 }else if(dx<0 && dy<0){
1549 }else if(dx<0 && dy>0){
1550 ag= 2*PI+atan(dy/dx);
1551 }else if(dx==0 && dy>0){
1553 }else if(dx==0 && dy<0){
1555 }else if(dx==0 && dy==0){
1557 }else if(dx<0 && dy==0){
1559 }else if(dx>0 && dy==0){
1567 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1569 float d1x,d1y,d2x,d2y,dx,dy;
1581 rd=sqrt(dx*dx + dy*dy);
1583 ag=PI + atan(dy/dx);
1584 // printf("case 180-270\n");
1585 }else if(dx>0 && dy<0){
1586 ag=PI + atan(dy/dx);
1587 // printf("case 90-180\n");
1588 }else if(dx<0 && dy<0){
1590 // printf("case 0-90\n");
1591 }else if(dx<0 && dy>0){
1592 ag= 2*PI+atan(dy/dx);
1593 // printf("case 270-360\n");
1594 }else if(dx==0 && dy>0){
1596 }else if(dx==0 && dy<0){
1598 }else if(dx==0 && dy==0){
1600 }else if(dx<0 && dy==0){
1602 }else if(dx>0 && dy==0){
1612 Svg_parser::setSplit(Vertice* p,bool val){
1618 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1619 if(nodo->x==a && nodo->y==b)
1625 Svg_parser::newVertice(float x,float y){
1627 vert=(Vertice*)malloc(sizeof(Vertice));
1630 vert->radio1=vert->radio2=vert->angle1=vert->angle2=0;
1635 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1637 if(!attribute.empty()){
1638 String str(attribute);
1640 std::vector<String> tokens=tokenize(str,";");
1641 std::vector<String>::iterator aux=tokens.begin();
1642 while(aux!=tokens.end()){
1643 int mid= (*aux).find_first_of(":");
1644 if((*aux).substr(0,mid).compare(name)==0){
1645 int end=(*aux).size();
1646 *value=(*aux).substr(mid+1,end-mid);
1655 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1658 if(!path_style.empty())
1659 fnd=extractSubAttribute(path_style,name,&value);
1661 if(!master_style.empty())
1662 fnd=extractSubAttribute(master_style,name,&value);
1669 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1672 if(!path_style.empty())
1673 fnd=extractSubAttribute(path_style,name,&value);
1674 if(fnd==0 && !master_style.empty())
1675 fnd=extractSubAttribute(master_style,name,&value);
1677 if(!subattribute.empty())
1686 Svg_parser::randomLetter(){
1688 if(a) return (49 + rand()%9);
1689 else return (65 + rand()%24);
1693 Svg_parser::getRed(String hex){
1695 return hextodec(hex.substr(1,2));
1696 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1697 int start=hex.find_first_of("(")+1;
1698 int end =hex.find_last_of(")");
1699 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1700 return atoi(aux.data());
1705 Svg_parser::getGreen(String hex){
1707 return hextodec(hex.substr(3,2));
1708 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1709 int start=hex.find_first_of("(")+1;
1710 int end =hex.find_last_of(")");
1711 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1712 return atoi(aux.data());
1717 Svg_parser::getBlue(String hex){
1719 return hextodec(hex.substr(5,2));
1720 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1721 int start=hex.find_first_of("(")+1;
1722 int end =hex.find_last_of(")");
1723 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1724 return atoi(aux.data());
1729 Svg_parser::hextodec(String hex){
1738 else if(hex.at(i)=='1')
1740 else if(hex.at(i)=='2')
1742 else if(hex.at(i)=='3')
1744 else if(hex.at(i)=='4')
1746 else if(hex.at(i)=='5')
1748 else if(hex.at(i)=='6')
1750 else if(hex.at(i)=='7')
1752 else if(hex.at(i)=='8')
1754 else if(hex.at(i)=='9')
1756 else if(hex.at(i)=='a')
1758 else if(hex.at(i)=='b')
1760 else if(hex.at(i)=='c')
1762 else if(hex.at(i)=='d')
1764 else if(hex.at(i)=='e')
1766 else if(hex.at(i)=='f')
1774 result+=pow(16,i)*ihex[top-i-1];
1782 Svg_parser::getDimension(const String ac){
1786 int length=ac.size();
1788 if(isdigit(ac.at(length-1))){
1790 }else if(ac.at(length-1)=='%'){
1793 String mtc=ac.substr(length-2,length);
1794 String nmc=ac.substr(0,length-2);
1795 if(mtc.compare("px")==0){
1796 af=atof(nmc.data());
1797 }else if(mtc.compare("pt")==0){
1798 af=atof(nmc.data())*1.25;
1799 }else if(mtc.compare("em")==0){
1800 af=atof(nmc.data())*16;
1801 }else if(mtc.compare("mm")==0){
1802 af=atof(nmc.data())*3.54;
1803 }else if(mtc.compare("pc")==0){
1804 af=atof(nmc.data())*15;
1805 }else if(mtc.compare("cm")==0){
1806 af=atof(nmc.data())*35.43;
1807 }else if(mtc.compare("in")==0){
1808 af=atof(nmc.data())*90;
1817 Svg_parser::newMatrix(Matrix *a){
1819 data=(Matrix*)malloc(sizeof(Matrix));
1820 data->a=a->a; data->b=a->b; data->c=a->c;
1821 data->d=a->d; data->e=a->e; data->f=a->f;
1825 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1827 data=(Matrix*)malloc(sizeof(Matrix));
1828 data->a=a; data->b=b; data->c=c;
1829 data->d=d; data->e=e; data->f=f;
1833 Svg_parser::newMatrix(const String mvector){
1834 if(!mvector.empty()){
1835 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1836 std::vector<String> tokens=tokenize(mvector,",");
1837 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1838 data->a=atof(tokens.at(0).data());
1839 data->b=atof(tokens.at(1).data());
1840 data->c=atof(tokens.at(2).data());
1841 data->d=atof(tokens.at(3).data());
1842 data->e=atof(tokens.at(4).data());
1843 data->f=atof(tokens.at(5).data());
1846 return newMatrix(1,0,0,1,0,0);
1850 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1854 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1855 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1861 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1862 Matrix* aux=newMatrix(0,0,0,0,0,0);
1863 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1864 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1865 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1866 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1867 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1868 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1872 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1873 Matrix* aux=newMatrix(0,0,0,0,0,0);
1874 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1875 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1876 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1877 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1878 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1879 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1888 Svg_parser::matrixIsNull(Matrix *mtx){
1889 if(mtx == NULL) return true;
1894 Svg_parser::getRadian(float sexa){
1895 return (sexa*2*PI)/360;
1898 Svg_parser::removeS(String *input){
1899 for(unsigned int i=0;i<input->size();i++){
1900 if(input->at(i)==' '){
1906 Svg_parser::removeIntoS(String *input){
1908 for(unsigned int i=0;i<input->size();i++){
1909 if(input->at(i)=='('){
1911 }else if(input->at(i)==')'){
1913 }else if(into && input->at(i)==' '){
1919 Svg_parser::tokenize(const String& str,const String& delimiters){
1920 std::vector<String> tokens;
1921 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1922 String::size_type pos = str.find_first_of(delimiters, lastPos);
1923 while (String::npos != pos || String::npos != lastPos){
1924 tokens.push_back(str.substr(lastPos, pos - lastPos));
1925 lastPos = str.find_first_not_of(delimiters, pos);
1926 pos = str.find_first_of(delimiters, lastPos);
1931 Svg_parser::new_guid(){
1933 return GUID::hasher(uid).get_string();