2 * This file is part of the internal font implementation.
4 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
5 * Copyright (c) 2010 Google Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #import "platform/fonts/FontPlatformData.h"
27 #import <AppKit/NSFont.h>
28 #import <AvailabilityMacros.h>
29 #import <wtf/text/WTFString.h>
31 #import "platform/fonts/harfbuzz/HarfBuzzFace.h"
32 #include "third_party/skia/include/ports/SkTypeface_mac.h"
36 unsigned FontPlatformData::hash() const
38 ASSERT(m_font || !m_cgFont);
39 uintptr_t hashCodes[3] = { (uintptr_t)m_font, m_widthVariant, static_cast<uintptr_t>(m_isHashTableDeletedValue << 3 | m_orientation << 2 | m_syntheticBold << 1 | m_syntheticItalic) };
40 return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
43 // These CoreText Text Spacing feature selectors are not defined in CoreText.
44 enum TextSpacingCTFeatureSelector { TextSpacingProportional, TextSpacingFullWidth, TextSpacingHalfWidth, TextSpacingThirdWidth, TextSpacingQuarterWidth };
46 FontPlatformData::FontPlatformData(NSFont *nsFont, float size, bool syntheticBold, bool syntheticItalic, FontOrientation orientation, FontWidthVariant widthVariant)
48 , m_syntheticBold(syntheticBold)
49 , m_syntheticItalic(syntheticItalic)
50 , m_orientation(orientation)
51 , m_isColorBitmapFont(false)
52 , m_isCompositeFontReference(false)
53 , m_widthVariant(widthVariant)
55 , m_isHashTableDeletedValue(false)
57 ASSERT_ARG(nsFont, nsFont);
60 loadFont(nsFont, size, m_font, cgFont);
62 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
63 // FIXME: Chromium: The following code isn't correct for the Chromium port since the sandbox might
64 // have blocked font loading, in which case we'll only have the real loaded font file after the call to loadFont().
66 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(toCTFontRef(m_font));
67 m_isColorBitmapFont = traits & kCTFontColorGlyphsTrait;
68 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
69 m_isCompositeFontReference = traits & kCTFontCompositeTrait;
77 m_cgFont.adoptCF(cgFont);
80 void FontPlatformData::platformDataInit(const FontPlatformData& f)
82 m_font = f.m_font ? [f.m_font retain] : f.m_font;
84 m_cgFont = f.m_cgFont;
85 m_CTFont = f.m_CTFont;
87 m_inMemoryFont = f.m_inMemoryFont;
88 m_harfBuzzFace = f.m_harfBuzzFace;
89 m_typeface = f.m_typeface;
92 const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& f)
94 m_cgFont = f.m_cgFont;
95 if (m_font == f.m_font)
102 m_CTFont = f.m_CTFont;
104 m_inMemoryFont = f.m_inMemoryFont;
105 m_harfBuzzFace = f.m_harfBuzzFace;
106 m_typeface = f.m_typeface;
112 void FontPlatformData::setFont(NSFont *font)
114 ASSERT_ARG(font, font);
123 m_textSize = [font pointSize];
125 CGFontRef cgFont = 0;
126 NSFont* loadedFont = 0;
127 loadFont(m_font, m_textSize, loadedFont, cgFont);
129 // If loadFont replaced m_font with a fallback font, then release the
130 // previous font to counter the retain above. Then retain the new font.
131 if (loadedFont != m_font) {
133 CFRetain(loadedFont);
137 m_cgFont.adoptCF(cgFont);
138 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
140 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(toCTFontRef(m_font));
141 m_isColorBitmapFont = traits & kCTFontColorGlyphsTrait;
142 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
143 m_isCompositeFontReference = traits & kCTFontCompositeTrait;
150 bool FontPlatformData::roundsGlyphAdvances() const
152 return [m_font renderingMode] == NSFontAntialiasedIntegerAdvancementsRenderingMode;
155 bool FontPlatformData::allowsLigatures() const
157 return ![[m_font coveredCharacterSet] characterIsMember:'a'];
160 inline int mapFontWidthVariantToCTFeatureSelector(FontWidthVariant variant)
164 return TextSpacingProportional;
167 return TextSpacingHalfWidth;
170 return TextSpacingThirdWidth;
173 return TextSpacingQuarterWidth;
176 ASSERT_NOT_REACHED();
177 return TextSpacingProportional;
180 static CFDictionaryRef createFeatureSettingDictionary(int featureTypeIdentifier, int featureSelectorIdentifier)
182 RetainPtr<CFNumberRef> featureTypeIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeIdentifier));
183 RetainPtr<CFNumberRef> featureSelectorIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorIdentifier));
185 const void* settingKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
186 const void* settingValues[] = { featureTypeIdentifierNumber.get(), featureSelectorIdentifierNumber.get() };
188 return CFDictionaryCreate(kCFAllocatorDefault, settingKeys, settingValues, WTF_ARRAY_LENGTH(settingKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
191 static CTFontDescriptorRef cascadeToLastResortFontDescriptor()
193 static CTFontDescriptorRef descriptor;
197 const void* keys[] = { kCTFontCascadeListAttribute };
198 const void* descriptors[] = { CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0) };
199 const void* values[] = { CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks) };
200 RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
202 descriptor = CTFontDescriptorCreateWithAttributes(attributes.get());
207 static CTFontDescriptorRef cascadeToLastResortAndDisableSwashesFontDescriptor()
209 static CTFontDescriptorRef descriptor;
213 RetainPtr<CFDictionaryRef> lineInitialSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineInitialSwashesOffSelector));
214 RetainPtr<CFDictionaryRef> lineFinalSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineFinalSwashesOffSelector));
216 const void* settingDictionaries[] = { lineInitialSwashesOffSetting.get(), lineFinalSwashesOffSetting.get() };
217 RetainPtr<CFArrayRef> featureSettings(AdoptCF, CFArrayCreate(kCFAllocatorDefault, settingDictionaries, WTF_ARRAY_LENGTH(settingDictionaries), &kCFTypeArrayCallBacks));
219 const void* keys[] = { kCTFontFeatureSettingsAttribute };
220 const void* values[] = { featureSettings.get() };
221 RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
223 descriptor = CTFontDescriptorCreateCopyWithAttributes(cascadeToLastResortFontDescriptor(), attributes.get());
228 CTFontRef FontPlatformData::ctFont() const
231 return m_CTFont.get();
233 if (m_inMemoryFont) {
234 m_CTFont.adoptCF(CTFontCreateWithGraphicsFont(m_inMemoryFont->cgFont(), m_textSize, 0, cascadeToLastResortFontDescriptor()));
235 return m_CTFont.get();
238 m_CTFont = toCTFontRef(m_font);
240 CTFontDescriptorRef fontDescriptor;
241 RetainPtr<CFStringRef> postScriptName(AdoptCF, CTFontCopyPostScriptName(m_CTFont.get()));
242 // Hoefler Text Italic has line-initial and -final swashes enabled by default, so disable them.
243 if (CFEqual(postScriptName.get(), CFSTR("HoeflerText-Italic")) || CFEqual(postScriptName.get(), CFSTR("HoeflerText-BlackItalic")))
244 fontDescriptor = cascadeToLastResortAndDisableSwashesFontDescriptor();
246 fontDescriptor = cascadeToLastResortFontDescriptor();
247 m_CTFont.adoptCF(CTFontCreateCopyWithAttributes(m_CTFont.get(), m_textSize, 0, fontDescriptor));
249 m_CTFont.adoptCF(CTFontCreateWithGraphicsFont(m_cgFont.get(), m_textSize, 0, cascadeToLastResortFontDescriptor()));
251 if (m_widthVariant != RegularWidth) {
252 int featureTypeValue = kTextSpacingType;
253 int featureSelectorValue = mapFontWidthVariantToCTFeatureSelector(m_widthVariant);
254 RetainPtr<CTFontDescriptorRef> sourceDescriptor(AdoptCF, CTFontCopyFontDescriptor(m_CTFont.get()));
255 RetainPtr<CFNumberRef> featureType(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeValue));
256 RetainPtr<CFNumberRef> featureSelector(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorValue));
257 RetainPtr<CTFontDescriptorRef> newDescriptor(AdoptCF, CTFontDescriptorCreateCopyWithFeature(sourceDescriptor.get(), featureType.get(), featureSelector.get()));
258 RetainPtr<CTFontRef> newFont(AdoptCF, CTFontCreateWithFontDescriptor(newDescriptor.get(), m_textSize, 0));
264 return m_CTFont.get();
267 bool FontPlatformData::isAATFont(CTFontRef ctFont) const
269 CFDataRef table = CTFontCopyTable(ctFont, kCTFontTableMort, 0);
274 table = CTFontCopyTable(ctFont, kCTFontTableMorx, 0);