Add DynamicFont font using Gdscript code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mustafamax

I want to add a DynamicFont font using the Gdscript code
Here’s an example

extends CenterContainer

func _ready():
    var label = Label.new()

    label.text = "Hello World"
    label.rect_size.x = 320
    label.rect_size.y = 200

    add_child(label)

thank you

:bust_in_silhouette: Reply From: Zylann

To change the font used by a label from code, its theme needs to be overrided. You can do this:

label.add_font_override("font", load("res://your_dynamic_font.tres"))

See “Theme properties” to see which other theme properties you can override: Label — Godot Engine (3.1) documentation in English

If you are doing the same thing on many labels, consider creating a theme.

I don’t know how to create your_dynamic_font.tres
But I used this code and it worked
Thank you

extends VBoxContainer

func _ready():

    var dynamic_font = DynamicFont.new()
    dynamic_font.font_data = load("res://Fonts/Cairo-Bold.ttf")
    dynamic_font.size = 120
    dynamic_font.outline_size = 5
    dynamic_font.outline_color = Color( 0, 0, 0, 1 )
    dynamic_font.use_filter = true

    var label = Label.new()

    label.text = "Hello World"
    label.rect_size.x = 320
    label.rect_size.y = 200
    label.align = VALIGN_CENTER
    label.add_font_override("font", dynamic_font)
    label.add_color_override("font_color", Color.red)

    add_child(label)

mustafamax | 2019-08-07 20:40

If you want to create the dynamic font from the editor, you can press the New Resource button in the inspector dock, and select DynamicFont. Set its properties and then use the floppy button to save it.

Zylann | 2019-08-07 20:42

:bust_in_silhouette: Reply From: mustafamax

I used this code and it worked
thank you

extends VBoxContainer

func _ready():

    var dynamic_font = DynamicFont.new()
    dynamic_font.font_data = load("res://Fonts/Cairo-Bold.ttf")
    dynamic_font.size = 120
    dynamic_font.outline_size = 5
    dynamic_font.outline_color = Color( 0, 0, 0, 1 )
    dynamic_font.use_filter = true

    var label = Label.new()

    label.text = "Hello World"
    label.rect_size.x = 320
    label.rect_size.y = 200
    label.align = VALIGN_CENTER
    label.add_font_override("font", dynamic_font)
    label.add_color_override("font_color", Color.red)

    add_child(label)
:bust_in_silhouette: Reply From: newold

You can create an auto-load script with this code:

extends Node

var fonts = []

func create_dynamic_font(font_name, font_size, outline_size = 3, outline_color = Color(0, 0, 0)):
	var id = "%s-%s-%s-%s" % [font_name, font_size, outline_size, outline_color]
	var r = check_for_id_added(id)
	if r != -1: return r
	if (font_size < 1): font_size = 1
	var font = DynamicFont.new()
	var fontData = DynamicFontData.new()
	font.size = font_size
	if font_name.get_extension() != "ttf":
		fontData.font_path = "res://Fonts/%s.ttf" % font_name
	else:
		fontData.font_path = "res://Fonts/%s." % font_name
	font.outline_size = outline_size
	font.outline_color = outline_color
	font.font_data = fontData
	var new_font = { "id" : id, "src" : font}
	fonts.append(new_font)
	return fonts.size() - 1
	
func get_font(font_name, font_size, outline_size=0, outline_color=Color(0,0,0)):
	var index = create_dynamic_font(font_name, font_size, outline_size, outline_color)
	return fonts[index].src
	
func check_for_id_added(id):
	for i in range(fonts.size()):
		var struct = fonts[i]
		if struct.id == id: return i
	return -1

then you can use this command in any gdscript:

var myfont = Your_autoload_script.get_font(font_name, font_size, outline_size, outline_color)

You need puts fonts in a folder called “Fonts” in your project (create it if does not exists). You can only use fonts with extension “.ttf

Example:

# Create a font "Arial" with size 26, outline size 3 and outline color red.
# File "Arial.ttf" is in folder "Fonts" in your project
var myfont = Your_autoload_script.get_font("Arial", 26, 3, Color(1,0,0))