How to do a line break a text drawn by ofTruetypefont?

I want to create a text area where the length of text can be dynamically change.

so for example, if I write “I love openFrameworks!” in the certain rectangle area, I want to make the text automatically do a line break so it always fits into the width of the area.

something like this


I love openFrame
works!

Is this even possible with ofTruetypefont?

1 Like

I wrote an addon that might help you. Just set the width of the paragraph and your text will wrap to the next line. You can also set the alignment, indentation, line spacing etc… If you have any questions let me know.

1 Like

a line break in text is \n so you can add ‘\n’ to your string to create carriage returns

2 Likes

I did something recently along those lines - I stored the text in a temp variable as I’m typing it, then used that to check when the width of the text is close to the width of the window, and add a ‘\n’ to the ‘real’ text variable at that point, like this:

void ofApp::keyPressed(int key){

    //strSoFar is the temporary string variable, typeStr is the string (text) that is drawn on the screen 
         if ((myFont.stringWidth(strSoFar) ) > (ofGetWindowWidth())) {
 
          //add a carriage return to the string being drawn on the screen
            typeStr += "\n";
         // clear the temp variable at that point so we can start the horizontal measurement again
            strSoFar = "";
     }
  //add letters to both variables as we type.
    typeStr.append(1,(char)key);
    strSoFar.append(1, (char)key);
}
3 Likes

Hi, @braitsch

I tried your example code and I get the following error.

if(face) FT_Done_Face(face); EXC_BAD_ACCESS

it happens after “ttf.load(file, ptSize)” in “void ofxParagraph::setFont(std::string file, int ptSize)” function

if I remove “ttf.load(file, ptSize)” this line, then I could build it. but then the font won’t be allocated.

I’m using the latest OF nightly build 0.9.0 on iOS.

I would really appreciate if you can tell me how to fix this!

@braitsch

It works now. I deleted these two lines below and I could build it and load the font. but I don’t know why.

   font nFont = {file, ptSize, ttf};
   mFonts.push_back(nFont);

@braitsch

Now I get this error below. it happens randomly.

from ofTrueTypeFont.cpp,

int ofTrueTypeFont::getKerning(int c, int prevC) const{ // Thread1: EXC_BAD_ACCESS

}

@braitsch

Changing int “mWidth, mHeight, setWidth, getWidth, getHeight” to unsigned long fixed this error.

Thanks!

@braitsch

No, it didn’t fix.

It just reduced the chance of getting the error. I gave up now.

@braitsch

I finally fixed it.

In ofxParagraph::setFont, i got rid of the four lines below and then changed ttf.load to mFont.load and it works without any errors. Thanks!

// ofTrueTypeFont ttf;
// font nFont = {file, ptSize, mFont};
// mFonts.push_back(nFont);
// mFont = ttf;

Great, glad you got it to work for you. Mind submitting a pull request so I can see your fix?

I just rewrote the font caching to address the above issue and updated the addon to oF v0.9.0 for anyone coming across this post.

4 Likes