FontFile

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

FontFile contains a set of glyphs to represent Unicode characters imported from a font file, as well as a cache of rasterized glyphs, and a set of fallback Fonts to use.

Use FontVariation to access specific OpenType variation of the font, create simulated bold / slanted version, and draw lines of text.

For more complex text processing, use FontVariation in conjunction with TextLine or TextParagraph.

Supported font formats:

- Dynamic font importer: TrueType (.ttf), TrueType collection (.ttc), OpenType (.otf), OpenType collection (.otc), WOFF (.woff), WOFF2 (.woff2), Type 1 (.pfb, .pfm).

- Bitmap font importer: AngelCode BMFont (.fnt, .font), text and binary (version 3) format variants.

- Monospace image font importer: All supported image formats.

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

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

Note: If none 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.

package main

import (
	"graphics.gd/classdb/FontFile"
	"graphics.gd/classdb/Label"
	"graphics.gd/classdb/Resource"
)

func ExampleFontFile(label Label.Instance) {
	var f = Resource.Load[FontFile.Instance]("res://BarlowCondensed-Bold.ttf")
	label.AsControl().AddThemeFontOverride("font", f.AsFont())
	label.AsControl().AddThemeFontSizeOverride("font_size", 64)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsFontFile() Instance
}

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsFont

func (self *Extension[T]) AsFont() Font.Instance

func (*Extension[T]) AsFontFile

func (self *Extension[T]) AsFontFile() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

func (*Extension[T]) AsResource

func (self *Extension[T]) AsResource() Resource.Instance

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.FontFile

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AllowSystemFallback

func (self Instance) AllowSystemFallback() bool

If set to true, system fonts can be automatically used as fallbacks.

func (Instance) Antialiasing

func (self Instance) Antialiasing() TextServer.FontAntialiasing

Font anti-aliasing mode.

func (Instance) AsFont

func (self Instance) AsFont() Font.Instance

func (Instance) AsFontFile

func (self Instance) AsFontFile() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) AsResource

func (self Instance) AsResource() Resource.Instance

func (Instance) ClearCache

func (self Instance) ClearCache()

Removes all font cache entries.

func (Instance) ClearGlyphs

func (self Instance) ClearGlyphs(cache_index int, size Vector2i.XY)

Removes all rendered glyph information from the cache entry.

Note: This function will not remove textures associated with the glyphs, use RemoveTexture to remove them manually.

func (Instance) ClearKerningMap

func (self Instance) ClearKerningMap(cache_index int, size int)

Removes all kerning overrides.

func (Instance) ClearSizeCache

func (self Instance) ClearSizeCache(cache_index int)

Removes all font sizes from the cache entry.

func (Instance) ClearTextures

func (self Instance) ClearTextures(cache_index int, size Vector2i.XY)

Removes all textures from font cache entry.

Note: This function will not remove glyphs associated with the texture, use RemoveGlyph to remove them manually.

func (Instance) Data

func (self Instance) Data() []byte

Contents of the dynamic font source file.

func (Instance) DisableEmbeddedBitmaps

func (self Instance) DisableEmbeddedBitmaps() bool

If set to true, embedded font bitmap loading is disabled (bitmap-only and color fonts ignore this property).

func (Instance) FixedSize

func (self Instance) FixedSize() int

Font size, used only for the bitmap fonts.

func (Instance) FixedSizeScaleMode

func (self Instance) FixedSizeScaleMode() TextServer.FixedSizeScaleMode

Scaling mode, used only for the bitmap fonts with FixedSize greater than zero.

func (Instance) ForceAutohinter

func (self Instance) ForceAutohinter() bool

