0

I am currently trying to use a font family I have within my working directory in pygame. After looking at the pygame.font.Font documentation I couldn't find anything on this and I also searched on stack overflow but it seems like everyone is using the "fake bold" and "fake italics". I want to be able to change my font weight to all the installed options Montserrat has such as medium, extrabold, bold, light, etc.

Can this be done?

Currently what I have:

self.font = pygame.font.Font("Montserrat-VariableFont_wght.ttf", 40)

NOTE before answering I AM NOT looking for the bold and italics installed feature on pygame where you just write ,bold=true ,italics=true or the set_bold(True) feature

1
  • Sounds like something you should file an issue for over on github.com/pygame/pygame/issues (after searching the closed issues to make sure it's not already been shot down), because variable fonts ("fvar") are almost certainly not currently supported given that there's zero mention in the docs (Especially since FreeType, which is the shaper used by pygame, has had fvar support since v2.8, released in 2017) Commented Jul 2 at 1:24

1 Answer 1

0

The answer is simply: No, pygame does not support loading a type face by name from a font file, and you also can't create a font with a specific weight (neither by name nor by an integer value).


What you can do however is creating a sepereate file for each face in the font file.

If you want to stick with python, you could use the fontTools module to do this. I'm not really an expert on that topic, but I hacked together this simple script:

from fontTools.varLib import instancer
from fontTools.ttLib import TTFont

varfont = TTFont("Montserrat-VariableFont_wght.ttf")
fvar = varfont['fvar']
for instance in fvar.instances:
    instance_font = instancer.instantiateVariableFont(varfont, instance.coordinates)
    name = varfont['name'].getDebugName(instance.subfamilyNameID)
    instance_font.save(f"./Montserrat-VariableFont_wght-{name}.ttf")

that will create a font file for each font face (Black, Bold, ExtraBold, Thin etc).

Not the answer you're looking for? Browse other questions tagged or ask your own question.