diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-12-19 19:57:17 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-12-19 19:57:17 +0100 |
commit | fde590274c9d99e2cabc5b5f9971f2051659f1aa (patch) | |
tree | 109ab6ece65d7fd3d24c75329b05389b34b86dce | |
parent | 180361cda94f612d3ebfd2c460c24b457fae64d3 (diff) |
Add spacing control method for fonts and modernize loops.
-rw-r--r-- | plugingui/font.cc | 23 | ||||
-rw-r--r-- | 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<Character, 255> characters; + int spacing{1}; }; } // GUI:: |