Skip to content

Commit e3c2759

Browse files
committed
fix fonts not working after a call to getContext
issue was limited to osx and windows before this, if you create a canvas and get its context, the fonts would freeze - you cannot add any fonts after getContext is called once, even if you set up a new canvas. i fixed this by refreshing the Pango FontMap (the component that does font lookups) after you create a canvas+context, that canvas+context will not be able to use new fonts, but that is less of a problem
1 parent 42a7ba5 commit e3c2759

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/register_font.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include <pango/pangocairo.h>
2+
#include <pango/pango-fontmap.h>
3+
#include <pango/pango.h>
4+
15
#ifdef __APPLE__
26
#include <CoreFoundation/CoreFoundation.h>
37
#include <CoreText/CoreText.h>
@@ -9,13 +13,22 @@
913

1014
bool
1115
register_font(unsigned char *filepath) {
16+
bool success;
17+
1218
#ifdef __APPLE__
1319
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
14-
return CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
20+
success = CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
1521
#elif defined(_WIN32)
16-
return AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
22+
success = AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
1723
#else
18-
return FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
24+
success = FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
1925
#endif
26+
27+
// Tell Pango to throw away the current FontMap and create a new one. This
28+
// has the effect of registering the new font in Pango by re-looking up all
29+
// font families.
30+
if (success) pango_cairo_font_map_set_default(NULL);
31+
32+
return success;
2033
}
2134

0 commit comments

Comments
 (0)