If set to true, auto-hinting is supported and preferred over font built-in hinting. Used by dynamic fonts only (MSDF fonts don't support hinting).

func (Instance) GenerateMipmaps

func (self Instance) GenerateMipmaps() bool

If set to true, generate mipmaps for the font textures.

func (Instance) GetCacheAscent

func (self Instance) GetCacheAscent(cache_index int, size int) Float.X

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

func (Instance) GetCacheCount

func (self Instance) GetCacheCount() int

Returns number of the font cache entries.

func (Instance) GetCacheDescent

func (self Instance) GetCacheDescent(cache_index int, size int) Float.X

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

func (Instance) GetCacheScale

func (self Instance) GetCacheScale(cache_index int, size int) Float.X

Returns scaling factor of the color bitmap font.

func (Instance) GetCacheUnderlinePosition

func (self Instance) GetCacheUnderlinePosition(cache_index int, size int) Float.X

Returns pixel offset of the underline below the baseline.

func (Instance) GetCacheUnderlineThickness

func (self Instance) GetCacheUnderlineThickness(cache_index int, size int) Float.X

Returns thickness of the underline in pixels.

func (Instance) GetCharFromGlyphIndex

func (self Instance) GetCharFromGlyphIndex(size int, glyph_index int) int

Returns character code associated with 'glyph_index', or 0 if 'glyph_index' is invalid. See GetGlyphIndex.

func (Instance) GetEmbolden

func (self Instance) GetEmbolden(cache_index int) Float.X

Returns embolden strength, if is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.

func (Instance) GetExtraBaselineOffset

func (self Instance) GetExtraBaselineOffset(cache_index int) Float.X

Returns extra baseline offset (as a fraction of font height).

func (Instance) GetExtraSpacing

func (self Instance) GetExtraSpacing(cache_index int, spacing TextServer.SpacingType) int

Returns spacing for 'spacing' in pixels (not relative to the font size).

func (Instance) GetFaceIndex

func (self Instance) GetFaceIndex(cache_index int) int

Returns an active face index in the TrueType / OpenType collection.

func (Instance) GetGlyphAdvance

func (self Instance) GetGlyphAdvance(cache_index int, size int, glyph int) Vector2.XY

Returns glyph advance (offset of the next glyph).

Note: Advance for glyphs outlines is the same as the base glyph advance and is not saved.

func (Instance) GetGlyphIndex

func (self Instance) GetGlyphIndex(size int, char int, variation_selector int) int

Returns the glyph index of a 'char', optionally modified by the 'variation_selector'.

func (Instance) GetGlyphList

func (self Instance) GetGlyphList(cache_index int, size Vector2i.XY) []int32

Returns list of rendered glyphs in the cache entry.

func (Instance) GetGlyphOffset

func (self Instance) GetGlyphOffset(cache_index int, size Vector2i.XY, glyph int) Vector2.XY

Returns glyph offset from the baseline.

func (Instance) GetGlyphSize

func (self Instance) GetGlyphSize(cache_index int, size Vector2i.XY, glyph int) Vector2.XY

Returns glyph size.

func (Instance) GetGlyphTextureIdx

func (self Instance) GetGlyphTextureIdx(cache_index int, size Vector2i.XY, glyph int) int

Returns index of the cache texture containing the glyph.

func (Instance) GetGlyphUvRect

func (self Instance) GetGlyphUvRect(cache_index int, size Vector2i.XY, glyph int) Rect2.PositionSize

Returns rectangle in the cache texture containing the glyph.

func (Instance) GetKerning

func (self Instance) GetKerning(cache_index int, size int, glyph_pair Vector2i.XY) Vector2.XY

Returns kerning for the pair of glyphs.

func (Instance) GetKerningList

func (self Instance) GetKerningList(cache_index int, size int) []Vector2i.XY

Returns list of the kerning overrides.

func (Instance) GetLanguageSupportOverride

func (self Instance) GetLanguageSupportOverride(language string) bool

Returns true if support override is enabled for the 'language'.

func (Instance) GetLanguageSupportOverrides

func (self Instance) GetLanguageSupportOverrides() []string

Returns list of language support overrides.

func (Instance) GetScriptSupportOverride

func (self Instance) GetScriptSupportOverride(script string) bool

Returns true if support override is enabled for the 'script'.

func (Instance) GetScriptSupportOverrides

func (self Instance) GetScriptSupportOverrides() []string

Returns list of script support overrides.

func (Instance) GetSizeCacheList

func (self Instance) GetSizeCacheList(cache_index int) []Vector2i.XY

Returns list of the font sizes in the cache. Each size is Vector2i.XY with font size and outline size.

func (Instance) GetTextureCount

func (self Instance) GetTextureCount(cache_index int, size Vector2i.XY) int

Returns number of textures used by font cache entry.

func (Instance) GetTextureImage

func (self Instance) GetTextureImage(cache_index int, size Vector2i.XY, texture_index int) Image.Instance

Returns a copy of the font cache texture image.

func (Instance) GetTextureOffsets

func (self Instance) GetTextureOffsets(cache_index int, size Vector2i.XY, texture_index int) []int32

Returns a copy of the array containing glyph packing data.

func (Instance) GetTransform

func (self Instance) GetTransform(cache_index int) Transform2D.OriginXY

Returns 2D transform, applied to the font outlines, can be used for slanting, flipping and rotating glyphs.

func (Instance) GetVariationCoordinates

func (self Instance) GetVariationCoordinates(cache_index int) map[string]float32

Returns variation coordinates for the specified font cache entry. See Font.GetSupportedVariationList for more info.

func (Instance) Hinting

func (self Instance) Hinting() TextServer.Hinting

Font hinting mode. Used by dynamic fonts only.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) KeepRoundingRemainders

