From Tree, added support for using wchar_t strings with osg::Text/EncodedText.
This commit is contained in:
@@ -33,10 +33,10 @@
|
||||
#define OSGTEXT_ENCODEDTEXT 1
|
||||
|
||||
#include <osg/Referenced>
|
||||
#include <osgText/Export>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <osgText/Export>
|
||||
#include <string>
|
||||
|
||||
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<int>::const_iterator begin() const { return _unicodeText.begin(); }
|
||||
std::vector<int>::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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user