Repository navigation
Better options for TrueType Fonts. #75
Description
Activity
As far I've gotten the best option is to exclude the width (use a width of
0) of the font, then the font loader will automatically pick the width with the best fit. If you pick both a width and height then the font will be scaled down to fit without stretching it. The best results will come from fonts designed to be used as monospace fonts.There's no options for fine tuning the font size, but now is a good time to make suggestions.
I guess if I could wish for an API I would like the following:
tcod.tileset.set_truetype_font("data/OpenSans-Regular.ttf", 24, 24, font_size=16, antialiasing=True)
It would be cool to set an anchor point for the tiles where the bounding box of the font sits. Something like
upper_left,bottom_right, etc. Which could enable nice effects like distinguishing small and big items for example?- changed the title
[-]How to use TrueType Fonts correctly?[/-][+]Better options for TrueType Fonts.[/+]on May 25, 2019 A new example using FreeType to generate a tileset can be found here.
This example builds the font from Python so it's easier to modify. It's recommended to use ttf.py if the tileset generated by
tcod.tileset.set_truetype_fontis too small or narrow.tcod.tileset.set_truetype_font
ttf.py / FreeType screenshot
Since this isn't closed i do hope I'm not beating a dead issue here. I seem to be having the same issue as the original issue, just exaggerated because I used different settings, so I wanted to try to get some more information out there if possible. I'm using SDL3 and Metal with MacOS 26.5 (arm).
As already stated, trying to use a font plainly like this causes the text to appear at a sixth of the desired size and also for some reason slightly transparent, so all the text is completely unreadable.
- Using latest
tcod 21.2.1but tried19.6.3and same issue. libtcodsays it's version 2.2.2- I'm not using HIDPI, i connect to an external 1080p monitor and disable the internal display.
- Tried updating python to
3.14.6from3.14.4just in case but no change.
The fonts I tried were
ArimoandArial.I reproduced it with this:
# python 3.14.6 import numpy as np import tcod t = tcod.tileset.load_truetype_font("some_font.ttf", 12, 24) tile = t.get_tile(ord('H')) print("tile shape:", tile.shape) # (24, 12, 4) print("R/G/B range:", tile[...,0].min(), tile[...,0].max()) # 255, 255 print("A range:", tile[...,3].min(), tile[...,3].max()) # 0, 115 alpha = tile[..., 3] ys, xs = np.nonzero(alpha) print("actual bounding box: rows", ys.min(), ",", ys.max(), " cols", xs.min(), ",", xs.max()) # rows 9-11, cols 4-6 (3x3 rather than the 12x24px)
outputs
tile shape: (24, 12, 4) R/G/B range: 255 255 A range: 0 115 actual bounding box: rows 9 , 11 cols 4 , 6
Setting the width to 0 as suggested produces this, which is ... mildly better?

I'm not really sure if this produces anything helpful that wasn't already known, but this did just burn me so I wanted to provide what i could since I wanted to use a truetype font instead of freetype.
- Using latest
The issue isn't dead, it's just tricky to figure out a concise way to fix this issue. Generally the
tcod.tileset.load_truetype_fontfunction is too opaque to be useful so I wouldn't recommend using it. Instead I'd recommend copying the ttf.py script which calls thefreetype-pypackage and can be edited from Python to correct its issues.Reacted by Miles EIf this issue does get resolved it will be from adding
ttf.pyas a package in tcod. One will needfreetype-pyto be installed in order to importtcod.ttfbut won't need it otherwise. I never got settled on the API forttf.pywhich is why it remains as a separate script.My thought went to the possibility of just using the ttf.py script invisibly in the place of
load_truetype_fontso when it's called it goes through the motions you'd have to put in anyway to achieve the same effect. Not sure if that'd end up being misleading though.There will be a process to transition away from
load_truetype_font, but that's for me to handle after a better TTF solution is found.Generally the
load_truetype_fontfunction lacks tons of options to scale and translate glyphs correctly. Asking for tile width/height alone is not enough information to load a TTF as a tileset.



Hey! :)
I was playing around with TrueType fonts (OpenSans, Courier New, Fira Code) on MacOS. And it seems that the font is rendered to small in comparison to the tile size.
Is it possible to control the size of the font inside a tile?
There is also an issue with Fira Code (it doesn't render correctly). But I think that might be the custom glyphs they included in the font?