static int rsvg_defs_load_extern (const RsvgDefs * defs, const char *name) { RsvgHandle *handle; gchar *filename, *base_uri; GByteArray *chars; filename = rsvg_get_file_path (name, defs->base_uri); chars = _rsvg_acquire_xlink_href_resource (name, defs->base_uri, NULL); if (chars) { handle = rsvg_handle_new (); base_uri = rsvg_get_base_uri_from_filename (filename); rsvg_handle_set_base_uri (handle, base_uri); g_free (base_uri); if (rsvg_handle_write (handle, chars->data, chars->len, NULL) && rsvg_handle_close (handle, NULL)) { g_hash_table_insert (defs->externs, g_strdup (name), handle); } g_byte_array_free (chars, TRUE); } g_free (filename); return 0; }
int main (int argc, char **argv) { GError *err = NULL; GOptionContext *g_option_context; double x_zoom = 1.0; double y_zoom = 1.0; double dpi_x = -1.0; double dpi_y = -1.0; int width = -1; int height = -1; int bVersion = 0; char *bg_color = NULL; char *base_uri = NULL; int bKeepAspect = 0; char *id = NULL; int xid = -1; int from_stdin = 0; ViewerCbInfo info; struct RsvgSizeCallbackData size_data; char **args = NULL; gint n_args = 0; GOptionEntry options_table[] = { #ifdef ENABLE_XEMBED {"xid", 'i', 0, G_OPTION_ARG_INT, &xid, N_("XWindow ID [for X11 embedding]"), N_("<int>")}, #endif {"stdin", 's', 0, G_OPTION_ARG_NONE, &from_stdin, N_("Read from stdin instead of a file"), NULL}, {"dpi-x", 'd', 0, G_OPTION_ARG_DOUBLE, &dpi_x, N_("Set the # of Pixels Per Inch"), N_("<float>")}, {"dpi-y", 'p', 0, G_OPTION_ARG_DOUBLE, &dpi_y, N_("Set the # of Pixels Per Inch"), N_("<float>")}, {"x-zoom", 'x', 0, G_OPTION_ARG_DOUBLE, &x_zoom, N_("Set the x zoom factor"), N_("<float>")}, {"y-zoom", 'y', 0, G_OPTION_ARG_DOUBLE, &y_zoom, N_("Set the y zoom factor"), N_("<float>")}, {"width", 'w', 0, G_OPTION_ARG_INT, &width, N_("Set the image's width"), N_("<int>")}, {"height", 'h', 0, G_OPTION_ARG_INT, &height, N_("Set the image's height"), N_("<int>")}, {"bg-color", 'b', 0, G_OPTION_ARG_STRING, &bg_color, N_("Set the image background color (default: transparent)"), N_("<string>")}, {"base-uri", 'u', 0, G_OPTION_ARG_STRING, &base_uri, N_("Set the base URI (default: none)"), N_("<string>")}, {"id", 0, 0, G_OPTION_ARG_STRING, &id, N_("Only show one node (default: all)"), N_("<string>")}, {"keep-aspect", 'k', 0, G_OPTION_ARG_NONE, &bKeepAspect, N_("Preserve the image's aspect ratio"), NULL}, {"version", 'v', 0, G_OPTION_ARG_NONE, &bVersion, N_("Show version information"), NULL}, {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, N_("[FILE...]")}, {NULL} }; g_thread_init (NULL); info.pixbuf = NULL; info.svg_bytes = NULL; info.window = NULL; info.popup_menu = NULL; g_option_context = g_option_context_new ("- SVG Viewer"); g_option_context_add_main_entries (g_option_context, options_table, NULL); g_option_context_add_group (g_option_context, gtk_get_option_group (TRUE)); g_option_context_set_help_enabled (g_option_context, TRUE); if (!g_option_context_parse (g_option_context, &argc, &argv, NULL)) { exit (1); } g_option_context_free (g_option_context); if (bVersion != 0) { g_message (_("rsvg-view version %s\n"), VERSION); return 0; } if (args) { while (args[n_args] != NULL) n_args++; } if ((!from_stdin) && (n_args != 1)) { g_print (_("No files specified, and not using --stdin\n")); return 1; } /* initialize gtk+ and rsvg */ rsvg_init (); rsvg_set_default_dpi_x_y (dpi_x, dpi_y); /* if both are unspecified, assume user wants to zoom the pixbuf in at least 1 dimension */ if (width == -1 && height == -1) { size_data.type = RSVG_SIZE_ZOOM; size_data.x_zoom = x_zoom; size_data.y_zoom = y_zoom; } /* if both are unspecified, assume user wants to resize pixbuf in at least 1 dimension */ else if (x_zoom == 1.0 && y_zoom == 1.0) { size_data.type = RSVG_SIZE_WH; size_data.width = width; size_data.height = height; } /* assume the user wants to zoom the pixbuf, but cap the maximum size */ else { size_data.type = RSVG_SIZE_ZOOM_MAX; size_data.x_zoom = x_zoom; size_data.y_zoom = y_zoom; size_data.width = width; size_data.height = height; } size_data.keep_aspect_ratio = bKeepAspect; if (!from_stdin) { if (base_uri == NULL) base_uri = (char *) args[0]; info.svg_bytes = _rsvg_acquire_xlink_href_resource (args[0], base_uri, NULL); } else { info.svg_bytes = g_byte_array_new (); for (;;) { unsigned char buf[1024 * 8]; size_t nread = fread (buf, 1, sizeof (buf), stdin); if (nread > 0) g_byte_array_append (info.svg_bytes, buf, nread); if (nread < sizeof (buf)) { if (ferror (stdin)) { g_print (_("Error reading\n")); g_byte_array_free (info.svg_bytes, TRUE); fclose (stdin); return 1; } else if (feof (stdin)) break; } } fclose (stdin); } if (!info.svg_bytes || !info.svg_bytes->len) { g_print (_("Couldn't open %s\n"), args[0]); return 1; } info.base_uri = base_uri; info.id = id; info.pixbuf = pixbuf_from_data_with_size_data (info.svg_bytes->data, info.svg_bytes->len, &size_data, base_uri, id, &err); if (!info.pixbuf) { g_print (_("Error displaying image")); if (err) { g_print (": %s", err->message); g_error_free (err); } g_print ("\n"); return 1; } info.accel_group = gtk_accel_group_new (); view_pixbuf (&info, xid, bg_color); /* run the gtk+ main loop */ gtk_main (); g_object_unref (G_OBJECT (info.pixbuf)); g_byte_array_free (info.svg_bytes, TRUE); rsvg_term (); return 0; }