44 #include <librsvg/rsvg.h> 45 #include <librsvg/rsvg-cairo.h> 48 #ifdef CAIRO_HAS_PS_SURFACE 52 #ifdef CAIRO_HAS_PDF_SURFACE 53 #include <cairo-pdf.h> 56 #ifdef CAIRO_HAS_SVG_SURFACE 57 #include <cairo-svg.h> 81 display_error (GError *
err)
85 g_print (
"%s\n", err->message);
92 rsvg_cairo_size_callback (
int *width,
int *height, gpointer data)
94 RsvgDimensionData *dimensions = data;
95 *width = dimensions->width;
96 *height = dimensions->height;
100 static cairo_status_t
101 rsvg_cairo_write_func (
void *closure,
const unsigned char *data,
unsigned int length)
103 if (fwrite (data, 1, length, (FILE *) closure) == length)
104 return CAIRO_STATUS_SUCCESS;
105 return CAIRO_STATUS_WRITE_ERROR;
110 main (
int argc,
char **argv)
112 GOptionContext *g_option_context;
115 char *source_rect_string = NULL;
117 GError *
error = NULL;
118 char *filename = NULL;
122 cairo_surface_t *surface = NULL;
124 RsvgDimensionData dimensions;
125 FILE *output_file = stdout;
129 GOptionEntry options_table[] = {
130 {
"width",
'w', 0, G_OPTION_ARG_INT, &width,
131 N_(
"width [optional; defaults to the SVG's width]"), N_(
"<int>")},
132 {
"height",
'h', 0, G_OPTION_ARG_INT, &height,
133 N_(
"height [optional; defaults to the SVG's height]"), N_(
"<int>")},
134 {
"source-rect",
'r', 0, G_OPTION_ARG_STRING, &source_rect_string,
135 N_(
"source rectangle [optional; defaults to rectangle of the SVG document]"), N_(
"left:top:width:height")},
136 {
"output",
'o', 0, G_OPTION_ARG_STRING, &output,
137 N_(
"output filename"), NULL},
138 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, N_(
"FILE")},
143 setlocale (LC_ALL,
"");
145 g_option_context = g_option_context_new (_(
"- SVG Converter"));
146 g_option_context_add_main_entries (g_option_context, options_table, NULL);
147 g_option_context_set_help_enabled (g_option_context, TRUE);
148 if (!g_option_context_parse (g_option_context, &argc, &argv, &error))
150 display_error (error);
154 g_option_context_free (g_option_context);
158 output_file = fopen (output,
"wb");
161 fprintf (stderr, _(
"Error saving to file: %s\n"), output);
170 if(source_rect_string != NULL)
172 const int n = sscanf(source_rect_string,
"%lg:%lg:%lg:%lg",
173 &source_rect.left, &source_rect.top,
174 &source_rect.width, &source_rect.height);
175 if (n != 4 || source_rect.width <= 0.0 || source_rect.height < 0.0)
177 fprintf (stderr, _(
"Invalid source rect: %s\n"), source_rect_string);
184 rsvg = rsvg_handle_new_from_file (filename, &error);
188 fprintf (stderr, _(
"Error reading SVG:"));
189 display_error (error);
190 fprintf (stderr,
"\n");
195 if(source_rect_string == NULL)
197 rsvg_handle_set_size_callback (rsvg, rsvg_cairo_size_callback, &dimensions, NULL);
198 source_rect.left = 0;
200 source_rect.width = dimensions.width;
201 source_rect.height = dimensions.height;
204 rsvg_handle_get_dimensions (rsvg, &dimensions);
206 if (width != -1 && height != -1)
208 dimensions.width = width;
209 dimensions.height = height;
211 else if(source_rect_string != NULL)
213 dimensions.width = source_rect.width;
214 dimensions.height = source_rect.height;
217 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
221 cr = cairo_create (surface);
223 cairo_translate (cr, -source_rect.left, -source_rect.top);
225 if (width != -1 && height != -1 && source_rect_string != NULL)
228 (
double)dimensions.width / (
double)source_rect.width,
229 (
double)dimensions.height / (
double)source_rect.height);
232 rsvg_handle_render_cairo (rsvg, cr);
234 cairo_surface_write_to_png_stream (surface, rsvg_cairo_write_func, output_file);
236 g_object_unref (G_OBJECT (rsvg));
239 cairo_surface_destroy (surface);
241 fclose (output_file);
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.