summaryrefslogtreecommitdiff
path: root/rsvg-private.h
blob: 2227c8810f85dbbdaeef516093bf5e62cc688987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* vim: set sw=4 sts=4: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
   rsvg-private.h: Internals of RSVG

   Copyright (C) 2000 Eazel, Inc.
   Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Raph Levien <raph@artofcode.com>
*/

#ifndef RSVG_PRIVATE_H
#define RSVG_PRIVATE_H

#include "rsvg.h"
#include "rsvg-bpath-util.h"

#include <libxml/SAX.h>
#include <libxml/xmlmemory.h>
#include <pango/pango.h>
#include <glib/gslist.h>
#include <glib-object.h>
#include <math.h>

#if defined(HAVE_FLOAT_H)
# include <float.h>
#endif

G_BEGIN_DECLS 

typedef struct RsvgSaxHandler RsvgSaxHandler;
typedef struct RsvgDrawingCtx RsvgDrawingCtx;
typedef struct RsvgRender RsvgRender;
typedef struct _RsvgPropertyBag RsvgPropertyBag;
typedef struct _RsvgState RsvgState;
typedef struct _RsvgDefs RsvgDefs;
typedef struct _RsvgNode RsvgNode;
typedef struct _RsvgFilter RsvgFilter;
typedef struct _RsvgNodeChars RsvgNodeChars;
typedef struct _RsvgIRect RsvgIRect;

/* prepare for gettext */
#ifndef _
#define _(X) X
#endif

#ifndef N_
#define N_(X) X
#endif

#ifndef M_PI
#  ifdef G_PI
#    define M_PI G_PI
#  else
#    define M_PI 3.14159265358979323846
#  endif                        /* G_PI */
#endif                          /*  M_PI  */

#ifndef DBL_EPSILON
/* 1e-7 is a conservative value.  it's less than 2^(1-24) which is
 * the epsilon value for a 32-bit float.  The regular value for this
 * with 64-bit doubles is 2^(1-53) or approximately 1e-16.
 */
# define DBL_EPSILON 1e-7
#endif

/* RSVG_ONE_MINUS_EPSILON:
 *
 * DBL_EPSILON is the difference between 1 and the least value greater
 * than 1 that is representable in the given floating-point type.  Then
 * 1.0+DBL_EPSILON looks like:
 *
 *         1.00000000000...0000000001 * 2**0
 *
 * while 1.0-DBL_EPSILON looks like:
 *
 *         0.11111111111...1111111111 * 2**0
 *
 * and so represented as:
 *
 *         1.1111111111...11111111110 * 2**-1
 *
 * so, in fact, 1.0-(DBL_EPSILON*.5) works too, but I don't think it
 * really matters.  So, I'll go with the simple 1.0-DBL_EPSILON here.
 *
 * The following python session shows these observations:
 *
 *         >>> 1.0 + 2**(1-53)
 *         1.0000000000000002
 *         >>> 1.0 + 2**(1-54)
 *         1.0
 *         >>> 1.0 - 2**(1-53)
 *         0.99999999999999978
 *         >>> 1.0 - 2**(1-54)
 *         0.99999999999999989
 *         >>> 1.0 - 2**(1-53)*.5
 *         0.99999999999999989
 *         >>> 1.0 - 2**(1-55)
 *         1.0
 */
#define RSVG_ONE_MINUS_EPSILON (1.0 - DBL_EPSILON)

GType _rsvg_register_types (GTypeModule * module);

struct RsvgSaxHandler {
    void (*free) (RsvgSaxHandler * self);
    void (*start_element) (RsvgSaxHandler * self, const char *name, RsvgPropertyBag * atts);
    void (*end_element) (RsvgSaxHandler * self, const char *name);
    void (*characters) (RsvgSaxHandler * self, const char *ch, int len);
};

struct RsvgHandlePrivate {
    gboolean is_disposed;

    RsvgSizeFunc size_func;
    gpointer user_data;
    GDestroyNotify user_data_destroy;

    /* stack; there is a state for each element */

    RsvgDefs *defs;
    guint nest_level;
    RsvgNode *currentnode;
    /* this is the root level of the displayable tree, essentially what the
       file is converted into at the end */
    RsvgNode *treebase;

    GHashTable *css_props;

    /* not a handler stack. each nested handler keeps
     * track of its parent
     */
    RsvgSaxHandler *handler;
    int handler_nest;