func (self Instance) KeepRoundingRemainders() bool

If set to true, when aligning glyphs to the pixel boundaries rounding remainders are accumulated to ensure more uniform glyph distribution. This setting has no effect if subpixel positioning is enabled.

func (Instance) LoadBitmapFont

func (self Instance) LoadBitmapFont(path string) error

Loads an AngelCode BMFont (.fnt, .font) bitmap font from file 'path'.

Warning: This method should only be used in the editor or in cases when you need to load external fonts at run-time, such as fonts located at the user:// directory.

func (Instance) LoadDynamicFont

func (self Instance) LoadDynamicFont(path string) error

Loads a TrueType (.ttf), OpenType (.otf), WOFF (.woff), WOFF2 (.woff2) or Type 1 (.pfb, .pfm) dynamic font from file 'path'.

Warning: This method should only be used in the editor or in cases when you need to load external fonts at run-time, such as fonts located at the user:// directory.

func (Instance) ModulateColorGlyphs

func (self Instance) ModulateColorGlyphs() bool

If set to true, color modulation is applied when drawing colored glyphs, otherwise it's applied to the monochrome glyphs only.

func (Instance) MsdfPixelRange

func (self Instance) MsdfPixelRange() int

The width of the range around the shape between the minimum and maximum representable signed distance. If using font outlines, MsdfPixelRange must be set to at least twice the size of the largest font outline. The default MsdfPixelRange value of 16 allows outline sizes up to 8 to look correct.

func (Instance) MsdfSize

func (self Instance) MsdfSize() int

Source font size used to generate MSDF textures. Higher values allow for more precision, but are slower to render and require more memory. Only increase this value if you notice a visible lack of precision in glyph rendering.

func (Instance) MultichannelSignedDistanceField

func (self Instance) MultichannelSignedDistanceField() bool

If set to true, glyphs of all sizes are rendered using single multichannel signed distance field (MSDF) generated from the dynamic font vector data. Since this approach does not rely on rasterizing the font every time its size changes, this allows for resizing the font in real-time without any performance penalty. Text will also not look grainy for Controls that are scaled down (or for Label3Ds viewed from a long distance). As a downside, font hinting is not available with MSDF. The lack of font hinting may result in less crisp and less readable fonts at small sizes.

Note: If using font outlines, MsdfPixelRange must be set to at least twice the size of the largest font outline.

Note: MSDF font rendering does not render glyphs with overlapping shapes correctly. Overlapping shapes are not valid per the OpenType standard, but are still commonly found in many font files, especially those converted by Google Fonts. To avoid issues with overlapping glyphs, consider downloading the font file directly from the type foundry instead of relying on Google Fonts.

func (Instance) OpentypeFeatureOverrides

func (self Instance) OpentypeFeatureOverrides() map[any]any

Font OpenType feature set override.

func (Instance) Oversampling

func (self Instance) Oversampling() Float.X

If set to a positive value, overrides the oversampling factor of the viewport this font is used in. See Viewport.Oversampling. This value doesn't override the oversampling parameter of draw_* methods.

func (Instance) RemoveCache

func (self Instance) RemoveCache(cache_index int)

Removes specified font cache entry.

func (Instance) RemoveGlyph

func (self Instance) RemoveGlyph(cache_index int, size Vector2i.XY, glyph int)

Removes specified rendered glyph information from the cache entry.

Note: This function will not remove textures associated with the glyphs, use RemoveTexture to remove them manually.

func (Instance) RemoveKerning

func (self Instance) RemoveKerning(cache_index int, size int, glyph_pair Vector2i.XY)

Removes kerning override for the pair of glyphs.

func (Instance) RemoveLanguageSupportOverride

func (self Instance) RemoveLanguageSupportOverride(language string)

Remove language support override.

func (Instance) RemoveScriptSupportOverride

func (self Instance) RemoveScriptSupportOverride(script string)

Removes script support override.

