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; }
static GdkPixbuf * pixbuf_from_data_with_size_data (const guchar * buff, size_t len, struct RsvgSizeCallbackData *data, const char *base_uri, const char *id, GError ** error) { RsvgHandle *handle; GdkPixbuf *retval; handle = rsvg_handle_new (); if (!handle) { g_set_error (error, rsvg_error_quark (), 0, _("Error creating SVG reader")); return NULL; } rsvg_handle_set_size_callback (handle, _rsvg_size_callback, data, NULL); rsvg_handle_set_base_uri (handle, base_uri); if (!rsvg_handle_write (handle, buff, len, error)) { g_object_unref (G_OBJECT (handle)); return NULL; } if (!rsvg_handle_close (handle, error)) { g_object_unref (G_OBJECT (handle)); return NULL; } retval = rsvg_handle_get_pixbuf_sub (handle, id); g_object_unref (G_OBJECT (handle)); return retval; }
/* This function renders a pixbuf from an SVG file according to vals. */ static GdkPixbuf * load_rsvg_pixbuf (const gchar *filename, SvgLoadVals *vals, GError **error) { GdkPixbuf *pixbuf = NULL; RsvgHandle *handle; GIOChannel *io; gchar *uri; GIOStatus status = G_IO_STATUS_NORMAL; gboolean success = TRUE; io = g_io_channel_new_file (filename, "r", error); if (!io) return NULL; g_io_channel_set_encoding (io, NULL, NULL); handle = rsvg_handle_new (); rsvg_handle_set_dpi (handle, vals->resolution); /* set the base URI so that librsvg can resolve relative paths */ uri = g_filename_to_uri (filename, NULL, NULL); if (uri) { gchar *p = strrchr (uri, '/'); if (p) *p = '\0'; rsvg_handle_set_base_uri (handle, uri); g_free (uri); } rsvg_handle_set_size_callback (handle, load_set_size_callback, vals, NULL); while (success && status != G_IO_STATUS_EOF) { gchar buf[8192]; gsize len; status = g_io_channel_read_chars (io, buf, sizeof (buf), &len, error); switch (status) { case G_IO_STATUS_ERROR: success = FALSE; break; case G_IO_STATUS_EOF: success = rsvg_handle_close (handle, error); break; case G_IO_STATUS_NORMAL: success = rsvg_handle_write (handle, (const guchar *) buf, len, error); break; case G_IO_STATUS_AGAIN: break; } } g_io_channel_unref (io); if (success) pixbuf = rsvg_handle_get_pixbuf (handle); g_object_unref (handle); return pixbuf; }
static gboolean gdk_pixbuf__svg_image_stop_load (gpointer data, GError **error) { SvgContext *context = (SvgContext *)data; GdkPixbuf *pixbuf; gboolean result = TRUE; if (error) *error = NULL; if (!context->handle) { rsvg_propegate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE); return FALSE; } rsvg_handle_close (context->handle, error); pixbuf = rsvg_handle_get_pixbuf (context->handle); if (pixbuf != NULL) { emit_prepared (context, pixbuf); emit_updated (context, pixbuf); g_object_unref (pixbuf); } else { rsvg_propegate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE); result = FALSE; } g_object_unref (context->handle); g_free (context); return result; }
JNIEXPORT jboolean JNICALL Java_org_gnome_rsvg_RsvgHandle_rsvg_1handle_1close ( JNIEnv* env, jclass cls, jlong _self ) { gboolean result; jboolean _result; RsvgHandle* self; GError* error = NULL; // convert parameter self self = (RsvgHandle*) _self; // call function result = rsvg_handle_close(self, &error); // cleanup parameter self // check for error if (error) { bindings_java_throwGlibException(env, error); return JNI_FALSE; } // translate return value to JNI type _result = (jboolean) result; // and finally return _result; }
UT_Error _importGraphic(const UT_ConstByteBufPtr & pBB) { GdkPixbuf * pixbuf = NULL; GError * err = NULL; RsvgHandle * rsvg = rsvg_handle_new (); if ( FALSE == rsvg_handle_write ( rsvg, static_cast<const guchar *>(pBB->getPointer (0)), static_cast<gsize>(pBB->getLength ()), &err ) ) { UT_DEBUGMSG(("DOM: couldn't write to loader: %s\n", err->message)); g_error_free(err); return UT_ERROR ; } if ( FALSE == rsvg_handle_close ( rsvg, &err ) ) { UT_DEBUGMSG(("DOM: couldn't write to loader: %s\n", err->message)); g_error_free(err); g_object_unref(G_OBJECT(rsvg)); return UT_ERROR ; } pixbuf = rsvg_handle_get_pixbuf ( rsvg ) ; g_object_unref(G_OBJECT(rsvg)); if (!pixbuf) { return UT_ERROR; } g_object_ref (pixbuf); // Initialize stuff to create our PNG. UT_Error error = Initialize_PNG(); if (error) { return error; } if (setjmp(png_jmpbuf(m_pPNG))) { g_object_unref(G_OBJECT(pixbuf)); png_destroy_write_struct(&m_pPNG, &m_pPNGInfo); return UT_ERROR; } // // Build the png member variables. // _createPNGFromPixbuf(pixbuf); // // Get rid of these now that they are no longer needed // g_object_unref(G_OBJECT(pixbuf)); png_destroy_write_struct(&m_pPNG, &m_pPNGInfo); return UT_OK; }
CAMLprim value ml_rsvg_handle_close(value h) { GError *err = NULL; rsvg_handle_close(RsvgHandle_val(h), &err); if (err != NULL) ml_raise_gerror (err); return Val_unit; }
static void xkb_populate_popup_menu (t_xkb *xkb) { gint i, group_count; RsvgHandle *handle; GdkPixbuf *pixbuf, *tmp; gchar *imgfilename; GtkWidget *image; GtkWidget *menu_item; if (G_UNLIKELY (xkb == NULL)) return; xkb_destroy_popup_menu (xkb); xkb->popup = gtk_menu_new (); group_count = xkb_config_get_group_count (); for (i = 0; i < group_count; i++) { gchar *layout_string; imgfilename = xkb_util_get_flag_filename (xkb_config_get_group_name (i)); handle = rsvg_handle_new_from_file (imgfilename, NULL); g_free (imgfilename); if (handle) { tmp = rsvg_handle_get_pixbuf (handle); } layout_string = xkb_config_get_pretty_layout_name (i); menu_item = gtk_menu_item_new_with_label (layout_string); g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK (xkb_plugin_set_group), GINT_TO_POINTER (i)); if (handle) { image = gtk_image_new (); pixbuf = gdk_pixbuf_scale_simple (tmp, 15, 10, GDK_INTERP_BILINEAR); gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf); gtk_widget_show (image); g_object_unref (G_OBJECT (tmp)); g_object_unref (G_OBJECT (pixbuf)); gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image); rsvg_handle_close (handle, NULL); g_object_unref (handle); } gtk_widget_show (menu_item); gtk_menu_shell_append (GTK_MENU_SHELL (xkb->popup), menu_item); } }
int main(int argc, char *argv[]) { FILE *fp; RsvgHandle *rsvg; cairo_device_t *dev = NULL; cairo_surface_t *surface = NULL; cairo_t *cr = NULL; RsvgDimensionData dimensions; if (argc < 3) { usage(); return 0; } fp = fopen(argv[1], "r"); if (fp == NULL) { printf("could not open '%s' for read\n", argv[1]); return 1; } fclose(fp); fp = fopen(argv[2], "w"); if (fp == NULL) { printf("could not open '%s' for write\n", argv[2]); return 1; } dev = cairo_xml_create_for_stream((cairo_write_func_t)write_func, fp); rsvg_set_default_dpi_x_y(-1, -1); rsvg = rsvg_handle_new_from_file(argv[1], NULL); rsvg_handle_get_dimensions(rsvg, &dimensions); fprintf(fp, "<image width='%d' height='%d'>\n", dimensions.width, dimensions.height); surface = cairo_xml_surface_create(dev, CAIRO_CONTENT_COLOR_ALPHA, dimensions.width, dimensions.height); cr = cairo_create(surface); rsvg_handle_render_cairo(rsvg, cr); rsvg_handle_close(rsvg, NULL); cairo_destroy(cr); cairo_surface_destroy(surface); fprintf(fp, "</image>\n"); fclose(fp); return 0; }
PyObject * render_svg_to_buffer(PyObject *module, PyObject *args, PyObject *kwargs) { int len, w, h, i; guchar *svgdata; GError *error; RsvgHandle *svg; GdkPixbuf *pixbuf; gboolean res; size_cb_data cbdata; PyObject *buffer; guchar *buffer_ptr; if (!PyArg_ParseTuple(args, "iis#", &w, &h, &svgdata, &len)) return NULL; cbdata.w = w; cbdata.h = h; svg = rsvg_handle_new(); rsvg_handle_set_size_callback(svg, size_cb, &cbdata, NULL); res = rsvg_handle_write(svg, svgdata, len, &error); res = rsvg_handle_close(svg, &error); pixbuf = rsvg_handle_get_pixbuf(svg); rsvg_handle_free(svg); w = gdk_pixbuf_get_width(pixbuf); h = gdk_pixbuf_get_height(pixbuf); buffer = PyBuffer_New(w*h*4); PyObject_AsWriteBuffer(buffer, (void **)&buffer_ptr, &len); memcpy(buffer_ptr, gdk_pixbuf_get_pixels(pixbuf), w*h*4); g_object_unref(pixbuf); // RGBA to BGRA conversion. // FIXME: MMXify. for (i = 0; i < w*h*4; i+=4) { guchar save = buffer_ptr[i+2]; buffer_ptr[i+2] = buffer_ptr[i]; buffer_ptr[i] = save; } return Py_BuildValue("(iiO)", w, h, buffer); }
static VALUE rb_rsvg_handle_close(VALUE self) { gboolean result; GError *error = NULL; if (RVAL2CBOOL(rb_ivar_get(self, id_closed))) { return Qnil; } result = rsvg_handle_close(_SELF(self), &error); if (result) { rb_ivar_set(self, id_closed, Qtrue); } else { RAISE_GERROR(error); } return CBOOL2RVAL(result); }
void freeSVGCache(symbolObj *s) { #if defined(USE_SVG_CAIRO) || defined(USE_RSVG) struct svg_symbol_cache *cache = s->renderer_cache; assert(cache->svgc); #ifdef USE_SVG_CAIRO svg_cairo_destroy(cache->svgc); #else rsvg_handle_close(cache->svgc, NULL); #if LIBRSVG_CHECK_VERSION(2,35,0) g_object_unref(cache->svgc); #else rsvg_handle_free(cache->svgc); #endif #endif if(cache->pixmap_buffer) { msFreeRasterBuffer(cache->pixmap_buffer); free(cache->pixmap_buffer); } msFree(s->renderer_cache); #endif }
void xkb_cairo_draw_flag (cairo_t *cr, const gchar *group_name, gint panel_size, gint actual_width, gint actual_height, gint width, gint height, gint variant_markers_count, guint max_variant_markers_count, guint img_scale, guint text_scale, GdkColor fgcolor) { gchar *filename; RsvgHandle *handle; RsvgDimensionData dim; double scalex, scaley; double xx, yy; gint i; double layoutx, layouty, img_width, img_height; double radius, diameter; guint spacing; g_assert (cr != NULL); if (!group_name) return; filename = xkb_util_get_flag_filename (group_name); handle = rsvg_handle_new_from_file (filename, NULL); g_free (filename); if (!handle) { xkb_cairo_draw_label (cr, group_name, panel_size, actual_width, actual_height, width, height, variant_markers_count, text_scale, fgcolor); return; } rsvg_handle_get_dimensions (handle, &dim); scalex = (double) (width - 4) / dim.width; scaley = (double) (height - 4) / dim.height; scalex *= img_scale / 100.0; scaley *= img_scale / 100.0; img_width = dim.width * scalex; img_height = dim.height * scaley; DBG ("scale x/y: %.3f/%.3f, dim w/h: %d/%d, scaled w/h: %.1f/%.1f", scalex, scaley, dim.width, dim.height, scalex*dim.width, scaley*dim.height); layoutx = (actual_width - img_width) / 2; layouty = (actual_height - img_height) / 2; cairo_translate (cr, layoutx, layouty); cairo_save (cr); cairo_scale (cr, scalex, scaley); rsvg_handle_render_cairo (handle, cr); cairo_restore (cr); DBG ("actual width/height: %d/%d; w/h: %d/%d; img w/h: %.1f/%.1f; markers: %d, max markers: %d", actual_width, actual_height, width, height, img_width, img_height, variant_markers_count, max_variant_markers_count); DBG ("layout x/y: %.1f/%.1f", layoutx, layouty); if (variant_markers_count > 0) { diameter = 5.0; spacing = 1; /* check if the flag is too small to draw variant markers inside it */ if ((diameter + spacing) * (max_variant_markers_count-1) > img_width - 2) { /* draw markers below the flag */ diameter = 4; spacing = 0; layoutx = actual_width / 2 + (max_variant_markers_count - 2) * diameter / 2; layouty = (actual_height + img_height) / 2 + diameter + 1; DBG ("small flag"); } else { /* draw markers inside the flag */ spacing = 1; layoutx += img_width - diameter / 2 - 1; layouty += img_height - diameter / 2 - 1; DBG ("large flag"); } radius = diameter / 2.0; if (layouty > actual_height - radius) layouty = actual_height - radius; if (layoutx > actual_width - radius) layoutx = actual_width - radius; } /* draw variant_markers_count circles */ for (i = 0; i < variant_markers_count; i++) { gint x, y; cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); cairo_set_line_width (cr, 1); x = layoutx - (diameter + spacing) * i + 0.5; y = layouty; DBG ("variant center x/y: %d/%d, diameter: %.1f, spacing: %d", x, y, diameter, spacing); xkb_cairo_arc_for_flag (cr, x, y, radius, 0, 2 * G_PI); cairo_set_source_rgb (cr, 0, 0, 0); cairo_fill_preserve (cr); cairo_set_source_rgb (cr, 1, 1, 1); cairo_stroke (cr); } rsvg_handle_close (handle, NULL); g_object_unref (handle); }
static RsvgHandle* gvloadimage_rsvg_load(GVJ_t * job, usershape_t *us) { RsvgHandle* rsvgh = NULL; guchar *fileBuf = NULL; GError *err = NULL; gsize fileSize; gint result; int fd; struct stat stbuf; assert(job); assert(us); assert(us->name); if (us->data) { if (us->datafree == gvloadimage_rsvg_free) rsvgh = (RsvgHandle*)(us->data); /* use cached data */ else { us->datafree(us); /* free incompatible cache data */ us->data = NULL; } } if (!rsvgh) { /* read file into cache */ if (!gvusershape_file_access(us)) return NULL; switch (us->type) { case FT_SVG: rsvg_init(); rsvgh = rsvg_handle_new(); if (rsvgh == NULL) { fprintf(stderr, "rsvg_handle_new_from_file returned an error: %s\n", err->message); rsvg_term(); return NULL; } fd = fileno(us->f); fstat(fd, &stbuf); fileSize = stbuf.st_size; fileBuf = calloc(fileSize + 1, sizeof(guchar)); if (fileBuf == NULL) { rsvg_handle_free(rsvgh); rsvg_term(); return NULL; } rewind(us->f); if ((result = fread(fileBuf, 1, fileSize, us->f)) < fileSize) { rsvg_handle_free(rsvgh); rsvg_term(); return NULL; } if (rsvg_handle_write(rsvgh, (const guchar *)fileBuf, (gsize)fileSize, &err) == FALSE) { fprintf(stderr, "rsvg_handle_write returned an error: %s\n", err->message); free(fileBuf); rsvg_handle_free(rsvgh); rsvg_term(); return NULL; } free(fileBuf); rsvg_handle_close(rsvgh, &err); rsvg_handle_set_dpi(rsvgh, POINTS_PER_INCH); break; default: rsvgh = NULL; } if (rsvgh) { us->data = (void*)rsvgh; us->datafree = gvloadimage_rsvg_free; } gvusershape_file_release(us); } return rsvgh; }
static void gvloadimage_rsvg_free(usershape_t *us) { rsvg_handle_close((RsvgHandle*)us->data, NULL); }
/* This function retrieves the pixel size from an SVG file. Parsing * stops after the first chunk that provided the parser with enough * information to determine the size. This is usally the opening * <svg> element and should thus be in the first chunk (1024 bytes). */ static gboolean load_rsvg_size (const gchar *filename, SvgLoadVals *vals, GError **error) { RsvgHandle *handle; GIOChannel *io; GIOStatus status = G_IO_STATUS_NORMAL; gboolean success = TRUE; gboolean done = FALSE; io = g_io_channel_new_file (filename, "r", error); if (!io) return FALSE; g_io_channel_set_encoding (io, NULL, NULL); handle = rsvg_handle_new (); rsvg_handle_set_dpi (handle, vals->resolution); vals->width = SVG_DEFAULT_SIZE; vals->height = SVG_DEFAULT_SIZE; while (success && status != G_IO_STATUS_EOF && (! done)) { gchar buf[1024]; gsize len; RsvgDimensionData dim = { 0, 0, 0.0, 0.0 }; status = g_io_channel_read_chars (io, buf, sizeof (buf), &len, error); switch (status) { case G_IO_STATUS_ERROR: success = FALSE; break; case G_IO_STATUS_EOF: success = rsvg_handle_close (handle, error); break; case G_IO_STATUS_NORMAL: success = rsvg_handle_write (handle, (const guchar *) buf, len, error); rsvg_handle_get_dimensions (handle, &dim); if (dim.width > 0 && dim.height > 0) { vals->width = dim.width; vals->height = dim.height; done = TRUE; } break; case G_IO_STATUS_AGAIN: break; } } if (size_label) { if (done) { gchar *text = g_strdup_printf (_("%d × %d"), vals->width, vals->height); gtk_label_set_text (GTK_LABEL (size_label), text); g_free (text); } else { gtk_label_set_text (GTK_LABEL (size_label), _("SVG file does not\nspecify a size!")); } } g_io_channel_unref (io); g_object_unref (handle); if (vals->width < 1) vals->width = 1; if (vals->height < 1) vals->height = 1; return success; }
static bool rsvg_convert(struct content *c) { rsvg_content *d = (rsvg_content *) c; RsvgDimensionData rsvgsize; GError *err = NULL; if (rsvg_handle_close(d->rsvgh, &err) == FALSE) { NSLOG(netsurf, INFO, "rsvg_handle_close returned an error: %s", err->message); content_broadcast_errorcode(c, NSERROR_SVG_ERROR); return false; } assert(err == NULL); /* we should now be able to query librsvg for the natural size of the * graphic, so we can create our bitmap. */ rsvg_handle_get_dimensions(d->rsvgh, &rsvgsize); c->width = rsvgsize.width; c->height = rsvgsize.height; if ((d->bitmap = guit->bitmap->create(c->width, c->height, BITMAP_NEW)) == NULL) { NSLOG(netsurf, INFO, "Failed to create bitmap for rsvg render."); content_broadcast_errorcode(c, NSERROR_NOMEM); return false; } if ((d->cs = cairo_image_surface_create_for_data( (unsigned char *)guit->bitmap->get_buffer(d->bitmap), CAIRO_FORMAT_ARGB32, c->width, c->height, guit->bitmap->get_rowstride(d->bitmap))) == NULL) { NSLOG(netsurf, INFO, "Failed to create Cairo image surface for rsvg render."); content_broadcast_errorcode(c, NSERROR_NOMEM); return false; } if ((d->ct = cairo_create(d->cs)) == NULL) { NSLOG(netsurf, INFO, "Failed to create Cairo drawing context for rsvg render."); content_broadcast_errorcode(c, NSERROR_NOMEM); return false; } rsvg_handle_render_cairo(d->rsvgh, d->ct); rsvg_argb_to_abgr(guit->bitmap->get_buffer(d->bitmap), c->width, c->height, guit->bitmap->get_rowstride(d->bitmap)); guit->bitmap->modified(d->bitmap); content_set_ready(c); content_set_done(c); /* Done: update status bar */ content_set_status(c, ""); return true; }
static bool rsvg_convert(struct content *c) { rsvg_content *d = (rsvg_content *) c; union content_msg_data msg_data; RsvgDimensionData rsvgsize; GError *err = NULL; if (rsvg_handle_close(d->rsvgh, &err) == FALSE) { LOG(("rsvg_handle_close returned an error: %s", err->message)); msg_data.error = err->message; content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } assert(err == NULL); /* we should now be able to query librsvg for the natural size of the * graphic, so we can create our bitmap. */ rsvg_handle_get_dimensions(d->rsvgh, &rsvgsize); c->width = rsvgsize.width; c->height = rsvgsize.height; if ((d->bitmap = bitmap_create(c->width, c->height, BITMAP_NEW)) == NULL) { LOG(("Failed to create bitmap for rsvg render.")); msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } if ((d->cs = cairo_image_surface_create_for_data( (unsigned char *)bitmap_get_buffer(d->bitmap), CAIRO_FORMAT_ARGB32, c->width, c->height, bitmap_get_rowstride(d->bitmap))) == NULL) { LOG(("Failed to create Cairo image surface for rsvg render.")); msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } if ((d->ct = cairo_create(d->cs)) == NULL) { LOG(("Failed to create Cairo drawing context for rsvg render.")); msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } rsvg_handle_render_cairo(d->rsvgh, d->ct); rsvg_argb_to_abgr(bitmap_get_buffer(d->bitmap), c->width, c->height, bitmap_get_rowstride(d->bitmap)); bitmap_modified(d->bitmap); content_set_ready(c); content_set_done(c); /* Done: update status bar */ content_set_status(c, ""); return true; }