From 1b08b02df2130b0ffc6af78ff10200e0a3594cd2 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 10 Feb 2003 15:01:27 +0000 Subject: [PATCH] From Tree, added support for using wchar_t strings with osg::Text/EncodedText. --- include/osgText/EncodedText | 11 +++++++---- include/osgText/Text | 1 + src/osgText/EncodedText.cpp | 25 +++++++++++++++++++++++++ src/osgText/Text.cpp | 10 ++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/osgText/EncodedText b/include/osgText/EncodedText index 1ffa9d7af..ee39b8751 100644 --- a/include/osgText/EncodedText +++ b/include/osgText/EncodedText @@ -33,10 +33,10 @@ #define OSGTEXT_ENCODEDTEXT 1 #include +#include #include - -#include +#include namespace osgText { @@ -67,13 +67,16 @@ class OSGTEXT_EXPORT EncodedText : public osg::Referenced Encoding getOverrideEncoding() const { return _overrideEncoding; } Encoding getEncoding() const { return _encoding; } - void setText(const unsigned char* text, int length = -1); - std::vector::const_iterator begin() const { return _unicodeText.begin(); } std::vector::const_iterator end() const { return _unicodeText.end(); } protected: + friend class Text; + + std::string convertWideString(const char* text); + std::string convertWideString(const wchar_t* text); + void setText(const unsigned char* text, int length = -1); int getNextCharacter(const unsigned char*& charString) const; /// This method will extract any ZWNBSP signature at the start of the string diff --git a/include/osgText/Text b/include/osgText/Text index a4a10fb87..96d684ccc 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -123,6 +123,7 @@ class OSGTEXT_EXPORT Text : public osg::Drawable void setText(const char* text); void setText(const std::string& text); const std::string& getText() const { return _text; } + void setText(const wchar_t* text); virtual bool supports(PrimitiveFunctor& pf) const; virtual void accept(PrimitiveFunctor& pf) const; diff --git a/src/osgText/EncodedText.cpp b/src/osgText/EncodedText.cpp index 8d8846cbe..929fb12bf 100644 --- a/src/osgText/EncodedText.cpp +++ b/src/osgText/EncodedText.cpp @@ -231,3 +231,28 @@ void EncodedText::setOverrideEncoding(EncodedText::Encoding encoding) //NB As the original text is not cached we cannot confirm any ENCODING_SIGNATURE until text is set again } } + +std::string EncodedText::convertWideString(const wchar_t* text) +{ + std::string utf8string; + const wchar_t* pChars = text; + int currentChar = *pChars; + while (currentChar) + { + if (currentChar < 0x80) + utf8string+=(char)currentChar; + else if (currentChar < 0x800) + { + utf8string+=(char)(0xc0 | (currentChar>>6)); + utf8string+=(char)(0x80 | currentChar & 0x3f); + } + else + { + utf8string+=(char)(0xe0 | (currentChar>>12)); + utf8string+=(char)(0x80 | (currentChar>>6) & 0x3f); + utf8string+=(char)(0x80 | currentChar & 0x3f); + } + currentChar = *(++pChars); + } + return utf8string; +} diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 9443ac2cd..b6f58ac45 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -545,5 +545,15 @@ setText(const std::string& text) _initAlignment=false; _encodedText->setText((const unsigned char*)_text.data(),_text.size()); } + +void Text:: +setText(const wchar_t* text) +{ + _encodedText->setOverrideEncoding(EncodedText::ENCODING_UTF8); + _text=_encodedText->convertWideString(text); + _initAlignment=false; + _encodedText->setText((const unsigned char*)_text.data(),_text.size()); +} + // Text ///////////////////////////////////////////////////////////////////////////////