    GHashTable *entities;       /* g_malloc'd string -> xmlEntityPtr */

    xmlParserCtxtPtr ctxt;
    GError **error;

    double dpi_x;
    double dpi_y;

    GString *title;
    GString *desc;
    GString *metadata;

    gchar *base_uri;

    gboolean finished;

    gboolean first_write;
    gboolean is_gzipped;
    void *gzipped_data;         /* really a GsfOutput */
};

typedef struct {
    gboolean active;
    double x, y, w, h;
} RsvgViewBox;

/*Contextual information for the drawing phase*/

struct RsvgDrawingCtx {
    RsvgRender *render;
    GSList *state;
    GError **error;
    RsvgDefs *defs;
    gchar *base_uri;
    GMemChunk *state_allocator;
    PangoContext *pango_context;
    double dpi_x, dpi_y;
    RsvgViewBox vb;
    GSList *vb_stack;
    GSList *drawsub_stack;
};

/*Abstract base class for context for our backends (one as yet)*/

struct RsvgRender {
    void (*free) (RsvgRender * self);

    PangoContext    *(*create_pango_context)	(RsvgDrawingCtx * ctx);
    void	     (*render_pango_layout)	(RsvgDrawingCtx * ctx, PangoLayout *layout, 
						 double x, double y);
    void	     (*render_path)		(RsvgDrawingCtx * ctx, const RsvgBpathDef * path);
    void	     (*render_image)		(RsvgDrawingCtx * ctx, const GdkPixbuf * pixbuf,
						 double x, double y, double w, double h);
    void	     (*pop_discrete_layer)	(RsvgDrawingCtx * ctx);
    void	     (*push_discrete_layer)	(RsvgDrawingCtx * ctx);
    void	     (*add_clipping_rect)	(RsvgDrawingCtx * ctx, double x, double y, 
						 double w, double h);
    GdkPixbuf	    *(*get_image_of_node)	(RsvgDrawingCtx * ctx, RsvgNode * drawable, 
						 double w, double h);
};


typedef struct {
    double length;
    char factor;
} RsvgLength;

struct _RsvgIRect {
    int x0, y0, x1, y1;
};

typedef struct {
    gdouble x, y, w, h;
    gboolean virgin;
    double affine[6];
} RsvgBbox;

typedef enum {
    RSVG_SIZE_ZOOM,
    RSVG_SIZE_WH,
    RSVG_SIZE_WH_MAX,
    RSVG_SIZE_ZOOM_MAX
} RsvgSizeType;

typedef enum {
    objectBoundingBox, userSpaceOnUse
} RsvgCoordUnits;

struct RsvgSizeCallbackData {
    RsvgSizeType type;
    double x_zoom;
    double y_zoom;
    gint width;
    gint height;

    gboolean keep_aspect_ratio;
};

void _rsvg_size_callback (int *width, int *height, gpointer data);

struct _RsvgPropertyBag {
    GHashTable *props;
};

struct _RsvgNode {
    RsvgState *state;
    RsvgNode *parent;
    GString *type;
    GPtrArray *children;
    void (*free) (RsvgNode * self);
    void (*draw) (RsvgNode * self, RsvgDrawingCtx * ctx, int dominate);
    void (*set_atts) (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag *);
};

struct _RsvgNodeChars {
    RsvgNode super;
    GString *contents;
};

typedef void (*RsvgPropertyBagEnumFunc) (const char *key, const char *value, gpointer user_data);

RsvgPropertyBag	    *rsvg_property_bag_new	(const char **atts);
void		     rsvg_property_bag_free	(RsvgPropertyBag * bag);
G_CONST_RETURN char *rsvg_property_bag_lookup	(RsvgPropertyBag * bag, const char *key);
guint		     rsvg_property_bag_size	(RsvgPropertyBag * bag);
void		     rsvg_property_bag_enumerate (RsvgPropertyBag * bag, RsvgPropertyBagEnumFunc func, 
						  gpointer user_data);

GdkPixbuf *rsvg_pixbuf_from_data_with_size_data (const guchar * buff,
                                                 size_t len,
                                                 struct RsvgSizeCallbackData *data,
                                                 const char *base_uri, GError ** error);

