Gradient support
[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 **
14 **      This package is free software; you can redistribute it and/or
15 **      modify it under the terms of the GNU General Public License as
16 **      published by the Free Software Foundation; either version 2 of
17 **      the License, or (at your option) any later version.
18 **
19 **      This package is distributed in the hope that it will be useful,
20 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 **      General Public License for more details.
23 **      \endlegal
24 */
25 /* ========================================================================= */
26
27 /* === S T A R T =========================================================== */
28
29 #ifndef __SVG_PARSER_H
30 #define __SVG_PARSER_H
31
32 /* === H E A D E R S ======================================================= */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <math.h>
37 #include <string.h>
38
39 #include <libxml++/libxml++.h>
40 #include <ETL/angle>
41 #include <ETL/stringf>
42 #include <synfig/canvas.h>
43 #include <synfig/loadcanvas.h>
44 #include <synfig/color.h>
45 #include <synfig/gamma.h>
46 #include <synfig/guid.h>
47
48 /* === M A C R O S ========================================================= */
49
50 /* === T Y P E D E F S ===================================================== */
51
52 /* === C L A S S E S & S T R U C T S ======================================= */
53
54 namespace synfig{
55
56 typedef struct matrix_t{
57         float a,c,e;
58         float b,d,f;
59 }Matrix;
60
61 typedef struct stop_t{
62          float r,g,b;
63          float a;
64          float pos;
65 }ColorStop;
66 typedef struct linear_g{
67         char name[40];
68         float x1,x2,y1,y2;
69         std::list<ColorStop*> *stops; //paradas de color
70         Matrix *transform;
71 }LinearGradient;
72 typedef struct radial_g{
73         char name[40];
74         float cx,cy;//center point
75         //float fx,fy;
76         //focus, i dont see it in synfig
77         //if this value is omitted then will be = cx,cy
78         float r; //radio radius
79         std::list<ColorStop*> *stops; //paradas de color
80         Matrix *transform;
81 }RadialGradient;
82
83 typedef struct url_t{
84         int type;
85         void* data;
86 }URL;
87
88 typedef struct vertice_t{
89         float x,y;
90         float radio1,angle1;
91         float radio2,angle2;
92         bool split;
93 }Vertice;
94 class Svg_parser
95 {
96                 //this is inkscape oriented in some cases
97 public:
98
99 private:
100                 Gamma gamma;
101                 String filepath;
102                 String id_name;
103                 xmlpp::DomParser parser;
104                 xmlpp::Document document;
105                 xmlpp::Element* nodeRoot;//output
106                 Glib::ustring width;
107                 Glib::ustring height;
108                 Glib::ustring docname;
109                 int uid;
110                 int kux,set_canvas;
111                 float ox,oy;
112                 bool loop;//aux :D
113                 //urls
114                 std::list<LinearGradient*> lg;
115                 std::list<RadialGradient*> rg;
116
117 public:
118                 Svg_parser();
119                 Canvas::Handle load_svg_canvas(std::string _filepath,String &errors, String &warnings);
120                 //String get_id();
121                 //void set_id(String source);
122
123 private:        //parser bucle
124                 void parser_node(const xmlpp::Node* node);
125                 //parser headers
126                 void parser_svg(const xmlpp::Node* node);
127                 void parser_canvas(const xmlpp::Node* node);
128                 //layers
129                 void parser_graphics(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx_parent);
130                 void rect_simple(const xmlpp::Element* nodeElement,xmlpp::Element* root,String fill, String fill_opacity, String opacity);
131                 void parser_layer(const xmlpp::Node* node,xmlpp::Element* root,String parent_style,Matrix* mtx);
132                 std::list<std::list<Vertice*> > parser_polygon_path(Glib::ustring polygon_points, Matrix* mtx);
133                 void parser_effects(const xmlpp::Element* nodeElement,xmlpp::Element* root,String parent_style,Matrix* mtx);
134                 void parser_transform(xmlpp::Element* root,Matrix* mtx);
135                 //defs
136                 void parser_defs(const xmlpp::Node* node);
137                 void parser_linearGradient(const xmlpp::Node* node);
138                 void parser_radialGradient(const xmlpp::Node* node);
139                 ColorStop* newColorStop(String color,float opacity,float pos);
140                 LinearGradient* newLinearGradient(String name,float x1,float y1, float x2,float y2,std::list<ColorStop*> *stops, Matrix* transform);
141                 RadialGradient* newRadialGradient(String name,float cx,float cy,float r,std::list<ColorStop*> *stops, Matrix* transform);
142                 //builds urls
143                 void AdjustPointUrl();
144                 std::list<ColorStop*>* find_colorStop(String name);
145                 void build_url(xmlpp::Element* root, String name,Matrix *mtx);
146                 void build_linearGradient(xmlpp::Element* root,LinearGradient* data,Matrix* mtx);
147                 void build_radialGradient(xmlpp::Element* root,RadialGradient* data,Matrix* mtx);
148                 void build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops);
149                 void build_stop_color(xmlpp::Element* root, std::list<ColorStop*> *stops,String name);
150                 Color adjustGamma(float r,float g,float b,float a);
151                 //builds
152                 void build_gamma(xmlpp::Element* root,float gamma);
153                 Matrix* build_transform(const String transform);
154                 void build_rotate(xmlpp::Element* root,float dx,float dy,float angle);
155                 void build_translate(xmlpp::Element* root,float dx,float dy);
156                 void build_points(xmlpp::Element* root,std::list<Vertice*> p);
157                 void build_vertice(xmlpp::Element* root , Vertice *p);
158                 void build_bline(xmlpp::Element* root,std::list<Vertice*> p,bool loop,String blinegui);
159                 void build_param (xmlpp::Element* root,String name,String type,String value);
160                 void build_param (xmlpp::Element* root,String name,String type,float value);
161                 void build_param (xmlpp::Element* root,String name,String type,int value);
162                 void build_integer (xmlpp::Element* root,String name, int value);
163                 void build_real (xmlpp::Element* root,String name,float value);
164                 void build_vector (xmlpp::Element* root,String name,float x,float y);
165                 void build_vector (xmlpp::Element* root,String name,float x,float y,String guid);
166                 void build_color(xmlpp::Element* root,float r,float g,float b,float a);
167                 xmlpp::Element* nodeStartBasicLayer(xmlpp::Element* root);
168
169                 //points,etc
170                 void coor2vect(float *x,float *y);
171                 void setTg2(Vertice* p,float p1x,float p1y,float p2x,float p2y);
172                 void setTg1(Vertice *p,float p1x,float p1y,float p2x,float p2y);
173                 void setSplit(Vertice* p,bool val);
174                 int isFirst(Vertice* nodo,float a, float b);
175                 Vertice* newVertice(float x,float y);
176
177                 //matrix operations
178                 Matrix* newMatrix(float a,float b,float c,float d,float e,float f);
179                 Matrix* newMatrix(const String mvector);
180                 Matrix* newMatrix(Matrix *a);
181                 void transformPoint2D(Matrix *mtx,float *a,float *b);
182                 bool matrixIsNull(Matrix* mtx);
183                 void composeMatrix(Matrix **mtx,Matrix *mtx1,Matrix *mtx2);
184                 void multiplyMatrix(Matrix **mtx1,Matrix *mtx2);
185                 float getRadian(float sexa);
186                 //attributes
187                 int extractSubAttribute(const String attribute, String name,String* value);
188                 String loadAttribute(String name,const String path_style,const String master_style,const String subattribute,const String defaultVal);
189                 String loadAttribute(String name,const String path_style,const String master_style,const String defaultVal);
190                 std::vector<String> get_tokens_path(String path);
191                 std::list<std::list<Vertice*> > parser_path_d(String path_d,Matrix* mtx);
192                 int randomLetter();
193                 int getRed(String hex);
194                 int getGreen(String hex);
195                 int getBlue(String hex);
196                 int hextodec(String hex);
197                 float getDimension(const String ac);
198                 //funciones string
199                 void removeS(String *input);
200                 void removeIntoS(String *input);
201                 std::vector<String> tokenize(const String& str,const String& delimiters);
202                 void get_canvas_name(String _filepath);
203                 String new_guid();
204 };
205 // END of Svg_parser class
206
207 /* === E X T E R N S ======================================================= */
208
209 extern Canvas::Handle open_svg(std::string _filepath,String &errors, String &warnings);
210
211 }; // END of namespace synfig
212
213 /* === E N D =============================================================== */
214
215 #endif