DynamicFont

Inherits: Font < Resource < Reference < Object

DynamicFont renders vector font files at runtime.

Description

DynamicFont renders vector font files dynamically at runtime instead of using a prerendered texture atlas like BitmapFont. This trades the faster loading time of BitmapFonts for the ability to change font parameters like size and spacing during runtime. DynamicFontData is used for referencing the font file paths. DynamicFont also supports defining one or more fallback fonts, which will be used when displaying a character not supported by the main font.

DynamicFont uses the FreeType library for rasterization. Supported formats are TrueType (.ttf), OpenType (.otf), Web Open Font Format 1 (.woff), and Web Open Font Format 2 (.woff2).

var dynamic_font = DynamicFont.new()
dynamic_font.font_data = load("res://BarlowCondensed-Bold.ttf")
dynamic_font.size = 64
$"Label".set("custom_fonts/font", dynamic_font)

Note: DynamicFont doesn't support features such as kerning, right-to-left typesetting, ligatures, text shaping, variable fonts and optional font features yet. If you wish to "bake" an optional font feature into a TTF font file, you can use FontForge to do so. In FontForge, use File > Generate Fonts, click Options, choose the desired features then generate the font.

Tutorials

Properties

int

extra_spacing_bottom

0

int

extra_spacing_char

0

int

extra_spacing_space

0

int

extra_spacing_top

0

DynamicFontData

font_data

Color

outline_color

Color( 1, 1, 1, 1 )

int

outline_size

0

int

size

16

bool

use_filter

false

bool

use_mipmaps

false

Methods

void

add_fallback ( DynamicFontData data )

String

get_available_chars ( ) const

DynamicFontData

get_fallback ( int idx ) const

int

get_fallback_count ( ) const

int

get_spacing ( int type ) const

void

remove_fallback ( int idx )

void

set_fallback ( int idx, DynamicFontData data )

void

set_spacing ( int type, int value )


Enumerations

enum SpacingType:

SpacingType SPACING_TOP = 0

Spacing at the top.

SpacingType SPACING_BOTTOM = 1

Spacing at the bottom.

SpacingType SPACING_CHAR = 2

Spacing for each character.

SpacingType SPACING_SPACE = 3

Spacing for the space character.


Property Descriptions

int extra_spacing_bottom = 0

  • void set_spacing ( int type, int value )

  • int get_spacing ( int type ) const

Extra spacing at the bottom in pixels.


int extra_spacing_char = 0

  • void set_spacing ( int type, int value )

  • int get_spacing ( int type ) const

Extra spacing for each character in pixels.

This can be a negative number to make the distance between characters smaller.


int extra_spacing_space = 0

  • void set_spacing ( int type, int value )

  • int get_spacing ( int type ) const

Extra spacing for the space character (in addition to extra_spacing_char) in pixels.

This can be a negative number to make the distance between words smaller.


int extra_spacing_top = 0

  • void set_spacing ( int type, int value )

  • int get_spacing ( int type ) const

Extra spacing at the top in pixels.


DynamicFontData font_data

The font data.


Color outline_color = Color( 1, 1, 1, 1 )

  • void set_outline_color ( Color value )

  • Color get_outline_color ( )

The font outline's color.

Note: It's recommended to leave this at the default value so that you can adjust it in individual controls. For example, if the outline is made black here, it won't be possible to change its color using a Label's font outline modulate theme item.


int outline_size = 0

  • void set_outline_size ( int value )

  • int get_outline_size ( )

The font outline's thickness in pixels (not relative to the font size).


int size = 16

  • void set_size ( int value )

  • int get_size ( )

The font size in pixels.


bool use_filter = false

  • void set_use_filter ( bool value )

  • bool get_use_filter ( )

If true, filtering is used. This makes the font blurry instead of pixelated when scaling it if font oversampling is disabled or ineffective. It's recommended to enable this when using the font in a control whose size changes over time, unless a pixel art aesthetic is desired.


bool use_mipmaps = false

  • void set_use_mipmaps ( bool value )

  • bool get_use_mipmaps ( )

If true, mipmapping is used. This improves the font's appearance when downscaling it if font oversampling is disabled or ineffective.


Method Descriptions

void add_fallback ( DynamicFontData data )

Adds a fallback font.


String get_available_chars ( ) const

Returns a string containing all the characters available in the main and all the fallback fonts.

If a given character is included in more than one font, it appears only once in the returned string.


DynamicFontData get_fallback ( int idx ) const

Returns the fallback font at index idx.


int get_fallback_count ( ) const

Returns the number of fallback fonts.


int get_spacing ( int type ) const

Returns the spacing for the given type (see SpacingType).


void remove_fallback ( int idx )

Removes the fallback font at index idx.


void set_fallback ( int idx, DynamicFontData data )

Sets the fallback font at index idx.


void set_spacing ( int type, int value )

Sets the spacing for type (see SpacingType) to value in pixels (not relative to the font size).