diff --git a/obj_surface.c b/obj_surface.c index 2b95a3e..a9e2a83 100644 --- a/obj_surface.c +++ b/obj_surface.c @@ -752,6 +752,38 @@ supports_mime_type(lua_State *L) lua_pushboolean(L, cairo_surface_supports_mime_type(*obj, mime_type)); return 1; } + +static int +map_to_image(lua_State *L) +{ + cairo_rectangle_int_t rect; + cairo_rectangle_int_t *prect; + cairo_surface_t **obj = luaL_checkudata(L, 1, OOCAIRO_MT_NAME_SURFACE); + cairo_surface_t **res; + + lua_settop(L, 2); + if (lua_isnil(L, 2)) { + prect = NULL; + } else { + prect = ▭ + from_lua_rectangle(L, prect, 2); + } + + res = create_surface_userdata(L); + *res = cairo_surface_map_to_image(*obj, prect); + return 1; +} + +static int +unmap_image(lua_State *L) +{ + cairo_surface_t **obj = luaL_checkudata(L, 1, OOCAIRO_MT_NAME_SURFACE); + cairo_surface_t **img = luaL_checkudata(L, 2, OOCAIRO_MT_NAME_SURFACE); + /* unmap_image will drop a reference, so we need a new reference for it */ + cairo_surface_reference(*img); + cairo_surface_unmap_image(*obj, *img); + return 0; +} #endif static int @@ -808,6 +840,10 @@ surface_methods[] = { { "status", surface_status }, #ifdef CAIRO_HAS_PNG_FUNCTIONS { "write_to_png", surface_write_to_png }, +#endif +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) + { "map_to_image", map_to_image }, + { "unmap_image", unmap_image }, #endif { 0, 0 } }; diff --git a/test/surface.lua b/test/surface.lua index cfe8e54..cf6c3fe 100644 --- a/test/surface.lua +++ b/test/surface.lua @@ -449,6 +449,18 @@ if Cairo.check_version(1, 12, 0) then test(surface, Cairo.MIME_TYPE_URI, Cairo.MIME_TYPE_JP2) end end + + function test_map_image () + local surface = Cairo.image_surface_create("rgb24", 2, 2) + + -- first unbounded + local img = surface:map_to_image() + surface:unmap_image(img) + + -- then bounded + local img = surface:map_to_image({ x = 1, y = 1, width = 1, height = 1}) + surface:unmap_image(img) + end end -- vi:ts=4 sw=4 expandtab