Changeset 866

Show
Ignore:
Timestamp:
04/22/08 22:42:09 (5 months ago)
Author:
astrange
Message:

Reorder font lookups and add a cleanup function for the cache.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Subtitles/SubATSUIRenderer.m

    r864 r866  
    287287} 
    288288 
     289static NSMutableDictionary *fontIDCache = nil; 
     290 
     291static void CleanupFontIDCache() __attribute__((destructor)); 
     292static void CleanupFontIDCache() 
     293{ 
     294        if (fontIDCache) [fontIDCache release]; 
     295        fontIDCache = nil; 
     296} 
     297 
    289298// XXX: Assumes ATSUFontID = ATSFontRef. This is true. 
    290 // May appear as a memory leak, but it isn't, not really. 
    291299static ATSUFontID GetFontIDForSSAName(NSString *name) 
    292300{ 
    293301        ByteCount nlen = [name length]; 
    294302        unichar *uname = (unichar*)[name cStringUsingEncoding:NSUnicodeStringEncoding]; 
    295         static NSMutableDictionary *fontIDCache = nil; 
    296303         
    297304        if (!fontIDCache) fontIDCache = [[NSMutableDictionary alloc] init]; 
     
    305312        ATSUFindFontFromName(uname, nlen * sizeof(unichar), kFontFamilyName, kFontNoPlatformCode, kFontNoScript, kFontNoLanguage, &font); 
    306313         
     314        if (font == kATSUInvalidFontID) font = ATSFontFindFromName((CFStringRef)name, kATSOptionFlagsDefault); // for bugs in ATS under 10.4 
    307315        if (font == kATSUInvalidFontID) { // try a case-insensitive search 
    308316                ItemCount fontCount; 
     
    317325                ByteCount len; 
    318326                ItemCount x, index; 
    319                 const ByteCount kBufLength = 1024
    320                 char buf[kBufLength]; 
     327                const ByteCount kBufLength = 1024/sizeof(unichar)
     328                unichar buf[kBufLength]; 
    321329           
    322330                for (x = 0; x < fontCount; x++) { 
    323                         ATSUFindFontName(fontIDs[x], kFontFamilyName, kFontMicrosoftPlatform, kFontNoScript, kFontNoLanguage, kBufLength, buf, &len, &index); 
     331                        ATSUFindFontName(fontIDs[x], kFontFamilyName, kFontMicrosoftPlatform, kFontNoScript, kFontNoLanguage, kBufLength, (Ptr)buf, &len, &index); 
     332                        NSString *fname = [NSString stringWithCharacters:buf length:len/sizeof(unichar)]; 
    324333                         
    325                         CFStringRef fname = CFStringCreateWithBytes(NULL,(UInt8 *) buf, len, kCFStringEncodingUnicode, false); 
    326                         if ([name caseInsensitiveCompare:(NSString *) fname] == NSOrderedSame) { 
     334                        if ([name caseInsensitiveCompare:fname] == NSOrderedSame) { 
    327335                                font = fontIDs[x]; 
    328336                                break; 
    329337                        } 
    330                          
    331                         CFRelease(fname); 
    332338                } 
    333339                 
    334                 if (font == kATSUInvalidFontID) font = ATSFontFindFromName((CFStringRef)name,        kATSOptionFlagsDefault); // for bugs in ATS under 10.4 
    335340                if (font == kATSUInvalidFontID) font = ATSFontFindFromName((CFStringRef)@"Helvetica",kATSOptionFlagsDefault); // final fallback 
    336341        }