:github_url: hide .. Generated automatically by doc/tools/make_rst.py in Godot's source tree. .. DO NOT EDIT THIS FILE, but the Font.xml source instead. .. The source is found in doc/classes or modules//doc_classes. .. _class_Font: Font ==== **Inherits:** :ref:`Resource` **<** :ref:`RefCounted` **<** :ref:`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. .. tabs:: .. code-tab:: gdscript 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) .. code-tab:: csharp var font = new Font(); font.AddData(ResourceLoader.Load("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 :ref:`FontData` language and script support. Use language overrides to use same ``Font`` stack for multiple languages: .. tabs:: .. code-tab:: gdscript # 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); .. code-tab:: csharp // Use Naskh font for Persian and Nastaʼlīq font for Urdu text. var fontDataFA = ResourceLoader.Load("res://NotoNaskhArabicUI_Regular.ttf"); fontDataFA.SetLanguageSupportOverride("fa", true); fontDataFA.SetLanguageSupportOverride("ur", false); var fontDataUR = ResourceLoader.Load("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: .. tabs:: .. code-tab:: gdscript # Use specified font for Egyptian hieroglyphs. var font_data = load("res://unifont.ttf"); font_data.set_script_support_override("Egyp", true); .. code-tab:: csharp // Use specified font for Egyptian hieroglyphs. var fontData = ResourceLoader.Load("res://unifont.ttf"); fontData.SetScriptSupportOverride("Egyp", true); Properties ---------- +-------------------------------------+-------------------------------------------------------------------------+--------+ | :ref:`int` | :ref:`spacing_bottom` | ``0`` | +-------------------------------------+-------------------------------------------------------------------------+--------+ | :ref:`int` | :ref:`spacing_top` | ``0`` | +-------------------------------------+-------------------------------------------------------------------------+--------+ | :ref:`Dictionary` | :ref:`variation_coordinates` | ``{}`` | +-------------------------------------+-------------------------------------------------------------------------+--------+ Methods ------- +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`add_data` **(** :ref:`FontData` data **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`clear_data` **(** **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`draw_char` **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`int` char, :ref:`int` next=0, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`Color` outline_modulate=Color(1, 1, 1, 0) **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`draw_multiline_string` **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`String` text, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`int` max_lines=-1, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`Color` outline_modulate=Color(1, 1, 1, 0), :ref:`int` flags=99 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`draw_string` **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`String` text, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`Color` outline_modulate=Color(1, 1, 1, 0), :ref:`int` flags=3 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_ascent` **(** :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2` | :ref:`get_char_size` **(** :ref:`int` char, :ref:`int` next=0, :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`FontData` | :ref:`get_data` **(** :ref:`int` idx **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_data_count` **(** **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`RID` | :ref:`get_data_rid` **(** :ref:`int` idx **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_descent` **(** :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_height` **(** :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2` | :ref:`get_multiline_string_size` **(** :ref:`String` text, :ref:`float` width=-1, :ref:`int` size=16, :ref:`int` flags=96 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_spacing` **(** :ref:`SpacingType` spacing **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2` | :ref:`get_string_size` **(** :ref:`String` text, :ref:`int` size=16, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`int` flags=3 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_supported_chars` **(** **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_underline_position` **(** :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`float` | :ref:`get_underline_thickness` **(** :ref:`int` size=16 **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_char` **(** :ref:`int` char **)** |const| | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`remove_data` **(** :ref:`int` idx **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`set_data` **(** :ref:`int` idx, :ref:`FontData` data **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`set_spacing` **(** :ref:`SpacingType` spacing, :ref:`int` value **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | void | :ref:`update_changes` **(** **)** | +---------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ Property Descriptions --------------------- .. _class_Font_property_spacing_bottom: - :ref:`int` **spacing_bottom** +-----------+--------------------+ | *Default* | ``0`` | +-----------+--------------------+ | *Setter* | set_spacing(value) | +-----------+--------------------+ | *Getter* | get_spacing() | +-----------+--------------------+ Extra spacing at the bottom of the line in pixels. ---- .. _class_Font_property_spacing_top: - :ref:`int` **spacing_top** +-----------+--------------------+ | *Default* | ``0`` | +-----------+--------------------+ | *Setter* | set_spacing(value) | +-----------+--------------------+ | *Getter* | get_spacing() | +-----------+--------------------+ Extra spacing at the top of the line in pixels. ---- .. _class_Font_property_variation_coordinates: - :ref:`Dictionary` **variation_coordinates** +-----------+----------------------------------+ | *Default* | ``{}`` | +-----------+----------------------------------+ | *Setter* | set_variation_coordinates(value) | +-----------+----------------------------------+ | *Getter* | get_variation_coordinates() | +-----------+----------------------------------+ Default font `variation coordinates `__. Method Descriptions ------------------- .. _class_Font_method_add_data: - void **add_data** **(** :ref:`FontData` data **)** Add font data source to the set. ---- .. _class_Font_method_clear_data: - void **clear_data** **(** **)** Removes all font data sourcers for the set. ---- .. _class_Font_method_draw_char: - :ref:`float` **draw_char** **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`int` char, :ref:`int` next=0, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`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 :ref:`draw_string` or :ref:`TextLine` instead. ---- .. _class_Font_method_draw_multiline_string: - void **draw_multiline_string** **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`String` text, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`int` max_lines=-1, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`Color` outline_modulate=Color(1, 1, 1, 0), :ref:`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 :ref:`CanvasItem.draw_multiline_string`. ---- .. _class_Font_method_draw_string: - void **draw_string** **(** :ref:`RID` canvas_item, :ref:`Vector2` pos, :ref:`String` text, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`int` size=16, :ref:`Color` modulate=Color(1, 1, 1, 1), :ref:`int` outline_size=0, :ref:`Color` outline_modulate=Color(1, 1, 1, 0), :ref:`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 :ref:`CanvasItem.draw_string`. ---- .. _class_Font_method_get_ascent: - :ref:`float` **get_ascent** **(** :ref:`int` size=16 **)** |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). ---- .. _class_Font_method_get_char_size: - :ref:`Vector2` **get_char_size** **(** :ref:`int` char, :ref:`int` next=0, :ref:`int` size=16 **)** |const| 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 :ref:`get_string_size` or :ref:`TextLine` instead. The height returned is the font height (see also :ref:`get_height`) and has no relation to the glyph height. ---- .. _class_Font_method_get_data: - :ref:`FontData` **get_data** **(** :ref:`int` idx **)** |const| Returns the font data source at index ``idx``. If the index does not exist, returns ``null``. ---- .. _class_Font_method_get_data_count: - :ref:`int` **get_data_count** **(** **)** |const| Returns the number of font data sources. ---- .. _class_Font_method_get_data_rid: - :ref:`RID` **get_data_rid** **(** :ref:`int` idx **)** |const| Returns TextServer RID of the font data resources. ---- .. _class_Font_method_get_descent: - :ref:`float` **get_descent** **(** :ref:`int` size=16 **)** |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). ---- .. _class_Font_method_get_height: - :ref:`float` **get_height** **(** :ref:`int` size=16 **)** |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). ---- .. _class_Font_method_get_multiline_string_size: - :ref:`Vector2` **get_multiline_string_size** **(** :ref:`String` text, :ref:`float` width=-1, :ref:`int` size=16, :ref:`int` flags=96 **)** |const| Returns the size of a bounding box of a string broken into the lines, taking kerning and advance into account. See also :ref:`draw_multiline_string`. ---- .. _class_Font_method_get_spacing: - :ref:`int` **get_spacing** **(** :ref:`SpacingType` spacing **)** |const| Returns the spacing for the given ``type`` (see :ref:`SpacingType`). ---- .. _class_Font_method_get_string_size: - :ref:`Vector2` **get_string_size** **(** :ref:`String` text, :ref:`int` size=16, :ref:`HorizontalAlignment` alignment=0, :ref:`float` width=-1, :ref:`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 :ref:`get_height`. See also :ref:`draw_string`. ---- .. _class_Font_method_get_supported_chars: - :ref:`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. ---- .. _class_Font_method_get_underline_position: - :ref:`float` **get_underline_position** **(** :ref:`int` size=16 **)** |const| 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. ---- .. _class_Font_method_get_underline_thickness: - :ref:`float` **get_underline_thickness** **(** :ref:`int` size=16 **)** |const| 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. ---- .. _class_Font_method_has_char: - :ref:`bool` **has_char** **(** :ref:`int` char **)** |const| Returns ``true`` if a Unicode ``char`` is available in the font. ---- .. _class_Font_method_remove_data: - void **remove_data** **(** :ref:`int` idx **)** Removes the font data source at index ``idx``. If the index does not exist, nothing happens. ---- .. _class_Font_method_set_data: - void **set_data** **(** :ref:`int` idx, :ref:`FontData` data **)** Sets the font data source at index ``idx``. If the index does not exist, nothing happens. ---- .. _class_Font_method_set_spacing: - void **set_spacing** **(** :ref:`SpacingType` spacing, :ref:`int` value **)** Sets the spacing for ``type`` (see :ref:`SpacingType`) to ``value`` in pixels (not relative to the font size). ---- .. _class_Font_method_update_changes: - void **update_changes** **(** **)** After editing a font (changing data sources, etc.). Call this function to propagate changes to controls that might use it. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`