Ejemplo n.º 1
1
/*  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;
}
Ejemplo n.º 2
0
JNIEXPORT jlong JNICALL
Java_org_gnome_rsvg_RsvgHandle_rsvg_1handle_1get_1pixbuf
(
	JNIEnv* env,
	jclass cls,
	jlong _self
)
{
	GdkPixbuf* result;
	jlong _result;
	RsvgHandle* self;

	// convert parameter self
	self = (RsvgHandle*) _self;

	// call function
	result = rsvg_handle_get_pixbuf(self);

	// cleanup parameter self

	// translate return value to JNI type
	_result = (jlong) result;

	// cleanup return value
	if (result != NULL) {
		bindings_java_memory_cleanup((GObject*)result, FALSE);
	}

	// and finally
	return _result;
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
static VALUE
rb_rsvg_handle_get_pixbuf(int argc, VALUE *argv, VALUE self)
{
    VALUE id;
    VALUE rb_pixbuf;
    GdkPixbuf *pixbuf = NULL;

    rb_scan_args(argc, argv, "01", &id);
    if (NIL_P(id)) {
        pixbuf = rsvg_handle_get_pixbuf(_SELF(self));
    } else {
#ifdef HAVE_RSVG_HANDLE_GET_PIXBUF_SUB
        pixbuf = rsvg_handle_get_pixbuf_sub(_SELF(self),
                                            (const char *)RVAL2CSTR(id));
#else
        rb_warning("rsvg_handle_get_pixbuf_sub isn't "
                   "supported in your librsvg");
#endif
    }

    rb_pixbuf = GOBJ2RVAL(pixbuf);
    if (pixbuf)
        g_object_unref(pixbuf);
    return rb_pixbuf;
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
	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;
	}
Ejemplo n.º 7
0
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);
    }
}
Ejemplo n.º 8
0
Archivo: svg.c Proyecto: clones/kaa
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);
}
Ejemplo n.º 9
0
int resize_launcher(void *obj)
{
	Launcher *launcher = obj;
	GSList *l;
	int count, icon_size;
	int icons_per_column=1, icons_per_row=1, marging=0;

	if (panel_horizontal) {
		icon_size = launcher->area.height;
	} else {
		icon_size = launcher->area.width;
	}
	icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
	if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
		icon_size = launcher_max_icon_size;

	// Resize icons if necessary
	for (l = launcher->list_icons; l ; l = l->next) {
		LauncherIcon *launcherIcon = (LauncherIcon *)l->data;
		if (launcherIcon->icon_size != icon_size || !launcherIcon->icon_original) {
			launcherIcon->icon_size = icon_size;
			launcherIcon->area.width = launcherIcon->icon_size;
			launcherIcon->area.height = launcherIcon->icon_size;

			// Get the path for an icon file with the new size
			char *new_icon_path = get_icon_path(launcher->list_themes, launcherIcon->icon_name, launcherIcon->icon_size);
			if (!new_icon_path) {
				// Draw a blank icon
				free_icon(launcherIcon->icon_original);
				launcherIcon->icon_original = NULL;
				free_icon(launcherIcon->icon_scaled);
				launcherIcon->icon_scaled = NULL;
				continue;
			}
			if (launcherIcon->icon_path && strcmp(new_icon_path, launcherIcon->icon_path) == 0) {
				// If it's the same file just rescale
				free_icon(launcherIcon->icon_scaled);
				launcherIcon->icon_scaled = scale_icon(launcherIcon->icon_original, icon_size);
				free(new_icon_path);
				fprintf(stderr, "launcher.c %d: Using icon %s\n", __LINE__, launcherIcon->icon_path);
			} else {
				// Free the old files
				free_icon(launcherIcon->icon_original);
				free_icon(launcherIcon->icon_scaled);
				launcherIcon->icon_original = launcherIcon->icon_scaled = NULL;
				// Load the new file and scale
#ifdef HAVE_RSVG
				if (g_str_has_suffix(new_icon_path, ".svg")) {
					GError* err = NULL;
					RsvgHandle* svg = rsvg_handle_new_from_file(new_icon_path, &err);

					if (err != NULL) {
						fprintf(stderr, "Could not load svg image!: %s", err->message);
						g_error_free(err);
						launcherIcon->icon_original = NULL;
					} else {
						char suffix[128];
						sprintf(suffix, "tmpicon-%d.png", getpid());
						gchar *name = g_build_filename(g_get_user_config_dir(), "tint2", suffix, NULL);
						GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf(svg);
						gdk_pixbuf_save(pixbuf, name, "png", NULL, NULL);
						launcherIcon->icon_original = imlib_load_image_immediately_without_cache(name);
						g_remove(name);
						g_free(name);
						g_object_unref(G_OBJECT(pixbuf));
						g_object_unref(G_OBJECT(svg));
					}
				} else
#endif
				{
					launcherIcon->icon_original = imlib_load_image_immediately(new_icon_path);
				}
				// On loading error, fallback to default
				if (!launcherIcon->icon_original) {
					free(new_icon_path);
					new_icon_path = get_icon_path(launcher->list_themes, DEFAULT_ICON, launcherIcon->icon_size);
					if (new_icon_path)
						launcherIcon->icon_original = imlib_load_image_immediately(new_icon_path);
				}

				if (!launcherIcon->icon_original) {
					// Loading default icon failed, draw a blank icon
					free(new_icon_path);
				} else {
					// Loaded icon successfully
					launcherIcon->icon_scaled = scale_icon(launcherIcon->icon_original, launcherIcon->icon_size);
					free(launcherIcon->icon_path);
					launcherIcon->icon_path = new_icon_path;
					fprintf(stderr, "launcher.c %d: Using icon %s\n", __LINE__, launcherIcon->icon_path);
				}
			}
		}
	}
	
	count = g_slist_length(launcher->list_icons);

	if (panel_horizontal) {
		if (!count) {
			launcher->area.width = 0;
		} else {
			int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
			// here icons_per_column always higher than 0
			icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
			marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
			icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
			launcher->area.width = (2 * launcher->area.bg->border.width) +
								   (2 * launcher->area.paddingxlr) +
								   (icon_size * icons_per_row) +
								   ((icons_per_row-1) * launcher->area.paddingx);
		}
	}
	else {
		if (!count) {
			launcher->area.height = 0;
		} else {
			int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
			// here icons_per_row always higher than 0
			icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
			marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
			icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
			launcher->area.height = (2 * launcher->area.bg->border.width) +
									(2 * launcher->area.paddingxlr) +
									(icon_size * icons_per_column) +
									((icons_per_column-1) * launcher->area.paddingx);
		}
	}

	int i, posx, posy;
	int start = launcher->area.bg->border.width + launcher->area.paddingy + marging/2;
	if (panel_horizontal) {
		posy = start;
		posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
	} else {
		posx = start;
		posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
	}

	for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
		LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
		
		launcherIcon->y = posy;
		launcherIcon->x = posx;
		launcherIcon->area.posy = ((Area*)launcherIcon->area.parent)->posy + launcherIcon->y;
		launcherIcon->area.posx = ((Area*)launcherIcon->area.parent)->posx + launcherIcon->x;
		launcherIcon->area.width = launcherIcon->icon_size;
		launcherIcon->area.height = launcherIcon->icon_size;
		//printf("launcher %d : %d,%d\n", i, posx, posy);
		if (panel_horizontal) {
			if (i % icons_per_column) {
				posy += icon_size + launcher->area.paddingx;
			} else {
				posy = start;
				posx += (icon_size + launcher->area.paddingx);
			}
		} else {
			if (i % icons_per_row) {
				posx += icon_size + launcher->area.paddingx;
			} else {
				posx = start;
				posy += (icon_size + launcher->area.paddingx);
			}
		}
	}
	return 1;
}