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 "FontPlatformData.h"
27 #import "PlatformString.h"
28 #import "WebCoreSystemInterface.h"
29 #import <AppKit/NSFont.h>
33 // These CoreText Text Spacing feature selectors are not defined in CoreText.
34 enum TextSpacingCTFeatureSelector { TextSpacingProportional, TextSpacingFullWidth, TextSpacingHalfWidth, TextSpacingThirdWidth, TextSpacingQuarterWidth };
37 void FontPlatformData::loadFont(NSFont* nsFont, float, NSFont*& outNSFont, CGFontRef& cgFont)
40 cgFont = CTFontCopyGraphicsFont(toCTFontRef(nsFont), 0);
42 #endif // PLATFORM(MAC)
44 FontPlatformData::FontPlatformData(NSFont *nsFont, float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation,
45 TextOrientation textOrientation, FontWidthVariant widthVariant)
46 : m_syntheticBold(syntheticBold)
47 , m_syntheticOblique(syntheticOblique)
48 , m_orientation(orientation)
49 , m_textOrientation(textOrientation)
51 , m_widthVariant(widthVariant)
53 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
54 // FIXME: Chromium: The following code isn't correct for the Chromium port since the sandbox might
55 // have blocked font loading, in which case we'll only have the real loaded font file after the call to loadFont().
56 , m_isColorBitmapFont(CTFontGetSymbolicTraits(toCTFontRef(nsFont)) & kCTFontColorGlyphsTrait)
58 , m_isColorBitmapFont(false)
61 ASSERT_ARG(nsFont, nsFont);
64 loadFont(nsFont, size, m_font, cgFont);
69 m_cgFont.adoptCF(cgFont);
72 FontPlatformData:: ~FontPlatformData()
74 if (m_font && m_font != reinterpret_cast<NSFont *>(-1))
78 void FontPlatformData::platformDataInit(const FontPlatformData& f)
80 m_font = f.m_font && f.m_font != reinterpret_cast<NSFont *>(-1) ? const_cast<NSFont *>(static_cast<const NSFont *>(CFRetain(f.m_font))) : f.m_font;
82 m_cgFont = f.m_cgFont;
83 m_CTFont = f.m_CTFont;
85 #if PLATFORM(CHROMIUM) && OS(DARWIN)
86 m_inMemoryFont = f.m_inMemoryFont;
90 const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& f)
92 m_cgFont = f.m_cgFont;
93 if (m_font == f.m_font)
95 if (f.m_font && f.m_font != reinterpret_cast<NSFont *>(-1))
97 if (m_font && m_font != reinterpret_cast<NSFont *>(-1))
100 m_CTFont = f.m_CTFont;
101 #if PLATFORM(CHROMIUM) && OS(DARWIN)
102 m_inMemoryFont = f.m_inMemoryFont;
107 bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const
109 if (m_font || other.m_font)
110 return m_font == other.m_font;
111 return m_cgFont == other.m_cgFont;
114 void FontPlatformData::setFont(NSFont *font)
116 ASSERT_ARG(font, font);
117 ASSERT(m_font != reinterpret_cast<NSFont *>(-1));
126 m_size = [font pointSize];
128 CGFontRef cgFont = 0;
129 NSFont* loadedFont = 0;
130 loadFont(m_font, m_size, loadedFont, cgFont);
132 #if PLATFORM(CHROMIUM) && OS(DARWIN)
133 // If loadFont replaced m_font with a fallback font, then release the
134 // previous font to counter the retain above. Then retain the new font.
135 if (loadedFont != m_font) {
137 CFRetain(loadedFont);
142 m_cgFont.adoptCF(cgFont);
143 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
144 m_isColorBitmapFont = CTFontGetSymbolicTraits(toCTFontRef(m_font)) & kCTFontColorGlyphsTrait;
149 bool FontPlatformData::roundsGlyphAdvances() const
151 return [m_font renderingMode] == NSFontAntialiasedIntegerAdvancementsRenderingMode;
154 bool FontPlatformData::allowsLigatures() const
156 return ![[m_font coveredCharacterSet] characterIsMember:'a'];
159 inline int mapFontWidthVariantToCTFeatureSelector(FontWidthVariant variant)
163 return TextSpacingProportional;
166 return TextSpacingHalfWidth;
169 return TextSpacingThirdWidth;
172 return TextSpacingQuarterWidth;
175 ASSERT_NOT_REACHED();
176 return TextSpacingProportional;
179 static CFDictionaryRef createFeatureSettingDictionary(int featureTypeIdentifier, int featureSelectorIdentifier)
181 RetainPtr<CFNumberRef> featureTypeIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeIdentifier));
182 RetainPtr<CFNumberRef> featureSelectorIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorIdentifier));
184 const void* settingKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
185 const void* settingValues[] = { featureTypeIdentifierNumber.get(), featureSelectorIdentifierNumber.get() };
187 return CFDictionaryCreate(kCFAllocatorDefault, settingKeys, settingValues, WTF_ARRAY_LENGTH(settingKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
190 static CTFontDescriptorRef cascadeToLastResortFontDescriptor()
192 static CTFontDescriptorRef descriptor;
196 const void* keys[] = { kCTFontCascadeListAttribute };
197 const void* descriptors[] = { CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0) };
198 const void* values[] = { CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks) };
199 RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
201 descriptor = CTFontDescriptorCreateWithAttributes(attributes.get());
206 static CTFontDescriptorRef cascadeToLastResortAndDisableSwashesFontDescriptor()
208 static CTFontDescriptorRef descriptor;
212 RetainPtr<CFDictionaryRef> lineInitialSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineInitialSwashesOffSelector));
213 RetainPtr<CFDictionaryRef> lineFinalSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineFinalSwashesOffSelector));
215 const void* settingDictionaries[] = { lineInitialSwashesOffSetting.get(), lineFinalSwashesOffSetting.get() };
216 RetainPtr<CFArrayRef> featureSettings(AdoptCF, CFArrayCreate(kCFAllocatorDefault, settingDictionaries, WTF_ARRAY_LENGTH(settingDictionaries), &kCFTypeArrayCallBacks));
218 const void* keys[] = { kCTFontFeatureSettingsAttribute };
219 const void* values[] = { featureSettings.get() };
220 RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
222 descriptor = CTFontDescriptorCreateCopyWithAttributes(cascadeToLastResortFontDescriptor(), attributes.get());
227 // Adding a cascade list breaks the font on Leopard
228 static bool canSetCascadeListForCustomFont()
230 #if PLATFORM(CHROMIUM)
231 static SInt32 systemVersion;
232 if (!systemVersion) {
233 if (Gestalt(gestaltSystemVersion, &systemVersion) != noErr)
237 return systemVersion >= 0x1060;
238 #elif !defined(BUILDING_ON_LEOPARD)
245 CTFontRef FontPlatformData::ctFont() const
248 return m_CTFont.get();
250 m_CTFont = toCTFontRef(m_font);
252 CTFontDescriptorRef fontDescriptor;
253 RetainPtr<CFStringRef> postScriptName(AdoptCF, CTFontCopyPostScriptName(m_CTFont.get()));
254 // Hoefler Text Italic has line-initial and -final swashes enabled by default, so disable them.
255 if (CFEqual(postScriptName.get(), CFSTR("HoeflerText-Italic")) || CFEqual(postScriptName.get(), CFSTR("HoeflerText-BlackItalic")))
256 fontDescriptor = cascadeToLastResortAndDisableSwashesFontDescriptor();
258 fontDescriptor = cascadeToLastResortFontDescriptor();
259 m_CTFont.adoptCF(CTFontCreateCopyWithAttributes(m_CTFont.get(), m_size, 0, fontDescriptor));
261 m_CTFont.adoptCF(CTFontCreateWithGraphicsFont(m_cgFont.get(), m_size, 0, canSetCascadeListForCustomFont() ? cascadeToLastResortFontDescriptor() : 0));
263 if (m_widthVariant != RegularWidth) {
264 int featureTypeValue = kTextSpacingType;
265 int featureSelectorValue = mapFontWidthVariantToCTFeatureSelector(m_widthVariant);
266 RetainPtr<CTFontDescriptorRef> sourceDescriptor(AdoptCF, CTFontCopyFontDescriptor(m_CTFont.get()));
267 RetainPtr<CFNumberRef> featureType(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeValue));
268 RetainPtr<CFNumberRef> featureSelector(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorValue));
269 RetainPtr<CTFontDescriptorRef> newDescriptor(AdoptCF, CTFontDescriptorCreateCopyWithFeature(sourceDescriptor.get(), featureType.get(), featureSelector.get()));
270 RetainPtr<CTFontRef> newFont(AdoptCF, CTFontCreateWithFontDescriptor(newDescriptor.get(), m_size, 0));
276 return m_CTFont.get();
280 String FontPlatformData::description() const
282 RetainPtr<CFStringRef> cgFontDescription(AdoptCF, CFCopyDescription(cgFont()));
283 return String(cgFontDescription.get()) + " " + String::number(m_size)
284 + (m_syntheticBold ? " synthetic bold" : "") + (m_syntheticOblique ? " synthetic oblique" : "") + (m_orientation ? " vertical orientation" : "");
288 } // namespace WebCore