Tweaking Pango font searching to prioritize a directory

Hi!

I’m hacking on GNU LilyPond, a music typesetting application that uses Pango with Fontconfig + FreeType for font rendering. There are two categories of fonts in LilyPond:

  • Normal text fonts, which should be searched on the system,
  • Music fonts, which are developed together with LilyPond. A music font file is tied to a given version of LilyPond. These files should be searched in LilyPond’s data directory only.

(This is the big picture; I’m simplifying things a bit.)

Recently, we discovered that LilyPond can use music font files from the outside if they happen to be in the usual system directories for fonts. This is a relatively unusual situation, which is why it wasn’t noticed before, but it’s not good.

Basically, LilyPond currently tweaks the Fontconfig configuration to add its datadir to the list of search directories, then asks Pango to find the fonts by their name (“Emmentaler”), and hopes for the best, which doesn’t work if “Emmentaler” is also found elsewhere.

From this description, it seems like it would suffice to use two FcConfigs with Pango, one that is restricted to the datadir and one that also includes system fonts. However, this doesn’t work very well because LilyPond currently sets up a fallback to normal text fonts for its music fonts, and we’d like to keep that for backwards compatibility. Also, on the long term, we want to start supporting loading custom music fonts outside of LilyPond’s internal paths.

Therefore, the ideal way to resolve fonts for LilyPond would be to use fonts from the datadir if found there, and only if not found in the datadir, try fonts installed on the system.

I looked for a way to do this in the Fontconfig XML conf file, but didn’t find a good one. (I’ve also asked on the Fontconfig mailingn list about this, https://lists.freedesktop.org/archives/fontconfig/2022-December/006936.html, but there was no response so far.)

Therefore, I would like to know if it’s possible to use Pango APIs for achieving this. It seems that Fontconfig itself can do it, since it can give a list of matching fonts; see this reply: https://lists.freedesktop.org/archives/fontconfig/2022-December/006925.html.

I was hoping to do this by overriding the PangoFontMap.load_font virtual method, but it seems that the PangoFT2FontMap class is final.

Any ideas?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.