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

            
31
/* Disable deprecation warnings coming from librsvg */
32
#define RSVG_DISABLE_DEPRECATION_WARNINGS
33
#include <librsvg/rsvg.h>
34

            
35
#define FAIL(msg)							\
36
    do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
37

            
38
int main (int argc, char *argv[])
39
{
40
    GError *error = NULL;
41
    RsvgHandle *handle;
42
    RsvgDimensionData dimensions;
43
    const char *filename = argv[1];
44
    const char *output_filename = argv[2];
45
    cairo_surface_t *surface;
46
    cairo_t *cr;
47
    cairo_status_t status;
48

            
49
    if (argc != 3)
50
	FAIL ("usage: svg2png input_file.svg output_file.png");
51

            
52
    #if GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION <= 34
53
    g_type_init ();
54
    #endif
55

            
56
    error = NULL;
57

            
58
    handle = rsvg_handle_new_from_file (filename, &error);
59
    if (!handle)
60
	FAIL (error->message);
61

            
62
    rsvg_handle_set_dpi (handle, 72.0);
63
    rsvg_handle_get_dimensions (handle, &dimensions);
64

            
65
    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
66
					  dimensions.width, dimensions.height);
67
    cr = cairo_create (surface);
68
    cairo_surface_destroy (surface);
69

            
70
    cairo_set_source_rgb (cr, 1,1,1);
71
    cairo_paint (cr);
72
    cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
73

            
74
    if (!rsvg_handle_render_cairo (handle, cr))
75
	FAIL (error->message);
76

            
77
    cairo_pop_group_to_source (cr);
78
    cairo_paint (cr);
79

            
80
    status = cairo_surface_write_to_png (cairo_get_target (cr),
81
					 output_filename);
82
    cairo_destroy (cr);
83
    if (status)
84
	FAIL (cairo_status_to_string (status));
85

            
86
    g_object_unref (handle);
87
    return 0;
88
}