/* Load an SVG file and resize it to given dimensions. if width or height is set to 0 no resizing is applied (partly based on TuxPaint's SVG loading function) */ SDL_Surface* LoadSVGOfDimensions(char* filename, int width, int height) { cairo_surface_t* temp_surf; cairo_t* context; RsvgHandle* file_handle; RsvgDimensionData dimensions; SDL_Surface* dest; float scale_x; float scale_y; int bpp = 32; Uint32 Rmask, Gmask, Bmask, Amask; DEBUGCODE{ fprintf(stderr, "LoadSVGOfDimensions(): looking for %s\n", filename); } rsvg_init(); file_handle = rsvg_handle_new_from_file(filename, NULL); if(file_handle == NULL) { DEBUGCODE{ fprintf(stderr, "LoadSVGOfDimensions(): file %s not found\n", filename); } rsvg_term(); return NULL; }
static const char * _rsvg_render_page (const char *filename, cairo_surface_t **surface_out) { RsvgHandle *handle; RsvgDimensionData dimensions; GError *error = NULL; cairo_surface_t *surface; cairo_t *cr; cairo_status_t status; handle = rsvg_handle_new_from_file (filename, &error); if (handle == NULL) return error->message; /* XXX g_error_free */ rsvg_handle_get_dimensions (handle, &dimensions); surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, dimensions.width, dimensions.height); cr = cairo_create (surface); rsvg_handle_render_cairo (handle, cr); g_object_unref (handle); status = cairo_status (cr); cairo_destroy (cr); if (status) { cairo_surface_destroy (surface); return cairo_status_to_string (status); } *surface_out = surface; return NULL; }
int svg_to_png(const char* file, const char* dest) { if (!gdk_init_check(NULL, NULL)) { g_warning("Init gdk environment failed"); return -1; } GError* error = NULL; RsvgHandle* handler = rsvg_handle_new_from_file(file, &error); if (error) { g_warning("New RsvgHandle failed: %s", error->message); g_error_free(error); return -1; } GdkPixbuf* pbuf = rsvg_handle_get_pixbuf(handler); g_object_unref(G_OBJECT(handler)); error = NULL; gdk_pixbuf_save(pbuf, dest, "png", &error, NULL); g_object_unref(G_OBJECT(pbuf)); if (error) { g_warning("Save to png file failed: %s", error->message); g_error_free(error); return -1; } return 0; }
static RsvgHandle * _cairo_get_svg (CairoRenderer *renderer, const char *file) { RsvgHandle *svg; GError *error = NULL; svg = g_hash_table_lookup (renderer->svgs, file); if (svg) return svg; svg = rsvg_handle_new_from_file (file, &error); if (svg == NULL) { if (error) { g_warning ("could not load file %s: %s", file, error->message); g_clear_error (&error); } return NULL; } g_hash_table_insert (renderer->svgs, (char *) file, svg); return svg; }
static cairo_surface_t * fill_image_buffer_from_file (cairo_t *cr, const char *filePath) { GError *error = NULL; RsvgHandle *handle; cairo_surface_t *tmp_surface; cairo_t *tmp_cr; handle = rsvg_handle_new_from_file (filePath, &error); if (handle == NULL) { g_warning("rsvg_handle_new_from_file(\"%s\") failed: %s", filePath, (error ? error->message : "unknown error")); if (error) g_error_free(error); return NULL; } tmp_surface = cairo_surface_create_similar (cairo_get_target (cr), CAIRO_CONTENT_COLOR_ALPHA, 32, 32); tmp_cr = cairo_create (tmp_surface); rsvg_handle_render_cairo (handle, tmp_cr); cairo_destroy (tmp_cr); rsvg_handle_free (handle); return tmp_surface; }
static gboolean get_image_size (const char *filename, int *width, int *height) { RsvgHandle *handle; RsvgDimensionData dimensions; GError* error = NULL; if (filename == NULL) return FALSE; handle = rsvg_handle_new_from_file (filename, &error); if (error != NULL) { g_printerr ("%s\n", error->message); g_error_free (error); } if (handle == NULL) return FALSE; /* Compute image size */ rsvg_handle_get_dimensions (handle, &dimensions); g_object_unref (handle); if (dimensions.width == 0 || dimensions.height == 0) return FALSE; if (width) *width = dimensions.width; if (height) *height = dimensions.height; return TRUE; }
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; }
int msPreloadSVGSymbol(symbolObj *symbol) { #if defined(USE_SVG_CAIRO) || defined(USE_RSVG) struct svg_symbol_cache *cache; if(!symbol->renderer_cache) { cache = msSmallCalloc(1,sizeof(struct svg_symbol_cache)); symbol->renderer_free_func = &freeSVGCache; } else { cache = symbol->renderer_cache; } if(cache->svgc) return MS_SUCCESS; #ifdef USE_SVG_CAIRO { unsigned int svg_width, svg_height; int status; status = svg_cairo_create(&cache->svgc); if (status) { msSetError(MS_RENDERERERR, "problem creating cairo svg", "msPreloadSVGSymbol()"); return MS_FAILURE; } status = svg_cairo_parse(cache->svgc, symbol->full_pixmap_path); if (status) { msSetError(MS_RENDERERERR, "problem parsing svg symbol", "msPreloadSVGSymbol()"); return MS_FAILURE; } svg_cairo_get_size (cache->svgc, &svg_width, &svg_height); if (svg_width == 0 || svg_height == 0) { msSetError(MS_RENDERERERR, "problem parsing svg symbol", "msPreloadSVGSymbol()"); return MS_FAILURE; } symbol->sizex = svg_width; symbol->sizey = svg_height; } #else { RsvgDimensionData dim; cache->svgc = rsvg_handle_new_from_file(symbol->full_pixmap_path,NULL); if(!cache->svgc) { msSetError(MS_RENDERERERR,"failed to load svg file %s", "msPreloadSVGSymbol()", symbol->full_pixmap_path); } rsvg_handle_get_dimensions_sub (cache->svgc, &dim, NULL); symbol->sizex = dim.width; symbol->sizey = dim.height; } #endif symbol->renderer_cache = cache; return MS_SUCCESS; #else msSetError(MS_MISCERR, "SVG Symbols requested but is not built with libsvgcairo", "msPreloadSVGSymbol()"); return MS_FAILURE; #endif }
JobFileSvg::JobFileSvg(const char* filename, double scale): JobFile(filename,scale), m_pCairo(NULL), m_cairoMutex(), m_pSurface(NULL), m_pRsvgHandle(NULL) { m_cairoMutex.lock(); float dpi=254.0; GError *error = NULL; RsvgDimensionData dimensions; g_type_init(); rsvg_set_default_dpi_x_y (dpi,dpi);//no reaction? m_pRsvgHandle = rsvg_handle_new_from_file (filename, &error); if( m_pRsvgHandle != NULL ){ rsvg_handle_get_dimensions (m_pRsvgHandle, &dimensions); //m_position.x = (1024-dimensions.width)/2; //m_position.y = (768-dimensions.height)/2; m_pSurface = (cairo_surface_t *)cairo_image_surface_create( CAIRO_FORMAT_ARGB32, m_scale*dimensions.width, m_scale*dimensions.height //1024, 768 ); m_pCairo = cairo_create(m_pSurface); cairo_scale( m_pCairo, m_scale, m_scale); m_size.width = m_scale*dimensions.width; m_size.height = m_scale*dimensions.height; m_position.x = (1024- m_size.width)/2; m_position.y = (768- m_size.height)/2; /* Check existens of layers "#layer0","#layer1",... * to get number of layers and the guarantee that * no inner layer is missed. * */ m_nmbrOfLayers = 0; while( true ){ std::ostringstream layerid; layerid << "#layer" << m_nmbrOfLayers; if( !rsvg_handle_has_sub( m_pRsvgHandle, layerid.str().c_str() )){ break; } m_nmbrOfLayers++; //std::cout << "Found layer " << layerid.str() << std::endl; } std::cout << "Found layers: " << m_nmbrOfLayers << std::endl; m_maxLayer = m_nmbrOfLayers-1; m_cairoMutex.unlock(); }else{ std::cout << "Error while loading file '" << filename << "'." << std::endl; m_cairoMutex.unlock(); throw JOB_LOAD_EXCEPTION; } }
ImagesStorage::ImagesStorage() { GError* error = 0; RsvgHandle* rsvgCards = rsvg_handle_new_from_file("PreferansData/svg-cards-2.0.svg", &error); loadFrenchDeck(rsvgCards); rsvg_handle_free(rsvgCards); }
HippoCanvasThemeImage * hippo_canvas_theme_image_new (const char *filename, int border_top, int border_right, int border_bottom, int border_left, GError **error) { HippoCanvasThemeImage *image; g_return_val_if_fail(error == NULL || *error == NULL, NULL); image = g_object_new(HIPPO_TYPE_CANVAS_THEME_IMAGE, NULL); #ifdef HAVE_LIBRSVG if (g_str_has_suffix(filename, ".svg") || g_str_has_suffix(filename, ".SVG")) { image->type = THEME_IMAGE_SVG; image->u.svg = rsvg_handle_new_from_file(filename, error); if (image->u.svg == NULL) { /* librsvg does a horrible job setting error, it's probably NULL */ if (*error == NULL) g_set_error(error, HIPPO_CANVAS_THEME_IMAGE_ERROR, HIPPO_CANVAS_THEME_IMAGE_ERROR_FAILED, "Failed to read SVG image '%s'", filename); goto fail; } } else #endif if (g_str_has_suffix(filename, ".png") || g_str_has_suffix(filename, ".PNG")) { image->type = THEME_IMAGE_SURFACE; image->u.surface = cairo_image_surface_create_from_png(filename); if (image->u.surface == NULL) { g_set_error(error, HIPPO_CANVAS_THEME_IMAGE_ERROR, HIPPO_CANVAS_THEME_IMAGE_ERROR_FAILED, "Failed to read PNG image '%s'", filename); goto fail; } } else { g_set_error(error, HIPPO_CANVAS_THEME_IMAGE_ERROR, HIPPO_CANVAS_THEME_IMAGE_ERROR_FAILED, "Unknown filetype for image '%s'", filename); goto fail; } image->border_top = border_top; image->border_right = border_right; image->border_bottom = border_bottom; image->border_left = border_left; return image; fail: g_object_unref(image); return NULL; }
static void size_allocate_callback(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data) { dt_iop_module_t *self = (dt_iop_module_t *)user_data; dt_iop_zonesystem_gui_data_t *g = (dt_iop_zonesystem_gui_data_t *)self->gui_data; if(g->image) cairo_surface_destroy(g->image); free(g->image_buffer); /* load the dt logo as a brackground */ char filename[PATH_MAX] = { 0 }; char datadir[PATH_MAX] = { 0 }; char *logo; dt_logo_season_t season = get_logo_season(); if(season != DT_LOGO_SEASON_NONE) logo = g_strdup_printf("%%s/pixmaps/idbutton-%d.svg", (int)season); else logo = g_strdup("%s/pixmaps/idbutton.svg"); dt_loc_get_datadir(datadir, sizeof(datadir)); snprintf(filename, sizeof(filename), logo, datadir); g_free(logo); RsvgHandle *svg = rsvg_handle_new_from_file(filename, NULL); if(svg) { cairo_surface_t *surface; cairo_t *cr; RsvgDimensionData dimension; rsvg_handle_get_dimensions(svg, &dimension); float svg_size = MAX(dimension.width, dimension.height); float final_size = MIN(allocation->width, allocation->height) * 0.75; float factor = final_size / svg_size; float final_width = dimension.width * factor * darktable.gui->ppd, final_height = dimension.height * factor * darktable.gui->ppd; int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, final_width); g->image_buffer = (guint8 *)calloc(stride * final_height, sizeof(guint8)); surface = dt_cairo_image_surface_create_for_data(g->image_buffer, CAIRO_FORMAT_ARGB32, final_width, final_height, stride); if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { free(g->image_buffer); g->image_buffer = NULL; } else { cr = cairo_create(surface); cairo_scale(cr, factor, factor); rsvg_handle_render_cairo(svg, cr); cairo_destroy(cr); cairo_surface_flush(surface); g->image = surface; g->image_width = final_width / darktable.gui->ppd; g->image_height = final_height / darktable.gui->ppd; } g_object_unref(svg); } }
static Bool readSvgFileToImage(char *file, int *width, int *height, void **data) { cairo_surface_t *surface; FILE *fp; GError *error = NULL; RsvgHandle *svgHandle; RsvgDimensionData svgDimension; fp = fopen(file, "r"); if (!fp) return FALSE; fclose(fp); svgHandle = rsvg_handle_new_from_file(file, &error); if (!svgHandle) return FALSE; rsvg_handle_get_dimensions(svgHandle, &svgDimension); *width = svgDimension.width; *height = svgDimension.height; *data = malloc(svgDimension.width * svgDimension.height * 4); if (!*data) { rsvg_handle_free(svgHandle); return FALSE; } surface = cairo_image_surface_create_for_data(*data, CAIRO_FORMAT_ARGB32, svgDimension.width, svgDimension.height, svgDimension.width * 4); if (surface) { cairo_t *cr; cr = cairo_create(surface); cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); cairo_paint(cr); cairo_set_operator(cr, CAIRO_OPERATOR_OVER); rsvg_handle_render_cairo(svgHandle, cr); cairo_destroy(cr); cairo_surface_destroy(surface); } rsvg_handle_free(svgHandle); return TRUE; }
int main (int argc, char **argv) { RsvgDimensionData dimensions; RsvgHandle *logo_handle; cairo_surface_t *surface; GError *error = NULL; cairo_t *cr; cairo_status_t status; char *input, *size, *output; GString *layer; char *layer_name; int h, w; g_type_init (); input = argv[1]; size = argv[2]; layer = g_string_new (argv[3]); g_string_ascii_down (layer); g_string_prepend_c (layer, '#'); output = argv[4]; if (sscanf (size, "%dx%d", &w, &h) != 2) { g_warning ("Couldn't parse size '%s'", size); return 1; } logo_handle = rsvg_handle_new_from_file (input, &error); if (!logo_handle) { g_warning ("Couldn't open '%s': %s", input, error->message); g_error_free (error); return 1; } surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h); cr = cairo_create (surface); rsvg_handle_get_dimensions (logo_handle, &dimensions); cairo_scale (cr, (double) w / dimensions.width, (double) h / dimensions.height); layer_name = g_string_free (layer, FALSE); rsvg_handle_render_cairo_sub (logo_handle, cr, "#background"); // rsvg_handle_render_cairo_sub (logo_handle, cr, "#base"); rsvg_handle_render_cairo_sub (logo_handle, cr, layer_name); status = cairo_surface_write_to_png (surface, output); if (status != CAIRO_STATUS_SUCCESS) { g_warning ("Couldn't write output '%s': %s", output, cairo_status_to_string (status)); return 1; } g_free (layer_name); cairo_destroy (cr); return 0; }
bool SvgScreen::readSvgToImage (const char *file, CompSize &size, void *&data) { cairo_surface_t *surface; std::ifstream svgFile; GError *error = NULL; RsvgHandle *svgHandle; RsvgDimensionData svgDimension; svgFile.open (file); if (!svgFile.is_open ()) return false; svgFile.close (); svgHandle = rsvg_handle_new_from_file (file, &error); if (!svgHandle) return false; rsvg_handle_get_dimensions (svgHandle, &svgDimension); size.setWidth (svgDimension.width); size.setHeight (svgDimension.height); data = malloc (svgDimension.width * svgDimension.height * 4); if (!data) { rsvg_handle_free (svgHandle); return false; } surface = cairo_image_surface_create_for_data ((unsigned char *) data, CAIRO_FORMAT_ARGB32, svgDimension.width, svgDimension.height, svgDimension.width * 4); if (surface) { cairo_t *cr; cr = cairo_create (surface); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); rsvg_handle_render_cairo (svgHandle, cr); cairo_destroy (cr); cairo_surface_destroy (surface); } rsvg_handle_free (svgHandle); return true; }
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); } }
static RsvgHandle* load_svg(const char *path) { GError *err = NULL; RsvgHandle *image = rsvg_handle_new_from_file(path, &err); if (err != NULL) { g_log("sails", G_LOG_LEVEL_ERROR, "can't open file \"%s\": %s", path, err->message); } else { g_message("loaded image \"%s\"", path); } return image; }
SVG::SVG(const UTF8String& sFilename, bool bUnescapeIllustratorIDs) : m_sFilename(sFilename), m_bUnescapeIllustratorIDs(bUnescapeIllustratorIDs) { GError* pErr = 0; m_pRSVG = rsvg_handle_new_from_file(m_sFilename.c_str(), &pErr); if (!m_pRSVG) { throw Exception(AVG_ERR_INVALID_ARGS, string("Could not open svg file: ") + m_sFilename); g_error_free(pErr); } }
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; }
static VALUE rb_rsvg_handle_new_from_file(VALUE self, VALUE file) { GError *error = NULL; RsvgHandle *handle; handle = rsvg_handle_new_from_file((const gchar *)RVAL2CSTR(file), &error); if (error) RAISE_GERROR(error); return GOBJ2RVAL(handle); }
SVG::SVG(const UTF8String& sFilename, bool bUnescapeIllustratorIDs) : m_sFilename(sFilename), m_bUnescapeIllustratorIDs(bUnescapeIllustratorIDs) { if (!s_RSVGInitialized) { rsvg_init(); s_RSVGInitialized = true; } GError* pErr = new GError; m_pRSVG = rsvg_handle_new_from_file(m_sFilename.c_str(), &pErr); if (!m_pRSVG) { throw Exception(AVG_ERR_INVALID_ARGS, string("Could not open svg file: ") + m_sFilename); } delete pErr; }
static void gst_rsvg_overlay_set_svg_data (GstRsvgOverlay * overlay, const gchar * data, gboolean consider_as_filename) { GstBaseTransform *btrans = GST_BASE_TRANSFORM (overlay); gsize size; GError *error = NULL; if (overlay->handle) { g_object_unref (overlay->handle); overlay->handle = NULL; gst_base_transform_set_passthrough (btrans, TRUE); } /* data may be NULL */ if (data) { size = strlen (data); if (size) { /* Read data either from string or from file */ if (consider_as_filename) overlay->handle = rsvg_handle_new_from_file (data, &error); else overlay->handle = rsvg_handle_new_from_data ((guint8 *) data, size, &error); if (error || overlay->handle == NULL) { if (error) { GST_ERROR_OBJECT (overlay, "Cannot read SVG data: %s\n%s", error->message, data); g_error_free (error); } else { GST_ERROR_OBJECT (overlay, "Cannot read SVG data: %s", data); } } else { /* Get SVG dimension. */ RsvgDimensionData svg_dimension; rsvg_handle_get_dimensions (overlay->handle, &svg_dimension); overlay->svg_width = svg_dimension.width; overlay->svg_height = svg_dimension.height; gst_base_transform_set_passthrough (btrans, FALSE); GST_INFO_OBJECT (overlay, "updated SVG, %d x %d", overlay->svg_width, overlay->svg_height); } } } }
int main (int argc, char *argv[]) { GError *error = NULL; RsvgHandle *handle; RsvgDimensionData dim; double width, height; const char *filename = argv[1]; const char *output_filename = argv[2]; cairo_surface_t *surface; cairo_t *cr; cairo_status_t status; if (argc != 3) FAIL ("usage: svg2pdf input_file.svg output_file.pdf"); g_type_init (); rsvg_set_default_dpi (72.0); handle = rsvg_handle_new_from_file (filename, &error); if (error != NULL) FAIL (error->message); rsvg_handle_get_dimensions (handle, &dim); width = dim.width; height = dim.height; surface = cairo_pdf_surface_create (output_filename, width, height); cr = cairo_create (surface); /* Clear background */ cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */ cairo_paint (cr); rsvg_handle_render_cairo (handle, cr); status = cairo_status (cr); if (status) FAIL (cairo_status_to_string (status)); cairo_destroy (cr); cairo_surface_destroy (surface); return 0; }
JNIEXPORT jlong JNICALL Java_org_gnome_rsvg_RsvgHandle_rsvg_1handle_1new_1from_1file ( JNIEnv* env, jclass cls, jstring _fileName ) { RsvgHandle* result; jlong _result; const gchar* fileName; GError* error = NULL; // convert parameter fileName fileName = (const gchar*) bindings_java_getString(env, _fileName); if (fileName == NULL) { return 0L; // Java Exception already thrown } // call function result = rsvg_handle_new_from_file(fileName, &error); // cleanup parameter fileName bindings_java_releaseString(fileName); // check for error if (error) { bindings_java_throwGlibException(env, error); return 0L; } // translate return value to JNI type _result = (jlong) result; // cleanup return value if (result != NULL) { bindings_java_memory_cleanup((GObject*)result, TRUE); } // and finally return _result; }
int main (int argc, char** argv) { cairo_t* cr3; cairo_surface_t* image; RsvgHandle* r_svg; RsvgError rs_err; RsvgDimensionData svgdim; cairo_matrix_t matrix; rsvg_init(); r_svg = rsvg_handle_new_from_file(argv[1], NULL); rsvg_handle_get_dimensions(r_svg, &svgdim); int width = svgdim.width; int height = svgdim.height; unsigned char* buf = (unsigned char*)malloc(width * height * 4); image = cairo_image_surface_create_for_data(buf, CAIRO_FORMAT_ARGB32, width, height, width * 4); cr3 = cairo_create(image); cairo_set_source_rgb(cr3, 1.0, 0.0, 0.0); cairo_paint(cr3); rsvg_handle_render_cairo(r_svg, cr3); cairo_surface_write_to_png(image, "output.png"); rsvg_handle_free(r_svg); rsvg_term(); cairo_destroy(cr3); cairo_surface_destroy(image); free(buf); return 0; }
void cairo_context::render_svg(const std::string& fname) { GError *error = NULL; static std::map<std::string, RsvgHandle*> cache; RsvgHandle* handle = NULL; auto itor = cache.find(fname); if(itor == cache.end()) { std::string real_fname = module::map_file(fname); ASSERT_LOG(sys::file_exists(real_fname), "Could not find svg file: " << fname); handle = rsvg_handle_new_from_file(real_fname.c_str(), &error); ASSERT_LOG(error == NULL, "SVG rendering error: " << error->message); } else { handle = itor->second; } rsvg_handle_render_cairo(handle, cairo_); cairo_status_t status = cairo_status(cairo_); ASSERT_LOG(status == 0, "SVG rendering error: " << cairo_status_to_string(status)); }
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); }
void cd_clock_load_theme (CairoDockModuleInstance *myApplet) { cd_message ("%s (%s)", __func__, myConfig.cThemePath); //\_______________ On charge le theme choisi (on n'a pas besoin de connaitre les dimmensions de l'icone). if (myConfig.cThemePath != NULL) { GString *sElementPath = g_string_new (""); int i; for (i = 0; i < CLOCK_ELEMENTS; i ++) { g_string_printf (sElementPath, "%s/%s", myConfig.cThemePath, s_cFileNames[i]); myData.pSvgHandles[i] = rsvg_handle_new_from_file (sElementPath->str, NULL); } i = 0; while (i < CLOCK_FRAME && myData.pSvgHandles[i] == NULL) { i ++; if (i == CLOCK_HOUR_HAND_SHADOW) i = CLOCK_FACE_SHADOW; } if (i != CLOCK_FRAME) rsvg_handle_get_dimensions (myData.pSvgHandles[i], &myData.DimensionData); if (myData.pSvgHandles[CLOCK_HOUR_HAND] != NULL) rsvg_handle_get_dimensions (myData.pSvgHandles[CLOCK_HOUR_HAND], &myData.needleDimension); cd_debug ("clock bg dimension : %dx%d", (int) myData.DimensionData.width, (int) myData.DimensionData.height); cd_debug ("clock needle dimension : %dx%d", (int) myData.needleDimension.width, (int) myData.needleDimension.height); // recuperation des parametres des aiguilles. g_string_printf (sElementPath, "%s/%s", myConfig.cThemePath, "theme.conf"); GKeyFile *pKeyFile = cairo_dock_open_key_file (sElementPath->str); if (pKeyFile != NULL) { GError *erreur = NULL; myData.iNeedleRealHeight = g_key_file_get_integer (pKeyFile, "Needle", "height", &erreur); if (erreur != NULL) { cd_warning (erreur->message); g_error_free (erreur); erreur = NULL; } myData.iNeedleOffsetX = g_key_file_get_double (pKeyFile, "Needle", "offset x", &erreur); if (erreur != NULL) { cd_warning (erreur->message); g_error_free (erreur); erreur = NULL; } g_key_file_free (pKeyFile); } else // on prend des valeurs par defaut assez larges. { g_print ("clock : default needle size\n"); myData.iNeedleRealHeight = .5 * myData.needleDimension.height; myData.iNeedleOffsetX = .5 * myData.needleDimension.width; } myData.iNeedleRealWidth = myData.needleDimension.width/2 + myData.iNeedleOffsetX; myData.iNeedleOffsetY = .5 * myData.iNeedleRealHeight; cd_debug ("clock needle : H=%d; dx=%d\n", myData.iNeedleRealHeight, myData.iNeedleOffsetX); g_string_free (sElementPath, TRUE); } else { myData.DimensionData.width = 48; // valeurs par defaut si aucun theme trouve. myData.DimensionData.height = 48; myData.needleDimension.width = 48; myData.needleDimension.height = 48; } }
void gui_init(dt_lib_module_t *self) { char filename[PATH_MAX] = { 0 }; char datadir[PATH_MAX] = { 0 }; /* initialize ui widgets */ dt_lib_darktable_t *d = (dt_lib_darktable_t *)g_malloc0(sizeof(dt_lib_darktable_t)); self->data = (void *)d; /* create drawing area */ self->widget = gtk_event_box_new(); /* connect callbacks */ g_signal_connect(G_OBJECT(self->widget), "draw", G_CALLBACK(_lib_darktable_draw_callback), self); g_signal_connect(G_OBJECT(self->widget), "button-press-event", G_CALLBACK(_lib_darktable_button_press_callback), self); /* create a cairo surface of dt icon */ char *logo; dt_logo_season_t season = get_logo_season(); if(season != DT_LOGO_SEASON_NONE) logo = g_strdup_printf("%%s/pixmaps/idbutton-%d.%%s", (int)season); else logo = g_strdup("%s/pixmaps/idbutton.%s"); dt_loc_get_datadir(datadir, sizeof(datadir)); snprintf(filename, sizeof(filename), logo, datadir, "svg"); // first we try the SVG { GError *error = NULL; RsvgHandle *svg = rsvg_handle_new_from_file(filename, &error); if(!svg || error) { fprintf(stderr, "warning: can't load darktable logo from SVG file `%s', falling back to PNG version\n%s\n", filename, error->message); g_error_free(error); error = NULL; goto png_fallback; } cairo_surface_t *surface; cairo_t *cr; RsvgDimensionData dimension; rsvg_handle_get_dimensions(svg, &dimension); int width = DT_PIXEL_APPLY_DPI(dimension.width) * darktable.gui->ppd, height = DT_PIXEL_APPLY_DPI(dimension.height) * darktable.gui->ppd; int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); d->image_buffer = (guint8 *)calloc(stride * height, sizeof(guint8)); surface = dt_cairo_image_surface_create_for_data(d->image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride); if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { free(d->image_buffer); d->image_buffer = NULL; g_object_unref(svg); fprintf(stderr, "warning: can't load darktable logo from SVG file `%s', falling back to PNG version\n", filename); goto png_fallback; } cr = cairo_create(surface); cairo_scale(cr, darktable.gui->dpi_factor, darktable.gui->dpi_factor); rsvg_handle_render_cairo(svg, cr); cairo_surface_flush(surface); d->image = surface; g_object_unref(svg); } goto done; png_fallback: // let's fall back to the PNG { cairo_surface_t *surface; cairo_t *cr; snprintf(filename, sizeof(filename), logo, datadir, "png"); surface = cairo_image_surface_create_from_png(filename); if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "warning: can't load darktable logo from PNG file `%s'\n", filename); d->image = NULL; goto done; } int png_width = cairo_image_surface_get_width(surface), png_height = cairo_image_surface_get_height(surface); // blow up the PNG. Ugly, but at least it has the correct size afterwards :-/ int width = DT_PIXEL_APPLY_DPI(png_width) * darktable.gui->ppd, height = DT_PIXEL_APPLY_DPI(png_height) * darktable.gui->ppd; int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); d->image_buffer = (guint8 *)calloc(stride * height, sizeof(guint8)); d->image = dt_cairo_image_surface_create_for_data(d->image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride); if(cairo_surface_status(d->image) != CAIRO_STATUS_SUCCESS) { free(d->image_buffer); d->image_buffer = NULL; cairo_surface_destroy(surface); fprintf(stderr, "warning: can't load darktable logo from PNG file `%s'\n", filename); d->image = NULL; goto done; } cr = cairo_create(d->image); cairo_rectangle(cr, 0, 0, width, height); cairo_scale(cr, darktable.gui->dpi_factor, darktable.gui->dpi_factor); cairo_set_source_surface(cr, surface, 0, 0); cairo_fill(cr); cairo_surface_flush(d->image); cairo_surface_destroy(surface); } done: g_free(logo); d->image_width = d->image ? dt_cairo_image_surface_get_width(d->image) : 0; d->image_height = d->image ? dt_cairo_image_surface_get_height(d->image) : 0; /* set size of drawing area */ gtk_widget_set_size_request(self->widget, d->image_width + (int)DT_PIXEL_APPLY_DPI(180), d->image_height + (int)DT_PIXEL_APPLY_DPI(8)); }
// maybe this should be (partly) in common/imageio.[c|h]? static void _lib_import_update_preview(GtkFileChooser *file_chooser, gpointer data) { GtkWidget *preview; char *filename; GdkPixbuf *pixbuf = NULL; gboolean have_preview = FALSE, no_preview_fallback = FALSE; preview = GTK_WIDGET(data); filename = gtk_file_chooser_get_preview_filename(file_chooser); if(!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { no_preview_fallback = TRUE; } else { // don't create dng thumbnails to avoid crashes in libtiff when these are hdr: char *c = filename + strlen(filename); while(c > filename && *c != '.') c--; if(!strcasecmp(c, ".dng")) no_preview_fallback = TRUE; } // unfortunately we can not use following, because frequently it uses wrong orientation // pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL); have_preview = (pixbuf != NULL); if(!have_preview && !no_preview_fallback) { uint8_t *buffer = NULL; size_t size; char *mime_type = NULL; if(!dt_exif_get_thumbnail(filename, &buffer, &size, &mime_type)) { // Scale the image to the correct size GdkPixbuf *tmp; GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); if (!gdk_pixbuf_loader_write(loader, buffer, size, NULL)) goto cleanup; if (!(tmp = gdk_pixbuf_loader_get_pixbuf(loader))) goto cleanup; float ratio = 1.0 * gdk_pixbuf_get_height(tmp) / gdk_pixbuf_get_width(tmp); int width = 128, height = 128 * ratio; pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_BILINEAR); have_preview = TRUE; cleanup: gdk_pixbuf_loader_close(loader, NULL); free(mime_type); free(buffer); g_object_unref(loader); // This should clean up tmp as well } } if(have_preview && !no_preview_fallback) { // get image orientation dt_image_t img = { 0 }; (void)dt_exif_read(&img, filename); // Rotate the image to the correct orientation GdkPixbuf *tmp = pixbuf; if(img.orientation == ORIENTATION_ROTATE_CCW_90_DEG) { tmp = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE); } else if(img.orientation == ORIENTATION_ROTATE_CW_90_DEG) { tmp = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE); } else if(img.orientation == ORIENTATION_ROTATE_180_DEG) { tmp = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN); } if(pixbuf != tmp) { g_object_unref(pixbuf); pixbuf = tmp; } } if(no_preview_fallback || !have_preview) { guint8 *image_buffer = NULL; /* load the dt logo as a brackground */ char filename[PATH_MAX] = { 0 }; char datadir[PATH_MAX] = { 0 }; char *logo; dt_logo_season_t season = get_logo_season(); if(season != DT_LOGO_SEASON_NONE) logo = g_strdup_printf("%%s/pixmaps/idbutton-%d.svg", (int)season); else logo = g_strdup("%s/pixmaps/idbutton.svg"); dt_loc_get_datadir(datadir, sizeof(datadir)); snprintf(filename, sizeof(filename), logo, datadir); g_free(logo); RsvgHandle *svg = rsvg_handle_new_from_file(filename, NULL); if(svg) { cairo_surface_t *surface; cairo_t *cr; RsvgDimensionData dimension; rsvg_handle_get_dimensions(svg, &dimension); float svg_size = MAX(dimension.width, dimension.height); float final_size = 128; float factor = final_size / svg_size; float final_width = dimension.width * factor * darktable.gui->ppd, final_height = dimension.height * factor * darktable.gui->ppd; int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, final_width); image_buffer = (guint8 *)calloc(stride * final_height, sizeof(guint8)); surface = dt_cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_ARGB32, final_width, final_height, stride); if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { free(image_buffer); image_buffer = NULL; } else { cr = cairo_create(surface); cairo_scale(cr, factor, factor); rsvg_handle_render_cairo(svg, cr); cairo_surface_flush(surface); pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, final_width / darktable.gui->ppd, final_height / darktable.gui->ppd); } g_object_unref(svg); } have_preview = TRUE; } if(have_preview) gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); if(pixbuf) g_object_unref(pixbuf); g_free(filename); gtk_file_chooser_set_preview_widget_active(file_chooser, have_preview); }