1 /* svg.h: Public interface for libsvg
2 
3    Copyright � 2002 USC/Information Sciences Institute
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with this program; if not, write to the
17    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.
19 
20    Author: Carl Worth <cworth@isi.edu>
21 */
22 
23 #ifndef SVG_H
24 #define SVG_H
25 
26 #include <stdio.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef struct svg svg_t;
33 typedef struct svg_group svg_group_t;
34 typedef struct svg_element svg_element_t;
35 
36 /* XXX: I'm still not convinced I want to export this structure */
37 typedef struct svg_color {
38     int is_current_color;
39     unsigned int rgb;
40 } svg_color_t;
41 
42 typedef enum svg_length_unit {
43     SVG_LENGTH_UNIT_CM,
44     SVG_LENGTH_UNIT_EM,
45     SVG_LENGTH_UNIT_EX,
46     SVG_LENGTH_UNIT_IN,
47     SVG_LENGTH_UNIT_MM,
48     SVG_LENGTH_UNIT_PC,
49     SVG_LENGTH_UNIT_PCT,
50     SVG_LENGTH_UNIT_PT,
51     SVG_LENGTH_UNIT_PX
52 } svg_length_unit_t;
53 
54 typedef enum svg_length_orientation {
55     SVG_LENGTH_ORIENTATION_HORIZONTAL,
56     SVG_LENGTH_ORIENTATION_VERTICAL,
57     SVG_LENGTH_ORIENTATION_OTHER
58 } svg_length_orientation_t;
59 
60 typedef struct svg_length {
61     double value;
62     svg_length_unit_t unit : 4;
63     svg_length_orientation_t orientation : 2;
64 } svg_length_t;
65 
66 typedef struct svg_rect {
67     double x;
68     double y;
69     double width;
70     double height;
71 } svg_rect_t;
72 
73 typedef enum svg_preserve_aspect_ratio {
74     SVG_PRESERVE_ASPECT_RATIO_UNKNOWN,
75     SVG_PRESERVE_ASPECT_RATIO_NONE,
76     SVG_PRESERVE_ASPECT_RATIO_XMINYMIN,
77     SVG_PRESERVE_ASPECT_RATIO_XMIDYMIN,
78     SVG_PRESERVE_ASPECT_RATIO_XMAXYMIN,
79     SVG_PRESERVE_ASPECT_RATIO_XMINYMID,
80     SVG_PRESERVE_ASPECT_RATIO_XMIDYMID,
81     SVG_PRESERVE_ASPECT_RATIO_XMAXYMID,
82     SVG_PRESERVE_ASPECT_RATIO_XMINYMAX,
83     SVG_PRESERVE_ASPECT_RATIO_XMIDYMAX,
84     SVG_PRESERVE_ASPECT_RATIO_XMAXYMAX
85 } svg_preserve_aspect_ratio_t;
86 
87 typedef enum svg_meet_or_slice {
88    SVG_MEET_OR_SLICE_UNKNOWN,
89    SVG_MEET_OR_SLICE_MEET,
90    SVG_MEET_OR_SLICE_SLICE
91 } svg_meet_or_slice_t;
92 
93 typedef struct svg_view_box {
94     svg_rect_t box;
95     svg_preserve_aspect_ratio_t aspect_ratio : 4;
96     svg_meet_or_slice_t meet_or_slice : 2;
97 } svg_view_box_t;
98 
99 /* XXX: I want to think very carefully about the names of these error
100    messages. First, they should follow the SVG spec. closely. Second,
101    which errors do we really want? */
102 typedef enum svg_status {
103     SVG_STATUS_SUCCESS = 0,
104     SVG_STATUS_NO_MEMORY,
105     SVG_STATUS_IO_ERROR,
106     SVG_STATUS_FILE_NOT_FOUND,
107     SVG_STATUS_INVALID_VALUE,
108     SVG_STATUS_INVALID_CALL,
109     SVG_STATUS_PARSE_ERROR
110 } svg_status_t;
111 
112 typedef enum svg_fill_rule {
113     SVG_FILL_RULE_NONZERO,
114     SVG_FILL_RULE_EVEN_ODD
115 } svg_fill_rule_t;
116 
117 typedef enum svg_font_style {
118     SVG_FONT_STYLE_NORMAL,
119     SVG_FONT_STYLE_ITALIC,
120     SVG_FONT_STYLE_OBLIQUE
121 } svg_font_style_t;
122 
123 typedef enum svg_stroke_line_cap {
124     SVG_STROKE_LINE_CAP_BUTT,
125     SVG_STROKE_LINE_CAP_ROUND,
126     SVG_STROKE_LINE_CAP_SQUARE
127 } svg_stroke_line_cap_t;
128 
129 typedef enum svg_stroke_line_join {
130     SVG_STROKE_LINE_JOIN_BEVEL,
131     SVG_STROKE_LINE_JOIN_MITER,
132     SVG_STROKE_LINE_JOIN_ROUND
133 } svg_stroke_line_join_t;
134 
135 typedef enum svg_text_anchor {
136     SVG_TEXT_ANCHOR_START,
137     SVG_TEXT_ANCHOR_MIDDLE,
138     SVG_TEXT_ANCHOR_END
139 } svg_text_anchor_t;
140 
141 typedef enum svg_gradient_type_t {
142     SVG_GRADIENT_LINEAR,
143     SVG_GRADIENT_RADIAL
144 } svg_gradient_type_t;
145 
146 typedef enum svg_gradient_units {
147     SVG_GRADIENT_UNITS_USER,
148     SVG_GRADIENT_UNITS_BBOX
149 } svg_gradient_units_t;
150 
151 typedef enum svg_gradient_spread {
152     SVG_GRADIENT_SPREAD_PAD,
153     SVG_GRADIENT_SPREAD_REPEAT,
154     SVG_GRADIENT_SPREAD_REFLECT
155 } svg_gradient_spread_t;
156 
157 typedef struct svg_gradient_stop {
158     svg_color_t color;
159     double offset;
160     double opacity;
161 } svg_gradient_stop_t;
162 
163 typedef struct svg_gradient {
164     svg_gradient_type_t type;
165     union {
166 	struct {
167 	    svg_length_t x1;
168 	    svg_length_t y1;
169 	    svg_length_t x2;
170 	    svg_length_t y2;
171 	} linear;
172 	struct {
173 	    svg_length_t cx;
174 	    svg_length_t cy;
175 	    svg_length_t r;
176 	    svg_length_t fx;
177 	    svg_length_t fy;
178 	} radial;
179     } u;
180     svg_gradient_units_t units;
181     svg_gradient_spread_t spread;
182     /* XXX: Why not just a regular svg_transform here? */
183     double transform[6];
184 
185     svg_gradient_stop_t *stops;
186     int num_stops;
187     int stops_size;
188 } svg_gradient_t;
189 
190 typedef enum svg_pattern_units {
191     SVG_PATTERN_UNITS_USER,
192     SVG_PATTERN_UNITS_BBOX
193 } svg_pattern_units_t;
194 
195 /* XXX: I'm still not convinced I want to export this structure */
196 typedef struct svg_pattern {
197     svg_element_t *group_element;
198     svg_pattern_units_t units;
199     svg_pattern_units_t content_units;
200     svg_length_t x, y, width, height;
201     double transform[6];
202 } svg_pattern_t;
203 
204 /* XXX: I'm still not convinced I want to export this structure */
205 /* XXX: This needs to be fleshed out considerably more than this */
206 typedef enum svg_paint_type {
207     SVG_PAINT_TYPE_NONE,
208     SVG_PAINT_TYPE_COLOR,
209     SVG_PAINT_TYPE_GRADIENT,
210     SVG_PAINT_TYPE_PATTERN
211 } svg_paint_type_t;
212 
213 /* XXX: I'm still not convinced I want to export this structure */
214 typedef struct svg_paint {
215     svg_paint_type_t type;
216     union {
217 	svg_color_t	color;
218 	svg_gradient_t	*gradient;
219 	svg_element_t	*pattern_element;
220     } p;
221 } svg_paint_t;
222 
223 /* XXX: Here's another piece of the API that needs deep consideration. */
224 typedef struct svg_render_engine {
225     /* hierarchy */
226     svg_status_t (* begin_group) (void *closure, double opacity);
227     svg_status_t (* begin_element) (void *closure);
228     svg_status_t (* end_element) (void *closure);
229     svg_status_t (* end_group) (void *closure, double opacity);
230     /* path creation */
231     svg_status_t (* move_to) (void *closure, double x, double y);
232     svg_status_t (* line_to) (void *closure, double x, double y);
233     svg_status_t (* curve_to) (void *closure,
234 			       double x1, double y1,
235 			       double x2, double y2,
236 			       double x3, double y3);
237     svg_status_t (* quadratic_curve_to) (void *closure,
238 					 double x1, double y1,
239 					 double x2, double y2);
240     svg_status_t (* arc_to) (void *closure,
241 		    	     double	rx,
242 			     double	ry,
243 			     double	x_axis_rotation,
244 			     int	large_arc_flag,
245 			     int	sweep_flag,
246 			     double	x,
247 			     double	y);
248     svg_status_t (* close_path) (void *closure);
249     /* style */
250     svg_status_t (* set_color) (void *closure, const svg_color_t *color);
251     svg_status_t (* set_fill_opacity) (void *closure, double fill_opacity);
252     svg_status_t (* set_fill_paint) (void *closure, const svg_paint_t *paint);
253     svg_status_t (* set_fill_rule) (void *closure, svg_fill_rule_t fill_rule);
254     svg_status_t (* set_font_family) (void *closure, const char *family);
255     svg_status_t (* set_font_size) (void *closure, double size);
256     svg_status_t (* set_font_style) (void *closure, svg_font_style_t font_style);
257     svg_status_t (* set_font_weight) (void *closure, unsigned int font_weight);
258     svg_status_t (* set_opacity) (void *closure, double opacity);
259     svg_status_t (* set_stroke_dash_array) (void *closure, double *dash_array, int num_dashes);
260     svg_status_t (* set_stroke_dash_offset) (void *closure, svg_length_t *offset);
261     svg_status_t (* set_stroke_line_cap) (void *closure, svg_stroke_line_cap_t line_cap);
262     svg_status_t (* set_stroke_line_join) (void *closure, svg_stroke_line_join_t line_join);
263     svg_status_t (* set_stroke_miter_limit) (void *closure, double limit);
264     svg_status_t (* set_stroke_opacity) (void *closure, double stroke_opacity);
265     svg_status_t (* set_stroke_paint) (void *closure, const svg_paint_t *paint);
266     svg_status_t (* set_stroke_width) (void *closure, svg_length_t *width);
267     svg_status_t (* set_text_anchor) (void *closure, svg_text_anchor_t text_anchor);
268     /* transform */
269     svg_status_t (* transform) (void *closure,
270 				double a, double b,
271 				double c, double d,
272 				double e, double f);
273     svg_status_t (* apply_view_box) (void *closure,
274 				     svg_view_box_t view_box,
275 				     svg_length_t *width,
276 				     svg_length_t *height);
277     svg_status_t (* set_viewport_dimension) (void *closure,
278 				    	     svg_length_t *width,
279 				    	     svg_length_t *height);
280     /* drawing */
281     svg_status_t (* render_line) (void *closure,
282 				  svg_length_t *x1,
283 				  svg_length_t *y1,
284 				  svg_length_t *x2,
285 				  svg_length_t *y2);
286     svg_status_t (* render_path) (void *closure);
287     svg_status_t (* render_ellipse) (void *closure,
288 				     svg_length_t *cx,
289 				     svg_length_t *cy,
290 				     svg_length_t *rx,
291 				     svg_length_t *ry);
292     svg_status_t (* render_rect) (void *closure,
293 				     svg_length_t *x,
294 				     svg_length_t *y,
295 				     svg_length_t *width,
296 				     svg_length_t *height,
297 				     svg_length_t *rx,
298 				     svg_length_t *ry);
299     svg_status_t (* render_text) (void *closure,
300 				  svg_length_t *x,
301 				  svg_length_t *y,
302 				  const char   *utf8);
303     svg_status_t (* render_image) (void		 *closure,
304 				   unsigned char *data,
305 				   unsigned int	 data_width,
306 				   unsigned int	 data_height,
307 				   svg_length_t	 *x,
308 				   svg_length_t	 *y,
309 				   svg_length_t	 *width,
310 				   svg_length_t	 *height);
311 } svg_render_engine_t;
312 
313 svg_status_t
314 svg_create (svg_t **svg);
315 
316 svg_status_t
317 svg_destroy (svg_t *svg);
318 
319 svg_status_t
320 svg_parse (svg_t *svg, const char *filename);
321 
322 svg_status_t
323 svg_parse_file (svg_t *svg, FILE *file);
324 
325 svg_status_t
326 svg_parse_buffer (svg_t *svg, const char *buf, size_t count);
327 
328 svg_status_t
329 svg_parse_chunk_begin (svg_t *svg);
330 svg_status_t
331 svg_parse_chunk       (svg_t *svg, const char *buf, size_t count);
332 svg_status_t
333 svg_parse_chunk_end   (svg_t *svg);
334 
335 svg_status_t
336 svg_render (svg_t		*svg,
337 	    svg_render_engine_t	*engine,
338 	    void		*closure);
339 
340 void
341 svg_get_size (svg_t *svg,
342 	      svg_length_t *width,
343 	      svg_length_t *height);
344 
345 /* svg_color */
346 
347 unsigned int
348 svg_color_get_red (const svg_color_t *color);
349 
350 unsigned int
351 svg_color_get_green (const svg_color_t *color);
352 
353 unsigned int
354 svg_color_get_blue (const svg_color_t *color);
355 
356 /* svg_element */
357 
358 svg_status_t
359 svg_element_render (svg_element_t		*element,
360 		    svg_render_engine_t	*engine,
361 		    void			*closure);
362 
363 svg_pattern_t *
364 svg_element_pattern (svg_element_t *element);
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif
371