1 ----------------------------------------------------------------------------
2 -- @author Damien Leone <damien.leone@gmail.com>
3 -- @author Julien Danjou <julien@danjou.info>
4 -- @copyright 2008-2009 Damien Leone, Julien Danjou
5 -- @release @AWESOME_VERSION@
6 ----------------------------------------------------------------------------
15 local setmetatable
= setmetatable
16 local util
= require("awful.util")
17 local lgi
= require("lgi")
18 local cairo
= lgi
.cairo
19 local Pango
= lgi
.Pango
20 local PangoCairo
= lgi
.PangoCairo
28 local beautiful
= { mt
= {} }
32 local descs
= setmetatable({}, { __mode
= 'k' })
33 local fonts
= setmetatable({}, { __mode
= 'v' })
36 local function load_font(name
)
37 name
= name
or active_font
38 if name
and type(name
) ~= "string" and descs
[name
] then
46 local desc
= Pango
.FontDescription
.from_string(name
)
47 local ctx
= PangoCairo
.font_map_get_default():create_context()
49 -- Apply default values from the context (e.g. a default font size)
50 desc
:merge(ctx
:get_font_description(), false)
52 -- Calculate fond height
53 local metrics
= ctx
:get_metrics(desc
, nil)
54 local height
= math
.ceil((metrics
:get_ascent() + metrics
:get_descent()) / Pango
.SCALE
)
56 local font
= { name
= name
, description
= desc
, height
= height
}
62 local function set_font(name
)
63 active_font
= load_font(name
).name
66 function beautiful
.get_font(name
)
67 return load_font(name
).description
70 function beautiful
.get_font_height(name
)
71 return load_font(name
).height
74 --- Init function, should be runned at the beginning of configuration file.
75 -- @param path The theme file path.
76 function beautiful
.init(path
)
80 -- try and grab user's $HOME directory and expand '~'
81 local homedir
= os
.getenv("HOME")
82 path
= path
:gsub("^~/", homedir
.. "/")
84 success
, theme
= pcall(function() return dofile(path
) end)
87 return print("E: beautiful: error loading theme file " .. theme
)
91 for k
, v
in pairs(theme
) do
92 if type(v
) == "string" then theme
[k
] = v
:gsub("^~/", homedir
.. "/") end
96 if theme
.font
then set_font(theme
.font
) end
98 return print("E: beautiful: error loading theme file " .. path
)
101 return print("E: beautiful: error loading theme: no path specified")
105 --- Get the current theme.
106 -- @return The current theme table.
107 function beautiful
.get()
111 function beautiful
.mt
:__index(k
)
115 -- Set the default font
118 return setmetatable(beautiful
, beautiful
.mt
)
120 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80