synfig-core  1.0.3
svg_parser.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
26 /* ========================================================================= */
27 
28 /* === S T A R T =========================================================== */
29 
30 #ifndef __SVG_PARSER_H
31 #define __SVG_PARSER_H
32 
33 /* === H E A D E R S ======================================================= */
34 
35 #include <cstdio>
36 #include <cstdlib>
37 #include <cmath>
38 #include <cstring>
39 #include <glibmm/ustring.h>
40 
41 #include <libxml++/libxml++.h>
42 #include <ETL/angle>
43 #include <ETL/stringf>
44 #include <synfig/canvas.h>
45 #include <synfig/loadcanvas.h>
46 #include <synfig/color.h>
47 #include <synfig/gamma.h>
48 #include <synfig/guid.h>
49 
50 /* === M A C R O S ========================================================= */
51 
52 /* === T Y P E D E F S ===================================================== */
53 
54 /* === C L A S S E S & S T R U C T S ======================================= */
55 
56 namespace synfig{
57 
58 typedef struct matrix_t{
59  float a,c,e;
60  float b,d,f;
61 }SVGMatrix;
62 
63 typedef struct stop_t{
64  float r,g,b;
65  float a;
66  float pos;
67 }ColorStop;
68 typedef struct linear_g{
69  char name[80];
70  float x1,x2,y1,y2;
71  std::list<ColorStop*> *stops;
74 typedef struct radial_g{
75  char name[80];
76  float cx,cy;//center point
77  //float fx,fy; //not supported by Synfig
78  float r; //radius
79  std::list<ColorStop*> *stops;
82 
83 typedef struct url_t{
84  int type;
85  void* data;
86 }URL;
87 
88 typedef struct Vertex_t{
89  float x,y;
90  float radius1,angle1;
91  float radius2,angle2;
92  bool split;
93 }Vertex;
94 
95 typedef struct bline_t{
96  std::list<Vertex*> *points;
97  bool loop;
100 }BLine;
101 
103 {
104  //this is inkscape oriented in some cases
105 public:
106 
107 private:
108  Gamma gamma;
109  String filepath;
110  String id_name;
111  xmlpp::DomParser parser;
112  xmlpp::Document document;
113  xmlpp::Element* nodeRoot;//output
114  Glib::ustring width;
115  Glib::ustring height;
116  Glib::ustring docname;
117  int uid;
118  int kux,set_canvas;
119  float ox,oy;
120  //urls
121  std::list<LinearGradient*> lg;
122  std::list<RadialGradient*> rg;
123 
124 public:
125  Svg_parser();
126  Canvas::Handle load_svg_canvas(std::string _filepath,String &errors, String &warnings);
127  //String get_id();
128  //void set_id(String source);
129 
130 private:
131  ChangeLocale locale;
132 
133  /* === PARSERS ==================================== */
134  void parser_node(const xmlpp::Node* node);
135  //parser headers
136  void parser_svg(const xmlpp::Node* node);
137  void parser_canvas(const xmlpp::Node* node);
138  void parser_graphics(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,SVGMatrix* mtx_parent);
139 
140  /* === LAYER PARSERS ============================== */
141  void parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,SVGMatrix* mtx);
142  void parser_rect(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity);
143  /* === CONVERT TO PATH PARSERS ==================== */
144  std::list<BLine *> parser_path_polygon(Glib::ustring polygon_points, SVGMatrix* mtx);
145  std::list<BLine *> parser_path_d(String path_d,SVGMatrix* mtx);
146 
147  /* === EFFECTS PARSERS ============================ */
148  void parser_effects(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,SVGMatrix* mtx);
149 
150  /* === DEFS PARSERS =============================== */
151  void parser_defs(const xmlpp::Node* node);
152  void parser_linearGradient(const xmlpp::Node* node);
153  void parser_radialGradient(const xmlpp::Node* node);
154  ColorStop* newColorStop(String color,float opacity,float pos);
155  LinearGradient* newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, SVGMatrix* transform);
156  RadialGradient* newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, SVGMatrix* transform);
157  BLine* newBLine(std::list<Vertex*> *points,bool loop);
158 
159  /* === BUILDS ===================================== */
160  void build_transform(xmlpp::Element* root,SVGMatrix* mtx);
161  std::list<ColorStop*>* find_colorStop(String name);
162  void build_fill(xmlpp::Element* root, String name,SVGMatrix *mtx);
163  void build_linearGradient(xmlpp::Element* root,LinearGradient* data,SVGMatrix* mtx);
164  void build_radialGradient(xmlpp::Element* root,RadialGradient* data,SVGMatrix* mtx);
165  void build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops);
166  void build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops,String name);
167  Color adjustGamma(float r,float g,float b,float a);
168 
169  void build_gamma(xmlpp::Element* root,float gamma);
170  void build_rotate(xmlpp::Element* root,float dx,float dy,float angle);
171  void build_translate(xmlpp::Element* root,float dx,float dy);
172  void build_points(xmlpp::Element* root,std::list<Vertex*> p);
173  void build_vertex(xmlpp::Element* root , Vertex *p);
174  void build_bline(xmlpp::Element* root,std::list<Vertex*> p,bool loop,String blineguid);
175  void build_param (xmlpp::Element* root,String name,String type,String value);
176  void build_param (xmlpp::Element* root,String name,String type,float value);
177  void build_param (xmlpp::Element* root,String name,String type,int value);
178  void build_integer (xmlpp::Element* root,String name, int value);
179  void build_real (xmlpp::Element* root,String name,float value);
180  void build_vector (xmlpp::Element* root,String name,float x,float y);
181  void build_vector (xmlpp::Element* root,String name,float x,float y,String guid);
182  void build_color(xmlpp::Element* root,float r,float g,float b,float a);
183  xmlpp::Element* nodeStartBasicLayer(xmlpp::Element* root);
184  xmlpp::Element* nodeStartBasicLayer(xmlpp::Element* root, String name);
185 
186  /* === COORDINATES & TRANSFORMATIONS ============== */
187 
188  //points,etc
189  void coor2vect(float *x,float *y);
190  void setTg2(Vertex* p,float p1x,float p1y,float p2x,float p2y);
191  void setTg1(Vertex *p,float p1x,float p1y,float p2x,float p2y);
192  void setSplit(Vertex* p,bool val);
193  int isFirst(Vertex* nodo,float a, float b);
194  Vertex* newVertex(float x,float y);
195 
196  //matrix operations
197  SVGMatrix* parser_transform(const String transform);
198  SVGMatrix* newSVGMatrix(float a,float b,float c,float d,float e,float f);
199  SVGMatrix* newSVGMatrix(const String mvector);
200  SVGMatrix* newSVGMatrix(SVGMatrix *a);
201  void transformPoint2D(SVGMatrix *mtx,float *a,float *b);
202  bool matrixIsNull(SVGMatrix* mtx);
203  void composeSVGMatrix(SVGMatrix **mtx,SVGMatrix *mtx1,SVGMatrix *mtx2);
204  void multiplySVGMatrix(SVGMatrix **mtx1,SVGMatrix *mtx2);
205  float getRadian(float sexa);
206 
207  /* === EXTRA METHODS ============================== */
208 
209  //attributes
210  int extractSubAttribute(const String attribute, String name,String* value);
211  String loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal);
212  String loadAttribute(String name,const String path_style,const String master_style,const String defaultVal);
213  std::vector<String> get_tokens_path(String path);
214  int randomLetter();
215  int getRed(String hex);
216  int getGreen(String hex);
217  int getBlue(String hex);
218  int hextodec(String hex);
219  int getColor(String name, int position);
220  float getDimension(const String ac);
221  //string functions
222  void removeS(String *input);
223  void removeIntoS(String *input);
224  std::vector<String> tokenize(const String& str,const String& delimiters);
225  void get_canvas_name(String _filepath);
226  String new_guid();
227 };
228 // END of Svg_parser class
229 
230 /* === E X T E R N S ======================================================= */
231 
232 extern Canvas::Handle open_svg(std::string _filepath,String &errors, String &warnings);
233 
234 }; // END of namespace synfig
235 
236 /* === E N D =============================================================== */
237 
238 #endif