func (Instance) RemoveSizeCache

func (self Instance) RemoveSizeCache(cache_index int, size Vector2i.XY)

Removes specified font size from the cache entry.

func (Instance) RemoveTexture

func (self Instance) RemoveTexture(cache_index int, size Vector2i.XY, texture_index int)

Removes specified texture from the cache entry.

Note: This function will not remove glyphs associated with the texture. Remove them manually using RemoveGlyph.

func (Instance) RenderGlyph

func (self Instance) RenderGlyph(cache_index int, size Vector2i.XY, index int)

Renders specified glyph to the font cache texture.

func (Instance) RenderRange

func (self Instance) RenderRange(cache_index int, size Vector2i.XY, start int, end int)

Renders the range of characters to the font cache texture.

func (Instance) SetAllowSystemFallback

func (self Instance) SetAllowSystemFallback(value bool)

SetAllowSystemFallback sets the property returned by [IsAllowSystemFallback].

func (Instance) SetAntialiasing

func (self Instance) SetAntialiasing(value TextServer.FontAntialiasing)

SetAntialiasing sets the property returned by [GetAntialiasing].

func (Instance) SetCacheAscent

func (self Instance) SetCacheAscent(cache_index int, size int, ascent Float.X)

Sets the font ascent (number of pixels above the baseline).

func (Instance) SetCacheDescent

func (self Instance) SetCacheDescent(cache_index int, size int, descent Float.X)

Sets the font descent (number of pixels below the baseline).

func (Instance) SetCacheScale

func (self Instance) SetCacheScale(cache_index int, size int, scale Float.X)

Sets scaling factor of the color bitmap font.

func (Instance) SetCacheUnderlinePosition

func (self Instance) SetCacheUnderlinePosition(cache_index int, size int, underline_position Float.X)

Sets pixel offset of the underline below the baseline.

func (Instance) SetCacheUnderlineThickness

func (self Instance) SetCacheUnderlineThickness(cache_index int, size int, underline_thickness Float.X)

Sets thickness of the underline in pixels.

func (Instance) SetData

func (self Instance) SetData(value []byte)

SetData sets the property returned by [GetData].

func (Instance) SetDisableEmbeddedBitmaps

func (self Instance) SetDisableEmbeddedBitmaps(value bool)

SetDisableEmbeddedBitmaps sets the property returned by [GetDisableEmbeddedBitmaps].

func (Instance) SetEmbolden

func (self Instance) SetEmbolden(cache_index int, strength Float.X)

Sets embolden strength, if is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.

func (Instance) SetExtraBaselineOffset

func (self Instance) SetExtraBaselineOffset(cache_index int, baseline_offset Float.X)

Sets extra baseline offset (as a fraction of font height).

func (Instance) SetExtraSpacing

func (self Instance) SetExtraSpacing(cache_index int, spacing TextServer.SpacingType, value int)

Sets the spacing for 'spacing' to 'value' in pixels (not relative to the font size).

func (Instance) SetFaceIndex

func (self Instance) SetFaceIndex(cache_index int, face_index int)

Sets an active face index in the TrueType / OpenType collection.

func (Instance) SetFixedSize

func (self Instance) SetFixedSize(value int)

SetFixedSize sets the property returned by [GetFixedSize].

func (Instance) SetFixedSizeScaleMode

func (self Instance) SetFixedSizeScaleMode(value TextServer.FixedSizeScaleMode)

SetFixedSizeScaleMode sets the property returned by [GetFixedSizeScaleMode].

func (Instance) SetFontName

func (self Instance) SetFontName(value string)

Font family name.

func (Instance) SetFontStretch

func (self Instance) SetFontStretch(value int)

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

func (Instance) SetFontStyle

func (self Instance) SetFontStyle(value TextServer.FontStyle)

Font style flags.

func (Instance) SetFontWeight

func (self Instance) SetFontWeight(value int)

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

func (Instance) SetForceAutohinter

func (self Instance) SetForceAutohinter(value bool)

SetForceAutohinter sets the property returned by [IsForceAutohinter].

func (Instance) SetGenerateMipmaps

func (self Instance) SetGenerateMipmaps(value bool)

SetGenerateMipmaps sets the property returned by [GetGenerateMipmaps].

func (Instance) SetGlyphAdvance

func (self Instance) SetGlyphAdvance(cache_index int, size int, glyph int, advance Vector2.XY)

Sets glyph advance (offset of the next glyph).

