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 = root;
283 xmlpp::Element* child_fill;
284 xmlpp::Element* child_stroke;
287 if(nodename.compare("rect")==0 && typeFill!=0){
288 if (mtx) child_layer = nodeStartBasicLayer(root->add_child("layer"), id);
289 child_fill=child_layer;
290 parser_rect(nodeElement,child_fill,fill,fill_opacity,opacity);
292 build_fill (child_fill,fill,NULL);
294 parser_effects(nodeElement,child_layer,parent_style,mtx);
297 if ((!(SVG_RESOLVE_BLINE) && mtx) || typeFill==2 || typeStroke==2)
298 child_layer = nodeStartBasicLayer(root->add_child("layer"), id);
299 child_fill=child_layer;
300 child_stroke=child_layer;
302 //=======================================================================
305 //if we are creating a bline
307 //First, create the list of Verteces
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);
322 std::list<BLine *>::iterator aux;
325 if(typeFill!=0){//region layer
327 child_fill=nodeStartBasicLayer(child_fill->add_child("layer"));
329 for (aux = k.begin(); aux!=k.end(); aux++){
330 xmlpp::Element *child_region=child_fill->add_child("layer");
331 child_region->set_attribute("type","region");
332 child_region->set_attribute("active","true");
333 child_region->set_attribute("version","0.1");
334 child_region->set_attribute("desc",id);
335 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
336 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
337 build_param (child_region->add_child("param"),"blend_method","integer","0");
338 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
339 build_vector (child_region->add_child("param"),"offset",0,0, *(*aux)->offset_id );
340 build_param (child_region->add_child("param"),"invert","bool","false");
341 build_param (child_region->add_child("param"),"antialias","bool","true");
342 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
343 build_param (child_region->add_child("param"),"blurtype","integer","1");
344 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
345 else build_param (child_region->add_child("param"),"winding_style","integer","0");
347 build_bline (child_region->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
350 if(typeFill==2){ //gradient in onto mode (fill)
351 if (SVG_RESOLVE_BLINE)
352 build_fill(child_fill,fill,mtx);
354 build_fill(child_fill,fill,NULL);
357 if(typeStroke!=0){//outline layer
359 child_stroke=nodeStartBasicLayer(child_stroke->add_child("layer"),"stroke");
361 for (aux=k.begin(); aux!=k.end(); aux++){
362 xmlpp::Element *child_outline=child_stroke->add_child("layer");
363 child_outline->set_attribute("type","outline");
364 child_outline->set_attribute("active","true");
365 child_outline->set_attribute("version","0.2");
366 child_outline->set_attribute("desc",id);
367 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
368 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
369 build_param (child_outline->add_child("param"),"blend_method","integer","0");
370 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
371 build_vector (child_outline->add_child("param"),"offset",0,0,*(*aux)->offset_id);
372 build_param (child_outline->add_child("param"),"invert","bool","false");
373 build_param (child_outline->add_child("param"),"antialias","bool","true");
374 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
375 build_param (child_outline->add_child("param"),"blurtype","integer","1");
377 build_param (child_outline->add_child("param"),"winding_style","integer","0");
379 build_bline (child_outline->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
381 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
382 build_param (child_outline->add_child("param"),"width","real",stroke_width);
383 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
384 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
385 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
386 if(stroke_linecap.compare("butt")==0){
387 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
388 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
390 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
391 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
393 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
394 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
397 if(typeStroke==2){ //gradient in onto mode (stroke)
398 if (SVG_RESOLVE_BLINE)
399 build_fill(child_stroke,stroke,mtx);
401 build_fill(child_stroke,stroke,NULL);
405 if (SVG_RESOLVE_BLINE)
406 parser_effects(nodeElement,child_layer,parent_style,NULL);
408 parser_effects(nodeElement,child_layer,parent_style,mtx);
412 /* === LAYER PARSERS ======================================================= */
415 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
416 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
417 Glib::ustring label =nodeElement->get_attribute_value("label");
418 Glib::ustring style =nodeElement->get_attribute_value("style");
419 Glib::ustring fill =nodeElement->get_attribute_value("fill");
424 }else if(!fill.empty()){
425 layer_style.append("fill:");
426 layer_style.append(fill);
427 }else if(!parent_style.empty()){
428 layer_style=parent_style;
431 root->set_attribute("type","PasteCanvas");
432 root->set_attribute("active","true");
433 root->set_attribute("version","0.1");
434 if(!label.empty()) root->set_attribute("desc",label);
435 else root->set_attribute("desc","Inline Canvas");
437 build_real(root->add_child("param"),"z_depth",0.0);
438 build_real(root->add_child("param"),"amount",1.0);
439 build_integer(root->add_child("param"),"blend_method",0);
440 build_vector (root->add_child("param"),"origin",0,0);
442 //printf(" canvas attributes ");
444 xmlpp::Element *child_canvas=root->add_child("param");
445 child_canvas->set_attribute("name","canvas");
446 child_canvas=child_canvas->add_child("canvas");
447 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
449 xmlpp::Node::NodeList list = node->get_children();
450 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
451 Glib::ustring name =(*iter)->get_name();
452 parser_graphics (*iter,child_canvas,layer_style,mtx);
455 if (SVG_SEP_TRANSFORMS) parser_effects(nodeElement,child_canvas,parent_style,NULL);
456 else parser_effects(nodeElement,child_canvas,parent_style,mtx);
461 Svg_parser::parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
462 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
463 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
464 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
465 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
466 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
468 xmlpp::Element *child_rect=root->add_child("layer");
469 child_rect->set_attribute("type","rectangle");
470 child_rect->set_attribute("active","true");
471 child_rect->set_attribute("version","0.2");
472 child_rect->set_attribute("desc",rect_id);
474 build_real(child_rect->add_child("param"),"z_depth",0.0);
475 build_real(child_rect->add_child("param"),"amount",1.0);
476 build_integer(child_rect->add_child("param"),"blend_method",0);
477 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
479 float auxx=atof(rect_x.c_str());
480 float auxy=atof(rect_y.c_str());
481 coor2vect(&auxx,&auxy);
482 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
483 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
484 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
485 coor2vect(&auxx,&auxy);
486 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
491 /* === CONVERT TO PATH PARSERS ============================================= */
494 Svg_parser::parser_path_polygon(Glib::ustring polygon_points, Matrix* mtx){
495 std::list<BLine *> k0;
496 if(polygon_points.empty())
498 std::list<Vertex*> points;
499 std::vector<String> tokens=get_tokens_path (polygon_points);
501 float ax,ay; ax=ay=0;
502 for(i=0;i<tokens.size();i++){
503 ax=atof(tokens.at(i).data());
504 i++; if(tokens.at(i).compare(",")==0) i++;
505 ay=atof(tokens.at(i).data());
507 if(mtx) transformPoint2D(mtx,&ax,&ay);
511 points.push_back(newVertex(ax,ay));
513 k0.push_front(newBLine(&points, true));
518 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
519 std::list<BLine *> k;
520 std::list<Vertex*> k1;
522 std::vector<String> tokens=get_tokens_path(path_d);
523 String command="M"; //the current command
524 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
526 float actual_x=0,actual_y=0; //in svg coordinate space
527 float old_x=0,old_y=0; //needed in rare cases
528 float init_x=0,init_y=0; //for closepath commands
530 for(unsigned int i=0;i<tokens.size();i++){
531 //if the token is a command, change the current command
532 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) {
533 command=tokens.at(i);
539 //if command is absolute, set actual_x/y to zero
540 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) {
545 //now parse the commands
546 if(command.compare("M")==0 || command.compare("m")==0){ //move to
548 k.push_front(newBLine(&k1, false));
552 actual_x+=atof(tokens.at(i).data());
553 i++; if(tokens.at(i).compare(",")==0) i++;
554 actual_y+=atof(tokens.at(i).data());
561 if(mtx) transformPoint2D(mtx,&ax,&ay);
563 k1.push_back(newVertex (ax,ay)); //first element
564 setSplit(k1.back(),TRUE);
565 //"If a moveto is followed by multiple pairs of coordinates,
566 // the subsequent pairs are treated as implicit lineto commands."
567 if (command.compare("M")==0)
571 }else if(command.compare("C")==0 || command.compare("c")==0){ //curve
573 tgx2=actual_x+atof(tokens.at(i).data());
574 i++; if(tokens.at(i).compare(",")==0) i++;
575 tgy2=actual_y+atof(tokens.at(i).data());
577 i++; tgx=actual_x+atof(tokens.at(i).data());
578 i++; if(tokens.at(i).compare(",")==0) i++;
579 tgy=actual_y+atof(tokens.at(i).data());
581 i++; actual_x+=atof(tokens.at(i).data());
582 i++; if(tokens.at(i).compare(",")==0) i++;
583 actual_y+=atof(tokens.at(i).data());
589 transformPoint2D(mtx,&tgx2,&tgy2);
590 transformPoint2D(mtx,&ax,&ay);
591 transformPoint2D(mtx,&tgx,&tgy);
594 coor2vect(&tgx2,&tgy2);
596 coor2vect(&tgx,&tgy);
598 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
599 if(isFirst(k1.front(),ax,ay)){
600 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
602 k1.push_back(newVertex (ax,ay));
603 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
604 setSplit(k1.back(),TRUE);
606 }else if(command.compare("Q")==0 || command.compare("q")==0){ //quadractic curve
608 tgx=actual_x+atof(tokens.at(i).data());
609 i++; if(tokens.at(i).compare(",")==0) i++;
610 tgy=actual_y+atof(tokens.at(i).data());
612 i++; actual_x+=atof(tokens.at(i).data());
613 i++; if(tokens.at(i).compare(",")==0) i++;
614 actual_y+=atof(tokens.at(i).data());
620 transformPoint2D(mtx,&ax,&ay);
621 transformPoint2D(mtx,&tgx,&tgy);
625 coor2vect(&tgx,&tgy);
627 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
628 setSplit(k1.back(),FALSE);
629 k1.push_back(newVertex (ax,ay));
630 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
631 }else if(command.compare("L")==0 || command.compare("l")==0){ //line to
633 actual_x+=atof(tokens.at(i).data());
634 i++; if(tokens.at(i).compare(",")==0) i++;
635 actual_y+=atof(tokens.at(i).data());
640 if(mtx) transformPoint2D(mtx,&ax,&ay);
644 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
645 if(isFirst(k1.front(),ax,ay)){
646 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
648 k1.push_back(newVertex(ax,ay));
649 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
651 }else if(command.compare("H")==0 || command.compare("h")==0){// horizontal move
652 //the same that L but only Horizontal movement
654 actual_x+=atof(tokens.at(i).data());
659 if(mtx) transformPoint2D(mtx,&ax,&ay);
663 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
664 if(isFirst(k1.front(),ax,ay)){
665 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
667 k1.push_back(newVertex(ax,ay));
668 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
670 }else if(command.compare("V")==0 || command.compare("v")==0){//vertical
672 actual_y+=atof(tokens.at(i).data());
677 if(mtx) transformPoint2D(mtx,&ax,&ay);
681 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
682 if(isFirst(k1.front(),ax,ay)){
683 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
685 k1.push_back(newVertex(ax,ay));
686 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
688 }else if(command.compare("T")==0 || command.compare("t")==0){// I don't know what does it
689 actual_x+=atof(tokens.at(i).data());
690 i++; if(tokens.at(i).compare(",")==0) i++;
691 actual_y+=atof(tokens.at(i).data());
692 }else if(command.compare("A")==0 || command.compare("a")==0){//elliptic arc
694 //isn't complete support, is only for circles
696 //this curve have 6 parameters
698 float radius_x,radius_y;
702 radius_x=atof(tokens.at(i).data());
703 i++; if(tokens.at(i).compare(",")==0) i++;
704 radius_y=atof(tokens.at(i).data());
706 i++; angle=atof(tokens.at(i).data());
708 i++; large=atoi(tokens.at(i).data());
709 i++; sweep=atoi(tokens.at(i).data());
711 i++; actual_x+=atof(tokens.at(i).data());
712 i++; if(tokens.at(i).compare(",")==0) i++;
713 actual_y+=atof(tokens.at(i).data());
715 if(!large && !sweep){
717 tgx2 = old_x + radius_x*0.5;
720 tgy = actual_y + radius_y*0.5;
726 transformPoint2D(mtx,&tgx2,&tgy2);
727 transformPoint2D(mtx,&ax,&ay);
728 transformPoint2D(mtx,&tgx,&tgy);
731 coor2vect(&tgx2,&tgy2);
733 coor2vect(&tgx,&tgy);
735 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
736 if(isFirst(k1.front(),ax,ay)){
737 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
739 k1.push_back(newVertex (ax,ay));
740 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
741 setSplit(k1.back(),TRUE);
743 }else if(!large && sweep){
746 tgy2 = old_y + radius_y*0.5;
747 tgx = actual_x + radius_x*0.5;
754 transformPoint2D(mtx,&tgx2,&tgy2);
755 transformPoint2D(mtx,&ax,&ay);
756 transformPoint2D(mtx,&tgx,&tgy);
759 coor2vect(&tgx2,&tgy2);
761 coor2vect(&tgx,&tgy);
763 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
764 if(isFirst(k1.front(),ax,ay)){
765 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
767 k1.push_back(newVertex (ax,ay));
768 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
769 setSplit(k1.back(),TRUE);
771 }else if( large && !sweep){//rare
772 //this need more than one vertex
773 }else if( large && sweep){//circles in inkscape are made with this kind of arc
776 if(old_x>actual_x) sense =-1;
777 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
778 in_x = (old_x+actual_x)/2;
779 in_y = old_y - sense*radius_y;
780 in_tgx1 = in_x - sense*(radius_x*0.5);
781 in_tgx2 = in_x + sense*(radius_x*0.5);
786 tgy2=actual_y - sense*(radius_y*0.5);
788 tgy =actual_y - sense*(radius_y*0.5);
794 transformPoint2D(mtx,&tgx2,&tgy2);
795 transformPoint2D(mtx,&tgx ,&tgy );
796 transformPoint2D(mtx,&ax,&ay);
798 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
799 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
800 transformPoint2D(mtx,&in_x,&in_y);
803 coor2vect(&tgx2 , &tgy2);
804 coor2vect(&ax , &ay );
805 coor2vect(&tgx , &tgy );
807 coor2vect(&in_tgx2 , &in_tgy2);
808 coor2vect(&in_tgx1 , &in_tgy1);
809 coor2vect(&in_x , &in_y );
812 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
813 //save the intermediate point
814 k1.push_back(newVertex (in_x,in_y));
815 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
816 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
817 setSplit(k1.back(),TRUE); //this could be changed
819 if(isFirst(k1.front(),ax,ay)){
820 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
822 k1.push_back(newVertex (ax,ay));
823 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
824 setSplit(k1.back(),TRUE);
827 }else if(command.compare("z")==0){
828 k.push_front(newBLine(&k1, true));
830 if (i<tokens.size() && tokens.at(i).compare("M")!=0 && tokens.at(i).compare("m")!=0) {
831 //starting a new path, but not with a moveto
837 if(mtx) transformPoint2D(mtx,&ax,&ay);
839 k1.push_back(newVertex (ax,ay)); //first element
840 setSplit(k1.back(),TRUE);
842 i--; //decrement i to balance "i++" at command change
844 std::cout<<"unsupported path token: "<<tokens.at(i)<<std::endl;
848 k.push_front(newBLine(&k1, false)); //last element
853 /* === EFFECTS PARSERS ===================================================== */
856 Svg_parser::parser_effects(const xmlpp::Element* /*nodeElement*/,xmlpp::Element* root,String /*parent_style*/,Matrix* mtx){
857 build_transform(root, mtx);
860 /* === DEFS PARSERS ======================================================== */
863 Svg_parser::parser_defs(const xmlpp::Node* node){
864 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
866 xmlpp::Node::NodeList list = node->get_children();
867 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
868 Glib::ustring name =(*iter)->get_name();
869 if(name.compare("linearGradient")==0){
870 parser_linearGradient(*iter);
871 }else if(name.compare("radialGradient")==0){
872 parser_radialGradient(*iter);
878 /* === BUILDS ============================================================== */
881 Svg_parser::build_transform(xmlpp::Element* root,Matrix* mtx){
883 xmlpp::Element *child_transform=root->add_child("layer");
884 child_transform->set_attribute("type","warp");
885 child_transform->set_attribute("active","true");
886 child_transform->set_attribute("version","0.1");
887 child_transform->set_attribute("desc","Transform");
890 x=100;y=100;coor2vect(&x,&y);
891 build_vector (child_transform->add_child("param"),"src_tl",x,y);
893 x=200;y=200;coor2vect(&x,&y);
894 build_vector (child_transform->add_child("param"),"src_br",x,y);
898 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
899 build_vector (child_transform->add_child("param"),"dest_tl",x,y);
902 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
903 build_vector (child_transform->add_child("param"),"dest_tr",x,y);
906 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
907 build_vector (child_transform->add_child("param"),"dest_br",x,y);
910 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
911 build_vector (child_transform->add_child("param"),"dest_bl",x,y);
913 build_param (child_transform->add_child("param"),"clip","bool","false");
914 build_param (child_transform->add_child("param"),"horizon","real","4.0");
918 std::list<ColorStop*>*
919 Svg_parser::find_colorStop(String name){
921 if(lg.empty()&& rg.empty())
925 if(find.at(0)=='#') find.erase(0,1);
927 std::list<LinearGradient*>::iterator aux=lg.begin();
928 while(aux!=lg.end()){//only find into linear gradients
929 if(find.compare((*aux)->name)==0){
930 return (*aux)->stops;
939 Svg_parser::build_fill(xmlpp::Element* root, String name,Matrix *mtx){
941 int start=name.find_first_of("#")+1;
942 int end=name.find_first_of(")");
943 String find= name.substr(start,end-start);
944 bool encounter=false;
946 std::list<LinearGradient*>::iterator aux=lg.begin();
947 while(aux!=lg.end()){
948 if(find.compare((*aux)->name)==0){
949 build_linearGradient (root,*aux,mtx);
955 if(!encounter && !rg.empty()){
956 std::list<RadialGradient*>::iterator aux=rg.begin();
957 while(aux!=rg.end()){
958 if(find.compare((*aux)->name)==0){
959 build_radialGradient (root,*aux,mtx);
968 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
969 std::list<ColorStop*>::iterator aux_stop=stops->begin();
970 while(aux_stop!=stops->end()){
971 xmlpp::Element *child=root->add_child("color");
972 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
973 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
974 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
975 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
976 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
981 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
983 xmlpp::Element* gradient=root->add_child("layer");
985 gradient->set_attribute("type","linear_gradient");
986 gradient->set_attribute("active","true");
987 gradient->set_attribute("desc",data->name);
988 build_param (gradient->add_child("param"),"z_depth","real","0");
989 build_param (gradient->add_child("param"),"amount","real","1");
991 build_param (gradient->add_child("param"),"blend_method","integer","21");
999 if (mtx || data->transform){
1001 if (mtx && data->transform){
1002 composeMatrix(&mtx2,mtx,data->transform);
1005 }else if (data->transform){
1006 mtx2=data->transform;
1008 //matrix transforms the gradient as a whole
1009 //it does not preserve angles, so we cant' simply transform both points
1011 //set point (x3,y3) on the same gradient line as (x2,y2)
1012 //the gradient line is perpendicular to (x1,y1)(x2,y2)
1015 //transform everything
1016 transformPoint2D(mtx2,&x1,&y1);
1017 transformPoint2D(mtx2,&x2,&y2);
1018 transformPoint2D(mtx2,&x3,&y3);
1020 if (x2!=x3 && y2!=y3) {//divide by zero check
1022 //set k as slope between (x2,y2) and (x3,y3)
1023 //k is the slope of gradient lines post-transformation
1025 //set point (x2,y2) on the gradient line passing through (x3,y3)
1026 //so that the line (x1,y1)(x2,y2) is perpendicular to (x2,y2)(x3,y3)
1027 x2= (x3*k+x1/k+y1-y3)/(k+(1/k));
1029 } else if (x2==x3 && y2!=y3) {
1031 } else if (x2!=x3 && y2==y3) {
1034 std::cout<<"SVG Import warning: gradient points equal each other"<<std::endl;
1038 coor2vect (&x1,&y1);
1039 coor2vect (&x2,&y2);
1041 build_vector (gradient->add_child("param"),"p1",x1,y1);
1042 build_vector (gradient->add_child("param"),"p2",x2,y2);
1044 xmlpp::Element *child_stops=gradient->add_child("param");
1045 child_stops->set_attribute("name","gradient");
1046 child_stops->set_attribute("guid",GUID::hasher(data->name).get_string());
1047 build_stop_color (child_stops->add_child("gradient"),data->stops);
1048 build_param (gradient->add_child("param"),"loop","bool","false");
1049 build_param (gradient->add_child("param"),"zigzag","bool","false");
1053 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1055 xmlpp::Element* gradient;
1057 if (mtx || data->transform) {
1058 xmlpp::Element* layer=root->add_child("layer");
1060 layer->set_attribute("type","PasteCanvas");
1061 layer->set_attribute("active","true");
1062 layer->set_attribute("version","0.1");
1063 layer->set_attribute("desc",data->name);
1064 build_param (layer->add_child("param"),"z_depth","real","0");
1065 build_param (layer->add_child("param"),"amount","real","1");
1066 build_param (layer->add_child("param"),"blend_method","integer","21"); //straight onto
1067 build_vector (layer->add_child("param"),"origin",0,0);
1068 xmlpp::Element *child=layer->add_child("param");
1069 child->set_attribute("name","canvas");
1070 xmlpp::Element* child_layer=child->add_child("canvas");
1072 gradient=child_layer->add_child("layer");
1073 gradient->set_attribute("desc",data->name);
1074 build_param (gradient->add_child("param"),"blend_method","integer","0"); //composite
1076 if (mtx && data->transform){
1077 composeMatrix(&mtx2,mtx,data->transform);
1080 }else if (data->transform){
1081 mtx2=data->transform;
1083 build_transform(child_layer,mtx2);
1086 gradient=root->add_child("layer");
1087 gradient->set_attribute("desc",data->name);
1088 build_param (gradient->add_child("param"),"blend_method","integer","21"); //straight onto
1091 gradient->set_attribute("type","radial_gradient");
1092 gradient->set_attribute("active","true");
1093 build_param (gradient->add_child("param"),"z_depth","real","0");
1094 build_param (gradient->add_child("param"),"amount","real","1");
1096 xmlpp::Element *child_stops=gradient->add_child("param");
1097 child_stops->set_attribute("name","gradient");
1098 child_stops->set_attribute("guid",GUID::hasher(data->name).get_string());
1099 build_stop_color (child_stops->add_child("gradient"),data->stops);
1101 //here the center point and radius
1107 coor2vect (&cx,&cy);
1109 build_vector (gradient->add_child("param"),"center",cx,cy);
1110 build_param (gradient->add_child("param"),"radius","real",r);
1112 build_param (gradient->add_child("param"),"loop","bool","false");
1113 build_param (gradient->add_child("param"),"zigzag","bool","false");
1118 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1119 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1120 Glib::ustring id =nodeElement->get_attribute_value("id");
1121 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1122 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1123 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1124 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1125 Glib::ustring link =nodeElement->get_attribute_value("href");
1126 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1128 //resolve transformations
1130 if(!transform.empty())
1131 mtx=parser_transform (transform);
1133 std::list<ColorStop*> *stops;
1135 stops=find_colorStop (link);
1138 stops=new std::list<ColorStop*>();
1139 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1141 xmlpp::Node::NodeList list = node->get_children();
1142 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1143 Glib::ustring name =(*iter)->get_name();
1144 if(name.compare("stop")==0){
1145 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1146 Glib::ustring style =nodeIter->get_attribute_value("style");
1147 float offset=atof(nodeIter->get_attribute_value("offset").data());
1151 extractSubAttribute (style,"stop-color",&stop_color);
1152 extractSubAttribute (style,"stop-opacity",&opacity);
1154 if(opacity.empty()) opacity="1";
1155 if(stop_color.empty()) stop_color="#000000";//black for default :S
1156 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1162 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops,mtx));
1167 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1168 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1169 Glib::ustring id =nodeElement->get_attribute_value("id");
1170 float cx =atof(nodeElement->get_attribute_value("cx").data());
1171 float cy =atof(nodeElement->get_attribute_value("cy").data());
1172 float fx =atof(nodeElement->get_attribute_value("fx").data());
1173 float fy =atof(nodeElement->get_attribute_value("fy").data());
1174 float r =atof(nodeElement->get_attribute_value("r").data());
1175 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1176 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1178 if (cx!=fx || cy!=fy)
1179 std::cout<<"SVG Parser: ignoring focus attributes for radial gradient";
1181 //resolve transformations
1183 if(!transform.empty())
1184 mtx=parser_transform (transform);
1186 std::list<ColorStop*> *stops=NULL;
1188 //inkscape always use link, i dont need parser stops here, but it's posible
1189 stops=find_colorStop (link);
1192 rg.push_back(newRadialGradient(id,cx,cy,r,stops,mtx));
1197 Svg_parser::newColorStop(String color,float opacity,float pos){
1199 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1200 float r=getRed(color);
1201 float g=getGreen(color);
1202 float b=getBlue(color);
1204 Color ret=adjustGamma(r/255,g/255,b/255,a);
1205 _stop->r=ret.get_r();
1206 _stop->g=ret.get_g();
1207 _stop->b=ret.get_b();
1208 _stop->a=ret.get_a();
1213 Svg_parser::adjustGamma(float r,float g,float b,float a){
1215 if(gamma.get_gamma_r()!=1.0){
1217 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1219 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1221 if(gamma.get_gamma_g()!=1.0){
1223 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1225 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1227 if(gamma.get_gamma_b()!=1.0){
1229 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1231 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1237 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform){
1238 LinearGradient* data;
1239 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1240 sprintf(data->name,"%s",name.data());
1246 data->transform=transform;
1251 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform){
1252 RadialGradient* data;
1253 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1254 sprintf(data->name,"%s",name.data());
1259 data->transform=transform;
1264 Svg_parser::newBLine(std::list<Vertex*> *points,bool loop){
1266 data=(BLine*)malloc(sizeof(BLine));
1267 //sprintf(data->name,"%s",name.data());
1268 data->points=new std::list<Vertex*> (*points);
1270 data->bline_id=new String(new_guid());
1271 data->offset_id=new String(new_guid());
1276 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1277 root->set_attribute("type","colorcorrect");
1278 root->set_attribute("active","true");
1279 root->set_attribute("version","0.1");
1280 root->set_attribute("desc","Gamma");
1281 build_real (root->add_child("param"),"gamma",gamma);
1285 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1286 root->set_attribute("type","translate");
1287 root->set_attribute("active","true");
1288 root->set_attribute("version","0.1");
1289 build_vector (root->add_child("param"),"origin",dx,dy);
1292 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1293 root->set_attribute("type","rotate");
1294 root->set_attribute("active","true");
1295 root->set_attribute("version","0.1");
1296 build_vector (root->add_child("param"),"origin",dx,dy);
1297 build_real (root->add_child("param"),"amount",angle);
1300 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertex*> p){
1301 root->set_attribute("name","vector_list");
1302 xmlpp::Element *child=root->add_child("dynamic_list");
1303 child->set_attribute("type","vector");
1304 std::list<Vertex*>::iterator aux = p.begin();
1305 while(aux!=p.end()){
1306 xmlpp::Element *child_entry=child->add_child("entry");
1307 xmlpp::Element *child_vector=child_entry->add_child("vector");
1308 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1309 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1314 Svg_parser::build_vertex(xmlpp::Element* root , Vertex *p){
1315 xmlpp::Element *child_comp=root->add_child("composite");
1316 child_comp->set_attribute("type","bline_point");
1317 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1318 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1319 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1320 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1321 else build_param (child_comp->add_child("split"),"","bool","false");
1323 xmlpp::Element *child_t1=child_comp->add_child("t1");
1324 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1325 child_rc->set_attribute("type","vector");
1326 build_param (child_rc->add_child("radius"),"","real",p->radius1);
1327 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1329 xmlpp::Element *child_t2=child_comp->add_child("t2");
1330 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1331 child_rc2->set_attribute("type","vector");
1332 build_param (child_rc2->add_child("radius"),"","real",p->radius2);
1333 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1337 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertex*> p,bool loop,String blineguid){
1338 root->set_attribute("name","bline");
1339 xmlpp::Element *child=root->add_child("bline");
1340 child->set_attribute("type","bline_point");
1342 child->set_attribute("loop","true");
1344 child->set_attribute("loop","false");
1345 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1346 std::list<Vertex*>::iterator aux = p.begin();
1347 while(aux!=p.end()){
1348 if(*aux) build_vertex (child->add_child("entry"),*aux);
1354 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1355 if(!type.empty() && !value.empty()){
1356 if(!name.empty()) root->set_attribute("name",name);
1357 xmlpp::Element *child=root->add_child(type);
1358 child->set_attribute("value",value);
1360 root->get_parent()->remove_child(root);
1364 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1366 if(!name.empty()) root->set_attribute("name",name);
1367 xmlpp::Element *child=root->add_child(type);
1368 child->set_attribute("value",etl::strprintf ("%f",value));
1370 root->get_parent()->remove_child(root);
1374 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1376 if(!name.empty()) root->set_attribute("name",name);
1377 xmlpp::Element *child=root->add_child(type);
1378 char *enteroc=new char[10];
1379 sprintf(enteroc,"%d",value);
1380 child->set_attribute("value",enteroc);
1383 root->get_parent()->remove_child(root);
1388 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1389 if(name.compare("")!=0) root->set_attribute("name",name);
1390 xmlpp::Element *child=root->add_child("integer");
1391 char *enteroc=new char[10];
1392 sprintf(enteroc,"%d",value);
1393 child->set_attribute("value",enteroc);
1396 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1397 if(name.compare("")!=0) root->set_attribute("name",name);
1398 xmlpp::Element *child=root->add_child("real");
1399 char *realc=new char[20];
1400 sprintf(realc,"%f",value);
1401 child->set_attribute("value",realc);
1405 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1406 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1407 root->get_parent()->remove_child(root);
1408 printf("Color aborted\n");
1411 Color ret=adjustGamma(r/255,g/255,b/255,a);
1413 root->set_attribute("name","color");
1414 xmlpp::Element *child=root->add_child("color");
1415 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1416 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1417 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1418 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1421 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1423 if(name.compare("")!=0) root->set_attribute("name",name);
1424 xmlpp::Element *child=root->add_child("vector");
1425 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1426 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1430 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1431 if(name.compare("")!=0) root->set_attribute("name",name);
1432 xmlpp::Element *child=root->add_child("vector");
1433 if(!guid.empty()) child->set_attribute("guid",guid);
1434 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1435 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1439 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root, String name){
1440 root->set_attribute("type","PasteCanvas");
1441 root->set_attribute("active","true");
1442 root->set_attribute("version","0.1");
1443 root->set_attribute("desc",name);
1444 build_param (root->add_child("param"),"z_depth","real","0");
1445 build_param (root->add_child("param"),"amount","real","1");
1446 build_param (root->add_child("param"),"blend_method","integer","0");
1447 build_vector (root->add_child("param"),"origin",0,0);
1448 xmlpp::Element *child=root->add_child("param");
1449 child->set_attribute("name","canvas");
1450 return child->add_child("canvas");
1453 /* === COORDINATES & TRANSFORMATIONS ======================================= */
1455 Svg_parser::coor2vect(float *x,float *y){
1459 sy= atof(height.c_str())-sy;
1468 Svg_parser::setTg1(Vertex *p,float p1x,float p1y,float p2x,float p2y){
1470 float d1x,d1y,d2x,d2y,dx,dy;
1481 rd=sqrt(dx*dx + dy*dy);
1483 ag=PI + atan(dy/dx);
1484 }else if(dx>0 && dy<0){
1485 ag=PI + atan(dy/dx);
1486 }else if(dx<0 && dy<0){
1488 }else if(dx<0 && dy>0){
1489 ag= 2*PI+atan(dy/dx);
1490 }else if(dx==0 && dy>0){
1492 }else if(dx==0 && dy<0){
1494 }else if(dx==0 && dy==0){
1496 }else if(dx<0 && dy==0){
1498 }else if(dx>0 && dy==0){
1506 Svg_parser::setTg2(Vertex* p,float p1x,float p1y,float p2x,float p2y){
1508 float d1x,d1y,d2x,d2y,dx,dy;
1520 rd=sqrt(dx*dx + dy*dy);
1522 ag=PI + atan(dy/dx);
1523 // printf("case 180-270\n");
1524 }else if(dx>0 && dy<0){
1525 ag=PI + atan(dy/dx);
1526 // printf("case 90-180\n");
1527 }else if(dx<0 && dy<0){
1529 // printf("case 0-90\n");
1530 }else if(dx<0 && dy>0){
1531 ag= 2*PI+atan(dy/dx);
1532 // printf("case 270-360\n");
1533 }else if(dx==0 && dy>0){
1535 }else if(dx==0 && dy<0){
1537 }else if(dx==0 && dy==0){
1539 }else if(dx<0 && dy==0){
1541 }else if(dx>0 && dy==0){
1551 Svg_parser::setSplit(Vertex* p,bool val){
1557 Svg_parser::isFirst(Vertex* nodo,float a, float b){
1558 if(nodo->x==a && nodo->y==b)
1564 Svg_parser::newVertex(float x,float y){
1566 vert=(Vertex*)malloc(sizeof(Vertex));
1569 vert->radius1=vert->radius2=vert->angle1=vert->angle2=0;
1575 Svg_parser::parser_transform(const String transform){
1577 String tf(transform);
1579 std::vector<String> tokens=tokenize(tf," ");
1580 std::vector<String>::iterator aux=tokens.begin();
1581 while(aux!=tokens.end()){
1582 if((*aux).compare(0,9,"translate")==0){
1585 start =(*aux).find_first_of("(")+1;
1586 end =(*aux).find_first_of(",");
1587 dx =atof((*aux).substr(start,end-start).data());
1588 start =(*aux).find_first_of(",")+1;
1589 end =(*aux).size()-1;
1590 dy =atof((*aux).substr(start,end-start).data());
1592 a=newMatrix(1,0,0,1,dx,dy);
1594 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1595 }else if((*aux).compare(0,5,"scale")==0){
1597 a=newMatrix(1,0,0,1,0,0);
1598 }else if((*aux).compare(0,6,"rotate")==0){
1599 float angle,seno,coseno;
1601 start =(*aux).find_first_of("(")+1;
1602 end =(*aux).size()-1;
1603 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1607 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1609 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1610 }else if((*aux).compare(0,6,"matrix")==0){
1611 int start =(*aux).find_first_of('(')+1;
1612 int end =(*aux).find_first_of(')');
1614 a=newMatrix((*aux).substr(start,end-start));
1616 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1618 a=newMatrix(1,0,0,1,0,0);
1626 Svg_parser::newMatrix(Matrix *a){
1628 data=(Matrix*)malloc(sizeof(Matrix));
1629 data->a=a->a; data->b=a->b; data->c=a->c;
1630 data->d=a->d; data->e=a->e; data->f=a->f;
1634 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1636 data=(Matrix*)malloc(sizeof(Matrix));
1637 data->a=a; data->b=b; data->c=c;
1638 data->d=d; data->e=e; data->f=f;
1642 Svg_parser::newMatrix(const String mvector){
1643 if(!mvector.empty()){
1644 std::vector<String> tokens=tokenize(mvector,",");
1645 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1646 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1647 data->a=atof(tokens.at(0).data());
1648 data->b=atof(tokens.at(1).data());
1649 data->c=atof(tokens.at(2).data());
1650 data->d=atof(tokens.at(3).data());
1651 data->e=atof(tokens.at(4).data());
1652 data->f=atof(tokens.at(5).data());
1655 return newMatrix(1,0,0,1,0,0);
1659 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1663 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1664 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1670 Svg_parser::composeMatrix(Matrix **mtx,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);
1681 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1682 Matrix* aux=newMatrix(0,0,0,0,0,0);
1683 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1684 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1685 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1686 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1687 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1688 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1697 Svg_parser::matrixIsNull(Matrix *mtx){
1698 if(mtx == NULL) return true;
1702 /* === EXTRA METHODS ======================================================= */
1705 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1707 if(!attribute.empty()){
1708 String str(attribute);
1710 std::vector<String> tokens=tokenize(str,";");
1711 std::vector<String>::iterator aux=tokens.begin();
1712 while(aux!=tokens.end()){
1713 int mid= (*aux).find_first_of(":");
1714 if((*aux).substr(0,mid).compare(name)==0){
1715 int end=(*aux).size();
1716 *value=(*aux).substr(mid+1,end-mid);
1725 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1728 if(!path_style.empty())
1729 fnd=extractSubAttribute(path_style,name,&value);
1731 if(!master_style.empty())
1732 fnd=extractSubAttribute(master_style,name,&value);
1739 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1742 if(!path_style.empty())
1743 fnd=extractSubAttribute(path_style,name,&value);
1744 if(fnd==0 && !master_style.empty())
1745 fnd=extractSubAttribute(master_style,name,&value);
1747 if(!subattribute.empty())
1756 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
1757 std::vector<String> tokens;
1762 while(i<path.size()){
1765 case 0: //initial state
1766 if(a=='m'){ e=1; i++;}
1767 else if(a=='c'){ e= 2; i++;}
1768 else if(a=='q'){ e= 3; i++;}
1769 else if(a=='t'){ e= 4; i++;}
1770 else if(a=='a'){ e= 5; i++;}
1771 else if(a=='l'){ e= 6; i++;}
1772 else if(a=='v'){ e= 7; i++;}
1773 else if(a=='h'){ e= 8; i++;}
1774 else if(a=='M'){ e= 9; i++;}
1775 else if(a=='C'){ e=10; i++;}
1776 else if(a=='Q'){ e=11; i++;}
1777 else if(a=='T'){ e=12; i++;}
1778 else if(a=='A'){ e=13; i++;}
1779 else if(a=='L'){ e=14; i++;}
1780 else if(a=='V'){ e=15; i++;}
1781 else if(a=='H'){ e=16; i++;}
1782 else if(a=='z' || a=='Z'){ e=17; i++;}
1783 else if(a=='-' || a=='.' || a=='e' || a=='E' || isdigit (a)){ e=18;}
1784 else if(a==','){ e=19; i++;}
1785 else if(a==' '){i++;}
1788 case 1 : tokens.push_back("m"); e=0; break;//move
1789 case 2 : tokens.push_back("c"); e=0; break;//curve
1790 case 3 : tokens.push_back("q"); e=0; break;//quadratic
1791 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
1792 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
1793 case 6 : tokens.push_back("l"); e=0; break;//line to
1794 case 7 : tokens.push_back("v"); e=0; break;//vertical
1795 case 8 : tokens.push_back("h"); e=0; break;//horizontal
1797 case 9 : tokens.push_back("M"); e=0; break;
1798 case 10: tokens.push_back("C"); e=0; break;
1799 case 11: tokens.push_back("Q"); e=0; break;
1800 case 12: tokens.push_back("T"); e=0; break;
1801 case 13: tokens.push_back("A"); e=0; break;
1802 case 14: tokens.push_back("L"); e=0; break;
1803 case 15: tokens.push_back("V"); e=0; break;
1804 case 16: tokens.push_back("H"); e=0; break;
1806 case 17: tokens.push_back("z"); e=0; break;//loop
1807 case 18: if(a=='-' || a=='.' || a=='e' || a=='E' || isdigit (a)){
1808 buffer.append(path.substr(i,1));i++;
1813 case 19: tokens.push_back(","); e=0; break;
1814 case 20: tokens.push_back(buffer);
1820 switch(e){//last element
1821 case 1 : tokens.push_back("m"); break;
1822 case 2 : tokens.push_back("c"); break;
1823 case 3 : tokens.push_back("q"); break;
1824 case 4 : tokens.push_back("t"); break;
1825 case 5 : tokens.push_back("a"); break;
1826 case 6 : tokens.push_back("l"); break;
1827 case 7 : tokens.push_back("v"); break;
1828 case 8 : tokens.push_back("h"); break;
1829 case 9 : tokens.push_back("M"); break;
1830 case 10: tokens.push_back("C"); break;
1831 case 11: tokens.push_back("Q"); break;
1832 case 12: tokens.push_back("T"); break;
1833 case 13: tokens.push_back("A"); break;
1834 case 14: tokens.push_back("L"); break;
1835 case 15: tokens.push_back("V"); break;
1836 case 16: tokens.push_back("H"); break;
1837 case 17: tokens.push_back("z"); break;
1838 case 18: tokens.push_back(buffer); break;
1839 case 19: tokens.push_back(","); break;
1840 case 20: tokens.push_back(buffer); break;
1847 Svg_parser::randomLetter(){
1849 if(a) return (49 + rand()%9);
1850 else return (65 + rand()%24);
1854 Svg_parser::getRed(String hex){
1856 //allow for 3-digit hex codes (#rgb = #rrggbb)
1857 if (hex.length()<7) return (16+1) * hextodec(hex.substr(1,1));
1858 return hextodec(hex.substr(1,2));
1859 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1860 int start=hex.find_first_of("(")+1;
1861 int end =hex.find_last_of(")");
1862 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1863 return atoi(aux.data());
1868 Svg_parser::getGreen(String hex){
1870 if (hex.length()<7) return (16+1) * hextodec(hex.substr(2,1));
1871 return hextodec(hex.substr(3,2));
1872 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1873 int start=hex.find_first_of("(")+1;
1874 int end =hex.find_last_of(")");
1875 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1876 return atoi(aux.data());
1881 Svg_parser::getBlue(String hex){
1883 if (hex.length()<7) return (16+1) * hextodec(hex.substr(3,1));
1884 return hextodec(hex.substr(5,2));
1885 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1886 int start=hex.find_first_of("(")+1;
1887 int end =hex.find_last_of(")");
1888 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1889 return atoi(aux.data());
1894 Svg_parser::hextodec(String hex){
1903 else if(hex.at(i)=='1')
1905 else if(hex.at(i)=='2')
1907 else if(hex.at(i)=='3')
1909 else if(hex.at(i)=='4')
1911 else if(hex.at(i)=='5')
1913 else if(hex.at(i)=='6')
1915 else if(hex.at(i)=='7')
1917 else if(hex.at(i)=='8')
1919 else if(hex.at(i)=='9')
1921 else if(hex.at(i)=='a')
1923 else if(hex.at(i)=='b')
1925 else if(hex.at(i)=='c')
1927 else if(hex.at(i)=='d')
1929 else if(hex.at(i)=='e')
1931 else if(hex.at(i)=='f')
1939 result+=pow(16,i)*ihex[top-i-1];
1947 Svg_parser::getDimension(const String ac){
1951 int length=ac.size();
1953 if(isdigit(ac.at(length-1))){
1955 }else if(ac.at(length-1)=='%'){
1958 String mtc=ac.substr(length-2,length);
1959 String nmc=ac.substr(0,length-2);
1960 if(mtc.compare("px")==0){
1961 af=atof(nmc.data());
1962 }else if(mtc.compare("pt")==0){
1963 af=atof(nmc.data())*1.25;
1964 }else if(mtc.compare("em")==0){
1965 af=atof(nmc.data())*16;
1966 }else if(mtc.compare("mm")==0){
1967 af=atof(nmc.data())*3.54;
1968 }else if(mtc.compare("pc")==0){
1969 af=atof(nmc.data())*15;
1970 }else if(mtc.compare("cm")==0){
1971 af=atof(nmc.data())*35.43;
1972 }else if(mtc.compare("in")==0){
1973 af=atof(nmc.data())*90;
1982 Svg_parser::getRadian(float sexa){
1983 return (sexa*2*PI)/360;
1986 Svg_parser::removeS(String *input){
1987 for(unsigned int i=0;i<input->size();i++){
1988 if(input->at(i)==' '){
1994 Svg_parser::removeIntoS(String *input){
1996 for(unsigned int i=0;i<input->size();i++){
1997 if(input->at(i)=='('){
1999 }else if(input->at(i)==')'){
2001 }else if(into && input->at(i)==' '){
2007 Svg_parser::tokenize(const String& str,const String& delimiters){
2008 std::vector<String> tokens;
2009 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
2010 String::size_type pos = str.find_first_of(delimiters, lastPos);
2011 while (String::npos != pos || String::npos != lastPos){
2012 tokens.push_back(str.substr(lastPos, pos - lastPos));
2013 lastPos = str.find_first_not_of(delimiters, pos);
2014 pos = str.find_first_of(delimiters, lastPos);
2019 Svg_parser::new_guid(){
2021 return GUID::hasher(uid).get_string();