Font#

Availability (Key)#

macOS

GTK

Windows

iOS

Android

Web

The font class is used for abstracting the platforms implementation of fonts.

Reference#

class toga.fonts.Font(family, size, style=NORMAL, variant=NORMAL, weight=NORMAL)#
bind(factory=None)#
measure(text, dpi, tight=False)#
static register(family, path, weight=NORMAL, style=NORMAL, variant=NORMAL)#

Registers a file-based font with its family name, style, variant and weight. When invalid values for style, variant or weight are passed, NORMAL will be used.

When a font file includes multiple font weight/style/etc., each variant must be registered separately:

# Register a simple regular font
Font.register("Font Awesome 5 Free Solid", "resources/Font Awesome 5 Free-Solid-900.otf")

# Register a regular and bold font, contained in separate font files
Font.register("Roboto", "resources/Roboto-Regular.ttf")
Font.register("Roboto", "resources/Roboto-Bold.ttf", weight=Font.BOLD)

# Register a single font file that contains both a regular and bold weight
Font.register("Bahnschrift", "resources/Bahnschrift.ttf")
Font.register("Bahnschrift", "resources/Bahnschrift.ttf", weight=Font.BOLD)
Parameters:
  • family – The font family name. This is the name that can be referenced in style definitions.

  • path – The path to the font file.

  • weight – The font weight. Default value is NORMAL.

  • style – The font style. Default value is NORMAL.

  • variant – The font variant. Default value is NORMAL.

static registered_font_key(family, weight, style, variant)#

Creates a key for storing a registered font in the font cache.

If weight, style or variant contain an invalid value, NORMAL is used instead.

Parameters:
  • family – The font family name. This is the name that can be referenced in style definitions.

  • weight – The font weight. Default value is NORMAL.

  • style – The font style. Default value is NORMAL.

  • variant – The font variant. Default value is NORMAL.

Returns:

The font key