103 GOptionContext *g_option_context;
106 char *source_rect_string =
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))
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,
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:"));
181 fprintf (stderr,
"\n");
186 if(source_rect_string ==
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);
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);
219 (
double)dimensions.width / (
double)source_rect.
width,
220 (
double)dimensions.height / (
double)source_rect.
height);
223 rsvg_handle_render_cairo (rsvg, cr);
227 g_object_unref (G_OBJECT (rsvg));
230 cairo_surface_destroy (surface);
232 fclose (output_file);