2 WebTextRendererFactory.m
3 Copyright 2002, Apple, Inc. All rights reserved.
6 #import <WebKit/WebAssertions.h>
7 #import <WebKit/WebBridge.h>
8 #import <WebKit/WebKitLogging.h>
9 #import <WebKit/WebKitSystemBits.h>
10 #import <WebKit/WebPreferences.h>
11 #import <WebKit/WebTextRendererFactory.h>
12 #import <WebKit/WebTextRenderer.h>
14 #import <CoreGraphics/CoreGraphicsPrivate.h>
15 #import <CoreGraphics/CGFontLCDSupport.h>
16 #import <CoreGraphics/CGFontCache.h>
18 #import <mach-o/dyld.h>
20 #define IMPORTANT_FONT_TRAITS (0 \
22 | NSCompressedFontMask \
23 | NSCondensedFontMask \
24 | NSExpandedFontMask \
28 | NSSmallCapsFontMask \
31 #define DESIRED_WEIGHT 5
33 @interface NSFont (WebPrivate)
34 - (ATSUFontID)_atsFontID;
37 @interface NSFont (WebAppKitSecretAPI)
38 - (BOOL)_isFakeFixedPitch;
41 @implementation WebTextRendererFactory
43 - (BOOL)coalesceTextDrawing
45 return [viewStack objectAtIndex: [viewStack count]-1] == [NSView focusView] ? YES : NO;
48 - (void)startCoalesceTextDrawing
51 viewStack = [[NSMutableArray alloc] init];
53 viewBuffers = [[NSMutableDictionary alloc] init];
54 [viewStack addObject: [NSView focusView]];
57 - (void)endCoalesceTextDrawing
59 ASSERT([self coalesceTextDrawing]);
61 NSView *targetView = [viewStack objectAtIndex: [viewStack count]-1];
62 [viewStack removeLastObject];
63 NSValue *viewKey = [NSValue valueWithNonretainedObject: targetView];
64 NSMutableSet *glyphBuffers = [viewBuffers objectForKey:viewKey];
66 [glyphBuffers makeObjectsPerformSelector: @selector(drawInView:) withObject: targetView];
67 [glyphBuffers makeObjectsPerformSelector: @selector(reset)];
68 [viewBuffers removeObjectForKey: viewKey];
71 - (WebGlyphBuffer *)glyphBufferForFont: (NSFont *)font andColor: (NSColor *)color
73 ASSERT([self coalesceTextDrawing]);
75 NSMutableSet *glyphBuffers;
76 WebGlyphBuffer *glyphBuffer = nil;
77 NSValue *viewKey = [NSValue valueWithNonretainedObject: [NSView focusView]];
79 glyphBuffers = [viewBuffers objectForKey:viewKey];
80 if (glyphBuffers == nil){
81 glyphBuffers = [[NSMutableSet alloc] init];
82 [viewBuffers setObject: glyphBuffers forKey: viewKey];
83 [glyphBuffers release];
86 NSEnumerator *enumerator = [glyphBuffers objectEnumerator];
89 // Could use a dictionary w/ font/color key for faster lookup.
90 while ((value = [enumerator nextObject])) {
91 if ([value font] == font && [[value color] isEqual: color])
94 if (glyphBuffer == nil){
95 glyphBuffer = [[WebGlyphBuffer alloc] initWithFont: font color: color];
96 [glyphBuffers addObject: glyphBuffer];
97 [glyphBuffer release];
104 getAppDefaultValue(CFStringRef key, int *v)
106 CFPropertyListRef value;
108 value = CFPreferencesCopyValue(key, kCFPreferencesCurrentApplication,
109 kCFPreferencesAnyUser,
110 kCFPreferencesAnyHost);
112 value = CFPreferencesCopyValue(key, kCFPreferencesCurrentApplication,
113 kCFPreferencesCurrentUser,
114 kCFPreferencesAnyHost);
119 if (CFGetTypeID(value) == CFNumberGetTypeID()) {
121 CFNumberGetValue(value, kCFNumberIntType, v);
122 } else if (CFGetTypeID(value) == CFStringGetTypeID()) {
124 *v = CFStringGetIntValue(value);
135 getUserDefaultValue(CFStringRef key, int *v)
137 CFPropertyListRef value;
139 value = CFPreferencesCopyValue(key, kCFPreferencesAnyApplication,
140 kCFPreferencesCurrentUser,
141 kCFPreferencesCurrentHost);
145 if (CFGetTypeID(value) == CFNumberGetTypeID()) {
147 CFNumberGetValue(value, kCFNumberIntType, v);
148 } else if (CFGetTypeID(value) == CFStringGetTypeID()) {
150 *v = CFStringGetIntValue(value);
160 static int getLCDScaleParameters(void)
165 key = CFSTR("AppleFontSmoothing");
166 if (!getAppDefaultValue(key, &mode)) {
167 if (!getUserDefaultValue(key, &mode))
172 case kCGFontSmoothingLCDLight:
173 case kCGFontSmoothingLCDMedium:
174 case kCGFontSmoothingLCDStrong:
182 static CFMutableDictionaryRef fontCache;
183 static CFMutableDictionaryRef fixedPitchFonts;
187 [cacheForScreen release];
188 [cacheForPrinter release];
190 cacheForScreen = [[NSMutableDictionary alloc] init];
191 cacheForPrinter = [[NSMutableDictionary alloc] init];
194 CFRelease(fontCache);
197 if (fixedPitchFonts) {
198 CFRelease (fixedPitchFonts);
206 fontsChanged( ATSFontNotificationInfoRef info, void *_factory)
208 WebTextRendererFactory *factory = (WebTextRendererFactory *)_factory;
210 LOG (FontCache, "clearing font caches");
214 [factory clearCaches];
217 #define MINIMUM_GLYPH_CACHE_SIZE 1536 * 1024
219 + (void)createSharedFactory
221 if (![self sharedFactory]) {
222 [[[self alloc] init] release];
224 #if !defined(BUILDING_ON_PANTHER)
225 // Turn on local font cache, in addition to the system cache.
227 CGFontSetShouldUseMulticache(true);
230 CGFontCache *fontCache;
231 fontCache = CGFontCacheGetLocalCache();
232 CGFontCacheSetShouldAutoExpire (fontCache, false);
235 if (WebSystemMainMemory() > 128 * 1024 * 1024)
236 s = MINIMUM_GLYPH_CACHE_SIZE*getLCDScaleParameters();
238 s = MINIMUM_GLYPH_CACHE_SIZE;
240 LOG (CacheSizes, "Glyph cache size set to %d bytes.", s);
242 CGFontCacheSetMaxSize (fontCache, s);
244 // Ignore errors returned from ATSFontNotificationSubscribe. If we can't subscribe then we
245 // won't be told about changes to fonts.
246 ATSFontNotificationSubscribe( fontsChanged, kATSFontNotifyOptionDefault, (void *)[super sharedFactory], nil );
248 ASSERT([[self sharedFactory] isKindOfClass:self]);
251 + (WebTextRendererFactory *)sharedFactory;
253 return (WebTextRendererFactory *)[super sharedFactory];
256 - (BOOL)isFontFixedPitch: (NSFont *)font
260 if (!fixedPitchFonts) {
261 fixedPitchFonts = CFDictionaryCreateMutable (0, 0, &kCFTypeDictionaryKeyCallBacks, 0);
264 CFBooleanRef val = CFDictionaryGetValue (fixedPitchFonts, font);
266 ret = (val == kCFBooleanTrue);
269 // Special case Osaka-Mono. According to <rdar://problem/3999467>, we should treat Osaka-Mono
270 // as fixed pitch. Note that the AppKit does not report MS-PGothic as fixed pitch.
272 // Special case MS PGothic. According to <rdar://problem/4032938, we should not treat MS-PGothic
273 // as fixed pitch. Note that AppKit does report MS-PGothic as fixed pitch.
274 if (([font isFixedPitch] || [font _isFakeFixedPitch] || [[font fontName] caseInsensitiveCompare:@"Osaka-Mono"] == NSOrderedSame) &&
275 ![[font fontName] caseInsensitiveCompare:@"MS-PGothic"] == NSOrderedSame) {
276 CFDictionarySetValue (fixedPitchFonts, font, kCFBooleanTrue);
280 CFDictionarySetValue (fixedPitchFonts, font, kCFBooleanFalse);
291 cacheForScreen = [[NSMutableDictionary alloc] init];
292 cacheForPrinter = [[NSMutableDictionary alloc] init];
299 [cacheForScreen release];
300 [cacheForPrinter release];
301 [viewBuffers release];
307 - (WebTextRenderer *)rendererWithFont:(NSFont *)font usingPrinterFont:(BOOL)usingPrinterFont
309 NSMutableDictionary *cache = usingPrinterFont ? cacheForPrinter : cacheForScreen;
310 WebTextRenderer *renderer = [cache objectForKey:font];
311 if (renderer == nil) {
312 renderer = [[WebTextRenderer alloc] initWithFont:font usingPrinterFont:usingPrinterFont];
313 [cache setObject:renderer forKey:font];
319 - (NSFont *)fallbackFontWithTraits:(NSFontTraitMask)traits size:(float)size
321 NSFont *font = [self cachedFontFromFamily:@"Helvetica" traits:traits size:size];
323 // The Helvetica fallback will almost always work, since that's a basic
324 // font that we ship with all systems. But in the highly unusual case where
325 // the user removed Helvetica, we fall back on Lucida Grande because that's
326 // guaranteed to be there, according to Nathan Taylor. This is good enough
327 // to avoid a crash, at least. To reduce the possibility of failure even further,
328 // we don't even bother with traits.
329 font = [self cachedFontFromFamily:@"Lucida Grande" traits:0 size:size];
336 - (NSFont *)fontWithFamilies:(NSString **)families traits:(NSFontTraitMask)traits size:(float)size
342 while (families && families[i] != 0 && font == nil){
343 family = families[i++];
344 if ([family length] != 0)
345 font = [self cachedFontFromFamily: family traits:traits size:size];
348 // We didn't find a font. Use a fallback font.
349 static int matchCount = 3;
350 static NSString *matchWords[] = { @"Arabic", @"Pashto", @"Urdu" };
351 static NSString *matchFamilies[] = { @"Geeza Pro", @"Geeza Pro", @"Geeza Pro" };
353 // First we'll attempt to find an appropriate font using a match based on
354 // the presence of keywords in the the requested names. For example, we'll
355 // match any name that contains "Arabic" to Geeza Pro.
358 while (families && families[i] != 0 && font == nil) {
359 family = families[i++];
360 if ([family length] != 0) {
362 while (j < matchCount && font == nil) {
363 if ([family rangeOfString:matchWords[j] options:NSCaseInsensitiveSearch].location != NSNotFound) {
364 font = [self cachedFontFromFamily:matchFamilies[j] traits:traits size:size];
371 // Still nothing found, use the final fallback.
373 font = [self fallbackFontWithTraits:traits size:size];
380 static BOOL acceptableChoice(NSFontTraitMask desiredTraits, int desiredWeight,
381 NSFontTraitMask candidateTraits, int candidateWeight)
383 return (candidateTraits & desiredTraits) == desiredTraits;
386 static BOOL betterChoice(NSFontTraitMask desiredTraits, int desiredWeight,
387 NSFontTraitMask chosenTraits, int chosenWeight,
388 NSFontTraitMask candidateTraits, int candidateWeight)
390 if (!acceptableChoice(desiredTraits, desiredWeight, candidateTraits, candidateWeight)) {
394 // A list of the traits we care about.
395 // The top item in the list is the worst trait to mismatch; if a font has this
396 // and we didn't ask for it, we'd prefer any other font in the family.
397 const NSFontTraitMask masks[] = {
401 NSCompressedFontMask,
408 NSFontTraitMask mask;
409 while ((mask = masks[i++])) {
410 if ((desiredTraits & mask) != 0) {
411 ASSERT((chosenTraits & mask) != 0);
412 ASSERT((candidateTraits & mask) != 0);
415 BOOL chosenHasUnwantedTrait = (chosenTraits & mask) != 0;
416 BOOL candidateHasUnwantedTrait = (candidateTraits & mask) != 0;
417 if (!candidateHasUnwantedTrait && chosenHasUnwantedTrait) {
420 if (!chosenHasUnwantedTrait && candidateHasUnwantedTrait) {
425 int chosenWeightDelta = chosenWeight - desiredWeight;
426 int candidateWeightDelta = candidateWeight - desiredWeight;
428 int chosenWeightDeltaMagnitude = ABS(chosenWeightDelta);
429 int candidateWeightDeltaMagnitude = ABS(candidateWeightDelta);
431 // Smaller magnitude wins.
432 // If both have same magnitude, tie breaker is that the smaller weight wins.
433 // Otherwise, first font in the array wins (should almost never happen).
434 if (candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude) {
437 if (candidateWeightDeltaMagnitude == chosenWeightDeltaMagnitude && candidateWeight < chosenWeight) {
444 // Family name is somewhat of a misnomer here. We first attempt to find an exact match
445 // comparing the desiredFamily to the PostScript name of the installed fonts. If that fails
446 // we then do a search based on the family names of the installed fonts.
447 - (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits size:(float)size
449 NSFontManager *fontManager = [NSFontManager sharedFontManager];
452 LOG (FontSelection, "looking for %@ with traits %x\n", desiredFamily, desiredTraits);
454 // Look for an exact match first.
455 NSEnumerator *availableFonts = [[fontManager availableFonts] objectEnumerator];
456 NSString *availableFont;
457 while ((availableFont = [availableFonts nextObject])) {
458 if ([desiredFamily caseInsensitiveCompare:availableFont] == NSOrderedSame) {
459 NSFont *nameMatchedFont = [NSFont fontWithName:availableFont size:size];
461 // Special case Osaka-Mono. According to <rdar://problem/3999467>, we need to
462 // treat Osaka-Mono as fixed pitch.
463 if ([desiredFamily caseInsensitiveCompare:@"Osaka-Mono"] == NSOrderedSame && desiredTraits == 0) {
464 LOG (FontSelection, "found exact match for Osaka-Mono\n");
465 return nameMatchedFont;
468 NSFontTraitMask traits = [fontManager traitsOfFont:nameMatchedFont];
470 if ((traits & desiredTraits) == desiredTraits){
471 font = [fontManager convertFont:nameMatchedFont toHaveTrait:desiredTraits];
472 LOG (FontSelection, "returning exact match (%@)\n\n", [[[font fontDescriptor] fontAttributes] objectForKey: NSFontNameAttribute]);
475 LOG (FontSelection, "found exact match, but not desired traits, available traits %x\n", traits);
480 // Do a simple case insensitive search for a matching font family.
481 // NSFontManager requires exact name matches.
482 // This addresses the problem of matching arial to Arial, etc., but perhaps not all the issues.
483 NSEnumerator *e = [[fontManager availableFontFamilies] objectEnumerator];
484 NSString *availableFamily;
485 while ((availableFamily = [e nextObject])) {
486 if ([desiredFamily caseInsensitiveCompare:availableFamily] == NSOrderedSame) {
491 // Found a family, now figure out what weight and traits to use.
492 BOOL choseFont = false;
493 int chosenWeight = 0;
494 NSFontTraitMask chosenTraits = 0;
496 NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
497 unsigned n = [fonts count];
499 for (i = 0; i < n; i++) {
500 NSArray *fontInfo = [fonts objectAtIndex:i];
502 // Array indices must be hard coded because of lame AppKit API.
503 int fontWeight = [[fontInfo objectAtIndex:2] intValue];
504 NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValue];
509 newWinner = acceptableChoice(desiredTraits, DESIRED_WEIGHT, fontTraits, fontWeight);
511 newWinner = betterChoice(desiredTraits, DESIRED_WEIGHT,
512 chosenTraits, chosenWeight, fontTraits, fontWeight);
517 chosenWeight = fontWeight;
518 chosenTraits = fontTraits;
520 if (chosenWeight == DESIRED_WEIGHT
521 && (chosenTraits & IMPORTANT_FONT_TRAITS) == (desiredTraits & IMPORTANT_FONT_TRAITS)) {
528 LOG (FontSelection, "nothing appropriate to return\n\n");
532 font = [fontManager fontWithFamily:availableFamily traits:chosenTraits weight:chosenWeight size:size];
533 LOG (FontSelection, "returning font family %@ (%@) traits %x, fontID = %x\n\n",
534 availableFamily, [[[font fontDescriptor] fontAttributes] objectForKey: NSFontNameAttribute], chosenTraits, (unsigned int)[font _atsFontID]);
541 NSFontTraitMask traits;
545 static const void *FontCacheKeyCopy(CFAllocatorRef allocator, const void *value)
547 const FontCacheKey *key = (const FontCacheKey *)value;
548 FontCacheKey *result = malloc(sizeof(FontCacheKey));
549 result->family = [key->family copy];
550 result->traits = key->traits;
551 result->size = key->size;
555 static void FontCacheKeyFree(CFAllocatorRef allocator, const void *value)
557 const FontCacheKey *key = (const FontCacheKey *)value;
558 [key->family release];
562 static Boolean FontCacheKeyEqual(const void *value1, const void *value2)
564 const FontCacheKey *key1 = (const FontCacheKey *)value1;
565 const FontCacheKey *key2 = (const FontCacheKey *)value2;
566 return key1->size == key2->size && key1->traits == key2->traits && [key1->family isEqualToString:key2->family];
569 static CFHashCode FontCacheKeyHash(const void *value)
571 const FontCacheKey *key = (const FontCacheKey *)value;
572 return [key->family hash] ^ key->traits ^ (int)key->size;
575 static const void *FontCacheValueRetain(CFAllocatorRef allocator, const void *value)
583 static void FontCacheValueRelease(CFAllocatorRef allocator, const void *value)
590 - (NSFont *)cachedFontFromFamily:(NSString *)family traits:(NSFontTraitMask)traits size:(float)size
595 static const CFDictionaryKeyCallBacks fontCacheKeyCallBacks = { 0, FontCacheKeyCopy, FontCacheKeyFree, NULL, FontCacheKeyEqual, FontCacheKeyHash };
596 static const CFDictionaryValueCallBacks fontCacheValueCallBacks = { 0, FontCacheValueRetain, FontCacheValueRelease, NULL, NULL };
597 fontCache = CFDictionaryCreateMutable(NULL, 0, &fontCacheKeyCallBacks, &fontCacheValueCallBacks);
600 const FontCacheKey fontKey = { family, traits, size };
603 if (CFDictionaryGetValueIfPresent(fontCache, &fontKey, &value)) {
604 font = (NSFont *)value;
606 if ([family length] == 0) {
609 font = [self fontWithFamily:family traits:traits size:size];
610 CFDictionaryAddValue(fontCache, &fontKey, font);
613 #ifdef DEBUG_MISSING_FONT
614 static int unableToFindFontCount = 0;
616 unableToFindFontCount++;
617 NSLog(@"unableToFindFontCount %@, traits 0x%08x, size %f, %d\n", family, traits, size, unableToFindFontCount);