Table of Contents

Class Font

Namespace
Godot
Assembly
GodotSharp.dll

Abstract base class for different font types. It has methods for drawing text and font character introspection.

public class Font : Resource, IDisposable
Inheritance
Font
Implements
Derived
Inherited Members

Properties

Fallbacks

Array of fallback Fonts to use as a substitute if a glyph is not found in this current Font.

If this array is empty in a FontVariation, the BaseFont's fallbacks are used instead.

public Array<Font> Fallbacks { get; set; }

Property Value

Array<Font>

Methods

DrawChar(Rid, Vector2, long, int, Color?)

Draw a single Unicode character char into a canvas item using the font, at a given position, with modulate color. pos 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 DrawString(Rid, Vector2, string, HorizontalAlignment, float, int, Color?, JustificationFlag, Direction, Orientation) or TextLine instead.

public float DrawChar(Rid canvasItem, Vector2 pos, long @char, int fontSize, Color? modulate = null)

Parameters

canvasItem Rid
pos Vector2
char long
fontSize int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

Returns

float

DrawCharOutline(Rid, Vector2, long, int, int, Color?)

Draw a single Unicode character char outline into a canvas item using the font, at a given position, with modulate color and size outline size. pos 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 DrawString(Rid, Vector2, string, HorizontalAlignment, float, int, Color?, JustificationFlag, Direction, Orientation) or TextLine instead.

public float DrawCharOutline(Rid canvasItem, Vector2 pos, long @char, int fontSize, int size = -1, Color? modulate = null)

Parameters

canvasItem Rid
pos Vector2
char long
fontSize int
size int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

Returns

float

DrawMultilineString(Rid, Vector2, string, HorizontalAlignment, float, int, int, Color?, LineBreakFlag, JustificationFlag, Direction, Orientation)

Breaks text into lines using rules specified by brkFlags and draws it into a canvas item using the font, at a given position, with modulate color, optionally clipping the width and aligning horizontally. pos 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 DrawMultilineString(Font, Vector2, string, HorizontalAlignment, float, int, int, Color?, LineBreakFlag, JustificationFlag, Direction, Orientation).

