Add awesome.pixbuf_to_surface

This function takes a GdkPixbuf, copies it to a cairo image surface and
returns the image surface.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2018-01-18 18:10:35 +01:00
parent 19a1ee6c16
commit c5badcbe37
3 changed files with 27 additions and 1 deletions

2
draw.c
View File

@ -81,7 +81,7 @@ draw_surface_from_data(int width, int height, uint32_t *data)
* \param buf The pixbuf
* \return Number of items pushed on the lua stack.
*/
static cairo_surface_t *
cairo_surface_t *
draw_surface_from_pixbuf(GdkPixbuf *buf)
{
int width = gdk_pixbuf_get_width(buf);

4
draw.h
View File

@ -30,6 +30,9 @@
#include "common/array.h"
#include "common/util.h"
/* Forward definition */
typedef struct _GdkPixbuf GdkPixbuf;
typedef struct area_t area_t;
struct area_t
{
@ -57,6 +60,7 @@ DO_ARRAY(cairo_surface_t *, cairo_surface, cairo_surface_array_destroy_surface)
cairo_surface_t *draw_surface_from_data(int width, int height, uint32_t *data);
cairo_surface_t *draw_dup_image_surface(cairo_surface_t *surface);
cairo_surface_t *draw_load_image(lua_State *L, const char *path, GError **error);
cairo_surface_t *draw_surface_from_pixbuf(GdkPixbuf *buf);
xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual);
xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s);

22
luaa.c
View File

@ -281,6 +281,23 @@ luaA_sync(lua_State *L)
return 0;
}
/** Translate a GdkPixbuf to a cairo image surface..
*
* @param pixbuf The pixbuf as a light user datum.
* @return A cairo surface as light user datum.
* @function pixbuf_to_surface
*/
static int
luaA_pixbuf_to_surface(lua_State *L)
{
GdkPixbuf *pixbuf = (GdkPixbuf *) lua_touserdata(L, 1);
cairo_surface_t *surface = draw_surface_from_pixbuf(pixbuf);
/* lua has to make sure to free the ref or we have a leak */
lua_pushlightuserdata(L, surface);
return 1;
}
/** Load an image from a given path.
*
* @param name The file name.
@ -292,6 +309,10 @@ luaA_sync(lua_State *L)
static int
luaA_load_image(lua_State *L)
{
/* TODO: Deprecate this function, Lua can use GdkPixbuf directly plus
* awesome.pixbuf_to_surface
*/
GError *error = NULL;
const char *filename = luaL_checkstring(L, 1);
cairo_surface_t *surface = draw_load_image(L, filename, &error);
@ -776,6 +797,7 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath)
{ "emit_signal", luaA_awesome_emit_signal },
{ "systray", luaA_systray },
{ "load_image", luaA_load_image },
{ "pixbuf_to_surface", luaA_pixbuf_to_surface },
{ "set_preferred_icon_size", luaA_set_preferred_icon_size },
{ "register_xproperty", luaA_register_xproperty },
{ "set_xproperty", luaA_set_xproperty },