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 void SimpleFontData::platformInit()
102 m_ATSUStyleInitialized = false;
103 m_ATSUMirrors = false;
104 m_checkedShapesArabic = false;
105 m_shapesArabic = false;
107 m_syntheticBoldOffset = m_font.m_syntheticBold ? 1.0f : 0.f;
109 bool failedSetup = false;
110 if (!initFontData(this)) {
111 // Ack! Something very bad happened, like a corrupt font.
112 // Try looking for an alternate 'base' font for this renderer.
114 // Special case hack to use "Times New Roman" in place of "Times".
115 // "Times RO" is a common font whose family name is "Times".
116 // It overrides the normal "Times" family font.
117 // It also appears to have a corrupt regular variant.
118 NSString *fallbackFontFamily;
119 if ([[m_font.font() familyName] isEqual:@"Times"])
120 fallbackFontFamily = @"Times New Roman";
122 fallbackFontFamily = webFallbackFontFamily();
124 // Try setting up the alternate font.
125 // This is a last ditch effort to use a substitute font when something has gone wrong.
127 RetainPtr<NSFont> initialFont = m_font.font();
129 m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:fallbackFontFamily]);
131 NSString *filePath = wkPathFromFont(initialFont.get());
133 filePath = @"not known";
135 if (!initFontData(this)) {
136 if ([fallbackFontFamily isEqual:@"Times New Roman"]) {
137 // OK, couldn't setup Times New Roman as an alternate to Times, fallback
138 // on the system font. If this fails we have no alternative left.
139 m_font.setFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toFamily:webFallbackFontFamily()]);
140 if (!initFontData(this)) {
141 // We tried, Times, Times New Roman, and the system font. No joy. We have to give up.
142 LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath);
146 // We tried the requested font and the system font. No joy. We have to give up.
147 LOG_ERROR("unable to initialize with font %@ at %@", initialFont.get(), filePath);
152 // Report the problem.
153 LOG_ERROR("Corrupt font detected, using %@ in place of %@ located at \"%@\".",
154 [m_font.font() familyName], [initialFont.get() familyName], filePath);
157 // If all else fails, try to set up using the system font.
158 // This is probably because Times and Times New Roman are both unavailable.
160 m_font.setFont([NSFont systemFontOfSize:[m_font.font() pointSize]]);
161 LOG_ERROR("failed to set up font, using system font %s", m_font.font());
168 #ifdef BUILDING_ON_TIGER
169 wkGetFontMetrics(m_font.m_cgFont, &iAscent, &iDescent, &iLineGap, &m_unitsPerEm);
171 iAscent = CGFontGetAscent(m_font.m_cgFont);
172 iDescent = CGFontGetDescent(m_font.m_cgFont);
173 iLineGap = CGFontGetLeading(m_font.m_cgFont);
174 m_unitsPerEm = CGFontGetUnitsPerEm(m_font.m_cgFont);
177 float pointSize = m_font.m_size;
178 float fAscent = scaleEmToUnits(iAscent, m_unitsPerEm) * pointSize;
179 float fDescent = -scaleEmToUnits(iDescent, m_unitsPerEm) * pointSize;
180 float fLineGap = scaleEmToUnits(iLineGap, m_unitsPerEm) * pointSize;
182 // We need to adjust Times, Helvetica, and Courier to closely match the
183 // vertical metrics of their Microsoft counterparts that are the de facto
184 // web standard. The AppKit adjustment of 20% is too big and is
185 // incorrectly added to line spacing, so we use a 15% adjustment instead
186 // and add it to the ascent.
187 NSString *familyName = [m_font.font() familyName];
188 if ([familyName isEqualToString:@"Times"] || [familyName isEqualToString:@"Helvetica"] || [familyName isEqualToString:@"Courier"])
189 fAscent += floorf(((fAscent + fDescent) * 0.15f) + 0.5f);
191 m_ascent = lroundf(fAscent);
192 m_descent = lroundf(fDescent);
193 m_lineGap = lroundf(fLineGap);
194 m_lineSpacing = m_ascent + m_descent + m_lineGap;
196 // Hack Hiragino line metrics to allow room for marked text underlines.
197 // <rdar://problem/5386183>
198 if (m_descent < 3 && m_lineGap >= 3 && [familyName hasPrefix:@"Hiragino"]) {
199 m_lineGap -= 3 - m_descent;
203 // Measure the actual character "x", because AppKit synthesizes X height rather than getting it from the font.
204 // Unfortunately, NSFont will round this for us so we don't quite get the right value.
205 GlyphPage* glyphPageZero = GlyphPageTreeNode::getRootChild(this, 0)->page();
206 NSGlyph xGlyph = glyphPageZero ? glyphPageZero->glyphDataForCharacter('x').glyph : 0;
208 NSRect xBox = [m_font.font() boundingRectForGlyph:xGlyph];
209 // Use the maximum of either width or height because "x" is nearly square
210 // and web pages that foolishly use this metric for width will be laid out
211 // poorly if we return an accurate height. Classic case is Times 13 point,
212 // which has an "x" that is 7x6 pixels.
213 m_xHeight = MAX(NSMaxX(xBox), NSMaxY(xBox));
215 m_xHeight = [m_font.font() xHeight];
218 void SimpleFontData::platformDestroy()
221 wkReleaseStyleGroup(m_styleGroup);
223 if (m_ATSUStyleInitialized)
224 ATSUDisposeStyle(m_ATSUStyle);
227 SimpleFontData* SimpleFontData::smallCapsFontData(const FontDescription& fontDescription) const
229 if (!m_smallCapsFontData) {
230 if (isCustomFont()) {
231 FontPlatformData smallCapsFontData(m_font);
232 smallCapsFontData.m_size = smallCapsFontData.m_size * smallCapsFontSizeMultiplier;
233 m_smallCapsFontData = new SimpleFontData(smallCapsFontData, true, false);
235 BEGIN_BLOCK_OBJC_EXCEPTIONS;
236 float size = [m_font.font() pointSize] * smallCapsFontSizeMultiplier;
237 FontPlatformData smallCapsFont([[NSFontManager sharedFontManager] convertFont:m_font.font() toSize:size]);
239 // AppKit resets the type information (screen/printer) when you convert a font to a different size.
240 // We have to fix up the font that we're handed back.
241 smallCapsFont.setFont(fontDescription.usePrinterFont() ? [smallCapsFont.font() printerFont] : [smallCapsFont.font() screenFont]);
243 if (smallCapsFont.font()) {
244 NSFontManager *fontManager = [NSFontManager sharedFontManager];
245 NSFontTraitMask fontTraits = [fontManager traitsOfFont:m_font.font()];
247 if (m_font.m_syntheticBold)
248 fontTraits |= NSBoldFontMask;
249 if (m_font.m_syntheticOblique)
250 fontTraits |= NSItalicFontMask;
252 NSFontTraitMask smallCapsFontTraits = [fontManager traitsOfFont:smallCapsFont.font()];
253 smallCapsFont.m_syntheticBold = (fontTraits & NSBoldFontMask) && !(smallCapsFontTraits & NSBoldFontMask);
254 smallCapsFont.m_syntheticOblique = (fontTraits & NSItalicFontMask) && !(smallCapsFontTraits & NSItalicFontMask);
256 m_smallCapsFontData = FontCache::getCachedFontData(&smallCapsFont);
258 END_BLOCK_OBJC_EXCEPTIONS;
261 return m_smallCapsFontData;
264 bool SimpleFontData::containsCharacters(const UChar* characters, int length) const
266 NSString *string = [[NSString alloc] initWithCharactersNoCopy:(UniChar*)characters length:length freeWhenDone:NO];
267 NSCharacterSet *set = [[m_font.font() coveredCharacterSet] invertedSet];
268 bool result = set && [string rangeOfCharacterFromSet:set].location == NSNotFound;
273 void SimpleFontData::determinePitch()
275 NSFont* f = m_font.font();
276 // Special case Osaka-Mono.
277 // According to <rdar://problem/3999467>, we should treat Osaka-Mono as fixed pitch.
278 // Note that the AppKit does not report Osaka-Mono as fixed pitch.
280 // Special case MS-PGothic.
281 // According to <rdar://problem/4032938>, we should not treat MS-PGothic as fixed pitch.
282 // Note that AppKit does report MS-PGothic as fixed pitch.
284 // Special case MonotypeCorsiva
285 // According to <rdar://problem/5454704>, we should not treat MonotypeCorsiva as fixed pitch.
286 // Note that AppKit does report MonotypeCorsiva as fixed pitch.
288 NSString *name = [f fontName];
289 m_treatAsFixedPitch = ([f isFixedPitch] || [f _isFakeFixedPitch] ||
290 [name caseInsensitiveCompare:@"Osaka-Mono"] == NSOrderedSame) &&
291 [name caseInsensitiveCompare:@"MS-PGothic"] != NSOrderedSame &&
292 [name caseInsensitiveCompare:@"MonotypeCorsiva"] != NSOrderedSame;
295 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
297 NSFont* font = m_font.font();
298 float pointSize = m_font.m_size;
299 CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
301 if (!wkGetGlyphTransformedAdvances(m_font.m_cgFont, font, &m, &glyph, &advance)) {
302 LOG_ERROR("Unable to cache glyph widths for %@ %f", [font displayName], pointSize);
305 return advance.width + m_syntheticBoldOffset;
308 void SimpleFontData::checkShapesArabic() const
310 ASSERT(!m_checkedShapesArabic);
312 m_checkedShapesArabic = true;
314 ATSUFontID fontID = m_font.m_atsuFontID;
316 LOG_ERROR("unable to get ATSUFontID for %@", m_font.font());
320 // This function is called only on fonts that contain Arabic glyphs. Our
321 // heuristic is that if such a font has a glyph metamorphosis table, then
322 // it includes shaping information for Arabic.
323 FourCharCode tables[] = { 'morx', 'mort' };
324 for (unsigned i = 0; i < sizeof(tables) / sizeof(tables[0]); ++i) {
326 OSStatus status = ATSFontGetTable(fontID, tables[i], 0, 0, 0, &tableSize);
327 if (status == noErr) {
328 m_shapesArabic = true;
332 if (status != kATSInvalidFontTableAccess)
333 LOG_ERROR("ATSFontGetTable failed (%d)", status);