Note: Advance for glyphs outlines is the same as the base glyph advance and is not saved.

func (Instance) SetGlyphOffset

func (self Instance) SetGlyphOffset(cache_index int, size Vector2i.XY, glyph int, offset Vector2.XY)

Sets glyph offset from the baseline.

func (Instance) SetGlyphSize

func (self Instance) SetGlyphSize(cache_index int, size Vector2i.XY, glyph int, gl_size Vector2.XY)

Sets glyph size.

func (Instance) SetGlyphTextureIdx

func (self Instance) SetGlyphTextureIdx(cache_index int, size Vector2i.XY, glyph int, texture_idx int)

Sets index of the cache texture containing the glyph.

func (Instance) SetGlyphUvRect

func (self Instance) SetGlyphUvRect(cache_index int, size Vector2i.XY, glyph int, uv_rect Rect2.PositionSize)

Sets rectangle in the cache texture containing the glyph.

func (Instance) SetHinting

func (self Instance) SetHinting(value TextServer.Hinting)

SetHinting sets the property returned by [GetHinting].

func (Instance) SetKeepRoundingRemainders

func (self Instance) SetKeepRoundingRemainders(value bool)

SetKeepRoundingRemainders sets the property returned by [GetKeepRoundingRemainders].

func (Instance) SetKerning

func (self Instance) SetKerning(cache_index int, size int, glyph_pair Vector2i.XY, kerning Vector2.XY)

Sets kerning for the pair of glyphs.

func (Instance) SetLanguageSupportOverride

func (self Instance) SetLanguageSupportOverride(language string, supported bool)

Adds override for Font.IsLanguageSupported.

func (Instance) SetModulateColorGlyphs

func (self Instance) SetModulateColorGlyphs(value bool)

SetModulateColorGlyphs sets the property returned by [IsModulateColorGlyphs].

func (Instance) SetMsdfPixelRange

func (self Instance) SetMsdfPixelRange(value int)

SetMsdfPixelRange sets the property returned by [GetMsdfPixelRange].

func (Instance) SetMsdfSize

func (self Instance) SetMsdfSize(value int)

SetMsdfSize sets the property returned by [GetMsdfSize].

func (Instance) SetMultichannelSignedDistanceField

func (self Instance) SetMultichannelSignedDistanceField(value bool)

SetMultichannelSignedDistanceField sets the property returned by [IsMultichannelSignedDistanceField].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetOpentypeFeatureOverrides

func (self Instance) SetOpentypeFeatureOverrides(value map[any]any)

SetOpentypeFeatureOverrides sets the property returned by [GetOpentypeFeatureOverrides].

func (Instance) SetOversampling

func (self Instance) SetOversampling(value Float.X)

SetOversampling sets the property returned by [GetOversampling].

func (Instance) SetScriptSupportOverride

func (self Instance) SetScriptSupportOverride(script string, supported bool)

Adds override for Font.IsScriptSupported.

func (Instance) SetStyleName

func (self Instance) SetStyleName(value string)

Font style name.

func (Instance) SetSubpixelPositioning

func (self Instance) SetSubpixelPositioning(value TextServer.SubpixelPositioning)

SetSubpixelPositioning sets the property returned by [GetSubpixelPositioning].

func (Instance) SetTextureImage

func (self Instance) SetTextureImage(cache_index int, size Vector2i.XY, texture_index int, image Image.Instance)

Sets font cache texture image.

func (Instance) SetTextureOffsets

func (self Instance) SetTextureOffsets(cache_index int, size Vector2i.XY, texture_index int, offset []int32)

Sets array containing glyph packing data.

func (Instance) SetTransform

func (self Instance) SetTransform(cache_index int, transform Transform2D.OriginXY)

Sets 2D transform, applied to the font outlines, can be used for slanting, flipping, and rotating glyphs.

func (Instance) SetVariationCoordinates

func (self Instance) SetVariationCoordinates(cache_index int, variation_coordinates map[string]float32)

Sets variation coordinates for the specified font cache entry. See Font.GetSupportedVariationList for more info.

func (Instance) SubpixelPositioning

func (self Instance) SubpixelPositioning() TextServer.SubpixelPositioning

Font glyph subpixel positioning mode. Subpixel positioning provides shaper text and better kerning for smaller font sizes, at the cost of higher memory usage and lower font rasterization speed. Use [Textserver.SubpixelPositioningAuto] to automatically enable it based on the font size.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL