Skip to content

Windows: Add API to load uninstalled fonts into fontmap (for Windows 10 and later)

Chun-wei Fan requested to merge dwrite-custom-font into main

Hi,

This attempts to add an API for PangoWin32 to allow one to specify a path to a font file (which is not installed to the system's fonts) so that it can be loaded into the underlying PangoWin32FontMap in the font map that we are using, as using AddFontReserouceEx() with the FR_PRIVATE flag is no longer supported and we would want to move on to DirectWrite more, as mentioned in issue #720.

For now, this will support Windows 10 and later; support for Windows 7/8.1 will probably be done in a separate MR if there is sufficient demand for it, as there is more complexity involved of adding such support for these systems (and they are out of support at this point).

This mimics what is done in the FontConfig backend, by dumping the cache in the existing font map, and reloading the font map with the system's installed fonts plus the specified font file, if it is supported by DirectWrite, and emits the notifications and signals as needed.

Note that probably due to an issue in Cairo (it would be helpful if someone can help find out), the code in this state will load the specified font file correctly, but each character of the text gets subtracted by one, for instance "BBcc" would become "AAbb".

Test program for this changeset: test-roboto.c, which is modified from the reproducing program from the reporter of issue #720, which makes use of a downloadable font from Google, Roboto-Regular.ttf, as stated in that issue report.


Edit: Yup, it does seem to me that this is indeed a Cairo-DWrite issue. If I purposely turn off the code in pangocairo-win32font.c that would use the DirectWrite support in Cairo (i.e. changing #ifdef HAVE_CAIRO_WIN32_DIRECTWRITE in pango_cairo_win32_font_create_font_face() to #ifndef HAVE_CAIRO_WIN32_DIRECTWRITE), then the correct text will be in the resulting SVG file with the correct font.

So, it turned out that there are subtle differences when the hb_font_t (hb_face_t,, rather) that we create in Pango is created with an HFONT (what we did) compared to when we use HarfBuzz's DirectWrite APIs using a IDWriteFontFace, which is what we do nowadays. So, we ought to create the hb_face_t using a IDWriteFontFace if Cairo-DWrite is found and HarfBuzz build supports DirectWrite; otherwise we use the HFONT as we did before.

So, as far as Pango goes, I think this is it.


Fixes #720 for Windows 10 and later; if applicable, Windows 7/8.x will be done in another MR, as including support for those systems in this MR will make this MR too big, which will build on top of the code in this MR.

With blessings, thank you!

Edited by Chun-wei Fan

Merge request reports