0 votes

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

asked Aug 7, 2019 in Engine by mustafamax (32 points)

3 Answers

+1 vote
Best answer

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)
answered Aug 7, 2019 by mustafamax (32 points)
+1 vote

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: https://docs.godotengine.org/en/3.1/classes/class_label.html#theme-properties

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

answered Aug 7, 2019 by Zylann (26,157 points)

I don't know how to create yourdynamicfont.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)

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.

+1 vote

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))
answered Aug 7, 2019 by newold (152 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.