2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #import "SimpleFontData.h"
33 #import "BlockExceptions.h"
38 #import "FontDescription.h"
39 #import "SharedBuffer.h"
40 #import "WebCoreSystemInterface.h"
41 #import <ApplicationServices/ApplicationServices.h>
43 #import <unicode/uchar.h>
44 #import <wtf/Assertions.h>
45 #import <wtf/RetainPtr.h>
47 @interface NSFont (WebAppKitSecretAPI)
48 - (BOOL)_isFakeFixedPitch;
53 const float smallCapsFontSizeMultiplier = 0.7f;
54 const float contextDPI = 72.0f;
55 static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return x * (contextDPI / (contextDPI * unitsPerEm)); }
57 bool initFontData(SimpleFontData* fontData)
59 if (!fontData->m_font.m_cgFont)
63 if (ATSUCreateStyle(&fontStyle) != noErr)
66 ATSUFontID fontId = fontData->m_font.m_atsuFontID;
68 ATSUDisposeStyle(fontStyle);
72 ATSUAttributeTag tag = kATSUFontTag;
73 ByteCount size = sizeof(ATSUFontID);
74 ATSUFontID *valueArray[1] = {&fontId};
75 OSStatus status = ATSUSetAttributes(fontStyle, 1, &tag, &size, (void* const*)valueArray);
76 if (status != noErr) {
77 ATSUDisposeStyle(fontStyle);
81 if (wkGetATSStyleGroup(fontStyle, &fontData->m_styleGroup) != noErr) {
82 ATSUDisposeStyle(fontStyle);
86 ATSUDisposeStyle(fontStyle);
91 static NSString *webFallbackFontFamily(void)
93 static RetainPtr<NSString> webFallbackFontFamily = nil;
94 if (!webFallbackFontFamily)
95 webFallbackFontFamily = [[NSFont systemFontOfSize:16.0f] familyName];
96 return webFallbackFontFamily.get();
99 static NSString* pathFromFont(NSFont *font)
101 ATSFontRef atsFont = FMGetATSFontRefFromFont(wkGetNSFontATSUFontId(font));
104 #ifndef BUILDING_ON_TIGER
105 OSStatus status = ATSFontGetFileReference(atsFont, &fileRef);
110 OSStatus status = ATSFontGetFileSpecification(atsFont, &oFile);
114 status = FSpMakeFSRef(&oFile, &fileRef);
119 UInt8 filePathBuffer[PATH_MAX];
120 status = FSRefMakePath(&fileRef, filePathBuffer, PATH_MAX);
122 return [NSString stringWithUTF8String:(const char*)filePathBuffer];
128 void SimpleFontData::platformInit()
131 m_ATSUStyleInitialized = false;
132 m_ATSUMirrors = false;
133 m_checkedShapesArabic = false;
134 m_shapesArabic = false;
136 m_syntheticBoldOffset = m_font.m_syntheticBold ? 1.0f : 0.f;
138 bool failedSetup = false;
139 if (!initFontData(this)) {
140 // Ack! Something very bad happened, like a corrupt font.
141 // Try looking for an alternate 'base' font for this renderer.
143 // Special case hack to use "Times New Roman" in place of "Times".
144 // "Times RO" is a common font whose family name is "Times".
145 // It overrides the normal "Times" family font.
146 // It also appears to have a corrupt regular variant.
147 NSString *fallbackFontFamily;
148 if ([[m_font.font() familyName] isEqual:@"Times"])
149 fallbackFontFamily = @"Times New Roman";
151 fallbackFontFamily = webFallbackFontFamily();
153 // Try setting up the alternate font.
154 // This is a last ditch effort to use a substitute font when something has gone wrong.
156 RetainPtr<NSFont> initialFont = m_font.font();
158 m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:fallbackFontFamily]);
160 NSString *filePath = pathFromFont(initialFont.get());
162 filePath = @"not known";
164 if (!initFontData(this)) {
165 if ([fallbackFontFamily isEqual:@"Times New Roman"]) {
166 // OK, couldn't setup Times New Roman as an alternate to Times, fallback
167 // on the system font. If this fails we have no alternative left.
168 m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:webFallbackFontFamily()]);
169 if (!initFontData(this)) {
170 // We tried, Times, Times New Roman, and the system font. No joy. We have to give up.
171 LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath);
175 // We tried the requested font and the system font. No joy. We have to give up.
176 LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath);
181 // Report the problem.
182 LOG_ERROR("Corrupt font detected, using %@ in place of %@ located at \"%@\".",
183 [m_font.font() familyName], [initialFont.get() familyName], filePath);
186 // If all else fails, try to set up using the system font.
187 // This is probably because Times and Times New Roman are both unavailable.
189 m_font.setFont([NSFont systemFontOfSize:[m_font.font() pointSize]]);
190 LOG_ERROR("failed to set up font, using system font %s", m_font.font());
197 #ifdef BUILDING_ON_TIGER
198 wkGetFontMetrics(m_font.m_cgFont, &iAscent, &iDescent, &iLineGap, &m_unitsPerEm);
200 iAscent = CGFontGetAscent(m_font.m_cgFont);
201 iDescent = CGFontGetDescent(m_font.m_cgFont);
202 iLineGap = CGFontGetLeading(m_font.m_cgFont);
203 m_unitsPerEm = CGFontGetUnitsPerEm(m_font.m_cgFont);
206 float pointSize = m_font.m_size;
207 float fAscent = scaleEmToUnits(iAscent, m_unitsPerEm) * pointSize;
208 float fDescent = -scaleEmToUnits(iDescent, m_unitsPerEm) * pointSize;
209 float fLineGap = scaleEmToUnits(iLineGap, m_unitsPerEm) * pointSize;
211 // We need to adjust Times, Helvetica, and Courier to closely match the
212 // vertical metrics of their Microsoft counterparts that are the de facto
213 // web standard. The AppKit adjustment of 20% is too big and is
214 // incorrectly added to line spacing, so we use a 15% adjustment instead
215 // and add it to the ascent.
216 NSString *familyName = [m_font.font() familyName];
217 if ([familyName isEqualToString:@"Times"] || [familyName isEqualToString:@"Helvetica"] || [familyName isEqualToString:@"Courier"])
218 fAscent += floorf(((fAscent + fDescent) * 0.15f) + 0.5f);
220 m_ascent = lroundf(fAscent);
221 m_descent = lroundf(fDescent);
222 m_lineGap = lroundf(fLineGap);
223 m_lineSpacing = m_ascent + m_descent + m_lineGap;
225 // Hack Hiragino line metrics to allow room for marked text underlines.
226 // <rdar://problem/5386183>
227 if (m_descent < 3 && m_lineGap >= 3 && [familyName hasPrefix:@"Hiragino"]) {
228 m_lineGap -= 3 - m_descent;
232 // Measure the actual character "x", because AppKit synthesizes X height rather than getting it from the font.
233 // Unfortunately, NSFont will round this for us so we don't quite get the right value.
234 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
235 NSGlyph xGlyph = glyphPageZero ? glyphPageZero->glyphDataForCharacter('x').glyph : 0;
237 NSRect xBox = [m_font.font() boundingRectForGlyph:xGlyph];
238 // Use the maximum of either width or height because "x" is nearly square
239 // and web pages that foolishly use this metric for width will be laid out
240 // poorly if we return an accurate height. Classic case is Times 13 point,
241 // which has an "x" that is 7x6 pixels.
242 m_xHeight = MAX(NSMaxX(xBox), NSMaxY(xBox));
244 m_xHeight = [m_font.font() xHeight];
247 void SimpleFontData::platformDestroy()
250 wkReleaseStyleGroup(m_styleGroup);
252 if (m_ATSUStyleInitialized)
253 ATSUDisposeStyle(m_ATSUStyle);
256 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
258 if (!m_smallCapsFontData) {
259 if (isCustomFont()) {
260 FontPlatformData smallCapsFontData(m_font);
261 smallCapsFontData.m_size = smallCapsFontData.m_size * smallCapsFontSizeMultiplier;
262 m_smallCapsFontData = new SimpleFontData(smallCapsFontData, true, false);
264 BEGIN_BLOCK_OBJC_EXCEPTIONS;
265 float size = [m_font.font() pointSize] * smallCapsFontSizeMultiplier;
266 FontPlatformData smallCapsFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toSize:size]);
268 // AppKit resets the type information (screen/printer) when you convert a font to a different size.
269 // We have to fix up the font that we're handed back.
270 smallCapsFont.setFont(fontDescription.usePrinterFont() ? [smallCapsFont.font() printerFont] : [smallCapsFont.font() screenFont]);
272 if (smallCapsFont.font()) {
273 NSFontManager *fontManager = [NSFontManager sharedFontManager];
274 NSFontTraitMask fontTraits = [fontManager traitsOfFont:m_font.font()];
276 if (m_font.m_syntheticBold)
277 fontTraits |= NSBoldFontMask;
278 if (m_font.m_syntheticOblique)
279 fontTraits |= NSItalicFontMask;
281 NSFontTraitMask smallCapsFontTraits = [fontManager traitsOfFont:smallCapsFont.font()];
282 smallCapsFont.m_syntheticBold = (fontTraits & NSBoldFontMask) && !(smallCapsFontTraits & NSBoldFontMask);
283 smallCapsFont.m_syntheticOblique = (fontTraits & NSItalicFontMask) && !(smallCapsFontTraits & NSItalicFontMask);
285 m_smallCapsFontData = FontCache::getCachedFontData(&smallCapsFont);
287 END_BLOCK_OBJC_EXCEPTIONS;
290 return m_smallCapsFontData;
293 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
295 NSString *string = [[NSString alloc] initWithCharactersNoCopy:(UniChar*)characters length:length freeWhenDone:NO];
296 NSCharacterSet *set = [[m_font.font() coveredCharacterSet] invertedSet];
297 bool result = set && [string rangeOfCharacterFromSet:set].location == NSNotFound;
302 void SimpleFontData::determinePitch()
304 NSFont* f = m_font.font();
305 // Special case Osaka-Mono.
306 // According to <rdar://problem/3999467>, we should treat Osaka-Mono as fixed pitch.
307 // Note that the AppKit does not report Osaka-Mono as fixed pitch.
309 // Special case MS-PGothic.
310 // According to <rdar://problem/4032938>, we should not treat MS-PGothic as fixed pitch.
311 // Note that AppKit does report MS-PGothic as fixed pitch.
313 // Special case MonotypeCorsiva
314 // According to <rdar://problem/5454704>, we should not treat MonotypeCorsiva as fixed pitch.
315 // Note that AppKit does report MonotypeCorsiva as fixed pitch.
317 NSString *name = [f fontName];
318 m_treatAsFixedPitch = ([f isFixedPitch] || [f _isFakeFixedPitch] ||
319 [name caseInsensitiveCompare:@"Osaka-Mono"] == NSOrderedSame) &&
320 [name caseInsensitiveCompare:@"MS-PGothic"] != NSOrderedSame &&
321 [name caseInsensitiveCompare:@"MonotypeCorsiva"] != NSOrderedSame;
324 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
326 NSFont* font = m_font.font();
327 float pointSize = m_font.m_size;
328 CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
330 if (!wkGetGlyphTransformedAdvances(m_font.m_cgFont, font, &m, &glyph, &advance)) {
331 LOG_ERROR("Unable to cache glyph widths for %@ %f", [font displayName], pointSize);
334 return advance.width + m_syntheticBoldOffset;
337 void SimpleFontData::checkShapesArabic() const
339 ASSERT(!m_checkedShapesArabic);
341 m_checkedShapesArabic = true;
343 ATSUFontID fontID = m_font.m_atsuFontID;
345 LOG_ERROR("unable to get ATSUFontID for %@", m_font.font());
349 // This function is called only on fonts that contain Arabic glyphs. Our
350 // heuristic is that if such a font has a glyph metamorphosis table, then
351 // it includes shaping information for Arabic.
352 FourCharCode tables[] = { 'morx', 'mort' };
353 for (unsigned i = 0; i < sizeof(tables) / sizeof(tables[0]); ++i) {
355 OSStatus status = ATSFontGetTable(fontID, tables[i], 0, 0, 0, &tableSize);
356 if (status == noErr) {
357 m_shapesArabic = true;
361 if (status != kATSInvalidFontTableAccess)
362 LOG_ERROR("ATSFontGetTable failed (%d)", status);