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 whether the font object will be the full character set or a subset (i.e., extended ASCII, which can include diacritics like umlauts, or 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.
createStringMesh( ... )
void createStringMesh(const string &s, float x, float y, bool vFlipped)This function, which is used internally, builds a mesh representation of the font. This function is used by ofTrueTypeFont::getStringMesh().
drawChar( ... )
void drawChar(uint32_t c, float x, float y, bool vFlipped)This function draws a character at position x,y. It uses the texture representation of the font.
drawCharAsShape( ... )
void drawCharAsShape(uint32_t c, float x, float y, bool vFlipped, bool filled)This function draws a character at position x,y. It uses the path of the font, which involves
drawString( ... )
void drawString(const string &s, float x, float y)Draw a string s at position x,y
Parameters:
s String to draw
x X position of string
y Y position of string
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:
ofTrueTypeFont myfont;
.....
// in setup:
myfont.load("arial.ttf", 32);
// in draw:
myfont.drawString("hi!!", 100,100);
Your strings can even be multiline:
myfont.drawString("a test of\nmultiline text", 300,300);
Or:
myfont.drawString(R"(a test of
multiline text)", 300,300);
you can also using dynamically gengerated 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 drawStringAsShapes(const string &s, float x, float y)Draws the string as if it was geometrical shapes.
Uses the information contained in ofTTFContour and ofTTFCharacter.
Parameters:
x X position of shapes
y Y position of shapes
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.
finishLibraries( )
void finishLibraries()getAscenderHeight( )
float getAscenderHeight()Get the ascender distance for this font.
The ascender is the vertical distance from the baseline to the highest "character" coordinate. The meaning of "character" coordinate depends on the font. Some fonts take accents into account, others do not, and still others define it simply to be the highest coordinate over all glyphs.
Returns: the font ascender height in pixels.
getCharacterAsPoints( ... )
ofPath getCharacterAsPoints(uint32_t character, bool vflip=true, bool filled=true)\todo
getDescenderHeight( )
float getDescenderHeight()Get the descender distance for this font.
The descender is the vertical distance from the baseline to the lowest "character" coordinate. The meaning of "character" coordinate depends on the font. Some fonts take accents into account, others do not, and still others define it simply to be the lowest coordinate over all glyphs. This value will be negative for descenders below the baseline (which is typical).
Returns: the font descender height in pixels.
getFirstGlyphPosForTexture( ... )
glm::vec2 getFirstGlyphPosForTexture(const string &str, bool vflip)getFontTexture( )
const ofTexture & getFontTexture()Returns the texture (as a reference) that ofTrueTypeFont uses internally. When you load in a font, it parses the ttf (or .otf) file and rasterizes it to a texture for fast drawing. This gives you low level access to that texture.
getGlyphBBox( )
const ofRectangle & getGlyphBBox()Get the global bounding box for this font.
The global bounding box is the rectangle inside of which all glyphs in the font can fit. Glyphs are drawn starting from (0,0) in the returned box (though note that the box can extend in any direction out from the origin).
Returns: the font descender height in pixels.
getGlyphProperties( ... )
const ofTrueTypeFont::glyphProps & getGlyphProperties(uint32_t glyph)getKerning( ... )
int getKerning(uint32_t c, uint32_t prevC)getLetterSpacing( )
float getLetterSpacing()Returns letter spacing of font object.
You can control this by the ofTrueTypeFont::setLetterSpacing() function. 1.0 = default spacing, less then 1.0 would be tighter spacing, greater then 1.0 would be wider spacing.
Returns: the letter spacing of font object.
Returns the letter spacing of the font object. You can control this by the ofTrueTypeFont::setLetterSpacing() function. 1.0 = default spacing, less then 1.0 would be tighter spacing, greater then 1.0 would be wider spacing.
getLineHeight( )
float getLineHeight()Computes line height based on font size.
Returns: the current line height.
The line height is computed, based on the font size, and can be adjusted. Useful if you are print multi-line text. This function returns the current line height.
getNumCharacters( )
size_t getNumCharacters()Get the number of characters in the loaded character set.
If you allocate the font using different parameters, you can load in partial and full character sets, this helps you know how many characters it can represent.
Returns: Number of characters in loaded character set.
Returns the number of characters this font represents. If you allocate the font using different parameters, you can load in partial and full character sets, this helps you know how many characters it can represent.
getSize( )
int getSize()Returns the size of the font.
Returns: Size of font, set when font was loaded.
Returns the size of the font. This is set when you load in the font.
getSpaceSize( )
float getSpaceSize()Returns a variable that represents how wide spaces are.
It's a scalar for the width of the letter 'p', so 1.0 means that a space will be the size of the lower case 'p' of that font. 2.0 means that it's 2 times the size of the lower case 'p', etc.
Returns: the width of the space.
This is a variable to represent how wide spaces are sized. It's a scalar for the width of the letter 'p', so 1.0 means that a space will be the size of the lower case 'p' of that font. 2.0 means that it's 2 times the size of the lower case 'p', etc.
getStringAsPoints( )
int getStringAsPoints()This returns a vector of ofTTFCharacters (which is actually an ofPath) for a given string. This means you can get access to the point data / outlines of the letter forms.
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0);
font.load("vag.ttf", 100, false, false, true);
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
// get the string as paths
bool vflip = true; // OF flips y coordinate in the default perspective,
// should be false if using a camera for example
bool filled = true; // or false for contours
vector < ofPath
getStringBoundingBox( ... )
ofRectangle getStringBoundingBox(const string &s, float x, float y, bool vflip=true)Returns the bounding box of a string as a rectangle.
Parameters:
s The string to get bounding box of.
x X position of returned rectangle.
y Y position of returned rectangle.
Returns: the bounding box of a string as a rectangle.
Returns the bounding box of a string as a rectangle, useful if you want to position the type or calculate the size of graphics that relate to the font.
e.g:
//in setup()
franklinBook.load("frabk.ttf", 32);
//in update()
char tempString[255];
ofRectangle rect = franklinBook.getStringBoundingBox(tempString, 0,0);
//in draw
ofSetColor(0xcccccc);
ofDrawRectangle(rect.x, rect.y, rect.width, rect.height);
getStringMesh( ... )
const ofMesh & getStringMesh(const string &s, float x, float y, bool vflip=true)Returns the string as an ofMesh. Note: this is a mesh that contains vertices and texture coordinates for the textured font, not the points of the font that are returned via any of the get points functions.
getStringTexture( ... )
ofTexture getStringTexture(const string &s, bool vflip=true)hasFullCharacterSet( )
bool hasFullCharacterSet()Does the font have a full character set?
Returns: true if the font was allocated with a full character set.
Returns true or false if this font was allocated with a full character set.
indexForGlyph( ... )
size_t indexForGlyph(uint32_t glyph)initLibraries( )
bool initLibraries()isAntiAliased( )
bool isAntiAliased()Is the font anti-aliased?
Returns: true if the font was set to be anti-aliased.
Returns true of false if the font is set to be anti-aliased. This is set when you load.
isLoaded( )
bool isLoaded()Has the font been loaded successfully?
Returns: true if the font was loaded.
Returns true or false if the font is loaded properly.
isValidGlyph( ... )
bool isValidGlyph(uint32_t )load( ... )
bool load(const filesystem::path &filename, int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=true, bool makeContours=false, float simplifyAmt=0.3f, int dpi=0)Loads the font specified by filename, allows you to control size, aliasing, and other parameters.
loads a font, and allows you to set the following parameters: the filename, the size, if the font is anti-aliased, if it has a full character set, if you need it to have contours (for getStringPoints) and parameters that control the simplification amount for those contours and the dpi of the font.
default (without dpi), non-full char set, anti aliased, 96 dpi
Parameters:
filename The name of the font file to load.
fontsize The size in pixels to load the font.
_bAntiAliased true if the font should be anti-aliased.
_bFullCharacterSet true if the full character set should be cached.
makeControus true if the vector contours should be cached.
simplifyAmt the amount to simplify the vector contours. Larger number means more simplified.
dpi the dots per inch used to specify rendering size.
Returns: true if the font was loaded correctly.
load( ... )
bool load(const ofTrueTypeFontSettings &settings)loadGlyph( ... )
ofTrueTypeFont::glyph loadGlyph(uint32_t utf8)ofTrueTypeFont( ... )
ofTrueTypeFont(ofTrueTypeFont &&mom)ofTrueTypeFont( ... )
ofTrueTypeFont(const ofTrueTypeFont &mom)ofTrueTypeFont( )
ofTrueTypeFont()Construct a default ofTrueTypeFont.
operator=( ... )
ofTrueTypeFont & operator=(ofTrueTypeFont &&mom)operator=( ... )
ofTrueTypeFont & operator=(const ofTrueTypeFont &mom)reloadTextures( )
void reloadTextures()setDirection( ... )
void setDirection(ofTrueTypeFontDirection direction)Returns: current font direction
setGlobalDpi( ... )
void setGlobalDpi(int newDpi)Set the default dpi for all typefaces.
setLetterSpacing( ... )
void setLetterSpacing(float spacing)Sets the letter spacing of the font object.
1.0 = default spacing, less then 1.0 would be tighter spacing, greater then 1.0 would be wider spacing.
Parameters:
spacing Spacing of font object.
Sets the letter spacing of the font object. 1.0 = default spacing, less then 1.0 would be tighter spacing, greater then 1.0 would be wider spacing.
setLineHeight( ... )
void setLineHeight(float height)Sets line height for text drawn on screen.
Note the line height is automatically computed based on the font size, when you load in the font.
Parameters:
height Line height for text drawn on screen.
Sets the line height for text that is drawn on screen. Note the line height is automatically computed based on the font size, when you load in the font.
setSpaceSize( ... )
void setSpaceSize(float size)Sets the size of the space ' ' character.
This number, which defaults to 1.0, scales the width of the letter 'p' for the space.
Parameters:
size Scales the width of the letter 'p' for the space.
Sets the size of the space ' ' character. This number, which defaults to 1.0, scales the width of the letter 'p' for the space.
stringHeight( ... )
float stringHeight(const string &s)Returns the string height.
This is essentially the height component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
Parameters:
s The string to get the height of.
Returns: the string height.
Returns the string height. This is essentially the height component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
stringWidth( ... )
float stringWidth(const string &s)Returns the string width.
This is essentially the width component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
Parameters:
s The string to get the width of.
Returns: the string width.
Returns the string width. This is essentially the width component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
unloadTextures( )
void unloadTextures()~ofTrueTypeFont( )
~ofTrueTypeFont()Destroy the ofTrueTypeFont.
The destructor for the font object will clear the resources, such as textures, that have been allocated.