35 #include <librsvg/rsvg.h> 36 #include <librsvg/rsvg-cairo.h> 39 #ifdef CAIRO_HAS_PS_SURFACE 43 #ifdef CAIRO_HAS_PDF_SURFACE 44 #include <cairo-pdf.h> 47 #ifdef CAIRO_HAS_SVG_SURFACE 48 #include <cairo-svg.h> 72 display_error (GError *
err)
76 g_print (
"%s\n", err->message);
83 rsvg_cairo_size_callback (
int *width,
int *height, gpointer data)
85 RsvgDimensionData *dimensions = data;
86 *width = dimensions->width;
87 *height = dimensions->height;
92 rsvg_cairo_write_func (
void *closure,
const unsigned char *data,
unsigned int length)
94 if (fwrite (data, 1, length, (FILE *) closure) == length)
95 return CAIRO_STATUS_SUCCESS;
96 return CAIRO_STATUS_WRITE_ERROR;
101 main (
int argc,
char **argv)
103 GOptionContext *g_option_context;
106 char *source_rect_string = NULL;
108 GError *
error = NULL;
109 char *filename = NULL;
113 cairo_surface_t *surface = NULL;
115 RsvgDimensionData dimensions;
116 FILE *output_file = stdout;
120 GOptionEntry options_table[] = {
121 {
"width",
'w', 0, G_OPTION_ARG_INT, &width,
122 N_(
"width [optional; defaults to the SVG's width]"), N_(
"<int>")},
123 {
"height",
'h', 0, G_OPTION_ARG_INT, &height,
124 N_(
"height [optional; defaults to the SVG's height]"), N_(
"<int>")},
125 {
"source-rect",
'r', 0, G_OPTION_ARG_STRING, &source_rect_string,
126 N_(
"source rectangle [optional; defaults to rectangle of the SVG document]"), N_(
"left:top:width:height")},
127 {
"output",
'o', 0, G_OPTION_ARG_STRING, &output,
128 N_(
"output filename"), NULL},
129 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, N_(
"FILE")},
134 setlocale (LC_ALL,
"");
136 g_option_context = g_option_context_new (_(
"- SVG Converter"));
137 g_option_context_add_main_entries (g_option_context, options_table, NULL);
138 g_option_context_set_help_enabled (g_option_context, TRUE);
139 if (!g_option_context_parse (g_option_context, &argc, &argv, &error))
141 display_error (error);
145 g_option_context_free (g_option_context);
149 output_file = fopen (output,
"wb");
152 fprintf (stderr, _(
"Error saving to file: %s\n"), output);
161 if(source_rect_string != NULL)
163 const int n = sscanf(source_rect_string,
"%lg:%lg:%lg:%lg",
164 &source_rect.left, &source_rect.top,
165 &source_rect.width, &source_rect.height);
166 if (n != 4 || source_rect.width <= 0.0 || source_rect.height < 0.0)
168 fprintf (stderr, _(
"Invalid source rect: %s\n"), source_rect_string);
175 rsvg = rsvg_handle_new_from_file (filename, &error);
179 fprintf (stderr, _(
"Error reading SVG:"));
180 display_error (error);
181 fprintf (stderr,
"\n");
186 if(source_rect_string == NULL)
188 rsvg_handle_set_size_callback (rsvg, rsvg_cairo_size_callback, &dimensions, NULL);
189 source_rect.left = 0;
191 source_rect.width = dimensions.width;
192 source_rect.height = dimensions.height;
195 rsvg_handle_get_dimensions (rsvg, &dimensions);
197 if (width != -1 && height != -1)
199 dimensions.width = width;
200 dimensions.height = height;
202 else if(source_rect_string != NULL)
204 dimensions.width = source_rect.width;
205 dimensions.height = source_rect.height;
208 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
212 cr = cairo_create (surface);
214 cairo_translate (cr, -source_rect.left, -source_rect.top);
216 if (width != -1 && height != -1 && source_rect_string != NULL)
219 (
double)dimensions.width / (
double)source_rect.width,
220 (
double)dimensions.height / (
double)source_rect.height);
223 rsvg_handle_render_cairo (rsvg, cr);
225 cairo_surface_write_to_png_stream (surface, rsvg_cairo_write_func, output_file);
227 g_object_unref (G_OBJECT (rsvg));
230 cairo_surface_destroy (surface);
232 fclose (output_file);
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.