Font#
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 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)