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
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
Ridpos
Vector2char
longfontSize
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.
Returns
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
Ridpos
Vector2char
longfontSize
intsize
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.
Returns
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.
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
Ridpos
Vector2text
stringalignment
HorizontalAlignmentwidth
floatfontSize
intmaxLines
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.brkFlags
TextServer.LineBreakFlagjustificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
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.
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
Ridpos
Vector2text
stringalignment
HorizontalAlignmentwidth
floatfontSize
intmaxLines
intsize
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.brkFlags
TextServer.LineBreakFlagjustificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
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.
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
Ridpos
Vector2text
stringalignment
HorizontalAlignmentwidth
floatfontSize
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.justificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
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.
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
Ridpos
Vector2text
stringalignment
HorizontalAlignmentwidth
floatfontSize
intsize
intmodulate
Color?If the parameter is null, then the default value is
new Color(1, 1, 1, 1)
.justificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
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
DictionaryfaceIndex
intstrength
floattransform
Transform2D?If the parameter is null, then the default value is
Transform2D.Identity
.spacingTop
intspacingBottom
intspacingSpace
intspacingGlyph
int
Returns
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
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
Returns
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
GetFaceCount()
Returns number of faces in the TrueType / OpenType collection.
public long GetFaceCount()
Returns
GetFontName()
Returns font family name.
public string GetFontName()
Returns
GetFontStretch()
Returns font stretch amount, compared to a normal width. A percentage value between 50%
and 200%
.
public int GetFontStretch()
Returns
GetFontStyle()
Returns font style flags, see TextServer.FontStyle.
public TextServer.FontStyle GetFontStyle()
Returns
GetFontStyleName()
Returns font style name.
public string GetFontStyleName()
Returns
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
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
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.
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
stringalignment
HorizontalAlignmentwidth
floatfontSize
intmaxLines
intbrkFlags
TextServer.LineBreakFlagjustificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
TextServer.Orientation
Returns
GetOpentypeFeatures()
Returns a set of OpenType feature tags. More info: OpenType feature tags.
public Dictionary GetOpentypeFeatures()
Returns
GetOtNameStrings()
Returns Dictionary with OpenType font name strings (localized font names, version, description, license information, sample text, etc.).
public Dictionary GetOtNameStrings()
Returns
GetRids()
Returns Array of valid FontRids, which can be passed to the TextServer methods.
public Array<Rid> GetRids()
Returns
GetSpacing(SpacingType)
Returns the spacing for the given type
(see TextServer.SpacingType).
public int GetSpacing(TextServer.SpacingType spacing)
Parameters
spacing
TextServer.SpacingType
Returns
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
stringalignment
HorizontalAlignmentwidth
floatfontSize
intjustificationFlags
TextServer.JustificationFlagdirection
TextServer.Directionorientation
TextServer.Orientation
Returns
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
GetSupportedFeatureList()
Returns list of OpenType features supported by font.
public Dictionary GetSupportedFeatureList()
Returns
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
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
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
HasChar(long)
Returns true
if a Unicode char
is available in the font.
public bool HasChar(long @char)
Parameters
char
long
Returns
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_nameName of the method to check for.
Returns
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_nameName of the signal to check for.
Returns
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_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsLanguageSupported(string)
Returns true
, if font supports given language (ISO 639 code).
public bool IsLanguageSupported(string language)
Parameters
language
string
Returns
IsScriptSupported(string)
Returns true
, if font supports given script (ISO 15924 code).
public bool IsScriptSupported(string script)
Parameters
script
string
Returns
SetCacheCapacity(int, int)
Sets LRU cache capacity for draw_*
methods.
public void SetCacheCapacity(int singleLine, int multiLine)