Using custom fonts from font files on Windows
For quite some time now, I've been using the API AddFontResourceExW
to add fonts from TTF files to Pango's Fontmap on Windows. This has been working fine till Pango 1.50.12. I see on that version pango added support for using DWrite backend (which is nice) but this broke the old behaviour.
A Minimal Reproducer
The file "Roboto-Regular.tff" should be placed in same directory as the executable and can be downloaded from Google Fonts.
#include <pango/pangocairo.h>
#include <cairo/cairo-svg.h>
#include <windows.h>
#include <string.h>
int main(int argc, char **argv)
{
printf("Added %d fonts\n", AddFontResourceExW(L"Roboto-Regular.ttf", FR_PRIVATE, 0));
cairo_t *cr;
cairo_surface_t *surface;
PangoLayout *layout;
PangoFontDescription *desc;
char filename[100] = "test-v";
strcat(filename, PANGO_VERSION_STRING);
strcat(filename, ".svg");
surface = cairo_svg_surface_create(filename, 500, 500);
cr = cairo_create(surface);
layout = pango_cairo_create_layout(cr);
pango_layout_set_text(layout, "Test Hello World", -1);
desc = pango_font_description_new();
pango_font_description_set_family(desc, "Roboto");
pango_layout_set_font_description(layout, desc);
pango_cairo_show_layout(cr, layout);
g_object_unref(layout);
cairo_destroy(cr);
cairo_surface_destroy(surface);
RemoveFontResourceW(L"Roboto-Regular.ttf");
return 0;
}
I did my research to find the equivalent API for DWrite and ended up finding that it's not possible, see this table from Microsoft's Docs. Another option I found was to use the GDI's EnumFontFamiliesExW
and the convert the LPLOGFONTW
returned to IDWriteFont
using the compatibility API provided, ie. using CreateFontFromLOGFONT
and use it as needed.
Quick search in the codebase got me that the option was already implemented but was commented out, https://gitlab.gnome.org/GNOME/pango/blob/main/pango/pangowin32-fontmap.c#L723, any reason why? (uncommenting that and running the reproducer simply works).
Or is there any other way I can achieve the same on the Application side? Thanks!