1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/*
3
 * Copyright © 2004,2006 Red Hat, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Red Hat, Inc. makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Author: Carl D. Worth <cworth@cworth.org>
25
 */
26

            
27
#include "cairo-boilerplate-private.h"
28

            
29
#if CAIRO_CAN_TEST_SVG_SURFACE
30

            
31
#include <cairo-svg.h>
32
#include <cairo-svg-surface-private.h>
33
#include <cairo-paginated-surface-private.h>
34

            
35
#if HAVE_SIGNAL_H
36
#include <stdlib.h>
37
#include <signal.h>
38
#endif
39

            
40
#if HAVE_SYS_WAIT_H
41
#include <sys/wait.h>
42
#endif
43

            
44
#if ! CAIRO_HAS_RECORDING_SURFACE
45
#define CAIRO_SURFACE_TYPE_RECORDING CAIRO_INTERNAL_SURFACE_TYPE_RECORDING
46
#endif
47

            
48
static const cairo_user_data_key_t svg_closure_key;
49

            
50
typedef struct _svg_target_closure {
51
    char    *filename;
52
    int     width, height;
53
    cairo_surface_t	*target;
54
} svg_target_closure_t;
55

            
56
static cairo_surface_t *
57
_cairo_boilerplate_svg_create_surface (const char		 *name,
58
				       cairo_content_t		  content,
59
				       cairo_svg_version_t	  version,
60
				       double			  width,
61
				       double			  height,
62
				       double			  max_width,
63
				       double			  max_height,
64
				       cairo_boilerplate_mode_t   mode,
65
				       void			**closure)
66
{
67
    svg_target_closure_t *ptc;
68
    cairo_surface_t *surface;
69
    cairo_status_t status;
70

            
71
    *closure = ptc = xmalloc (sizeof (svg_target_closure_t));
72

            
73
    ptc->width = ceil (width);
74
    ptc->height = ceil (height);
75

            
76
    xasprintf (&ptc->filename, "%s.out.svg", name);
77
    xunlink (ptc->filename);
78

            
79
    surface = cairo_svg_surface_create (ptc->filename, width, height);
80
    if (cairo_surface_status (surface))
81
	goto CLEANUP_FILENAME;
82

            
83
    cairo_svg_surface_restrict_to_version (surface, version);
84
    cairo_surface_set_fallback_resolution (surface, 72., 72.);
85

            
86
    if (content == CAIRO_CONTENT_COLOR) {
87
	ptc->target = surface;
88
	surface = cairo_surface_create_similar (ptc->target,
89
						CAIRO_CONTENT_COLOR,
90
						ptc->width, ptc->height);
91
	if (cairo_surface_status (surface))
92
	    goto CLEANUP_TARGET;
93
    } else
94
	ptc->target = NULL;
95

            
96
    status = cairo_surface_set_user_data (surface, &svg_closure_key, ptc, NULL);
97
    if (status == CAIRO_STATUS_SUCCESS)
98
	return surface;
99

            
100
    cairo_surface_destroy (surface);
101
    surface = cairo_boilerplate_surface_create_in_error (status);
102

            
103
  CLEANUP_TARGET:
104
    cairo_surface_destroy (ptc->target);
105
  CLEANUP_FILENAME:
106
    free (ptc->filename);
107
    free (ptc);
108
    return surface;
109
}
110

            
111
static cairo_surface_t *
112
_cairo_boilerplate_svg11_create_surface (const char		   *name,
113
					 cairo_content_t	    content,
114
					 double 		    width,
115
					 double 		    height,
116
					 double 		    max_width,
117
					 double 		    max_height,
118
					 cairo_boilerplate_mode_t   mode,
119
					 void			  **closure)
120
{
121
    /* current default, but be explicit in case the default changes */
122
    return _cairo_boilerplate_svg_create_surface (name, content,
123
						  CAIRO_SVG_VERSION_1_1,
124
						  width, height,
125
						  max_width, max_height,
126
						  mode,
127
						  closure);
128
}
129

            
130
static cairo_status_t
131
_cairo_boilerplate_svg_finish_surface (cairo_surface_t *surface)
132
{
133
    svg_target_closure_t *ptc = cairo_surface_get_user_data (surface,
134
							     &svg_closure_key);
135
    cairo_status_t status;
136

            
137
    if (ptc->target) {
138
	/* Both surface and ptc->target were originally created at the
139
	 * same dimensions. We want a 1:1 copy here, so we first clear any
140
	 * device offset and scale on surface.
141
	 *
142
	 * In a more realistic use case of device offsets, the target of
143
	 * this copying would be of a different size than the source, and
144
	 * the offset would be desirable during the copy operation. */
145
	double x_offset, y_offset;
146
	double x_scale, y_scale;
147
	cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
148
	cairo_surface_get_device_scale (surface, &x_scale, &y_scale);
149
	cairo_surface_set_device_offset (surface, 0, 0);
150
	cairo_surface_set_device_scale (surface, 1, 1);
151
	cairo_t *cr;
152
	cr = cairo_create (ptc->target);
153
	cairo_set_source_surface (cr, surface, 0, 0);
154
	cairo_paint (cr);
155
	cairo_show_page (cr);
156
	status = cairo_status (cr);
157
	cairo_destroy (cr);
158
	cairo_surface_set_device_offset (surface, x_offset, y_offset);
159
	cairo_surface_set_device_scale (surface, x_scale, y_scale);
160

            
161
	if (status)
162
	    return status;
163

            
164
	cairo_surface_finish (surface);
165
	status = cairo_surface_status (surface);
166
	if (status)
167
	    return status;
168

            
169
	surface = ptc->target;
170
    }
171

            
172
    cairo_surface_finish (surface);
173
    status = cairo_surface_status (surface);
174
    if (status)
175
	return status;
176

            
177
    return CAIRO_STATUS_SUCCESS;
178
}
179

            
180
static cairo_status_t
181
_cairo_boilerplate_svg_surface_write_to_png (cairo_surface_t *surface,
182
					     const char      *filename)
