From b15870b7cb63316060a5ff565cf980a981633135 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 6 Sep 2016 11:23:43 +0200 Subject: Fix crash when trying to display non ascii characters. --- plugingui/font.cc | 15 +++++++++------ test/gui.cc | 45 --------------------------------------------- test/paintertest.cc | 3 ++- 3 files changed, 11 insertions(+), 52 deletions(-) delete mode 100644 test/gui.cc diff --git a/plugingui/font.cc b/plugingui/font.cc index 3be7dd5..a9b102e 100644 --- a/plugingui/font.cc +++ b/plugingui/font.cc @@ -26,13 +26,16 @@ */ #include "font.h" -namespace GUI { +#include + +namespace GUI +{ Font::Font(const std::string& fontfile) : img_font(fontfile) { - size_t px = 0; - size_t c; + std::size_t px = 0; + std::size_t c; for(c = 0; c < (characters.size() - 1) && px < img_font.width(); ++c) { @@ -73,7 +76,7 @@ size_t Font::textWidth(const std::string& text) const { size_t len = 0; - for(auto cha : text) + for(unsigned char cha : text) { auto& character = characters[cha]; len += character.width + spacing + character.post_bias; @@ -103,9 +106,9 @@ PixelBufferAlpha *Font::render(const std::string& text) const new PixelBufferAlpha(textWidth(text), textHeight(text)); int x_offset = 0; - for(auto cha : text) + for(unsigned char cha : text) { - auto& character = characters[cha]; + auto& character = characters.at(cha); for(size_t x = 0; x < character.width; ++x) { for(size_t y = 0; y < img_font.height(); ++y) diff --git a/test/gui.cc b/test/gui.cc deleted file mode 100644 index 59e98b0..0000000 --- a/test/gui.cc +++ /dev/null @@ -1,45 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * gui.cc - * - * Fri Nov 29 18:08:57 CET 2013 - * Copyright 2013 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of DrumGizmo. - * - * DrumGizmo is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * DrumGizmo is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with DrumGizmo; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include - -class test_gui : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE(test_gui); - CPPUNIT_TEST(test1); - CPPUNIT_TEST_SUITE_END(); - -public: - void setUp() {} - void tearDown() {} - - void test1() { - } -}; - -// Registers the fixture into the 'registry' -CPPUNIT_TEST_SUITE_REGISTRATION(test_gui); - diff --git a/test/paintertest.cc b/test/paintertest.cc index 5c8cf45..69b28ff 100644 --- a/test/paintertest.cc +++ b/test/paintertest.cc @@ -116,7 +116,8 @@ public: { // Success criterion is simply to not assert in the drawing routines... GUI::Font font; - std::string someText = "Hello World"; + // a string with unicode characters + std::string someText = "Hello World - лæ"; std::size_t width = font.textWidth(someText); std::size_t height = font.textHeight(someText); -- cgit v1.2.3