2012-05-27 19:20:34 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2012 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
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
|
2016-01-20 07:53:15 +01:00
|
|
|
local color = 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
|
|
|
|
Bump minimum lgi dependency to 0.7.0
Before commit 1b2826 in lgi, the get_rgba() function on cairo SolidPatterns was
specified like this:
get_rgba = { ret = cairo.Status,
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' } },
The above commit fixed this (without saying so) and the code became:
get_rgba = { ret = cairo.Status,
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' } },
The prototype for the corresponding cairo function is:
cairo_public cairo_status_t
cairo_pattern_get_rgba (cairo_pattern_t *pattern,
double *red, double *green,
double *blue, double *alpha);
As you see, this functions gets four double* as arguments and it will save its
result via those pointers. Old versions of lgi call this function with too few
arguments and this will cause a segmentation fault when cairo dereferences an
invalid pointer.
Signed-off-by: Uli Schlachter <psychon@znc.in>
2014-03-17 17:52:09 +01:00
|
|
|
-- Keep this in sync with build-utils/lgi-check.sh!
|
2015-08-11 18:52:10 +02:00
|
|
|
local ver_major, ver_minor, ver_patch = string.match(require('lgi.version'), '(%d)%.(%d)%.(%d)')
|
|
|
|
if tonumber(ver_major) <= 0 and (tonumber(ver_minor) < 7 or (tonumber(ver_minor) == 7 and tonumber(ver_patch) < 1)) then
|
|
|
|
error("lgi too old, need at least version 0.7.1")
|
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.
|
2016-01-15 18:27:22 +01:00
|
|
|
-- @param _surface The surface to load or nil
|
|
|
|
-- @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
|
|
|
|
-- @return An error message, or nil on success
|
|
|
|
function surface.load_uncached_silently(_surface, default)
|
2014-04-09 21:55:07 +02:00
|
|
|
local file
|
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
|
2016-01-15 18:27:22 +01:00
|
|
|
local err
|
2014-04-09 21:55:07 +02:00
|
|
|
file = _surface
|
2016-01-15 18:27:22 +01:00
|
|
|
_surface, err = capi.awesome.load_image(file)
|
|
|
|
if not _surface then
|
2016-01-17 17:33:39 +01:00
|
|
|
return get_default(default), err
|
2016-01-15 18:27:22 +01:00
|
|
|
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.
|
|
|
|
-- @param _surface The surface to load or nil
|
|
|
|
-- @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.
|
|
|
|
-- @return An error message, or nil on success
|
|
|
|
function surface.load_silently(_surface, default)
|
2014-03-29 21:41:47 +01:00
|
|
|
if type(_surface) == "string" then
|
|
|
|
local cache = surface_cache[_surface]
|
2014-04-09 21:55:07 +02:00
|
|
|
if cache then
|
|
|
|
return cache
|
2014-03-29 21:41:47 +01:00
|
|
|
end
|
2016-02-28 17:21:10 +01:00
|
|
|
local result, err = surface.load_uncached_silently(_surface, default)
|
|
|
|
if not err then
|
|
|
|
-- Cache the file
|
|
|
|
surface_cache[_surface] = result
|
|
|
|
end
|
|
|
|
return result, err
|
2014-03-29 21:41:47 +01:00
|
|
|
end
|
2016-01-15 18:27:22 +01:00
|
|
|
return surface.load_uncached_silently(_surface, default)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_load_and_handle_errors(_surface, func)
|
|
|
|
if type(_surface) == 'nil' then
|
2016-01-17 17:33:39 +01:00
|
|
|
return get_default()
|
2016-01-15 18:27:22 +01:00
|
|
|
end
|
|
|
|
local result, err = func(_surface, false)
|
|
|
|
if result then
|
|
|
|
return result
|
|
|
|
end
|
2016-05-11 21:50:02 +02:00
|
|
|
gdebug.print_error(debug.traceback(
|
|
|
|
"Failed to load '" .. tostring(_surface) .. "': " .. 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`.
|
|
|
|
-- @param _surface The surface to load or nil
|
|
|
|
-- @return The loaded surface, or nil
|
|
|
|
function surface.load_uncached(_surface)
|
|
|
|
return do_load_and_handle_errors(_surface, surface.load_uncached_silently)
|
|
|
|
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`.
|
|
|
|
-- @param _surface The surface to load or nil
|
|
|
|
-- @return The loaded surface, or nil
|
|
|
|
function surface.load(_surface)
|
|
|
|
return do_load_and_handle_errors(_surface, 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
|
|
|
|
-- @return The surface's width and height
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
-- @param[opt=white] shape_color The shape color or pattern
|
|
|
|
-- @param[opt=transparent] bg_color The surface background color
|
|
|
|
-- @treturn cairo.surface the new surface
|
|
|
|
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.
|
|
|
|
-- @param draw A wibox or a client
|
|
|
|
-- @param shape or gears.shape function or a custom function with a context,
|
|
|
|
-- width and height as parameter.
|
|
|
|
-- @param[opt] Any additional parameters will be passed to the shape function
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
local function redraw(h)
|
|
|
|
h:draw({dpi=96}, cr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local h = hierarchy.new({dpi=96}, self, width, height, redraw, no_op, {})
|
|
|
|
|
|
|
|
redraw(h)
|
|
|
|
|
|
|
|
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.
|
|
|
|
-- @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
|
|
|
|
-- @return The hierarchy
|
|
|
|
function surface.widget_to_svg(widget, path, width, height)
|
|
|
|
local img = cairo.SvgSurface.create(path, width, height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
|
|
|
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.
|
|
|
|
-- @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
|
|
|
|
-- @return The hierarchy
|
|
|
|
function surface.widget_to_surface(widget, width, height, format)
|
|
|
|
local img = cairo.ImageSurface(format or cairo.Format.ARGB32, width, height)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
|
|
|
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
|