From 8717bae5f4a141911ba58b34b6ac630780e089ed Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 6 Sep 2016 14:24:41 +0200 Subject: Fix another crash in the font renderer. --- plugingui/font.cc | 18 ++++++++++++++---- plugingui/font.h | 4 ++-- test/paintertest.cc | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/plugingui/font.cc b/plugingui/font.cc index a9b102e..a56d392 100644 --- a/plugingui/font.cc +++ b/plugingui/font.cc @@ -44,8 +44,12 @@ Font::Font(const std::string& fontfile) if(c > 0) { - characters[c - 1].width = - character.offset - characters[c - 1].offset - 1; + assert(character.offset >= characters[c - 1].offset); + characters[c - 1].width = character.offset - characters[c - 1].offset; + if(characters[c].offset != characters[c - 1].offset) + { + --characters[c - 1].width; + } } ++px; @@ -69,7 +73,12 @@ Font::Font(const std::string& fontfile) ++c; - characters[c - 1].width = characters[c].offset - characters[c - 1].offset - 1; + assert(characters[c].offset >= characters[c - 1].offset); + characters[c - 1].width = characters[c].offset - characters[c - 1].offset; + if(characters[c].offset != characters[c - 1].offset) + { + --characters[c - 1].width; + } } size_t Font::textWidth(const std::string& text) const @@ -106,8 +115,9 @@ PixelBufferAlpha *Font::render(const std::string& text) const new PixelBufferAlpha(textWidth(text), textHeight(text)); int x_offset = 0; - for(unsigned char cha : text) + for(std::size_t i = 0; i < text.length(); ++i) { + unsigned char cha = text[i]; auto& character = characters.at(cha); for(size_t x = 0; x < character.width; ++x) { diff --git a/plugingui/font.h b/plugingui/font.h index e18f1bd..84e1f63 100644 --- a/plugingui/font.h +++ b/plugingui/font.h @@ -51,8 +51,8 @@ private: class Character { public: - int offset{0}; - size_t width{0}; + std::size_t offset{0}; + std::size_t width{0}; int pre_bias{0}; int post_bias{0}; }; diff --git a/test/paintertest.cc b/test/paintertest.cc index 69b28ff..f1c2779 100644 --- a/test/paintertest.cc +++ b/test/paintertest.cc @@ -117,7 +117,7 @@ public: // Success criterion is simply to not assert in the drawing routines... GUI::Font font; // a string with unicode characters - std::string someText = "Hello World - лæ"; + std::string someText = "Hello World - лæ Библиотека"; std::size_t width = font.textWidth(someText); std::size_t height = font.textHeight(someText); -- cgit v1.2.3