Last active
January 3, 2016 02:39
-
-
Save ftake/8397208 to your computer and use it in GitHub Desktop.
How to get localized font names with the CoreText API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Coo! But the code doesn't work for users who uses English system but want to display font name as Japanese...