]> git.cworth.org Git - svg2png/blob - svg2pdf.c
Add example compilation command line
[svg2png] / svg2pdf.c
1 /* gcc `pkg-config --cflags --libs librsvg-2.0 cairo-pdf` -o svg2pdf svg2pdf.c
2  *
3  * Copyright © 2005 Red Hat, Inc.
4  * Copyright © 2006 Red Hat, Inc.
5  * Copyright © 2007 Red Hat, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software
8  * and its documentation for any purpose is hereby granted without
9  * fee, provided that the above copyright notice appear in all copies
10  * and that both that copyright notice and this permission notice
11  * appear in supporting documentation, and that the name of
12  * Red Hat, Inc. not be used in advertising or publicity pertaining to
13  * distribution of the software without specific, written prior
14  * permission. Red Hat, Inc. makes no representations about the
15  * suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
19  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
21  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
23  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  *
26  * Authors: Kristian Høgsberg <krh@redhat.com>
27  *          Carl Worth <cworth@redhat.com>
28  *          Behdad Esfahbod <besfahbo@redhat.com>
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <librsvg/rsvg.h>
34 #include <librsvg/rsvg-cairo.h>
35
36 #include <cairo-pdf.h>
37
38 #define FAIL(msg)                                                       \
39     do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
40
41 #define PIXELS_PER_POINT 1
42
43 int main (int argc, char *argv[])
44 {
45     GError *error = NULL;
46     RsvgHandle *handle;
47     RsvgDimensionData dim;
48     double width, height;
49     const char *filename = argv[1];
50     const char *output_filename = argv[2];
51     cairo_surface_t *surface;
52     cairo_t *cr;
53     cairo_status_t status;
54
55     if (argc != 3)
56         FAIL ("usage: svg2pdf input_file.svg output_file.pdf");
57
58     g_type_init ();
59
60     rsvg_set_default_dpi (72.0);
61     handle = rsvg_handle_new_from_file (filename, &error);
62     if (error != NULL)
63         FAIL (error->message);
64
65     rsvg_handle_get_dimensions (handle, &dim);
66     width = dim.width;
67     height = dim.height;
68
69     surface = cairo_pdf_surface_create (output_filename, width, height);
70     cr = cairo_create (surface);
71
72     /* Clear background */
73     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
74     cairo_paint (cr);
75
76     rsvg_handle_render_cairo (handle, cr);
77
78     status = cairo_status (cr);
79     if (status)
80         FAIL (cairo_status_to_string (status));
81
82     cairo_destroy (cr);
83     cairo_surface_destroy (surface);
84
85     return 0;
86 }