SFML 2.3.2

- Main Page
- Related Pages
- Modules
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Classes | Public Member Functions | List of all members
sf::Font Class Reference
Graphics module

Class for loading and manipulating character fonts. More...

`#include <Font.hpp>`

## Classes
struct | Info
| Holds various information about a font. More...

## Public Member Functions
| Font ()
| Default constructor. More...
| Font (const Font &copy)
| Copy constructor. More...
| ~Font ()
| Destructor. More...
bool | loadFromFile (const std::string &filename)
| Load the font from a file. More...
bool | loadFromMemory (const void *data, std::size_t sizeInBytes)
| Load the font from a file in memory. More...
bool | loadFromStream (InputStream &stream)
| Load the font from a custom stream. More...
const Info & | getInfo () const
| Get the font information. More...
const Glyph & | getGlyph (Uint32 codePoint, unsigned int characterSize, bool bold) const
| Retrieve a glyph of the font. More...
float | getKerning (Uint32 first, Uint32 second, unsigned int characterSize) const
| Get the kerning offset of two glyphs. More...
float | getLineSpacing (unsigned int characterSize) const
| Get the line spacing. More...
float | getUnderlinePosition (unsigned int characterSize) const
| Get the position of the underline. More...
float | getUnderlineThickness (unsigned int characterSize) const
| Get the thickness of the underline. More...
const Texture & | getTexture (unsigned int characterSize) const
| Retrieve the texture containing the loaded glyphs of a certain size. More...
Font & | operator= (const Font &right)
| Overload of assignment operator. More...

## Detailed Description

Class for loading and manipulating character fonts.

Fonts can be loaded from a file, from memory or from a custom stream, and supports 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 information about the font:

- Global metrics, such as the line spacing
- Per-glyph metrics, such as bounding box or kerning
- Pixel representation of glyphs

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);
text2.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.

Note that if the font is a bitmap font, it is not scalable, thus not all requested sizes will be available to use. This needs to be taken into consideration when using sf::Text. If you need to display text of a certain size, make sure the corresponding bitmap font that supports that size is used.

See also
  sf::Text

Definition at line 50 of file Font.hpp.

## Constructor & Destructor Documentation

sf::Font::Font | ( |  | ) |

Default constructor.

This constructor defines an empty font

sf::Font::Font | ( | const Font & | copy | ) |

Copy constructor.

Parameters

  copy | Instance to copy

sf::Font::~Font | ( |  | ) |

Destructor.

Cleans up all the internal resources used by the font

## Member Function Documentation

const Glyph& sf::Font::getGlyph | ( | Uint32 | codePoint,
|  | unsigned int | characterSize,
|  | bool | bold
| ) |  | const

Retrieve a glyph of the font.

If the font is a bitmap font, not all character sizes might be available. If the glyph is not available at the requested size, an empty glyph is returned.

Parameters

  codePoint | Unicode code point of the character to get
  characterSize | Reference character size
  bold | Retrieve the bold version or the regular one?

Returns
  The glyph corresponding to codePoint and characterSize

const Info& sf::Font::getInfo | ( |  | ) | const

Get the font information.

Returns
  A structure that holds the font information

float 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.

Parameters

  first | Unicode code point of the first character
  second | Unicode code point of the second character
  characterSize | Reference character size

Returns
  Kerning value for first and second, in pixels

float sf::Font::getLineSpacing | ( | unsigned int | characterSize | ) | const

Get the line spacing.

Line spacing is the vertical offset to apply between two consecutive lines of text.

Parameters

  characterSize | Reference character size

Returns
  Line spacing, in pixels

const Texture& sf::Font::getTexture | ( | unsigned int | characterSize | ) | const

Retrieve the texture containing the loaded glyphs of a certain size.

The contents of the returned texture changes as more glyphs are requested, thus it is not very relevant. It is mainly used internally by sf::Text.

Parameters

  characterSize | Reference character size

Returns
  Texture containing the glyphs of the requested size

float sf::Font::getUnderlinePosition | ( | unsigned int | characterSize | ) | const

Get the position of the underline.

Underline position is the vertical offset to apply between the baseline and the underline.

Parameters

  characterSize | Reference character size

Returns
  Underline position, in pixels

See also
  getUnderlineThickness

float sf::Font::getUnderlineThickness | ( | unsigned int | characterSize | ) | const

Get the thickness of the underline.

Underline thickness is the vertical size of the underline.

Parameters

  characterSize | Reference character size

Returns
  Underline thickness, in pixels

See also
  getUnderlinePosition

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.

Warning
  SFML cannot preload all the font data in this function, so the file has to remain accessible until the sf::Font object loads a new font or is destroyed.

Parameters

  filename | Path of the font file to load

Returns
  True if loading succeeded, false if it failed

See also
  loadFromMemory, loadFromStream

bool sf::Font::loadFromMemory | ( | const void * | data,
|  | std::size_t | sizeInBytes
| ) |  |

Load the font from a file in memory.

The supported font formats are: TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.

Warning
  SFML cannot preload all the font data in this function, so the buffer pointed by data has to remain valid until the sf::Font object loads a new font or is destroyed.

Parameters

  data | Pointer to the file data in memory
  sizeInBytes | Size of the data to load, in bytes

Returns
  True if loading succeeded, false if it failed

See also
  loadFromFile, loadFromStream

bool sf::Font::loadFromStream | ( | InputStream & | stream | ) |

Load the font from a custom stream.

The supported font formats are: TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42. Warning: SFML cannot preload all the font data in this function, so the contents of stream have to remain valid as long as the font is used.

Warning
  SFML cannot preload all the font data in this function, so the stream has to remain accessible until the sf::Font object loads a new font or is destroyed.

Parameters

  stream | Source stream to read from

Returns
  True if loading succeeded, false if it failed

See also
  loadFromFile, loadFromMemory

Font& sf::Font::operator= | ( | const Font & | right | ) |

Overload of assignment operator.

Parameters

  right | Instance to assign

Returns
  Reference to self

The documentation for this class was generated from the following file:

- Font.hpp