Debian Codegraph @

texk/web2c/xetexdir/XeTeXFontMgr_Mac.mm

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009 by Jonathan Kew
5 Copyright (c) 2012, 2013 by Jiang Jiang
6
7 SIL Author(s): Jonathan Kew
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
24 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the name of the copyright holders
29 shall not be used in advertising or otherwise to promote the sale,
30 use or other dealings in this Software without prior written
31 authorization from the copyright holders.
32 \****************************************************************************/
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 #include <w2c/config.h>
38 #ifdef __cplusplus
39 }
40 #endif
41
42 #include "XeTeXFontMgr_Mac.h"
43
44 #include <Cocoa/Cocoa.h>
45
46 CTFontDescriptorRef findFontWithName(CFStringRef name, CFStringRef key)
47 {
48 CFStringRef keys[] = { key };
49 CFTypeRef values[] = { name };
50 CFDictionaryRef attributes = CFDictionaryCreate(NULL, (const void **) &keys, (const void **) &values, 1,
51 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
52 CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(attributes);
53 CFRelease(attributes);
54
55 CFSetRef mandatoryAttributes = CFSetCreate(NULL, (const void **) &keys, 1, &kCFTypeSetCallBacks);
56 CFArrayRef matches = CTFontDescriptorCreateMatchingFontDescriptors(descriptor, mandatoryAttributes);
57 CFRelease(mandatoryAttributes);
58 CFRelease(descriptor);
59
60 CTFontDescriptorRef matched = NULL;
61 if (matches) {
62 if (CFArrayGetCount(matches)) {
63 matched = (CTFontDescriptorRef) CFArrayGetValueAtIndex(matches, 0);
64 CFRetain(matched);
65 }
66 CFRelease(matches);
67 }
68 return matched;
69 }
70
71 void
72 XeTeXFontMgr_Mac::appendNameToList(CTFontRef font,
73 std::list<std::string>* nameList,
74 CFStringRef nameKey)
75 {
76 CFStringRef name = CTFontCopyName(font, nameKey);
77 if (name) {
78 appendToList(nameList, [(NSString *) name UTF8String]);
79 CFRelease(name);
80 }
81 CFStringRef language;
82 name = CTFontCopyLocalizedName(font, nameKey, &language);
83 if (name) {
84 appendToList(nameList, [(NSString *) name UTF8String]);
85 CFRelease(name);
86 }
87 }
88
89 XeTeXFontMgr::NameCollection*
90 XeTeXFontMgr_Mac::readNames(CTFontDescriptorRef fontRef)
91 {
92 NameCollection* names = new NameCollection;
93
94 CFStringRef psName = (CFStringRef) CTFontDescriptorCopyAttribute(fontRef, kCTFontNameAttribute);
95 if (!psName)
96 return names;
97
98 NSAutoreleasePool *pool = [NSAutoreleasePool new];
99
100 names->m_psName = [(NSString *) psName UTF8String];
101 CFRelease(psName);
102
103 CTFontRef font = CTFontCreateWithFontDescriptor(fontRef, 0.0, 0);
104 appendNameToList(font, &names->m_fullNames, kCTFontFullNameKey);
105 appendNameToList(font, &names->m_familyNames, kCTFontFamilyNameKey);
106 appendNameToList(font, &names->m_styleNames, kCTFontStyleNameKey);
107 CFRelease(font);
108
109 [pool release];
110
111 return names;
112 }
113
114 void
115 XeTeXFontMgr_Mac::addFontsToCaches(CFArrayRef fonts)
116 {
117 NSEnumerator* enumerator = [(NSArray*)fonts objectEnumerator];
118 while (id aFont = [enumerator nextObject]) {
119 CTFontDescriptorRef fontRef = findFontWithName((CFStringRef)[aFont objectAtIndex: 0], kCTFontNameAttribute);
120 NameCollection* names = readNames(fontRef);
121 addToMaps(fontRef, names);
122 delete names;
123 }
124 }
125
126 void
127 XeTeXFontMgr_Mac::addFamilyToCaches(CTFontDescriptorRef familyRef)
128 {
129 CFStringRef nameStr = (CFStringRef) CTFontDescriptorCopyAttribute(familyRef, kCTFontFamilyNameAttribute);
130 if (nameStr) {
131 NSArray* members = [[NSFontManager sharedFontManager]
132 availableMembersOfFontFamily: (NSString*)nameStr];
133 CFRelease(nameStr);
134 addFontsToCaches((CFArrayRef)members);
135 }
136 }
137
138 void
139 XeTeXFontMgr_Mac::addFontAndSiblingsToCaches(CTFontDescriptorRef fontRef)
140 {
141 CFStringRef name = (CFStringRef) CTFontDescriptorCopyAttribute(fontRef, kCTFontNameAttribute);
142 if (name) {
143 NSFont* font = [NSFont fontWithName:(NSString*)name size:10.0];
144 CFRelease(name);
145 NSArray* members = [[NSFontManager sharedFontManager]
146 availableMembersOfFontFamily: [font familyName]];
147 addFontsToCaches((CFArrayRef)members);
148 }
149 }
150
151 void
152 XeTeXFontMgr_Mac::searchForHostPlatformFonts(const std::string& name)
153 {
154 // the name might be:
155 // FullName
156 // Family-Style (if there's a hyphen)
157 // PSName
158 // Family
159 // ...so we need to try it as each of these
160
161 CFStringRef nameStr = CFStringCreateWithCString(kCFAllocatorDefault, name.c_str(), kCFStringEncodingUTF8);
162 CTFontDescriptorRef matched = findFontWithName(nameStr, kCTFontDisplayNameAttribute);
163 if (matched) {
164 // found it, so locate the family, and add all members to the caches
165 addFontAndSiblingsToCaches(matched);
166 CFRelease(matched);
167 return;
168 }
169
170 int hyph = name.find('-');
171 if (hyph > 0 && hyph < name.length() - 1) {
172 std::string family(name.begin(), name.begin() + hyph);
173 CFStringRef familyStr = CFStringCreateWithCString(kCFAllocatorDefault, family.c_str(), kCFStringEncodingUTF8);
174
175 NSArray* familyMembers = [[NSFontManager sharedFontManager]
176 availableMembersOfFontFamily: (NSString*)familyStr];
177 if ([familyMembers count] > 0) {
178 addFontsToCaches((CFArrayRef)familyMembers);
179 return;
180 }
181
182 matched = findFontWithName(familyStr, kCTFontFamilyNameAttribute);
183 if (matched) {
184 addFamilyToCaches(matched);
185 CFRelease(matched);
186 return;
187 }
188 }
189
190 matched = findFontWithName(nameStr, kCTFontNameAttribute);
191 if (matched) {
192 addFontAndSiblingsToCaches(matched);
193 CFRelease(matched);
194 return;
195 }
196
197 NSArray* familyMembers = [[NSFontManager sharedFontManager]
198 availableMembersOfFontFamily: (NSString*)nameStr];
199 if ([familyMembers count] > 0) {
200 addFontsToCaches((CFArrayRef)familyMembers);
201 return;
202 }
203
204 matched = findFontWithName(nameStr, kCTFontFamilyNameAttribute);
205 if (matched) {
206 addFamilyToCaches(matched);
207 CFRelease(matched);
208 return;
209 }
210 }
211
212 NSAutoreleasePool* pool = NULL;
213
214 void
215 XeTeXFontMgr_Mac::initialize()
216 {
217 pool = [[NSAutoreleasePool alloc] init];
218 }
219
220 void
221 XeTeXFontMgr_Mac::terminate()
222 {
223 if (pool != NULL) {
224 [pool release];
225 }
226 }
227
228 std::string
229 XeTeXFontMgr_Mac::getPlatformFontDesc(PlatformFontRef descriptor) const
230 {
231 std::string path;
232 CTFontRef ctFont = CTFontCreateWithFontDescriptor(descriptor, 0.0, 0);
233 if (ctFont) {
234 CFURLRef url = NULL;
235 #if !defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
236 /* kCTFontURLAttribute was not avialable before 10.6 */
237 FSRef fsref;
238 ATSFontRef atsFont = CTFontGetPlatformFont(ctFont, NULL);
239 OSStatus status = ATSFontGetFileReference(atsFont, &fsref);
240 if (status == noErr)
241 url = CFURLCreateFromFSRef(NULL, &fsref);
242 #else
243 url = (CFURLRef) CTFontCopyAttribute(ctFont, kCTFontURLAttribute);
244 #endif
245 if (url) {
246 UInt8 posixPath[PATH_MAX];
247 if (CFURLGetFileSystemRepresentation(url, true, posixPath, PATH_MAX)) {
248 path = (char*)posixPath;
249 }
250 CFRelease(url);
251 }
252 CFRelease(ctFont);
253 }
254 if (path.length() == 0)
255 path = "[unknown]";
256 return path;
257 }