2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2009-02-22 16:53:30 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2018-05-12 16:03:00 +00:00
|
|
|
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
|
2009-02-22 16:53:30 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
#include "fonts.h"
|
2009-02-22 16:53:30 +00:00
|
|
|
#include "logging.h"
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
int selected_font = 0;
|
2010-09-08 15:30:04 +00:00
|
|
|
std::vector<font_list> fonts;
|
2009-08-07 07:21:56 +00:00
|
|
|
char fontloaded = 0;
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void font_setting::lua_setter(lua::state &l, bool init) {
|
|
|
|
lua::stack_sentry s(l, -2);
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
Base::lua_setter(l, init);
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (init && out_to_x.get(*state)) {
|
2018-08-03 16:22:11 +00:00
|
|
|
if (fonts.empty()) { fonts.resize(1); }
|
2018-05-12 16:03:00 +00:00
|
|
|
fonts[0].name = do_convert(l, -1).first;
|
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
++s;
|
2010-09-08 15:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
font_setting font;
|
|
|
|
|
|
|
|
#ifdef BUILD_XFT
|
|
|
|
namespace {
|
2018-05-12 16:03:00 +00:00
|
|
|
class xftalpha_setting : public conky::simple_config_setting<float> {
|
2018-05-12 23:26:31 +00:00
|
|
|
using Base = conky::simple_config_setting<float>;
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
protected:
|
2018-05-12 23:26:31 +00:00
|
|
|
void lua_setter(lua::state &l, bool init) override {
|
2018-05-12 16:03:00 +00:00
|
|
|
lua::stack_sentry s(l, -2);
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
Base::lua_setter(l, init);
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
if (init && out_to_x.get(*state)) {
|
|
|
|
fonts[0].font_alpha = do_convert(l, -1).first * 0xffff;
|
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
++s;
|
|
|
|
}
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
public:
|
|
|
|
xftalpha_setting() : Base("xftalpha", 1.0, false) {}
|
|
|
|
};
|
2010-09-08 15:30:04 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
xftalpha_setting xftalpha;
|
|
|
|
} // namespace
|
2010-09-08 15:30:04 +00:00
|
|
|
#endif /* BUILD_XFT */
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
void set_font() {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2018-08-03 16:22:11 +00:00
|
|
|
if (use_xft.get(*state)) { return; }
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2018-08-03 16:22:11 +00:00
|
|
|
if (fonts.size() > selected_font && fonts[selected_font].font != nullptr &&
|
|
|
|
window.gc != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
XSetFont(display, window.gc, fonts[selected_font].font->fid);
|
|
|
|
}
|
2009-06-10 20:08:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
void setup_fonts() {
|
2018-08-03 16:22:11 +00:00
|
|
|
DBGP("setting up fonts");
|
|
|
|
if (!out_to_x.get(*state)) { return; }
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2018-05-12 16:03:00 +00:00
|
|
|
if (use_xft.get(*state)) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (window.xftdraw != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
XftDrawDestroy(window.xftdraw);
|
2018-05-12 23:26:31 +00:00
|
|
|
window.xftdraw = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
window.xftdraw = XftDrawCreate(display, window.drawable, window.visual,
|
|
|
|
window.colourmap);
|
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2018-05-12 16:03:00 +00:00
|
|
|
set_font();
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int add_font(const char *data_in) {
|
2018-08-03 16:22:11 +00:00
|
|
|
if (!out_to_x.get(*state)) { return 0; }
|
2018-05-12 23:26:31 +00:00
|
|
|
fonts.emplace_back();
|
2018-05-12 16:03:00 +00:00
|
|
|
fonts.rbegin()->name = data_in;
|
2009-02-22 16:53:30 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
return fonts.size() - 1;
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:59:07 +00:00
|
|
|
void free_fonts(bool utf8) {
|
2018-08-03 16:22:11 +00:00
|
|
|
if (!out_to_x.get(*state)) { return; }
|
2018-05-12 23:26:31 +00:00
|
|
|
for (auto &font : fonts) {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2018-05-12 16:03:00 +00:00
|
|
|
if (use_xft.get(*state)) {
|
|
|
|
/*
|
|
|
|
* Do we not need to close fonts with Xft? Unsure. Not freeing the
|
|
|
|
* fonts seems to incur a slight memory leak, but it also prevents
|
|
|
|
* a crash.
|
|
|
|
*
|
|
|
|
* XftFontClose(display, fonts[i].xftfont);
|
|
|
|
*/
|
|
|
|
} else
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2018-05-12 16:03:00 +00:00
|
|
|
{
|
2018-08-03 16:22:11 +00:00
|
|
|
if (font.font != nullptr) { XFreeFont(display, font.font); }
|
2018-05-12 23:26:31 +00:00
|
|
|
if (utf8 && (font.fontset != nullptr)) {
|
|
|
|
XFreeFontSet(display, font.fontset);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fonts.clear();
|
|
|
|
selected_font = 0;
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2018-05-12 23:26:31 +00:00
|
|
|
if (window.xftdraw != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
XftDrawDestroy(window.xftdraw);
|
2018-05-12 23:26:31 +00:00
|
|
|
window.xftdraw = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_XFT */
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:59:07 +00:00
|
|
|
void load_fonts(bool utf8) {
|
2018-08-03 16:22:11 +00:00
|
|
|
DBGP("loading fonts");
|
|
|
|
if (!out_to_x.get(*state)) { return; }
|
2018-05-12 23:26:31 +00:00
|
|
|
for (auto &font : fonts) {
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_XFT
|
2018-05-12 16:03:00 +00:00
|
|
|
/* load Xft font */
|
|
|
|
if (use_xft.get(*state)) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (font.xftfont == nullptr) {
|
|
|
|
font.xftfont = XftFontOpenName(display, screen, font.name.c_str());
|
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-08-03 16:22:11 +00:00
|
|
|
if (font.xftfont != nullptr) { continue; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
NORM_ERR("can't load Xft font '%s'", font.name.c_str());
|
|
|
|
if ((font.xftfont = XftFontOpenName(display, screen, "courier-12")) !=
|
|
|
|
nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
CRIT_ERR(nullptr, nullptr, "can't load Xft font '%s'", "courier-12");
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
#endif
|
2018-05-12 23:26:31 +00:00
|
|
|
if (utf8 && font.fontset == nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
char **missing;
|
|
|
|
int missingnum;
|
|
|
|
char *missingdrawn;
|
2018-05-12 23:26:31 +00:00
|
|
|
font.fontset = XCreateFontSet(display, font.name.c_str(), &missing,
|
|
|
|
&missingnum, &missingdrawn);
|
2018-05-12 16:03:00 +00:00
|
|
|
XFreeStringList(missing);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (font.fontset == nullptr) {
|
|
|
|
NORM_ERR("can't load font '%s'", font.name.c_str());
|
|
|
|
font.fontset = XCreateFontSet(display, "fixed", &missing, &missingnum,
|
|
|
|
&missingdrawn);
|
|
|
|
if (font.fontset == nullptr) {
|
|
|
|
CRIT_ERR(nullptr, nullptr, "can't load font '%s'", "fixed");
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* load normal font */
|
2018-05-12 23:26:31 +00:00
|
|
|
if ((font.font == nullptr) &&
|
|
|
|
(font.font = XLoadQueryFont(display, font.name.c_str())) == nullptr) {
|
|
|
|
NORM_ERR("can't load font '%s'", font.name.c_str());
|
|
|
|
if ((font.font = XLoadQueryFont(display, "fixed")) == nullptr) {
|
|
|
|
CRIT_ERR(nullptr, nullptr, "can't load font '%s'", "fixed");
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 16:53:30 +00:00
|
|
|
}
|