Skip to content

Better options for TrueType Fonts. #75

Description

@jsbeckr

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?

Activity

  1. self-assigned this
    on May 13, 2019
  2. HexDecimal commented on May 13, 2019

    @HexDecimal
    Collaborator

    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.

  3. jsbeckr commented on May 14, 2019

    @jsbeckr
    Author

    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?

  4. changed the title [-]How to use TrueType Fonts correctly?[/-] [+]Better options for TrueType Fonts.[/+] on May 25, 2019
  5. HexDecimal commented on Apr 6, 2021

    @HexDecimal
    Collaborator

    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

  6. mila-e commented on Aug 4, 2026

    @mila-e

    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.1 but tried 19.6.3 and same issue.
    • libtcod says 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.6 from 3.14.4 just in case but no change.

    The fonts I tried were Arimo and Arial.

    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

    which produces:
    Image

    Setting the width to 0 as suggested produces this, which is ... mildly better?
    Image

    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.

  7. HexDecimal commented on Aug 5, 2026

    @HexDecimal
    Collaborator

    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_font function is too opaque to be useful so I wouldn't recommend using it. Instead I'd recommend copying the ttf.py script which calls the freetype-py package and can be edited from Python to correct its issues.

  8. HexDecimal commented on Aug 5, 2026

    @HexDecimal
    Collaborator

    If this issue does get resolved it will be from adding ttf.py as a package in tcod. One will need freetype-py to be installed in order to import tcod.ttf but won't need it otherwise. I never got settled on the API for ttf.py which is why it remains as a separate script.

  9. mila-e commented on Aug 5, 2026

    @mila-e

    My thought went to the possibility of just using the ttf.py script invisibly in the place of load_truetype_font so 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.

  10. HexDecimal commented on Aug 5, 2026

    @HexDecimal
    Collaborator

    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_font function 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions