refactor: Font index cleanup

This commit is contained in:
Michael Carlberg 2016-12-20 15:09:11 +01:00
parent 8ef8900ca2
commit c14c46b998
8 changed files with 40 additions and 49 deletions

View File

@ -33,7 +33,7 @@ class parser {
size_t text(string&& data);
uint32_t parse_color(const string& s, uint32_t fallback = 0);
int8_t parse_fontindex(const string& s);
uint8_t parse_fontindex(const string& s);
attribute parse_attr(const char attr);
mousebtn parse_action_btn(const string& data);
string parse_action_cmd(string&& data);

View File

@ -47,7 +47,7 @@ class renderer
void set_foreground(const uint32_t color);
void set_underline(const uint32_t color);
void set_overline(const uint32_t color);
void set_fontindex(const int8_t font);
void set_fontindex(const uint8_t font);
void set_alignment(const alignment align);
void set_attribute(const attribute attr, const bool state);
void toggle_attribute(const attribute attr);
@ -123,7 +123,7 @@ class renderer
alignment m_alignment{alignment::NONE};
map<gc, uint32_t> m_colors;
uint8_t m_attributes{0U};
int8_t m_fontindex{DEFAULT_FONT_INDEX};
uint8_t m_fontindex{0};
xcb_font_t m_gcfont{XCB_NONE};
};

View File

@ -61,7 +61,6 @@ static constexpr const char* PATH_MEMORY_INFO{"@SETTING_PATH_MEMORY_INFO@"};
static constexpr const char* PATH_MESSAGING_FIFO{"@SETTING_PATH_MESSAGING_FIFO@"};
static constexpr const char* PATH_TEMPERATURE_INFO{"@SETTING_PATH_TEMPERATURE_INFO@"};
static const int8_t DEFAULT_FONT_INDEX{-1};
static constexpr const char* BUILDER_SPACE_TOKEN{"%__"};
auto version_details = [](const std::vector<std::string>& args) {

View File

@ -117,7 +117,7 @@ namespace signals {
DEFINE_VALUE_SIGNAL(71, change_foreground, uint32_t);
DEFINE_VALUE_SIGNAL(72, change_underline, uint32_t);
DEFINE_VALUE_SIGNAL(73, change_overline, uint32_t);
DEFINE_VALUE_SIGNAL(74, change_font, int8_t);
DEFINE_VALUE_SIGNAL(74, change_font, uint8_t);
DEFINE_VALUE_SIGNAL(75, change_alignment, alignment);
DEFINE_VALUE_SIGNAL(76, offset_pixel, int16_t);
DEFINE_VALUE_SIGNAL(77, attribute_set, attribute);

View File

@ -58,8 +58,8 @@ class font_manager {
void set_visual(shared_ptr<Visual>&& v);
void cleanup();
bool load(const string& name, int8_t fontindex = DEFAULT_FONT_INDEX, int8_t offset_y = 0);
void set_preferred_font(int8_t index);
bool load(const string& name, uint8_t fontindex = 0, int8_t offset_y = 0);
void fontindex(uint8_t index);
shared_ptr<font_ref> match_char(const uint16_t chr);
uint8_t glyph_width(const shared_ptr<font_ref>& font, const uint16_t chr);
void drawtext(const shared_ptr<font_ref>& font, xcb_pixmap_t pm, xcb_gcontext_t gc, int16_t x, int16_t y,
@ -88,7 +88,7 @@ class font_manager {
Colormap m_colormap;
map<uint8_t, shared_ptr<font_ref>> m_fonts{};
int8_t m_fontindex{DEFAULT_FONT_INDEX};
uint8_t m_fontindex{0};
XftDraw* m_xftdraw{nullptr};
XftColor m_xftcolor{};

View File

@ -227,15 +227,15 @@ uint32_t parser::parse_color(const string& s, uint32_t fallback) {
/**
* Process font index and convert it to the correct value
*/
int8_t parser::parse_fontindex(const string& s) {
uint8_t parser::parse_fontindex(const string& s) {
if (s.empty() || s[0] == '-') {
return -1;
return 0;
}
try {
return std::stoul(s, nullptr, 10);
} catch (const std::invalid_argument& err) {
return -1;
return 0;
}
}

View File

@ -566,13 +566,13 @@ bool renderer::on(const change_overline& evt) {
}
bool renderer::on(const change_font& evt) {
int8_t font{*evt()};
uint8_t font{*evt()};
if (m_fontindex == font) {
m_log.trace_x("renderer: ignoring unchanged font index(%i)", static_cast<int8_t>(font));
m_log.trace_x("renderer: ignoring unchanged font index(%i)", static_cast<uint8_t>(font));
} else {
m_log.trace_x("renderer: set_fontindex(%i)", static_cast<int8_t>(font));
m_fontmanager->set_preferred_font(font);
m_log.trace_x("renderer: fontindex(%i)", static_cast<uint8_t>(font));
m_fontmanager->fontindex(font);
m_fontindex = font;
}

View File

@ -67,11 +67,11 @@ void font_manager::cleanup() {
}
}
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()) {
bool font_manager::load(const string& name, uint8_t fontindex, int8_t offset_y) {
if (fontindex > 0 && m_fonts.find(fontindex) != m_fonts.end()) {
m_logger.warn("A font with index '%i' has already been loaded, skip...", fontindex);
return false;
} else if (fontindex == DEFAULT_FONT_INDEX) {
} else if (fontindex == 0) {
fontindex = m_fonts.size();
m_logger.trace("font_manager: Assign font '%s' to index '%d'", name.c_str(), fontindex);
} else {
@ -126,43 +126,35 @@ bool font_manager::load(const string& name, int8_t fontindex, int8_t offset_y) {
return true;
}
void font_manager::set_preferred_font(int8_t index) {
if (index <= 0) {
m_fontindex = DEFAULT_FONT_INDEX;
return;
}
for (auto&& font : m_fonts) {
if (font.first == index) {
m_fontindex = index;
break;
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;
}
}
}
}
shared_ptr<font_ref> font_manager::match_char(const uint16_t chr) {
if (m_fonts.empty()) {
return {};
}
if (m_fontindex != DEFAULT_FONT_INDEX && static_cast<size_t>(m_fontindex) <= m_fonts.size()) {
auto iter = m_fonts.find(m_fontindex);
if (iter == m_fonts.end() || !iter->second) {
return {};
} else if (has_glyph_xft(iter->second, chr)) {
return iter->second;
} else if (has_glyph_xcb(iter->second, chr)) {
return iter->second;
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;
}
}
}
}
for (auto&& font : m_fonts) {
if (!font.second) {
return {};
} else if (has_glyph_xft(font.second, chr)) {
return font.second;
} else if (has_glyph_xcb(font.second, chr)) {
return font.second;
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;
}
}
}