gboolean     rsvg_eval_switch_attributes	(RsvgPropertyBag * atts, gboolean * p_has_cond);
GdkPixbuf   *_rsvg_pixbuf_new_cleared		(GdkColorspace colorspace, gboolean has_alpha,
						 int bits_per_sample, int width, int height);

gchar	    *rsvg_get_base_uri_from_filename	(const gchar * file_name);
GByteArray  *_rsvg_acquire_xlink_href_resource	(const char *href,
						 const char *base_uri, GError ** err);

void rsvg_pop_discrete_layer	(RsvgDrawingCtx * ctx);
void rsvg_push_discrete_layer	(RsvgDrawingCtx * ctx);
void rsvg_render_path		(RsvgDrawingCtx * ctx, const char *d);
void rsvg_render_image		(RsvgDrawingCtx * ctx, GdkPixbuf * pb,
				 double x, double y, double w, double h);
void rsvg_render_free		(RsvgRender * render);
void rsvg_add_clipping_rect	(RsvgDrawingCtx * ctx, double x, double y, double w, double h);
GdkPixbuf *rsvg_get_image_of_node (RsvgDrawingCtx * ctx, RsvgNode * drawable, double w, double h);


void _rsvg_affine_invert (double dst_affine[6], const double src_affine[6]);

/* flip the matrix, FALSE, FALSE is a simple copy operation, and
   TRUE, TRUE equals a rotation by 180 degrees */
void _rsvg_affine_flip (double dst_affine[6], const double src_affine[6], int horz, int vert);

void _rsvg_affine_multiply (double dst[6], const double src1[6], const double src2[6]);

/* set up the identity matrix */
void _rsvg_affine_identity (double dst[6]);

/* set up a scaling matrix */
void _rsvg_affine_scale (double dst[6], double sx, double sy);

/* set up a rotation matrix; theta is given in degrees */
void _rsvg_affine_rotate (double dst[6], double theta);

/* set up a shearing matrix; theta is given in degrees */
void _rsvg_affine_shear (double dst[6], double theta);

/* set up a translation matrix */
void _rsvg_affine_translate (double dst[6], double tx, double ty);


/* find the affine's "expansion factor", i.e. the scale amount */
double _rsvg_affine_expansion (const double src[6]);

/* Determine whether the affine transformation is rectilinear,
   i.e. whether a rectangle aligned to the grid is transformed into
   another rectangle aligned to the grid. */
int _rsvg_affine_rectilinear (const double src[6]);

/* Determine whether two affine transformations are equal within grid allignment */
int _rsvg_affine_equal (double matrix1[6], double matrix2[6]);

void rsvg_node_set_atts (RsvgNode * node, RsvgHandle * ctx, RsvgPropertyBag * atts);

void rsvg_drawing_ctx_free (RsvgDrawingCtx * handle);

void rsvg_bbox_init	(RsvgBbox * self, double *affine);
void rsvg_bbox_insert	(RsvgBbox * dst, RsvgBbox * src);
void rsvg_bbox_clip	(RsvgBbox * dst, RsvgBbox * src);

double _rsvg_css_normalize_length	(const RsvgLength * in, RsvgDrawingCtx * ctx, char dir);
double _rsvg_css_hand_normalize_length	(const RsvgLength * in, gdouble pixels_per_inch,
					 gdouble width_or_height, gdouble font_size);

RsvgLength _rsvg_css_parse_length (const char *str);

void _rsvg_push_view_box    (RsvgDrawingCtx * ctx, double w, double h);
void _rsvg_pop_view_box	    (RsvgDrawingCtx * ctx);

void rsvg_SAX_handler_struct_init (void);

char *rsvg_get_url_string (const char *str);

void rsvg_return_if_fail_warning (const char *pretty_function,
                                  const char *expression, GError ** error);

#define rsvg_return_if_fail(expr, error)		G_STMT_START{			\
     if G_LIKELY(expr) { } else       					\
       {								\
	 rsvg_return_if_fail_warning (G_GNUC_PRETTY_FUNCTION,		        \
		                   #expr, error);				\
	 return;							\
       };				}G_STMT_END

#define rsvg_return_val_if_fail(expr,val,error)	G_STMT_START{			\
     if G_LIKELY(expr) { } else						\
       {								\
	 rsvg_return_if_fail_warning (G_GNUC_PRETTY_FUNCTION,		        \
		                   #expr, error);				\
	 return (val);							\
       };				}G_STMT_END

G_END_DECLS

#endif