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,"none");
253 String stroke =loadAttribute("stroke",obj_style,parent_style,"none");
254 String stroke_width =loadAttribute("stroke-width",obj_style,parent_style,"1px");
255 String stroke_linecap =loadAttribute("stroke-linecap",obj_style,parent_style,"butt");
256 String stroke_linejoin =loadAttribute("stroke-linejoin",obj_style,parent_style,"miter");
257 String stroke_opacity =loadAttribute("stroke-opacity",obj_style,parent_style,"1");
258 String fill_opacity =loadAttribute("fill-opacity",obj_style,parent_style,"1");
259 String opacity =loadAttribute("opacity",obj_style,parent_style,"1");
263 int typeFill=0; //nothing
265 if(fill.compare("none")!=0){
268 if(typeFill==1 && fill.compare(0,3,"url")==0){
269 typeFill=2; //gradient
272 int typeStroke=0;//nothing
274 if(stroke.compare("none")!=0){
275 typeStroke=1; //simple
277 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
278 typeStroke=2; //gradient
281 xmlpp::Element *root_layer = root;
282 if(typeFill!=0 && typeStroke!=0){
283 root_layer=nodeStartBasicLayer(root->add_child("layer"));
285 /*if ((SVG_CONVERT_PATHS == 1 && typeFill!=0) || (SVG_CONVERT_PATHS == 2 && typeFill!=0 && typeStroke==0)) {
287 if(nodename.compare("rect")==0){
288 rect_simple(nodeElement,root_layer,fill,fill_opacity,opacity);
292 build_url (root_layer->add_child("layer"),fill,mtx);
296 if(nodename.compare("rect")==0){
297 parser_rect(nodeElement,root,parent_style,mtx);
298 }else if(nodename.compare("polygon")==0){
299 parser_polygon(nodeElement,root,parent_style,mtx);
300 }else if(nodename.compare("path")==0){
301 parser_path (nodeElement,root,parent_style,mtx);
308 Svg_parser::rect_simple(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
309 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
310 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
311 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
312 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
313 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
315 xmlpp::Element *child_rect=root->add_child("layer");
316 child_rect->set_attribute("type","rectangle");
317 child_rect->set_attribute("active","true");
318 child_rect->set_attribute("version","0.2");
319 child_rect->set_attribute("desc",rect_id);
321 build_real(child_rect->add_child("param"),"z_depth",0.0);
322 build_real(child_rect->add_child("param"),"amount",1.0);
323 build_integer(child_rect->add_child("param"),"blend_method",0);
324 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
326 float auxx=atof(rect_x.c_str());
327 float auxy=atof(rect_y.c_str());
328 coor2vect(&auxx,&auxy);
329 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
330 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
331 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
332 coor2vect(&auxx,&auxy);
333 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
340 Svg_parser::parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
341 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
342 Glib::ustring rect_style =nodeElement->get_attribute_value("style");
343 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
344 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
345 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
346 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
348 String fill =loadAttribute("fill",rect_style,parent_style,"none");
349 String fill_opacity =loadAttribute("fill-opacity",rect_style,parent_style,"1");
350 String opacity =loadAttribute("opacity",rect_style,parent_style,"1");
352 //it's some complicated
357 if(fill.compare(0,3,"url")==0){
359 root=nodeStartBasicLayer(root->add_child("layer"));
361 xmlpp::Element *child_layer;
363 child_layer=root->add_child("layer");
367 xmlpp::Element *child_rect=child_layer->add_child("layer");
368 child_rect->set_attribute("type","rectangle");
369 child_rect->set_attribute("active","true");
370 child_rect->set_attribute("version","0.2");
371 child_rect->set_attribute("desc",rect_id);
373 build_real(child_rect->add_child("param"),"z_depth",0.0);
374 build_real(child_rect->add_child("param"),"amount",1.0);
375 build_integer(child_rect->add_child("param"),"blend_method",0);
376 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
378 float auxx=atof(rect_x.c_str());
379 float auxy=atof(rect_y.c_str());
380 coor2vect(&auxx,&auxy);
381 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
382 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
383 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
384 coor2vect(&auxx,&auxy);
385 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
387 build_url (child_layer->add_child("layer"),fill,mtx);
392 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
393 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
394 Glib::ustring label =nodeElement->get_attribute_value("label");
395 Glib::ustring style =nodeElement->get_attribute_value("style");
396 Glib::ustring fill =nodeElement->get_attribute_value("fill");
401 }else if(!fill.empty()){
402 layer_style.append("fill:");
403 layer_style.append(fill);
404 }else if(!parent_style.empty()){
405 layer_style=parent_style;
408 root->set_attribute("type","PasteCanvas");
409 root->set_attribute("active","true");
410 root->set_attribute("version","0.1");
411 if(!label.empty()) root->set_attribute("desc",label);
412 else root->set_attribute("desc","unknown layer");
414 build_real(root->add_child("param"),"z_depth",0.0);
415 build_real(root->add_child("param"),"amount",1.0);
416 build_integer(root->add_child("param"),"blend_method",0);
417 build_vector (root->add_child("param"),"origin",0,0);
419 //printf(" canvas attributes ");
421 xmlpp::Element *child_canvas=root->add_child("param");
422 child_canvas->set_attribute("name","canvas");
423 child_canvas=child_canvas->add_child("canvas");
424 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
426 xmlpp::Node::NodeList list = node->get_children();
427 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
428 Glib::ustring name =(*iter)->get_name();
429 parser_graphics (*iter,child_canvas,layer_style,mtx);
435 Svg_parser::parser_polygon(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
436 //load sub-attributes
437 Glib::ustring polygon_style =nodeElement->get_attribute_value("style");
438 Glib::ustring polygon_id =nodeElement->get_attribute_value("id");
439 Glib::ustring polygon_points =nodeElement->get_attribute_value("points");
440 Glib::ustring polygon_fill =nodeElement->get_attribute_value("fill");
441 String fill =loadAttribute("fill",polygon_style,parent_style,polygon_fill,"none");
442 String fill_rule =loadAttribute("fill-rule",polygon_style,parent_style,"evenodd");
443 String fill_opacity =loadAttribute("fill-opacity",polygon_style,parent_style,"1");
444 String opacity =loadAttribute("opacity",polygon_style,parent_style,"1");
448 if(polygon_points.empty())
450 std::list<Vertice*> k;
451 std::vector<String> tokens=get_tokens_path (polygon_points);
453 float ax,ay; ax=ay=0;
454 for(i=0;i<tokens.size();i++){
455 ax=atof(tokens.at(i).data());
456 i++; if(tokens.at(i).compare(",")==0) i++;
457 ay=atof(tokens.at(i).data());
459 if(mtx) transformPoint2D(mtx,&ax,&ay);
463 k.push_back(newVertice(ax,ay));
466 xmlpp::Element *child_polygon;
470 if(fill.compare(0,3,"url")==0){
471 typeFill=2;//gradient
472 root=nodeStartBasicLayer(root->add_child("layer"));
474 child_polygon=root->add_child("layer");
475 child_polygon->set_attribute("type","polygon");
476 child_polygon->set_attribute("active","true");
477 child_polygon->set_attribute("version","0.1");
478 child_polygon->set_attribute("desc",polygon_id);
479 build_param (child_polygon->add_child("param"),"z_depth","real","0.0000000000");
480 build_param (child_polygon->add_child("param"),"amount","real","1.0000000000");
481 build_param (child_polygon->add_child("param"),"blend_method","integer","0");
482 build_color (child_polygon->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
483 build_vector(child_polygon->add_child("param"),"offset",0,0);
484 build_param (child_polygon->add_child("param"),"invert","bool","false");
485 build_param (child_polygon->add_child("param"),"antialias","bool","true");
486 build_param (child_polygon->add_child("param"),"feather","real","0.0000000000");
487 build_param (child_polygon->add_child("param"),"blurtype","integer","1");
488 if(fill_rule.compare("evenodd")==0) build_param (child_polygon->add_child("param"),"winding_style","integer","1");
489 else build_param (child_polygon->add_child("param"),"winding_style","integer","0");
490 build_points (child_polygon->add_child("param"),k);
493 build_url(root->add_child("layer"),fill,mtx);
498 Svg_parser::parser_path(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
499 //load sub-attributes
500 Glib::ustring path_style =nodeElement->get_attribute_value("style");
501 Glib::ustring path_id =nodeElement->get_attribute_value("id");
502 Glib::ustring path_d =nodeElement->get_attribute_value("d");
503 Glib::ustring path_transform =nodeElement->get_attribute_value("transform");
504 Glib::ustring path_fill =nodeElement->get_attribute_value("fill");
506 String fill =loadAttribute("fill",path_style,parent_style,path_fill,"none");
507 String fill_rule =loadAttribute("fill-rule",path_style,parent_style,"evenodd");
508 String stroke =loadAttribute("stroke",path_style,parent_style,"none");
509 String stroke_width =loadAttribute("stroke-width",path_style,parent_style,"1px");
510 String stroke_linecap =loadAttribute("stroke-linecap",path_style,parent_style,"butt");
511 String stroke_linejoin =loadAttribute("stroke-linejoin",path_style,parent_style,"miter");
512 String stroke_opacity =loadAttribute("stroke-opacity",path_style,parent_style,"1");
513 String fill_opacity =loadAttribute("fill-opacity",path_style,parent_style,"1");
514 String opacity =loadAttribute("opacity",path_style,parent_style,"1");
516 //parser path_d attribute, this is obviously important
517 std::list<std::list<Vertice*> > k;
518 k=parser_path_d (path_d,mtx);
521 int typeFill=0; //nothing
523 if(fill.compare("none")!=0){
526 if(typeFill==1 && fill.compare(0,3,"url")==0){
527 typeFill=2; //gradient
530 int typeStroke=0;//nothing
532 if(stroke.compare("none")!=0){
533 typeStroke=1; //simple
535 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
536 typeStroke=2; //gradient
541 if(n!=1){ //if n is > than 1 then we must create a paste canvas for all paths
542 root=nodeStartBasicLayer(root->add_child("layer"));
544 std::list<std::list<Vertice*> >::iterator aux = k.begin();
545 for (; aux!=k.end(); aux++){
546 if(typeFill!=0 && typeStroke!=0){
548 offset_id=new_guid();
550 if(typeFill==1 || typeFill==2){//region layer
551 xmlpp::Element *child_fill=root;
552 if(n==1 && typeFill==2){//open gradient or url (fill)
553 child_fill=nodeStartBasicLayer(root->add_child("layer"));
555 xmlpp::Element *child_region=child_fill->add_child("layer");
556 child_region->set_attribute("type","region");
557 child_region->set_attribute("active","true");
558 child_region->set_attribute("version","0.1");
559 child_region->set_attribute("desc",path_id);
560 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
561 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
562 build_param (child_region->add_child("param"),"blend_method","integer","0");
563 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
564 if(offset_id.empty()) build_vector (child_region->add_child("param"),"offset",0,0);
565 else build_vector (child_region->add_child("param"),"offset",0,0,offset_id);
566 build_param (child_region->add_child("param"),"invert","bool","false");
567 build_param (child_region->add_child("param"),"antialias","bool","true");
568 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
569 build_param (child_region->add_child("param"),"blurtype","integer","1");
570 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
571 else build_param (child_region->add_child("param"),"winding_style","integer","0");
573 build_bline (child_region->add_child("param"),*aux,loop,bline_id);
575 if(n==1 && typeFill==2){ //gradient in onto mode (fill)
576 build_url(child_fill->add_child("layer"),fill,mtx);
580 if(typeStroke==1 || typeStroke==2){ //layer outline
581 xmlpp::Element *child_stroke=root;
582 if(n==1 && typeStroke==2){//open gradient in straigth onto (stroke)
583 child_stroke=nodeStartBasicLayer(root->add_child("layer"));
585 xmlpp::Element *child_outline=child_stroke->add_child("layer");
586 child_outline->set_attribute("type","outline");
587 child_outline->set_attribute("active","true");
588 child_outline->set_attribute("version","0.2");
589 child_outline->set_attribute("desc",path_id);
590 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
591 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
592 build_param (child_outline->add_child("param"),"blend_method","integer","0");
593 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
594 if(offset_id.empty()) build_vector (child_outline->add_child("param"),"offset",0,0);
595 else build_vector (child_outline->add_child("param"),"offset",0,0,offset_id);
596 build_param (child_outline->add_child("param"),"invert","bool","false");
597 build_param (child_outline->add_child("param"),"antialias","bool","true");
598 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
599 build_param (child_outline->add_child("param"),"blurtype","integer","1");
601 build_param (child_outline->add_child("param"),"winding_style","integer","0");
603 build_bline (child_outline->add_child("param"),*aux,loop,bline_id);
605 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
606 build_param (child_outline->add_child("param"),"width","real",stroke_width);
607 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
608 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
609 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
610 if(stroke_linecap.compare("butt")==0){
611 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
612 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
614 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
615 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
617 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
618 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
620 if(n==1 && typeStroke==2){ //gradient in onto mode (stroke)
621 build_url(child_stroke->add_child("layer"),stroke,mtx);
625 if(n!=1){//only fill for several canvas in one path
627 build_url(root->add_child("layer"),fill,mtx);
633 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
634 std::vector<String> tokens;
639 while(i<path.size()){
642 case 0: //initial state
643 if(a=='m'){ e=1; i++;}
644 else if(a=='c'){ e= 2; i++;}
645 else if(a=='q'){ e= 3; i++;}
646 else if(a=='t'){ e= 4; i++;}
647 else if(a=='a'){ e= 5; i++;}
648 else if(a=='l'){ e= 6; i++;}
649 else if(a=='v'){ e= 7; i++;}
650 else if(a=='h'){ e= 8; i++;}
651 else if(a=='M'){ e= 9; i++;}
652 else if(a=='C'){ e=10; i++;}
653 else if(a=='Q'){ e=11; i++;}
654 else if(a=='T'){ e=12; i++;}
655 else if(a=='A'){ e=13; i++;}
656 else if(a=='L'){ e=14; i++;}
657 else if(a=='V'){ e=15; i++;}
658 else if(a=='H'){ e=16; i++;}
659 else if(a=='z' || a=='Z'){ e=17; i++;}
660 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
661 else if(a==','){ e=19; i++;}
662 else if(a==' '){i++;}
665 case 1 : tokens.push_back("m"); e=0; break;//move
666 case 2 : tokens.push_back("c"); e=0; break;//curve
667 case 3 : tokens.push_back("q"); e=0; break;//quadratic
668 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
669 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
670 case 6 : tokens.push_back("l"); e=0; break;//line to
671 case 7 : tokens.push_back("v"); e=0; break;//vertical
672 case 8 : tokens.push_back("h"); e=0; break;//horizontal
674 case 9 : tokens.push_back("M"); e=0; break;
675 case 10: tokens.push_back("C"); e=0; break;
676 case 11: tokens.push_back("Q"); e=0; break;
677 case 12: tokens.push_back("T"); e=0; break;
678 case 13: tokens.push_back("A"); e=0; break;
679 case 14: tokens.push_back("L"); e=0; break;
680 case 15: tokens.push_back("V"); e=0; break;
681 case 16: tokens.push_back("H"); e=0; break;
683 case 17: tokens.push_back("z"); e=0; break;//loop
684 case 18: if(a=='-'||a=='.'|| isdigit (a)){
685 buffer.append(path.substr(i,1));i++;
690 case 19: tokens.push_back(","); e=0; break;
691 case 20: tokens.push_back(buffer);
697 switch(e){//last element
698 case 1 : tokens.push_back("m"); break;
699 case 2 : tokens.push_back("c"); break;
700 case 3 : tokens.push_back("q"); break;
701 case 4 : tokens.push_back("t"); break;
702 case 5 : tokens.push_back("a"); break;
703 case 6 : tokens.push_back("l"); break;
704 case 7 : tokens.push_back("v"); break;
705 case 8 : tokens.push_back("h"); break;
706 case 9 : tokens.push_back("M"); break;
707 case 10: tokens.push_back("C"); break;
708 case 11: tokens.push_back("Q"); break;
709 case 12: tokens.push_back("T"); break;
710 case 13: tokens.push_back("A"); break;
711 case 14: tokens.push_back("L"); break;
712 case 15: tokens.push_back("V"); break;
713 case 16: tokens.push_back("H"); break;
714 case 17: tokens.push_back("z"); break;
715 case 18: tokens.push_back(buffer); break;
716 case 19: tokens.push_back(","); break;
717 case 20: tokens.push_back(buffer); break;
723 std::list<std::list<Vertice*> >
724 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
725 std::list<std::list<Vertice*> > k;
726 std::list<Vertice*> k1;
727 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
728 float actual_x=0,actual_y=0;//for relative methods;
731 std::vector<String> tokens=get_tokens_path(path_d);
732 for(i=0;i<tokens.size();i++){
733 if(tokens.at(i).compare("M")==0){//absolute move to
738 i++; ax=atof(tokens.at(i).data());
739 i++; if(tokens.at(i).compare(",")==0) i++;
740 ay=atof(tokens.at(i).data());
744 if(mtx) transformPoint2D(mtx,&ax,&ay);
746 k1.push_back(newVertice (ax,ay)); //first element
747 setSplit(k1.back(),TRUE);
748 }else if(tokens.at(i).compare("C")==0){ //absolute curve
750 i++; tgx2=atof(tokens.at(i).data());
751 i++; if(tokens.at(i).compare(",")==0) i++;
752 tgy2=atof(tokens.at(i).data());
754 i++; tgx=atof(tokens.at(i).data());
755 i++; if(tokens.at(i).compare(",")==0) i++;
756 tgy=atof(tokens.at(i).data());
758 i++; ax=atof(tokens.at(i).data());
759 i++; if(tokens.at(i).compare(",")==0) i++;
760 ay=atof(tokens.at(i).data());
765 transformPoint2D(mtx,&tgx2,&tgy2);
766 transformPoint2D(mtx,&ax,&ay);
767 transformPoint2D(mtx,&tgx,&tgy);
770 coor2vect(&tgx2,&tgy2);
772 coor2vect(&tgx,&tgy);
774 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
775 if(isFirst(k1.front(),ax,ay)){
776 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
778 k1.push_back(newVertice (ax,ay));
779 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
780 setSplit(k1.back(),TRUE);
782 }else if(tokens.at(i).compare("Q")==0){ //absolute quadractic curve
784 i++; tgx=ax=atof(tokens.at(i).data());
785 i++; if(tokens.at(i).compare(",")==0) i++;
786 tgy=ay=atof(tokens.at(i).data());
788 i++; ax=atof(tokens.at(i).data());
789 i++; if(tokens.at(i).compare(",")==0) i++;
790 ay=atof(tokens.at(i).data());
795 transformPoint2D(mtx,&ax,&ay);
796 transformPoint2D(mtx,&tgx,&tgy);
800 coor2vect(&tgx,&tgy);
802 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
803 setSplit(k1.back(),FALSE);
804 k1.push_back(newVertice (ax,ay));
805 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
806 }else if(tokens.at(i).compare("L")==0){ //absolute line to
808 i++; ax=atof(tokens.at(i).data());
809 i++; if(tokens.at(i).compare(",")==0) i++;
810 ay=atof(tokens.at(i).data());
814 if(mtx) transformPoint2D(mtx,&ax,&ay);
818 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
819 if(isFirst(k1.front(),ax,ay)){
820 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
822 k1.push_back(newVertice(ax,ay));
823 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
825 }else if(tokens.at(i).compare("l")==0){//relative line to
827 i++; ax=atof(tokens.at(i).data());
828 i++; if(tokens.at(i).compare(",")==0) i++;
829 ay=atof(tokens.at(i).data());
836 if(mtx) transformPoint2D(mtx,&ax,&ay);
840 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
841 if(isFirst(k1.front(),ax,ay)){
842 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
844 k1.push_back(newVertice(ax,ay));
845 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
847 }else if(tokens.at(i).compare("H")==0){//absolute horizontal move
848 //the same that L but only Horizontal movement
850 i++; ax=atof(tokens.at(i).data());
855 if(mtx) transformPoint2D(mtx,&ax,&ay);
859 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
860 if(isFirst(k1.front(),ax,ay)){
861 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
863 k1.push_back(newVertice(ax,ay));
864 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
866 }else if(tokens.at(i).compare("h")==0){//horizontal relative
867 i++; ax=atof(tokens.at(i).data());
873 if(mtx) transformPoint2D(mtx,&ax,&ay);
877 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
878 if(isFirst(k1.front(),ax,ay)){
879 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
881 k1.push_back(newVertice(ax,ay));
882 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
884 }else if(tokens.at(i).compare("V")==0){//vertical absolute
886 i++; ay=atof(tokens.at(i).data());
891 if(mtx) transformPoint2D(mtx,&ax,&ay);
895 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
896 if(isFirst(k1.front(),ax,ay)){
897 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
899 k1.push_back(newVertice(ax,ay));
900 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
902 }else if(tokens.at(i).compare("v")==0){//relative
904 i++; ay=atof(tokens.at(i).data());
910 if(mtx) transformPoint2D(mtx,&ax,&ay);
914 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
915 if(isFirst(k1.front(),ax,ay)){
916 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
918 k1.push_back(newVertice(ax,ay));
919 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
921 }else if(tokens.at(i).compare("T")==0){// I don't know what does it
922 }else if(tokens.at(i).compare("A")==0){//elliptic arc
924 //isn't complete support, is only for circles
926 //this curve have 6 parameters
928 float radio_x,radio_y;
932 i++; radio_x=atof(tokens.at(i).data());
933 i++; if(tokens.at(i).compare(",")==0) i++;
934 radio_y=atof(tokens.at(i).data());
936 i++; angle=atof(tokens.at(i).data());
938 i++; large=atoi(tokens.at(i).data());
939 i++; sweep=atoi(tokens.at(i).data());
941 i++; ax=atof(tokens.at(i).data());
942 i++; if(tokens.at(i).compare(",")==0) i++;
943 ay=atof(tokens.at(i).data());
945 if(!large && !sweep){
947 tgx2 = actual_x + radio_x*0.5;
950 tgy = ay + radio_y*0.5;
955 transformPoint2D(mtx,&tgx2,&tgy2);
956 transformPoint2D(mtx,&ax,&ay);
957 transformPoint2D(mtx,&tgx,&tgy);
960 coor2vect(&tgx2,&tgy2);
962 coor2vect(&tgx,&tgy);
964 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
965 if(isFirst(k1.front(),ax,ay)){
966 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
968 k1.push_back(newVertice (ax,ay));
969 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
970 setSplit(k1.back(),TRUE);
972 }else if(!large && sweep){
975 tgy2 = actual_y + radio_y*0.5;
976 tgx = ax + radio_x*0.5;
982 transformPoint2D(mtx,&tgx2,&tgy2);
983 transformPoint2D(mtx,&ax,&ay);
984 transformPoint2D(mtx,&tgx,&tgy);
987 coor2vect(&tgx2,&tgy2);
989 coor2vect(&tgx,&tgy);
991 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
992 if(isFirst(k1.front(),ax,ay)){
993 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
995 k1.push_back(newVertice (ax,ay));
996 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
997 setSplit(k1.back(),TRUE);
999 }else if( large && !sweep){//rare
1000 //this need more than one vertex
1001 }else if( large && sweep){//circles in inkscape are made with this kind of arc
1002 if(actual_y==ay){//circles
1003 //intermediate point
1005 if(actual_x>ax) sense =-1;
1006 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
1007 in_x = (actual_x+ax)/2;
1008 in_y = actual_y - sense*radio_y;
1009 in_tgx1 = in_x - sense*(radio_x*0.5);
1010 in_tgx2 = in_x + sense*(radio_x*0.5);
1015 tgy2=ay - sense*(radio_y*0.5);
1017 tgy =ay - sense*(radio_y*0.5);
1023 transformPoint2D(mtx,&tgx2,&tgy2);
1024 transformPoint2D(mtx,&tgx ,&tgy );
1025 transformPoint2D(mtx,&ax,&ay);
1027 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
1028 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
1029 transformPoint2D(mtx,&in_x,&in_y);
1032 coor2vect(&tgx2 , &tgy2);
1033 coor2vect(&ax , &ay );
1034 coor2vect(&tgx , &tgy );
1036 coor2vect(&in_tgx2 , &in_tgy2);
1037 coor2vect(&in_tgx1 , &in_tgy1);
1038 coor2vect(&in_x , &in_y );
1041 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
1042 //save the intermediate point
1043 k1.push_back(newVertice (in_x,in_y));
1044 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
1045 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
1046 setSplit(k1.back(),TRUE); //this could be changed
1047 //save the new point
1048 if(isFirst(k1.front(),ax,ay)){
1049 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
1051 k1.push_back(newVertice (ax,ay));
1052 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
1053 setSplit(k1.back(),TRUE);
1057 }else if(tokens.at(i).compare("z")==0){
1060 std::cout<<"don't supported: "<<tokens.at(i)<<std::endl;
1064 k.push_front(k1); //last element
1069 Svg_parser::parser_defs(const xmlpp::Node* node){
1070 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1072 xmlpp::Node::NodeList list = node->get_children();
1073 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1074 Glib::ustring name =(*iter)->get_name();
1075 if(name.compare("linearGradient")==0){
1076 parser_linearGradient(*iter);
1077 }else if(name.compare("radialGradient")==0){
1078 parser_radialGradient(*iter);
1084 Svg_parser::AdjustPointUrl(){
1086 if(!lg.empty()){//linealgradient
1087 std::list<LinearGradient*>::iterator aux=lg.begin();
1088 while(aux!=lg.end()){
1089 LinearGradient* auxlg=*aux;
1090 coor2vect (&auxlg->x1,&auxlg->y1);
1091 coor2vect (&auxlg->x2,&auxlg->y2);
1095 if(!rg.empty()){//radialgradient
1096 std::list<RadialGradient*>::iterator aux=rg.begin();
1097 while(aux!=rg.end()){
1098 RadialGradient* auxrg=*aux;
1099 coor2vect (&auxrg->cx,&auxrg->cy);
1100 auxrg->r= auxrg->r/kux;
1106 std::list<ColorStop*>*
1107 Svg_parser::find_colorStop(String name){
1109 if(lg.empty()&& rg.empty())
1113 if(find.at(0)=='#') find.erase(0,1);
1115 std::list<LinearGradient*>::iterator aux=lg.begin();
1116 while(aux!=lg.end()){//only find into linear gradients
1117 if(find.compare((*aux)->name)==0){
1118 return (*aux)->stops;
1126 Svg_parser::build_url(xmlpp::Element* root, String name,Matrix *mtx){
1128 if(lg.empty()&& rg.empty())
1129 root->get_parent()->remove_child(root);
1131 int start=name.find_first_of("#")+1;
1132 int end=name.find_first_of(")");
1133 String find= name.substr(start,end-start);
1134 bool encounter=false;
1136 std::list<LinearGradient*>::iterator aux=lg.begin();
1137 while(aux!=lg.end()){
1138 if(find.compare((*aux)->name)==0){
1139 build_linearGradient (root,*aux,mtx);
1145 if(!encounter && !rg.empty()){
1146 std::list<RadialGradient*>::iterator aux=rg.begin();
1147 while(aux!=rg.end()){
1148 if(find.compare((*aux)->name)==0){
1149 build_radialGradient (root,*aux,mtx);
1156 root->get_parent()->remove_child(root);
1158 root->get_parent()->remove_child(root);
1162 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
1163 std::list<ColorStop*>::iterator aux_stop=stops->begin();
1164 while(aux_stop!=stops->end()){
1165 xmlpp::Element *child=root->add_child("color");
1166 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
1167 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
1168 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
1169 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
1170 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
1175 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
1177 root->set_attribute("type","linear_gradient");
1178 root->set_attribute("active","true");
1179 root->set_attribute("desc","Gradient004");
1180 build_param (root->add_child("param"),"z_depth","real","0");
1181 build_param (root->add_child("param"),"amount","real","1");
1183 build_param (root->add_child("param"),"blend_method","integer","21");
1190 transformPoint2D(mtx,&x1,&y1);
1191 transformPoint2D(mtx,&x2,&y2);
1193 coor2vect (&x1,&y1);
1194 coor2vect (&x2,&y2);
1196 build_vector (root->add_child("param"),"p1",x1,y1);
1197 build_vector (root->add_child("param"),"p2",x2,y2);
1199 xmlpp::Element *child=root->add_child("param");
1200 child->set_attribute("name","gradient");
1201 build_stop_color (child->add_child("gradient"),data->stops);
1202 build_param (root->add_child("param"),"loop","bool","false");
1203 build_param (root->add_child("param"),"zigzag","bool","false");
1207 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1210 root->set_attribute("type","radial_gradient");
1211 root->set_attribute("active","true");
1212 build_param (root->add_child("param"),"z_depth","real","0");
1213 build_param (root->add_child("param"),"amount","real","1");
1215 build_param (root->add_child("param"),"blend_method","integer","21");
1217 xmlpp::Element *child=root->add_child("param");
1218 child->set_attribute("name","gradient");
1219 build_stop_color (child->add_child("gradient"),data->stops);
1220 //here the center point and radio
1226 transformPoint2D(mtx,&cx,&cy);
1229 coor2vect (&cx,&cy);
1231 build_vector (root->add_child("param"),"center",cx,cy);
1232 build_param (root->add_child("param"),"radius","real",r);
1234 build_param (root->add_child("param"),"loop","bool","false");
1235 build_param (root->add_child("param"),"zigzag","bool","false");
1240 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1241 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1242 Glib::ustring id =nodeElement->get_attribute_value("id");
1243 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1244 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1245 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1246 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1247 Glib::ustring link =nodeElement->get_attribute_value("href");
1249 std::list<ColorStop*> *stops;
1251 stops=find_colorStop (link);
1254 stops=new std::list<ColorStop*>();
1255 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1257 xmlpp::Node::NodeList list = node->get_children();
1258 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1259 Glib::ustring name =(*iter)->get_name();
1260 if(name.compare("stop")==0){
1261 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1262 Glib::ustring style =nodeIter->get_attribute_value("style");
1263 float offset=atof(nodeIter->get_attribute_value("offset").data());
1267 extractSubAttribute (style,"stop-color",&stop_color);
1268 extractSubAttribute (style,"stop-opacity",&opacity);
1270 if(opacity.empty()) opacity="1";
1271 if(stop_color.empty()) stop_color="#000000";//black for default :S
1272 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1278 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops));
1283 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1284 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1285 Glib::ustring id =nodeElement->get_attribute_value("id");
1286 float cx =atof(nodeElement->get_attribute_value("cx").data());
1287 float cy =atof(nodeElement->get_attribute_value("cy").data());
1288 float r =atof(nodeElement->get_attribute_value("r").data());
1289 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1290 std::list<ColorStop*> *stops=NULL;
1292 //inkscape always use link, i dont need parser stops here, but it's posible
1293 stops=find_colorStop (link);
1296 rg.push_back(newRadialGradient(id,cx,cy,r,stops));
1301 Svg_parser::newColorStop(String color,float opacity,float pos){
1303 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1304 float r=getRed(color);
1305 float g=getGreen(color);
1306 float b=getBlue(color);
1308 Color ret=adjustGamma(r/255,g/255,b/255,a);
1309 _stop->r=ret.get_r();
1310 _stop->g=ret.get_g();
1311 _stop->b=ret.get_b();
1312 _stop->a=ret.get_a();
1317 Svg_parser::adjustGamma(float r,float g,float b,float a){
1319 if(gamma.get_gamma_r()!=1.0){
1321 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1323 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1325 if(gamma.get_gamma_g()!=1.0){
1327 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1329 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1331 if(gamma.get_gamma_b()!=1.0){
1333 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1335 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1341 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops){
1342 LinearGradient* data;
1343 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1344 sprintf(data->name,"%s",name.data());
1354 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops){
1355 RadialGradient* data;
1356 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1357 sprintf(data->name,"%s",name.data());
1367 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1368 root->set_attribute("type","colorcorrect");
1369 root->set_attribute("active","true");
1370 root->set_attribute("version","0.1");
1371 root->set_attribute("desc","Gamma");
1372 build_real (root->add_child("param"),"gamma",gamma);
1375 Svg_parser::build_transform(const String transform){
1377 String tf(transform);
1379 std::vector<String> tokens=tokenize(tf," ");
1380 std::vector<String>::iterator aux=tokens.begin();
1381 while(aux!=tokens.end()){
1382 if((*aux).compare(0,9,"translate")==0){
1385 start =(*aux).find_first_of("(")+1;
1386 end =(*aux).find_first_of(",");
1387 dx =atof((*aux).substr(start,end-start).data());
1388 start =(*aux).find_first_of(",")+1;
1389 end =(*aux).size()-1;
1390 dy =atof((*aux).substr(start,end-start).data());
1392 a=newMatrix(1,0,0,1,dx,dy);
1394 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1395 }else if((*aux).compare(0,5,"scale")==0){
1397 a=newMatrix(1,0,0,1,0,0);
1398 }else if((*aux).compare(0,6,"rotate")==0){
1399 float angle,seno,coseno;
1401 start =(*aux).find_first_of("(")+1;
1402 end =(*aux).size()-1;
1403 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1407 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1409 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1410 }else if((*aux).compare(0,6,"matrix")==0){
1411 int start =(*aux).find_first_of('(')+1;
1412 int end =(*aux).find_first_of(')');
1414 a=newMatrix((*aux).substr(start,end-start));
1416 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1418 a=newMatrix(1,0,0,1,0,0);
1426 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1427 root->set_attribute("type","translate");
1428 root->set_attribute("active","true");
1429 root->set_attribute("version","0.1");
1430 build_vector (root->add_child("param"),"origin",dx,dy);
1433 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1434 root->set_attribute("type","rotate");
1435 root->set_attribute("active","true");
1436 root->set_attribute("version","0.1");
1437 build_vector (root->add_child("param"),"origin",dx,dy);
1438 build_real (root->add_child("param"),"amount",angle);
1441 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1442 root->set_attribute("name","vector_list");
1443 xmlpp::Element *child=root->add_child("dynamic_list");
1444 child->set_attribute("type","vector");
1445 std::list<Vertice*>::iterator aux = p.begin();
1446 while(aux!=p.end()){
1447 xmlpp::Element *child_entry=child->add_child("entry");
1448 xmlpp::Element *child_vector=child_entry->add_child("vector");
1449 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1450 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1455 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1456 xmlpp::Element *child_comp=root->add_child("composite");
1457 child_comp->set_attribute("type","bline_point");
1458 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1459 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1460 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1461 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1462 else build_param (child_comp->add_child("split"),"","bool","false");
1464 xmlpp::Element *child_t1=child_comp->add_child("t1");
1465 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1466 child_rc->set_attribute("type","vector");
1467 build_param (child_rc->add_child("radius"),"","real",p->radio1);
1468 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1470 xmlpp::Element *child_t2=child_comp->add_child("t2");
1471 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1472 child_rc2->set_attribute("type","vector");
1473 build_param (child_rc2->add_child("radius"),"","real",p->radio2);
1474 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1478 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1479 root->set_attribute("name","bline");
1480 xmlpp::Element *child=root->add_child("bline");
1481 child->set_attribute("type","bline_point");
1483 child->set_attribute("loop","true");
1485 child->set_attribute("loop","false");
1486 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1487 std::list<Vertice*>::iterator aux = p.begin();
1488 while(aux!=p.end()){
1489 if(*aux) build_vertice (child->add_child("entry"),*aux);
1495 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1496 if(!type.empty() && !value.empty()){
1497 if(!name.empty()) root->set_attribute("name",name);
1498 xmlpp::Element *child=root->add_child(type);
1499 child->set_attribute("value",value);
1501 root->get_parent()->remove_child(root);
1505 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1507 if(!name.empty()) root->set_attribute("name",name);
1508 xmlpp::Element *child=root->add_child(type);
1509 child->set_attribute("value",etl::strprintf ("%f",value));
1511 root->get_parent()->remove_child(root);
1515 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1517 if(!name.empty()) root->set_attribute("name",name);
1518 xmlpp::Element *child=root->add_child(type);
1519 char *enteroc=new char[10];
1520 sprintf(enteroc,"%d",value);
1521 child->set_attribute("value",enteroc);
1524 root->get_parent()->remove_child(root);
1529 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1530 if(name.compare("")!=0) root->set_attribute("name",name);
1531 xmlpp::Element *child=root->add_child("integer");
1532 char *enteroc=new char[10];
1533 sprintf(enteroc,"%d",value);
1534 child->set_attribute("value",enteroc);
1537 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1538 if(name.compare("")!=0) root->set_attribute("name",name);
1539 xmlpp::Element *child=root->add_child("real");
1540 char *realc=new char[20];
1541 sprintf(realc,"%f",value);
1542 child->set_attribute("value",realc);
1546 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1547 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1548 root->get_parent()->remove_child(root);
1549 printf("Color aborted\n");
1552 Color ret=adjustGamma(r/255,g/255,b/255,a);
1554 root->set_attribute("name","color");
1555 xmlpp::Element *child=root->add_child("color");
1556 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1557 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1558 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1559 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1562 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1564 if(name.compare("")!=0) root->set_attribute("name",name);
1565 xmlpp::Element *child=root->add_child("vector");
1566 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1567 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1571 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1572 if(name.compare("")!=0) root->set_attribute("name",name);
1573 xmlpp::Element *child=root->add_child("vector");
1574 if(!guid.empty()) child->set_attribute("guid",guid);
1575 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1576 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1580 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1581 root->set_attribute("type","PasteCanvas");
1582 root->set_attribute("active","true");
1583 root->set_attribute("version","0.1");
1584 root->set_attribute("desc","Composite");
1585 build_param (root->add_child("param"),"z_depth","real","0");
1586 build_param (root->add_child("param"),"amount","real","1");
1587 build_param (root->add_child("param"),"blend_method","integer","0");
1588 build_vector (root->add_child("param"),"origin",0,0);
1589 xmlpp::Element *child=root->add_child("param");
1590 child->set_attribute("name","canvas");
1591 return child->add_child("canvas");
1596 Svg_parser::coor2vect(float *x,float *y){
1600 sy= atof(height.c_str())-sy;
1609 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1611 float d1x,d1y,d2x,d2y,dx,dy;
1622 rd=sqrt(dx*dx + dy*dy);
1624 ag=PI + atan(dy/dx);
1625 }else if(dx>0 && dy<0){
1626 ag=PI + atan(dy/dx);
1627 }else if(dx<0 && dy<0){
1629 }else if(dx<0 && dy>0){
1630 ag= 2*PI+atan(dy/dx);
1631 }else if(dx==0 && dy>0){
1633 }else if(dx==0 && dy<0){
1635 }else if(dx==0 && dy==0){
1637 }else if(dx<0 && dy==0){
1639 }else if(dx>0 && dy==0){
1647 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1649 float d1x,d1y,d2x,d2y,dx,dy;
1661 rd=sqrt(dx*dx + dy*dy);
1663 ag=PI + atan(dy/dx);
1664 // printf("case 180-270\n");
1665 }else if(dx>0 && dy<0){
1666 ag=PI + atan(dy/dx);
1667 // printf("case 90-180\n");
1668 }else if(dx<0 && dy<0){
1670 // printf("case 0-90\n");
1671 }else if(dx<0 && dy>0){
1672 ag= 2*PI+atan(dy/dx);
1673 // printf("case 270-360\n");
1674 }else if(dx==0 && dy>0){
1676 }else if(dx==0 && dy<0){
1678 }else if(dx==0 && dy==0){
1680 }else if(dx<0 && dy==0){
1682 }else if(dx>0 && dy==0){
1692 Svg_parser::setSplit(Vertice* p,bool val){
1698 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1699 if(nodo->x==a && nodo->y==b)
1705 Svg_parser::newVertice(float x,float y){
1707 vert=(Vertice*)malloc(sizeof(Vertice));
1710 vert->radio1=vert->radio2=vert->angle1=vert->angle2=0;
1715 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1717 if(!attribute.empty()){
1718 String str(attribute);
1720 std::vector<String> tokens=tokenize(str,";");
1721 std::vector<String>::iterator aux=tokens.begin();
1722 while(aux!=tokens.end()){
1723 int mid= (*aux).find_first_of(":");
1724 if((*aux).substr(0,mid).compare(name)==0){
1725 int end=(*aux).size();
1726 *value=(*aux).substr(mid+1,end-mid);
1735 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1738 if(!path_style.empty())
1739 fnd=extractSubAttribute(path_style,name,&value);
1741 if(!master_style.empty())
1742 fnd=extractSubAttribute(master_style,name,&value);
1749 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1752 if(!path_style.empty())
1753 fnd=extractSubAttribute(path_style,name,&value);
1754 if(fnd==0 && !master_style.empty())
1755 fnd=extractSubAttribute(master_style,name,&value);
1757 if(!subattribute.empty())
1766 Svg_parser::randomLetter(){
1768 if(a) return (49 + rand()%9);
1769 else return (65 + rand()%24);
1773 Svg_parser::getRed(String hex){
1775 return hextodec(hex.substr(1,2));
1776 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1777 int start=hex.find_first_of("(")+1;
1778 int end =hex.find_last_of(")");
1779 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1780 return atoi(aux.data());
1785 Svg_parser::getGreen(String hex){
1787 return hextodec(hex.substr(3,2));
1788 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1789 int start=hex.find_first_of("(")+1;
1790 int end =hex.find_last_of(")");
1791 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1792 return atoi(aux.data());
1797 Svg_parser::getBlue(String hex){
1799 return hextodec(hex.substr(5,2));
1800 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1801 int start=hex.find_first_of("(")+1;
1802 int end =hex.find_last_of(")");
1803 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1804 return atoi(aux.data());
1809 Svg_parser::hextodec(String hex){
1818 else if(hex.at(i)=='1')
1820 else if(hex.at(i)=='2')
1822 else if(hex.at(i)=='3')
1824 else if(hex.at(i)=='4')
1826 else if(hex.at(i)=='5')
1828 else if(hex.at(i)=='6')
1830 else if(hex.at(i)=='7')
1832 else if(hex.at(i)=='8')
1834 else if(hex.at(i)=='9')
1836 else if(hex.at(i)=='a')
1838 else if(hex.at(i)=='b')
1840 else if(hex.at(i)=='c')
1842 else if(hex.at(i)=='d')
1844 else if(hex.at(i)=='e')
1846 else if(hex.at(i)=='f')
1854 result+=pow(16,i)*ihex[top-i-1];
1862 Svg_parser::getDimension(const String ac){
1866 int length=ac.size();
1868 if(isdigit(ac.at(length-1))){
1870 }else if(ac.at(length-1)=='%'){
1873 String mtc=ac.substr(length-2,length);
1874 String nmc=ac.substr(0,length-2);
1875 if(mtc.compare("px")==0){
1876 af=atof(nmc.data());
1877 }else if(mtc.compare("pt")==0){
1878 af=atof(nmc.data())*1.25;
1879 }else if(mtc.compare("em")==0){
1880 af=atof(nmc.data())*16;
1881 }else if(mtc.compare("mm")==0){
1882 af=atof(nmc.data())*3.54;
1883 }else if(mtc.compare("pc")==0){
1884 af=atof(nmc.data())*15;
1885 }else if(mtc.compare("cm")==0){
1886 af=atof(nmc.data())*35.43;
1887 }else if(mtc.compare("in")==0){
1888 af=atof(nmc.data())*90;
1897 Svg_parser::newMatrix(Matrix *a){
1899 data=(Matrix*)malloc(sizeof(Matrix));
1900 data->a=a->a; data->b=a->b; data->c=a->c;
1901 data->d=a->d; data->e=a->e; data->f=a->f;
1905 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1907 data=(Matrix*)malloc(sizeof(Matrix));
1908 data->a=a; data->b=b; data->c=c;
1909 data->d=d; data->e=e; data->f=f;
1913 Svg_parser::newMatrix(const String mvector){
1914 if(!mvector.empty()){
1915 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1916 std::vector<String> tokens=tokenize(mvector,",");
1917 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1918 data->a=atof(tokens.at(0).data());
1919 data->b=atof(tokens.at(1).data());
1920 data->c=atof(tokens.at(2).data());
1921 data->d=atof(tokens.at(3).data());
1922 data->e=atof(tokens.at(4).data());
1923 data->f=atof(tokens.at(5).data());
1926 return newMatrix(1,0,0,1,0,0);
1930 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1934 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1935 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1941 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1942 Matrix* aux=newMatrix(0,0,0,0,0,0);
1943 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1944 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1945 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1946 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1947 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1948 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1952 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1953 Matrix* aux=newMatrix(0,0,0,0,0,0);
1954 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1955 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1956 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1957 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1958 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1959 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1968 Svg_parser::matrixIsNull(Matrix *mtx){
1969 if(mtx == NULL) return true;
1974 Svg_parser::getRadian(float sexa){
1975 return (sexa*2*PI)/360;
1978 Svg_parser::removeS(String *input){
1979 for(unsigned int i=0;i<input->size();i++){
1980 if(input->at(i)==' '){
1986 Svg_parser::removeIntoS(String *input){
1988 for(unsigned int i=0;i<input->size();i++){
1989 if(input->at(i)=='('){
1991 }else if(input->at(i)==')'){
1993 }else if(into && input->at(i)==' '){
1999 Svg_parser::tokenize(const String& str,const String& delimiters){
2000 std::vector<String> tokens;
2001 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
2002 String::size_type pos = str.find_first_of(delimiters, lastPos);
2003 while (String::npos != pos || String::npos != lastPos){
2004 tokens.push_back(str.substr(lastPos, pos - lastPos));
2005 lastPos = str.find_first_not_of(delimiters, pos);
2006 pos = str.find_first_of(delimiters, lastPos);
2011 Svg_parser::new_guid(){
2013 return GUID::hasher(uid).get_string();