Skip to content

Instantly share code, notes, and snippets.

@ftake
Last active January 3, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ftake/8397208 to your computer and use it in GitHub Desktop.
Save ftake/8397208 to your computer and use it in GitHub Desktop.
How to get localized font names with the CoreText API
#include <iostream>
#include <CoreText/CTFont.h>
#include <CoreText/CTFontCollection.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFBase.h>
void callBack(const void *value, void *context) {
CTFontDescriptorRef pDesc = static_cast<CTFontDescriptorRef>(value);
CFStringRef pFamilyName = (CFStringRef)CTFontDescriptorCopyAttribute(pDesc, kCTFontFamilyNameAttribute);
CFStringRef lang = CFSTR("ja");
CFStringRef pLocalizedName = (CFStringRef)CTFontDescriptorCopyLocalizedAttribute(pDesc, kCTFontFamilyNameAttribute, &lang);
CFShow(pFamilyName);
char buf[128];
CFStringGetCString(pLocalizedName, buf, 128, kCFStringEncodingUTF8);
std::cout << buf << std::endl;
}
int main(int argc, const char * argv[])
{
static const int nMaxDictEntries = 8;
CFMutableDictionaryRef pCFDict = CFDictionaryCreateMutable(NULL,
nMaxDictEntries,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue( pCFDict, kCTFontCollectionRemoveDuplicatesOption, kCFBooleanTrue );
CTFontCollectionRef pCollection = CTFontCollectionCreateFromAvailableFonts( pCFDict );
CFRelease( pCFDict );
CFArrayRef pFontArray = CTFontCollectionCreateMatchingFontDescriptors(pCollection);
const long nFont = CFArrayGetCount(pFontArray);
const CFRange aFullRange = CFRangeMake(0, nFont);
CFArrayApplyFunction(pFontArray, aFullRange, callBack, 0);
return 0;
}
@mealdy
Copy link

mealdy commented Sep 23, 2015

Coo! But the code doesn't work for users who uses English system but want to display font name as Japanese...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment