ofTrueTypeFont

The ofTrueTypeFont class provides an interface to load fonts into openframeworks. The fonts are converted to textures, and can be drawn on screen. There are some options when you load the font - what size the font is rendered at, wether or not it is anti-aliased, and wether the font object will be the full character set or a subset (ie, extended ascii, which can include accents, umlauts, or normal ascii). The default is anti-aliased, non-full character set. The library uses freetype, which has certain patent problems in regards to true type hinting, especially at small sizes, so non-anti-aliased type doesn't always render beautifully. But we find it quite adequate, and at larger sizes it seems to works well.

ofTrueTypeFont methods

loadFont(...)

void ofTrueTypeFont::loadFont(string filename, int fontsize)

Loads a fonts of a given filename in, and renders it to a texture at a given size (fontsize). It will look for the font file in the data/ folder. For example, to load the font arial at type size 32:


// int the h file:

ofFont myFont;
...

myFont.loadFont("arial.ttf", 32);

loadFont(...)

void ofTrueTypeFont::loadFont(string filename, int fontsize, bool _bAntiAliased, bool _bFullCharacterSet, bool makeContours)

This loads a font, but in addition to setting the font name and size, you can also pass in two flags: is this font antiAliased, and does it include the full character set?

getLineHeight()

float ofTrueTypeFont::getLineHeight()

The line height is computed, based on the font size, and can be adjusted. Useful if you are print multi-line text.

setLineHeight(...)

void ofTrueTypeFont::setLineHeight(float height)

Sets the line height for text that is drawn on screen.

stringWidth(...)

float ofTrueTypeFont::stringWidth(string s)

stringHeight(...)

float ofTrueTypeFont::stringHeight(string s)

getStringBoundingBox(...)

ofRectangle ofTrueTypeFont::getStringBoundingBox(string s, float x, float y)

e.g:


//in setup() 

franklinBook.loadFont("frabk.ttf", 32);

//in update()

char tempString[255];
ofRectangle rect = franklinBook.getStringBoundingBox(tempString, 0,0);

//in draw

ofSetColor(0xcccccc);
ofRect(rect.x, rect.y, rect.width, rect.height);

drawString(...)

void ofTrueTypeFont::drawString(string s, float x, float y)

Draws a string with that typeface, on screen, at point(x,y). For example, you can write some text on screen like this:


// in the h file:
ofFont myfont;
.....

// in setup:
myfont.loadFont("arial.ttf", 32);

// in draw:
myfont.drawString("hi!!", 100,100);

Your strings can even be multiline:


myfont.drawString("a test of multiline text", 300,300);

you can also using dynamically generated strings. For example, to print the frame rate:


char fpsStr[255]; // an array of chars
sprintf(fpsStr, "frame rate: %f", ofGetFrameRate());
myfont.drawString(fpsStr, 100,100);

drawStringAsShapes(...)

void ofTrueTypeFont::drawStringAsShapes(string s, float x, float y)

drawStringAsShapes function draws the s string as if it was a geometrical shapes using the information contained in ofTTFContour and ofTTFCharacter. Parameters x and y sets the position of the shape.

loadFont(...)

bool ofTrueTypeFont::loadFont(string filename, int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=false, bool makeContours=false, float simplifyAmt=0.3, int dpi=0)

isLoaded()

bool ofTrueTypeFont::isLoaded()

isAntiAliased()

bool ofTrueTypeFont::isAntiAliased()

hasFullCharacterSet()

bool ofTrueTypeFont::hasFullCharacterSet()

getSize()

int ofTrueTypeFont::getSize()

getLetterSpacing()

float ofTrueTypeFont::getLetterSpacing()

setLetterSpacing(...)

void ofTrueTypeFont::setLetterSpacing(float spacing)

getSpaceSize()

float ofTrueTypeFont::getSpaceSize()

setSpaceSize(...)

void ofTrueTypeFont::setSpaceSize(float size)

getNumCharacters()

int ofTrueTypeFont::getNumCharacters()

getStringAsPoints(...)

ofTTFCharacter ofTrueTypeFont::getStringAsPoints(string str)

bind()

void ofTrueTypeFont::bind()

unbind()

void ofTrueTypeFont::unbind()

setGlobalDpi(...)

void ofTrueTypeFont::setGlobalDpi(int newDpi)

ofTrueTypeFont variables

bool bLoadedOk

bool ofTrueTypeFont::bLoadedOk

bLoadedOk is a boolean variable containing true if the font was successfully loaded.

bool bAntiAlised

bool ofTrueTypeFont::bAntiAlised

A variable which tells you if the font is antiAliased.

bool bFullCharacterSet

bool ofTrueTypeFont::bFullCharacterSet

A variable which tells you if the font contains the full character set, or a subset.

int nCharacters

int ofTrueTypeFont::nCharacters

nCharacters contains the number of characters that our font has.