From 1f37a5da7270c804c1d164ab950d1a482861784b Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Fri, 10 May 2019 12:09:36 +0200 Subject: [PATCH 1/2] Use Ogre::UTFString to convert utf8-encoded std::string to UTF32 required by Ogre::Font --- src/rviz/ogre_helpers/movable_text.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/rviz/ogre_helpers/movable_text.cpp b/src/rviz/ogre_helpers/movable_text.cpp index 4ef34034df..63b4335322 100644 --- a/src/rviz/ogre_helpers/movable_text.cpp +++ b/src/rviz/ogre_helpers/movable_text.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include @@ -218,17 +219,17 @@ void MovableText::showOnTop(bool show) void MovableText::_setupGeometry() { + Ogre::UTFString::utf32string utfCaption(Ogre::UTFString(mCaption).asUTF32()); + assert(mpFont); assert(!mpMaterial.isNull()); unsigned int vertexCount = 0; //count letters to determine how many vertices are needed - std::string::iterator i = mCaption.begin(); - std::string::iterator iend = mCaption.end(); - for ( ; i != iend; ++i ) + for (auto ch : utfCaption) { - if ((*i != ' ') && (*i != '\n')) + if ((ch != ' ') && (ch != '\n')) { vertexCount += 6; } @@ -241,7 +242,7 @@ void MovableText::_setupGeometry() mUpdateColors = true; } - if (mCaption.empty()) + if (utfCaption.empty()) { return; } @@ -298,11 +299,9 @@ void MovableText::_setupGeometry() float total_height = mCharHeight; float total_width = 0.0f; float current_width = 0.0f; - i = mCaption.begin(); - iend = mCaption.end(); - for ( ; i != iend; ++i ) + for (auto ch : utfCaption) { - if (*i == '\n') + if (ch == '\n') { total_height += mCharHeight + mLineSpacing; @@ -312,13 +311,13 @@ void MovableText::_setupGeometry() } current_width = 0.0; } - else if (*i == ' ') + else if (ch == ' ') { current_width += spaceWidth; } else { - current_width += mpFont->getGlyphAspectRatio(*i) * mCharHeight * 2.0; + current_width += mpFont->getGlyphAspectRatio(ch) * mCharHeight * 2.0; } } @@ -360,12 +359,13 @@ void MovableText::_setupGeometry() Ogre::Vector3 min(9999999.0f), max(-9999999.0f), currPos(0.0f); Ogre::Real maxSquaredRadius = -99999999.0f; float largestWidth = 0.0f; - for (i = mCaption.begin(); i != iend; ++i) + auto iend = utfCaption.end(); + for (auto i = utfCaption.begin(); i != iend; ++i) { if (newLine) { len = 0.0f; - for (String::iterator j = i; j != iend && *j != '\n'; j++) + for (auto j = i; j != iend && *j != '\n'; j++) { if (*j == ' ') len += spaceWidth; From 1eb71e03e9e0b7e9a05c76f30dfb56e789eeee94 Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Fri, 10 May 2019 12:32:38 +0200 Subject: [PATCH 2/2] enable utf codepoint range 0-999 --- src/rviz/ogre_helpers/movable_text.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rviz/ogre_helpers/movable_text.cpp b/src/rviz/ogre_helpers/movable_text.cpp index 63b4335322..652b1cc49e 100644 --- a/src/rviz/ogre_helpers/movable_text.cpp +++ b/src/rviz/ogre_helpers/movable_text.cpp @@ -116,6 +116,8 @@ void MovableText::setFontName(const String &fontName) throw Exception(Exception::ERR_ITEM_NOT_FOUND, "Could not find font " + fontName, "MovableText::setFontName"); + // to support non-ascii letters, setup the codepoint range before loading + mpFont->addCodePointRange(std::make_pair(0, 999)); mpFont->load(); if (!mpMaterial.isNull()) {