Skip to content

Commit

Permalink
fix fonts not working after a call to getContext
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chearon committed Mar 10, 2016
1 parent 42a7ba5 commit e3c2759
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/register_font.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <pango/pangocairo.h>
#include <pango/pango-fontmap.h>
#include <pango/pango.h>

#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <CoreText/CoreText.h>
Expand All @@ -9,13 +13,22 @@

bool
register_font(unsigned char *filepath) {
bool success;

#ifdef __APPLE__
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
return CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
success = CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
#elif defined(_WIN32)
return AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
success = AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
#else
return FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
success = FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
#endif

// Tell Pango to throw away the current FontMap and create a new one. This
// has the effect of registering the new font in Pango by re-looking up all
// font families.
if (success) pango_cairo_font_map_set_default(NULL);

return success;
}

0 comments on commit e3c2759

Please sign in to comment.