Font

Inherits: Resource < RefCounted < Object

Font class is set of font data sources used to draw text.

Description

Font contains a set of glyphs to represent Unicode characters, as well as the ability to draw it with variable width, ascent, descent and kerning.

Note: A character is a symbol that represents an item (letter, digit etc.) in an abstract way.

Note: A glyph is a bitmap or shape used to draw a one or more characters in a context-dependent manner. Glyph indices are bound to the specific font data source.

Note: If a non of the font data sources contain glyphs for a character used in a string, the character in question will be replaced with a box displaying its hexadecimal code.

var font = Font.new()
font.add_data(load("res://BarlowCondensed-Bold.ttf"))
$"Label".set("custom_fonts/font", font)
$"Label".set("custom_fonts/font_size", 64)

To control font substitution priority use FontData language and script support.

Use language overrides to use same Font stack for multiple languages:

# Use Naskh font for Persian and Nastaʼlīq font for Urdu text.
var font_data_fa = load("res://NotoNaskhArabicUI_Regular.ttf");
font_data_fa.set_language_support_override("fa", true);
font_data_fa.set_language_support_override("ur", false);

var font_data_ur = load("res://NotoNastaliqUrdu_Regular.ttf");
font_data_ur.set_language_support_override("fa", false);
font_data_ur.set_language_support_override("ur", true);

Use script overrides to specify supported scripts for bitmap font or for less common scripts not directly supported by TrueType format:

# Use specified font for Egyptian hieroglyphs.
var font_data = load("res://unifont.ttf");
font_data.set_script_support_override("Egyp", true);

Methods

void

add_data ( FontData data )

void

clear_data ( )

float

draw_char ( RID canvas_item, Vector2 pos, int char, int next=0, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0) ) const

void

draw_multiline_string ( RID canvas_item, Vector2 pos, String text, HAlign align=0, float width=-1, int max_lines=-1, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=99 ) const

void

draw_string ( RID canvas_item, Vector2 pos, String text, HAlign align=0, float width=-1, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=3 ) const

float

get_ascent ( int size=-1 ) const

Vector2

get_char_size ( int char, int next=0, int size=-1 ) const

FontData

get_data ( int idx ) const

int

get_data_count ( ) const

RID

get_data_rid ( int idx ) const

float

get_descent ( int size=-1 ) const

float

get_height ( int size=-1 ) const

Vector2

get_multiline_string_size ( String text, float width=-1, int size=-1, int flags=96 ) const

int

get_spacing ( SpacingType spacing ) const

Vector2

get_string_size ( String text, int size=-1, HAlign align=0, float width=-1, int flags=3 ) const

String

get_supported_chars ( ) const

float

get_underline_position ( int size=-1 ) const

float

get_underline_thickness ( int size=-1 ) const

bool

has_char ( int char ) const

void

remove_data ( int idx )

void

set_data ( int idx, FontData data )

void

set_spacing ( SpacingType spacing, int value )

void

update_changes ( )

Property Descriptions

  • int base_size

Default

16

Setter

set_base_size(value)

Getter

get_base_size()

Default font size.


  • int spacing_bottom

Default

0

Setter

set_spacing(value)

Getter

get_spacing()

Extra spacing at the bottom of the line in pixels.


  • int spacing_top

Default

0

Setter

set_spacing(value)

Getter

get_spacing()

Extra spacing at the top of the line in pixels.


Default

{}

Setter

set_variation_coordinates(value)

Getter

get_variation_coordinates()

Default font variation coordinates.

Method Descriptions

Add font data source to the set.


  • void clear_data ( )

Removes all font data sourcers for the set.


  • float draw_char ( RID canvas_item, Vector2 pos, int char, int next=0, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0) ) const

Draw a single Unicode character char into a canvas item using the font, at a given position, with modulate color, and optionally kerning if next is passed. position specifies the baseline, not the top. To draw from the top, ascent must be added to the Y axis.

Note: Do not use this function to draw strings character by character, use draw_string or TextLine instead.


  • void draw_multiline_string ( RID canvas_item, Vector2 pos, String text, HAlign align=0, float width=-1, int max_lines=-1, int size=-1, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=99 ) const

Breaks text to the lines using rules specified by flags and draws it into a canvas item using the font, at a given position, with modulate color, optionally clipping the width and aligning horizontally. position specifies the baseline of the first line, not the top. To draw from the top, ascent must be added to the Y axis.

See also CanvasItem.draw_multiline_string.


Draw text into a canvas item using the font, at a given position, with modulate color, optionally clipping the width and aligning horizontally. position specifies the baseline, not the top. To draw from the top, ascent must be added to the Y axis.

See also CanvasItem.draw_string.


  • float get_ascent ( int size=-1 ) const

Returns the average font ascent (number of pixels above the baseline).

Note: Real ascent of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the ascent of empty line).


Returns the size of a character, optionally taking kerning into account if the next character is provided.

Note: Do not use this function to calculate width of the string character by character, use get_string_size or TextLine instead. The height returned is the font height (see also get_height) and has no relation to the glyph height.


Returns the font data source at index idx. If the index does not exist, returns null.


  • int get_data_count ( ) const

Returns the number of font data sources.


  • RID get_data_rid ( int idx ) const

Returns TextServer RID of the font data resources.


  • float get_descent ( int size=-1 ) const

Returns the average font descent (number of pixels below the baseline).

Note: Real descent of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the descent of empty line).


  • float get_height ( int size=-1 ) const

Returns the total average font height (ascent plus descent) in pixels.

Note: Real height of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the height of empty line).


Returns the size of a bounding box of a string broken into the lines, taking kerning and advance into account.

See also draw_multiline_string.


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


Returns the size of a bounding box of a string, taking kerning and advance into account.

Note: Real height of the string is context-dependent and can be significantly different from the value returned by get_height.

See also draw_string.


  • String get_supported_chars ( ) const

Returns a string containing all the characters available in the font.

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


  • float get_underline_position ( int size=-1 ) const

Return average pixel offset of the underline below the baseline.

Note: Real underline position of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate.


  • float get_underline_thickness ( int size=-1 ) const

Return average thickness of the underline.

Note: Real underline thickness of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate.


Return true if a Unicode char is available in the font.


  • void remove_data ( int idx )

Removes the font data source at index idx. If the index does not exist, nothing happens.


Sets the font data source at index idx. If the index does not exist, nothing happens.


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


  • void update_changes ( )

After editing a font (changing data sources, etc.). Call this function to propagate changes to controls that might use it.