I'm trying to load a custom font from a .ttf file into my Python program using the Arcade library, like so:
arcade.load_font("Assets/Fonts/CopperplateGothic.ttf")
self.actionsLabel = arcade.Text("Actions", font_name="CopperplateGothicStd-32BC", color=highlightColour, font_size=32, x=0, y=buttonY+buttonHeight+20, batch=self.headingTexts)
print(self.actionsLabel.font_name)
The call to arcade.load_font
doesn't produce an exception, so the font appears to be loaded correctly. However, the font isn't actually used when displaying the self.actionsLabel
.
I've tried these values for font_name
in the arcade.Text
constructor:
"Copperplate Std 32 BC"
(this is also how the font appears in the font selector in other programs)."CopperplateGothicStd-32BC"
But none of these work; the text simply appears in a fallback font (Segoe UI). However, using the name "MS Comic Sans"
chooses that font, as expected, so the problem must be somewhere else.
I also tried converting the font file to OpenType first, to no avail.
How can I use a font that I've loaded explicitly in the code, rather than one pre-installed in the system?
I tested on font file Copperplate-Gothic-Std-32-BC.ttf
downloaded from:
Copperplate Gothic Std Font Family Download | Free Font.Download
Arcade
uses pyglet
and digging in source code of Arcade and pyglet I found class freetype which loads ttf
and it has function to display font name
.
import pyglet.font.freetype
filename = 'Copperplate-Gothic-Std-32-BC.ttf'
font = pyglet.font.freetype.FreeTypeFace.from_file(filename)
print(font.name)
and it displays me name Copperplate Gothic Std
This name works for me in pyglet
with pyglet.font.add_file
which is used in arcade.load_font
import pyglet
filename = 'Copperplate-Gothic-Std-32-BC.ttf'
pyglet.font.add_file(filename )
font_name = 'Copperplate Gothic Std'
font = pyglet.font.load(font_name)
print(font.name)
Minimal working code to test this name in Arcade
import arcade
WIDTH = 400
HEIGHT = 300
TITLE = "Example"
filename = 'Copperplate-Gothic-Std-32-BC.ttf'
font_name = "Copperplate Gothic Std"
class MyGame(arcade.Window):
def __init__(self):
super().__init__(WIDTH, HEIGHT, TITLE)
arcade.load_font(filename)
self.actionsLabel = arcade.Text("Actions", font_name=font_name, font_size=32, x=WIDTH//2, y=HEIGHT//2, anchor_x="center", anchor_y="center")
print(self.actionsLabel.font_name)
def on_draw(self):
self.clear()
self.actionsLabel.draw()
if __name__ == "__main__":
MyGame()
arcade.run()
Result:
EDIT:
I tested only on Linux but Windows may use different classes to load fonts:
Win32DirectWriteFont
or GDIPlusFont
(see source code in pyglet/font),
and it may work in different way.