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 //=======================================================================
306 //if we are creating a bline
308 //First, create the list of vertices
309 if (SVG_RESOLVE_BLINE) {
310 if(nodename.compare("path")==0){
311 k=parser_path_d (nodeElement->get_attribute_value("d"),mtx);
312 } else if(nodename.compare("polygon")==0){
313 k=parser_path_polygon (nodeElement->get_attribute_value("points"),mtx);
316 if(nodename.compare("path")==0){
317 k=parser_path_d (nodeElement->get_attribute_value("d"),NULL);
318 } else if(nodename.compare("polygon")==0){
319 k=parser_path_polygon (nodeElement->get_attribute_value("points"),NULL);
323 std::list<BLine *>::iterator aux;
326 if(typeFill!=0){//region layer
327 for (aux = k.begin(); aux!=k.end(); aux++){
328 xmlpp::Element *child_region=child_fill->add_child("layer");
329 child_region->set_attribute("type","region");
330 child_region->set_attribute("active","true");
331 child_region->set_attribute("version","0.1");
332 child_region->set_attribute("desc",id);
333 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
334 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
335 build_param (child_region->add_child("param"),"blend_method","integer","0");
336 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
337 build_vector (child_region->add_child("param"),"offset",0,0, *(*aux)->offset_id );
338 build_param (child_region->add_child("param"),"invert","bool","false");
339 build_param (child_region->add_child("param"),"antialias","bool","true");
340 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
341 build_param (child_region->add_child("param"),"blurtype","integer","1");
342 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
343 else build_param (child_region->add_child("param"),"winding_style","integer","0");
345 build_bline (child_region->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
348 if(typeFill==2){ //gradient in onto mode (fill)
349 if (SVG_RESOLVE_BLINE)
350 build_fill(child_fill,fill,mtx);
352 build_fill(child_fill,fill,NULL);
355 if(typeStroke!=0){//outline layer
356 for (aux=k.begin(); aux!=k.end(); aux++){
357 xmlpp::Element *child_outline=child_stroke->add_child("layer");
358 child_outline->set_attribute("type","outline");
359 child_outline->set_attribute("active","true");
360 child_outline->set_attribute("version","0.2");
361 child_outline->set_attribute("desc",id);
362 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
363 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
364 build_param (child_outline->add_child("param"),"blend_method","integer","0");
365 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
366 build_vector (child_outline->add_child("param"),"offset",0,0,*(*aux)->offset_id);
367 build_param (child_outline->add_child("param"),"invert","bool","false");
368 build_param (child_outline->add_child("param"),"antialias","bool","true");
369 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
370 build_param (child_outline->add_child("param"),"blurtype","integer","1");
372 build_param (child_outline->add_child("param"),"winding_style","integer","0");
374 build_bline (child_outline->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
376 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
377 build_param (child_outline->add_child("param"),"width","real",stroke_width);
378 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
379 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
380 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
381 if(stroke_linecap.compare("butt")==0){
382 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
383 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
385 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
386 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
388 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
389 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
392 if(typeStroke==2){ //gradient in onto mode (stroke)
393 if (SVG_RESOLVE_BLINE)
394 build_fill(child_stroke,fill,mtx);
396 build_fill(child_stroke,fill,NULL);
398 if (SVG_RESOLVE_BLINE)
399 parser_effects(nodeElement,child_layer,parent_style,NULL);
401 parser_effects(nodeElement,child_layer,parent_style,mtx);
407 /* === LAYER PARSERS ======================================================= */
410 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
411 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
412 Glib::ustring label =nodeElement->get_attribute_value("label");
413 Glib::ustring style =nodeElement->get_attribute_value("style");
414 Glib::ustring fill =nodeElement->get_attribute_value("fill");
419 }else if(!fill.empty()){
420 layer_style.append("fill:");
421 layer_style.append(fill);
422 }else if(!parent_style.empty()){
423 layer_style=parent_style;
426 root->set_attribute("type","PasteCanvas");
427 root->set_attribute("active","true");
428 root->set_attribute("version","0.1");
429 if(!label.empty()) root->set_attribute("desc",label);
430 else root->set_attribute("desc","Inline Canvas");
432 build_real(root->add_child("param"),"z_depth",0.0);
433 build_real(root->add_child("param"),"amount",1.0);
434 build_integer(root->add_child("param"),"blend_method",0);
435 build_vector (root->add_child("param"),"origin",0,0);
437 //printf(" canvas attributes ");
439 xmlpp::Element *child_canvas=root->add_child("param");
440 child_canvas->set_attribute("name","canvas");
441 child_canvas=child_canvas->add_child("canvas");
442 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
444 xmlpp::Node::NodeList list = node->get_children();
445 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
446 Glib::ustring name =(*iter)->get_name();
447 parser_graphics (*iter,child_canvas,layer_style,mtx);
450 if (SVG_SEP_TRANSFORMS) parser_effects(nodeElement,child_canvas,parent_style,NULL);
451 else parser_effects(nodeElement,child_canvas,parent_style,mtx);
456 Svg_parser::parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
457 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
458 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
459 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
460 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
461 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
463 xmlpp::Element *child_rect=root->add_child("layer");
464 child_rect->set_attribute("type","rectangle");
465 child_rect->set_attribute("active","true");
466 child_rect->set_attribute("version","0.2");
467 child_rect->set_attribute("desc",rect_id);
469 build_real(child_rect->add_child("param"),"z_depth",0.0);
470 build_real(child_rect->add_child("param"),"amount",1.0);
471 build_integer(child_rect->add_child("param"),"blend_method",0);
472 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
474 float auxx=atof(rect_x.c_str());
475 float auxy=atof(rect_y.c_str());
476 coor2vect(&auxx,&auxy);
477 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
478 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
479 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
480 coor2vect(&auxx,&auxy);
481 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
486 /* === CONVERT TO PATH PARSERS ============================================= */
489 Svg_parser::parser_path_polygon(Glib::ustring polygon_points, Matrix* mtx){
490 std::list<BLine *> k0;
491 if(polygon_points.empty())
493 std::list<Vertice*> points;
494 std::vector<String> tokens=get_tokens_path (polygon_points);
496 float ax,ay; ax=ay=0;
497 for(i=0;i<tokens.size();i++){
498 ax=atof(tokens.at(i).data());
499 i++; if(tokens.at(i).compare(",")==0) i++;
500 ay=atof(tokens.at(i).data());
502 if(mtx) transformPoint2D(mtx,&ax,&ay);
506 points.push_back(newVertice(ax,ay));
508 k0.push_front(newBLine(&points, true));
513 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
514 std::list<BLine *> k;
515 std::list<Vertice*> k1;
517 std::vector<String> tokens=get_tokens_path(path_d);
518 String command="M"; //the current command
519 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
521 float actual_x=0,actual_y=0; //in svg coordinate space
522 float old_x=0,old_y=0; //needed in rare cases
523 float init_x=0,init_y=0; //for closepath commands
525 for(unsigned int i=0;i<tokens.size();i++){
526 //if the token is a command, change the current command
527 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) {
528 command=tokens.at(i);
534 //if command is absolute, set actual_x/y to zero
535 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) {
540 //now parse the commands
541 if(command.compare("M")==0 || command.compare("m")==0){ //move to
543 k.push_front(newBLine(&k1, false));
547 actual_x+=atof(tokens.at(i).data());
548 i++; if(tokens.at(i).compare(",")==0) i++;
549 actual_y+=atof(tokens.at(i).data());
556 if(mtx) transformPoint2D(mtx,&ax,&ay);
558 k1.push_back(newVertice (ax,ay)); //first element
559 setSplit(k1.back(),TRUE);
560 //"If a moveto is followed by multiple pairs of coordinates,
561 // the subsequent pairs are treated as implicit lineto commands."
562 if (command.compare("M")==0)
566 }else if(command.compare("C")==0 || command.compare("c")==0){ //curve
568 tgx2=actual_x+atof(tokens.at(i).data());
569 i++; if(tokens.at(i).compare(",")==0) i++;
570 tgy2=actual_y+atof(tokens.at(i).data());
572 i++; tgx=actual_x+atof(tokens.at(i).data());
573 i++; if(tokens.at(i).compare(",")==0) i++;
574 tgy=actual_y+atof(tokens.at(i).data());
576 i++; actual_x+=atof(tokens.at(i).data());
577 i++; if(tokens.at(i).compare(",")==0) i++;
578 actual_y+=atof(tokens.at(i).data());
584 transformPoint2D(mtx,&tgx2,&tgy2);
585 transformPoint2D(mtx,&ax,&ay);
586 transformPoint2D(mtx,&tgx,&tgy);
589 coor2vect(&tgx2,&tgy2);
591 coor2vect(&tgx,&tgy);
593 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
594 if(isFirst(k1.front(),ax,ay)){
595 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
597 k1.push_back(newVertice (ax,ay));
598 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
599 setSplit(k1.back(),TRUE);
601 }else if(command.compare("Q")==0 || command.compare("q")==0){ //quadractic curve
603 tgx=actual_x+atof(tokens.at(i).data());
604 i++; if(tokens.at(i).compare(",")==0) i++;
605 tgy=actual_y+atof(tokens.at(i).data());
607 i++; actual_x+=atof(tokens.at(i).data());
608 i++; if(tokens.at(i).compare(",")==0) i++;
609 actual_y+=atof(tokens.at(i).data());
615 transformPoint2D(mtx,&ax,&ay);
616 transformPoint2D(mtx,&tgx,&tgy);
620 coor2vect(&tgx,&tgy);
622 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
623 setSplit(k1.back(),FALSE);
624 k1.push_back(newVertice (ax,ay));
625 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
626 }else if(command.compare("L")==0 || command.compare("l")==0){ //line to
628 actual_x+=atof(tokens.at(i).data());
629 i++; if(tokens.at(i).compare(",")==0) i++;
630 actual_y+=atof(tokens.at(i).data());
635 if(mtx) transformPoint2D(mtx,&ax,&ay);
639 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
640 if(isFirst(k1.front(),ax,ay)){
641 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
643 k1.push_back(newVertice(ax,ay));
644 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
646 }else if(command.compare("H")==0 || command.compare("h")==0){// horizontal move
647 //the same that L but only Horizontal movement
649 actual_x+=atof(tokens.at(i).data());
654 if(mtx) transformPoint2D(mtx,&ax,&ay);
658 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
659 if(isFirst(k1.front(),ax,ay)){
660 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
662 k1.push_back(newVertice(ax,ay));
663 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
665 }else if(command.compare("V")==0 || command.compare("v")==0){//vertical
667 actual_y+=atof(tokens.at(i).data());
672 if(mtx) transformPoint2D(mtx,&ax,&ay);
676 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
677 if(isFirst(k1.front(),ax,ay)){
678 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
680 k1.push_back(newVertice(ax,ay));
681 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
683 }else if(command.compare("T")==0 || command.compare("t")==0){// I don't know what does it
684 actual_x+=atof(tokens.at(i).data());
685 i++; if(tokens.at(i).compare(",")==0) i++;
686 actual_y+=atof(tokens.at(i).data());
687 }else if(command.compare("A")==0 || command.compare("a")==0){//elliptic arc
689 //isn't complete support, is only for circles
691 //this curve have 6 parameters
693 float radius_x,radius_y;
697 radius_x=atof(tokens.at(i).data());
698 i++; if(tokens.at(i).compare(",")==0) i++;
699 radius_y=atof(tokens.at(i).data());
701 i++; angle=atof(tokens.at(i).data());
703 i++; large=atoi(tokens.at(i).data());
704 i++; sweep=atoi(tokens.at(i).data());
706 i++; actual_x+=atof(tokens.at(i).data());
707 i++; if(tokens.at(i).compare(",")==0) i++;
708 actual_y+=atof(tokens.at(i).data());
710 if(!large && !sweep){
712 tgx2 = old_x + radius_x*0.5;
715 tgy = actual_y + radius_y*0.5;
721 transformPoint2D(mtx,&tgx2,&tgy2);
722 transformPoint2D(mtx,&ax,&ay);
723 transformPoint2D(mtx,&tgx,&tgy);
726 coor2vect(&tgx2,&tgy2);
728 coor2vect(&tgx,&tgy);
730 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
731 if(isFirst(k1.front(),ax,ay)){
732 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
734 k1.push_back(newVertice (ax,ay));
735 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
736 setSplit(k1.back(),TRUE);
738 }else if(!large && sweep){
741 tgy2 = old_y + radius_y*0.5;
742 tgx = actual_x + radius_x*0.5;
749 transformPoint2D(mtx,&tgx2,&tgy2);
750 transformPoint2D(mtx,&ax,&ay);
751 transformPoint2D(mtx,&tgx,&tgy);
754 coor2vect(&tgx2,&tgy2);
756 coor2vect(&tgx,&tgy);
758 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
759 if(isFirst(k1.front(),ax,ay)){
760 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
762 k1.push_back(newVertice (ax,ay));
763 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
764 setSplit(k1.back(),TRUE);
766 }else if( large && !sweep){//rare
767 //this need more than one vertex
768 }else if( large && sweep){//circles in inkscape are made with this kind of arc
771 if(old_x>actual_x) sense =-1;
772 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
773 in_x = (old_x+actual_x)/2;
774 in_y = old_y - sense*radius_y;
775 in_tgx1 = in_x - sense*(radius_x*0.5);
776 in_tgx2 = in_x + sense*(radius_x*0.5);
781 tgy2=actual_y - sense*(radius_y*0.5);
783 tgy =actual_y - sense*(radius_y*0.5);
789 transformPoint2D(mtx,&tgx2,&tgy2);
790 transformPoint2D(mtx,&tgx ,&tgy );
791 transformPoint2D(mtx,&ax,&ay);
793 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
794 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
795 transformPoint2D(mtx,&in_x,&in_y);
798 coor2vect(&tgx2 , &tgy2);
799 coor2vect(&ax , &ay );
800 coor2vect(&tgx , &tgy );
802 coor2vect(&in_tgx2 , &in_tgy2);
803 coor2vect(&in_tgx1 , &in_tgy1);
804 coor2vect(&in_x , &in_y );
807 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
808 //save the intermediate point
809 k1.push_back(newVertice (in_x,in_y));
810 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
811 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
812 setSplit(k1.back(),TRUE); //this could be changed
814 if(isFirst(k1.front(),ax,ay)){
815 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
817 k1.push_back(newVertice (ax,ay));
818 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
819 setSplit(k1.back(),TRUE);
822 }else if(command.compare("z")==0){
823 k.push_front(newBLine(&k1, true));
825 if (i<tokens.size() && tokens.at(i).compare("M")!=0 && tokens.at(i).compare("m")!=0) {
826 //starting a new path, but not with a moveto
832 if(mtx) transformPoint2D(mtx,&ax,&ay);
834 k1.push_back(newVertice (ax,ay)); //first element
835 setSplit(k1.back(),TRUE);
837 i--; //decrement i to balance "i++" at command change
839 std::cout<<"unsupported path token: "<<tokens.at(i)<<std::endl;
843 k.push_front(newBLine(&k1, false)); //last element
848 /* === EFFECTS PARSERS ===================================================== */
851 Svg_parser::parser_effects(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx){
852 build_transform(root, mtx);
855 /* === DEFS PARSERS ======================================================== */
858 Svg_parser::parser_defs(const xmlpp::Node* node){
859 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
861 xmlpp::Node::NodeList list = node->get_children();
862 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
863 Glib::ustring name =(*iter)->get_name();
864 if(name.compare("linearGradient")==0){
865 parser_linearGradient(*iter);
866 }else if(name.compare("radialGradient")==0){
867 parser_radialGradient(*iter);
873 /* === BUILDS ============================================================== */
876 Svg_parser::build_transform(xmlpp::Element* root,Matrix* mtx){
878 xmlpp::Element *child_transform=root->add_child("layer");
879 child_transform->set_attribute("type","warp");
880 child_transform->set_attribute("active","true");
881 child_transform->set_attribute("version","0.1");
882 child_transform->set_attribute("desc","Transform");
885 x=100;y=100;coor2vect(&x,&y);
886 build_vector (child_transform->add_child("param"),"src_tl",x,y);
888 x=200;y=200;coor2vect(&x,&y);
889 build_vector (child_transform->add_child("param"),"src_br",x,y);
893 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
894 build_vector (child_transform->add_child("param"),"dest_tl",x,y);
897 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
898 build_vector (child_transform->add_child("param"),"dest_tr",x,y);
901 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
902 build_vector (child_transform->add_child("param"),"dest_br",x,y);
905 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
906 build_vector (child_transform->add_child("param"),"dest_bl",x,y);
908 build_param (child_transform->add_child("param"),"clip","bool","false");
909 build_param (child_transform->add_child("param"),"horizon","real","4.0");
913 std::list<ColorStop*>*
914 Svg_parser::find_colorStop(String name){
916 if(lg.empty()&& rg.empty())
920 if(find.at(0)=='#') find.erase(0,1);
922 std::list<LinearGradient*>::iterator aux=lg.begin();
923 while(aux!=lg.end()){//only find into linear gradients
924 if(find.compare((*aux)->name)==0){
925 return (*aux)->stops;
934 Svg_parser::build_fill(xmlpp::Element* root, String name,Matrix *mtx){
936 int start=name.find_first_of("#")+1;
937 int end=name.find_first_of(")");
938 String find= name.substr(start,end-start);
939 bool encounter=false;
941 std::list<LinearGradient*>::iterator aux=lg.begin();
942 while(aux!=lg.end()){
943 if(find.compare((*aux)->name)==0){
944 build_linearGradient (root,*aux,mtx);
950 if(!encounter && !rg.empty()){
951 std::list<RadialGradient*>::iterator aux=rg.begin();
952 while(aux!=rg.end()){
953 if(find.compare((*aux)->name)==0){
954 build_radialGradient (root,*aux,mtx);
963 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
964 std::list<ColorStop*>::iterator aux_stop=stops->begin();
965 while(aux_stop!=stops->end()){
966 xmlpp::Element *child=root->add_child("color");
967 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
968 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
969 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
970 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
971 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
976 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
978 xmlpp::Element* gradient=root->add_child("layer");
980 gradient->set_attribute("type","linear_gradient");
981 gradient->set_attribute("active","true");
982 gradient->set_attribute("desc","Gradient004");
983 build_param (gradient->add_child("param"),"z_depth","real","0");
984 build_param (gradient->add_child("param"),"amount","real","1");
986 build_param (gradient->add_child("param"),"blend_method","integer","21");
994 if (mtx || data->transform){
996 if (mtx && data->transform){
997 composeMatrix(&mtx2,mtx,data->transform);
1000 }else if (data->transform){
1001 mtx2=data->transform;
1003 //matrix transforms the gradient as a whole
1004 //it does not preserve angles, so we cant' simply transform both points
1006 //set point (x3,y3) on the same gradient line as (x2,y2)
1007 //the gradient line is perpendicular to (x1,y1)(x2,y2)
1010 //transform everything
1011 transformPoint2D(mtx2,&x1,&y1);
1012 transformPoint2D(mtx2,&x2,&y2);
1013 transformPoint2D(mtx2,&x3,&y3);
1015 if (x2!=x3 && y2!=y3) {//divide by zero check
1017 //set k as slope between (x2,y2) and (x3,y3)
1018 //k is the slope of gradient lines post-transformation
1020 //set point (x2,y2) on the gradient line passing through (x3,y3)
1021 //so that the line (x1,y1)(x2,y2) is perpendicular to (x2,y2)(x3,y3)
1022 x2= (x3*k+x1/k+y1-y3)/(k+(1/k));
1024 } else if (x2==x3 && y2!=y3) {
1026 } else if (x2!=x3 && y2==y3) {
1029 std::cout<<"SVG Import warning: gradient points equal each other"<<std::endl;
1033 coor2vect (&x1,&y1);
1034 coor2vect (&x2,&y2);
1036 build_vector (gradient->add_child("param"),"p1",x1,y1);
1037 build_vector (gradient->add_child("param"),"p2",x2,y2);
1039 xmlpp::Element *child_stops=gradient->add_child("param");
1040 child_stops->set_attribute("name","gradient");
1041 build_stop_color (child_stops->add_child("gradient"),data->stops);
1042 build_param (gradient->add_child("param"),"loop","bool","false");
1043 build_param (gradient->add_child("param"),"zigzag","bool","false");
1047 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1049 xmlpp::Element* gradient;
1051 if (mtx || data->transform) {
1052 xmlpp::Element* layer=root->add_child("layer");
1054 layer->set_attribute("type","PasteCanvas");
1055 layer->set_attribute("active","true");
1056 layer->set_attribute("version","0.1");
1057 layer->set_attribute("desc","Composite");
1058 build_param (layer->add_child("param"),"z_depth","real","0");
1059 build_param (layer->add_child("param"),"amount","real","1");
1060 build_param (layer->add_child("param"),"blend_method","integer","21"); //straight onto
1061 build_vector (layer->add_child("param"),"origin",0,0);
1062 xmlpp::Element *child=layer->add_child("param");
1063 child->set_attribute("name","canvas");
1064 xmlpp::Element* child_layer=child->add_child("canvas");
1066 gradient=child_layer->add_child("layer");
1067 build_param (gradient->add_child("param"),"blend_method","integer","0"); //composite
1069 if (mtx && data->transform){
1070 composeMatrix(&mtx2,mtx,data->transform);
1073 }else if (data->transform){
1074 mtx2=data->transform;
1076 build_transform(child_layer,mtx2);
1079 gradient=root->add_child("layer");
1080 build_param (gradient->add_child("param"),"blend_method","integer","21"); //straight onto
1083 gradient->set_attribute("type","radial_gradient");
1084 gradient->set_attribute("active","true");
1085 build_param (gradient->add_child("param"),"z_depth","real","0");
1086 build_param (gradient->add_child("param"),"amount","real","1");
1088 xmlpp::Element *child_stops=gradient->add_child("param");
1089 child_stops->set_attribute("name","gradient");
1090 build_stop_color (child_stops->add_child("gradient"),data->stops);
1092 //here the center point and radius
1098 coor2vect (&cx,&cy);
1100 build_vector (gradient->add_child("param"),"center",cx,cy);
1101 build_param (gradient->add_child("param"),"radius","real",r);
1103 build_param (gradient->add_child("param"),"loop","bool","false");
1104 build_param (gradient->add_child("param"),"zigzag","bool","false");
1109 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1110 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1111 Glib::ustring id =nodeElement->get_attribute_value("id");
1112 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1113 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1114 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1115 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1116 Glib::ustring link =nodeElement->get_attribute_value("href");
1117 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1119 //resolve transformations
1121 if(!transform.empty())
1122 mtx=parser_transform (transform);
1124 std::list<ColorStop*> *stops;
1126 stops=find_colorStop (link);
1129 stops=new std::list<ColorStop*>();
1130 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1132 xmlpp::Node::NodeList list = node->get_children();
1133 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1134 Glib::ustring name =(*iter)->get_name();
1135 if(name.compare("stop")==0){
1136 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1137 Glib::ustring style =nodeIter->get_attribute_value("style");
1138 float offset=atof(nodeIter->get_attribute_value("offset").data());
1142 extractSubAttribute (style,"stop-color",&stop_color);
1143 extractSubAttribute (style,"stop-opacity",&opacity);
1145 if(opacity.empty()) opacity="1";
1146 if(stop_color.empty()) stop_color="#000000";//black for default :S
1147 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1153 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops,mtx));
1158 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1159 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1160 Glib::ustring id =nodeElement->get_attribute_value("id");
1161 float cx =atof(nodeElement->get_attribute_value("cx").data());
1162 float cy =atof(nodeElement->get_attribute_value("cy").data());
1163 float fx =atof(nodeElement->get_attribute_value("fx").data());
1164 float fy =atof(nodeElement->get_attribute_value("fy").data());
1165 float r =atof(nodeElement->get_attribute_value("r").data());
1166 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1167 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1169 if (cx!=fx || cy!=fy)
1170 std::cout<<"SVG Parser: ignoring focus attributes for radial gradient";
1172 //resolve transformations
1174 if(!transform.empty())
1175 mtx=parser_transform (transform);
1177 std::list<ColorStop*> *stops=NULL;
1179 //inkscape always use link, i dont need parser stops here, but it's posible
1180 stops=find_colorStop (link);
1183 rg.push_back(newRadialGradient(id,cx,cy,r,stops,mtx));
1188 Svg_parser::newColorStop(String color,float opacity,float pos){
1190 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1191 float r=getRed(color);
1192 float g=getGreen(color);
1193 float b=getBlue(color);
1195 Color ret=adjustGamma(r/255,g/255,b/255,a);
1196 _stop->r=ret.get_r();
1197 _stop->g=ret.get_g();
1198 _stop->b=ret.get_b();
1199 _stop->a=ret.get_a();
1204 Svg_parser::adjustGamma(float r,float g,float b,float a){
1206 if(gamma.get_gamma_r()!=1.0){
1208 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1210 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1212 if(gamma.get_gamma_g()!=1.0){
1214 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1216 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1218 if(gamma.get_gamma_b()!=1.0){
1220 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1222 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1228 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform){
1229 LinearGradient* data;
1230 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1231 sprintf(data->name,"%s",name.data());
1237 data->transform=transform;
1242 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform){
1243 RadialGradient* data;
1244 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1245 sprintf(data->name,"%s",name.data());
1250 data->transform=transform;
1255 Svg_parser::newBLine(std::list<Vertice*> *points,bool loop){
1257 data=(BLine*)malloc(sizeof(BLine));
1258 //sprintf(data->name,"%s",name.data());
1259 data->points=new std::list<Vertice*> (*points);
1261 data->bline_id=new String(new_guid());
1262 data->offset_id=new String(new_guid());
1267 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1268 root->set_attribute("type","colorcorrect");
1269 root->set_attribute("active","true");
1270 root->set_attribute("version","0.1");
1271 root->set_attribute("desc","Gamma");
1272 build_real (root->add_child("param"),"gamma",gamma);
1276 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1277 root->set_attribute("type","translate");
1278 root->set_attribute("active","true");
1279 root->set_attribute("version","0.1");
1280 build_vector (root->add_child("param"),"origin",dx,dy);
1283 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1284 root->set_attribute("type","rotate");
1285 root->set_attribute("active","true");
1286 root->set_attribute("version","0.1");
1287 build_vector (root->add_child("param"),"origin",dx,dy);
1288 build_real (root->add_child("param"),"amount",angle);
1291 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertice*> p){
1292 root->set_attribute("name","vector_list");
1293 xmlpp::Element *child=root->add_child("dynamic_list");
1294 child->set_attribute("type","vector");
1295 std::list<Vertice*>::iterator aux = p.begin();
1296 while(aux!=p.end()){
1297 xmlpp::Element *child_entry=child->add_child("entry");
1298 xmlpp::Element *child_vector=child_entry->add_child("vector");
1299 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1300 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1305 Svg_parser::build_vertice(xmlpp::Element* root , Vertice *p){
1306 xmlpp::Element *child_comp=root->add_child("composite");
1307 child_comp->set_attribute("type","bline_point");
1308 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1309 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1310 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1311 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1312 else build_param (child_comp->add_child("split"),"","bool","false");
1314 xmlpp::Element *child_t1=child_comp->add_child("t1");
1315 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1316 child_rc->set_attribute("type","vector");
1317 build_param (child_rc->add_child("radius"),"","real",p->radius1);
1318 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1320 xmlpp::Element *child_t2=child_comp->add_child("t2");
1321 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1322 child_rc2->set_attribute("type","vector");
1323 build_param (child_rc2->add_child("radius"),"","real",p->radius2);
1324 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1328 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blineguid){
1329 root->set_attribute("name","bline");
1330 xmlpp::Element *child=root->add_child("bline");
1331 child->set_attribute("type","bline_point");
1333 child->set_attribute("loop","true");
1335 child->set_attribute("loop","false");
1336 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1337 std::list<Vertice*>::iterator aux = p.begin();
1338 while(aux!=p.end()){
1339 if(*aux) build_vertice (child->add_child("entry"),*aux);
1345 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1346 if(!type.empty() && !value.empty()){
1347 if(!name.empty()) root->set_attribute("name",name);
1348 xmlpp::Element *child=root->add_child(type);
1349 child->set_attribute("value",value);
1351 root->get_parent()->remove_child(root);
1355 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1357 if(!name.empty()) root->set_attribute("name",name);
1358 xmlpp::Element *child=root->add_child(type);
1359 child->set_attribute("value",etl::strprintf ("%f",value));
1361 root->get_parent()->remove_child(root);
1365 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1367 if(!name.empty()) root->set_attribute("name",name);
1368 xmlpp::Element *child=root->add_child(type);
1369 char *enteroc=new char[10];
1370 sprintf(enteroc,"%d",value);
1371 child->set_attribute("value",enteroc);
1374 root->get_parent()->remove_child(root);
1379 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1380 if(name.compare("")!=0) root->set_attribute("name",name);
1381 xmlpp::Element *child=root->add_child("integer");
1382 char *enteroc=new char[10];
1383 sprintf(enteroc,"%d",value);
1384 child->set_attribute("value",enteroc);
1387 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1388 if(name.compare("")!=0) root->set_attribute("name",name);
1389 xmlpp::Element *child=root->add_child("real");
1390 char *realc=new char[20];
1391 sprintf(realc,"%f",value);
1392 child->set_attribute("value",realc);
1396 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1397 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1398 root->get_parent()->remove_child(root);
1399 printf("Color aborted\n");
1402 Color ret=adjustGamma(r/255,g/255,b/255,a);
1404 root->set_attribute("name","color");
1405 xmlpp::Element *child=root->add_child("color");
1406 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1407 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1408 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1409 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1412 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1414 if(name.compare("")!=0) root->set_attribute("name",name);
1415 xmlpp::Element *child=root->add_child("vector");
1416 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1417 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1421 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1422 if(name.compare("")!=0) root->set_attribute("name",name);
1423 xmlpp::Element *child=root->add_child("vector");
1424 if(!guid.empty()) child->set_attribute("guid",guid);
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::nodeStartBasicLayer(xmlpp::Element* root){
1431 root->set_attribute("type","PasteCanvas");
1432 root->set_attribute("active","true");
1433 root->set_attribute("version","0.1");
1434 root->set_attribute("desc","Composite");
1435 build_param (root->add_child("param"),"z_depth","real","0");
1436 build_param (root->add_child("param"),"amount","real","1");
1437 build_param (root->add_child("param"),"blend_method","integer","0");
1438 build_vector (root->add_child("param"),"origin",0,0);
1439 xmlpp::Element *child=root->add_child("param");
1440 child->set_attribute("name","canvas");
1441 return child->add_child("canvas");
1444 /* === COORDINATES & TRANSFORMATIONS ======================================= */
1446 Svg_parser::coor2vect(float *x,float *y){
1450 sy= atof(height.c_str())-sy;
1459 Svg_parser::setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y){
1461 float d1x,d1y,d2x,d2y,dx,dy;
1472 rd=sqrt(dx*dx + dy*dy);
1474 ag=PI + atan(dy/dx);
1475 }else if(dx>0 && dy<0){
1476 ag=PI + atan(dy/dx);
1477 }else if(dx<0 && dy<0){
1479 }else if(dx<0 && dy>0){
1480 ag= 2*PI+atan(dy/dx);
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){
1489 }else if(dx>0 && dy==0){
1497 Svg_parser::setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y){
1499 float d1x,d1y,d2x,d2y,dx,dy;
1511 rd=sqrt(dx*dx + dy*dy);
1513 ag=PI + atan(dy/dx);
1514 // printf("case 180-270\n");
1515 }else if(dx>0 && dy<0){
1516 ag=PI + atan(dy/dx);
1517 // printf("case 90-180\n");
1518 }else if(dx<0 && dy<0){
1520 // printf("case 0-90\n");
1521 }else if(dx<0 && dy>0){
1522 ag= 2*PI+atan(dy/dx);
1523 // printf("case 270-360\n");
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){
1532 }else if(dx>0 && dy==0){
1542 Svg_parser::setSplit(Vertice* p,bool val){
1548 Svg_parser::isFirst(Vertice* nodo,float a, float b){
1549 if(nodo->x==a && nodo->y==b)
1555 Svg_parser::newVertice(float x,float y){
1557 vert=(Vertice*)malloc(sizeof(Vertice));
1560 vert->radius1=vert->radius2=vert->angle1=vert->angle2=0;
1566 Svg_parser::parser_transform(const String transform){
1568 String tf(transform);
1570 std::vector<String> tokens=tokenize(tf," ");
1571 std::vector<String>::iterator aux=tokens.begin();
1572 while(aux!=tokens.end()){
1573 if((*aux).compare(0,9,"translate")==0){
1576 start =(*aux).find_first_of("(")+1;
1577 end =(*aux).find_first_of(",");
1578 dx =atof((*aux).substr(start,end-start).data());
1579 start =(*aux).find_first_of(",")+1;
1580 end =(*aux).size()-1;
1581 dy =atof((*aux).substr(start,end-start).data());
1583 a=newMatrix(1,0,0,1,dx,dy);
1585 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1586 }else if((*aux).compare(0,5,"scale")==0){
1588 a=newMatrix(1,0,0,1,0,0);
1589 }else if((*aux).compare(0,6,"rotate")==0){
1590 float angle,seno,coseno;
1592 start =(*aux).find_first_of("(")+1;
1593 end =(*aux).size()-1;
1594 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1598 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1600 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1601 }else if((*aux).compare(0,6,"matrix")==0){
1602 int start =(*aux).find_first_of('(')+1;
1603 int end =(*aux).find_first_of(')');
1605 a=newMatrix((*aux).substr(start,end-start));
1607 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1609 a=newMatrix(1,0,0,1,0,0);
1617 Svg_parser::newMatrix(Matrix *a){
1619 data=(Matrix*)malloc(sizeof(Matrix));
1620 data->a=a->a; data->b=a->b; data->c=a->c;
1621 data->d=a->d; data->e=a->e; data->f=a->f;
1625 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1627 data=(Matrix*)malloc(sizeof(Matrix));
1628 data->a=a; data->b=b; data->c=c;
1629 data->d=d; data->e=e; data->f=f;
1633 Svg_parser::newMatrix(const String mvector){
1634 if(!mvector.empty()){
1635 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1636 std::vector<String> tokens=tokenize(mvector,",");
1637 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1638 data->a=atof(tokens.at(0).data());
1639 data->b=atof(tokens.at(1).data());
1640 data->c=atof(tokens.at(2).data());
1641 data->d=atof(tokens.at(3).data());
1642 data->e=atof(tokens.at(4).data());
1643 data->f=atof(tokens.at(5).data());
1646 return newMatrix(1,0,0,1,0,0);
1650 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1654 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1655 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1661 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1662 Matrix* aux=newMatrix(0,0,0,0,0,0);
1663 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1664 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1665 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1666 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1667 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1668 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1672 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1673 Matrix* aux=newMatrix(0,0,0,0,0,0);
1674 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1675 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1676 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1677 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1678 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1679 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1688 Svg_parser::matrixIsNull(Matrix *mtx){
1689 if(mtx == NULL) return true;
1693 /* === EXTRA METHODS ======================================================= */
1696 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1698 if(!attribute.empty()){
1699 String str(attribute);
1701 std::vector<String> tokens=tokenize(str,";");
1702 std::vector<String>::iterator aux=tokens.begin();
1703 while(aux!=tokens.end()){
1704 int mid= (*aux).find_first_of(":");
1705 if((*aux).substr(0,mid).compare(name)==0){
1706 int end=(*aux).size();
1707 *value=(*aux).substr(mid+1,end-mid);
1716 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1719 if(!path_style.empty())
1720 fnd=extractSubAttribute(path_style,name,&value);
1722 if(!master_style.empty())
1723 fnd=extractSubAttribute(master_style,name,&value);
1730 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1733 if(!path_style.empty())
1734 fnd=extractSubAttribute(path_style,name,&value);
1735 if(fnd==0 && !master_style.empty())
1736 fnd=extractSubAttribute(master_style,name,&value);
1738 if(!subattribute.empty())
1747 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
1748 std::vector<String> tokens;
1753 while(i<path.size()){
1756 case 0: //initial state
1757 if(a=='m'){ e=1; i++;}
1758 else if(a=='c'){ e= 2; i++;}
1759 else if(a=='q'){ e= 3; i++;}
1760 else if(a=='t'){ e= 4; i++;}
1761 else if(a=='a'){ e= 5; i++;}
1762 else if(a=='l'){ e= 6; i++;}
1763 else if(a=='v'){ e= 7; i++;}
1764 else if(a=='h'){ e= 8; i++;}
1765 else if(a=='M'){ e= 9; i++;}
1766 else if(a=='C'){ e=10; i++;}
1767 else if(a=='Q'){ e=11; i++;}
1768 else if(a=='T'){ e=12; i++;}
1769 else if(a=='A'){ e=13; i++;}
1770 else if(a=='L'){ e=14; i++;}
1771 else if(a=='V'){ e=15; i++;}
1772 else if(a=='H'){ e=16; i++;}
1773 else if(a=='z' || a=='Z'){ e=17; i++;}
1774 else if(a=='-' ||a=='.'|| isdigit (a)){ e=18;}
1775 else if(a==','){ e=19; i++;}
1776 else if(a==' '){i++;}
1779 case 1 : tokens.push_back("m"); e=0; break;//move
1780 case 2 : tokens.push_back("c"); e=0; break;//curve
1781 case 3 : tokens.push_back("q"); e=0; break;//quadratic
1782 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
1783 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
1784 case 6 : tokens.push_back("l"); e=0; break;//line to
1785 case 7 : tokens.push_back("v"); e=0; break;//vertical
1786 case 8 : tokens.push_back("h"); e=0; break;//horizontal
1788 case 9 : tokens.push_back("M"); e=0; break;
1789 case 10: tokens.push_back("C"); e=0; break;
1790 case 11: tokens.push_back("Q"); e=0; break;
1791 case 12: tokens.push_back("T"); e=0; break;
1792 case 13: tokens.push_back("A"); e=0; break;
1793 case 14: tokens.push_back("L"); e=0; break;
1794 case 15: tokens.push_back("V"); e=0; break;
1795 case 16: tokens.push_back("H"); e=0; break;
1797 case 17: tokens.push_back("z"); e=0; break;//loop
1798 case 18: if(a=='-'||a=='.'|| isdigit (a)){
1799 buffer.append(path.substr(i,1));i++;
1804 case 19: tokens.push_back(","); e=0; break;
1805 case 20: tokens.push_back(buffer);
1811 switch(e){//last element
1812 case 1 : tokens.push_back("m"); break;
1813 case 2 : tokens.push_back("c"); break;
1814 case 3 : tokens.push_back("q"); break;
1815 case 4 : tokens.push_back("t"); break;
1816 case 5 : tokens.push_back("a"); break;
1817 case 6 : tokens.push_back("l"); break;
1818 case 7 : tokens.push_back("v"); break;
1819 case 8 : tokens.push_back("h"); break;
1820 case 9 : tokens.push_back("M"); break;
1821 case 10: tokens.push_back("C"); break;
1822 case 11: tokens.push_back("Q"); break;
1823 case 12: tokens.push_back("T"); break;
1824 case 13: tokens.push_back("A"); break;
1825 case 14: tokens.push_back("L"); break;
1826 case 15: tokens.push_back("V"); break;
1827 case 16: tokens.push_back("H"); break;
1828 case 17: tokens.push_back("z"); break;
1829 case 18: tokens.push_back(buffer); break;
1830 case 19: tokens.push_back(","); break;
1831 case 20: tokens.push_back(buffer); break;
1838 Svg_parser::randomLetter(){
1840 if(a) return (49 + rand()%9);
1841 else return (65 + rand()%24);
1845 Svg_parser::getRed(String hex){
1847 return hextodec(hex.substr(1,2));
1848 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1849 int start=hex.find_first_of("(")+1;
1850 int end =hex.find_last_of(")");
1851 String aux=tokenize(hex.substr(start,end-start),",").at(0);
1852 return atoi(aux.data());
1857 Svg_parser::getGreen(String hex){
1859 return hextodec(hex.substr(3,2));
1860 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1861 int start=hex.find_first_of("(")+1;
1862 int end =hex.find_last_of(")");
1863 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1864 return atoi(aux.data());
1869 Svg_parser::getBlue(String hex){
1871 return hextodec(hex.substr(5,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(2);
1876 return atoi(aux.data());
1881 Svg_parser::hextodec(String hex){
1890 else if(hex.at(i)=='1')
1892 else if(hex.at(i)=='2')
1894 else if(hex.at(i)=='3')
1896 else if(hex.at(i)=='4')
1898 else if(hex.at(i)=='5')
1900 else if(hex.at(i)=='6')
1902 else if(hex.at(i)=='7')
1904 else if(hex.at(i)=='8')
1906 else if(hex.at(i)=='9')
1908 else if(hex.at(i)=='a')
1910 else if(hex.at(i)=='b')
1912 else if(hex.at(i)=='c')
1914 else if(hex.at(i)=='d')
1916 else if(hex.at(i)=='e')
1918 else if(hex.at(i)=='f')
1926 result+=pow(16,i)*ihex[top-i-1];
1934 Svg_parser::getDimension(const String ac){
1938 int length=ac.size();
1940 if(isdigit(ac.at(length-1))){
1942 }else if(ac.at(length-1)=='%'){
1945 String mtc=ac.substr(length-2,length);
1946 String nmc=ac.substr(0,length-2);
1947 if(mtc.compare("px")==0){
1948 af=atof(nmc.data());
1949 }else if(mtc.compare("pt")==0){
1950 af=atof(nmc.data())*1.25;
1951 }else if(mtc.compare("em")==0){
1952 af=atof(nmc.data())*16;
1953 }else if(mtc.compare("mm")==0){
1954 af=atof(nmc.data())*3.54;
1955 }else if(mtc.compare("pc")==0){
1956 af=atof(nmc.data())*15;
1957 }else if(mtc.compare("cm")==0){
1958 af=atof(nmc.data())*35.43;
1959 }else if(mtc.compare("in")==0){
1960 af=atof(nmc.data())*90;
1969 Svg_parser::getRadian(float sexa){
1970 return (sexa*2*PI)/360;
1973 Svg_parser::removeS(String *input){
1974 for(unsigned int i=0;i<input->size();i++){
1975 if(input->at(i)==' '){
1981 Svg_parser::removeIntoS(String *input){
1983 for(unsigned int i=0;i<input->size();i++){
1984 if(input->at(i)=='('){
1986 }else if(input->at(i)==')'){
1988 }else if(into && input->at(i)==' '){
1994 Svg_parser::tokenize(const String& str,const String& delimiters){
1995 std::vector<String> tokens;
1996 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
1997 String::size_type pos = str.find_first_of(delimiters, lastPos);
1998 while (String::npos != pos || String::npos != lastPos){
1999 tokens.push_back(str.substr(lastPos, pos - lastPos));
2000 lastPos = str.find_first_not_of(delimiters, pos);
2001 pos = str.find_first_of(delimiters, lastPos);
2006 Svg_parser::new_guid(){
2008 return GUID::hasher(uid).get_string();