/* 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 void vips_foreign_load_svg_parse( VipsForeignLoadSvg *svg, VipsImage *out ) { RsvgDimensionData dimensions; int width; int height; double scale; double res; /* Calculate dimensions at default dpi/scale. */ rsvg_handle_set_dpi( svg->page, 72.0 ); rsvg_handle_get_dimensions( svg->page, &dimensions ); width = dimensions.width; height = dimensions.height; /* Calculate dimensions at required dpi/scale. */ scale = svg->scale * svg->dpi / 72.0; if( scale != 1.0 ) { rsvg_handle_set_dpi( svg->page, svg->dpi * svg->scale ); rsvg_handle_get_dimensions( svg->page, &dimensions ); if( width == dimensions.width && height == dimensions.height ) { /* SVG without width and height always reports the same * dimensions regardless of dpi. Apply dpi/scale using * cairo instead. */ svg->cairo_scale = scale; width = width * scale; height = height * scale; } else { /* SVG with width and height reports correctly scaled * dimensions. */ width = dimensions.width; height = dimensions.height; } } /* We need pixels/mm for vips. */ res = svg->dpi / 25.4; vips_image_init_fields( out, width, height, 4, VIPS_FORMAT_UCHAR, VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, res, res ); /* We render to a linecache, so fat strips work well. */ vips_image_pipelinev( out, VIPS_DEMAND_STYLE_FATSTRIP, NULL ); }
JNIEXPORT void JNICALL Java_org_gnome_rsvg_RsvgHandle_rsvg_1handle_1set_1dpi ( JNIEnv* env, jclass cls, jlong _self, jdouble _dpi ) { RsvgHandle* self; double dpi; // convert parameter self self = (RsvgHandle*) _self; // convert parameter dpi dpi = (double) _dpi; // call function rsvg_handle_set_dpi(self, dpi); // cleanup parameter self // cleanup parameter dpi }
int svg2pdf(const char *svg_filename, const char *pdf_filename) { RsvgHandle *svg_handle; RsvgDimensionData dimension_data; cairo_surface_t *surface; cairo_t *cr; rsvg_set_default_dpi(72.0); svg_handle = rsvg_handle_new_from_file(svg_filename, NULL); if(svg_handle == NULL) { return 0; } rsvg_handle_set_dpi(svg_handle, 72.0); rsvg_handle_get_dimensions(svg_handle, &dimension_data); surface = cairo_pdf_surface_create(pdf_filename, dimension_data.width, dimension_data.height); if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { return 0; } cr = cairo_create(surface); if(cairo_status(cr) != CAIRO_STATUS_SUCCESS) { return 0; } if(!rsvg_handle_render_cairo(svg_handle, cr)) { return 0; } cairo_show_page(cr); cairo_surface_destroy(surface); cairo_destroy(cr); g_object_unref(svg_handle); return 1; }
static VALUE rb_rsvg_handle_set_dpi(VALUE self, VALUE dpi) { #ifdef HAVE_RSVG_HANDLE_SET_DPI rsvg_handle_set_dpi(_SELF(self), NUM2DBL(dpi)); #else rb_warning("rsvg_handle_set_dpi isn't supported in your librsvg"); #endif return self; }
static RsvgHandle * load_rsvg_handle_new (gdouble xres, gdouble yres) { RsvgHandle *handle = rsvg_handle_new (); #if (((LIBRSVG_MAJOR_VERSION == 2) && (LIBRSVG_MINOR_VERSION == 13) && \ ((LIBRSVG_MICRO_VERSION == 0) || \ (LIBRSVG_MICRO_VERSION == 1) || \ (LIBRSVG_MICRO_VERSION == 2))) || \ ((LIBRSVG_MAJOR_VERSION == 2) && (LIBRSVG_MINOR_VERSION == 11) && \ (LIBRSVG_MICRO_VERSION == 0))) rsvg_handle_set_dpi (handle, xres, yres); #else rsvg_handle_set_dpi_x_y (handle, xres, yres); #endif return 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; }
/* 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; }