1 /*
2  * Copyright © 2005 Red Hat, Inc.
3  * Copyright © 2005 Emmanuel Pacaud <emmanuel.pacaud@free.fr>
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: Kristian Høgsberg <krh@redhat.com>
25  *	   Emmanuel Pacaud <emmanuel.pacaud@free.fr>
26  */
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <librsvg/rsvg.h>
31 
32 #define FAIL(msg)							\
33     do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
34 
main(int argc,char * argv[])35 int main (int argc, char *argv[])
36 {
37     GError *error = NULL;
38     RsvgHandle *handle;
39     RsvgDimensionData dimensions;
40     const char *filename = argv[1];
41     const char *output_filename = argv[2];
42     cairo_surface_t *surface;
43     cairo_t *cr;
44     cairo_status_t status;
45 
46     if (argc != 3)
47 	FAIL ("usage: svg2png input_file.svg output_file.png");
48 
49     #if GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION <= 34
50     g_type_init ();
51     #endif
52 
53     error = NULL;
54 
55     handle = rsvg_handle_new_from_file (filename, &error);
56     if (!handle)
57 	FAIL (error->message);
58 
59     rsvg_handle_set_dpi (handle, 72.0);
60     rsvg_handle_get_dimensions (handle, &dimensions);
61 
62     surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
63 					  dimensions.width, dimensions.height);
64     cr = cairo_create (surface);
65     cairo_surface_destroy (surface);
66 
67     cairo_set_source_rgb (cr, 1,1,1);
68     cairo_paint (cr);
69     cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
70 
71     if (!rsvg_handle_render_cairo (handle, cr))
72 	FAIL (error->message);
73 
74     cairo_pop_group_to_source (cr);
75     cairo_paint (cr);
76 
77     status = cairo_surface_write_to_png (cairo_get_target (cr),
78 					 output_filename);
79     cairo_destroy (cr);
80     if (status)
81 	FAIL (cairo_status_to_string (status));
82 
83     g_object_unref (handle);
84     return 0;
85 }
86