Simutrans Chat Room
Where cool people of Simutrans can meet up.

Use Fontconfig for font autodetection

Started by Roboron, March 22, 2024, 03:47:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Support for TrueType fonts was improved recently, and with such an improvement it now makes sense to set a TrueType font as default. But Simutrans does not ship a default .ttf font and font autodetection is currently only supported on Windows and Android!

For linux and other unix-like systems we can rely on Fontconfig for such a task. Fontconfig can find an appropriate font to use for Simutrans, taking into account user language and other parameters.

In this patch I told Fontconfig to find a "Sans" type of font on the user system - but even if no "Sans" font is available, Fontconfig will always return a font!

Captura de pantalla_20240322_163434.png

In this example, I opened Simutrans first with my locale set to Japanese. Fontconfig then decided that "Noto Sans CJK" was the most appropriate font in my system. Then I opened it again with my locale set to English, and this time it returned "Noto Sans" (no CJK). There are other Sans fonts and my system, but Fontconfig choose this as the best.

Note: This may also work on macos, I can't test as I don't have a mac VM anymore, but it probably would require to include the configuration file from homebrew.


Mingw cannot use fontconfig when built with cmake. And it should be added to autoconf makefile builds too as well as too the libraries.

Otherwise this is very helpful ... Thank you


Quote from: prissi on March 26, 2024, 05:55:23 AMMingw cannot use fontconfig when built with cmake.

It can, but it needs:

1) To use pkgconf to get the link flags,
2) Even then, a library (libiconv) was missing, so I had to add it manually (this also happens with fluidsynth).

Check the attached patch if you are interested. On my Windows 11 system, it picked Times.ttf as the font.


Might not be a good choice for Japanese and other CJK languages. Does fontconfig takes this is account as well?

It seems fontconfig support two byte language codes. Maybe it would be clever to use the current language code for detection? Can be obtained from SImutranslator ...


Yes, it is a great idea! By using the language from the translator, we can also rely on fontconfig to find a suitable font when the player changes the language - we never have to show the font selection dialog!

I have only found one little problem: the translator/language are initialized after the system font is selected. Nevermind, in the attached patch I solved this by calling dr_get_system_font() twice, the second one after the language initialization (if the previous font does not have the necessary characters) - and passing the language as an optional parameter.

Quote from: prissi on April 05, 2024, 02:47:58 AMMight not be a good choice for Japanese and other CJK languages. Does fontconfig takes this is account as well?

For Japanese on Windows 11 it picks msjh.ttc


Quote from: Roboron on April 05, 2024, 08:41:49 PM...
For Japanese on Windows 11 it picks msjh.ttc

The bold fonts (msjhbd.ttc [JhengHei] or msyhbd.ttc [YaHei]) have better contrast.

I use msyhbd.ttc [YaHei] on Windows 10 for more years.


We could add priority to bold fonts, but I guess that's a matter of taste.


Strange, why does it picks a chinese font for Japanese. It works but is not the nicest choice. But then, on windows it should pick the current windows system font which matches the current language anyway.