Font#

macOS

GTK

Windows

iOS

Android

Web

y

y

y

y

y

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)#
bold()#

Generate a bold version of this font

italic()#

Generate an italic version of this font

measure(text, dpi, tight=False)#
normal_style()#

Generate a normal style version of this font

normal_variant()#

Generate a normal variant of this font

normal_weight()#

Generate a normal weight version of this font

oblique()#

Generate an oblique version of this font

static register(family, path, weight=NORMAL, style=NORMAL, variant=NORMAL)#

Registers a file-based font with it’s 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 registerered 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 (str) – The font family name. This is the name that can be referenced in style definitions.

  • path (str) – The path to the font file.

  • weight (str) – The font weight: Font.NORMAL (default) or a value from Font.FONT_WEIGHTS

  • style (str) – The font style: Font.NORMAL (default) or a value from Font.FONT_STYLES

  • variant (str) – The font variant: Font.NORMAL (default) or a value from Font.FONT_VARIANTS

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, Font.NORMAL is used instead.

Parameters:
  • family (str) – The font family name

  • weight (str) – The font weight: Font.NORMAL (default) or a value from Font.FONT_WEIGHTS

  • style (str) – The font style: Font.NORMAL (default) or a value from Font.FONT_STYLES

  • variant (str) – The font variant: Font.NORMAL (default) or a value from Font.FONT_VARIANTS

Returns:

The font key (str)

small_caps()#

Generate a small-caps variant of this font