diff options
author | Khaled Hosny <khaledhosny@eglug.org> | 2013-02-19 00:32:39 +0000 |
---|---|---|
committer | Khaled Hosny <khaledhosny@eglug.org> | 2013-02-19 00:32:39 +0000 |
commit | b9a5b9cad6d163192c3ab462fc86adc525a81e71 (patch) | |
tree | 11f84a8fdd80d31f9e49565a2e75a4e782146308 /Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm | |
parent | e43ae240f33296136cb498e6d9d0e9a55eba7493 (diff) |
Sync with XeTeX
git-svn-id: svn://tug.org/texlive/trunk@29157 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm')
-rw-r--r-- | Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm | 234 |
1 files changed, 88 insertions, 146 deletions
diff --git a/Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm b/Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm index bcc122adf01..a1b997c0573 100644 --- a/Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm +++ b/Build/source/texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm @@ -2,6 +2,7 @@ Part of the XeTeX typesetting system Copyright (c) 1994-2008 by SIL International Copyright (c) 2009 by Jonathan Kew + Copyright (c) 2012, 2013 by Jiang Jiang SIL Author(s): Jonathan Kew @@ -36,133 +37,70 @@ authorization from the copyright holders. #include <Cocoa/Cocoa.h> -XeTeXFontMgr::NameCollection* -XeTeXFontMgr_Mac::readNames(ATSFontRef fontRef) +CTFontDescriptorRef findFontWithName(CFStringRef name, CFStringRef key) { - const int BUFUNIT = 256; - static int bufSize = BUFUNIT; - static char* buffer = new char[bufSize]; + CFStringRef keys[] = { key }; + CFTypeRef values[] = { name }; + CFDictionaryRef attributes = CFDictionaryCreate(NULL, (const void **) &keys, (const void **) &values, 1, + &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(attributes); + CFRelease(attributes); + + CFSetRef mandatoryAttributes = CFSetCreate(NULL, (const void **) &keys, 1, &kCFTypeSetCallBacks); + CFArrayRef matches = CTFontDescriptorCreateMatchingFontDescriptors(descriptor, mandatoryAttributes); + CFRelease(mandatoryAttributes); + CFRelease(descriptor); + + CTFontDescriptorRef matched = NULL; + if (matches) { + if (CFArrayGetCount(matches)) { + matched = (CTFontDescriptorRef) CFArrayGetValueAtIndex(matches, 0); + CFRetain(matched); + } + CFRelease(matches); + } + return matched; +} + +void +XeTeXFontMgr_Mac::appendNameToList(CTFontRef font, + std::list<std::string>* nameList, + CFStringRef nameKey) +{ + CFStringRef name = CTFontCopyName(font, nameKey); + if (name) { + appendToList(nameList, [(NSString *) name UTF8String]); + CFRelease(name); + } + CFStringRef language; + name = CTFontCopyLocalizedName(font, nameKey, &language); + if (name) { + appendToList(nameList, [(NSString *) name UTF8String]); + CFRelease(name); + } +} +XeTeXFontMgr::NameCollection* +XeTeXFontMgr_Mac::readNames(CTFontDescriptorRef fontRef) +{ NameCollection* names = new NameCollection; - CFStringRef psName; - OSStatus status = ATSFontGetPostScriptName(fontRef, kATSOptionFlagsDefault, &psName); - if (status != noErr) + CFStringRef psName = (CFStringRef) CTFontDescriptorCopyAttribute(fontRef, kCTFontNameAttribute); + if (!psName) return names; - - // extract UTF-8 form of name - CFIndex length = CFStringGetLength(psName); // in 16-bit character units - if (length > 0) { - length = length * 6 + 1; - if (length >= bufSize) { - delete[] buffer; - bufSize = ((length / BUFUNIT) + 1) * BUFUNIT; - buffer = new char[bufSize]; - } - if (CFStringGetCString(psName, buffer, bufSize, kCFStringEncodingUTF8)) - names->psName = buffer; - } + + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + + names->psName = [(NSString *) psName UTF8String]; CFRelease(psName); - ATSUFontID fontID = FMGetFontFromATSFontRef(fontRef); - - ItemCount nameCount; - status = ATSUCountFontNames(fontID, &nameCount); - if (status != noErr) - die("ATSUCountFontNames failed, status=%d", status); - - std::list<std::string> familyNames; - std::list<std::string> subFamilyNames; - - for (int n = 0; n < nameCount; ++n) { - ByteCount nameLength; - FontNameCode nameCode; - FontPlatformCode namePlatform; - FontScriptCode nameScript; - FontLanguageCode nameLang; - status = ATSUGetIndFontName(fontID, n, 0, 0, &nameLength, - &nameCode, &namePlatform, &nameScript, &nameLang); - if (status != noErr && status != kATSUNoFontNameErr) - die("ATSUGetIndFontName failed, status=%d", status); - if (status == noErr && nameLength > 0) { - switch (nameCode) { - case kFontFullName: - case kFontFamilyName: - case kFontStyleName: - case 16: // preferred family -- use instead of family, if present - case 17: // preferred subfamily -- use instead of style, if present - { - bool preferredName = false; - TextEncoding encoding; - CFStringRef nameStr = 0; - if (nameLength >= bufSize) { - delete[] buffer; - bufSize = ((nameLength / BUFUNIT) + 1) * BUFUNIT; - buffer = new char[bufSize]; - } - status = ATSUGetIndFontName(fontID, n, bufSize, buffer, &nameLength, - &nameCode, &namePlatform, &nameScript, &nameLang); - if (namePlatform == kFontMacintoshPlatform) { - GetTextEncodingFromScriptInfo(nameScript, nameLang, 0, &encoding); - nameStr = CFStringCreateWithBytes(0, (UInt8*)buffer, nameLength, encoding, false); - if (nameScript == kFontRomanScript && nameLang == kFontEnglishLanguage) - preferredName = true; - } - else if ((namePlatform == kFontUnicodePlatform) || (namePlatform == kFontMicrosoftPlatform)) - nameStr = CFStringCreateWithCharacters(0, (UniChar*)buffer, nameLength / 2); - if (nameStr != 0) { - std::list<std::string>* nameList = NULL; - switch (nameCode) { - case kFontFullName: - nameList = &names->fullNames; - break; - case kFontFamilyName: - nameList = &names->familyNames; - break; - case kFontStyleName: - nameList = &names->styleNames; - break; - case 16: - nameList = &familyNames; - break; - case 17: - nameList = &subFamilyNames; - break; - } - - // extract UTF-8 form of name - length = CFStringGetLength(nameStr); // in 16-bit character units - if (length > 0) { - length = length * 6 + 1; - if (length >= bufSize) { - delete[] buffer; - bufSize = ((length / BUFUNIT) + 1) * BUFUNIT; - buffer = new char[bufSize]; - } - if (CFStringGetCString(nameStr, buffer, bufSize, kCFStringEncodingUTF8)) { - if (buffer[0] != 0) { - if (nameList != NULL) { - if (preferredName) - prependToList(nameList, buffer); - else - appendToList(nameList, buffer); - } - } - } - } - - CFRelease(nameStr); - } - } - break; - } - } - } + CTFontRef font = CTFontCreateWithFontDescriptor(fontRef, 0.0, 0); + appendNameToList(font, &names->fullNames, kCTFontFullNameKey); + appendNameToList(font, &names->familyNames, kCTFontFamilyNameKey); + appendNameToList(font, &names->styleNames, kCTFontStyleNameKey); + CFRelease(font); - if (familyNames.size() > 0) - names->familyNames = familyNames; - if (subFamilyNames.size() > 0) - names->styleNames = subFamilyNames; + [pool release]; return names; } @@ -172,7 +110,7 @@ XeTeXFontMgr_Mac::addFontsToCaches(CFArrayRef fonts) { NSEnumerator* enumerator = [(NSArray*)fonts objectEnumerator]; while (id aFont = [enumerator nextObject]) { - ATSFontRef fontRef = ATSFontFindFromPostScriptName((CFStringRef)[aFont objectAtIndex: 0], kATSOptionFlagsDefault); + CTFontDescriptorRef fontRef = findFontWithName((CFStringRef)[aFont objectAtIndex: 0], kCTFontNameAttribute); NameCollection* names = readNames(fontRef); addToMaps(fontRef, names); delete names; @@ -180,11 +118,10 @@ XeTeXFontMgr_Mac::addFontsToCaches(CFArrayRef fonts) } void -XeTeXFontMgr_Mac::addFamilyToCaches(ATSFontFamilyRef familyRef) +XeTeXFontMgr_Mac::addFamilyToCaches(CTFontDescriptorRef familyRef) { - CFStringRef nameStr; - OSStatus status = ATSFontFamilyGetName(familyRef, kATSOptionFlagsDefault, &nameStr); - if (status == noErr) { + CFStringRef nameStr = (CFStringRef) CTFontDescriptorCopyAttribute(familyRef, kCTFontFamilyNameAttribute); + if (nameStr) { NSArray* members = [[NSFontManager sharedFontManager] availableMembersOfFontFamily: (NSString*)nameStr]; CFRelease(nameStr); @@ -193,11 +130,10 @@ XeTeXFontMgr_Mac::addFamilyToCaches(ATSFontFamilyRef familyRef) } void -XeTeXFontMgr_Mac::addFontAndSiblingsToCaches(ATSFontRef fontRef) +XeTeXFontMgr_Mac::addFontAndSiblingsToCaches(CTFontDescriptorRef fontRef) { - CFStringRef name; - OSStatus status = ATSFontGetPostScriptName(fontRef, kATSOptionFlagsDefault, &name); - if (status == noErr) { + CFStringRef name = (CFStringRef) CTFontDescriptorCopyAttribute(fontRef, kCTFontNameAttribute); + if (name) { NSFont* font = [NSFont fontWithName:(NSString*)name size:10.0]; CFRelease(name); NSArray* members = [[NSFontManager sharedFontManager] @@ -217,10 +153,11 @@ XeTeXFontMgr_Mac::searchForHostPlatformFonts(const std::string& name) // ...so we need to try it as each of these CFStringRef nameStr = CFStringCreateWithCString(kCFAllocatorDefault, name.c_str(), kCFStringEncodingUTF8); - ATSFontRef fontRef = ATSFontFindFromName(nameStr, kATSOptionFlagsDefault); - if (fontRef != kATSFontRefUnspecified) { + CTFontDescriptorRef matched = findFontWithName(nameStr, kCTFontDisplayNameAttribute); + if (matched) { // found it, so locate the family, and add all members to the caches - addFontAndSiblingsToCaches(fontRef); + addFontAndSiblingsToCaches(matched); + CFRelease(matched); return; } @@ -236,16 +173,18 @@ XeTeXFontMgr_Mac::searchForHostPlatformFonts(const std::string& name) return; } - ATSFontFamilyRef familyRef = ATSFontFamilyFindFromName(familyStr, kATSOptionFlagsDefault); - if (familyRef != 0xffffffff) { - addFamilyToCaches(familyRef); + matched = findFontWithName(familyStr, kCTFontFamilyNameAttribute); + if (matched) { + addFamilyToCaches(matched); + CFRelease(matched); return; } } - fontRef = ATSFontFindFromPostScriptName(nameStr, kATSOptionFlagsDefault); - if (fontRef != kATSFontRefUnspecified) { - addFontAndSiblingsToCaches(fontRef); + matched = findFontWithName(nameStr, kCTFontNameAttribute); + if (matched) { + addFontAndSiblingsToCaches(matched); + CFRelease(matched); return; } @@ -256,9 +195,10 @@ XeTeXFontMgr_Mac::searchForHostPlatformFonts(const std::string& name) return; } - ATSFontFamilyRef familyRef = ATSFontFamilyFindFromName(nameStr, kATSOptionFlagsDefault); - if (familyRef != 0xffffffff) { - addFamilyToCaches(familyRef); + matched = findFontWithName(nameStr, kCTFontFamilyNameAttribute); + if (matched) { + addFamilyToCaches(matched); + CFRelease(matched); return; } } @@ -280,18 +220,20 @@ XeTeXFontMgr_Mac::terminate() } std::string -XeTeXFontMgr_Mac::getPlatformFontDesc(PlatformFontRef font) const +XeTeXFontMgr_Mac::getPlatformFontDesc(PlatformFontRef descriptor) const { - FSSpec fileSpec; std::string path; - if (ATSFontGetFileSpecification(font, &fileSpec) == noErr) { - FSRef fileRef; - if (FSpMakeFSRef(&fileSpec, &fileRef) == noErr) { + CTFontRef ctFont = CTFontCreateWithFontDescriptor(descriptor, 0.0, 0); + if (ctFont) { + CFURLRef url = (CFURLRef) CTFontCopyAttribute(ctFont, kCTFontURLAttribute); + if (url) { UInt8 posixPath[PATH_MAX]; - if (FSRefMakePath(&fileRef, posixPath, PATH_MAX) == noErr) { + if (CFURLGetFileSystemRepresentation(url, true, posixPath, PATH_MAX)) { path = (char*)posixPath; } + CFRelease(url); } + CFRelease(ctFont); } if (path.length() == 0) path = "[unknown]"; |