1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/src/x11/fonts.cpp

312 lines
9.3 KiB
C++
Raw Normal View History

2016-12-09 03:02:47 -05:00
#include "components/logger.hpp"
2016-12-13 23:13:59 -05:00
#include "errors.hpp"
2016-11-02 15:22:45 -04:00
#include "utils/color.hpp"
#include "utils/factory.hpp"
2016-11-20 17:04:31 -05:00
#include "utils/memory.hpp"
#include "x11/connection.hpp"
#include "x11/draw.hpp"
2016-11-20 17:04:31 -05:00
#include "x11/fonts.hpp"
2016-11-02 15:22:45 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-11-02 15:22:45 -04:00
void font_ref::_deleter::operator()(font_ref* font) {
font->glyph_widths.clear();
font->width_lut.clear();
2016-12-13 23:13:59 -05:00
if (font->xft != nullptr || font->ptr != XCB_NONE) {
auto& conn = connection::make();
if (font->xft != nullptr) {
XftFontClose(conn, font->xft);
}
if (font->ptr != XCB_NONE) {
xcb_close_font(conn, font->ptr);
}
}
delete font;
}
2016-12-13 23:13:59 -05:00
2016-11-20 17:04:31 -05:00
/**
2016-12-09 03:02:47 -05:00
* Create instance
2016-11-20 17:04:31 -05:00
*/
2016-12-09 03:40:46 -05:00
font_manager::make_type font_manager::make() {
return factory_util::unique<font_manager>(connection::make(), logger::make());
2016-11-20 17:04:31 -05:00
}
font_manager::font_manager(connection& conn, const logger& logger)
: m_connection(conn)
, m_logger(logger)
, m_display(m_connection)
, m_visual(m_connection.visual())
, m_colormap(XDefaultColormap(m_display, m_connection.default_screen())) {
if (!XftInit(nullptr) || !XftInitFtLibrary()) {
throw application_error("Could not initialize Xft library");
}
2016-11-02 15:22:45 -04:00
}
2016-11-20 17:04:31 -05:00
font_manager::~font_manager() {
cleanup();
if (m_display) {
if (m_xftcolor_allocated) {
2016-12-21 17:22:02 -05:00
XftColorFree(m_display, m_visual, m_colormap, &m_xftcolor);
2016-12-13 23:13:59 -05:00
}
2016-12-21 17:22:02 -05:00
XFreeColormap(m_display, m_colormap);
2016-12-13 23:13:59 -05:00
}
2016-11-02 15:22:45 -04:00
}
2016-12-21 17:22:02 -05:00
void font_manager::set_visual(Visual* v) {
m_visual = v;
}
void font_manager::cleanup() {
if (m_xftdraw != nullptr) {
XftDrawDestroy(m_xftdraw);
m_xftdraw = nullptr;
}
}
2016-12-20 09:09:11 -05:00
bool font_manager::load(const string& name, uint8_t fontindex, int8_t offset_y) {
if (fontindex > 0 && m_fonts.find(fontindex) != m_fonts.end()) {
2016-11-02 15:22:45 -04:00
m_logger.warn("A font with index '%i' has already been loaded, skip...", fontindex);
return false;
2016-12-20 09:09:11 -05:00
} else if (fontindex == 0) {
2016-11-02 15:22:45 -04:00
fontindex = m_fonts.size();
2016-12-23 09:54:06 -05:00
m_logger.trace("font_manager: Assign font '%s' to index '%u'", name, fontindex);
2016-11-02 15:22:45 -04:00
} else {
2016-12-23 09:54:06 -05:00
m_logger.trace("font_manager: Add font '%s' to index '%u'", name, fontindex);
2016-11-02 15:22:45 -04:00
}
shared_ptr<font_ref> font{new font_ref{}, font_ref::deleter};
2016-11-02 15:22:45 -04:00
font->offset_y = offset_y;
if (open_xcb_font(font, name)) {
2016-12-13 23:13:59 -05:00
m_logger.info("Loaded font (xlfd=%s)", name);
} else if (font->ptr != XCB_NONE) {
m_connection.close_font_checked(font->ptr);
font->ptr = XCB_NONE;
2016-12-13 23:13:59 -05:00
}
if (font->ptr == XCB_NONE &&
2016-12-21 17:22:02 -05:00
(font->xft = XftFontOpenName(m_display, m_connection.default_screen(), name.c_str())) != nullptr) {
font->ascent = font->xft->ascent;
font->descent = font->xft->descent;
font->height = font->ascent + font->descent;
if (font->xft->pattern != nullptr) {
// XftChar8* file;
// XftPatternGetString(font->xft->pattern, "file", 0, &file);
// m_logger.info("Loaded font (pattern=%s, file=%s)", name, file);
m_logger.info("Loaded font (pattern=%s)", name);
2016-12-13 23:13:59 -05:00
} else {
m_logger.info("Loaded font (pattern=%s)", name);
}
}
if (font->ptr == XCB_NONE && font->xft == nullptr) {
2016-11-02 15:22:45 -04:00
return false;
}
m_fonts.emplace(make_pair(fontindex, move(font)));
2016-12-13 23:13:59 -05:00
int max_height{0};
2016-11-02 15:22:45 -04:00
2016-11-25 07:55:15 -05:00
for (auto& iter : m_fonts) {
if (iter.second->height > max_height) {
2016-11-02 15:22:45 -04:00
max_height = iter.second->height;
2016-11-25 07:55:15 -05:00
}
}
2016-11-02 15:22:45 -04:00
for (auto& iter : m_fonts) {
iter.second->height = max_height;
}
return true;
}
2016-12-20 09:09:11 -05:00
void font_manager::fontindex(uint8_t index) {
if ((m_fontindex = index) > 0) {
for (auto&& font : m_fonts) {
if (font.first == index) {
m_fontindex = index;
break;
}
2016-11-21 09:07:00 -05:00
}
}
}
shared_ptr<font_ref> font_manager::match_char(const uint16_t chr) {
2016-12-20 09:09:11 -05:00
if (!m_fonts.empty()) {
if (m_fontindex > 0 && static_cast<size_t>(m_fontindex) <= m_fonts.size()) {
auto iter = m_fonts.find(m_fontindex);
if (iter != m_fonts.end() && iter->second) {
if (has_glyph_xft(iter->second, chr)) {
return iter->second;
} else if (has_glyph_xcb(iter->second, chr)) {
return iter->second;
}
}
2016-11-25 07:55:15 -05:00
}
2016-12-20 09:09:11 -05:00
for (auto&& font : m_fonts) {
if (font.second && has_glyph_xft(font.second, chr)) {
return font.second;
} else if (font.second && has_glyph_xcb(font.second, chr)) {
return font.second;
}
}
2016-11-02 15:22:45 -04:00
}
return {};
2016-11-21 09:07:00 -05:00
}
uint8_t font_manager::glyph_width(const shared_ptr<font_ref>& font, const uint16_t chr) {
if (font && font->xft != nullptr) {
return glyph_width_xft(move(font), chr);
} else if (font && font->ptr != XCB_NONE) {
return glyph_width_xcb(move(font), chr);
} else {
return 0;
}
2016-11-21 09:07:00 -05:00
}
void font_manager::drawtext(const shared_ptr<font_ref>& font, xcb_pixmap_t pm, xcb_gcontext_t gc, int16_t x, int16_t y,
const uint16_t* chars, size_t num_chars) {
if (m_xftdraw == nullptr) {
2016-12-21 17:22:02 -05:00
m_xftdraw = XftDrawCreate(m_display, pm, m_visual, m_colormap);
}
if (font->xft != nullptr) {
XftDrawString16(m_xftdraw, &m_xftcolor, font->xft, x, y, chars, num_chars);
} else if (font->ptr != XCB_NONE) {
2016-12-21 17:22:02 -05:00
vector<uint16_t> ucs(num_chars);
for (size_t i = 0; i < num_chars; i++) {
2016-12-21 17:22:02 -05:00
ucs[i] = (chars[i] >> 8) | (chars[i] << 8);
}
2016-12-21 17:22:02 -05:00
xcb_poly_text_16(pm, gc, x, y, num_chars, ucs.data());
2016-12-13 23:13:59 -05:00
}
2016-11-21 09:07:00 -05:00
}
2016-12-13 23:13:59 -05:00
void font_manager::allocate_color(uint32_t color) {
// clang-format off
XRenderColor x{
color_util::red_channel<uint16_t>(color),
color_util::green_channel<uint16_t>(color),
color_util::blue_channel<uint16_t>(color),
color_util::alpha_channel<uint16_t>(color)};
// clang-format on
2016-12-13 23:13:59 -05:00
allocate_color(x);
2016-11-21 09:07:00 -05:00
}
2016-12-13 23:13:59 -05:00
void font_manager::allocate_color(XRenderColor color) {
if (m_xftcolor_allocated) {
2016-12-21 17:22:02 -05:00
XftColorFree(m_display, m_visual, m_colormap, &m_xftcolor);
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
2016-12-21 17:22:02 -05:00
if (!(m_xftcolor_allocated = XftColorAllocValue(m_display, m_visual, m_colormap, &color, &m_xftcolor))) {
2016-11-02 15:22:45 -04:00
m_logger.err("Failed to allocate color");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
}
bool font_manager::open_xcb_font(const shared_ptr<font_ref>& font, string fontname) {
2016-11-02 15:22:45 -04:00
try {
2016-12-13 23:13:59 -05:00
uint32_t font_id{m_connection.generate_id()};
m_connection.open_font_checked(font_id, fontname);
2016-11-02 15:22:45 -04:00
m_logger.trace("Found X font '%s'", fontname);
font->ptr = font_id;
2016-11-02 15:22:45 -04:00
2016-12-13 23:13:59 -05:00
auto query = m_connection.query_font(font_id);
2016-11-02 15:22:45 -04:00
if (query->char_infos_len == 0) {
2016-11-20 17:04:31 -05:00
m_logger.warn("X font '%s' does not contain any characters... (Verify the XLFD string)", fontname);
2016-11-02 15:22:45 -04:00
return false;
}
font->descent = query->font_descent;
font->height = query->font_ascent + query->font_descent;
font->width = query->max_bounds.character_width;
font->char_max = query->max_byte1 << 8 | query->max_char_or_byte2;
font->char_min = query->min_byte1 << 8 | query->min_char_or_byte2;
2016-11-02 15:22:45 -04:00
auto chars = query.char_infos();
2016-11-25 07:55:15 -05:00
for (auto it = chars.begin(); it != chars.end(); it++) {
font->width_lut.emplace_back(forward<xcb_charinfo_t>(*it));
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
return true;
} catch (const std::exception& e) {
2016-11-20 17:04:31 -05:00
m_logger.trace("font_manager: Could not find X font '%s' (what: %s)", fontname, e.what());
2016-11-02 15:22:45 -04:00
}
return false;
}
uint8_t font_manager::glyph_width_xft(const shared_ptr<font_ref>& font, const uint16_t chr) {
auto it = font->glyph_widths.find(chr);
if (it != font->glyph_widths.end()) {
return it->second;
}
XGlyphInfo extents{};
2016-12-21 17:22:02 -05:00
FT_UInt glyph{XftCharIndex(m_display, font->xft, static_cast<FcChar32>(chr))};
2016-12-21 17:22:02 -05:00
XftFontLoadGlyphs(m_display, font->xft, FcFalse, &glyph, 1);
XftGlyphExtents(m_display, font->xft, &glyph, 1, &extents);
XftFontUnloadGlyphs(m_display, font->xft, &glyph, 1);
font->glyph_widths.emplace_hint(it, chr, extents.xOff); //.emplace_back(chr, extents.xOff);
return extents.xOff;
}
uint8_t font_manager::glyph_width_xcb(const shared_ptr<font_ref>& font, const uint16_t chr) {
if (!font || font->ptr == XCB_NONE) {
return 0;
} else if (static_cast<size_t>(chr - font->char_min) < font->width_lut.size()) {
return font->width_lut[chr - font->char_min].character_width;
} else {
return font->width;
}
}
bool font_manager::has_glyph_xft(const shared_ptr<font_ref>& font, const uint16_t chr) {
if (!font || font->xft == nullptr) {
return false;
2016-12-21 17:22:02 -05:00
} else if (XftCharExists(m_display, font->xft, static_cast<FcChar32>(chr)) == FcFalse) {
return false;
} else {
return true;
}
}
bool font_manager::has_glyph_xcb(const shared_ptr<font_ref>& font, const uint16_t chr) {
if (font->ptr == XCB_NONE) {
return false;
} else if (chr < font->char_min || chr > font->char_max) {
return false;
} else if (static_cast<size_t>(chr - font->char_min) >= font->width_lut.size()) {
return false;
} else if (font->width_lut[chr - font->char_min].character_width == 0) {
return false;
2016-11-02 15:22:45 -04:00
} else {
return true;
}
}
2016-12-21 17:22:02 -05:00
void font_manager::xcb_poly_text_16(
xcb_drawable_t d, xcb_gcontext_t gc, int16_t x, int16_t y, uint8_t len, uint16_t* str) {
static const xcb_protocol_request_t xcb_req = {5, nullptr, XCB_POLY_TEXT_16, 1};
xcb_poly_text_16_request_t req{XCB_POLY_TEXT_16, 0, len, d, gc, x, y};
uint8_t xcb_lendelta[2]{len, 0};
struct iovec xcb_parts[7]{};
xcb_parts[2].iov_base = reinterpret_cast<char*>(&req);
xcb_parts[2].iov_len = sizeof(req);
xcb_parts[3].iov_base = nullptr;
xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;
xcb_parts[4].iov_base = xcb_lendelta;
xcb_parts[4].iov_len = sizeof(xcb_lendelta);
xcb_parts[5].iov_base = reinterpret_cast<char*>(str);
xcb_parts[5].iov_len = len * sizeof(int16_t);
xcb_parts[6].iov_base = nullptr;
xcb_parts[6].iov_len = -(xcb_parts[4].iov_len + xcb_parts[5].iov_len) & 3;
xcb_send_request(m_connection, 0, xcb_parts + 2, &xcb_req);
}
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END