factoriax.playground.ui.fonts

factoriax.playground.ui.fonts#

Cached pixel-font loader and text rendering for pygame applications.

factoriax.playground.ui.fonts.get_pixel_font(size: int) → Font#

Return a pixel-style monospace font at the requested size.

The cache holds one font for each size, so the number of frames does not change the number of fonts.

If a play session called pygame.quit(), pygame.font is closed. The function then starts it again, and empties the font cache and the text cache. A caller therefore always gets a font that it can draw with.

Parameters:

size – Desired font height in pixels.

Return type:

pygame.font.Font

factoriax.playground.ui.fonts.render_text_rgba(text: str, font: Font, color: tuple[int, int, int]) → ndarray#

Render text to an RGBA array with a fully transparent background.

Results are cached by (id(font), text, color) so that identical text drawn on consecutive frames is rasterised at most once.

Parameters:
  • text – String to render.

  • font – pygame Font to use.

  • color – RGB glyph colour.

  • int

  • int]

Return type:

RGBA uint8 array of shape (h, w, 4).