Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

How to align text vertically without making it shake

$
0
0

@cuinjune wrote:

Hi, I'm trying to create a simple text alignment program using ofTrueTypeFont.

I found out ofTrueTypeFont draws string based on its bottom left corner as a default.

So I tried to change it's vertical alignment base(Top/Middle/Bottom) with a key press.

Here's the test code below.

I used Sansation font for the testing. Sansation.ttf.zip (14.2 KB)

in ofApp.h

ofTrueTypeFont font;
string text;

enum ALIGN_MODE {

    ALIGN_TO_BOTTOM,
    ALIGN_TO_MIDDLE,
    ALIGN_TO_TOP
};
ALIGN_MODE alignMode;

in ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){

    font.load("Sansation.ttf", 50);
    alignMode = ALIGN_TO_BOTTOM;
}

//--------------------------------------------------------------
void ofApp::update(){

    //set text with a randomly changing value
    text = "Value : " + ofToString(ofRandom(100));
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofTranslate(100, 100);
    ofSetColor(ofColor::red);

    float stringHeight = font.stringHeight(text);

    switch (alignMode) {

        case ALIGN_TO_BOTTOM:
            font.drawString(text, 0, 0); //doesn't shake
            break;
        case ALIGN_TO_MIDDLE:
            font.drawString(text, 0, stringHeight/2); //shakes
            break;
        case ALIGN_TO_TOP:
            font.drawString(text, 0, stringHeight); //shakes badly
            break;
        default:
            break;
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

    switch (key) {
        case '1':
            alignMode = ALIGN_TO_BOTTOM;
            break;
        case '2':
            alignMode = ALIGN_TO_MIDDLE;
            break;
        case '3':
            alignMode = ALIGN_TO_TOP;
            break;
        default:
            break;
    }
}

If you run the program, it will look like the screenshot below.

And If you change the alignment by pressing key '2' or '3', you will see the text shaking up and down badly.

I think it is due to ofTrueTypeFont::stringHeight() returning different values depending on the string.

Would it be possible to align the text vertically without making it shake up and down depending on the string?

Thank you very much in advance!

Posts: 8

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles