ofTrueTypeFontSettings draw Chinese have problems


#1

Hi, I’m trying to draw Chinese in oF and I’m using v0.10.0.
here is my code:

void ofApp::setup(){
	ofTrueTypeFontSettings settings("Microsoft YaHei UI", 24);
	settings.antialiased = true;
	settings.contours = true;
	settings.dpi = 72;
	//settings.direction = OF_TTF_LEFT_TO_RIGHT;
	settings.addRanges(ofAlphabet::Chinese);
	settings.addRanges(ofAlphabet::Latin);

	ttf.load(settings);

}

void ofApp::draw(){
	ofPushStyle();
	ofSetColor(255, 0, 0);
	ttf.drawString("你好啊怎aaa啊啊怎", 100, 100);
	ofPopStyle();
}

It complied successfully when I drew “你好啊怎aaa啊啊”. But it turned out syntax error when I added other “怎”. I don’t know why.

The error shows:

I used an addon called ofxFontStash , which is a very good addon, in oF 0.9.8 before. It’s totally ok when I type any Chinese in ‘.cpp’ files.

I want to use oF 0.10.0 to draw Chinese because it can show complex fonts like Chinese, Japanese without any addons. It’s definitely more convenient.


#2

You probably need to save the file as utf8 for it to work. If you go to File > Save as… there should be an option to change the file encoding, chnage it to utf8 and save it with the same name


#3

Hi, arturo. I’m sorry I did not mention that I’ve saved the file as “Unicode (UTF-8 without signature) - Codepage 65001”.


#4

not sure then, might be a bug in visual studio 2017? perhaps ask in microsoft forums


#5

Hi, I tried another way to show this. I saved the sentence in text file and read the text file from oF. Then it works.
Here is my code:

void ofApp::setup(){
	ofTrueTypeFontSettings settings("Microsoft YaHei UI", 24);
	settings.antialiased = true;
	settings.contours = true;
	settings.dpi = 72;
	//settings.direction = OF_TTF_LEFT_TO_RIGHT;
	settings.addRanges(ofAlphabet::Chinese);
	settings.addRanges(ofAlphabet::Latin);

	ttf.load(settings);

	auto bufferLines = ofBufferFromFile("intro.txt").getText();
	for (int i = 0; i < bufferLines.size(); i++) {
		str += bufferLines[i];
	}
}

//--------------------------------------------------------------
void ofApp::draw(){
	ofPushStyle();
	ofSetColor(255, 0, 0);
	ttf.drawString(str, 100, 100);
	ofPopStyle();
}

Here is the screenshot of the result:

It might be the problem that visual studio 2017 cannot perfectly recognize Chinese. I will post the problem in microsoft forums later.

Still thank you a lot!