From fde590274c9d99e2cabc5b5f9971f2051659f1aa Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Dec 2015 19:57:17 +0100 Subject: Add spacing control method for fonts and modernize loops. --- plugingui/font.cc | 23 ++++++++++++++--------- plugingui/font.h | 15 ++++++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/plugingui/font.cc b/plugingui/font.cc index 8cc0a4f..bcaa027 100644 --- a/plugingui/font.cc +++ b/plugingui/font.cc @@ -38,8 +38,6 @@ Font::Font(const std::string& fontfile) { auto& character = characters[c]; character.offset = px + 1; - character.pre_bias = 0; - character.post_bias = 0; if(c > 0) { @@ -76,11 +74,10 @@ size_t Font::textWidth(const std::string& text) const { size_t len = 0; - for(size_t i = 0; i < text.length(); i++) + for(auto cha : text) { - unsigned int cha = text[i]; auto& character = characters[cha]; - len += character.width + 1 + character.post_bias; + len += character.width + spacing + character.post_bias; } return len; @@ -91,16 +88,24 @@ size_t Font::textHeight(const std::string& text) const return img_font.height(); } +void Font::setLetterSpacing(int letterSpacing) +{ + spacing = letterSpacing; +} + +int Font::letterSpacing() const +{ + return spacing; +} + PixelBufferAlpha *Font::render(const std::string& text) const { - int border = 1; PixelBufferAlpha *pb = new PixelBufferAlpha(textWidth(text), textHeight(text)); int x_offset = 0; - for(size_t i = 0; i < text.length(); ++i) + for(auto cha : text) { - unsigned int cha = text[i]; auto& character = characters[cha]; for(size_t x = 0; x < character.width; ++x) { @@ -111,7 +116,7 @@ PixelBufferAlpha *Font::render(const std::string& text) const c.red * 255, c.green * 255, c.blue * 255, c.alpha * 255); } } - x_offset += character.width + border + character.post_bias; + x_offset += character.width + spacing + character.post_bias; } return pb; diff --git a/plugingui/font.h b/plugingui/font.h index d350481..e29528d 100644 --- a/plugingui/font.h +++ b/plugingui/font.h @@ -41,19 +41,24 @@ public: size_t textWidth(const std::string& text) const; size_t textHeight(const std::string& text = "") const; + void setLetterSpacing(int letterSpacing); + int letterSpacing() const; + PixelBufferAlpha *render(const std::string& text) const; private: Image img_font; - struct Character { - int offset = 0; - size_t width = 0; - int pre_bias = 0; - int post_bias = 0; + class Character { + public: + int offset{0}; + size_t width{0}; + int pre_bias{0}; + int post_bias{0}; }; std::array characters; + int spacing{1}; }; } // GUI:: -- cgit v1.2.3