2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
2014-05-24 23:18:19 +02:00
|
|
|
--- Theme library.
|
|
|
|
--
|
2008-08-05 22:26:34 +02:00
|
|
|
-- @author Damien Leone <damien.leone@gmail.com>
|
2008-08-05 22:21:22 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
2009-04-18 16:05:18 +02:00
|
|
|
-- @copyright 2008-2009 Damien Leone, Julien Danjou
|
2008-08-18 11:41:58 +02:00
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-24 23:18:19 +02:00
|
|
|
-- @module beautiful
|
2008-08-05 22:21:22 +02:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
2009-04-18 16:05:18 +02:00
|
|
|
local os = os
|
2008-08-05 22:21:22 +02:00
|
|
|
local print = print
|
2009-08-21 21:26:50 +02:00
|
|
|
local pcall = pcall
|
2009-04-28 19:40:14 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local dofile = dofile
|
2008-08-05 22:21:22 +02:00
|
|
|
local setmetatable = setmetatable
|
2008-09-29 16:49:18 +02:00
|
|
|
local util = require("awful.util")
|
2012-05-27 19:20:34 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local cairo = lgi.cairo
|
|
|
|
local Pango = lgi.Pango
|
|
|
|
local PangoCairo = lgi.PangoCairo
|
2008-08-20 18:15:08 +02:00
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
2012-05-27 19:20:34 +02:00
|
|
|
awesome = awesome
|
2008-08-20 18:15:08 +02:00
|
|
|
}
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2015-05-14 22:57:22 +02:00
|
|
|
local xresources = require("beautiful.xresources")
|
|
|
|
|
|
|
|
local beautiful = { xresources = xresources, mt = {} }
|
2008-08-05 22:21:22 +02:00
|
|
|
|
|
|
|
-- Local data
|
2012-11-27 22:55:42 +01:00
|
|
|
local theme = {}
|
2011-04-14 22:40:56 +02:00
|
|
|
local descs = setmetatable({}, { __mode = 'k' })
|
|
|
|
local fonts = setmetatable({}, { __mode = 'v' })
|
2011-04-14 07:04:48 +02:00
|
|
|
local active_font
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Load a font from a string or a font description.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @see https://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string
|
|
|
|
-- @tparam string|lgi.Pango.FontDescription name Font, which can be a
|
|
|
|
-- string or a lgi.Pango.FontDescription.
|
|
|
|
-- @treturn table A table with `name`, `description` and `height`.
|
2011-04-14 07:04:48 +02:00
|
|
|
local function load_font(name)
|
|
|
|
name = name or active_font
|
2015-07-13 19:27:38 +02:00
|
|
|
if name and type(name) ~= "string" then
|
|
|
|
if descs[name] then
|
|
|
|
name = descs[name]
|
|
|
|
else
|
|
|
|
name = name:to_string()
|
|
|
|
end
|
2011-04-14 07:04:48 +02:00
|
|
|
end
|
|
|
|
if fonts[name] then
|
|
|
|
return fonts[name]
|
|
|
|
end
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2014-03-23 18:57:48 +01:00
|
|
|
-- Load new font
|
|
|
|
local desc = Pango.FontDescription.from_string(name)
|
2012-05-27 20:14:56 +02:00
|
|
|
local ctx = PangoCairo.font_map_get_default():create_context()
|
2015-05-14 22:57:22 +02:00
|
|
|
ctx:set_resolution(beautiful.xresources.get_dpi())
|
2010-10-06 14:18:11 +02:00
|
|
|
|
2014-03-23 18:57:48 +01:00
|
|
|
-- Apply default values from the context (e.g. a default font size)
|
|
|
|
desc:merge(ctx:get_font_description(), false)
|
|
|
|
|
2015-06-19 21:03:27 +02:00
|
|
|
-- Calculate font height.
|
2014-03-23 18:57:48 +01:00
|
|
|
local metrics = ctx:get_metrics(desc, nil)
|
|
|
|
local height = math.ceil((metrics:get_ascent() + metrics:get_descent()) / Pango.SCALE)
|
|
|
|
|
2011-04-14 07:04:48 +02:00
|
|
|
local font = { name = name, description = desc, height = height }
|
|
|
|
fonts[name] = font
|
|
|
|
descs[desc] = name
|
|
|
|
return font
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Set an active font
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
|
|
|
-- @param name The font
|
2011-04-14 07:04:48 +02:00
|
|
|
local function set_font(name)
|
|
|
|
active_font = load_font(name).name
|
|
|
|
end
|
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Get a font description.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
2015-07-24 01:56:36 +02:00
|
|
|
-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#PangoFontDescription.
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @tparam string|lgi.Pango.FontDescription name The name of the font.
|
|
|
|
-- @treturn lgi.Pango.FontDescription
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get_font(name)
|
2011-04-14 07:04:48 +02:00
|
|
|
return load_font(name).description
|
|
|
|
end
|
|
|
|
|
2015-07-13 19:27:38 +02:00
|
|
|
--- Get a new font with merged attributes, based on another one.
|
|
|
|
--
|
2015-07-24 01:56:36 +02:00
|
|
|
-- See https://developer.gnome.org/pango/stable/pango-Fonts.html#pango-font-description-from-string.
|
2015-07-13 19:27:38 +02:00
|
|
|
-- @tparam string|Pango.FontDescription name The base font.
|
|
|
|
-- @tparam string merge Attributes that should be merged, e.g. "bold".
|
|
|
|
-- @treturn lgi.Pango.FontDescription
|
|
|
|
function beautiful.get_merged_font(name, merge)
|
|
|
|
local font = beautiful.get_font(name)
|
|
|
|
local merge = Pango.FontDescription.from_string(merge)
|
|
|
|
local merged = font:copy_static()
|
|
|
|
merged:merge(merge, true)
|
|
|
|
return beautiful.get_font(merged:to_string())
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the height of a font.
|
2014-05-26 21:43:39 +02:00
|
|
|
--
|
|
|
|
-- @param name Name of the font
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get_font_height(name)
|
2011-04-14 07:04:48 +02:00
|
|
|
return load_font(name).height
|
2010-10-06 14:18:11 +02:00
|
|
|
end
|
2008-08-05 22:21:22 +02:00
|
|
|
|
|
|
|
--- Init function, should be runned at the beginning of configuration file.
|
2014-05-25 14:24:52 +02:00
|
|
|
-- @tparam string|table config The theme to load. It can be either the path to
|
2015-10-14 00:21:29 +02:00
|
|
|
-- the theme file (returning a table) or directly the table
|
|
|
|
-- containing all the theme values.
|
2015-01-20 15:37:35 +01:00
|
|
|
function beautiful.init(config)
|
|
|
|
if config then
|
2009-08-21 21:26:50 +02:00
|
|
|
local success
|
2014-03-24 01:10:45 +01:00
|
|
|
local homedir = os.getenv("HOME")
|
|
|
|
|
2015-01-20 15:37:35 +01:00
|
|
|
-- If config is the path to the theme file,
|
|
|
|
-- run this file,
|
|
|
|
-- else if it is the theme table, save it
|
|
|
|
if type(config) == 'string' then
|
|
|
|
-- Expand the '~' $HOME shortcut
|
|
|
|
config = config:gsub("^~/", homedir .. "/")
|
2015-07-16 15:24:22 +02:00
|
|
|
success, theme = xpcall(function() return dofile(config) end,
|
|
|
|
debug.traceback)
|
2015-01-20 15:37:35 +01:00
|
|
|
elseif type(config) == 'table' then
|
|
|
|
success = true
|
|
|
|
theme = config
|
|
|
|
end
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2009-08-21 21:26:50 +02:00
|
|
|
if not success then
|
|
|
|
return print("E: beautiful: error loading theme file " .. theme)
|
|
|
|
elseif theme then
|
2009-04-28 19:40:14 +02:00
|
|
|
-- expand '~'
|
|
|
|
if homedir then
|
|
|
|
for k, v in pairs(theme) do
|
2014-03-24 01:10:45 +01:00
|
|
|
if type(v) == "string" then theme[k] = v:gsub("^~/", homedir .. "/") end
|
2009-04-28 19:40:14 +02:00
|
|
|
end
|
|
|
|
end
|
2008-08-05 22:21:22 +02:00
|
|
|
|
2010-10-06 14:18:11 +02:00
|
|
|
if theme.font then set_font(theme.font) end
|
2009-04-28 19:40:14 +02:00
|
|
|
else
|
2015-01-20 15:37:35 +01:00
|
|
|
return print("E: beautiful: error loading theme file " .. config)
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
2009-04-28 19:40:14 +02:00
|
|
|
else
|
2015-01-20 15:37:35 +01:00
|
|
|
return print("E: beautiful: error loading theme: no theme specified")
|
2008-08-05 22:21:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-13 11:53:41 +01:00
|
|
|
--- Get the current theme.
|
2014-05-25 14:24:52 +02:00
|
|
|
--
|
|
|
|
-- @treturn table The current theme table.
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.get()
|
2009-04-28 22:54:55 +02:00
|
|
|
return theme
|
2008-11-13 11:53:41 +01:00
|
|
|
end
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
function beautiful.mt:__index(k)
|
|
|
|
return theme[k]
|
|
|
|
end
|
|
|
|
|
2010-10-06 14:18:11 +02:00
|
|
|
-- Set the default font
|
|
|
|
set_font("sans 8")
|
|
|
|
|
2012-06-12 03:24:22 +02:00
|
|
|
return setmetatable(beautiful, beautiful.mt)
|
2008-08-06 15:39:51 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|