//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield //Distributed under the terms of the GNU Library General Public License (LGPL) //as published by the Free Software Foundation. /* -------------------------------------------------------------------------- * * openscenegraph textLib / FTGL wrapper (http://homepages.paradise.net.nz/henryj/code/) * * -------------------------------------------------------------------------- * * prog: max rheiner;mrn@paus.ch * date: 4/25/2001 (m/d/y) * * -------------------------------------------------------------------------- * * -------------------------------------------------------------------------- */ #include #include using namespace osgText; EncodedText::EncodedText() { _encoding = ENCODING_ASCII; _overrideEncoding = ENCODING_SIGNATURE; } int EncodedText::getNextCharacter(const unsigned char*& charString) const { // For more info on unicode encodings see: // http://www-106.ibm.com/developerworks/unicode/library/u-encode.html switch(_encoding) { case ENCODING_ASCII: { return *charString++; } case ENCODING_UTF8: { int char0 = *charString++; if (char0 < 0x80) // 1-byte character { return char0; } int char1 = *charString++; if (char0<0xe0) // 2-byte character { return ((char0&0x1f)<<6) | (char1&0x3f); } int char2 = *charString++; if (char0<0xf0) // 3-byte character { return ((char0&0xf)<<12) | ((char1&0x3f)<<6) | (char2&0x3f); } int char3 = *charString++; if (char0<0xf8) // 4-byte character { return ((char0&0x7)<<18) | ((char1&0x3f)<<12) | ((char2&0x3f)<<6) | (char3&0x3f); } break; } case ENCODING_UTF16_BE: { int char0 = *charString++; int char1 = *charString++; if ((char0<=0xD7) || (char0>=0xE0)) // simple character { return (char0<<8) | char1; } else if ((char0>=0xD8)&&(char0<=0xDB)) //using planes (this should get called very rarely) { int char2 = *charString++; int char3 = *charString++; int highSurrogate = (char0<<8) | char1; int lowSurrogate = (char2<<8) | char3; if ((char2>=0xDC)&&(char2<=0xDF)) //only for the valid range of low surrogate { // This covers the range of all 17 unicode planes return ((highSurrogate-0xD800)*0x400) + (lowSurrogate-0xD800) + 0x10000; } } break; } case ENCODING_UTF16_LE: { int char1 = *charString++; int char0 = *charString++; if ((char0<=0xD7) || (char0>=0xE0)) // simple character { return (char0<<8) | char1; } else if ((char0>=0xD8)&&(char0<=0xDB)) //using planes (this should get called very rarely) { int char3 = *charString++; int char2 = *charString++; int highSurrogate = (char0<<8) | char1; int lowSurrogate = (char2<<8) | char3; if ((char2>=0xDC)&&(char2<=0xDF)) //only for the valid range of low surrogate { // This covers the range of all 17 unicode planes return ((highSurrogate-0xD800)*0x400) + (lowSurrogate-0xD800) + 0x10000; } } break; } case ENCODING_UTF32_BE: { int character = ((((int)charString[0])<<24) | (((int)charString[1])<<16) | (((int)charString[2])<<8) | charString[3]); charString+=4; if (character<0x110000) { // Character is constrained to the range set by the unicode standard return character; } break; } case ENCODING_UTF32_LE: { int character = ((((int)charString[3])<<24) | (((int)charString[2])<<16) | (((int)charString[1])<<8) | charString[0]); charString+=4; if (character<0x110000) { // Character is constrained to the range set by the unicode standard return character; } break; } default: { // Should not reach this point unless the encoding is unhandled // ENCODING_UTF16, ENCODING_UTF32 and ENCODING_SIGNATURE should never enter this method osg::notify(osg::FATAL)<<"Error: Invalid string encoding"<>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; }