Browse Source

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
master
Caleb Hearon 9 years ago
parent
commit
4dec675746
  1. 19
      src/register_font.cc

19
src/register_font.cc

@ -1,3 +1,7 @@
#include <pango/pangocairo.h>
#include <pango/pango-fontmap.h>
#include <pango/pango.h>
#ifdef __APPLE__ #ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
#include <CoreText/CoreText.h> #include <CoreText/CoreText.h>
@ -9,13 +13,22 @@
bool bool
register_font(unsigned char *filepath) { register_font(unsigned char *filepath) {
bool success;
#ifdef __APPLE__ #ifdef __APPLE__
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false); CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
return CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL); success = CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
#elif defined(_WIN32) #elif defined(_WIN32)
return AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0; success = AddFontResourceEx((LPCSTR)filepath, FR_PRIVATE, 0) != 0;
#else #else
return FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath)); success = FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
#endif #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;
} }

Loading…
Cancel
Save