ofTrueTypeFontSettings draw Chinese have problems

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.

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

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

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

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!

Hello,i have followed your codes,but i cannot run as you display.My font is “Verdana.ttf”.Is there something wrong with my font? should i have to load “Microsoft YaHei UI”?

yes surely verdana doesn’t have chinese glyphs. you should try a font that has them. google fonts https://fonts.google.com/ for example gives you the option to look for fonts by language

Thank you very much.

Hello,i have realized it,but it become too slow.Is the speed affect by “settings.addRanges(ofAlphabet::Chinese)”?When i remove this line of code,speed become fast.Could you please tell me some solutions?Thank you.

have you tried to run your application in release mode rather than debug? specially in visual studio that will make a big difference

also if even with that the application is still slow you can try a couple of things:

  • instead of adding the full chinese alphabet you can try to add specific ranges. For example the chinese alphabet is composed by {ofUnicode::Space, ofUnicode::IdeographicSpace, ofUnicode::CJKLettersAndMonths, ofUnicode::CJKUnified} The two spaces are just one glyph each but the other two specially CJKUnified are pretty big so perhaps you can add only the glyphs you are going to use if you know that before hand by creating your own ranges as:
ofUnicode::range myRange{begin, end};

where begin and end are the first and last (not included) glyphs you want to use. for example CJKUnified is:

ofUnicode::range CJKUnified {0x4E00, 0x9FD5};

which includes around 21000 glyphs. you can find unicode glyphs here: https://unicode-table.com/en/

You can then add individual ranges by calling:

settings.addRange(myRange)

instead of adding full alphabets

  • ttf.getStringTexture("text") which returns a texture with the text you pass, you can create texts in setup and then reuse them afterwards which should be pretty fast but not as flexible as drawString

Thank you for your careful reply. I will try it.

Thanks again,i have trid to change debug mode to release ,and the speed becomes many times faster than before.

hi! arturo, could you please tell me why OF_functions about OfEasyCam cannot work in setup() but it work in draw()? for example, camera.setPosition() . But OfCamera’s functions can work in setup().