Font

sfml.graphics.Font
See theFont companion object
trait 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 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 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 Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a Text is a lightweight object which can combine the glyphs data and metrics of a Font to display any text on a render target. Note that it is also possible to bind several Text instances to the same Font.

It is important to note that the Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a Font must not be destructed while it is used by a Text (i.e. never write a function that uses a local Font instance for creating a text).

// Declare a new font
val font = Font()

// Load it from a file
if !(font.loadFromFile("arial.ttf")) then
   // error...
   ???

// Create a text which uses our font
val text1 = Text();
text1.font = font
text1.characterSize = 30


// Create another text using the same font, but with different parameters
val text2 = Text()
text2.font = font
text2.characterSize = 50

Apart from loading font files, and passing them to instances of 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 Text. If you need to display text of a certain size, make sure the corresponding bitmap font that supports that size is used.

Attributes

See also
Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def loadFromFile(filename: String): Boolean

Load the font from a file.

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 knows nothing about the standard fonts installed on the user's system, thus you can't load them directly.

Value parameters

filename

Path of the font file to load

Attributes

Returns

True if loading succeeded, false if it failed

Note

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