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 ======================================================= */
36 #include "svg_parser.h"
38 /* === U S I N G =========================================================== */
40 using namespace synfig;
42 /* === G L O B A L S ======================================================= */
46 //Seperate transformations: apply transformations on a per-layer basis, rather than on canvases
47 #define SVG_SEP_TRANSFORMS 1
49 //Resolve BLine transformations: resolve transformations instead of creating transformation layers
50 #define SVG_RESOLVE_BLINE 1
52 /* === P R O C E D U R E S ================================================= */
55 synfig::open_svg(std::string _filepath,String &errors, String &warnings){
56 Canvas::Handle canvas;
60 canvas=parser.load_svg_canvas(_filepath,errors,warnings);
61 //canvas->set_id(parser.get_id());
63 std::cout<<"error"<<std::endl;
69 Svg_parser::load_svg_canvas(std::string _filepath,String &errors, String &warnings){
71 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
73 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
75 parser.set_substitute_entities();
76 parser.parse_file(filepath);
79 const xmlpp::Node* pNode = parser.get_document()->get_root_node();
82 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
83 }catch(const std::exception& ex){
84 std::cout << "Exception caught: " << ex.what() << std::endl;
86 #endif //LIBXMLCPP_EXCEPTIONS_ENABLED
87 Canvas::Handle canvas;
89 //canvas=synfig::open_canvas(nodeRoot,_filepath,errors,warnings);
90 canvas=synfig::open_canvas(nodeRoot,errors,warnings);
95 Svg_parser::Svg_parser(){
98 set_canvas=0;//we must run parser_canvas method
99 // 0.5 in gamma parameter of color correct layer is 1/0.5 = 2 (thinking) it must be 2.2!!!!
100 gamma.set_gamma(2.2);
104 Svg_parser::get_id(){
105 if(!id_name.empty()) return id_name;
109 Svg_parser::set_id(String source){
110 const char bad_chars[]=" :#@$^&()*";
111 int start= source.find_last_of('/')+1;
112 int end= source.find_last_of('.');
113 String x=source.substr(start,end-start);
115 for(unsigned int i=0;i<sizeof(bad_chars);i++){
116 unsigned int pos=x.find_first_of(bad_chars[i]);
117 if(pos!=String::npos)
124 id_name="id_arbitrario";
131 /* === PARSERS ============================================================= */
134 Svg_parser::parser_node(const xmlpp::Node* node){
135 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
136 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
137 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
139 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
142 Glib::ustring nodename = node->get_name();
143 if(!nodeText && !nodeComment && !nodename.empty()){
144 if(nodename.compare("svg")==0){
146 }else if(nodename.compare("namedview")==0){
148 }else if(nodename.compare("defs")==0){
151 if(set_canvas==0) parser_canvas (node);
152 parser_graphics(node,nodeRoot,"",NULL);
153 if(nodename.compare("g")==0) return;
157 xmlpp::Node::NodeList list = node->get_children();
158 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
159 parser_node(*iter); //recursive
166 Svg_parser::parser_svg (const xmlpp::Node* node){
167 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
168 width =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("width")));
169 height =etl::strprintf("%f",getDimension(nodeElement->get_attribute_value("height")));
170 docname=nodeElement->get_attribute_value("docname","");
174 Svg_parser::parser_canvas (const xmlpp::Node* node){
175 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
176 if(width.compare("")==0){
177 width=nodeElement->get_attribute_value("width","");
179 if(height.compare("")==0){
180 height=nodeElement->get_attribute_value("height","");
182 if(width.compare("")==0 && height.compare("")!=0){
185 if(width.compare("")!=0 && height.compare("")==0){
188 if(height.compare("")==0 && width.compare("")==0){
193 nodeRoot=document.create_root_node("canvas", "", "");
194 nodeRoot->set_attribute("version","0.5");
195 nodeRoot->set_attribute("width",width);
196 nodeRoot->set_attribute("height",height);
197 nodeRoot->set_attribute("xres","2834.645752");
198 nodeRoot->set_attribute("yres","2834.645752");
201 view_x=atof(width.c_str())/kux;
202 view_y=atof(height.c_str())/kux;
205 char attr_view_box[60];
206 sprintf(attr_view_box,"%f %f %f %f",-1.0*view_x,view_y,view_x,-1.0*view_y);
207 nodeRoot->set_attribute("view-box",attr_view_box);
208 ox=atof(width.c_str() )/2;
209 oy=atof(height.c_str())/2;
210 nodeRoot->set_attribute("antialias","1");
211 nodeRoot->set_attribute("fps","24.000");
212 nodeRoot->set_attribute("begin-time","0f");
213 nodeRoot->set_attribute("end-time","5s");
214 nodeRoot->set_attribute("bgcolor","0.500000 0.500000 0.500000 1.000000");
215 if(!id_name.empty()) nodeRoot->add_child("name")->set_child_text(id_name);
216 else nodeRoot->add_child("name")->set_child_text("Synfig Animation 1");
222 Svg_parser::parser_graphics(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent){
223 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
224 Glib::ustring nodename = node->get_name();
225 if (nodename.compare("g")==0 || nodename.compare("path")==0 || nodename.compare("polygon")==0 || nodename.compare("rect")==0){} else return;
227 //load sub-attributes
228 Glib::ustring id =nodeElement->get_attribute_value("id");
229 Glib::ustring transform =nodeElement->get_attribute_value("transform");
231 //resolve transformations
233 if(!transform.empty())
234 mtx=parser_transform (transform);
235 if (SVG_SEP_TRANSFORMS)
239 composeMatrix(&mtx,mtx_parent,mtx);
241 mtx=newMatrix(mtx_parent);
244 if(nodename.compare("g")==0){
245 parser_layer (node,root->add_child("layer"),parent_style,mtx);
249 Glib::ustring obj_style =nodeElement->get_attribute_value("style");
250 Glib::ustring obj_fill =nodeElement->get_attribute_value("fill");
253 String fill =loadAttribute("fill",obj_style,parent_style,obj_fill,"none");
254 String fill_rule =loadAttribute("fill-rule",obj_style,parent_style,"evenodd");
255 String stroke =loadAttribute("stroke",obj_style,parent_style,"none");
256 String stroke_width =loadAttribute("stroke-width",obj_style,parent_style,"1px");
257 String stroke_linecap =loadAttribute("stroke-linecap",obj_style,parent_style,"butt");
258 String stroke_linejoin =loadAttribute("stroke-linejoin",obj_style,parent_style,"miter");
259 String stroke_opacity =loadAttribute("stroke-opacity",obj_style,parent_style,"1");
260 String fill_opacity =loadAttribute("fill-opacity",obj_style,parent_style,"1");
261 String opacity =loadAttribute("opacity",obj_style,parent_style,"1");
265 int typeFill=0; //nothing
267 if(fill.compare("none")!=0){
270 if(typeFill==1 && fill.compare(0,3,"url")==0){
271 typeFill=2; //gradient
274 int typeStroke=0;//nothing
276 if(stroke.compare("none")!=0){
277 typeStroke=1; //simple
279 if(typeStroke==1 && stroke.compare(0,3,"url")==0){
280 typeStroke=2; //gradient
283 xmlpp::Element* child_layer = root;
284 xmlpp::Element* child_fill;
285 xmlpp::Element* child_stroke;
288 if(nodename.compare("rect")==0 && typeFill!=0){
289 if (mtx) child_layer = nodeStartBasicLayer(root->add_child("layer"), id);
290 child_fill=child_layer;
291 parser_rect(nodeElement,child_fill,fill,fill_opacity,opacity);
293 build_fill (child_fill,fill,NULL);
295 parser_effects(nodeElement,child_layer,parent_style,mtx);
298 if ((!(SVG_RESOLVE_BLINE) && mtx) || typeFill==2 || typeStroke==2)
299 child_layer = nodeStartBasicLayer(root->add_child("layer"), id);
300 child_fill=child_layer;
301 child_stroke=child_layer;
303 //=======================================================================
306 //if we are creating a bline
308 //First, create the list of Verteces
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
328 child_fill=nodeStartBasicLayer(child_fill->add_child("layer"));
330 for (aux = k.begin(); aux!=k.end(); aux++){
331 xmlpp::Element *child_region=child_fill->add_child("layer");
332 child_region->set_attribute("type","region");
333 child_region->set_attribute("active","true");
334 child_region->set_attribute("version","0.1");
335 child_region->set_attribute("desc",id);
336 build_param (child_region->add_child("param"),"z_depth","real","0.0000000000");
337 build_param (child_region->add_child("param"),"amount","real","1.0000000000");
338 build_param (child_region->add_child("param"),"blend_method","integer","0");
339 build_color (child_region->add_child("param"),getRed(fill),getGreen(fill),getBlue(fill),atof(fill_opacity.data())*atof(opacity.data()));
340 build_vector (child_region->add_child("param"),"offset",0,0, *(*aux)->offset_id );
341 build_param (child_region->add_child("param"),"invert","bool","false");
342 build_param (child_region->add_child("param"),"antialias","bool","true");
343 build_param (child_region->add_child("param"),"feather","real","0.0000000000");
344 build_param (child_region->add_child("param"),"blurtype","integer","1");
345 if(fill_rule.compare("evenodd")==0) build_param (child_region->add_child("param"),"winding_style","integer","1");
346 else build_param (child_region->add_child("param"),"winding_style","integer","0");
348 build_bline (child_region->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
351 if(typeFill==2){ //gradient in onto mode (fill)
352 if (SVG_RESOLVE_BLINE)
353 build_fill(child_fill,fill,mtx);
355 build_fill(child_fill,fill,NULL);
358 if(typeStroke!=0){//outline layer
360 child_stroke=nodeStartBasicLayer(child_stroke->add_child("layer"),"stroke");
362 for (aux=k.begin(); aux!=k.end(); aux++){
363 xmlpp::Element *child_outline=child_stroke->add_child("layer");
364 child_outline->set_attribute("type","outline");
365 child_outline->set_attribute("active","true");
366 child_outline->set_attribute("version","0.2");
367 child_outline->set_attribute("desc",id);
368 build_param (child_outline->add_child("param"),"z_depth","real","0.0000000000");
369 build_param (child_outline->add_child("param"),"amount","real","1.0000000000");
370 build_param (child_outline->add_child("param"),"blend_method","integer","0");
371 build_color (child_outline->add_child("param"),getRed(stroke),getGreen(stroke),getBlue(stroke),atof(stroke_opacity.data())*atof(opacity.data()));
372 build_vector (child_outline->add_child("param"),"offset",0,0,*(*aux)->offset_id);
373 build_param (child_outline->add_child("param"),"invert","bool","false");
374 build_param (child_outline->add_child("param"),"antialias","bool","true");
375 build_param (child_outline->add_child("param"),"feather","real","0.0000000000");
376 build_param (child_outline->add_child("param"),"blurtype","integer","1");
378 build_param (child_outline->add_child("param"),"winding_style","integer","0");
380 build_bline (child_outline->add_child("param"),*(*aux)->points,(*aux)->loop,*(*aux)->bline_id);
382 stroke_width=etl::strprintf("%f",getDimension(stroke_width)/kux);
383 build_param (child_outline->add_child("param"),"width","real",stroke_width);
384 build_param (child_outline->add_child("param"),"expand","real","0.0000000000");
385 if(stroke_linejoin.compare("miter")==0) build_param (child_outline->add_child("param"),"sharp_cusps","bool","true");
386 else build_param (child_outline->add_child("param"),"sharp_cusps","bool","false");
387 if(stroke_linecap.compare("butt")==0){
388 build_param (child_outline->add_child("param"),"round_tip[0]","bool","false");
389 build_param (child_outline->add_child("param"),"round_tip[1]","bool","false");
391 build_param (child_outline->add_child("param"),"round_tip[0]","bool","true");
392 build_param (child_outline->add_child("param"),"round_tip[1]","bool","true");
394 build_param (child_outline->add_child("param"),"loopyness","real","1.0000000000");
395 build_param (child_outline->add_child("param"),"homogeneous_width","bool","true");
398 if(typeStroke==2){ //gradient in onto mode (stroke)
399 if (SVG_RESOLVE_BLINE)
400 build_fill(child_stroke,stroke,mtx);
402 build_fill(child_stroke,stroke,NULL);
406 if (SVG_RESOLVE_BLINE)
407 parser_effects(nodeElement,child_layer,parent_style,NULL);
409 parser_effects(nodeElement,child_layer,parent_style,mtx);
413 /* === LAYER PARSERS ======================================================= */
416 Svg_parser::parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx){
417 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
418 Glib::ustring label =nodeElement->get_attribute_value("label");
419 Glib::ustring style =nodeElement->get_attribute_value("style");
420 Glib::ustring fill =nodeElement->get_attribute_value("fill");
425 }else if(!fill.empty()){
426 layer_style.append("fill:");
427 layer_style.append(fill);
428 }else if(!parent_style.empty()){
429 layer_style=parent_style;
432 root->set_attribute("type","PasteCanvas");
433 root->set_attribute("active","true");
434 root->set_attribute("version","0.1");
435 if(!label.empty()) root->set_attribute("desc",label);
436 else root->set_attribute("desc","Inline Canvas");
438 build_real(root->add_child("param"),"z_depth",0.0);
439 build_real(root->add_child("param"),"amount",1.0);
440 build_integer(root->add_child("param"),"blend_method",0);
441 build_vector (root->add_child("param"),"origin",0,0);
443 //printf(" canvas attributes ");
445 xmlpp::Element *child_canvas=root->add_child("param");
446 child_canvas->set_attribute("name","canvas");
447 child_canvas=child_canvas->add_child("canvas");
448 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
450 xmlpp::Node::NodeList list = node->get_children();
451 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
452 Glib::ustring name =(*iter)->get_name();
453 parser_graphics (*iter,child_canvas,layer_style,mtx);
456 if (SVG_SEP_TRANSFORMS) parser_effects(nodeElement,child_canvas,parent_style,NULL);
457 else parser_effects(nodeElement,child_canvas,parent_style,mtx);
462 Svg_parser::parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity){
463 Glib::ustring rect_id =nodeElement->get_attribute_value("id");
464 Glib::ustring rect_x =nodeElement->get_attribute_value("x");
465 Glib::ustring rect_y =nodeElement->get_attribute_value("y");
466 Glib::ustring rect_width =nodeElement->get_attribute_value("width");
467 Glib::ustring rect_height =nodeElement->get_attribute_value("height");
469 xmlpp::Element *child_rect=root->add_child("layer");
470 child_rect->set_attribute("type","rectangle");
471 child_rect->set_attribute("active","true");
472 child_rect->set_attribute("version","0.2");
473 child_rect->set_attribute("desc",rect_id);
475 build_real(child_rect->add_child("param"),"z_depth",0.0);
476 build_real(child_rect->add_child("param"),"amount",1.0);
477 build_integer(child_rect->add_child("param"),"blend_method",0);
478 build_color (child_rect->add_child("param"),getRed (fill),getGreen (fill),getBlue(fill),atof(opacity.data())*atof(fill_opacity.data()));
480 float auxx=atof(rect_x.c_str());
481 float auxy=atof(rect_y.c_str());
482 coor2vect(&auxx,&auxy);
483 build_vector (child_rect->add_child("param"),"point1",auxx,auxy);
484 auxx= atof(rect_x.c_str()) + atof(rect_width.c_str());
485 auxy= atof(rect_y.c_str()) + atof(rect_height.c_str());
486 coor2vect(&auxx,&auxy);
487 build_vector (child_rect->add_child("param"),"point2",auxx,auxy);
492 /* === CONVERT TO PATH PARSERS ============================================= */
495 Svg_parser::parser_path_polygon(Glib::ustring polygon_points, Matrix* mtx){
496 std::list<BLine *> k0;
497 if(polygon_points.empty())
499 std::list<Vertex*> points;
500 std::vector<String> tokens=get_tokens_path (polygon_points);
502 float ax,ay; ax=ay=0;
503 for(i=0;i<tokens.size();i++){
504 ax=atof(tokens.at(i).data());
505 i++; if(tokens.at(i).compare(",")==0) i++;
506 ay=atof(tokens.at(i).data());
508 if(mtx) transformPoint2D(mtx,&ax,&ay);
512 points.push_back(newVertex(ax,ay));
514 k0.push_front(newBLine(&points, true));
519 Svg_parser::parser_path_d(String path_d,Matrix* mtx){
520 std::list<BLine *> k;
521 std::list<Vertex*> k1;
523 std::vector<String> tokens=get_tokens_path(path_d);
524 String command="M"; //the current command
525 float ax,ay,tgx,tgy,tgx2,tgy2;//each method
527 float actual_x=0,actual_y=0; //in svg coordinate space
528 float old_x=0,old_y=0; //needed in rare cases
529 float init_x=0,init_y=0; //for closepath commands
531 for(unsigned int i=0;i<tokens.size();i++){
532 //if the token is a command, change the current command
533 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) {
534 command=tokens.at(i);
540 //if command is absolute, set actual_x/y to zero
541 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) {
546 //now parse the commands
547 if(command.compare("M")==0 || command.compare("m")==0){ //move to
549 k.push_front(newBLine(&k1, false));
553 actual_x+=atof(tokens.at(i).data());
554 i++; if(tokens.at(i).compare(",")==0) i++;
555 actual_y+=atof(tokens.at(i).data());
562 if(mtx) transformPoint2D(mtx,&ax,&ay);
564 k1.push_back(newVertex (ax,ay)); //first element
565 setSplit(k1.back(),TRUE);
566 //"If a moveto is followed by multiple pairs of coordinates,
567 // the subsequent pairs are treated as implicit lineto commands."
568 if (command.compare("M")==0)
572 }else if(command.compare("C")==0 || command.compare("c")==0){ //curve
574 tgx2=actual_x+atof(tokens.at(i).data());
575 i++; if(tokens.at(i).compare(",")==0) i++;
576 tgy2=actual_y+atof(tokens.at(i).data());
578 i++; tgx=actual_x+atof(tokens.at(i).data());
579 i++; if(tokens.at(i).compare(",")==0) i++;
580 tgy=actual_y+atof(tokens.at(i).data());
582 i++; actual_x+=atof(tokens.at(i).data());
583 i++; if(tokens.at(i).compare(",")==0) i++;
584 actual_y+=atof(tokens.at(i).data());
590 transformPoint2D(mtx,&tgx2,&tgy2);
591 transformPoint2D(mtx,&ax,&ay);
592 transformPoint2D(mtx,&tgx,&tgy);
595 coor2vect(&tgx2,&tgy2);
597 coor2vect(&tgx,&tgy);
599 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
600 if(isFirst(k1.front(),ax,ay)){
601 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
603 k1.push_back(newVertex (ax,ay));
604 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
605 setSplit(k1.back(),TRUE);
607 }else if(command.compare("Q")==0 || command.compare("q")==0){ //quadractic curve
609 tgx=actual_x+atof(tokens.at(i).data());
610 i++; if(tokens.at(i).compare(",")==0) i++;
611 tgy=actual_y+atof(tokens.at(i).data());
613 i++; actual_x+=atof(tokens.at(i).data());
614 i++; if(tokens.at(i).compare(",")==0) i++;
615 actual_y+=atof(tokens.at(i).data());
621 transformPoint2D(mtx,&ax,&ay);
622 transformPoint2D(mtx,&tgx,&tgy);
626 coor2vect(&tgx,&tgy);
628 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
629 setSplit(k1.back(),FALSE);
630 k1.push_back(newVertex (ax,ay));
631 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
632 }else if(command.compare("L")==0 || command.compare("l")==0){ //line to
634 actual_x+=atof(tokens.at(i).data());
635 i++; if(tokens.at(i).compare(",")==0) i++;
636 actual_y+=atof(tokens.at(i).data());
641 if(mtx) transformPoint2D(mtx,&ax,&ay);
645 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
646 if(isFirst(k1.front(),ax,ay)){
647 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
649 k1.push_back(newVertex(ax,ay));
650 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
652 }else if(command.compare("H")==0 || command.compare("h")==0){// horizontal move
653 //the same that L but only Horizontal movement
655 actual_x+=atof(tokens.at(i).data());
660 if(mtx) transformPoint2D(mtx,&ax,&ay);
664 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
665 if(isFirst(k1.front(),ax,ay)){
666 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
668 k1.push_back(newVertex(ax,ay));
669 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
671 }else if(command.compare("V")==0 || command.compare("v")==0){//vertical
673 actual_y+=atof(tokens.at(i).data());
678 if(mtx) transformPoint2D(mtx,&ax,&ay);
682 setTg2(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
683 if(isFirst(k1.front(),ax,ay)){
684 setTg1(k1.front(),k1.front()->x,k1.front()->y,k1.front()->x,k1.front()->y);
686 k1.push_back(newVertex(ax,ay));
687 setTg1(k1.back(),k1.back()->x,k1.back()->y,k1.back()->x,k1.back()->y);
689 }else if(command.compare("T")==0 || command.compare("t")==0){// I don't know what does it
690 actual_x+=atof(tokens.at(i).data());
691 i++; if(tokens.at(i).compare(",")==0) i++;
692 actual_y+=atof(tokens.at(i).data());
693 }else if(command.compare("A")==0 || command.compare("a")==0){//elliptic arc
695 //isn't complete support, is only for circles
697 //this curve have 6 parameters
699 float radius_x,radius_y;
703 radius_x=atof(tokens.at(i).data());
704 i++; if(tokens.at(i).compare(",")==0) i++;
705 radius_y=atof(tokens.at(i).data());
707 i++; angle=atof(tokens.at(i).data());
709 i++; large=atoi(tokens.at(i).data());
710 i++; sweep=atoi(tokens.at(i).data());
712 i++; actual_x+=atof(tokens.at(i).data());
713 i++; if(tokens.at(i).compare(",")==0) i++;
714 actual_y+=atof(tokens.at(i).data());
716 if(!large && !sweep){
718 tgx2 = old_x + radius_x*0.5;
721 tgy = actual_y + radius_y*0.5;
727 transformPoint2D(mtx,&tgx2,&tgy2);
728 transformPoint2D(mtx,&ax,&ay);
729 transformPoint2D(mtx,&tgx,&tgy);
732 coor2vect(&tgx2,&tgy2);
734 coor2vect(&tgx,&tgy);
736 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
737 if(isFirst(k1.front(),ax,ay)){
738 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
740 k1.push_back(newVertex (ax,ay));
741 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
742 setSplit(k1.back(),TRUE);
744 }else if(!large && sweep){
747 tgy2 = old_y + radius_y*0.5;
748 tgx = actual_x + radius_x*0.5;
755 transformPoint2D(mtx,&tgx2,&tgy2);
756 transformPoint2D(mtx,&ax,&ay);
757 transformPoint2D(mtx,&tgx,&tgy);
760 coor2vect(&tgx2,&tgy2);
762 coor2vect(&tgx,&tgy);
764 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
765 if(isFirst(k1.front(),ax,ay)){
766 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
768 k1.push_back(newVertex (ax,ay));
769 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
770 setSplit(k1.back(),TRUE);
772 }else if( large && !sweep){//rare
773 //this need more than one vertex
774 }else if( large && sweep){//circles in inkscape are made with this kind of arc
777 if(old_x>actual_x) sense =-1;
778 float in_x,in_y,in_tgx1,in_tgy1,in_tgx2,in_tgy2;
779 in_x = (old_x+actual_x)/2;
780 in_y = old_y - sense*radius_y;
781 in_tgx1 = in_x - sense*(radius_x*0.5);
782 in_tgx2 = in_x + sense*(radius_x*0.5);
787 tgy2=actual_y - sense*(radius_y*0.5);
789 tgy =actual_y - sense*(radius_y*0.5);
795 transformPoint2D(mtx,&tgx2,&tgy2);
796 transformPoint2D(mtx,&tgx ,&tgy );
797 transformPoint2D(mtx,&ax,&ay);
799 transformPoint2D(mtx,&in_tgx2,&in_tgy2);
800 transformPoint2D(mtx,&in_tgx1,&in_tgy1);
801 transformPoint2D(mtx,&in_x,&in_y);
804 coor2vect(&tgx2 , &tgy2);
805 coor2vect(&ax , &ay );
806 coor2vect(&tgx , &tgy );
808 coor2vect(&in_tgx2 , &in_tgy2);
809 coor2vect(&in_tgx1 , &in_tgy1);
810 coor2vect(&in_x , &in_y );
813 setTg2(k1.back(),k1.back()->x,k1.back()->y,tgx2,tgy2);
814 //save the intermediate point
815 k1.push_back(newVertex (in_x,in_y));
816 setTg1(k1.back(),k1.back()->x,k1.back()->y, in_tgx1 , in_tgy1);
817 setTg2(k1.back(),k1.back()->x,k1.back()->y, in_tgx2 , in_tgy2);
818 setSplit(k1.back(),TRUE); //this could be changed
820 if(isFirst(k1.front(),ax,ay)){
821 setTg1(k1.front(),k1.front()->x,k1.front()->y,tgx,tgy);
823 k1.push_back(newVertex (ax,ay));
824 setTg1(k1.back(),k1.back()->x,k1.back()->y,tgx,tgy);
825 setSplit(k1.back(),TRUE);
828 }else if(command.compare("z")==0){
829 k.push_front(newBLine(&k1, true));
831 if (i<tokens.size() && tokens.at(i).compare("M")!=0 && tokens.at(i).compare("m")!=0) {
832 //starting a new path, but not with a moveto
838 if(mtx) transformPoint2D(mtx,&ax,&ay);
840 k1.push_back(newVertex (ax,ay)); //first element
841 setSplit(k1.back(),TRUE);
843 i--; //decrement i to balance "i++" at command change
845 std::cout<<"unsupported path token: "<<tokens.at(i)<<std::endl;
849 k.push_front(newBLine(&k1, false)); //last element
854 /* === EFFECTS PARSERS ===================================================== */
857 Svg_parser::parser_effects(const xmlpp::Element* /*nodeElement*/,xmlpp::Element* root,String /*parent_style*/,Matrix* mtx){
858 build_transform(root, mtx);
861 /* === DEFS PARSERS ======================================================== */
864 Svg_parser::parser_defs(const xmlpp::Node* node){
865 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
867 xmlpp::Node::NodeList list = node->get_children();
868 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
869 Glib::ustring name =(*iter)->get_name();
870 if(name.compare("linearGradient")==0){
871 parser_linearGradient(*iter);
872 }else if(name.compare("radialGradient")==0){
873 parser_radialGradient(*iter);
879 /* === BUILDS ============================================================== */
882 Svg_parser::build_transform(xmlpp::Element* root,Matrix* mtx){
884 xmlpp::Element *child_transform=root->add_child("layer");
885 child_transform->set_attribute("type","warp");
886 child_transform->set_attribute("active","true");
887 child_transform->set_attribute("version","0.1");
888 child_transform->set_attribute("desc","Transform");
891 x=100;y=100;coor2vect(&x,&y);
892 build_vector (child_transform->add_child("param"),"src_tl",x,y);
894 x=200;y=200;coor2vect(&x,&y);
895 build_vector (child_transform->add_child("param"),"src_br",x,y);
899 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
900 build_vector (child_transform->add_child("param"),"dest_tl",x,y);
903 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
904 build_vector (child_transform->add_child("param"),"dest_tr",x,y);
907 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
908 build_vector (child_transform->add_child("param"),"dest_br",x,y);
911 transformPoint2D(mtx,&x,&y);coor2vect(&x,&y);
912 build_vector (child_transform->add_child("param"),"dest_bl",x,y);
914 build_param (child_transform->add_child("param"),"clip","bool","false");
915 build_param (child_transform->add_child("param"),"horizon","real","4.0");
919 std::list<ColorStop*>*
920 Svg_parser::find_colorStop(String name){
922 if(lg.empty()&& rg.empty())
926 if(find.at(0)=='#') find.erase(0,1);
928 std::list<LinearGradient*>::iterator aux=lg.begin();
929 while(aux!=lg.end()){//only find into linear gradients
930 if(find.compare((*aux)->name)==0){
931 return (*aux)->stops;
940 Svg_parser::build_fill(xmlpp::Element* root, String name,Matrix *mtx){
942 int start=name.find_first_of("#")+1;
943 int end=name.find_first_of(")");
944 String find= name.substr(start,end-start);
945 bool encounter=false;
947 std::list<LinearGradient*>::iterator aux=lg.begin();
948 while(aux!=lg.end()){
949 if(find.compare((*aux)->name)==0){
950 build_linearGradient (root,*aux,mtx);
956 if(!encounter && !rg.empty()){
957 std::list<RadialGradient*>::iterator aux=rg.begin();
958 while(aux!=rg.end()){
959 if(find.compare((*aux)->name)==0){
960 build_radialGradient (root,*aux,mtx);
969 Svg_parser::build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops){
970 std::list<ColorStop*>::iterator aux_stop=stops->begin();
971 while(aux_stop!=stops->end()){
972 xmlpp::Element *child=root->add_child("color");
973 child->set_attribute("pos",etl::strprintf("%f",(*aux_stop)->pos));
974 child->add_child("r")->set_child_text(etl::strprintf("%f",(*aux_stop)->r));
975 child->add_child("g")->set_child_text(etl::strprintf("%f",(*aux_stop)->g));
976 child->add_child("b")->set_child_text(etl::strprintf("%f",(*aux_stop)->b));
977 child->add_child("a")->set_child_text(etl::strprintf("%f",(*aux_stop)->a));
982 Svg_parser::build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx){
984 xmlpp::Element* gradient=root->add_child("layer");
986 gradient->set_attribute("type","linear_gradient");
987 gradient->set_attribute("active","true");
988 gradient->set_attribute("desc",data->name);
989 build_param (gradient->add_child("param"),"z_depth","real","0");
990 build_param (gradient->add_child("param"),"amount","real","1");
992 build_param (gradient->add_child("param"),"blend_method","integer","21");
1000 if (mtx || data->transform){
1002 if (mtx && data->transform){
1003 composeMatrix(&mtx2,mtx,data->transform);
1006 }else if (data->transform){
1007 mtx2=data->transform;
1009 //matrix transforms the gradient as a whole
1010 //it does not preserve angles, so we cant' simply transform both points
1012 //set point (x3,y3) on the same gradient line as (x2,y2)
1013 //the gradient line is perpendicular to (x1,y1)(x2,y2)
1016 //transform everything
1017 transformPoint2D(mtx2,&x1,&y1);
1018 transformPoint2D(mtx2,&x2,&y2);
1019 transformPoint2D(mtx2,&x3,&y3);
1021 if (x2!=x3 && y2!=y3) {//divide by zero check
1023 //set k as slope between (x2,y2) and (x3,y3)
1024 //k is the slope of gradient lines post-transformation
1026 //set point (x2,y2) on the gradient line passing through (x3,y3)
1027 //so that the line (x1,y1)(x2,y2) is perpendicular to (x2,y2)(x3,y3)
1028 x2= (x3*k+x1/k+y1-y3)/(k+(1/k));
1030 } else if (x2==x3 && y2!=y3) {
1032 } else if (x2!=x3 && y2==y3) {
1035 std::cout<<"SVG Import warning: gradient points equal each other"<<std::endl;
1039 coor2vect (&x1,&y1);
1040 coor2vect (&x2,&y2);
1042 build_vector (gradient->add_child("param"),"p1",x1,y1);
1043 build_vector (gradient->add_child("param"),"p2",x2,y2);
1045 xmlpp::Element *child_stops=gradient->add_child("param");
1046 child_stops->set_attribute("name","gradient");
1047 child_stops->set_attribute("guid",GUID::hasher(data->name).get_string());
1048 build_stop_color (child_stops->add_child("gradient"),data->stops);
1049 build_param (gradient->add_child("param"),"loop","bool","false");
1050 build_param (gradient->add_child("param"),"zigzag","bool","false");
1054 Svg_parser::build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx){
1056 xmlpp::Element* gradient;
1058 if (mtx || data->transform) {
1059 xmlpp::Element* layer=root->add_child("layer");
1061 layer->set_attribute("type","PasteCanvas");
1062 layer->set_attribute("active","true");
1063 layer->set_attribute("version","0.1");
1064 layer->set_attribute("desc",data->name);
1065 build_param (layer->add_child("param"),"z_depth","real","0");
1066 build_param (layer->add_child("param"),"amount","real","1");
1067 build_param (layer->add_child("param"),"blend_method","integer","21"); //straight onto
1068 build_vector (layer->add_child("param"),"origin",0,0);
1069 xmlpp::Element *child=layer->add_child("param");
1070 child->set_attribute("name","canvas");
1071 xmlpp::Element* child_layer=child->add_child("canvas");
1073 gradient=child_layer->add_child("layer");
1074 gradient->set_attribute("desc",data->name);
1075 build_param (gradient->add_child("param"),"blend_method","integer","0"); //composite
1077 if (mtx && data->transform){
1078 composeMatrix(&mtx2,mtx,data->transform);
1081 }else if (data->transform){
1082 mtx2=data->transform;
1084 build_transform(child_layer,mtx2);
1087 gradient=root->add_child("layer");
1088 gradient->set_attribute("desc",data->name);
1089 build_param (gradient->add_child("param"),"blend_method","integer","21"); //straight onto
1092 gradient->set_attribute("type","radial_gradient");
1093 gradient->set_attribute("active","true");
1094 build_param (gradient->add_child("param"),"z_depth","real","0");
1095 build_param (gradient->add_child("param"),"amount","real","1");
1097 xmlpp::Element *child_stops=gradient->add_child("param");
1098 child_stops->set_attribute("name","gradient");
1099 child_stops->set_attribute("guid",GUID::hasher(data->name).get_string());
1100 build_stop_color (child_stops->add_child("gradient"),data->stops);
1102 //here the center point and radius
1108 coor2vect (&cx,&cy);
1110 build_vector (gradient->add_child("param"),"center",cx,cy);
1111 build_param (gradient->add_child("param"),"radius","real",r);
1113 build_param (gradient->add_child("param"),"loop","bool","false");
1114 build_param (gradient->add_child("param"),"zigzag","bool","false");
1119 Svg_parser::parser_linearGradient(const xmlpp::Node* node){
1120 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1121 Glib::ustring id =nodeElement->get_attribute_value("id");
1122 float x1 =atof(nodeElement->get_attribute_value("x1").data());
1123 float y1 =atof(nodeElement->get_attribute_value("y1").data());
1124 float x2 =atof(nodeElement->get_attribute_value("x2").data());
1125 float y2 =atof(nodeElement->get_attribute_value("y2").data());
1126 Glib::ustring link =nodeElement->get_attribute_value("href");
1127 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1129 //resolve transformations
1131 if(!transform.empty())
1132 mtx=parser_transform (transform);
1134 std::list<ColorStop*> *stops;
1136 stops=find_colorStop (link);
1139 stops=new std::list<ColorStop*>();
1140 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
1142 xmlpp::Node::NodeList list = node->get_children();
1143 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter){
1144 Glib::ustring name =(*iter)->get_name();
1145 if(name.compare("stop")==0){
1146 const xmlpp::Element* nodeIter = dynamic_cast<const xmlpp::Element*>(*iter);
1147 Glib::ustring style =nodeIter->get_attribute_value("style");
1148 float offset=atof(nodeIter->get_attribute_value("offset").data());
1152 extractSubAttribute (style,"stop-color",&stop_color);
1153 extractSubAttribute (style,"stop-opacity",&opacity);
1155 if(opacity.empty()) opacity="1";
1156 if(stop_color.empty()) stop_color="#000000";//black for default :S
1157 stops->push_back(newColorStop(stop_color,atof(opacity.data()),offset));
1163 lg.push_back(newLinearGradient(id,x1,y1,x2,y2,stops,mtx));
1168 Svg_parser::parser_radialGradient(const xmlpp::Node* node){
1169 if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node)){
1170 Glib::ustring id =nodeElement->get_attribute_value("id");
1171 float cx =atof(nodeElement->get_attribute_value("cx").data());
1172 float cy =atof(nodeElement->get_attribute_value("cy").data());
1173 float fx =atof(nodeElement->get_attribute_value("fx").data());
1174 float fy =atof(nodeElement->get_attribute_value("fy").data());
1175 float r =atof(nodeElement->get_attribute_value("r").data());
1176 Glib::ustring link =nodeElement->get_attribute_value("href");//basic
1177 Glib::ustring transform =nodeElement->get_attribute_value("gradientTransform");
1179 if (cx!=fx || cy!=fy)
1180 std::cout<<"SVG Parser: ignoring focus attributes for radial gradient";
1182 //resolve transformations
1184 if(!transform.empty())
1185 mtx=parser_transform (transform);
1187 std::list<ColorStop*> *stops=NULL;
1189 //inkscape always use link, i dont need parser stops here, but it's posible
1190 stops=find_colorStop (link);
1193 rg.push_back(newRadialGradient(id,cx,cy,r,stops,mtx));
1198 Svg_parser::newColorStop(String color,float opacity,float pos){
1200 _stop=(ColorStop*)malloc(sizeof(ColorStop));
1201 float r=getRed(color);
1202 float g=getGreen(color);
1203 float b=getBlue(color);
1205 Color ret=adjustGamma(r/255,g/255,b/255,a);
1206 _stop->r=ret.get_r();
1207 _stop->g=ret.get_g();
1208 _stop->b=ret.get_b();
1209 _stop->a=ret.get_a();
1214 Svg_parser::adjustGamma(float r,float g,float b,float a){
1216 if(gamma.get_gamma_r()!=1.0){
1218 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
1220 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
1222 if(gamma.get_gamma_g()!=1.0){
1224 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
1226 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
1228 if(gamma.get_gamma_b()!=1.0){
1230 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
1232 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
1238 Svg_parser::newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform){
1239 LinearGradient* data;
1240 data=(LinearGradient*)malloc(sizeof(LinearGradient));
1241 sprintf(data->name,"%s",name.data());
1247 data->transform=transform;
1252 Svg_parser::newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform){
1253 RadialGradient* data;
1254 data=(RadialGradient*)malloc(sizeof(RadialGradient));
1255 sprintf(data->name,"%s",name.data());
1260 data->transform=transform;
1265 Svg_parser::newBLine(std::list<Vertex*> *points,bool loop){
1267 data=(BLine*)malloc(sizeof(BLine));
1268 //sprintf(data->name,"%s",name.data());
1269 data->points=new std::list<Vertex*> (*points);
1271 data->bline_id=new String(new_guid());
1272 data->offset_id=new String(new_guid());
1277 Svg_parser::build_gamma(xmlpp::Element* root,float gamma){
1278 root->set_attribute("type","colorcorrect");
1279 root->set_attribute("active","true");
1280 root->set_attribute("version","0.1");
1281 root->set_attribute("desc","Gamma");
1282 build_real (root->add_child("param"),"gamma",gamma);
1286 Svg_parser::build_translate(xmlpp::Element* root,float dx,float dy){
1287 root->set_attribute("type","translate");
1288 root->set_attribute("active","true");
1289 root->set_attribute("version","0.1");
1290 build_vector (root->add_child("param"),"origin",dx,dy);
1293 Svg_parser::build_rotate(xmlpp::Element* root,float dx,float dy,float angle){
1294 root->set_attribute("type","rotate");
1295 root->set_attribute("active","true");
1296 root->set_attribute("version","0.1");
1297 build_vector (root->add_child("param"),"origin",dx,dy);
1298 build_real (root->add_child("param"),"amount",angle);
1301 Svg_parser::build_points(xmlpp::Element* root,std::list<Vertex*> p){
1302 root->set_attribute("name","vector_list");
1303 xmlpp::Element *child=root->add_child("dynamic_list");
1304 child->set_attribute("type","vector");
1305 std::list<Vertex*>::iterator aux = p.begin();
1306 while(aux!=p.end()){
1307 xmlpp::Element *child_entry=child->add_child("entry");
1308 xmlpp::Element *child_vector=child_entry->add_child("vector");
1309 child_vector->add_child("x")->set_child_text(etl::strprintf("%f",(*aux)->x));
1310 child_vector->add_child("y")->set_child_text(etl::strprintf("%f",(*aux)->y));
1315 Svg_parser::build_vertex(xmlpp::Element* root , Vertex *p){
1316 xmlpp::Element *child_comp=root->add_child("composite");
1317 child_comp->set_attribute("type","bline_point");
1318 build_vector (child_comp->add_child("param"),"point",p->x,p->y);
1319 build_param (child_comp->add_child("width"),"","real","1.0000000000");
1320 build_param (child_comp->add_child("origin"),"","real","0.5000000000");
1321 if(p->split) build_param (child_comp->add_child("split"),"","bool","true");
1322 else build_param (child_comp->add_child("split"),"","bool","false");
1324 xmlpp::Element *child_t1=child_comp->add_child("t1");
1325 xmlpp::Element *child_rc=child_t1->add_child("radial_composite");
1326 child_rc->set_attribute("type","vector");
1327 build_param (child_rc->add_child("radius"),"","real",p->radius1);
1328 build_param (child_rc->add_child("theta"),"","angle",p->angle1);
1330 xmlpp::Element *child_t2=child_comp->add_child("t2");
1331 xmlpp::Element *child_rc2=child_t2->add_child("radial_composite");
1332 child_rc2->set_attribute("type","vector");
1333 build_param (child_rc2->add_child("radius"),"","real",p->radius2);
1334 build_param (child_rc2->add_child("theta"),"","angle",p->angle2);
1338 Svg_parser::build_bline(xmlpp::Element* root,std::list<Vertex*> p,bool loop,String blineguid){
1339 root->set_attribute("name","bline");
1340 xmlpp::Element *child=root->add_child("bline");
1341 child->set_attribute("type","bline_point");
1343 child->set_attribute("loop","true");
1345 child->set_attribute("loop","false");
1346 if(!blineguid.empty()) child->set_attribute("guid",blineguid);
1347 std::list<Vertex*>::iterator aux = p.begin();
1348 while(aux!=p.end()){
1349 if(*aux) build_vertex (child->add_child("entry"),*aux);
1355 Svg_parser::build_param(xmlpp::Element* root,String name,String type,String value){
1356 if(!type.empty() && !value.empty()){
1357 if(!name.empty()) root->set_attribute("name",name);
1358 xmlpp::Element *child=root->add_child(type);
1359 child->set_attribute("value",value);
1361 root->get_parent()->remove_child(root);
1365 Svg_parser::build_param(xmlpp::Element* root,String name,String type,float value){
1367 if(!name.empty()) root->set_attribute("name",name);
1368 xmlpp::Element *child=root->add_child(type);
1369 child->set_attribute("value",etl::strprintf ("%f",value));
1371 root->get_parent()->remove_child(root);
1375 Svg_parser::build_param(xmlpp::Element* root,String name,String type,int value){
1377 if(!name.empty()) root->set_attribute("name",name);
1378 xmlpp::Element *child=root->add_child(type);
1379 char *enteroc=new char[10];
1380 sprintf(enteroc,"%d",value);
1381 child->set_attribute("value",enteroc);
1384 root->get_parent()->remove_child(root);
1389 Svg_parser::build_integer(xmlpp::Element* root,String name,int value){
1390 if(name.compare("")!=0) root->set_attribute("name",name);
1391 xmlpp::Element *child=root->add_child("integer");
1392 char *enteroc=new char[10];
1393 sprintf(enteroc,"%d",value);
1394 child->set_attribute("value",enteroc);
1397 Svg_parser::build_real(xmlpp::Element* root,String name,float value){
1398 if(name.compare("")!=0) root->set_attribute("name",name);
1399 xmlpp::Element *child=root->add_child("real");
1400 char *realc=new char[20];
1401 sprintf(realc,"%f",value);
1402 child->set_attribute("value",realc);
1406 Svg_parser::build_color(xmlpp::Element* root,float r,float g,float b,float a){
1407 if(r>255 || g>255 || b>255 || a>1 || r<0 || g<0 || b<0 || a<0){
1408 root->get_parent()->remove_child(root);
1409 printf("Color aborted\n");
1412 Color ret=adjustGamma(r/255,g/255,b/255,a);
1414 root->set_attribute("name","color");
1415 xmlpp::Element *child=root->add_child("color");
1416 child->add_child("r")->set_child_text(etl::strprintf("%f",ret.get_r()));
1417 child->add_child("g")->set_child_text(etl::strprintf("%f",ret.get_g()));
1418 child->add_child("b")->set_child_text(etl::strprintf("%f",ret.get_b()));
1419 child->add_child("a")->set_child_text(etl::strprintf("%f",ret.get_a()));
1422 Svg_parser::build_vector(xmlpp::Element* root,String name,float x,float y){
1424 if(name.compare("")!=0) root->set_attribute("name",name);
1425 xmlpp::Element *child=root->add_child("vector");
1426 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1427 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1431 Svg_parser::build_vector (xmlpp::Element* root,String name,float x,float y,String guid){
1432 if(name.compare("")!=0) root->set_attribute("name",name);
1433 xmlpp::Element *child=root->add_child("vector");
1434 if(!guid.empty()) child->set_attribute("guid",guid);
1435 child->add_child("x")->set_child_text(etl::strprintf("%f",x));
1436 child->add_child("y")->set_child_text(etl::strprintf("%f",y));
1440 Svg_parser::nodeStartBasicLayer(xmlpp::Element* root, String name){
1441 root->set_attribute("type","PasteCanvas");
1442 root->set_attribute("active","true");
1443 root->set_attribute("version","0.1");
1444 root->set_attribute("desc",name);
1445 build_param (root->add_child("param"),"z_depth","real","0");
1446 build_param (root->add_child("param"),"amount","real","1");
1447 build_param (root->add_child("param"),"blend_method","integer","0");
1448 build_vector (root->add_child("param"),"origin",0,0);
1449 xmlpp::Element *child=root->add_child("param");
1450 child->set_attribute("name","canvas");
1451 return child->add_child("canvas");
1454 /* === COORDINATES & TRANSFORMATIONS ======================================= */
1456 Svg_parser::coor2vect(float *x,float *y){
1460 sy= atof(height.c_str())-sy;
1469 Svg_parser::setTg1(Vertex *p,float p1x,float p1y,float p2x,float p2y){
1471 float d1x,d1y,d2x,d2y,dx,dy;
1482 rd=sqrt(dx*dx + dy*dy);
1484 ag=PI + atan(dy/dx);
1485 }else if(dx>0 && dy<0){
1486 ag=PI + atan(dy/dx);
1487 }else if(dx<0 && dy<0){
1489 }else if(dx<0 && dy>0){
1490 ag= 2*PI+atan(dy/dx);
1491 }else if(dx==0 && dy>0){
1493 }else if(dx==0 && dy<0){
1495 }else if(dx==0 && dy==0){
1497 }else if(dx<0 && dy==0){
1499 }else if(dx>0 && dy==0){
1507 Svg_parser::setTg2(Vertex* p,float p1x,float p1y,float p2x,float p2y){
1509 float d1x,d1y,d2x,d2y,dx,dy;
1521 rd=sqrt(dx*dx + dy*dy);
1523 ag=PI + atan(dy/dx);
1524 // printf("case 180-270\n");
1525 }else if(dx>0 && dy<0){
1526 ag=PI + atan(dy/dx);
1527 // printf("case 90-180\n");
1528 }else if(dx<0 && dy<0){
1530 // printf("case 0-90\n");
1531 }else if(dx<0 && dy>0){
1532 ag= 2*PI+atan(dy/dx);
1533 // printf("case 270-360\n");
1534 }else if(dx==0 && dy>0){
1536 }else if(dx==0 && dy<0){
1538 }else if(dx==0 && dy==0){
1540 }else if(dx<0 && dy==0){
1542 }else if(dx>0 && dy==0){
1552 Svg_parser::setSplit(Vertex* p,bool val){
1558 Svg_parser::isFirst(Vertex* nodo,float a, float b){
1559 if(nodo->x==a && nodo->y==b)
1565 Svg_parser::newVertex(float x,float y){
1567 vert=(Vertex*)malloc(sizeof(Vertex));
1570 vert->radius1=vert->radius2=vert->angle1=vert->angle2=0;
1576 Svg_parser::parser_transform(const String transform){
1578 String tf(transform);
1580 std::vector<String> tokens=tokenize(tf," ");
1581 std::vector<String>::iterator aux=tokens.begin();
1582 while(aux!=tokens.end()){
1583 if((*aux).compare(0,9,"translate")==0){
1586 start =(*aux).find_first_of("(")+1;
1587 end =(*aux).find_first_of(",");
1588 dx =atof((*aux).substr(start,end-start).data());
1589 start =(*aux).find_first_of(",")+1;
1590 end =(*aux).size()-1;
1591 dy =atof((*aux).substr(start,end-start).data());
1593 a=newMatrix(1,0,0,1,dx,dy);
1595 multiplyMatrix(&a,newMatrix(1,0,0,1,dx,dy));
1596 }else if((*aux).compare(0,5,"scale")==0){
1598 a=newMatrix(1,0,0,1,0,0);
1599 }else if((*aux).compare(0,6,"rotate")==0){
1600 float angle,seno,coseno;
1602 start =(*aux).find_first_of("(")+1;
1603 end =(*aux).size()-1;
1604 angle=getRadian (atof((*aux).substr(start,end-start).data()));
1608 a=newMatrix(coseno,seno,-1*seno,coseno,0,0);
1610 multiplyMatrix(&a,newMatrix(coseno,seno,-1*seno,coseno,0,0));
1611 }else if((*aux).compare(0,6,"matrix")==0){
1612 int start =(*aux).find_first_of('(')+1;
1613 int end =(*aux).find_first_of(')');
1615 a=newMatrix((*aux).substr(start,end-start));
1617 multiplyMatrix(&a,newMatrix((*aux).substr(start,end-start)));
1619 a=newMatrix(1,0,0,1,0,0);
1627 Svg_parser::newMatrix(Matrix *a){
1629 data=(Matrix*)malloc(sizeof(Matrix));
1630 data->a=a->a; data->b=a->b; data->c=a->c;
1631 data->d=a->d; data->e=a->e; data->f=a->f;
1635 Svg_parser::newMatrix(float a,float b,float c,float d,float e,float f){
1637 data=(Matrix*)malloc(sizeof(Matrix));
1638 data->a=a; data->b=b; data->c=c;
1639 data->d=d; data->e=e; data->f=f;
1643 Svg_parser::newMatrix(const String mvector){
1644 if(!mvector.empty()){
1645 std::vector<String> tokens=tokenize(mvector,",");
1646 if(tokens.size()!=6) return newMatrix(1,0,0,1,0,0);
1647 Matrix* data=(Matrix*)malloc(sizeof(Matrix));
1648 data->a=atof(tokens.at(0).data());
1649 data->b=atof(tokens.at(1).data());
1650 data->c=atof(tokens.at(2).data());
1651 data->d=atof(tokens.at(3).data());
1652 data->e=atof(tokens.at(4).data());
1653 data->f=atof(tokens.at(5).data());
1656 return newMatrix(1,0,0,1,0,0);
1660 Svg_parser::transformPoint2D(Matrix *mtx,float *a,float *b){
1664 auxa= (*a)*(mtx->a) + (*b)*(mtx->c) + (mtx->e);
1665 auxb= (*a)*(mtx->b) + (*b)*(mtx->d) + (mtx->f);
1671 Svg_parser::composeMatrix(Matrix **mtx,Matrix* mtx1,Matrix* mtx2){
1672 Matrix* aux=newMatrix(0,0,0,0,0,0);
1673 aux->a=(mtx1->a)*(mtx2->a)+(mtx1->c)*(mtx2->b);
1674 aux->b=(mtx1->b)*(mtx2->a)+(mtx1->d)*(mtx2->b);
1675 aux->c=(mtx1->a)*(mtx2->c)+(mtx1->c)*(mtx2->d);
1676 aux->d=(mtx1->b)*(mtx2->c)+(mtx1->d)*(mtx2->d);
1677 aux->e=(mtx1->a)*(mtx2->e)+(mtx1->c)*(mtx2->f)+(mtx1->e);
1678 aux->f=(mtx1->b)*(mtx2->e)+(mtx1->d)*(mtx2->f)+(mtx1->f);
1682 Svg_parser::multiplyMatrix(Matrix **mtx1,Matrix *mtx2){
1683 Matrix* aux=newMatrix(0,0,0,0,0,0);
1684 aux->a=((*mtx1)->a)*(mtx2->a)+((*mtx1)->c)*(mtx2->b);
1685 aux->b=((*mtx1)->b)*(mtx2->a)+((*mtx1)->d)*(mtx2->b);
1686 aux->c=((*mtx1)->a)*(mtx2->c)+((*mtx1)->c)*(mtx2->d);
1687 aux->d=((*mtx1)->b)*(mtx2->c)+((*mtx1)->d)*(mtx2->d);
1688 aux->e=((*mtx1)->a)*(mtx2->e)+((*mtx1)->c)*(mtx2->f)+((*mtx1)->e);
1689 aux->f=((*mtx1)->b)*(mtx2->e)+((*mtx1)->d)*(mtx2->f)+((*mtx1)->f);
1698 Svg_parser::matrixIsNull(Matrix *mtx){
1699 if(mtx == NULL) return true;
1703 /* === EXTRA METHODS ======================================================= */
1706 Svg_parser::extractSubAttribute(const String attribute, String name,String* value){
1708 if(!attribute.empty()){
1709 String str(attribute);
1711 std::vector<String> tokens=tokenize(str,";");
1712 std::vector<String>::iterator aux=tokens.begin();
1713 while(aux!=tokens.end()){
1714 int mid= (*aux).find_first_of(":");
1715 if((*aux).substr(0,mid).compare(name)==0){
1716 int end=(*aux).size();
1717 *value=(*aux).substr(mid+1,end-mid);
1726 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String defaultVal){
1729 if(!path_style.empty())
1730 fnd=extractSubAttribute(path_style,name,&value);
1732 if(!master_style.empty())
1733 fnd=extractSubAttribute(master_style,name,&value);
1740 Svg_parser::loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal){
1743 if(!path_style.empty())
1744 fnd=extractSubAttribute(path_style,name,&value);
1745 if(fnd==0 && !master_style.empty())
1746 fnd=extractSubAttribute(master_style,name,&value);
1748 if(!subattribute.empty())
1757 Svg_parser::get_tokens_path(String path){ //mini path lexico-parser
1758 std::vector<String> tokens;
1763 while(i<path.size()){
1766 case 0: //initial state
1767 if(a=='m'){ e=1; i++;}
1768 else if(a=='c'){ e= 2; i++;}
1769 else if(a=='q'){ e= 3; i++;}
1770 else if(a=='t'){ e= 4; i++;}
1771 else if(a=='a'){ e= 5; i++;}
1772 else if(a=='l'){ e= 6; i++;}
1773 else if(a=='v'){ e= 7; i++;}
1774 else if(a=='h'){ e= 8; i++;}
1775 else if(a=='M'){ e= 9; i++;}
1776 else if(a=='C'){ e=10; i++;}
1777 else if(a=='Q'){ e=11; i++;}
1778 else if(a=='T'){ e=12; i++;}
1779 else if(a=='A'){ e=13; i++;}
1780 else if(a=='L'){ e=14; i++;}
1781 else if(a=='V'){ e=15; i++;}
1782 else if(a=='H'){ e=16; i++;}
1783 else if(a=='z' || a=='Z'){ e=17; i++;}
1784 else if(a=='-' || a=='.' || a=='e' || a=='E' || isdigit (a)){ e=18;}
1785 else if(a==','){ e=19; i++;}
1786 else if(a==' '){i++;}
1789 case 1 : tokens.push_back("m"); e=0; break;//move
1790 case 2 : tokens.push_back("c"); e=0; break;//curve
1791 case 3 : tokens.push_back("q"); e=0; break;//quadratic
1792 case 4 : tokens.push_back("t"); e=0; break;//smooth quadratic
1793 case 5 : tokens.push_back("a"); e=0; break;//elliptic arc
1794 case 6 : tokens.push_back("l"); e=0; break;//line to
1795 case 7 : tokens.push_back("v"); e=0; break;//vertical
1796 case 8 : tokens.push_back("h"); e=0; break;//horizontal
1798 case 9 : tokens.push_back("M"); e=0; break;
1799 case 10: tokens.push_back("C"); e=0; break;
1800 case 11: tokens.push_back("Q"); e=0; break;
1801 case 12: tokens.push_back("T"); e=0; break;
1802 case 13: tokens.push_back("A"); e=0; break;
1803 case 14: tokens.push_back("L"); e=0; break;
1804 case 15: tokens.push_back("V"); e=0; break;
1805 case 16: tokens.push_back("H"); e=0; break;
1807 case 17: tokens.push_back("z"); e=0; break;//loop
1808 case 18: if(a=='-' || a=='.' || a=='e' || a=='E' || isdigit (a)){
1809 buffer.append(path.substr(i,1));i++;
1814 case 19: tokens.push_back(","); e=0; break;
1815 case 20: tokens.push_back(buffer);
1821 switch(e){//last element
1822 case 1 : tokens.push_back("m"); break;
1823 case 2 : tokens.push_back("c"); break;
1824 case 3 : tokens.push_back("q"); break;
1825 case 4 : tokens.push_back("t"); break;
1826 case 5 : tokens.push_back("a"); break;
1827 case 6 : tokens.push_back("l"); break;
1828 case 7 : tokens.push_back("v"); break;
1829 case 8 : tokens.push_back("h"); break;
1830 case 9 : tokens.push_back("M"); break;
1831 case 10: tokens.push_back("C"); break;
1832 case 11: tokens.push_back("Q"); break;
1833 case 12: tokens.push_back("T"); break;
1834 case 13: tokens.push_back("A"); break;
1835 case 14: tokens.push_back("L"); break;
1836 case 15: tokens.push_back("V"); break;
1837 case 16: tokens.push_back("H"); break;
1838 case 17: tokens.push_back("z"); break;
1839 case 18: tokens.push_back(buffer); break;
1840 case 19: tokens.push_back(","); break;
1841 case 20: tokens.push_back(buffer); break;
1848 Svg_parser::randomLetter(){
1850 if(a) return (49 + rand()%9);
1851 else return (65 + rand()%24);
1855 Svg_parser::getRed(String hex){
1857 //allow for 3-digit hex codes (#rgb = #rrggbb)
1858 if (hex.length()<7) return (16+1) * hextodec(hex.substr(1,1));
1859 return hextodec(hex.substr(1,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(0);
1864 return atoi(aux.data());
1866 return getColor(hex,1);
1869 Svg_parser::getGreen(String hex){
1871 if (hex.length()<7) return (16+1) * hextodec(hex.substr(2,1));
1872 return hextodec(hex.substr(3,2));
1873 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1874 int start=hex.find_first_of("(")+1;
1875 int end =hex.find_last_of(")");
1876 String aux=tokenize(hex.substr(start,end-start),",").at(1);
1877 return atoi(aux.data());
1879 return getColor(hex,2);
1882 Svg_parser::getBlue(String hex){
1884 if (hex.length()<7) return (16+1) * hextodec(hex.substr(3,1));
1885 return hextodec(hex.substr(5,2));
1886 }else if(hex.compare(0,3,"rgb")==0 || hex.compare(0,3,"RGB")==0){
1887 int start=hex.find_first_of("(")+1;
1888 int end =hex.find_last_of(")");
1889 String aux=tokenize(hex.substr(start,end-start),",").at(2);
1890 return atoi(aux.data());
1892 return getColor(hex,3);
1895 Svg_parser::hextodec(String hex){
1904 else if(hex.at(i)=='1')
1906 else if(hex.at(i)=='2')
1908 else if(hex.at(i)=='3')
1910 else if(hex.at(i)=='4')
1912 else if(hex.at(i)=='5')
1914 else if(hex.at(i)=='6')
1916 else if(hex.at(i)=='7')
1918 else if(hex.at(i)=='8')
1920 else if(hex.at(i)=='9')
1922 else if(hex.at(i)=='a')
1924 else if(hex.at(i)=='b')
1926 else if(hex.at(i)=='c')
1928 else if(hex.at(i)=='d')
1930 else if(hex.at(i)=='e')
1932 else if(hex.at(i)=='f')
1940 result+=pow(16,i)*ihex[top-i-1];
1948 Svg_parser::getDimension(const String ac){
1952 int length=ac.size();
1954 if(isdigit(ac.at(length-1))){
1956 }else if(ac.at(length-1)=='%'){
1959 String mtc=ac.substr(length-2,length);
1960 String nmc=ac.substr(0,length-2);
1961 if(mtc.compare("px")==0){
1962 af=atof(nmc.data());
1963 }else if(mtc.compare("pt")==0){
1964 af=atof(nmc.data())*1.25;
1965 }else if(mtc.compare("em")==0){
1966 af=atof(nmc.data())*16;
1967 }else if(mtc.compare("mm")==0){
1968 af=atof(nmc.data())*3.54;
1969 }else if(mtc.compare("pc")==0){
1970 af=atof(nmc.data())*15;
1971 }else if(mtc.compare("cm")==0){
1972 af=atof(nmc.data())*35.43;
1973 }else if(mtc.compare("in")==0){
1974 af=atof(nmc.data())*90;
1983 Svg_parser::getRadian(float sexa){
1984 return (sexa*2*PI)/360;
1987 Svg_parser::removeS(String *input){
1988 for(unsigned int i=0;i<input->size();i++){
1989 if(input->at(i)==' '){
1995 Svg_parser::removeIntoS(String *input){
1997 for(unsigned int i=0;i<input->size();i++){
1998 if(input->at(i)=='('){
2000 }else if(input->at(i)==')'){
2002 }else if(into && input->at(i)==' '){
2008 Svg_parser::tokenize(const String& str,const String& delimiters){
2009 std::vector<String> tokens;
2010 String::size_type lastPos = str.find_first_not_of(delimiters, 0);
2011 String::size_type pos = str.find_first_of(delimiters, lastPos);
2012 while (String::npos != pos || String::npos != lastPos){
2013 tokens.push_back(str.substr(lastPos, pos - lastPos));
2014 lastPos = str.find_first_not_of(delimiters, pos);
2015 pos = str.find_first_of(delimiters, lastPos);
2020 Svg_parser::new_guid(){
2022 return GUID::hasher(uid).get_string();
2026 #define COLOR_NAME(color, r, g, b) else if(name.compare(0,strlen(color),color)==0) \
2028 {case 1: return r; case 2: return g; case 3: return b;} }
2031 Svg_parser::getColor(String name, int position){
2032 if (position<1 || position>3) return 0;
2033 COLOR_NAME("aliceblue",240, 248, 255)
2034 COLOR_NAME("antiquewhite",250, 235, 215)
2035 COLOR_NAME("aqua", 0, 255, 255)
2036 COLOR_NAME("aquamarine",127, 255, 212)
2037 COLOR_NAME("azure",240, 255, 255)
2038 COLOR_NAME("beige",245, 245, 220)
2039 COLOR_NAME("bisque",255, 228, 196)
2040 COLOR_NAME("black", 0, 0, 0)
2041 COLOR_NAME("blanchedalmond",255, 235, 205)
2042 COLOR_NAME("blue", 0, 0, 255)
2043 COLOR_NAME("blueviolet",138, 43, 226)
2044 COLOR_NAME("brown",165, 42, 42)
2045 COLOR_NAME("burlywood",222, 184, 135)
2046 COLOR_NAME("cadetblue", 95, 158, 160)
2047 COLOR_NAME("chartreuse",127, 255, 0)
2048 COLOR_NAME("chocolate",210, 105, 30)
2049 COLOR_NAME("coral",255, 127, 80)
2050 COLOR_NAME("cornflowerblue",100, 149, 237)
2051 COLOR_NAME("cornsilk",255, 248, 220)
2052 COLOR_NAME("crimson",220, 20, 60)
2053 COLOR_NAME("cyan", 0, 255, 255)
2054 COLOR_NAME("darkblue", 0, 0, 139)
2055 COLOR_NAME("darkcyan", 0, 139, 139)
2056 COLOR_NAME("darkgoldenrod",184, 134, 11)
2057 COLOR_NAME("darkgray",169, 169, 169)
2058 COLOR_NAME("darkgreen", 0, 100, 0)
2059 COLOR_NAME("darkgrey",169, 169, 169)
2060 COLOR_NAME("darkkhaki",189, 183, 107)
2061 COLOR_NAME("darkmagenta",139, 0, 139)
2062 COLOR_NAME("darkolivegreen", 85, 107, 47)
2063 COLOR_NAME("darkorange",255, 140, 0)
2064 COLOR_NAME("darkorchid",153, 50, 204)
2065 COLOR_NAME("darkred",139, 0, 0)
2066 COLOR_NAME("darksalmon",233, 150, 122)
2067 COLOR_NAME("darkseagreen",143, 188, 143)
2068 COLOR_NAME("darkslateblue", 72, 61, 139)
2069 COLOR_NAME("darkslategray", 47, 79, 79)
2070 COLOR_NAME("darkslategrey", 47, 79, 79)
2071 COLOR_NAME("darkturquoise", 0, 206, 209)
2072 COLOR_NAME("darkviolet",148, 0, 211)
2073 COLOR_NAME("deeppink",255, 20, 147)
2074 COLOR_NAME("deepskyblue", 0, 191, 255)
2075 COLOR_NAME("dimgray",105, 105, 105)
2076 COLOR_NAME("dimgrey",105, 105, 105)
2077 COLOR_NAME("dodgerblue", 30, 144, 255)
2078 COLOR_NAME("firebrick",178, 34, 34)
2079 COLOR_NAME("floralwhite",255, 250, 240)
2080 COLOR_NAME("forestgreen", 34, 139, 34)
2081 COLOR_NAME("fuchsia",255, 0, 255)
2082 COLOR_NAME("gainsboro",220, 220, 220)
2083 COLOR_NAME("ghostwhite",248, 248, 255)
2084 COLOR_NAME("gold",255, 215, 0)
2085 COLOR_NAME("goldenrod",218, 165, 32)
2086 COLOR_NAME("gray",128, 128, 128)
2087 COLOR_NAME("grey",128, 128, 128)
2088 COLOR_NAME("green", 0, 128, 0)
2089 COLOR_NAME("greenyellow",173, 255, 47)
2090 COLOR_NAME("honeydew",240, 255, 240)
2091 COLOR_NAME("hotpink",255, 105, 180)
2092 COLOR_NAME("indianred",205, 92, 92)
2093 COLOR_NAME("indigo", 75, 0, 130)
2094 COLOR_NAME("ivory",255, 255, 240)
2095 COLOR_NAME("khaki",240, 230, 140)
2096 COLOR_NAME("lavender",230, 230, 250)
2097 COLOR_NAME("lavenderblush",255, 240, 245)
2098 COLOR_NAME("lawngreen",124, 252, 0)
2099 COLOR_NAME("lemonchiffon",255, 250, 205)
2100 COLOR_NAME("lightblue",173, 216, 230)
2101 COLOR_NAME("lightcoral",240, 128, 128)
2102 COLOR_NAME("lightcyan",224, 255, 255)
2103 COLOR_NAME("lightgoldenrodyellow",250, 250, 210)
2104 COLOR_NAME("lightgray",211, 211, 211)
2105 COLOR_NAME("lightgreen",144, 238, 144)
2106 COLOR_NAME("lightgrey",211, 211, 211)
2107 COLOR_NAME("lightpink",255, 182, 193)
2108 COLOR_NAME("lightsalmon",255, 160, 122)
2109 COLOR_NAME("lightseagreen", 32, 178, 170)
2110 COLOR_NAME("lightskyblue",135, 206, 250)
2111 COLOR_NAME("lightslategray",119, 136, 153)
2112 COLOR_NAME("lightslategrey",119, 136, 153)
2113 COLOR_NAME("lightsteelblue",176, 196, 222)
2114 COLOR_NAME("lightyellow",255, 255, 224)
2115 COLOR_NAME("lime", 0, 255, 0)
2116 COLOR_NAME("limegreen", 50, 205, 50)
2117 COLOR_NAME("linen",250, 240, 230)
2118 COLOR_NAME("magenta",255, 0, 255)
2119 COLOR_NAME("maroon",128, 0, 0)
2120 COLOR_NAME("mediumaquamarine",102, 205, 170)
2121 COLOR_NAME("mediumblue", 0, 0, 205)
2122 COLOR_NAME("mediumorchid",186, 85, 211)
2123 COLOR_NAME("mediumpurple",147, 112, 219)
2124 COLOR_NAME("mediumseagreen", 60, 179, 113)
2125 COLOR_NAME("mediumslateblue",123, 104, 238)
2126 COLOR_NAME("mediumspringgreen", 0, 250, 154)
2127 COLOR_NAME("mediumturquoise", 72, 209, 204)
2128 COLOR_NAME("mediumvioletred",199, 21, 133)
2129 COLOR_NAME("midnightblue", 25, 25, 112)
2130 COLOR_NAME("mintcream",245, 255, 250)
2131 COLOR_NAME("mistyrose",255, 228, 225)
2132 COLOR_NAME("moccasin",255, 228, 181)
2133 COLOR_NAME("navajowhite",255, 222, 173)
2134 COLOR_NAME("navy", 0, 0, 128)
2135 COLOR_NAME("oldlace",253, 245, 230)
2136 COLOR_NAME("olive",128, 128, 0)
2137 COLOR_NAME("olivedrab",107, 142, 35)
2138 COLOR_NAME("orange",255, 165, 0)
2139 COLOR_NAME("orangered",255, 69, 0)
2140 COLOR_NAME("orchid",218, 112, 214)
2141 COLOR_NAME("palegoldenrod",238, 232, 170)
2142 COLOR_NAME("palegreen",152, 251, 152)
2143 COLOR_NAME("paleturquoise",175, 238, 238)
2144 COLOR_NAME("palevioletred",219, 112, 147)
2145 COLOR_NAME("papayawhip",255, 239, 213)
2146 COLOR_NAME("peachpuff",255, 218, 185)
2147 COLOR_NAME("peru",205, 133, 63)
2148 COLOR_NAME("pink",255, 192, 203)
2149 COLOR_NAME("plum",221, 160, 221)
2150 COLOR_NAME("powderblue",176, 224, 230)
2151 COLOR_NAME("purple",128, 0, 128)
2152 COLOR_NAME("red",255, 0, 0)
2153 COLOR_NAME("rosybrown",188, 143, 143)
2154 COLOR_NAME("royalblue", 65, 105, 225)
2155 COLOR_NAME("saddlebrown",139, 69, 19)
2156 COLOR_NAME("salmon",250, 128, 114)
2157 COLOR_NAME("sandybrown",244, 164, 96)
2158 COLOR_NAME("seagreen", 46, 139, 87)
2159 COLOR_NAME("seashell",255, 245, 238)
2160 COLOR_NAME("sienna",160, 82, 45)
2161 COLOR_NAME("silver",192, 192, 192)
2162 COLOR_NAME("skyblue",135, 206, 235)
2163 COLOR_NAME("slateblue",106, 90, 205)
2164 COLOR_NAME("slategray",112, 128, 144)
2165 COLOR_NAME("slategrey",112, 128, 144)
2166 COLOR_NAME("snow",255, 250, 250)
2167 COLOR_NAME("springgreen", 0, 255, 127)
2168 COLOR_NAME("steelblue", 70, 130, 180)
2169 COLOR_NAME("tan",210, 180, 140)
2170 COLOR_NAME("teal", 0, 128, 128)
2171 COLOR_NAME("thistle",216, 191, 216)
2172 COLOR_NAME("tomato",255, 99, 71)
2173 COLOR_NAME("turquoise", 64, 224, 208)
2174 COLOR_NAME("violet",238, 130, 238)
2175 COLOR_NAME("wheat",245, 222, 179)
2176 COLOR_NAME("white",255, 255, 255)
2177 COLOR_NAME("whitesmoke",245, 245, 245)
2178 COLOR_NAME("yellow",255, 255, 0)
2179 COLOR_NAME("yellowgreen",154, 205, 50)