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