Font


Object Hierarchy:

Object hierarchy for Font

Description:

[ Compact ]
[ CCode ( cname = "sfFont" , copy_function = "sfFont_copy" , cprefix = "sfFont_" , free_function = "sfFont_destroy" ) ]
public class Font

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
var font = new Font.fromFile("arial.ttf");

if (font == null) {
// error...
}

// Create a text which uses our font
var text1 = new Text();
text1.setFont(font);
text1.setCharacterSize(30);
text1.setStyle(sf.TextStyle.Regular);

// Create another text using the same font, but with different parameters
var text2 = new Text();
text2.setFont(font);
text2.setCharacterSize(50);
text2.setStyle(sf.TextStyle.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


Namespace: sf
Package: sfml

Content:

Creation methods:

Methods: