class methods
- drawString()
- drawStringAsShapes()
- getAscenderHeight()
- getCharacterAsPoints()
- getDescenderHeight()
- getFirstGlyphPosForTexture()
- getFontTexture()
- getGlyphBBox()
- getLetterSpacing()
- getLineHeight()
- getNumCharacters()
- getSize()
- getSpaceSize()
- getStringAsPoints()
- getStringBoundingBox()
- getStringMesh()
- getStringTexture()
- hasFullCharacterSet()
- isAntiAliased()
- isLoaded()
- isValidGlyph()
- load()
- operator=()
- setDirection()
- setGlobalDpi()
- setLetterSpacing()
- setLineHeight()
- setSpaceSize()
- stringHeight()
- stringWidth()
global functions
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.
drawString(...)
void ofTrueTypeFont::drawString(const 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:
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);
Documentation from code comments
Draw a string s at position x,y
Parameters:
s String to draw
x X position of string
y Y position of string
drawStringAsShapes(...)
void ofTrueTypeFont::drawStringAsShapes(const 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.
Documentation from code comments
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
getAscenderHeight()
float ofTrueTypeFont::getAscenderHeight()
Documentation from code comments
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 ofTrueTypeFont::getCharacterAsPoints(uint32_t character, bool vflip=true, bool filled=true)
Documentation from code comments
\todo
getDescenderHeight()
float ofTrueTypeFont::getDescenderHeight()
Documentation from code comments
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 ofTrueTypeFont::getFirstGlyphPosForTexture(const string &str, bool vflip)
getFontTexture()
const ofTexture & ofTrueTypeFont::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 & ofTrueTypeFont::getGlyphBBox()
Documentation from code comments
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.
getLetterSpacing()
float ofTrueTypeFont::getLetterSpacing()
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 ofTrueTypeFont::getLineHeight()
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.
Documentation from code comments
Computes line height based on font size.
Returns: the current line height.
getNumCharacters()
size_t ofTrueTypeFont::getNumCharacters()
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.
Documentation from code comments
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.
getSize()
int ofTrueTypeFont::getSize()
Returns the size of the font. This is set when you load in the font.
Documentation from code comments
Returns the size of the font.
Returns: Size of font, set when font was loaded.
getSpaceSize()
float ofTrueTypeFont::getSpaceSize()
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()
std::vector
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 > paths = font.getStringAsPoints("hello!", vflip, filled);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
for (int i = 0; i < paths.size(); i++){
// for every character break it out to polylines
vector <ofPolyline> polylines = paths[i].getOutline();
// for every polyline, draw every fifth point
for (int j = 0; j < polylines.size(); j++){
for (int k = 0; k < polylines[j].size(); k+=5){ // draw every "fifth" point
ofDrawCircle( polylines[j][k], 3);
}
}
}
ofPopMatrix();
}
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 > paths = font.getStringAsPoints("hello!", vflip, filled);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
for (int i = 0; i < paths.size(); i++){
// for every character break it out to polylines
vector <ofPolyline> polylines = paths[i].getOutline();
// for every polyline, draw every fifth point
for (int j = 0; j < polylines.size(); j++){
for (int k = 0; k < polylines[j].size(); k+=5){ // draw every "fifth" point
ofDrawCircle( polylines[j][k], 3);
}
}
}
ofPopMatrix();
}
getStringBoundingBox(...)
ofRectangle ofTrueTypeFont::getStringBoundingBox(const string &s, float x, float y, bool vflip=true)
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);
Documentation from code comments
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.
getStringMesh(...)
const ofMesh & ofTrueTypeFont::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.
hasFullCharacterSet()
bool ofTrueTypeFont::hasFullCharacterSet()
Returns true or false if this font was allocated with a full character set.
Documentation from code comments
Does the font have a full character set?
Returns: true if the font was allocated with a full character set.
isAntiAliased()
bool ofTrueTypeFont::isAntiAliased()
Returns true of false if the font is set to be anti-aliased. This is set when you load.
Documentation from code comments
Is the font anti-aliased?
Returns: true if the font was set to be anti-aliased.
isLoaded()
bool ofTrueTypeFont::isLoaded()
Returns true or false if the font is loaded properly.
Documentation from code comments
Has the font been loaded successfully?
Returns: true if the font was loaded.
load(...)
bool ofTrueTypeFont::load(const filesystem::path &filename, int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=true, bool makeContours=false, float simplifyAmt=0.3f, int dpi=0)
Documentation from code comments
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.
setDirection(...)
void ofTrueTypeFont::setDirection(ofTrueTypeFontDirection direction)
Documentation from code comments
Returns: current font direction
setGlobalDpi(...)
void ofTrueTypeFont::setGlobalDpi(int newDpi)
Documentation from code comments
Set the default dpi for all typefaces.
setLetterSpacing(...)
void ofTrueTypeFont::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.
Documentation from code comments
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.
setLineHeight(...)
void ofTrueTypeFont::setLineHeight(float height)
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.
Documentation from code comments
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.
setSpaceSize(...)
void ofTrueTypeFont::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.
Documentation from code comments
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.
stringHeight(...)
float ofTrueTypeFont::stringHeight(const string &s)
Returns the string height. This is essentially the height component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
Documentation from code comments
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.
stringWidth(...)
float ofTrueTypeFont::stringWidth(const string &s)
Returns the string width. This is essentially the width component of the ofTrueTypeFont::getStringBoundingBox() rectangle.
Documentation from code comments
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.
Last updated 화요일, 19 11월 2024 17:24:28 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue