Skip to content

Text Server ​

Godot's TextServer does a lot of text-related stuff, including management of font atlases and textures, and shaping of text.

To use TextServer, we have to get its instance with var ts := TextServerManager.get_primary_interface().

A Note about Font RIDs ​

In TextServer, everything is an RID, meaning that we have to get the RID of a font. Unlike other objects, where we can do object.get_rid(), we need to do font.get_rids()[0]. font.get_rids() returns a list of font RIDs, which starts with the normal font RID, but then has fallback font RIDs, in case the actual font cannot be rendered for whatever reason.

TODO: PUT A SECTION ABOUT DRAWING TEXT HERE!

Shaped Text ​

One of TextServer's objects is "shaped text", which allows you to have text with objects in it. Shaped text can help you put text, images, and other things on one or multiple lines of text. It also supports different languages very well.

You can create a shaped text object with ts.create_shaped_text(). To add a string, do ts.shaped_text_add_object(shaped_text, "HELLO.", font, 32)

To add an object, do ts.shaped_text_add_object(shaped_text, object, size_of_object). Note that object can be of ANY TYPE. However, all objects in a shaped_text must be unique. This means that you should not do this:

gdscript
var x := {"key": "value"}
var y := x
ts.shaped_text_add_object(shaped_text, x, Vector2(100, 100))
ts.shaped_text_add_object(shaped_text, y, Vector2(100, 100))

(In this case, x and y are references to the exact same object.)

Shaped text objects indirectly support line breaks, using ts.shaped_text_get_line_breaks(), where you can give the maximum line width. Note that you can use the TextServer.BREAK_MANDATORY flag to add line breaks when the string has '\n' (newline) characters. The TextServer.BREAK_WORD_BOUND flag can be used to only insert line breaks on the spaces between each word. The ts.shaped_text_get_line_breaks() returns an array of integers [a0, b0, a1, b1, a2, b2, ...], where aN is the inclusive index in the shaped text of the start of line N, and bN is the exclusive index in the shaped text of the end of line N. You need to manually parse this array to figure out where to add line breaks.