accents with ofxFTGL

I’m trying to get accents and special caracters in OF with ofxFTGL.

I dowloaded the version I found at
http://codaset.com/ari/ofou/source/gran-…-ns/ofxFTGL

I made the project attached “ofxFTGL_test2” with OF v0062

The caracter with accents are well displayed but an other caracter is displayed with. If I get the string length, for one caracter with accent the string length is 2 not 1.

When I try to display “éèçàioù” I got this

Does anyone manage to display accents with success ?

OfxFTGL_test2.zip

At last I used wstring instead of string (as Akira suggested it at
http://forum.openframeworks.cc/t/chinese-fonts/3307/0)

I also don’t use the ofxFTGLFont class but this code, where font is a pointer to a FTFont

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	font = new FTTextureFont(ofToDataPath("Arial.ttf").c_str());  
    font->Outset(0.0f, FONT_SIZE);  
    font->CharMap(ft_encoding_unicode);  
	  
    if(font->Error()) printf("error loading font");  
    if(!font->FaceSize(FONT_SIZE)) printf("Failed to set size");  
	  
	wphrase = L"éèçàioù pas bon cette tambouille";  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	glPushMatrix();  
    glTranslatef(100, 100, 0);  
    glScalef(1,-1,1);  
    font->Render(wphrase.c_str(), wphrase.length(), FTPoint(), FTPoint(), FTGL::RENDER_FRONT | FTGL::RENDER_BACK);  
    glPopMatrix();  
}  
  

Now I’m stuck with a loading from an xml file. I try to convert the string from the xml to a wstring with

  
if( XML.loadFile(ofToDataPath("Sentences.xml"))) phrase = XML.getValue("PHRASE1", "ok");  
	wstring temp(phrase.length(),L' ');  
	copy(phrase.begin(), phrase.end(), back_inserter(temp));  
	wphrase = temp;  

Where phrase is a string and wphrase is a wstring. But I got something like in the file attached instead of “éèçàioù pas bon cette tambouille”

Any ideas of an efficient way to convert string to wstring ?

i’ve used this sometimes with plain ofTrueTypeFont:

  
         typedef struct{  
                string character;  
                string code;  
         }charSubstitution;  
           
         void subsChars(string & origString){  
                static charSubstitution chars[]={  
                                {"à","\xE0"}, {"á","\xE1"},  
         {"â","\xE2"}, {"ã","\xE3"}, {"ä","\xE4"},  
         {"æ","\xE6"},  
                                {"ò","\xF2"},  
         {"ó","\xF3"} ,{"ô","\xF4"}, {"õ","\xF5"}, {"ö","\xF6"},  
                                {"ù","\xF9"}, {"ú","\xFA"},  
         {"û","\xFB"}, {"ü","\xFC"},  
                                {"è","\xE8"}, {"é","\xE9"},  
         {"ê","\xEA"}, {"ë","\xEB"},  
                                {"ì","\xEC"}, {"í","\xED"},  
         {"î","\xEE"}, {"ï","\xEF"},  
                                {"ç","\xE7"}, {"Ç","\xC7"}  
                };  
           
                for(int i=0; i<24; i++){  
           
                        while(origString.find(chars[i].character)!  
         =string::npos){  
                                origString =  
         origString.substr(0,origString.find(chars[i].character))  
         + chars[i].code +  
         origString.substr(origString.find(chars[i].character)+2);  
           
                        }  
                };  
         }  

it substitutes special characters by their corresponding codes, to use it just call the function passing it the string you want to render with special chars, if you need more characters that the ones in there, google for html special characters like:

http://www.utexas.edu/learn/html/spchar.html

and substitute &#xxx; by its corresponding hexadecimal value with a \x prefix

Yes, Thanks Arturo,

I used your function for a first conversion of my xml input

then use mbstowcs() to convert to a wstring

  
  
//First I convert the string input from the xml file  
subsChars(phrase);  
//then I convert it to a wstring 	  
	wphrase.resize(phrase.size());  
	mbstowcs(&wphrase[0], &phrase[0], phrase.size());  

at least for ofTrueTypeFont it works without even converting to wstring

Yes but I need to know the dimensions of each caracter.
As I did not find a way to do that with ofTrueTypeFont, I’m using FTGL.
That’s the only reason.

Thanks for this! Here’s a version with more characters (notably the uppercase ones) if anyone needs it:

  
  
            static charSubstitution chars[] = {    
                {"À","\xC0"}, {"Á","\xC1"}, {"Â","\xC2"}, {"Ã","\xC3"}, {"Ã","\xC4"}, {"Å","\xC5"}, {"Æ","\xC6"},  
                {"à","\xE0"}, {"á","\xE1"}, {"â","\xE2"}, {"ã","\xE3"}, {"ä","\xE4"}, {"å","\xE5"}, {"æ","\xE6"},  
                {"Ç","\xC7"},  
                {"ç","\xE7"},  
                {"È","\xC8"}, {"É","\xC9"}, {"Ê","\xCA"}, {"Ë","\xCB"},  
                {"è","\xE8"}, {"é","\xE9"}, {"ê","\xEA"}, {"ë","\xEB"},  
                {"Ì","\xCC"}, {"Í","\xCD"}, {"Î","\xCE"}, {"Ï","\xCF"},  
                {"ì","\xEC"}, {"í","\xED"}, {"î","\xEE"}, {"ï","\xEF"},  
                {"Ñ","\xD1"},  
                {"ñ","\xF1"},  
                {"Ò","\xD2"}, {"Ó","\xD3"}, {"Ô","\xD4"}, {"Õ","\xD5"}, {"Ö","\xD6"}, {"Ø","\xD8"},  
                {"ò","\xF2"}, {"ó","\xF3"}, {"ô","\xF4"}, {"õ","\xF5"}, {"ö","\xF6"}, {"ø","\xF8"},  
                {"ß","\xDF"},  
                {"Ù","\xD9"}, {"Ú","\xDA"}, {"Û","\xDB"}, {"Ü","\xDC"},  
                {"ù","\xF9"}, {"ú","\xFA"}, {"û","\xFB"}, {"ü","\xFC"},  
                {"ÿ","\xFF"}  
            };  
              
            for (int i=0; i < 56; i++) {    
                while (_orig.find(chars[i].character) != string::npos) {    
                    _orig = _orig.substr(0, _orig.find(chars[i].character)) + chars[i].code + _orig.substr(_orig.find(chars[i].character) + 2);  
                }    
            };  
  

i read somedays ago in another post that saving the cpp as latin instead of utf should just work without any substitution

I have in windows some accents in a XML file encoded with utf8 and strangely the method from arturo subsChars not find this accents in the string. Do I have to make something extra in the loading of XML?

  
  
if( !xml.loadFile("allContent.xml") ) return;  
xml.pushTag("allContent");  
string textQuestion = xml.getValue("content","",0);	  
subsChars(textQuestion);  
  

Using the Latin-1 is working too in windows. but I was wondering if I could have in utf8 thats looks more standart to save a txt file.

A version with even more characters:

  
  
class StringUtils {  
public:  
	  
	typedef struct{    
		string character;    
		string code;    
	}charSubstitution;   
  
	static string subsChars(string origString){    
		charSubstitution chars[]={    
			{"À","\xC0"}, {"Á","\xC1"}, {"Â","\xC2"}, {"Ã","\xC3"}, {"Ã","\xC4"}, {"Å","\xC5"}, {"Æ","\xC6"},    
			{"à","\xE0"}, {"á","\xE1"}, {"â","\xE2"}, {"ã","\xE3"}, {"ä","\xE4"}, {"å","\xE5"}, {"æ","\xE6"},    
			{"Ç","\xC7"},    
			{"ç","\xE7"},    
			{"È","\xC8"}, {"É","\xC9"}, {"Ê","\xCA"}, {"Ë","\xCB"},    
			{"è","\xE8"}, {"é","\xE9"}, {"ê","\xEA"}, {"ë","\xEB"},    
			{"Ì","\xCC"}, {"Í","\xCD"}, {"Î","\xCE"}, {"Ï","\xCF"},    
			{"ì","\xEC"}, {"í","\xED"}, {"î","\xEE"}, {"ï","\xEF"},    
			{"Ñ","\xD1"},    
			{"ñ","\xF1"},    
			{"Ò","\xD2"}, {"Ó","\xD3"}, {"Ô","\xD4"}, {"Õ","\xD5"}, {"Ö","\xD6"}, {"Ø","\xD8"},    
			{"ò","\xF2"}, {"ó","\xF3"}, {"ô","\xF4"}, {"õ","\xF5"}, {"ö","\xF6"}, {"ø","\xF8"},    
			{"ß","\xDF"},    
			{"Ù","\xD9"}, {"Ú","\xDA"}, {"Û","\xDB"}, {"Ü","\xDC"},    
			{"ù","\xF9"}, {"ú","\xFA"}, {"û","\xFB"}, {"ü","\xFC"},    
			{"ÿ","\xFF"}, {"ý","\xFD"},   
			{"-","\xAD"}, {"\"","\x22"}, {"'","\x27"}, {"-","\x2D"}, {"<","\x3C"}, {"=","\x3D"}, {">","\x3E"},  
			{"¡","\xA1"}, {"¢","\xA2"}, {"£","\xA3"}, {"¤","\xA4"}, {"¥","\xA5"}, {"¦","\xA6"}, {"§","\xA7"},  
			{"¨","\xA8"}, {"©","\xA9"}, {"ª","\xAA"}, {"«","\xAB"}, {"¬","\xAC"}, {"®","\xAE"}, {"¯","\xAF"},  
			{"°","\xB0"}, {"±","\xB1"}, {"²","\xB2"}, {"³","\xB3"}, {"´","\xB4"}, {"µ","\xB5"}, {"¶","\xB6"},  
			{"·","\xB7"}, {"¸","\xB8"}, {"¹","\xB9"}, {"º","\xBA"}, {"»","\xBB"}, {"¼","\xBC"}, {"½","\xBD"},  
			{"¾","\xBE"}, {"¿","\xBF"}, {"Ð","\xD0"}, {"Ý","\xDD"}, {"Þ","\xDE"}, {"ð","\xF0"}, {"÷","\xF7"},  
			{"þ","\xFE"}  
		};    
		  
		for(int i=0; i<100; i++){    
			  
			while(origString.find(chars[i].character)!=string::npos){    
				origString = origString.substr(0,origString.find(chars[i].character)) + chars[i].code + origString.substr(origString.find(chars[i].character)+2);  
			}  
		};    
		  
		return origString;  
	}  
  
};  
  

Would be nice if we can put the special symbols too:

  
  
? ? ? ? %u2022 @ ? ??? ? ? TM ? ? ~ %u20AC ? ? ? ? ® ? ? ? ? ^ + * & % # ? o 0 »-> ø ¤ ? ¿ © %u2020 ? <-« ? ? ? ? ° ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ???? ± ?? ? ? § ?? ¶ ? $ x » « ?> <?? ? ? ? ? ?????????? ?? ????????? ?????????????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^_^ ? = » « ? ? ? ? ??????? »« ? ? ±? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ?? ?? ?? ?? ?? ?? ?? ?? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ??" ??? ? ???? ? ? ?? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?? ? ? ? ?? ? ? ? ? ? ? .:??? ? ? ???????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? x ? ? ?.? »»--><--«« ? *??l?*?? ?????? ????????? ???? ?????????? ???????? ???? † ‡ ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .:. ? ?« ? ? ±? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ?? ? ?? ? ? ? ? ? ? ? ? ? ? ? 1 » 1?4 1?2 3?4 a « ¬ ? ± %u2020 %u2021 ? ? ? ? ? ? ? ? ? ? TM ? ? ? ? ? ? ? ? ? ? ?? ?? ?? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .B????? I ? Þ ? ? ? ? ?? ? ? ? ???? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ?? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ?? ?? ? ? ? ????? *.:? ??????? ? ? ? ? ??????? ? ? ? ? ??? ?? ?? ?????? ? ??. *.? ? ? ? ? ?? ? ????? ?? ??????????????? ???*?%u2018???.?.:* *.:??*?%u2019???.?.:* *.:??*? ???? ?.?.:* *.:??*? ?????.?.:*?? ? ? ? ? ? ?? ??? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? t ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  
  

I’ trying to wrote in russian with ofxFTGL.
Actually I’ve tried many things from using the the utf-8 (russian) text inside the code, and using the hexa notation without success.
Two characters are draw. And they are not the right ones.
I means it cuts the hexa in two parts and interpret it as ascii

my code is

  
//--------------------------------------------------------------  
void testApp::setup(){  
    font.loadFont("Arial.ttf");  
    font.setSize(80);  
    font.font->CharMap(FT_ENCODING_WANSUNG);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    ofBackground(0);  
    ofSetColor(255);  
    //tryinng to write the russian letter ?  
    font.drawString("\x04\x44", 200, 200);  
}