libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
402 stars 36 forks source link

Better options for TrueType Fonts. #75

Open jsbeckr opened 5 years ago

jsbeckr commented 5 years ago

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.

image

tcod.tileset.set_truetype_font("data/OpenSans-Regular.ttf", 24, 24)

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?

HexDecimal commented 5 years ago

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.

jsbeckr commented 5 years ago

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?

HexDecimal commented 3 years ago

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_font is too small or narrow.

tcod.tileset.set_truetype_font

tcod.tileset.set_truetype_font

ttf.py / FreeType screenshot

ttf.py screenshot