Render special characters (drawString vs getFontTexture)

Hallo,
When I’m drawing text using ofTrueTypeFont::drawString(), special characters like ä,Þ… are rendered correctly. When I’m using the getFontTexture function to render the same string, it isn’t working.

Here a demonstration code for illustration:

void testApp::setup(){
    ofBackground(0, 0, 0);
    ofSetFrameRate(1);
    ofSetVerticalSync(true);
    
    font.loadFont("univer.ttf", 70, true, true, false);
    
    test_1 = "norÞ";
    test_2 = "hällo";
    
    mesh_1 = font.getStringMesh(test_1, 0, 0);
    mesh_2 = font.getStringMesh(test_2, 0, 0);
    
    txt_vertices_1 = mesh_1.getVertices();
    txt_vertices_2 = mesh_2.getVertices();
}

void testApp::draw(){
    
    ofSetColor(255);
    font.drawString(test_1, 100, 100);
    font.drawString(test_2, 100, 200);
    
    mesh_1.clearColors();
    mesh_2.clearColors();
    
    for(int i=0; i<mesh_1.getVertices().size(); i++){
        mesh_1.setVertex(i, txt_vertices_1[i]+ofVec3f(100, 300, 0));
        mesh_1.addColor(ofFloatColor(1,0,0));
    }
    ofSetColor(0, 255, 0);
    for(int i=0; i<mesh_2.getVertices().size(); i++){
        mesh_2.setVertex(i, txt_vertices_2[i]+ofVec3f(100, 400, 0));
        mesh_2.addColor(ofFloatColor(0,1,0));
    }
    
    collector.clear();
    
    font.getFontTexture().bind();
    collector.append(mesh_1);
    collector.append(mesh_2);
    collector.draw();
    font.getFontTexture().unbind();
}

This is how my render looks like: screenshot

It seems like getFontTexture() is working with another charset or another encoding than the drawString() is using. Any ideas how I could fix this?

thanks for help,
wun

I found a fix for this problem by digging a little bit in the ofTrueTypeFont-sourcecode. If you have the same issue there you go:

include these files in your header:

#include "Poco/TextConverter.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Latin9Encoding.h"

than you need to convert the strings you are using like follows:

string str = "norÞ";
string str_tex_ready;
Poco::TextConverter(Poco::UTF8Encoding(), Poco::Latin9Encoding()).convert(str, str_tex_ready);

now your variable “str_texture_ready” will render correctly with getFontTexture(). In the same way you can also fix when you are getting wrong results from getStringBoundingBox() due to special characters.

hope this helps,
wun

@wunjeld folks frequently use ofxTrueTypeFontUC for drawing special characters.

but it’s good to know that your approach works!