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 "SharedBuffer.h"
28 #import "WebCoreSystemInterface.h"
29 #import <wtf/text/WTFString.h>
32 #import <AppKit/NSFont.h>
34 #import "CoreGraphicsSPI.h"
35 #import <CoreText/CoreText.h>
40 // These CoreText Text Spacing feature selectors are not defined in CoreText.
41 enum TextSpacingCTFeatureSelector { TextSpacingProportional, TextSpacingFullWidth, TextSpacingHalfWidth, TextSpacingThirdWidth, TextSpacingQuarterWidth };
43 FontPlatformData::FontPlatformData(CTFontRef font, float size, bool isPrinterFont, bool syntheticBold, bool syntheticOblique, FontOrientation orientation, FontWidthVariant widthVariant)
44 : m_syntheticBold(syntheticBold)
45 , m_syntheticOblique(syntheticOblique)
46 , m_orientation(orientation)
48 , m_widthVariant(widthVariant)
50 , m_cgFont(adoptCF(CTFontCopyGraphicsFont(font, NULL)))
51 , m_isColorBitmapFont(CTFontGetSymbolicTraits(font) & kCTFontTraitColorGlyphs)
52 , m_isCompositeFontReference(CTFontGetSymbolicTraits(font) & kCTFontCompositeTrait)
53 , m_isPrinterFont(isPrinterFont)
55 ASSERT_ARG(font, font);
60 // FIXME: Remove this when all NSFont usage is removed.
61 FontPlatformData::FontPlatformData(NSFont *font, float size, bool isPrinterFont, bool syntheticBold, bool syntheticOblique, FontOrientation orientation, FontWidthVariant widthVariant)
62 : FontPlatformData((CTFontRef)font, size, isPrinterFont, syntheticBold, syntheticOblique, orientation, widthVariant)
67 FontPlatformData::~FontPlatformData()
69 if (m_font && m_font != reinterpret_cast<CTFontRef>(-1))
73 void FontPlatformData::platformDataInit(const FontPlatformData& f)
75 m_font = f.m_font && f.m_font != reinterpret_cast<CTFontRef>(-1) ? static_cast<CTFontRef>(const_cast<void *>(CFRetain(f.m_font))) : f.m_font;
78 m_isEmoji = f.m_isEmoji;
80 m_cgFont = f.m_cgFont;
81 m_ctFont = f.m_ctFont;
82 m_isPrinterFont = f.m_isPrinterFont;
85 const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& f)
87 m_cgFont = f.m_cgFont;
89 m_isEmoji = f.m_isEmoji;
91 if (m_font && m_font != reinterpret_cast<CTFontRef>(-1) && f.m_font && f.m_font != reinterpret_cast<CTFontRef>(-1) && CFEqual(m_font, f.m_font))
93 if (f.m_font && f.m_font != reinterpret_cast<CTFontRef>(-1))
95 if (m_font && m_font != reinterpret_cast<CTFontRef>(-1))
98 m_ctFont = f.m_ctFont;
99 m_isPrinterFont = f.m_isPrinterFont;
104 bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const
107 if (m_font || other.m_font) {
109 result = m_font && m_font != reinterpret_cast<CTFontRef>(-1) && other.m_font && other.m_font != reinterpret_cast<CTFontRef>(-1) && CFEqual(m_font, other.m_font);
112 ASSERT(m_isEmoji == other.m_isEmoji);
115 result = m_font == other.m_font;
116 #endif // PLATFORM(IOS)
121 if (m_cgFont == other.m_cgFont)
122 ASSERT(m_isEmoji == other.m_isEmoji);
124 #endif // PLATFORM(IOS)
125 return m_cgFont == other.m_cgFont;
128 void FontPlatformData::setFont(CTFontRef font)
130 ASSERT_ARG(font, font);
131 ASSERT(m_font != reinterpret_cast<CTFontRef>(-1));
140 m_size = CTFontGetSize(font);
141 m_cgFont = adoptCF(CTFontCopyGraphicsFont(font, nullptr));
143 CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(m_font);
144 m_isColorBitmapFont = traits & kCTFontTraitColorGlyphs;
145 m_isCompositeFontReference = traits & kCTFontCompositeTrait;
150 bool FontPlatformData::roundsGlyphAdvances() const
153 return [(NSFont *)m_font renderingMode] == NSFontAntialiasedIntegerAdvancementsRenderingMode;
160 bool FontPlatformData::allowsLigatures() const
163 return ![[(NSFont *)m_font coveredCharacterSet] characterIsMember:'a'];
168 RetainPtr<CFCharacterSetRef> characterSet = adoptCF(CTFontCopyCharacterSet(ctFont()));
169 return !(characterSet.get() && CFCharacterSetIsCharacterMember(characterSet.get(), 'a'));
173 inline int mapFontWidthVariantToCTFeatureSelector(FontWidthVariant variant)
177 return TextSpacingProportional;
180 return TextSpacingHalfWidth;
183 return TextSpacingThirdWidth;
186 return TextSpacingQuarterWidth;
189 ASSERT_NOT_REACHED();
190 return TextSpacingProportional;
193 static CFDictionaryRef createFeatureSettingDictionary(int featureTypeIdentifier, int featureSelectorIdentifier)
195 RetainPtr<CFNumberRef> featureTypeIdentifierNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeIdentifier));
196 RetainPtr<CFNumberRef> featureSelectorIdentifierNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorIdentifier));
198 const void* settingKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
199 const void* settingValues[] = { featureTypeIdentifierNumber.get(), featureSelectorIdentifierNumber.get() };
201 return CFDictionaryCreate(kCFAllocatorDefault, settingKeys, settingValues, WTF_ARRAY_LENGTH(settingKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
204 static CTFontDescriptorRef cascadeToLastResortFontDescriptor()
206 static CTFontDescriptorRef descriptor;
210 RetainPtr<CTFontDescriptorRef> lastResort = adoptCF(CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0));
212 const void* descriptors[] = { lastResort.get() };
213 RetainPtr<CFArrayRef> array = adoptCF(CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks));
215 const void* keys[] = { kCTFontCascadeListAttribute };
216 const void* values[] = { array.get() };
217 RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
219 descriptor = CTFontDescriptorCreateWithAttributes(attributes.get());
224 static CTFontDescriptorRef cascadeToLastResortAndDisableSwashesFontDescriptor()
226 static CTFontDescriptorRef descriptor;
230 RetainPtr<CFDictionaryRef> lineInitialSwashesOffSetting = adoptCF(createFeatureSettingDictionary(kSmartSwashType, kLineInitialSwashesOffSelector));
231 RetainPtr<CFDictionaryRef> lineFinalSwashesOffSetting = adoptCF(createFeatureSettingDictionary(kSmartSwashType, kLineFinalSwashesOffSelector));
233 const void* settingDictionaries[] = { lineInitialSwashesOffSetting.get(), lineFinalSwashesOffSetting.get() };
234 RetainPtr<CFArrayRef> featureSettings = adoptCF(CFArrayCreate(kCFAllocatorDefault, settingDictionaries, WTF_ARRAY_LENGTH(settingDictionaries), &kCFTypeArrayCallBacks));
236 const void* keys[] = { kCTFontFeatureSettingsAttribute };
237 const void* values[] = { featureSettings.get() };
238 RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
240 descriptor = CTFontDescriptorCreateCopyWithAttributes(cascadeToLastResortFontDescriptor(), attributes.get());
245 CTFontRef FontPlatformData::ctFont() const
248 return m_ctFont.get();
250 ASSERT(m_cgFont.get());
254 CTFontDescriptorRef fontDescriptor;
255 RetainPtr<CFStringRef> postScriptName = adoptCF(CTFontCopyPostScriptName(m_ctFont.get()));
256 // Hoefler Text Italic has line-initial and -final swashes enabled by default, so disable them.
257 if (CFEqual(postScriptName.get(), CFSTR("HoeflerText-Italic")) || CFEqual(postScriptName.get(), CFSTR("HoeflerText-BlackItalic")))
258 fontDescriptor = cascadeToLastResortAndDisableSwashesFontDescriptor();
260 fontDescriptor = cascadeToLastResortFontDescriptor();
261 m_ctFont = adoptCF(CTFontCreateCopyWithAttributes(m_ctFont.get(), m_size, 0, fontDescriptor));
263 m_ctFont = adoptCF(CTFontCreateWithGraphicsFont(m_cgFont.get(), m_size, 0, cascadeToLastResortFontDescriptor()));
265 // Apple Color Emoji size is adjusted (and then re-adjusted by Core Text) and capped.
266 CGFloat size = !m_isEmoji ? m_size : m_size <= 15 ? 4 * (m_size + 2) / static_cast<CGFloat>(5) : 16;
267 CTFontDescriptorRef fontDescriptor;
268 const char* postScriptName = CGFontGetPostScriptName(m_cgFont.get());
269 if (postScriptName && (!strcmp(postScriptName, "HoeflerText-Italic") || !strcmp(postScriptName, "HoeflerText-BlackItalic")))
270 fontDescriptor = cascadeToLastResortAndDisableSwashesFontDescriptor();
272 fontDescriptor = cascadeToLastResortFontDescriptor();
273 m_ctFont = adoptCF(CTFontCreateWithGraphicsFont(m_cgFont.get(), size, 0, fontDescriptor));
274 #endif // !PLATFORM(IOS)
276 if (m_widthVariant != RegularWidth) {
277 int featureTypeValue = kTextSpacingType;
278 int featureSelectorValue = mapFontWidthVariantToCTFeatureSelector(m_widthVariant);
279 RetainPtr<CTFontDescriptorRef> sourceDescriptor = adoptCF(CTFontCopyFontDescriptor(m_ctFont.get()));
280 RetainPtr<CFNumberRef> featureType = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeValue));
281 RetainPtr<CFNumberRef> featureSelector = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorValue));
282 RetainPtr<CTFontDescriptorRef> newDescriptor = adoptCF(CTFontDescriptorCreateCopyWithFeature(sourceDescriptor.get(), featureType.get(), featureSelector.get()));
283 RetainPtr<CTFontRef> newFont = adoptCF(CTFontCreateWithFontDescriptor(newDescriptor.get(), m_size, 0));
289 return m_ctFont.get();
292 PassRefPtr<SharedBuffer> FontPlatformData::openTypeTable(uint32_t table) const
294 if (RetainPtr<CFDataRef> data = adoptCF(CGFontCopyTableForTag(cgFont(), table)))
295 return SharedBuffer::wrapCFData(data.get());
301 String FontPlatformData::description() const
303 RetainPtr<CFStringRef> cgFontDescription = adoptCF(CFCopyDescription(cgFont()));
304 return String(cgFontDescription.get()) + " " + String::number(m_size)
305 + (m_syntheticBold ? " synthetic bold" : "") + (m_syntheticOblique ? " synthetic oblique" : "") + (m_orientation ? " vertical orientation" : "");
309 } // namespace WebCore