183
{
184
    svg_target_closure_t *ptc = cairo_surface_get_user_data (surface,
185
							     &svg_closure_key);
186
    char    command[4096];
187
    int exitstatus;
188

            
189
    sprintf (command,
190
#ifndef _WIN32
191
             "./svg2png %s %s",
192
#else
193
             ".\\svg2png %s %s",
194
#endif
195
             ptc->filename, filename);
196

            
197
    exitstatus = system (command);
198
#if _XOPEN_SOURCE && HAVE_SIGNAL_H
199
    if (WIFSIGNALED (exitstatus))
200
	raise (WTERMSIG (exitstatus));
201
#endif
202
    if (exitstatus)
203
	return CAIRO_STATUS_WRITE_ERROR;
204

            
205
    return CAIRO_STATUS_SUCCESS;
206
}
207

            
208
static cairo_surface_t *
209
_cairo_boilerplate_svg_convert_to_image (cairo_surface_t *surface)
210
{
211
    svg_target_closure_t *ptc = cairo_surface_get_user_data (surface,
212
							     &svg_closure_key);
213

            
214
    return cairo_boilerplate_convert_to_image (ptc->filename, 0);
215
}
216

            
217
static cairo_surface_t *
218
_cairo_boilerplate_svg_get_image_surface (cairo_surface_t *surface,
219
					  int		   page,
220
					  int		   width,
221
					  int		   height)
222
{
223
    cairo_surface_t *image;
224
    double x_offset, y_offset;
225
    double x_scale, y_scale;
226

            
227
    if (page != 0)
228
	return cairo_boilerplate_surface_create_in_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
229

            
230
    image = _cairo_boilerplate_svg_convert_to_image (surface);
231
    cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
232
    cairo_surface_get_device_scale (surface, &x_scale, &y_scale);
233
    cairo_surface_set_device_offset (image, x_offset, y_offset);
234
    cairo_surface_set_device_scale (image, x_scale, y_scale);
235
    surface = _cairo_boilerplate_get_image_surface (image, 0, width, height);
236
    cairo_surface_destroy (image);
237

            
238
    return surface;
239
}
240

            
241
static void
242
_cairo_boilerplate_svg_cleanup (void *closure)
243
{
244
    svg_target_closure_t *ptc = closure;
245
    if (ptc->target != NULL) {
246
	cairo_surface_finish (ptc->target);
247
	cairo_surface_destroy (ptc->target);
248
    }
249
    free (ptc->filename);
250
    free (ptc);
251
}
252

            
253
static void
254
_cairo_boilerplate_svg_force_fallbacks (cairo_surface_t *abstract_surface,
255
				       double		 x_pixels_per_inch,
256
				       double		 y_pixels_per_inch)
257
{
258
    svg_target_closure_t *ptc = cairo_surface_get_user_data (abstract_surface,
259
							     &svg_closure_key);
260

            
261
    if (ptc->target)
262
	abstract_surface = ptc->target;
263

            
264
    cairo_paginated_surface_t *paginated = (cairo_paginated_surface_t*) abstract_surface;
265
    _cairo_svg_surface_set_force_fallbacks (paginated->target, TRUE);
266
    cairo_surface_set_fallback_resolution (&paginated->base,
267
					   x_pixels_per_inch,
268
					   y_pixels_per_inch);
269
}
270

            
271
static const cairo_boilerplate_target_t targets[] = {
272
    /* It seems we should be able to round-trip SVG content perfectly
273
     * through librsvg and cairo, but for some mysterious reason, some
274
     * systems get an error of 1 for some pixels on some of the text
275
     * tests. XXX: I'd still like to chase these down at some point.
276
     * For now just set the svg error tolerance to 1. */
277
    {
278
	"svg11", "svg", ".svg", NULL,
279
	CAIRO_SURFACE_TYPE_SVG, CAIRO_CONTENT_COLOR_ALPHA, 1,
280
	"cairo_svg_surface_create",
281
	_cairo_boilerplate_svg11_create_surface,
282
	cairo_surface_create_similar,
283
	_cairo_boilerplate_svg_force_fallbacks,
284
	_cairo_boilerplate_svg_finish_surface,
285
	_cairo_boilerplate_svg_get_image_surface,
286
	_cairo_boilerplate_svg_surface_write_to_png,
287
	_cairo_boilerplate_svg_cleanup,
288
	NULL, NULL, FALSE, TRUE, TRUE
289
    },
290
    {
291
	"svg11", "svg", ".svg", NULL,
292
	CAIRO_SURFACE_TYPE_RECORDING, CAIRO_CONTENT_COLOR, 1,
293
	"cairo_svg_surface_create",
294
	_cairo_boilerplate_svg11_create_surface,
295
	cairo_surface_create_similar,
296
	_cairo_boilerplate_svg_force_fallbacks,
297
	_cairo_boilerplate_svg_finish_surface,
298
	_cairo_boilerplate_svg_get_image_surface,
299
	_cairo_boilerplate_svg_surface_write_to_png,
300
	_cairo_boilerplate_svg_cleanup,
301
	NULL, NULL, FALSE, TRUE, TRUE
302
    },
303
};
304
1
CAIRO_BOILERPLATE (svg, targets)
305

            
306
#else
307

            
308
CAIRO_NO_BOILERPLATE (svg)
309

            
310
#endif