| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> | 3 * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com> |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 int candidateWeightDeltaMagnitude = abs(candidateWeight - desiredWeight); | 94 int candidateWeightDeltaMagnitude = abs(candidateWeight - desiredWeight); |
| 95 | 95 |
| 96 // If both are the same distance from the desired weight, prefer the candida
te if it is further from medium. | 96 // If both are the same distance from the desired weight, prefer the candida
te if it is further from medium. |
| 97 if (chosenWeightDeltaMagnitude == candidateWeightDeltaMagnitude) | 97 if (chosenWeightDeltaMagnitude == candidateWeightDeltaMagnitude) |
| 98 return abs(candidateWeight - 6) > abs(chosenWeight - 6); | 98 return abs(candidateWeight - 6) > abs(chosenWeight - 6); |
| 99 | 99 |
| 100 // Otherwise, prefer the one closer to the desired weight. | 100 // Otherwise, prefer the one closer to the desired weight. |
| 101 return candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude; | 101 return candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude; |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Workaround for <rdar://problem/5781372>. | |
| 105 static inline void fixUpWeight(NSInteger& weight, NSString *fontName) | |
| 106 { | |
| 107 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 | |
| 108 UNUSED_PARAM(weight); | |
| 109 UNUSED_PARAM(fontName); | |
| 110 #else | |
| 111 if (weight == 3 && [fontName rangeOfString:@"ultralight" options:NSCaseInsen
sitiveSearch | NSBackwardsSearch | NSLiteralSearch].location != NSNotFound) | |
| 112 weight = 2; | |
| 113 #endif | |
| 114 } | |
| 115 | |
| 116 static inline FontTraitsMask toTraitsMask(NSFontTraitMask appKitTraits, NSIntege
r appKitWeight) | 104 static inline FontTraitsMask toTraitsMask(NSFontTraitMask appKitTraits, NSIntege
r appKitWeight) |
| 117 { | 105 { |
| 118 return static_cast<FontTraitsMask>(((appKitTraits & NSFontItalicTrait) ? Fon
tStyleItalicMask : FontStyleNormalMask) | 106 return static_cast<FontTraitsMask>(((appKitTraits & NSFontItalicTrait) ? Fon
tStyleItalicMask : FontStyleNormalMask) |
| 119 | FontVariantNormalMask | 107 | FontVariantNormalMask |
| 120 | (appKitWeight == 1 ? FontWeight100Mask : | 108 | (appKitWeight == 1 ? FontWeight100Mask : |
| 121 appKitWeight == 2 ? FontWeight200Mask : | 109 appKitWeight == 2 ? FontWeight200Mask : |
| 122 appKitWeight <= 4 ? FontWeight300Mask : | 110 appKitWeight <= 4 ? FontWeight300Mask : |
| 123 appKitWeight == 5 ? FontWeight400Mask : | 111 appKitWeight == 5 ? FontWeight400Mask : |
| 124 appKitWeight == 6 ? FontWeight500Mask : | 112 appKitWeight == 6 ? FontWeight500Mask : |
| 125 appKitWeight <= 8 ? FontWeight600Mask : | 113 appKitWeight <= 8 ? FontWeight600Mask : |
| (...skipping 16 matching lines...) Expand all Loading... |
| 142 } | 130 } |
| 143 | 131 |
| 144 if (!availableFamily) { | 132 if (!availableFamily) { |
| 145 // Match by PostScript name. | 133 // Match by PostScript name. |
| 146 NSEnumerator *availableFonts = [[fontManager availableFonts] objectEnume
rator]; | 134 NSEnumerator *availableFonts = [[fontManager availableFonts] objectEnume
rator]; |
| 147 NSString *availableFont; | 135 NSString *availableFont; |
| 148 while ((availableFont = [availableFonts nextObject])) { | 136 while ((availableFont = [availableFonts nextObject])) { |
| 149 if ([desiredFamily caseInsensitiveCompare:availableFont] == NSOrdere
dSame) { | 137 if ([desiredFamily caseInsensitiveCompare:availableFont] == NSOrdere
dSame) { |
| 150 NSFont *font = [NSFont fontWithName:availableFont size:10]; | 138 NSFont *font = [NSFont fontWithName:availableFont size:10]; |
| 151 NSInteger weight = [fontManager weightOfFont:font]; | 139 NSInteger weight = [fontManager weightOfFont:font]; |
| 152 fixUpWeight(weight, desiredFamily); | |
| 153 traitsMasks.append(toTraitsMask([fontManager traitsOfFont:font],
weight)); | 140 traitsMasks.append(toTraitsMask([fontManager traitsOfFont:font],
weight)); |
| 154 break; | 141 break; |
| 155 } | 142 } |
| 156 } | 143 } |
| 157 return; | 144 return; |
| 158 } | 145 } |
| 159 | 146 |
| 160 NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
| 147 NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
|
| 161 unsigned n = [fonts count]; | 148 unsigned n = [fonts count]; |
| 162 unsigned i; | 149 unsigned i; |
| 163 for (i = 0; i < n; i++) { | 150 for (i = 0; i < n; i++) { |
| 164 NSArray *fontInfo = [fonts objectAtIndex:i]; | 151 NSArray *fontInfo = [fonts objectAtIndex:i]; |
| 165 // Array indices must be hard coded because of lame AppKit API. | 152 // Array indices must be hard coded because of lame AppKit API. |
| 166 NSString *fontFullName = [fontInfo objectAtIndex:0]; | |
| 167 NSInteger fontWeight = [[fontInfo objectAtIndex:2] intValue]; | 153 NSInteger fontWeight = [[fontInfo objectAtIndex:2] intValue]; |
| 168 fixUpWeight(fontWeight, fontFullName); | |
| 169 | 154 |
| 170 NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValu
e]; | 155 NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValu
e]; |
| 171 traitsMasks.append(toTraitsMask(fontTraits, fontWeight)); | 156 traitsMasks.append(toTraitsMask(fontTraits, fontWeight)); |
| 172 } | 157 } |
| 173 } | 158 } |
| 174 | 159 |
| 175 // Family name is somewhat of a misnomer here. We first attempt to find an exac
t match | 160 // Family name is somewhat of a misnomer here. We first attempt to find an exac
t match |
| 176 // comparing the desiredFamily to the PostScript name of the installed fonts. I
f that fails | 161 // comparing the desiredFamily to the PostScript name of the installed fonts. I
f that fails |
| 177 // we then do a search based on the family names of the installed fonts. | 162 // we then do a search based on the family names of the installed fonts. |
| 178 + (NSFont *)internalFontWithFamily:(NSString *)desiredFamily traits:(NSFontTrait
Mask)desiredTraits weight:(int)desiredWeight size:(float)size | 163 + (NSFont *)internalFontWithFamily:(NSString *)desiredFamily traits:(NSFontTrait
Mask)desiredTraits weight:(int)desiredWeight size:(float)size |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 | 207 |
| 223 NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
| 208 NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
|
| 224 unsigned n = [fonts count]; | 209 unsigned n = [fonts count]; |
| 225 unsigned i; | 210 unsigned i; |
| 226 for (i = 0; i < n; i++) { | 211 for (i = 0; i < n; i++) { |
| 227 NSArray *fontInfo = [fonts objectAtIndex:i]; | 212 NSArray *fontInfo = [fonts objectAtIndex:i]; |
| 228 | 213 |
| 229 // Array indices must be hard coded because of lame AppKit API. | 214 // Array indices must be hard coded because of lame AppKit API. |
| 230 NSString *fontFullName = [fontInfo objectAtIndex:0]; | 215 NSString *fontFullName = [fontInfo objectAtIndex:0]; |
| 231 NSInteger fontWeight = [[fontInfo objectAtIndex:2] intValue]; | 216 NSInteger fontWeight = [[fontInfo objectAtIndex:2] intValue]; |
| 232 fixUpWeight(fontWeight, fontFullName); | |
| 233 | 217 |
| 234 NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValu
e]; | 218 NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValu
e]; |
| 235 | 219 |
| 236 BOOL newWinner; | 220 BOOL newWinner; |
| 237 if (!choseFont) | 221 if (!choseFont) |
| 238 newWinner = acceptableChoice(desiredTraits, fontTraits); | 222 newWinner = acceptableChoice(desiredTraits, fontTraits); |
| 239 else | 223 else |
| 240 newWinner = betterChoice(desiredTraits, desiredWeight, chosenTraits,
chosenWeight, fontTraits, fontWeight); | 224 newWinner = betterChoice(desiredTraits, desiredWeight, chosenTraits,
chosenWeight, fontTraits, fontWeight); |
| 241 | 225 |
| 242 if (newWinner) { | 226 if (newWinner) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 return [self internalFontWithFamily:desiredFamily traits:desiredTraits weigh
t:desiredWeight size:size]; | 285 return [self internalFontWithFamily:desiredFamily traits:desiredTraits weigh
t:desiredWeight size:size]; |
| 302 } | 286 } |
| 303 | 287 |
| 304 + (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)des
iredTraits size:(float)size | 288 + (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)des
iredTraits size:(float)size |
| 305 { | 289 { |
| 306 int desiredWeight = (desiredTraits & NSBoldFontMask) ? 9 : 5; | 290 int desiredWeight = (desiredTraits & NSBoldFontMask) ? 9 : 5; |
| 307 return [self fontWithFamily:desiredFamily traits:desiredTraits weight:desire
dWeight size:size]; | 291 return [self fontWithFamily:desiredFamily traits:desiredTraits weight:desire
dWeight size:size]; |
| 308 } | 292 } |
| 309 | 293 |
| 310 @end | 294 @end |
| OLD | NEW |