How to use custom application fonts with Pango
I am at the Libre Graphics Meeting
in Toronto this week, which means that I got to talk to GIMP and Inkscape developers after many years, and was reminded that Pango still does not make it easy to use custom (aka. application) fonts, and it still does not allow turning OpenType features on or off. So I decided to give Pango some love.
OpenType features is bug 738505
. Akira and Matthias wrote the initial patch, but there are certain complexities in handling the attributes that needs to be fixed before this can go in. I'll see if I can get myself to do that tomorrow.
Custom fonts is a different issue. And by custom fonts I mean when an application wants to use a font file that it ships, but is not installed in the system or user font directories. Most of the times when people have this request, they also don't want any system fonts, ie, they only
want their custom fonts. A font viewer is a basic example of this that we never had a good solution for.
Webfonts, and other embedded fonts, are another use case. Eg, a document might bundle fonts that it uses. Many times, the document would want to refer to the font using font family name, so in that case you want the font to be added to the system fonts and go through the font matching process.
Back in 2006 when we considered pangocairo the only backend we care about, I proposed
that we add pangocairo API to use a certain cairo_face_t. The thinking was that by doing it in the pangocairo layer, we don't have to do it in individual platform backends (pangofc, pangowin32, pangoatsui / pangocoretext). That, however, never happened, because I was too lazy to implement it, but also because it was actually a lot of tricky work, to make the generic pangofc layer understand this custom object...
I have since changed my mind on how this should be done. Over the years different groups asked how they do this, I've had suggested different variations of the same solution: custom Fontconfig config; and Win32 / CoreText APIs on other platforms. Ie, do it at font host layer, not Pango layer. It's a legitimate approach, if not most convenient.
Here is one way to do it, using fontconfig API:
Before calling into Pango, do this:
- Create a new fontconfig configuration object using FcConfigCreate() and make it current using FcConfigSetCurrent(); this ensures that default configuration and fonts are not loaded,
- Add fonts to it using FcConfigAppFontAddFile() or FcConfigAppFontAddDir(),
- Use Pango as you otherwise would.
This way Pango will only see your fonts. If you want to add your fonts but also see the system fonts, you can skip the first step. That will make your custom fonts visible to all Pango users within the process, including Gtk+ and its font selection dialog. If, instead, you want to limit it to a particular document, we need something more involved; we talk about further down.
Recently I needed to do something similar in Noto
's test suite, which is written in Python. Since we currently don't have
fontconfig Python bindings, I either would have had to be bothered to do a ctypes binding of the few functions I needed, or find an even simpler solution. Which is:
Before calling into Pango, do this:
- Set the FONTCONFIG_FILE environment variable to point to your custom fonts.conf XML,
- Add your custom font directory to the above-mentioned XML.
That's it. Here's are the two steps for Noto: 1
Now, this sounds easy, and works for very limited usecases. But for it to be useful in apps like The GIMP or Inkscape, there are a few issues that need to be handled. And I'm writing this post to raise enough interest from Akira, Matthias, Khaled, and others, to help fix these so we can have a great custom-font experience. Writing a tutorial when these are all fixed would be great, but for now, this post is documentation enough!
First, the custom XML currently has to have a cachedir element or fontconfig warns. That's annoying to say the least. Filed here
. For now, you can use "<cachedir prefix="xdg">fontconfig</cachedir>" and that should work with recent-enough versions of fontconfig that understand the prefix="xdg". Older version will ignore it and try to create a fontconfig directory under the current directory and use as cache, so beware of that.
Python / JS / etc bindings for fontconfig will be useful if we are pushing in this direction. Filed here
Another problem with the suggested setups above is that it works for non-GTK use-cases, like a game, test suite, etc. But in most usecases, you definitely don't want your menus and other GUI elements to use the custom fonts. In the case of a document editor or graphics editor, you might want to add custom fonts per document. You can do that by using a separate PangoFcFontMap instance for each custom need. I outlined that
like this before:
- Use that PangoFontMap to create your context, etc.
- Every time you want to use Pango with that context, FcConfigSetCurrent the above config. Then reset it back to whatever it was before.
This will allow you to use Pango with custom-only fonts in parts of the application. If you also want the system fonts to be visible in this private font map, you can use the obscurely named FcInitLoadConfigAndFonts() instead of FcConfigCreate().
That's not awfully bad. Except that the last step is very cumbersome and a recipe for disaster. That step can be avoided if we have API to attach a custom FcConfig to a PangoFcFontMap. Owen and I talked about it years ago and I filed a bug
, but never happened... until yesterday.
pango_fc_font_map_set_config() yesterday, and just added
pango_fc_font_map_config_changed(). So you can attach your private FcConfig to a private PangoFcFontMap, inform the font map every time you change the FcConfig (eg, add more fonts to it), and you should be able to use that font map to create layouts and use normally, without switching FcConfig's all the time. However, remember: I wrote the code, but I didn't test it. So a brave first user is wanted.
Then there's the set of issues with Fontconfig; the fact that to add font or configuration to an FcConfig, you need to have those in files, and can't add from memory blobs. Fixing that should be fairly easy, and I filed those last year. Would be great if we can fix them soon. Here's one
for font blobs, and another
for configuration XML.
Another issue with using custom fonts is that every time you add a custom font, Fontconfig has to scan it. Ie, you get no benefit from the Fontconfig font cache. This can be really
slow for huge fonts. And became five times slower when the Adobe CFF rasterizer was integrated in FreeType. I tried a few different approaches to speed it up. Removing the (prematurely added) FC_HASH helped. But there's more that can be done. That bug is here
Now, here's the one remaining issue that I don't have a full answer for: by sidestepping the default fontconfig configuration, you will miss some essential features. Right now those are: synthetic italics, synthetic bold, and scaling of bitmap fonts. The reason is that these are encoded in configuration files instead of in the library. That sounds slightly over-engineered, and I like to improve it, but as of right now, that is the way it is. You also will lose system-wide and user's configuration.
Sometimes that's not a problem. For example, in the Noto test suite, we don't
want any system or user configuration or any synthetic emboldening or italics, so the FcConfigCreate() approach suites those kinds of scenarios perfectly well. Same about the preview part of a font-viewer, or other use cases where being system-independent is a goal. But in other situations it might not be desirable.
I can, of course, suggest that anyone creating a custom FcConfig to add the essential configuration to it; Eg. bitmap scaling
, synthetic bold and italic
, essential aliases
, user's configuration
. But that still leaves out generic aliases, system config, etc. And I don't like that approach, because that spreads our default configuration all around Fontconfig clients and will create a mess that we would have to clean up every time we change how we do configuration. It's a leaky abstraction.
Quite fortunately, there's an almost-perfect solution for that. The obscure FcInitLoadConfig() function creates a new FcConfig and loads the default configuration files, but does not
load any fonts referred to from those configuration files. As such, you can go ahead and add your own fonts to it, but still benefit from the configuration. Don't complain, if the configuration does undesirable font aliasing or other stuff. I'm hugely relieved that this might do exactly what we need. I have not tested this, so if you test, please kindly let me know how it works. I cannot think of obvious undesirable behaviors from this approach.
So, to wrap up, assuming you would use Pango to be released soon, this is how you do custom fonts with PangoCairoFc:
- Create a custom FcConfig:
- Use FcConfigCreate() if you don't want any system / user fonts or configuration whatsoever, neither you want any synthetic or other manipulations,
- Use FcInitLoadConfig() if you want system / user configuration, but no system / user fonts,
- Use FcInitLoadConfigAndFonts() if you want system / user configuration and fonts visible in your private font map,
- Call FcConfigParseAndLoad() to add any custom configuration you want to add. Such configuration can add custom font directories, or you can use next step,
- Call FcConfigAppFontAddFile() or FcConfigAppFontAddDir() to add custom fonts,
- Call pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT), to create a private PangoCairoFcFontMap, which is a subclass of PangoFcFontMap, which is a subclass of PangoFontMap,
- Call pango_fc_font_map_set_config() to attach your custom FcConfig to your private PangoFcFontMap,
- Call pango_fc_font_map_config_changed() whenever you add new fonts to your custom FcConfig,
- Use that PangoFontMap to create your context, etc, and use normally.
You can optimize the logic to use the default font map if there are no custom fonts involved, and do the above otherwise.
This can trivially be adapted to the PangoFT2 backend by the way.
If your application has its own font dialog, then you can implement that using the private font map as well, and things should work. But if you use the GTK+ font dialog, currently you can't attach your private font map to the font dialog instance. I filed
a request for that.
While there, let me address the issue of having or not having to restart applications when new fonts are installed. As I covered in my State of Text Rendering
in 2009 and presented at the Gran Canaria Desktop Summit, online font addition/removal
should Just Work in GNOME. I implemented that in 2008. Indeed, it does in simple applications. They are a bit slow (~4 seconds before new font shows up), so I filed
a bug to shorten the delay. However, a lot is not
- I implemented this in the GTK+ font-chooser dialog in 2009, to update online as fonts were added / removed. But font-chooser was rewrote in 2011 and is completely broken now. So I filed a new bug (UPDATE: this is fixed now),
- I wasn't loud enough in making this feature heard or document how custom font dialog widgets can implement this. Take a look at this deprecated font dialog implementation in GTK+. Or watch the new bug for discussion and how this will evolve,
- To implement this for non-GTK+-based applications, you need to catch the XSETTINGS signal yourself and respond; search for Fontconfig/Timestamp in GTK+ source,
- If you create private fontmaps and FcConfig's that include system / user configs or fonts (ie, you used FcInitLoadConfig() or FcInitLoadConfigAndFonts()), then you also need something similar to the above code in GTK+, that is, catch the signal, recreate your FcConfig (and add your custom fonts to it again), and attach the new FcConfig to your PangoFcFontMap.
As for pangocairo API to use a cairo_font_face_t
, I still think that would be useful, but much less so than before. Also, it won't be very useful until cairo adds API to create a cairo_font_face_t from a font file or memory blob. Right now the only way to do that is to write code for the FreeType, Win32, and ATSUI / CoreText font backends of cairo separately.
Even if you try to do that, you will hit a very well-known problem with AddFontMemResourceEx(); which is: if you try to add a custom font with a family name that matches that of an existing font on the system, when you try to use the font, you might end up using the version installed on the system. Ie. you can add fonts to the system, but you can't reliably address them. The hack that Firefox, HarfBuzz, and others use to work around this is to modify the font data before calling AddFontMemResourceEx() to set a unique font family name on it, so they can be sure there will be no collision. Here
's the HarfBuzz code for that.
Anyway, I hope this write up helps developers, after confusing them, implement custom fonts in their applications, and motivate others to help me fix remaining issues and generate examples and better documentation.
While writing this, I ended up doing some bug triage and closing obsolete issues around Pango, Fontconfig, and cairo, which is always nice :D.
Anyway, this alone makes me really happy that I attended LGM. The HarfBuzz Documentation Sprint
was also very productive, but takes a couple more weeks to get to a stage that we can show off what we produced.
Labels: fonts, gtk+, pango, pangocairo