public void DrawMultilineString(Rid canvasItem, Vector2 pos, string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, int maxLines = -1, Color? modulate = null, TextServer.LineBreakFlag brkFlags = LineBreakFlag.Mandatory | LineBreakFlag.WordBound, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

canvasItem Rid
pos Vector2
text string
alignment HorizontalAlignment
width float
fontSize int
maxLines int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

brkFlags TextServer.LineBreakFlag
justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

DrawMultilineStringOutline(Rid, Vector2, string, HorizontalAlignment, float, int, int, int, Color?, LineBreakFlag, JustificationFlag, Direction, Orientation)

Breaks text to the lines using rules specified by brkFlags and draws text outline into a canvas item using the font, at a given position, with modulate color and size outline size, optionally clipping the width and aligning horizontally. pos 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 DrawMultilineStringOutline(Font, Vector2, string, HorizontalAlignment, float, int, int, int, Color?, LineBreakFlag, JustificationFlag, Direction, Orientation).

public void DrawMultilineStringOutline(Rid canvasItem, Vector2 pos, string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, int maxLines = -1, int size = 1, Color? modulate = null, TextServer.LineBreakFlag brkFlags = LineBreakFlag.Mandatory | LineBreakFlag.WordBound, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

canvasItem Rid
pos Vector2
text string
alignment HorizontalAlignment
width float
fontSize int
maxLines int
size int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

brkFlags TextServer.LineBreakFlag
justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

DrawString(Rid, Vector2, string, HorizontalAlignment, float, int, Color?, JustificationFlag, Direction, Orientation)

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

See also DrawString(Font, Vector2, string, HorizontalAlignment, float, int, Color?, JustificationFlag, Direction, Orientation).

public void DrawString(Rid canvasItem, Vector2 pos, string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, Color? modulate = null, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

canvasItem Rid
pos Vector2
text string
alignment HorizontalAlignment
width float
fontSize int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

DrawStringOutline(Rid, Vector2, string, HorizontalAlignment, float, int, int, Color?, JustificationFlag, Direction, Orientation)

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

See also DrawStringOutline(Font, Vector2, string, HorizontalAlignment, float, int, int, Color?, JustificationFlag, Direction, Orientation).

public void DrawStringOutline(Rid canvasItem, Vector2 pos, string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, int size = 1, Color? modulate = null, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

canvasItem Rid
pos Vector2
text string
alignment HorizontalAlignment
width float
fontSize int
size int
modulate Color?

If the parameter is null, then the default value is new Color(1, 1, 1, 1).

justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

FindVariation(Dictionary, int, float, Transform2D?, int, int, int, int)

Returns TextServer RID of the font cache for specific variation.

public Rid FindVariation(Dictionary variationCoordinates, int faceIndex = 0, float strength = 0, Transform2D? transform = null, int spacingTop = 0, int spacingBottom = 0, int spacingSpace = 0, int spacingGlyph = 0)

Parameters

variationCoordinates Dictionary
faceIndex int
strength float
transform Transform2D?

If the parameter is null, then the default value is Transform2D.Identity.

spacingTop int
spacingBottom int
spacingSpace int
spacingGlyph int

Returns

Rid

GetAscent(int)

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).

public float GetAscent(int fontSize = 16)

Parameters

fontSize int

Returns

float

GetCharSize(long, int)

Returns the size of a character. Does not take kerning into account.

Note: Do not use this function to calculate width of the string character by character, use GetStringSize(string, HorizontalAlignment, float, int, JustificationFlag, Direction, Orientation) or TextLine instead. The height returned is the font height (see also GetHeight(int)) and has no relation to the glyph height.

public Vector2 GetCharSize(long @char, int fontSize)

Parameters

char long
fontSize int

Returns

Vector2

GetDescent(int)

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).

public float GetDescent(int fontSize = 16)

Parameters

fontSize int

Returns

float

GetFaceCount()

Returns number of faces in the TrueType / OpenType collection.

public long GetFaceCount()

Returns

long

GetFontName()

Returns font family name.

public string GetFontName()

Returns

string

GetFontStretch()

Returns font stretch amount, compared to a normal width. A percentage value between 50% and 200%.

public int GetFontStretch()

Returns

int

GetFontStyle()

Returns font style flags, see TextServer.FontStyle.

public TextServer.FontStyle GetFontStyle()

Returns

TextServer.FontStyle

GetFontStyleName()

Returns font style name.

public string GetFontStyleName()

Returns

string

GetFontWeight()

Returns weight (boldness) of the font. A value in the 100...999 range, normal font weight is 400, bold font weight is 700.

public int GetFontWeight()

Returns

int

GetHeight(int)

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).

public float GetHeight(int fontSize = 16)

Parameters

fontSize int

Returns

float

GetMultilineStringSize(string, HorizontalAlignment, float, int, int, LineBreakFlag, JustificationFlag, Direction, Orientation)

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

See also DrawMultilineString(Rid, Vector2, string, HorizontalAlignment, float, int, int, Color?, LineBreakFlag, JustificationFlag, Direction, Orientation).

public Vector2 GetMultilineStringSize(string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, int maxLines = -1, TextServer.LineBreakFlag brkFlags = LineBreakFlag.Mandatory | LineBreakFlag.WordBound, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

text string
alignment HorizontalAlignment
width float
fontSize int
maxLines int
brkFlags TextServer.LineBreakFlag
justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

Returns

Vector2

GetOpentypeFeatures()

Returns a set of OpenType feature tags. More info: OpenType feature tags.

public Dictionary GetOpentypeFeatures()

Returns

Dictionary

GetOtNameStrings()

Returns Dictionary with OpenType font name strings (localized font names, version, description, license information, sample text, etc.).

public Dictionary GetOtNameStrings()

Returns

Dictionary

GetRids()

Returns Array of valid FontRids, which can be passed to the TextServer methods.

public Array<Rid> GetRids()

Returns

Array<Rid>

GetSpacing(SpacingType)

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

public int GetSpacing(TextServer.SpacingType spacing)

Parameters

spacing TextServer.SpacingType

Returns

int

GetStringSize(string, HorizontalAlignment, float, int, JustificationFlag, Direction, Orientation)

Returns the size of a bounding box of a single-line string, taking kerning, advance and subpixel positioning into account. See also GetMultilineStringSize(string, HorizontalAlignment, float, int, int, LineBreakFlag, JustificationFlag, Direction, Orientation) and DrawString(Rid, Vector2, string, HorizontalAlignment, float, int, Color?, JustificationFlag, Direction, Orientation).

For example, to get the string size as displayed by a single-line Label, use:

Label label = GetNode<Label>("Label");
  Vector2 stringSize = label.GetThemeFont("font").GetStringSize(label.Text, HorizontalAlignment.Left, -1, label.GetThemeFontSize("font_size"));

Note: Since kerning, advance and subpixel positioning are taken into account by GetStringSize(string, HorizontalAlignment, float, int, JustificationFlag, Direction, Orientation), using separate GetStringSize(string, HorizontalAlignment, float, int, JustificationFlag, Direction, Orientation) calls on substrings of a string then adding the results together will return a different result compared to using a single GetStringSize(string, HorizontalAlignment, float, int, JustificationFlag, Direction, Orientation) call on the full string.

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

public Vector2 GetStringSize(string text, HorizontalAlignment alignment = HorizontalAlignment.Left, float width = -1, int fontSize = 16, TextServer.JustificationFlag justificationFlags = JustificationFlag.Kashida | JustificationFlag.WordBound, TextServer.Direction direction = Direction.Auto, TextServer.Orientation orientation = Orientation.Horizontal)

Parameters

text string
alignment HorizontalAlignment
width float
fontSize int
justificationFlags TextServer.JustificationFlag
direction TextServer.Direction
orientation TextServer.Orientation

Returns

Vector2

GetSupportedChars()

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.

public string GetSupportedChars()

Returns

string

GetSupportedFeatureList()

Returns list of OpenType features supported by font.

public Dictionary GetSupportedFeatureList()

Returns

Dictionary

GetSupportedVariationList()

Returns list of supported variation coordinates, each coordinate is returned as tag: Vector3i(min_value,max_value,default_value).

Font variations allow for continuous change of glyph characteristics along some given design axis, such as weight, width or slant.

To print available variation axes of a variable font:

var fv = FontVariation.new()
  fv.set_base_font = load("res://RobotoFlex.ttf")
  var variation_list = fv.get_supported_variation_list()
  for tag in variation_list:
      var name = TextServerManager.get_primary_interface().tag_to_name(tag)
      var values = variation_list[tag]
      print("variation axis: %s (%d)\n\tmin, max, default: %s" % [name, tag, values])

Note: To set and get variation coordinates of a FontVariation, use VariationOpentype.

public Dictionary GetSupportedVariationList()

Returns

Dictionary

GetUnderlinePosition(int)

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.

public float GetUnderlinePosition(int fontSize = 16)

Parameters

fontSize int

Returns

float

GetUnderlineThickness(int)

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.

public float GetUnderlineThickness(int fontSize = 16)

Parameters

fontSize int

Returns

float

HasChar(long)

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

public bool HasChar(long @char)

Parameters

char long

Returns

bool

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsLanguageSupported(string)

Returns true, if font supports given language (ISO 639 code).

public bool IsLanguageSupported(string language)

Parameters

language string

Returns

bool

IsScriptSupported(string)

Returns true, if font supports given script (ISO 15924 code).

public bool IsScriptSupported(string script)

Parameters

script string

Returns

bool

SetCacheCapacity(int, int)

Sets LRU cache capacity for draw_* methods.

public void SetCacheCapacity(int singleLine, int multiLine)

Parameters

singleLine int
multiLine int