openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.9k stars 2.55k forks source link

ofTrueTypeFont does not work properly on RaspberryPi3-Raspbian(stretch) with the drawString() #5991

Open genleung opened 6 years ago

genleung commented 6 years ago

OF0.10.0-RC3, ofTrueTypeFont does not work properly on RaspberryPi3-Raspbian(stretch) with the drawString() method. Here are the codes:

//-------------------------------------------------------------- void ofApp::setup(){ ofTrueTypeFontSettings settings( "fz.ttf", 36); settings.addRange(ofUnicode::CJKUnified); settings.addRange(ofUnicode::Latin); _font.load(settings); } //-------------------------------------------------------------- void ofApp::draw(){ ofSetColor(255,0,0); char fpsStr[256]; sprintf(fpsStr, "framerate: %f", ofGetFrameRate()); _font.drawString(fpsStr, 100,100); _font.drawString("你好,OF!", 100,400); }

and the snapshot of the running OF window on Raspbian: https://forum.openframeworks.cc/uploads/default/original/2X/8/8dec8b099302517837e930b00efb5010b3f5277e.jpeg

As you can see, the characters are improperly drawed as rectangle blocks. If I change the line

_font.drawString("你好,OF!", 100,400);

to

_font.getStringTexture("你好,OF!").draw(100,400);

The characters are correctly drawed, which is strange. But the code looks awkward.

================================================================ And I changed to use drawStringAsShapes(), the window will take about 10 seconds(tooooo long) to show up, but the Chinese characters could be displayed properly. Here are the codes:

//-------------------------------------------------------------- void ofApp::setup(){ ofTrueTypeFontSettings settings( "fz.ttf", 36); settings.addRange(ofUnicode::CJKUnified); settings.addRange(ofUnicode::Latin); settings.contours=true; _font.load(settings); } //-------------------------------------------------------------- void ofApp::draw(){ ofSetColor(255,0,0); char fpsStr[256]; sprintf(fpsStr, "framerate: %f", ofGetFrameRate()); _font.drawStringAsShapes(fpsStr, 100,100); _font.drawStringAsShapes("你好,OF!", 100,400); }

and the snapshot of the running OF window on Raspbian: https://forum.openframeworks.cc/uploads/default/original/2X/a/abcc55c44e561768169f5098d81aa6e81b792628.jpeg

genleung commented 6 years ago

Thanks to @arturoc who gives the suggestion: https://forum.openframeworks.cc/t/0-10-0-release-candidate-3/29348/62?u=genleung_lan

the problem i think is that you are trying to allocate too many glyphs. Textures in desktop allow for much bigger sizes but in the raspberry pi they are limited to a smaller size (don’t know exactly how much right now)

Instead of trying to allocate all of CJK + Latin in one font you can try to load only CJKUnified in one font and then another for latin.

You can also use: getStringTexture to get a specific text in a texture which will only need to load the specific characters you are asking for. Don’t use this every frame though since it will be slow. But you can pre-allocate the texts in setup and use them after. Also aligning text with this method can be tricky since the position will be from the top left corner instead of the baseline of the text.

The solution for this is to allocate more than one texture per font if needed and create an index to know in which texture each character, we might include it for next release

By now i’ve included a check in ofTruetypeFont to show an error in case the texture needed is bigger than the maximum supported in each platform

rdob commented 6 years ago

Hi, I've ran into the same issue, changing the GPU memory spilt to 128MB, or the graphic from 1920x1080 to 1280x800 helps ;)