Is there a way to render a SDL2 font as monospaced?

Viewed 250

My project is a stopwatch and non-monospaced fonts shake because of the differences between character sizes when running. I have tried font.set_hinting(sdl2::ttf::Hinting::Mono); but it does not seem to do anything.

1 Answers

The short answer is that to render a font as fixed-width (monospace), use a fixed-width font! Fixed- or variable-width is an attribute of the font, not the renderer.

However, if you call TTF_SetFontKerning with an open Font pointer to disable kerning,

TTF_SetFontKerning(font, 0);

the renderer will use a fixed spacing between the characters in a string. Note that this only works when rendering complete strings, not single glyphs.

The result will look anywhere from acceptable to really crappy, depending on the glyphs in the actual font used.

Related