From bcc17a4e6dad572862527165841084fef9edadb5 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 22 Dec 2015 21:48:26 -0500 Subject: [PATCH] Attempted to write the font enumeration code for OS X. It does not work yet (memory management is borked and duplicate removal does not work, period). --- darwin/draw.m | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/darwin/draw.m b/darwin/draw.m index 4405b30a..3b94f201 100644 --- a/darwin/draw.m +++ b/darwin/draw.m @@ -447,3 +447,97 @@ void uiDrawRestore(uiDrawContext *c) { CGContextRestoreGState(c->c); } + +// TODO for all relevant routines, make sure we are freeing memory correctly +// TODO make sure allocation failures throw exceptions? +struct uiDrawFontFamilies { +/* + CFNumberRef one; + CFDictionaryRef options; + CTFontCollectionRef collection; + CFArrayRef fonts; +*/ + NSFontCollection *collection; + NSArray *fonts; +}; + +uiDrawFontFamilies *uiDrawListFontFamilies(void) +{ + uiDrawFontFamilies *ff; +/* + const void *keys[1]; + const void *values[1]; + SInt32 one = 1; +*/ + + ff = uiNew(uiDrawFontFamilies); + +/* + ff->one = CFNumberCreate(NULL, kCFNumberSInt32Type, &one); + + // 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; +} + +uintmax_t uiDrawFontFamiliesNumFamilies(uiDrawFontFamilies *ff) +{ +// return CFArrayGetCount(ff->fonts); + return [ff->fonts count]; +} + +char *uiDrawFontFamiliesFamily(uiDrawFontFamilies *ff, uintmax_t n) +{ +/* + CTFontDescriptorRef font; + CFStringRef familystr; + char *family; + + font = (CTFontDescriptorRef) CFArrayGetValueAtIndex(ff->fonts, n); + familystr = (CFStringRef) CTFontDescriptorCopyAttribute(font, kCTFontFamilyNameAttribute); + // TODO create a uiDarwinCFStringToText()? + family = uiDarwinNSStringToText((NSString *) familystr); + CFRelease(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; +} + +void uiDrawFreeFontFamilies(uiDrawFontFamilies *ff) +{ +/* + 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); +}