2012-05-27 19:20:34 +02:00
|
|
|
---------------------------------------------------------------------------
|
2021-12-21 06:54:15 +01:00
|
|
|
-- Utilities to integrate and manipulate Cairo drawing surfaces.
|
|
|
|
--
|
2012-05-27 19:20:34 +02:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2012 Uli Schlachter
|
2014-05-19 15:15:39 +02:00
|
|
|
-- @module gears.surface
|
2012-05-27 19:20:34 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local type = type
|
|
|
|
local capi = { awesome = awesome }
|
|
|
|
local cairo = require("lgi").cairo
|
2018-01-18 18:19:07 +01:00
|
|
|
local GdkPixbuf = require("lgi").GdkPixbuf
|
2021-12-11 18:31:06 +01:00
|
|
|
local color, beautiful = nil, nil
|
2016-01-15 18:27:22 +01:00
|
|
|
local gdebug = require("gears.debug")
|
2016-03-29 06:10:37 +02:00
|
|
|
local hierarchy = require("wibox.hierarchy")
|
2012-05-27 19:20:34 +02:00
|
|
|
|
2017-06-13 15:02:55 +02:00
|
|
|
-- Keep this in sync with build-utils/lgi-check.c!
|
2015-08-11 18:52:10 +02:00
|
|
|
local ver_major, ver_minor, ver_patch = string.match(require('lgi.version'), '(%d)%.(%d)%.(%d)')
|
2017-01-02 19:53:02 +01:00
|
|
|
if tonumber(ver_major) <= 0 and (tonumber(ver_minor) < 8 or (tonumber(ver_minor) == 8 and tonumber(ver_patch) < 0)) then
|
|
|
|
error("lgi too old, need at least version 0.8.0")
|
2012-06-05 16:22:04 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
local surface = { mt = {} }
|
2014-03-29 21:41:47 +01:00
|
|
|
local surface_cache = setmetatable({}, { __mode = 'v' })
|
2012-05-27 19:20:34 +02:00
|
|
|
|
2016-01-17 17:33:39 +01:00
|
|
|
local function get_default(arg)
|
|
|
|
if type(arg) == 'nil' then
|
|
|
|
return cairo.ImageSurface(cairo.Format.ARGB32, 0, 0)
|
|
|
|
end
|
|
|
|
return arg
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
--- Try to convert the argument into an lgi cairo surface.
|
|
|
|
-- This is usually needed for loading images by file name.
|
2022-07-03 08:10:31 +02:00
|
|
|
-- @param surface The surface to load or nil
|
2016-01-15 18:27:22 +01:00
|
|
|
-- @param default The default value to return on error; when nil, then a surface
|
|
|
|
-- in an error state is returned.
|
|
|
|
-- @return The loaded surface, or the replacement default
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return An error message, or nil on success.
|
|
|
|
-- @staticfct load_uncached_silently
|
2016-01-15 18:27:22 +01:00
|
|
|
function surface.load_uncached_silently(_surface, default)
|
2016-01-17 17:33:39 +01:00
|
|
|
-- On nil, return some sane default
|
2012-06-12 10:13:46 +02:00
|
|
|
if not _surface then
|
2016-01-17 17:33:39 +01:00
|
|
|
return get_default(default)
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
-- lgi cairo surfaces don't get changed either
|
2012-06-12 10:13:46 +02:00
|
|
|
if cairo.Surface:is_type_of(_surface) then
|
|
|
|
return _surface
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
-- Strings are assumed to be file names and get loaded
|
2012-06-12 10:13:46 +02:00
|
|
|
if type(_surface) == "string" then
|
2018-01-18 18:19:07 +01:00
|
|
|
local pixbuf, err = GdkPixbuf.Pixbuf.new_from_file(_surface)
|
|
|
|
if not pixbuf then
|
|
|
|
return get_default(default), tostring(err)
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
2018-01-13 04:58:06 +01:00
|
|
|
_surface = capi.awesome.pixbuf_to_surface(pixbuf._native, _surface)
|
|
|
|
|
|
|
|
-- The shims implement load_image() to return a surface directly,
|
|
|
|
-- instead of a lightuserdatum.
|
|
|
|
if cairo.Surface:is_type_of(_surface) then
|
|
|
|
return _surface
|
|
|
|
end
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
-- Everything else gets forced into a surface
|
2016-02-28 17:21:10 +01:00
|
|
|
return cairo.Surface(_surface, true)
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
|
2016-01-15 18:27:22 +01:00
|
|
|
--- Try to convert the argument into an lgi cairo surface.
|
|
|
|
-- This is usually needed for loading images by file name and uses a cache.
|
|
|
|
-- In contrast to `load()`, errors are returned to the caller.
|
2022-07-03 08:10:31 +02:00
|
|
|
-- @param surface The surface to load or nil
|
2016-01-15 18:27:22 +01:00
|
|
|
-- @param default The default value to return on error; when nil, then a surface
|
|
|
|
-- in an error state is returned.
|
|
|
|
-- @return The loaded surface, or the replacement default, or nil if called with
|
|
|
|
-- nil.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return An error message, or nil on success.
|
|
|
|
-- @staticfct load_silently
|
2022-07-03 08:10:31 +02:00
|
|
|
function surface.load_silently(self, default)
|
|
|
|
if type(self) == "string" then
|
|
|
|
local cache = surface_cache[self]
|
2014-04-09 21:55:07 +02:00
|
|
|
if cache then
|
|
|
|
return cache
|
2014-03-29 21:41:47 +01:00
|
|
|
end
|
2022-07-03 08:10:31 +02:00
|
|
|
local result, err = surface.load_uncached_silently(self, default)
|
2016-02-28 17:21:10 +01:00
|
|
|
if not err then
|
|
|
|
-- Cache the file
|
2022-07-03 08:10:31 +02:00
|
|
|
surface_cache[self] = result
|
2016-02-28 17:21:10 +01:00
|
|
|
end
|
|
|
|
return result, err
|
2014-03-29 21:41:47 +01:00
|
|
|
end
|
2022-07-03 08:10:31 +02:00
|
|
|
return surface.load_uncached_silently(self, default)
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
|
|
|
|
2022-07-03 08:10:31 +02:00
|
|
|
local function do_load_and_handle_errors(self, func)
|
|
|
|
if type(self) == 'nil' then
|
2016-01-17 17:33:39 +01:00
|
|
|
return get_default()
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
2022-07-03 08:10:31 +02:00
|
|
|
local result, err = func(self, false)
|
2016-01-15 18:27:22 +01:00
|
|
|
if result then
|
|
|
|
return result
|
|
|
|
end
|
2016-05-11 21:50:02 +02:00
|
|
|
gdebug.print_error(debug.traceback(
|
2022-07-03 08:10:31 +02:00
|
|
|
"Failed to load '" .. tostring(self) .. "': " .. tostring(err)))
|
2016-01-17 17:33:39 +01:00
|
|
|
return get_default()
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Try to convert the argument into an lgi cairo surface.
|
|
|
|
-- This is usually needed for loading images by file name. Errors are handled
|
|
|
|
-- via `gears.debug.print_error`.
|
2022-07-03 08:10:31 +02:00
|
|
|
-- @param surface The surface to load or nil
|
2016-01-15 18:27:22 +01:00
|
|
|
-- @return The loaded surface, or nil
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct load_uncached
|
2022-07-03 08:10:31 +02:00
|
|
|
function surface.load_uncached(self)
|
|
|
|
return do_load_and_handle_errors(self, surface.load_uncached_silently)
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Try to convert the argument into an lgi cairo surface.
|
|
|
|
-- This is usually needed for loading images by file name. Errors are handled
|
|
|
|
-- via `gears.debug.print_error`.
|
2022-07-03 08:10:31 +02:00
|
|
|
-- @param surface The surface to load or nil
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return The loaded surface, or nil.
|
|
|
|
-- @staticfct gears.surface
|
2022-07-03 08:10:31 +02:00
|
|
|
function surface.load(self)
|
|
|
|
return do_load_and_handle_errors(self, surface.load_silently)
|
2014-03-29 21:41:47 +01:00
|
|
|
end
|
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
function surface.mt.__call(_, ...)
|
2012-06-12 10:13:46 +02:00
|
|
|
return surface.load(...)
|
|
|
|
end
|
|
|
|
|
2014-03-16 03:00:23 +01:00
|
|
|
--- Get the size of a cairo surface
|
|
|
|
-- @param surf The surface you are interested in
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return The surface's width and height.
|
|
|
|
-- @staticfct get_size
|
2014-03-16 03:00:23 +01:00
|
|
|
function surface.get_size(surf)
|
|
|
|
local cr = cairo.Context(surf)
|
|
|
|
local x, y, w, h = cr:clip_extents()
|
|
|
|
return w - x, h - y
|
|
|
|
end
|
|
|
|
|
2015-09-05 21:12:09 +02:00
|
|
|
--- Create a copy of a cairo surface.
|
|
|
|
-- The surfaces returned by `surface.load` are cached and must not be
|
|
|
|
-- modified to avoid unintended side-effects. This function allows to create
|
|
|
|
-- a copy of a cairo surface. This copy can then be freely modified.
|
|
|
|
-- The surface returned will be as compatible as possible to the input
|
|
|
|
-- surface. For example, it will likely be of the same surface type as the
|
|
|
|
-- input. The details are explained in the `create_similar` function on a cairo
|
|
|
|
-- surface.
|
2015-08-06 18:54:06 +02:00
|
|
|
-- @param s Source surface.
|
|
|
|
-- @return The surface's duplicate.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct duplicate_surface
|
2015-08-06 18:54:06 +02:00
|
|
|
function surface.duplicate_surface(s)
|
|
|
|
s = surface.load(s)
|
|
|
|
|
|
|
|
-- Figure out surface size (this does NOT work for unbounded recording surfaces)
|
|
|
|
local cr = cairo.Context(s)
|
|
|
|
local x, y, w, h = cr:clip_extents()
|
|
|
|
|
|
|
|
-- Create a copy
|
|
|
|
local result = s:create_similar(s.content, w - x, h - y)
|
|
|
|
cr = cairo.Context(result)
|
|
|
|
cr:set_source_surface(s, 0, 0)
|
|
|
|
cr.operator = cairo.Operator.SOURCE
|
|
|
|
cr:paint()
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2016-01-20 07:53:15 +01:00
|
|
|
--- Create a surface from a `gears.shape`
|
|
|
|
-- Any additional parameters will be passed to the shape function
|
|
|
|
-- @tparam number width The surface width
|
|
|
|
-- @tparam number height The surface height
|
|
|
|
-- @param shape A `gears.shape` compatible function
|
2021-12-17 22:46:08 +01:00
|
|
|
-- @param[opt="#000000"] shape_color The shape color or pattern
|
|
|
|
-- @param[opt="#00000000"] bg_color The surface background color
|
2016-01-20 07:53:15 +01:00
|
|
|
-- @treturn cairo.surface the new surface
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct load_from_shape
|
2016-01-20 07:53:15 +01:00
|
|
|
function surface.load_from_shape(width, height, shape, shape_color, bg_color, ...)
|
|
|
|
color = color or require("gears.color")
|
|
|
|
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.ARGB32, width, height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
|
|
|
cr:set_source(color(bg_color or "#00000000"))
|
|
|
|
cr:paint()
|
|
|
|
|
|
|
|
cr:set_source(color(shape_color or "#000000"))
|
|
|
|
|
|
|
|
shape(cr, width, height, ...)
|
|
|
|
|
|
|
|
cr:fill()
|
|
|
|
|
|
|
|
return img
|
|
|
|
end
|
|
|
|
|
2016-01-18 23:22:44 +01:00
|
|
|
--- Apply a shape to a client or a wibox.
|
|
|
|
--
|
|
|
|
-- If the wibox or client size change, this function need to be called
|
|
|
|
-- again.
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam client|wibox draw A wibox or a client.
|
|
|
|
-- @tparam gears.shape|function shape The shape.
|
|
|
|
-- @param[opt] ... Any additional parameters will be passed to the shape function.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct apply_shape_bounding
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2016-01-18 23:22:44 +01:00
|
|
|
function surface.apply_shape_bounding(draw, shape, ...)
|
|
|
|
local geo = draw:geometry()
|
|
|
|
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.A1, geo.width, geo.height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
|
|
|
cr:set_operator(cairo.Operator.CLEAR)
|
|
|
|
cr:set_source_rgba(0,0,0,1)
|
|
|
|
cr:paint()
|
|
|
|
cr:set_operator(cairo.Operator.SOURCE)
|
|
|
|
cr:set_source_rgba(1,1,1,1)
|
|
|
|
|
|
|
|
shape(cr, geo.width, geo.height, ...)
|
|
|
|
|
|
|
|
cr:fill()
|
|
|
|
|
|
|
|
draw.shape_bounding = img._native
|
2017-08-04 13:18:01 +02:00
|
|
|
img:finish()
|
2016-01-18 23:22:44 +01:00
|
|
|
end
|
2015-08-06 18:54:06 +02:00
|
|
|
|
2016-03-29 06:10:37 +02:00
|
|
|
local function no_op() end
|
|
|
|
|
|
|
|
local function run_in_hierarchy(self, cr, width, height)
|
2016-10-19 12:32:30 +02:00
|
|
|
local context = {dpi=96}
|
|
|
|
local h = hierarchy.new(context, self, width, height, no_op, no_op, {})
|
|
|
|
h:draw(context, cr)
|
2016-03-29 06:10:37 +02:00
|
|
|
return h
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create an SVG file with this widget content.
|
|
|
|
-- This is dynamic, so the SVG will be updated along with the widget content.
|
|
|
|
-- because of this, the painting may happen hover multiple event loop cycles.
|
2021-12-11 18:31:06 +01:00
|
|
|
-- @deprecated widget_to_svg
|
2016-03-29 06:10:37 +02:00
|
|
|
-- @tparam widget widget A widget
|
|
|
|
-- @tparam string path The output file path
|
|
|
|
-- @tparam number width The surface width
|
|
|
|
-- @tparam number height The surface height
|
|
|
|
-- @return The cairo surface
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return The hierarchy.
|
|
|
|
-- @see wibox.widget.draw_to_svg_file
|
|
|
|
-- @see wibox.widget.draw_to_image_surface
|
2016-03-29 06:10:37 +02:00
|
|
|
function surface.widget_to_svg(widget, path, width, height)
|
2017-05-30 10:34:33 +02:00
|
|
|
gdebug.deprecate("Use wibox.widget.draw_to_svg_file instead of "..
|
2021-12-11 18:31:06 +01:00
|
|
|
"gears.surface.widget_to_svg", {deprecated_in=5})
|
2016-03-29 06:10:37 +02:00
|
|
|
local img = cairo.SvgSurface.create(path, width, height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
2021-12-11 18:31:06 +01:00
|
|
|
-- Bad dependecy, but this is deprecated.
|
|
|
|
beautiful = beautiful or require("beautiful")
|
|
|
|
color = color or require("gears.color")
|
|
|
|
cr:set_source(color(beautiful.fg_normal))
|
|
|
|
|
2016-03-29 06:10:37 +02:00
|
|
|
return img, run_in_hierarchy(widget, cr, width, height)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a cairo surface with this widget content.
|
|
|
|
-- This is dynamic, so the SVG will be updated along with the widget content.
|
|
|
|
-- because of this, the painting may happen hover multiple event loop cycles.
|
2021-12-11 18:31:06 +01:00
|
|
|
-- @deprecated widget_to_surface
|
2016-03-29 06:10:37 +02:00
|
|
|
-- @tparam widget widget A widget
|
|
|
|
-- @tparam number width The surface width
|
|
|
|
-- @tparam number height The surface height
|
|
|
|
-- @param[opt=cairo.Format.ARGB32] format The surface format
|
|
|
|
-- @return The cairo surface
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @return The hierarchy.
|
|
|
|
-- @see wibox.widget.draw_to_svg_file
|
|
|
|
-- @see wibox.widget.draw_to_image_surface
|
2016-03-29 06:10:37 +02:00
|
|
|
function surface.widget_to_surface(widget, width, height, format)
|
2017-05-30 10:34:33 +02:00
|
|
|
gdebug.deprecate("Use wibox.widget.draw_to_image_surface instead of "..
|
|
|
|
"gears.surface.render_to_surface", {deprecated_in=5})
|
2016-03-29 06:10:37 +02:00
|
|
|
local img = cairo.ImageSurface(format or cairo.Format.ARGB32, width, height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
2021-12-11 18:31:06 +01:00
|
|
|
-- Bad dependecy, but this is deprecated.
|
|
|
|
color = color or require("gears.color")
|
|
|
|
beautiful = beautiful or require("beautiful")
|
|
|
|
cr:set_source(color(beautiful.fg_normal))
|
|
|
|
|
2016-03-29 06:10:37 +02:00
|
|
|
return img, run_in_hierarchy(widget, cr, width, height)
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
return setmetatable(surface, surface.mt)
|
2012-05-27 19:20:34 +02:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|