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
13 ** Copyright (c) 2009 Nikita Kitaev
15 ** This package is free software; you can redistribute it and/or
16 ** modify it under the terms of the GNU General Public License as
17 ** published by the Free Software Foundation; either version 2 of
18 ** the License, or (at your option) any later version.
20 ** This package is distributed in the hope that it will be useful,
21 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ** General Public License for more details.
26 /* ========================================================================= */
28 /* === H E A D E R S ======================================================= */
35 #include "svg_parser.h"
37 /* === U S I N G =========================================================== */
39 using namespace synfig;
41 /* === G L O B A L S ======================================================= */
45 //Seperate transformations: apply transformations on a per-layer basis, rather than on canvases
46 #define SVG_SEP_TRANSFORMS 1
48 //Resolve BLine transformations: resolve transformations instead of creating transformation layers
49 #define SVG_RESOLVE_BLINE 1
51 /* === P R O C E D U R E S ================================================= */
54 synfig::open_svg(std::string _filepath,String &errors, String &warnings){
55 Canvas::Handle canvas;
59 canvas=parser.load_svg_canvas(_filepath,errors,warnings);
60 //canvas->set_id(parser.get_id());
62 std::cout<<"error"<<std::endl;
68 Svg_parser::load_svg_canvas(std::string _filepath,String &errors, String &warnings){
70 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
72 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
74 parser.set_substitute_entities();
75 parser.parse_file(filepath);
78 const xmlpp::Node* pNode = parser.get_document()->get_root_node();
81 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
82 }catch(const std::exception& ex){
83 std::cout << "Exception caught: " << ex.what() << std::endl;
85 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
86 Canvas::Handle canvas;
88 //canvas=synfig::open_canvas(nodeRoot,_filepath,errors,warnings);
89 canvas=synfig::open_canvas(nodeRoot,errors,warnings);
94 Svg_parser::Svg_parser(){
97 set_canvas=0;//we must run parser_canvas method
98 // 0.5 in gamma parameter of color correct layer is 1/0.5 = 2 (thinking) it must be 2.2!!!!
103 Svg_parser::get_id(){
104 if(!id_name.empty()) return id_name;
108 Svg_parser::set_id(String source){
109 const char bad_chars[]=" :#@$^&()*";
110 int start= source.find_last_of('/')+1;
111 int end= source.find_last_of('.');
112 String x=source.substr(start,end-start);
114 for(unsigned int i=0;i<sizeof(bad_chars);i++){
115 unsigned int pos=x.find_first_of(bad_chars[i]);
116 if(pos!=String::npos)
123 id_name="id_arbitrario";
130 /* === PARSERS ============================================================= */
133 Svg_parser::parser_node(const xmlpp::Node* node){
134 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
135 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
136 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
138 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
141 Glib::ustring nodename = node->get_name();
142 if(!nodeText && !nodeComment && !nodename.empty()){
143 if(nodename.compare("svg")==0){
145 }else if(nodename.compare("namedview")==0){
147 }else if(nodename.compare("defs")==0){
150 if(set_canvas==0) parser_canvas (node);
151 parser_graphics(node,nodeRoot,"",NULL);
152 if(nodename.compare("g")==0) return;
156 xmlpp::Node::NodeList list = node->get_children();
157 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
158 parser_node(*iter); //recursive
165 Svg_parser::parser_svg (const xmlpp::Node* node){
166 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
167 width =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("width")));
168 height =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("height")));
169 docname=nodeElement->get_attribute_value("docname","");
173 Svg_parser::parser_canvas (const xmlpp::Node* node){
174 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
175 if(width.compare("")==0){
176 width=nodeElement->get_attribute_value("width","");
178 if(height.compare("")==0){
179 height=nodeElement->get_attribute_value("height","");
181 if(width.compare("")==0 && height.compare("")!=0){
184 if(width.compare("")!=0 && height.compare("")==0){
187 if(height.compare("")==0 && width.compare("")==0){
192 nodeRoot=document.create_root_node("canvas", "", "");
193 nodeRoot->set_attribute("version","0.5");
194 nodeRoot->set_attribute("width",width);
195 nodeRoot->set_attribute("height",height);
196 nodeRoot->set_attribute("xres","2834.645752");
197 nodeRoot->set_attribute("yres","2834.645752");
200 view_x=atof(width.c_str())/kux;
201 view_y=atof(height.c_str())/kux;
204 char attr_view_box[60];
205 sprintf(attr_view_box,"%f %f %f %f",-1.0*view_x,view_y,view_x,-1.0*view_y);
206 nodeRoot->set_attribute("view-box",attr_view_box);
207 ox=atof(width.c_str() )/2;
208 oy=atof(height.c_str())/2;
209 nodeRoot->set_attribute("antialias","1");
210 nodeRoot->set_attribute("fps","24.000");
211 nodeRoot->set_attribute("begin-time","0f");
212 nodeRoot->set_attribute("end-time","5s");
213 nodeRoot->set_attribute("bgcolor","0.500000 0.500000 0.500000 1.000000");
214 if(!id_name.empty()) nodeRoot->add_child("name")->set_child_text(id_name);
215 else nodeRoot->add_child("name")->set_child_text("Synfig Animation 1");
221 Svg_parser::parser_graphics(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
222 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
223 Glib::ustring nodename = node->get_name();
224 if (nodename.compare("g")==0 || nodename.compare("path")==0 || nodename.compare("polygon")==0 || nodename.compare("rect")==0){} else return;
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=parser_transform (transform);
234 if (SVG_SEP_TRANSFORMS)
238 composeMatrix(&mtx,mtx_parent,mtx);
240 mtx=newMatrix(mtx_parent);
243 if(nodename.compare("g")==0){
244 parser_layer (node,root->add_child("layer"),parent_style,mtx);
248 Glib::ustring obj_style =nodeElement->get_attribute_value("style");
249 Glib::ustring obj_fill =nodeElement->get_attribute_value("fill");
252 String fill =loadAttribute("fill",obj_style,parent_style,obj_fill,"none");
253 String fill_rule =loadAttribute("fill-rule",obj_style,parent_style,"evenodd");
254 String stroke =loadAttribute("stroke",obj_style,parent_style,"none");
255 String stroke_width =loadAttribute("stroke-width",obj_style,parent_style,"1px");
256 String stroke_linecap =loadAttribute("stroke-linecap",obj_style,parent_style,"butt");
257 String stroke_linejoin =loadAttribute("stroke-linejoin",obj_style,parent_style,"miter");
258 String stroke_opacity =loadAttribute("stroke-opacity",obj_style,parent_style,"1");
259 String fill_opacity =loadAttribute("fill-opacity",obj_style,parent_style,"1");
260 String opacity =loadAttribute("opacity",obj_style,parent_style,"1");
264 int typeFill=0; //nothing
266 if(fill.compare("none")!=0){
269 if(typeFill==1 && fill.compare(0,3,"url")==0){
270 typeFill=2; //gradient
273 int typeStroke=0;//nothing
275 if(stroke.compare("none")!=0){
276 typeStroke=1; //simple
278 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
279 typeStroke=2; //gradient
282 xmlpp::Element* child_layer = nodeStartBasicLayer(root->add_child("layer"));
283 xmlpp::Element* child_fill = child_layer;
284 xmlpp::Element* child_stroke = child_layer;
286 child_fill=nodeStartBasicLayer(child_fill->add_child("layer"));
289 child_stroke=nodeStartBasicLayer(child_stroke->add_child("layer"));
293 if(nodename.compare("rect")==0 && typeFill!=0){
294 parser_rect(nodeElement,child_fill,fill,fill_opacity,opacity);
296 build_fill (child_fill,fill,NULL);
298 parser_effects(nodeElement,child_layer,parent_style,mtx);
303 //=======================================================================
304 std::list<std::list<Vertice*> > k;
305 //if we are creating a bline
307 //First, create the list of vertices
308 if (SVG_RESOLVE_BLINE) {
309 if(nodename.compare("path")==0){
310 k=parser_path_d (nodeElement->get_attribute_value("d"),mtx);
311 } else if(nodename.compare("polygon")==0){
312 k=parser_path_polygon (nodeElement->get_attribute_value("points"),mtx);
315 if(nodename.compare("path")==0){
316 k=parser_path_d (nodeElement->get_attribute_value("d"),NULL);
317 } else if(nodename.compare("polygon")==0){
318 k=parser_path_polygon (nodeElement->get_attribute_value("points"),NULL);
325 for (int i=0;i<n;i++){
326 bline_id[i]=new_guid();
327 offset_id[i]=new_guid();
330 std::list<std::list<Vertice*> >::iterator aux = k.begin();
331 if(typeFill!=0){//region layer
332 for (int i=0; aux!=k.end(); aux++){
333 xmlpp::Element *child_region=child_fill->add_child("layer");
334 child_region->set_attribute("type","region");
335 child_region->set_attribute("active","true");
336 child_region->set_attribute("version","0.1");
337 child_region->set_attribute("desc",id);
338 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
339 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
340 build_param (child_region->add_child("param"),"blend_method","integer","0");
341 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
342 build_vector (child_region->add_child("param"),"offset",0,0,offset_id[i]);
343 build_param (child_region->add_child("param"),"invert","bool","false");
344 build_param (child_region->add_child("param"),"antialias","bool","true");
345 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
346 build_param (child_region->add_child("param"),"blurtype","integer","1");
347 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
348 else build_param (child_region->add_child("param"),"winding_style","integer","0");
350 build_bline (child_region->add_child("param"),*aux,loop,bline_id[i]);
354 if(typeFill==2){ //gradient in onto mode (fill)
355 if (SVG_RESOLVE_BLINE)
356 build_fill(child_fill,fill,mtx);
358 build_fill(child_fill,fill,NULL);
361 if(typeStroke!=0){//outline layer
363 for (aux=k.begin(); aux!=k.end(); aux++){
364 xmlpp::Element *child_outline=child_stroke->add_child("layer");
365 child_outline->set_attribute("type","outline");
366 child_outline->set_attribute("active","true");
367 child_outline->set_attribute("version","0.2");
368 child_outline->set_attribute("desc",id);
369 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
370 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
371 build_param (child_outline->add_child("param"),"blend_method","integer","0");
372 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
373 build_vector (child_outline->add_child("param"),"offset",0,0,offset_id[i]);
374 build_param (child_outline->add_child("param"),"invert","bool","false");
375 build_param (child_outline->add_child("param"),"antialias","bool","true");
376 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
377 build_param (child_outline->add_child("param"),"blurtype","integer","1");
379 build_param (child_outline->add_child("param"),"winding_style","integer","0");
381 build_bline (child_outline->add_child("param"),*aux,loop,bline_id[i]);
383 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
384 build_param (child_outline->add_child("param"),"width","real",stroke_width);
385 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
386 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
387 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
388 if(stroke_linecap.compare("butt")==0){
389 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
390 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
392 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
393 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
395 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
396 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
402 if(typeStroke==2){ //gradient in onto mode (stroke)
403 if (SVG_RESOLVE_BLINE)
404 build_fill(child_stroke,fill,mtx);
406 build_fill(child_stroke,fill,NULL);
408 if (SVG_RESOLVE_BLINE)
409 parser_effects(nodeElement,child_layer,parent_style,NULL);
411 parser_effects(nodeElement,child_layer,parent_style,mtx);
417 /* === LAYER PARSERS ======================================================= */
420 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
421 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
422 Glib::ustring label =nodeElement->get_attribute_value("label");
423 Glib::ustring style =nodeElement->get_attribute_value("style");
424 Glib::ustring fill =nodeElement->get_attribute_value("fill");
429 }else if(!fill.empty()){
430 layer_style.append("fill:");
431 layer_style.append(fill);
432 }else if(!parent_style.empty()){
433 layer_style=parent_style;
436 root->set_attribute("type","PasteCanvas");
437 root->set_attribute("active","true");
438 root->set_attribute("version","0.1");
439 if(!label.empty()) root->set_attribute("desc",label);
440 else root->set_attribute("desc","Inline Canvas");
442 build_real(root->add_child("param"),"z_depth",0.0);
443 build_real(root->add_child("param"),"amount",1.0);
444 build_integer(root->add_child("param"),"blend_method",0);
445 build_vector (root->add_child("param"),"origin",0,0);
447 //printf(" canvas attributes ");
449 xmlpp::Element *child_canvas=root->add_child("param");
450 child_canvas->set_attribute("name","canvas");
451 child_canvas=child_canvas->add_child("canvas");
452 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
454 xmlpp::Node::NodeList list = node->get_children();
455 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
456 Glib::ustring name =(*iter)->get_name();
457 parser_graphics (*iter,child_canvas,layer_style,mtx);
460 if (SVG_SEP_TRANSFORMS) parser_effects(nodeElement,child_canvas,parent_style,NULL);
461 else parser_effects(nodeElement,child_canvas,parent_style,mtx);
466 Svg_parser::parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
467 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
468 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
469 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
470 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
471 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
473 xmlpp::Element *child_rect=root->add_child("layer");
474 child_rect->set_attribute("type","rectangle");
475 child_rect->set_attribute("active","true");
476 child_rect->set_attribute("version","0.2");
477 child_rect->set_attribute("desc",rect_id);
479 build_real(child_rect->add_child("param"),"z_depth",0.0);
480 build_real(child_rect->add_child("param"),"amount",1.0);
481 build_integer(child_rect->add_child("param"),"blend_method",0);
482 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
484 float auxx=atof(rect_x.c_str());
485 float auxy=atof(rect_y.c_str());
486 coor2vect(&auxx,&auxy);
487 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
488 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
489 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
490 coor2vect(&auxx,&auxy);
491 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
496 /* === CONVERT TO PATH PARSERS ============================================= */
498 std::list<std::list<Vertice*> >
499 Svg_parser::parser_path_polygon(Glib::ustring polygon_points, Matrix* mtx){
500 std::list<std::list<Vertice*> > k0;
501 if(polygon_points.empty())
503 std::list<Vertice*> k;
504 std::vector<String> tokens=get_tokens_path (polygon_points);
506 float ax,ay; ax=ay=0;
507 for(i=0;i<tokens.size();i++){
508 ax=atof(tokens.at(i).data());
509 i++; if(tokens.at(i).compare(",")==0) i++;
510 ay=atof(tokens.at(i).data());
512 if(mtx) transformPoint2D(mtx,&ax,&ay);
516 k.push_back(newVertice(ax,ay));
522 std::list<std::list<Vertice*> >
523 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
524 std::list<std::list<Vertice*> > k;
525 std::list<Vertice*> k1;
526 std::vector<String> tokens=get_tokens_path(path_d);
527 String command="M"; //the current command
528 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
530 float actual_x=0,actual_y=0; //in svg coordinate space
531 float old_x=0,old_y=0; //needed in rare cases
532 float init_x=0,init_y=0; //for closepath commands
535 for(unsigned int i=0;i<tokens.size();i++){
536 //if the token is a command, change the current command
537 if(tokens.at(i).compare("M")==0 || tokens.at(i).compare("m")==0 || tokens.at(i).compare("L")==0 || tokens.at(i).compare("l")==0 || tokens.at(i).compare("H")==0 || tokens.at(i).compare("h")==0 || tokens.at(i).compare("V")==0 || tokens.at(i).compare("v")==0 || tokens.at(i).compare("C")==0 || tokens.at(i).compare("c")==0 || tokens.at(i).compare("S")==0 || tokens.at(i).compare("s")==0 || tokens.at(i).compare("Q")==0 || tokens.at(i).compare("q")==0 || tokens.at(i).compare("T")==0 || tokens.at(i).compare("t")==0 || tokens.at(i).compare("A")==0 || tokens.at(i).compare("a")==0 || tokens.at(i).compare("z")==0) {
538 command=tokens.at(i);
544 //if command is absolute, set actual_x/y to zero
545 if(command.compare("M")==0 || command.compare("L")==0 || command.compare("C")==0 || command.compare("S")==0 || command.compare("Q")==0 || command.compare("T")==0 || command.compare("A")==0 || command.compare("H")==0 || command.compare("V")==0) {
550 //now parse the commands
551 if(command.compare("M")==0 || command.compare("m")==0){ //move to
556 actual_x+=atof(tokens.at(i).data());
557 i++; if(tokens.at(i).compare(",")==0) i++;
558 actual_y+=atof(tokens.at(i).data());
565 if(mtx) transformPoint2D(mtx,&ax,&ay);
567 k1.push_back(newVertice (ax,ay)); //first element
568 setSplit(k1.back(),TRUE);
569 //"If a moveto is followed by multiple pairs of coordinates,
570 // the subsequent pairs are treated as implicit lineto commands."
571 if (command.compare("M")==0)
575 }else if(command.compare("C")==0 || command.compare("c")==0){ //curve
577 tgx2=actual_x+atof(tokens.at(i).data());
578 i++; if(tokens.at(i).compare(",")==0) i++;
579 tgy2=actual_y+atof(tokens.at(i).data());
581 i++; tgx=actual_x+atof(tokens.at(i).data());
582 i++; if(tokens.at(i).compare(",")==0) i++;
583 tgy=actual_y+atof(tokens.at(i).data());
585 i++; actual_x+=atof(tokens.at(i).data());
586 i++; if(tokens.at(i).compare(",")==0) i++;
587 actual_y+=atof(tokens.at(i).data());
593 transformPoint2D(mtx,&tgx2,&tgy2);
594 transformPoint2D(mtx,&ax,&ay);
595 transformPoint2D(mtx,&tgx,&tgy);
598 coor2vect(&tgx2,&tgy2);
600 coor2vect(&tgx,&tgy);
602 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
603 if(isFirst(k1.front(),ax,ay)){
604 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
606 k1.push_back(newVertice (ax,ay));
607 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
608 setSplit(k1.back(),TRUE);
610 }else if(command.compare("Q")==0 || command.compare("q")==0){ //quadractic curve
612 tgx=actual_x+atof(tokens.at(i).data());
613 i++; if(tokens.at(i).compare(",")==0) i++;
614 tgy=actual_y+atof(tokens.at(i).data());
616 i++; actual_x+=atof(tokens.at(i).data());
617 i++; if(tokens.at(i).compare(",")==0) i++;
618 actual_y+=atof(tokens.at(i).data());
624 transformPoint2D(mtx,&ax,&ay);
625 transformPoint2D(mtx,&tgx,&tgy);
629 coor2vect(&tgx,&tgy);
631 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
632 setSplit(k1.back(),FALSE);
633 k1.push_back(newVertice (ax,ay));
634 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
635 }else if(command.compare("L")==0 || command.compare("l")==0){ //line to
637 actual_x+=atof(tokens.at(i).data());
638 i++; if(tokens.at(i).compare(",")==0) i++;
639 actual_y+=atof(tokens.at(i).data());
644 if(mtx) transformPoint2D(mtx,&ax,&ay);
648 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
649 if(isFirst(k1.front(),ax,ay)){
650 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
652 k1.push_back(newVertice(ax,ay));
653 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
655 }else if(command.compare("H")==0 || command.compare("h")==0){// horizontal move
656 //the same that L but only Horizontal movement
658 actual_x+=atof(tokens.at(i).data());
663 if(mtx) transformPoint2D(mtx,&ax,&ay);
667 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
668 if(isFirst(k1.front(),ax,ay)){
669 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
671 k1.push_back(newVertice(ax,ay));
672 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
674 }else if(command.compare("V")==0 || command.compare("v")==0){//vertical
676 actual_y+=atof(tokens.at(i).data());
681 if(mtx) transformPoint2D(mtx,&ax,&ay);
685 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
686 if(isFirst(k1.front(),ax,ay)){
687 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
689 k1.push_back(newVertice(ax,ay));
690 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
692 }else if(command.compare("T")==0 || command.compare("t")==0){// I don't know what does it
693 actual_x+=atof(tokens.at(i).data());
694 i++; if(tokens.at(i).compare(",")==0) i++;
695 actual_y+=atof(tokens.at(i).data());
696 }else if(command.compare("A")==0 || command.compare("a")==0){//elliptic arc
698 //isn't complete support, is only for circles
700 //this curve have 6 parameters
702 float radius_x,radius_y;
706 radius_x=atof(tokens.at(i).data());
707 i++; if(tokens.at(i).compare(",")==0) i++;
708 radius_y=atof(tokens.at(i).data());
710 i++; angle=atof(tokens.at(i).data());
712 i++; large=atoi(tokens.at(i).data());
713 i++; sweep=atoi(tokens.at(i).data());
715 i++; actual_x+=atof(tokens.at(i).data());
716 i++; if(tokens.at(i).compare(",")==0) i++;
717 actual_y+=atof(tokens.at(i).data());
719 if(!large && !sweep){
721 tgx2 = old_x + radius_x*0.5;
724 tgy = actual_y + radius_y*0.5;
730 transformPoint2D(mtx,&tgx2,&tgy2);
731 transformPoint2D(mtx,&ax,&ay);
732 transformPoint2D(mtx,&tgx,&tgy);
735 coor2vect(&tgx2,&tgy2);
737 coor2vect(&tgx,&tgy);
739 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
740 if(isFirst(k1.front(),ax,ay)){
741 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
743 k1.push_back(newVertice (ax,ay));
744 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
745 setSplit(k1.back(),TRUE);
747 }else if(!large && sweep){
750 tgy2 = old_y + radius_y*0.5;
751 tgx = actual_x + radius_x*0.5;
758 transformPoint2D(mtx,&tgx2,&tgy2);
759 transformPoint2D(mtx,&ax,&ay);
760 transformPoint2D(mtx,&tgx,&tgy);
763 coor2vect(&tgx2,&tgy2);
765 coor2vect(&tgx,&tgy);
767 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
768 if(isFirst(k1.front(),ax,ay)){
769 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
771 k1.push_back(newVertice (ax,ay));
772 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
773 setSplit(k1.back(),TRUE);
775 }else if( large && !sweep){//rare
776 //this need more than one vertex
777 }else if( large && sweep){//circles in inkscape are made with this kind of arc
780 if(old_x>actual_x) sense =-1;
781 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
782 in_x = (old_x+actual_x)/2;
783 in_y = old_y - sense*radius_y;
784 in_tgx1 = in_x - sense*(radius_x*0.5);
785 in_tgx2 = in_x + sense*(radius_x*0.5);
790 tgy2=actual_y - sense*(radius_y*0.5);
792 tgy =actual_y - sense*(radius_y*0.5);
798 transformPoint2D(mtx,&tgx2,&tgy2);
799 transformPoint2D(mtx,&tgx ,&tgy );
800 transformPoint2D(mtx,&ax,&ay);
802 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
803 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
804 transformPoint2D(mtx,&in_x,&in_y);
807 coor2vect(&tgx2 , &tgy2);
808 coor2vect(&ax , &ay );
809 coor2vect(&tgx , &tgy );
811 coor2vect(&in_tgx2 , &in_tgy2);
812 coor2vect(&in_tgx1 , &in_tgy1);
813 coor2vect(&in_x , &in_y );
816 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
817 //save the intermediate point
818 k1.push_back(newVertice (in_x,in_y));
819 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
820 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
821 setSplit(k1.back(),TRUE); //this could be changed
823 if(isFirst(k1.front(),ax,ay)){
824 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
826 k1.push_back(newVertice (ax,ay));
827 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
828 setSplit(k1.back(),TRUE);
831 }else if(command.compare("z")==0){
836 if (i<tokens.size() && tokens.at(i).compare("M")!=0 && tokens.at(i).compare("m")!=0) {
837 //starting a new path, but not with a moveto
843 if(mtx) transformPoint2D(mtx,&ax,&ay);
845 k1.push_back(newVertice (ax,ay)); //first element
846 setSplit(k1.back(),TRUE);
848 i--; //decrement i to balance "i++" at command change
850 std::cout<<"unsupported path token: "<<tokens.at(i)<<std::endl;
854 k.push_front(k1); //last element
858 /* === EFFECTS PARSERS ===================================================== */
861 Svg_parser::parser_effects(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
862 build_transform(root, mtx);
865 /* === DEFS PARSERS ======================================================== */
868 Svg_parser::parser_defs(const xmlpp::Node* node){
869 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
871 xmlpp::Node::NodeList list = node->get_children();
872 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
873 Glib::ustring name =(*iter)->get_name();
874 if(name.compare("linearGradient")==0){
875 parser_linearGradient(*iter);
876 }else if(name.compare("radialGradient")==0){
877 parser_radialGradient(*iter);
883 /* === BUILDS ============================================================== */
886 Svg_parser::build_transform(xmlpp::Element* root,Matrix* mtx){
888 xmlpp::Element *child_transform=root->add_child("layer");
889 child_transform->set_attribute("type","warp");
890 child_transform->set_attribute("active","true");
891 child_transform->set_attribute("version","0.1");
892 child_transform->set_attribute("desc","Transform");
895 x=100;y=100;coor2vect(&x,&y);
896 build_vector (child_transform->add_child("param"),"src_tl",x,y);
898 x=200;y=200;coor2vect(&x,&y);
899 build_vector (child_transform->add_child("param"),"src_br",x,y);
903 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
904 build_vector (child_transform->add_child("param"),"dest_tl",x,y);
907 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
908 build_vector (child_transform->add_child("param"),"dest_tr",x,y);
911 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
912 build_vector (child_transform->add_child("param"),"dest_br",x,y);
915 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
916 build_vector (child_transform->add_child("param"),"dest_bl",x,y);
918 build_param (child_transform->add_child("param"),"clip","bool","false");
919 build_param (child_transform->add_child("param"),"horizon","real","4.0");
923 std::list<ColorStop*>*
924 Svg_parser::find_colorStop(String name){
926 if(lg.empty()&& rg.empty())
930 if(find.at(0)=='#') find.erase(0,1);
932 std::list<LinearGradient*>::iterator aux=lg.begin();
933 while(aux!=lg.end()){//only find into linear gradients
934 if(find.compare((*aux)->name)==0){
935 return (*aux)->stops;
944 Svg_parser::build_fill(xmlpp::Element* root, String name,Matrix *mtx){
946 int start=name.find_first_of("#")+1;
947 int end=name.find_first_of(")");
948 String find= name.substr(start,end-start);
949 bool encounter=false;
951 std::list<LinearGradient*>::iterator aux=lg.begin();
952 while(aux!=lg.end()){
953 if(find.compare((*aux)->name)==0){
954 build_linearGradient (root,*aux,mtx);
960 if(!encounter && !rg.empty()){
961 std::list<RadialGradient*>::iterator aux=rg.begin();
962 while(aux!=rg.end()){
963 if(find.compare((*aux)->name)==0){
964 build_radialGradient (root,*aux,mtx);
973 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
974 std::list<ColorStop*>::iterator aux_stop=stops->begin();
975 while(aux_stop!=stops->end()){
976 xmlpp::Element *child=root->add_child("color");
977 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
978 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
979 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
980 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
981 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
986 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
988 xmlpp::Element* gradient=root->add_child("layer");
990 gradient->set_attribute("type","linear_gradient");
991 gradient->set_attribute("active","true");
992 gradient->set_attribute("desc","Gradient004");
993 build_param (gradient->add_child("param"),"z_depth","real","0");
994 build_param (gradient->add_child("param"),"amount","real","1");
996 build_param (gradient->add_child("param"),"blend_method","integer","21");
1004 if (mtx || data->transform){
1006 if (mtx && data->transform){
1007 composeMatrix(&mtx2,mtx,data->transform);
1010 }else if (data->transform){
1011 mtx2=data->transform;
1013 //matrix transforms the gradient as a whole
1014 //it does not preserve angles, so we cant' simply transform both points
1016 //set point (x3,y3) on the same gradient line as (x2,y2)
1017 //the gradient line is perpendicular to (x1,y1)(x2,y2)
1020 //transform everything
1021 transformPoint2D(mtx2,&x1,&y1);
1022 transformPoint2D(mtx2,&x2,&y2);
1023 transformPoint2D(mtx2,&x3,&y3);
1025 if (x2!=x3 && y2!=y3) {//divide by zero check
1027 //set k as slope between (x2,y2) and (x3,y3)
1028 //k is the slope of gradient lines post-transformation
1030 //set point (x2,y2) on the gradient line passing through (x3,y3)
1031 //so that the line (x1,y1)(x2,y2) is perpendicular to (x2,y2)(x3,y3)
1032 x2= (x3*k+x1/k+y1-y3)/(k+(1/k));
1034 } else if (x2==x3 && y2!=y3) {
1036 } else if (x2!=x3 && y2==y3) {
1039 std::cout<<"SVG Import warning: gradient points equal each other"<<std::endl;
1043 coor2vect (&x1,&y1);
1044 coor2vect (&x2,&y2);
1046 build_vector (gradient->add_child("param"),"p1",x1,y1);
1047 build_vector (gradient->add_child("param"),"p2",x2,y2);
1049 xmlpp::Element *child_stops=gradient->add_child("param");
1050 child_stops->set_attribute("name","gradient");
1051 build_stop_color (child_stops->add_child("gradient"),data->stops);
1052 build_param (gradient->add_child("param"),"loop","bool","false");
1053 build_param (gradient->add_child("param"),"zigzag","bool","false");
1057 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1059 xmlpp::Element* gradient;
1061 if (mtx || data->transform) {
1062 xmlpp::Element* layer=root->add_child("layer");
1064 layer->set_attribute("type","PasteCanvas");
1065 layer->set_attribute("active","true");
1066 layer->set_attribute("version","0.1");
1067 layer->set_attribute("desc","Composite");
1068 build_param (layer->add_child("param"),"z_depth","real","0");
1069 build_param (layer->add_child("param"),"amount","real","1");
1070 build_param (layer->add_child("param"),"blend_method","integer","21"); //straight onto
1071 build_vector (layer->add_child("param"),"origin",0,0);
1072 xmlpp::Element *child=layer->add_child("param");
1073 child->set_attribute("name","canvas");
1074 xmlpp::Element* child_layer=child->add_child("canvas");
1076 gradient=child_layer->add_child("layer");
1077 build_param (gradient->add_child("param"),"blend_method","integer","0"); //composite
1079 if (mtx && data->transform){
1080 composeMatrix(&mtx2,mtx,data->transform);
1083 }else if (data->transform){
1084 mtx2=data->transform;
1086 build_transform(child_layer,mtx2);
1089 gradient=root->add_child("layer");
1090 build_param (gradient->add_child("param"),"blend_method","integer","21"); //straight onto
1093 gradient->set_attribute("type","radial_gradient");
1094 gradient->set_attribute("active","true");
1095 build_param (gradient->add_child("param"),"z_depth","real","0");
1096 build_param (gradient->add_child("param"),"amount","real","1");
1098 xmlpp::Element *child_stops=gradient->add_child("param");
1099 child_stops->set_attribute("name","gradient");
1100 build_stop_color (child_stops->add_child("gradient"),data->stops);
1102 //here the center point and radius
1108 coor2vect (&cx,&cy);
1110 build_vector (gradient->add_child("param"),"center",cx,cy);
1111 build_param (gradient->add_child("param"),"radius","real",r);
1113 build_param (gradient->add_child("param"),"loop","bool","false");
1114 build_param (gradient->add_child("param"),"zigzag","bool","false");
1119 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1120 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1121 Glib::ustring id =nodeElement->get_attribute_value("id");
1122 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1123 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1124 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1125 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1126 Glib::ustring link =nodeElement->get_attribute_value("href");
1127 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1129 //resolve transformations
1131 if(!transform.empty())
1132 mtx=parser_transform (transform);
1134 std::list<ColorStop*> *stops;
1136 stops=find_colorStop (link);
1139 stops=new std::list<ColorStop*>();
1140 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1142 xmlpp::Node::NodeList list = node->get_children();
1143 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1144 Glib::ustring name =(*iter)->get_name();
1145 if(name.compare("stop")==0){
1146 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1147 Glib::ustring style =nodeIter->get_attribute_value("style");
1148 float offset=atof(nodeIter->get_attribute_value("offset").data());
1152 extractSubAttribute (style,"stop-color",&stop_color);
1153 extractSubAttribute (style,"stop-opacity",&opacity);
1155 if(opacity.empty()) opacity="1";
1156 if(stop_color.empty()) stop_color="#000000";//black for default :S
1157 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1163 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops,mtx));
1168 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1169 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1170 Glib::ustring id =nodeElement->get_attribute_value("id");
1171 float cx =atof(nodeElement->get_attribute_value("cx").data());
1172 float cy =atof(nodeElement->get_attribute_value("cy").data());
1173 float fx =atof(nodeElement->get_attribute_value("fx").data());
1174 float fy =atof(nodeElement->get_attribute_value("fy").data());
1175 float r =atof(nodeElement->get_attribute_value("r").data());
1176 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1177 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1179 if (cx!=fx || cy!=fy)
1180 std::cout<<"SVG Parser: ignoring focus attributes for radial gradient";
1182 //resolve transformations
1184 if(!transform.empty())
1185 mtx=parser_transform (transform);
1187 std::list<ColorStop*> *stops=NULL;
1189 //inkscape always use link, i dont need parser stops here, but it's posible
1190 stops=find_colorStop (link);
1193 rg.push_back(newRadialGradient(id,cx,cy,r,stops,mtx));
1198 Svg_parser::newColorStop(String color,float opacity,float pos){
1200 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1201 float r=getRed(color);
1202 float g=getGreen(color);
1203 float b=getBlue(color);
1205 Color ret=adjustGamma(r/255,g/255,b/255,a);
1206 _stop->r=ret.get_r();
1207 _stop->g=ret.get_g();
1208 _stop->b=ret.get_b();
1209 _stop->a=ret.get_a();
1214 Svg_parser::adjustGamma(float r,float g,float b,float a){
1216 if(gamma.get_gamma_r()!=1.0){
1218 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1220 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1222 if(gamma.get_gamma_g()!=1.0){
1224 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1226 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1228 if(gamma.get_gamma_b()!=1.0){
1230 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1232 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1238 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform){
1239 LinearGradient* data;
1240 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1241 sprintf(data->name,"%s",name.data());
1247 data->transform=transform;
1252 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform){
1253 RadialGradient* data;
1254 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1255 sprintf(data->name,"%s",name.data());
1260 data->transform=transform;
1265 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1266 root->set_attribute("type","colorcorrect");
1267 root->set_attribute("active","true");
1268 root->set_attribute("version","0.1");
1269 root->set_attribute("desc","Gamma");
1270 build_real (root->add_child("param"),"gamma",gamma);
1274 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1275 root->set_attribute("type","translate");
1276 root->set_attribute("active","true");
1277 root->set_attribute("version","0.1");
1278 build_vector (root->add_child("param"),"origin",dx,dy);
1281 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1282 root->set_attribute("type","rotate");
1283 root->set_attribute("active","true");
1284 root->set_attribute("version","0.1");
1285 build_vector (root->add_child("param"),"origin",dx,dy);
1286 build_real (root->add_child("param"),"amount",angle);
1289 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1290 root->set_attribute("name","vector_list");
1291 xmlpp::Element *child=root->add_child("dynamic_list");
1292 child->set_attribute("type","vector");
1293 std::list<Vertice*>::iterator aux = p.begin();
1294 while(aux!=p.end()){
1295 xmlpp::Element *child_entry=child->add_child("entry");
1296 xmlpp::Element *child_vector=child_entry->add_child("vector");
1297 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1298 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1303 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1304 xmlpp::Element *child_comp=root->add_child("composite");
1305 child_comp->set_attribute("type","bline_point");
1306 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1307 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1308 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1309 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1310 else build_param (child_comp->add_child("split"),"","bool","false");
1312 xmlpp::Element *child_t1=child_comp->add_child("t1");
1313 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1314 child_rc->set_attribute("type","vector");
1315 build_param (child_rc->add_child("radius"),"","real",p->radius1);
1316 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1318 xmlpp::Element *child_t2=child_comp->add_child("t2");
1319 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1320 child_rc2->set_attribute("type","vector");
1321 build_param (child_rc2->add_child("radius"),"","real",p->radius2);
1322 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1326 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1327 root->set_attribute("name","bline");
1328 xmlpp::Element *child=root->add_child("bline");
1329 child->set_attribute("type","bline_point");
1331 child->set_attribute("loop","true");
1333 child->set_attribute("loop","false");
1334 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1335 std::list<Vertice*>::iterator aux = p.begin();
1336 while(aux!=p.end()){
1337 if(*aux) build_vertice (child->add_child("entry"),*aux);
1343 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1344 if(!type.empty() && !value.empty()){
1345 if(!name.empty()) root->set_attribute("name",name);
1346 xmlpp::Element *child=root->add_child(type);
1347 child->set_attribute("value",value);
1349 root->get_parent()->remove_child(root);
1353 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1355 if(!name.empty()) root->set_attribute("name",name);
1356 xmlpp::Element *child=root->add_child(type);
1357 child->set_attribute("value",etl::strprintf ("%f",value));
1359 root->get_parent()->remove_child(root);
1363 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1365 if(!name.empty()) root->set_attribute("name",name);
1366 xmlpp::Element *child=root->add_child(type);
1367 char *enteroc=new char[10];
1368 sprintf(enteroc,"%d",value);
1369 child->set_attribute("value",enteroc);
1372 root->get_parent()->remove_child(root);
1377 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1378 if(name.compare("")!=0) root->set_attribute("name",name);
1379 xmlpp::Element *child=root->add_child("integer");
1380 char *enteroc=new char[10];
1381 sprintf(enteroc,"%d",value);
1382 child->set_attribute("value",enteroc);
1385 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1386 if(name.compare("")!=0) root->set_attribute("name",name);
1387 xmlpp::Element *child=root->add_child("real");
1388 char *realc=new char[20];
1389 sprintf(realc,"%f",value);
1390 child->set_attribute("value",realc);
1394 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1395 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1396 root->get_parent()->remove_child(root);
1397 printf("Color aborted\n");
1400 Color ret=adjustGamma(r/255,g/255,b/255,a);
1402 root->set_attribute("name","color");
1403 xmlpp::Element *child=root->add_child("color");
1404 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1405 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1406 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1407 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1410 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1412 if(name.compare("")!=0) root->set_attribute("name",name);
1413 xmlpp::Element *child=root->add_child("vector");
1414 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1415 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1419 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1420 if(name.compare("")!=0) root->set_attribute("name",name);
1421 xmlpp::Element *child=root->add_child("vector");
1422 if(!guid.empty()) child->set_attribute("guid",guid);
1423 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1424 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1428 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root){
1429 root->set_attribute("type","PasteCanvas");
1430 root->set_attribute("active","true");
1431 root->set_attribute("version","0.1");
1432 root->set_attribute("desc","Composite");
1433 build_param (root->add_child("param"),"z_depth","real","0");
1434 build_param (root->add_child("param"),"amount","real","1");
1435 build_param (root->add_child("param"),"blend_method","integer","0");
1436 build_vector (root->add_child("param"),"origin",0,0);
1437 xmlpp::Element *child=root->add_child("param");
1438 child->set_attribute("name","canvas");
1439 return child->add_child("canvas");
1442 /* === COORDINATES & TRANSFORMATIONS ======================================= */
1444 Svg_parser::coor2vect(float *x,float *y){
1448 sy= atof(height.c_str())-sy;
1457 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1459 float d1x,d1y,d2x,d2y,dx,dy;
1470 rd=sqrt(dx*dx + dy*dy);
1472 ag=PI + atan(dy/dx);
1473 }else if(dx>0 && dy<0){
1474 ag=PI + atan(dy/dx);
1475 }else if(dx<0 && dy<0){
1477 }else if(dx<0 && dy>0){
1478 ag= 2*PI+atan(dy/dx);
1479 }else if(dx==0 && dy>0){
1481 }else if(dx==0 && dy<0){
1483 }else if(dx==0 && dy==0){
1485 }else if(dx<0 && dy==0){
1487 }else if(dx>0 && dy==0){
1495 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1497 float d1x,d1y,d2x,d2y,dx,dy;
1509 rd=sqrt(dx*dx + dy*dy);
1511 ag=PI + atan(dy/dx);
1512 // printf("case 180-270\n");
1513 }else if(dx>0 && dy<0){
1514 ag=PI + atan(dy/dx);
1515 // printf("case 90-180\n");
1516 }else if(dx<0 && dy<0){
1518 // printf("case 0-90\n");
1519 }else if(dx<0 && dy>0){
1520 ag= 2*PI+atan(dy/dx);
1521 // printf("case 270-360\n");
1522 }else if(dx==0 && dy>0){
1524 }else if(dx==0 && dy<0){
1526 }else if(dx==0 && dy==0){
1528 }else if(dx<0 && dy==0){
1530 }else if(dx>0 && dy==0){
1540 Svg_parser::setSplit(Vertice* p,bool val){
1546 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1547 if(nodo->x==a && nodo->y==b)
1553 Svg_parser::newVertice(float x,float y){
1555 vert=(Vertice*)malloc(sizeof(Vertice));
1558 vert->radius1=vert->radius2=vert->angle1=vert->angle2=0;
1564 Svg_parser::parser_transform(const String transform){
1566 String tf(transform);
1568 std::vector<String> tokens=tokenize(tf," ");
1569 std::vector<String>::iterator aux=tokens.begin();
1570 while(aux!=tokens.end()){
1571 if((*aux).compare(0,9,"translate")==0){
1574 start =(*aux).find_first_of("(")+1;
1575 end =(*aux).find_first_of(",");
1576 dx =atof((*aux).substr(start,end-start).data());
1577 start =(*aux).find_first_of(",")+1;
1578 end =(*aux).size()-1;
1579 dy =atof((*aux).substr(start,end-start).data());
1581 a=newMatrix(1,0,0,1,dx,dy);
1583 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1584 }else if((*aux).compare(0,5,"scale")==0){
1586 a=newMatrix(1,0,0,1,0,0);
1587 }else if((*aux).compare(0,6,"rotate")==0){
1588 float angle,seno,coseno;
1590 start =(*aux).find_first_of("(")+1;
1591 end =(*aux).size()-1;
1592 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1596 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1598 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1599 }else if((*aux).compare(0,6,"matrix")==0){
1600 int start =(*aux).find_first_of('(')+1;
1601 int end =(*aux).find_first_of(')');
1603 a=newMatrix((*aux).substr(start,end-start));
1605 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1607 a=newMatrix(1,0,0,1,0,0);
1615 Svg_parser::newMatrix(Matrix *a){
1617 data=(Matrix*)malloc(sizeof(Matrix));
1618 data->a=a->a; data->b=a->b; data->c=a->c;
1619 data->d=a->d; data->e=a->e; data->f=a->f;
1623 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1625 data=(Matrix*)malloc(sizeof(Matrix));
1626 data->a=a; data->b=b; data->c=c;
1627 data->d=d; data->e=e; data->f=f;
1631 Svg_parser::newMatrix(const String mvector){
1632 if(!mvector.empty()){
1633 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1634 std::vector<String> tokens=tokenize(mvector,",");
1635 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1636 data->a=atof(tokens.at(0).data());
1637 data->b=atof(tokens.at(1).data());
1638 data->c=atof(tokens.at(2).data());
1639 data->d=atof(tokens.at(3).data());
1640 data->e=atof(tokens.at(4).data());
1641 data->f=atof(tokens.at(5).data());
1644 return newMatrix(1,0,0,1,0,0);
1648 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1652 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1653 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1659 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1660 Matrix* aux=newMatrix(0,0,0,0,0,0);
1661 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1662 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1663 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1664 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1665 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1666 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1670 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1671 Matrix* aux=newMatrix(0,0,0,0,0,0);
1672 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1673 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1674 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1675 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1676 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1677 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1686 Svg_parser::matrixIsNull(Matrix *mtx){
1687 if(mtx == NULL) return true;
1691 /* === EXTRA METHODS ======================================================= */
1694 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1696 if(!attribute.empty()){
1697 String str(attribute);
1699 std::vector<String> tokens=tokenize(str,";");
1700 std::vector<String>::iterator aux=tokens.begin();
1701 while(aux!=tokens.end()){
1702 int mid= (*aux).find_first_of(":");
1703 if((*aux).substr(0,mid).compare(name)==0){
1704 int end=(*aux).size();
1705 *value=(*aux).substr(mid+1,end-mid);
1714 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1717 if(!path_style.empty())
1718 fnd=extractSubAttribute(path_style,name,&value);
1720 if(!master_style.empty())
1721 fnd=extractSubAttribute(master_style,name,&value);
1728 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1731 if(!path_style.empty())
1732 fnd=extractSubAttribute(path_style,name,&value);
1733 if(fnd==0 && !master_style.empty())
1734 fnd=extractSubAttribute(master_style,name,&value);
1736 if(!subattribute.empty())
1745 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
1746 std::vector<String> tokens;
1751 while(i<path.size()){
1754 case 0: //initial state
1755 if(a=='m'){ e=1; i++;}
1756 else if(a=='c'){ e= 2; i++;}
1757 else if(a=='q'){ e= 3; i++;}
1758 else if(a=='t'){ e= 4; i++;}
1759 else if(a=='a'){ e= 5; i++;}
1760 else if(a=='l'){ e= 6; i++;}
1761 else if(a=='v'){ e= 7; i++;}
1762 else if(a=='h'){ e= 8; i++;}
1763 else if(a=='M'){ e= 9; i++;}
1764 else if(a=='C'){ e=10; i++;}
1765 else if(a=='Q'){ e=11; i++;}
1766 else if(a=='T'){ e=12; i++;}
1767 else if(a=='A'){ e=13; i++;}
1768 else if(a=='L'){ e=14; i++;}
1769 else if(a=='V'){ e=15; i++;}
1770 else if(a=='H'){ e=16; i++;}
1771 else if(a=='z' || a=='Z'){ e=17; i++;}
1772 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
1773 else if(a==','){ e=19; i++;}
1774 else if(a==' '){i++;}
1777 case 1 : tokens.push_back("m"); e=0; break;//move
1778 case 2 : tokens.push_back("c"); e=0; break;//curve
1779 case 3 : tokens.push_back("q"); e=0; break;//quadratic
1780 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
1781 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
1782 case 6 : tokens.push_back("l"); e=0; break;//line to
1783 case 7 : tokens.push_back("v"); e=0; break;//vertical
1784 case 8 : tokens.push_back("h"); e=0; break;//horizontal
1786 case 9 : tokens.push_back("M"); e=0; break;
1787 case 10: tokens.push_back("C"); e=0; break;
1788 case 11: tokens.push_back("Q"); e=0; break;
1789 case 12: tokens.push_back("T"); e=0; break;
1790 case 13: tokens.push_back("A"); e=0; break;
1791 case 14: tokens.push_back("L"); e=0; break;
1792 case 15: tokens.push_back("V"); e=0; break;
1793 case 16: tokens.push_back("H"); e=0; break;
1795 case 17: tokens.push_back("z"); e=0; break;//loop
1796 case 18: if(a=='-'||a=='.'|| isdigit (a)){
1797 buffer.append(path.substr(i,1));i++;
1802 case 19: tokens.push_back(","); e=0; break;
1803 case 20: tokens.push_back(buffer);
1809 switch(e){//last element
1810 case 1 : tokens.push_back("m"); break;
1811 case 2 : tokens.push_back("c"); break;
1812 case 3 : tokens.push_back("q"); break;
1813 case 4 : tokens.push_back("t"); break;
1814 case 5 : tokens.push_back("a"); break;
1815 case 6 : tokens.push_back("l"); break;
1816 case 7 : tokens.push_back("v"); break;
1817 case 8 : tokens.push_back("h"); break;
1818 case 9 : tokens.push_back("M"); break;
1819 case 10: tokens.push_back("C"); break;
1820 case 11: tokens.push_back("Q"); break;
1821 case 12: tokens.push_back("T"); break;
1822 case 13: tokens.push_back("A"); break;
1823 case 14: tokens.push_back("L"); break;
1824 case 15: tokens.push_back("V"); break;
1825 case 16: tokens.push_back("H"); break;
1826 case 17: tokens.push_back("z"); break;
1827 case 18: tokens.push_back(buffer); break;
1828 case 19: tokens.push_back(","); break;
1829 case 20: tokens.push_back(buffer); break;
1836 Svg_parser::randomLetter(){
1838 if(a) return (49 + rand()%9);
1839 else return (65 + rand()%24);
1843 Svg_parser::getRed(String hex){
1845 return hextodec(hex.substr(1,2));
1846 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1847 int start=hex.find_first_of("(")+1;
1848 int end =hex.find_last_of(")");
1849 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1850 return atoi(aux.data());
1855 Svg_parser::getGreen(String hex){
1857 return hextodec(hex.substr(3,2));
1858 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1859 int start=hex.find_first_of("(")+1;
1860 int end =hex.find_last_of(")");
1861 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1862 return atoi(aux.data());
1867 Svg_parser::getBlue(String hex){
1869 return hextodec(hex.substr(5,2));
1870 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1871 int start=hex.find_first_of("(")+1;
1872 int end =hex.find_last_of(")");
1873 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1874 return atoi(aux.data());
1879 Svg_parser::hextodec(String hex){
1888 else if(hex.at(i)=='1')
1890 else if(hex.at(i)=='2')
1892 else if(hex.at(i)=='3')
1894 else if(hex.at(i)=='4')
1896 else if(hex.at(i)=='5')
1898 else if(hex.at(i)=='6')
1900 else if(hex.at(i)=='7')
1902 else if(hex.at(i)=='8')
1904 else if(hex.at(i)=='9')
1906 else if(hex.at(i)=='a')
1908 else if(hex.at(i)=='b')
1910 else if(hex.at(i)=='c')
1912 else if(hex.at(i)=='d')
1914 else if(hex.at(i)=='e')
1916 else if(hex.at(i)=='f')
1924 result+=pow(16,i)*ihex[top-i-1];
1932 Svg_parser::getDimension(const String ac){
1936 int length=ac.size();
1938 if(isdigit(ac.at(length-1))){
1940 }else if(ac.at(length-1)=='%'){
1943 String mtc=ac.substr(length-2,length);
1944 String nmc=ac.substr(0,length-2);
1945 if(mtc.compare("px")==0){
1946 af=atof(nmc.data());
1947 }else if(mtc.compare("pt")==0){
1948 af=atof(nmc.data())*1.25;
1949 }else if(mtc.compare("em")==0){
1950 af=atof(nmc.data())*16;
1951 }else if(mtc.compare("mm")==0){
1952 af=atof(nmc.data())*3.54;
1953 }else if(mtc.compare("pc")==0){
1954 af=atof(nmc.data())*15;
1955 }else if(mtc.compare("cm")==0){
1956 af=atof(nmc.data())*35.43;
1957 }else if(mtc.compare("in")==0){
1958 af=atof(nmc.data())*90;
1967 Svg_parser::getRadian(float sexa){
1968 return (sexa*2*PI)/360;
1971 Svg_parser::removeS(String *input){
1972 for(unsigned int i=0;i<input->size();i++){
1973 if(input->at(i)==' '){
1979 Svg_parser::removeIntoS(String *input){
1981 for(unsigned int i=0;i<input->size();i++){
1982 if(input->at(i)=='('){
1984 }else if(input->at(i)==')'){
1986 }else if(into && input->at(i)==' '){
1992 Svg_parser::tokenize(const String& str,const String& delimiters){
1993 std::vector<String> tokens;
1994 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1995 String::size_type pos = str.find_first_of(delimiters, lastPos);
1996 while (String::npos != pos || String::npos != lastPos){
1997 tokens.push_back(str.substr(lastPos, pos - lastPos));
1998 lastPos = str.find_first_not_of(delimiters, pos);
1999 pos = str.find_first_of(delimiters, lastPos);
2004 Svg_parser::new_guid(){
2006 return GUID::hasher(uid).get_string();