Struct sfml::graphics::Font

source ·
#[repr(C)]
pub struct Font { /* private fields */ }
Expand description

Type 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 from_file 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 type, 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).

Apart from loading font files, and passing them to instances of Text, you should normally not have to deal directly with this type. 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.

Implementations§

Get the kerning value corresponding to a given pair of characters in a font

Arguments
  • first - Unicode code point of the first character
  • second - Unicode code point of the second character
  • characterSize - Character size, in pixels

Return the kerning offset, in pixels

Usage Example
let kerning = font.kerning(0, 0, 32);
assert_eq!(kerning, 0.);

Get the line spacing value

Arguments
  • characterSize - Character size, in pixels

Return the line spacing, in pixels

Usage Example
let line_spacing = font.line_spacing(32);
assert_eq!(line_spacing, 35.);

Get a glyph in a font

Arguments
  • codePoint - Unicode code point of the character to get
  • characterSize - Character size, in pixels
  • bold - Retrieve the bold version or the regular one?

Return the corresponding glyph

Usage Example
let glyph = font.glyph(41, 32, false, 5.);
assert_eq!(glyph.bounds(), Rect::new(0., -23., 17., 39.));

Returns the font information.

Usage Example
let font = Font::from_file("examples/resources/sansation.ttf").unwrap();
let font_info = font.info();
assert_eq!(font_info.family, "Sansation");

Returns the position of the underline.

Usage Example
let underline_position = font.underline_position(32);
assert_eq!(underline_position, 3.734375);

Returns the thickness of the underline.

Usage Example
let underline_thickness = font.underline_thickness(32);
assert_eq!(underline_thickness, 2.34375);

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 Font object loads a new font or is destroyed.

Usage Example
let font = match Font::from_file("examples/resources/sansation.ttf") {
    Some(font) => font,
    None => {
        panic!("Failed to read font file!");
    }
};

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.

Safety

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

Returns

True if loading succeeded, false if it failed

See also

Font::from_file, Font::from_memory

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.

Safety

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

Returns

True if loading succeeded, false if it failed

See also Font::from_file, Font::from_stream

Get the texture containing the glyphs of a given size in a font

Arguments
  • character_size - Character size, in pixels
Usage Example
let texture = font.texture(32);
assert_eq!(texture.size(), Vector2::new(128, 128));

Trait Implementations§

Formats the value using the given formatter. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.