Tweaking Pango font searching to prioritize a directory
[Originally posted in Pango's Discourse Forum with zero responses.]
In GNU LilyPond, a music typesetting application that uses Pango with Fontconfig + FreeType for font rendering, there are two categories of fonts:
- Normal text fonts, which should be searched on the system, and
- 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, 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 data directory 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 FcConfig
s with Pango, one that is restricted to the data directory 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 backward 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 data directory if found there, and only if not found in the data directory, try fonts installed on the system.
A first try to do this was looking into the Fontconfig XML configuration file, but this didn't lead to good results. Another try was a question on the Fontconfig mailing list about this, but there was no response.
Is it possible to use Pango APIs for achieving this feature? It seems that Fontconfig itself can do it, since it can give a list of matching fonts; see this reply.
Overriding the PangoFontMap.load_font
virtual method might be a potential solution, but it seems that the PangoFT2FontMap
class is final.
Any ideas?