Oops, I completely overlooked the Core Text Font Manager Reference, which would have told me that CTFontManagerCopyAvailableFontFamilyNames() was what I was looking for. Yay; the font families stuff now works on OS X as well!

This commit is contained in:
Pietro Gagliardi 2015-12-23 00:58:06 -05:00
parent bcc17a4e6d
commit a9419e0cc4
1 changed files with 7 additions and 60 deletions

View File

@ -451,93 +451,40 @@ void uiDrawRestore(uiDrawContext *c)
// TODO for all relevant routines, make sure we are freeing memory correctly // TODO for all relevant routines, make sure we are freeing memory correctly
// TODO make sure allocation failures throw exceptions? // TODO make sure allocation failures throw exceptions?
struct uiDrawFontFamilies { struct uiDrawFontFamilies {
/*
CFNumberRef one;
CFDictionaryRef options;
CTFontCollectionRef collection;
CFArrayRef fonts; CFArrayRef fonts;
*/
NSFontCollection *collection;
NSArray *fonts;
}; };
uiDrawFontFamilies *uiDrawListFontFamilies(void) uiDrawFontFamilies *uiDrawListFontFamilies(void)
{ {
uiDrawFontFamilies *ff; uiDrawFontFamilies *ff;
/*
const void *keys[1];
const void *values[1];
SInt32 one = 1;
*/
ff = uiNew(uiDrawFontFamilies); ff = uiNew(uiDrawFontFamilies);
// TODO is there a way to get an error reason?
/* ff->fonts = CTFontManagerCopyAvailableFontFamilyNames();
ff->one = CFNumberCreate(NULL, kCFNumberSInt32Type, &one); if (ff->fonts == NULL)
complain("error getting available font names (no reason specified)");
// needed as the matching descriptors can have
keys[0] = kCTFontCollectionRemoveDuplicatesOption;
values[0] = ff->one;
// TODO kCTFontCollectionIncludeDisabledFontsOption? the Windows code enumerates hidden fonts; disable that?
ff->options = CFDictionaryCreate(NULL,
keys, values, 1,
NULL, NULL);
NSLog(@"%@", ff->options);
ff->collection = CTFontCollectionCreateFromAvailableFonts(ff->options);
ff->fonts = CTFontCollectionCreateMatchingFontDescriptors(ff->collection);
*/
ff->collection = [NSFontCollection fontCollectionWithAllAvailableDescriptors];
ff->fonts = [ff->collection matchingDescriptorsWithOptions:[NSDictionary
dictionaryWithObject:@YES // TODO
forKey:NSFontCollectionRemoveDuplicatesOption]];
return ff; return ff;
} }
uintmax_t uiDrawFontFamiliesNumFamilies(uiDrawFontFamilies *ff) uintmax_t uiDrawFontFamiliesNumFamilies(uiDrawFontFamilies *ff)
{ {
// return CFArrayGetCount(ff->fonts); return CFArrayGetCount(ff->fonts);
return [ff->fonts count];
} }
char *uiDrawFontFamiliesFamily(uiDrawFontFamilies *ff, uintmax_t n) char *uiDrawFontFamiliesFamily(uiDrawFontFamilies *ff, uintmax_t n)
{ {
/*
CTFontDescriptorRef font;
CFStringRef familystr; CFStringRef familystr;
char *family; char *family;
font = (CTFontDescriptorRef) CFArrayGetValueAtIndex(ff->fonts, n); familystr = (CFStringRef) CFArrayGetValueAtIndex(ff->fonts, n);
familystr = (CFStringRef) CTFontDescriptorCopyAttribute(font, kCTFontFamilyNameAttribute);
// TODO create a uiDarwinCFStringToText()? // TODO create a uiDarwinCFStringToText()?
family = uiDarwinNSStringToText((NSString *) familystr); family = uiDarwinNSStringToText((NSString *) familystr);
CFRelease(familystr); // Get Rule means we do not free familystr
// Get Rule means we do not free font
return family;
*/
NSFontDescriptor *desc;
NSString *familystr;
char *family;
desc = (NSFontDescriptor *) [ff->fonts objectAtIndex:n];
familystr = (NSString *) [desc objectForKey:NSFontFamilyAttribute];
family = uiDarwinNSStringToText(familystr);
// TODO release familystr and desc?
return family; return family;
} }
void uiDrawFreeFontFamilies(uiDrawFontFamilies *ff) void uiDrawFreeFontFamilies(uiDrawFontFamilies *ff)
{ {
/*
CFRelease(ff->fonts); CFRelease(ff->fonts);
// TODO is this correct?
CFRelease(ff->collection);
CFRelease(ff->options);
CFRelease(ff->one);
*/
//TODO [ff->fonts release];
//TODO [ff->collection release];
uiFree(ff); uiFree(ff);
} }