polybar/src/x11/fonts.cpp

248 lines
7.2 KiB
C++
Raw Normal View History

2016-11-20 22:04:31 +00:00
#include <X11/Xlib-xcb.h>
2016-11-02 19:22:45 +00:00
#include "utils/color.hpp"
2016-11-20 22:04:31 +00:00
#include "utils/memory.hpp"
#include "x11/connection.hpp"
#include "x11/fonts.hpp"
#include "x11/xlib.hpp"
2016-11-26 05:13:20 +00:00
#include "x11/xutils.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
2016-11-20 22:04:31 +00:00
/**
* Configure injection module
*/
di::injector<unique_ptr<font_manager>> configure_font_manager() {
return di::make_injector(configure_connection(), configure_logger());
}
2016-11-02 19:22:45 +00:00
array<char, XFT_MAXCHARS> xft_widths;
array<wchar_t, XFT_MAXCHARS> xft_chars;
2016-11-20 22:04:31 +00:00
void fonttype_deleter::operator()(fonttype* f) {
2016-11-25 12:55:15 +00:00
if (f->xft != nullptr) {
2016-11-20 22:04:31 +00:00
XftFontClose(xlib::get_display(), f->xft);
2016-11-25 12:55:15 +00:00
} else {
2016-11-20 22:04:31 +00:00
xcb_close_font(xutils::get_connection(), f->ptr);
2016-11-25 12:55:15 +00:00
}
2016-11-20 22:04:31 +00:00
}
font_manager::font_manager(connection& conn, const logger& logger) : m_connection(conn), m_logger(logger) {
2016-11-02 19:22:45 +00:00
m_display = xlib::get_display();
m_visual = xlib::get_visual(conn.default_screen());
m_colormap = xlib::create_colormap(conn.default_screen());
}
2016-11-20 22:04:31 +00:00
font_manager::~font_manager() {
2016-11-02 19:22:45 +00:00
XftColorFree(m_display, m_visual, m_colormap, &m_xftcolor);
XFreeColormap(m_display, m_colormap);
m_fonts.clear();
}
2016-11-25 12:55:15 +00:00
bool font_manager::load(const string& name, int8_t fontindex, int8_t offset_y) {
if (fontindex != DEFAULT_FONT_INDEX && m_fonts.find(fontindex) != m_fonts.end()) {
2016-11-02 19:22:45 +00:00
m_logger.warn("A font with index '%i' has already been loaded, skip...", fontindex);
return false;
} else if (fontindex == DEFAULT_FONT_INDEX) {
2016-11-02 19:22:45 +00:00
fontindex = m_fonts.size();
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Assign font '%s' to index '%d'", name.c_str(), fontindex);
2016-11-02 19:22:45 +00:00
} else {
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Add font '%s' to index '%i'", name, fontindex);
2016-11-02 19:22:45 +00:00
}
m_fonts.emplace(make_pair(fontindex, font_t{new fonttype(), fonttype_deleter{}}));
m_fonts[fontindex]->offset_y = offset_y;
m_fonts[fontindex]->ptr = 0;
m_fonts[fontindex]->xft = nullptr;
if (open_xcb_font(m_fonts[fontindex], name)) {
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Successfully loaded X font '%s'", name);
2016-11-02 19:22:45 +00:00
} else if ((m_fonts[fontindex]->xft = XftFontOpenName(m_display, 0, name.c_str())) != nullptr) {
m_fonts[fontindex]->ptr = 0;
m_fonts[fontindex]->ascent = m_fonts[fontindex]->xft->ascent;
m_fonts[fontindex]->descent = m_fonts[fontindex]->xft->descent;
m_fonts[fontindex]->height = m_fonts[fontindex]->ascent + m_fonts[fontindex]->descent;
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Successfully loaded Freetype font '%s'", name);
2016-11-02 19:22:45 +00:00
} else {
return false;
}
int max_height = 0;
2016-11-25 12:55:15 +00:00
for (auto& iter : m_fonts) {
if (iter.second->height > max_height) {
2016-11-02 19:22:45 +00:00
max_height = iter.second->height;
2016-11-25 12:55:15 +00:00
}
}
2016-11-02 19:22:45 +00:00
for (auto& iter : m_fonts) {
iter.second->height = max_height;
}
return true;
}
2016-11-21 14:07:00 +00:00
void font_manager::set_preferred_font(int8_t index) {
if (index <= 0) {
m_fontindex = DEFAULT_FONT_INDEX;
2016-11-21 14:07:00 +00:00
return;
}
for (auto&& font : m_fonts) {
if (font.first == index) {
m_fontindex = index;
break;
}
}
}
2016-11-20 22:04:31 +00:00
font_t& font_manager::match_char(uint16_t chr) {
2016-11-02 19:22:45 +00:00
static font_t notfound;
if (!m_fonts.empty()) {
if (m_fontindex != DEFAULT_FONT_INDEX && size_t(m_fontindex) <= m_fonts.size()) {
2016-11-02 19:22:45 +00:00
auto iter = m_fonts.find(m_fontindex);
2016-11-25 12:55:15 +00:00
if (iter != m_fonts.end() && has_glyph(iter->second, chr)) {
2016-11-02 19:22:45 +00:00
return iter->second;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
for (auto& font : m_fonts) {
2016-11-25 12:55:15 +00:00
if (has_glyph(font.second, chr)) {
2016-11-02 19:22:45 +00:00
return font.second;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
}
return notfound;
}
2016-11-21 14:07:00 +00:00
uint8_t font_manager::char_width(font_t& font, uint16_t chr) {
2016-11-25 12:55:15 +00:00
if (!font) {
2016-11-02 19:22:45 +00:00
return 0;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
if (font->xft == nullptr) {
2016-11-25 12:55:15 +00:00
if (static_cast<size_t>(chr - font->char_min) < font->width_lut.size()) {
2016-11-02 19:22:45 +00:00
return font->width_lut[chr - font->char_min].character_width;
2016-11-25 12:55:15 +00:00
} else {
2016-11-02 19:22:45 +00:00
return font->width;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
auto index = chr % XFT_MAXCHARS;
2016-11-25 12:55:15 +00:00
while (xft_chars[index] != 0 && xft_chars[index] != chr) {
index = (index + 1) % XFT_MAXCHARS;
}
2016-11-02 19:22:45 +00:00
if (!xft_chars[index]) {
XGlyphInfo gi;
FT_UInt glyph = XftCharIndex(m_display, font->xft, (FcChar32)chr);
XftFontLoadGlyphs(m_display, font->xft, FcFalse, &glyph, 1);
XftGlyphExtents(m_display, font->xft, &glyph, 1, &gi);
XftFontUnloadGlyphs(m_display, font->xft, &glyph, 1);
xft_chars[index] = chr;
xft_widths[index] = gi.xOff;
return gi.xOff;
} else if (xft_chars[index] == chr) {
return xft_widths[index];
}
return 0;
}
2016-11-20 22:04:31 +00:00
XftColor font_manager::xftcolor() {
2016-11-02 19:22:45 +00:00
return m_xftcolor;
}
2016-11-21 14:07:00 +00:00
XftDraw* font_manager::xftdraw() {
return m_xftdraw;
}
XftDraw* font_manager::create_xftdraw(xcb_pixmap_t pm, xcb_colormap_t cm) {
m_xftdraw = XftDrawCreate(xlib::get_display(), pm, xlib::get_visual(), cm);
return m_xftdraw;
}
void font_manager::destroy_xftdraw() {
XftDrawDestroy(m_xftdraw);
}
void font_manager::allocate_color(uint32_t color, bool initial_alloc) {
XRenderColor x;
x.red = color_util::red_channel<uint16_t>(color);
x.green = color_util::green_channel<uint16_t>(color);
x.blue = color_util::blue_channel<uint16_t>(color);
x.alpha = color_util::alpha_channel<uint16_t>(color);
allocate_color(x, initial_alloc);
}
2016-11-20 22:04:31 +00:00
void font_manager::allocate_color(XRenderColor color, bool initial_alloc) {
2016-11-25 12:55:15 +00:00
if (!initial_alloc) {
2016-11-02 19:22:45 +00:00
XftColorFree(m_display, m_visual, m_colormap, &m_xftcolor);
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
if (!XftColorAllocValue(m_display, m_visual, m_colormap, &color, &m_xftcolor)) {
2016-11-02 19:22:45 +00:00
m_logger.err("Failed to allocate color");
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
}
2016-11-21 14:07:00 +00:00
void font_manager::set_gcontext_font(xcb_gcontext_t gc, xcb_font_t font) {
2016-11-02 19:22:45 +00:00
const uint32_t values[1]{font};
m_connection.change_gc(gc, XCB_GC_FONT, values);
}
2016-11-20 22:04:31 +00:00
bool font_manager::open_xcb_font(font_t& fontptr, string fontname) {
2016-11-02 19:22:45 +00:00
try {
font xfont(m_connection, m_connection.generate_id());
m_connection.open_font_checked(xfont, fontname);
m_logger.trace("Found X font '%s'", fontname);
auto query = m_connection.query_font(xfont);
if (query->char_infos_len == 0) {
2016-11-20 22:04:31 +00:00
m_logger.warn("X font '%s' does not contain any characters... (Verify the XLFD string)", fontname);
2016-11-02 19:22:45 +00:00
return false;
}
fontptr->descent = query->font_descent;
fontptr->height = query->font_ascent + query->font_descent;
fontptr->width = query->max_bounds.character_width;
fontptr->char_max = query->max_byte1 << 8 | query->max_char_or_byte2;
fontptr->char_min = query->min_byte1 << 8 | query->min_char_or_byte2;
auto chars = query.char_infos();
2016-11-25 12:55:15 +00:00
for (auto it = chars.begin(); it != chars.end(); it++) {
2016-11-02 19:22:45 +00:00
fontptr->width_lut.emplace_back(forward<xcb_charinfo_t>(*it));
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
fontptr->ptr = xfont;
return true;
} catch (const xpp::x::error::name& e) {
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Could not find X font '%s'", fontname);
2016-11-02 19:22:45 +00:00
} catch (const shared_ptr<xcb_generic_error_t>& e) {
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Could not find X font '%s'", fontname);
2016-11-02 19:22:45 +00:00
} catch (const std::exception& e) {
2016-11-20 22:04:31 +00:00
m_logger.trace("font_manager: Could not find X font '%s' (what: %s)", fontname, e.what());
2016-11-02 19:22:45 +00:00
}
return false;
}
2016-11-20 22:04:31 +00:00
bool font_manager::has_glyph(font_t& font, uint16_t chr) {
2016-11-02 19:22:45 +00:00
if (font->xft != nullptr) {
2016-11-25 12:55:15 +00:00
return static_cast<bool>(XftCharExists(m_display, font->xft, (FcChar32)chr));
2016-11-02 19:22:45 +00:00
} else {
2016-11-25 12:55:15 +00:00
if (chr < font->char_min || chr > font->char_max) {
2016-11-02 19:22:45 +00:00
return false;
2016-11-25 12:55:15 +00:00
}
if (static_cast<size_t>(chr - font->char_min) >= font->width_lut.size()) {
2016-11-02 19:22:45 +00:00
return false;
2016-11-25 12:55:15 +00:00
}
if (font->width_lut[chr - font->char_min].character_width == 0) {
2016-11-02 19:22:45 +00:00
return false;
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
return true;
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END