Class for loading and manipulating character fonts. More...
#include <Font.hpp>
Classes | |
struct | GlyphInfo |
Structure storing a glyph together with its rectangle in the texture. | |
struct | Page |
Structure defining a page if glyphs. | |
struct | Row |
Structure defining a row of glyphs. | |
Public Member Functions | |
Font () | |
Default constructor. | |
Font (const Font ©) | |
Copy constructor. | |
~Font () | |
Destructor. | |
bool | LoadFromFile (const std::string &filename) |
Load the font from a file. | |
bool | LoadFromMemory (const void *data, std::size_t sizeInBytes) |
Load the font from a file. | |
const Glyph & | GetGlyph (Uint32 codePoint, unsigned int characterSize, bool bold) const |
Retrieve a glyph of the font. | |
int | GetKerning (Uint32 first, Uint32 second, unsigned int characterSize) const |
Get the kerning offset of two glyphs. | |
int | GetLineSpacing (unsigned int characterSize) const |
Get the line spacing. | |
const Image & | GetImage (unsigned int characterSize) const |
Retrieve the image containing the loaded glyphs of a certain size. | |
Font & | operator= (const Font &right) |
Overload of assignment operator. | |
Static Public Member Functions | |
static const Font & | GetDefaultFont () |
Return the default built-in font. |
Class for loading and manipulating character fonts.
Fonts can be loaded from a file or from memory, from the most common types of fonts.
See the LoadFromFile() function for the complete list of supported formats.
Once it is loaded, a sf::Font instance provides three types of informations about the font:
Fonts alone are not very useful: they hold the font data but cannot make anything useful of it. To do so you need to use the sf::Text class, which is able to properly output text with several options such as character size, style, color, position, rotation, etc. This separation allows more flexibility and better performances: indeed a sf::Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a sf::Text is a lightweight object which can combine the glyphs data and metrics of a sf::Font to display any text on a render target. Note that it is also possible to bind several sf::Text instances to the same sf::Font.
It is important to note that the sf::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a sf::Font must not be destructed while it is used by a sf::Text (i.e. never write a function that uses a local sf::Font instance for creating a text).
Usage example:
// Declare a new font sf::Font font; // Load it from a file if (!font.LoadFromFile("arial.ttf")) { // error... } // Create a text which uses our font sf::Text text1; text1.SetFont(font); text1.SetCharacterSize(30); text1.SetStyle(sf::Text::Regular); // Create another text using the same font, but with different parameters sf::Text text2; text2.SetFont(font); text2.SetCharacterSize(50); text1.SetStyle(sf::Text::Italic);
Apart from loading font files, and passing them to instances of sf::Text, you should normally not have to deal directly with this class. However, it may be useful to access the font metrics or rasterized glyphs for advanced usage.
Definition at line 48 of file Font.hpp.
sf::Font::Font | ( | ) |
sf::Font::Font | ( | const Font & | copy | ) |
sf::Font::~Font | ( | ) |
const Font & sf::Font::GetDefaultFont | ( | ) | [static] |
Return the default built-in font.
This font is provided for convenience, it is used by sf::Text instances by default. It is provided so that users don't have to provide and load a font file in order to display text on screen. The font used is Arial.
const Glyph & sf::Font::GetGlyph | ( | Uint32 | codePoint, | |
unsigned int | characterSize, | |||
bool | bold | |||
) | const |
const Image & sf::Font::GetImage | ( | unsigned int | characterSize | ) | const |
Retrieve the image containing the loaded glyphs of a certain size.
The contents of the returned image changes as more glyphs are requested, thus it is not very relevant. It is mainly used internally by sf::Text.
characterSize | Reference character size |
int sf::Font::GetKerning | ( | Uint32 | first, | |
Uint32 | second, | |||
unsigned int | characterSize | |||
) | const |
Get the kerning offset of two glyphs.
The kerning is an extra offset (negative) to apply between two glyphs when rendering them, to make the pair look more "natural". For example, the pair "AV" have a special kerning to make them closer than other characters. Most of the glyphs pairs have a kerning offset of zero, though.
first | Unicode code point of the first character | |
second | Unicode code point of the second character | |
characterSize | Reference character size |
int sf::Font::GetLineSpacing | ( | unsigned int | characterSize | ) | const |
bool sf::Font::LoadFromFile | ( | const std::string & | filename | ) |
Load the font from a file.
The supported font formats are: TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. Note that this function know nothing about the standard fonts installed on the user's system, thus you can't load them directly.
filename | Path of the font file to load |
bool sf::Font::LoadFromMemory | ( | const void * | data, | |
std::size_t | sizeInBytes | |||
) |
Load the font from a file.
The supported font formats are: TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. Note that this function know nothing about the standard fonts installed on the user's system, thus you can't load them directly.
data | Pointer to the file data in memory | |
sizeInBytes | Size of the data to load, in bytes |