How do I load a hand-made bitmap font into Godot as a BitmapFont?

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

I always hand-make the fonts for my games rather than use pre-existing fonts. In the past, when using the Allegro libraries, this was done simply by formatting the bitmaps in a particular way and using Allegro’s tools to quickly parse the bitmap’s glyphs into Allegro’s own internal font format.

However, I’m at a loss as to how to do this with Godot. From what I’ve read, the BitmapFont object in Godot uses the “BMFont” file format, but EVERY tool I’ve found online which exports to this format doesn’t actually support importing an existing bitmap and working with it, instead opting to generate bitmaps out of TTF files, which doesn’t help me at all. :frowning:

So yeah, how do I get my own hand-crafted bitmap fonts loaded into Godot as BitmapFont objects, and if I have to use a third party tool to do this, what tool would even work for this?

:bust_in_silhouette: Reply From: Lopy

Try FontForge. It can apparently open BitMap fonts, and I know it can edit glyphs and export to Godot.

You can create a Godot DynamicFont Ressource inside a file (New Resource option in the FileSystem dock), and set it’s Font/Font Data property to a .ttf file (which is an export fileformat supported by FontForge).

Finally, you can set the global Godot font in the project settings, under GUI/Theme/Custom Font.

Hmm… FontForge is definitely more geared towards vector fonts instead of bitmap fonts. The bitmap font features it has are primarily related to actual font files with bitmaps encoded into them, as opposed to loading in raw bitmaps and working with them, which you CAN do… on a per-glyph basis, so that wouldn’t really be any faster for me than hand-typing the BMFont files from scratch with all the appropriate coordinates for each glyph… which seems to be the route I’m going to have to take at this rate. Not the end of the world but understandable given how few people hand-craft their fonts in bitmap form. :stuck_out_tongue:

Gemini | 2020-12-28 14:44

:bust_in_silhouette: Reply From: bruvzg

You can create BitmpFont from a hand-made image by using add_texture and add_char function from GDScript:

e.g. monospaced font with 10x10 chars placed horizontally in the image:

var font = BitmapFont.new()
var texture = load("res://font_image.png")
var chars = "ABCDEF...."
font.add_texture(texture)
for i in range (0, chars.length()):
	font.add_char(chars.ord_at(i), 0, Rect2(10 * i, 0, 10, 10), Vector2(0, 0), 10)

And apply it to the controls using:

$Control.add_font_override("font", font)