Skip to content

Fix TTF font rendering and add more font control

Hugo Hromic requested to merge hhromic/solarus:font-options into dev

Summary

This MR fixes two long-standing TTF font rendering issues and adds support for specifying font hinting setting and font kerning from SDL_ttf to the Text Surface Lua API.

This MR only affects TTF fonts rendering, not bitmap fonts. Also this MR has been tested with all the available quests in the Solarus website plus others such as Ocean's Heart, VOADI, ALTTD, Word Breaker, Cythera Tech Demo.

Closes #1389 (closed), #1390 (closed)

Details

This MR contains two major improvements:

  • Add font hinting and kerning options to TextSurface Lua API
  • Fix antialiasing bleeding in TTF font rendering

There is also a minor code improvement for consistency:

  • Add missing Lua stack pop in opt_boolean_field()

Added Font Hinting and Kerning to the TextSurface Lua API

Without this MR, the engine simply uses SDL_ttf's own defaults for rendering TTF fonts: font_hinting set to normal and font_kerning set to true.

To allow for better control of TTF font rendering, two new fields have been exposed into the TextSurface Lua API's sol.text_surface.create() function so the quest-maker can change these settings. Note that this addition is 100% backward compatible and does not break any existing Lua code.

The new fields are:

  • font_hinting that can be (none|normal|light|mono) (default normal)
  • font_kerning that can be (true|false) (default true)

Example usage:

local text_surface = sol.text_surface.create{
    font = "AndikaNewBasic/AndikaNewBasic-Bold"
    font_size = 16,
    font_hinting = "light",
    font_kerning = true,
    color = {255,255,255},
    rendering_mode = "antialiasing",
    horizontal_alignment = "left",
    vertical_alignment = "top",
}

Note that the engine also properly caches the TTF fonts in the FontResource class by extending the cache key from <size> to <size, hinting, kerning>. There is no limitation for the quest-maker to create as many text surfaces with different settings as desired.

Enabled Pre-multiplied Alpha in Text Surface Creation

The fix for the incorrect antialiasing was pretty simple: set premultiplied to true in Surface::create() during rendering of the TTF font. This enables correct usage of already existing alpha channel data in the text surface. Also there is no need now to pre-fill the destination surface with a transparent white color.

Screenshots

In all the above screenshots, the left side is without this MR and the right side is with this MR.

Test Pattern

This test pattern showcases a font in Regular and Bold versions, all the available font hinting settings and kerning enabled/disabled. Without this MR, Solarus defaults to normal hinting and kerning enabled. With this MR, the quest-maker can use any desired setting for individual text surfaces.

Also notice the (correct) bolding effect of the fixed pre-multiplied antialiasing.

Word Breaker

This is the Word Breaker game from Llamazing. This game heavily uses TTF fonts and was the inspiration to develop this MR. Llamazing uses the new font_hinting parameter of the Text Surface Lua API to make the game look much better with this MR.

The difference is specially noticeable in the guessed words area, for example the letter I. Also notice how the text is properly bolded now and there are no antialiasing artifacts anymore.

Specifying font_hinting in versions of Solarus without this MR has no effect, therefore this option is backwards compatible.

Cythera Demo

This technical demo also from Llamazing is another good example for this MR. The game also uses TTF fonts to render text in dialogs. This game has not been improved with the new font_hinting option but still the antialiasing fix makes a big difference. This is specially noticeable on the yellow and white texts of the screenshots.

Edited by Hugo Hromic

Merge request reports

Loading