Skip to content

Instantly share code, notes, and snippets.

@yuanhao
Forked from ftake/gist:8397208
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save yuanhao/7ac82ab64d49fe2406d5 to your computer and use it in GitHub Desktop.

Select an option

Save yuanhao/7ac82ab64d49fe2406d5 to your computer and use it in GitHub Desktop.
#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