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)
var font = new Font();
font.AddData(ResourceLoader.Load<FontData>("res://BarlowCondensed-Bold.ttf"));
GetNode("Label").Set("custom_fonts/font", font);
GetNode("Label").Set("custom_font_sizes/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 Naskh font for Persian and Nastaʼlīq font for Urdu text.
var fontDataFA = ResourceLoader.Load<FontData>("res://NotoNaskhArabicUI_Regular.ttf");
fontDataFA.SetLanguageSupportOverride("fa", true);
fontDataFA.SetLanguageSupportOverride("ur", false);
var fontDataUR = ResourceLoader.Load<FontData>("res://NotoNastaliqUrdu_Regular.ttf");
fontDataUR.SetLanguageSupportOverride("fa", false);
fontDataUR.SetLanguageSupportOverride("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);
// Use specified font for Egyptian hieroglyphs.
var fontData = ResourceLoader.Load<FontData>("res://unifont.ttf");
fontData.SetScriptSupportOverride("Egyp", true);
Properties¶
|
||
|
||
|
Methods¶
void |
|
void |
clear_data ( ) |
draw_char ( RID canvas_item, Vector2 pos, int char, int next=0, int size=16, 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, HorizontalAlignment alignment=0, float width=-1, int max_lines=-1, int size=16, 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, HorizontalAlignment alignment=0, float width=-1, int size=16, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=3 ) const |
get_ascent ( int size=16 ) const |
|
get_char_size ( int char, int next=0, int size=16 ) const |
|
get_data_count ( ) const |
|
get_data_rid ( int idx ) const |
|
get_descent ( int size=16 ) const |
|
get_height ( int size=16 ) const |
|
get_multiline_string_size ( String text, float width=-1, int size=16, int flags=96 ) const |
|
get_spacing ( SpacingType spacing ) const |
|
get_string_size ( String text, int size=16, HorizontalAlignment alignment=0, float width=-1, int flags=3 ) const |
|
get_supported_chars ( ) const |
|
get_underline_position ( int size=16 ) const |
|
get_underline_thickness ( int size=16 ) const |
|
void |
remove_data ( int idx ) |
void |
|
void |
set_spacing ( SpacingType spacing, int value ) |
void |
update_changes ( ) |
Property Descriptions¶
int spacing_bottom
Default |
|
Setter |
set_spacing(value) |
Getter |
get_spacing() |
Extra spacing at the bottom of the line in pixels.
int spacing_top
Default |
|
Setter |
set_spacing(value) |
Getter |
get_spacing() |
Extra spacing at the top of the line in pixels.
Dictionary variation_coordinates
Default |
|
Setter |
set_variation_coordinates(value) |
Getter |
get_variation_coordinates() |
Default font variation coordinates.
Method Descriptions¶
void add_data ( FontData data )
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=16, 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, HorizontalAlignment alignment=0, float width=-1, int max_lines=-1, int size=16, 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.
void draw_string ( RID canvas_item, Vector2 pos, String text, HorizontalAlignment alignment=0, float width=-1, int size=16, Color modulate=Color(1, 1, 1, 1), int outline_size=0, Color outline_modulate=Color(1, 1, 1, 0), int flags=3 ) const
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.
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.
Returns TextServer RID of the font data resources.
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).
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.
int get_spacing ( SpacingType spacing ) const
Returns the spacing for the given type
(see SpacingType).
Vector2 get_string_size ( String text, int size=16, HorizontalAlignment alignment=0, float width=-1, int flags=3 ) const
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.
Returns 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.
Returns 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.
Returns 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.
void set_spacing ( SpacingType spacing, int value )
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.