diff options
author | Simon Hausmann <simon.hausmann@nokia.com> | 2012-01-06 14:44:00 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2012-01-06 14:44:00 +0100 |
commit | 40736c5763bf61337c8c14e16d8587db021a87d4 (patch) | |
tree | b17a9c00042ad89cb1308e2484491799aa14e9f8 /Source/WebCore/platform/graphics/cocoa | |
download | qtwebkit-40736c5763bf61337c8c14e16d8587db021a87d4.tar.gz |
Imported WebKit commit 2ea9d364d0f6efa8fa64acf19f451504c59be0e4 (http://svn.webkit.org/repository/webkit/trunk@104285)
Diffstat (limited to 'Source/WebCore/platform/graphics/cocoa')
-rw-r--r-- | Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm b/Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm new file mode 100644 index 000000000..7b1360883 --- /dev/null +++ b/Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm @@ -0,0 +1,288 @@ +/* + * This file is part of the internal font implementation. + * + * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. + * Copyright (c) 2010 Google Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#import "config.h" +#import "FontPlatformData.h" + +#import "PlatformString.h" +#import "WebCoreSystemInterface.h" +#import <AppKit/NSFont.h> + +namespace WebCore { + +// These CoreText Text Spacing feature selectors are not defined in CoreText. +enum TextSpacingCTFeatureSelector { TextSpacingProportional, TextSpacingFullWidth, TextSpacingHalfWidth, TextSpacingThirdWidth, TextSpacingQuarterWidth }; + +#if PLATFORM(MAC) +void FontPlatformData::loadFont(NSFont* nsFont, float, NSFont*& outNSFont, CGFontRef& cgFont) +{ + outNSFont = nsFont; + cgFont = CTFontCopyGraphicsFont(toCTFontRef(nsFont), 0); +} +#endif // PLATFORM(MAC) + +FontPlatformData::FontPlatformData(NSFont *nsFont, float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation, + TextOrientation textOrientation, FontWidthVariant widthVariant) + : m_syntheticBold(syntheticBold) + , m_syntheticOblique(syntheticOblique) + , m_orientation(orientation) + , m_textOrientation(textOrientation) + , m_size(size) + , m_widthVariant(widthVariant) + , m_font(nsFont) +#if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD) + // FIXME: Chromium: The following code isn't correct for the Chromium port since the sandbox might + // have blocked font loading, in which case we'll only have the real loaded font file after the call to loadFont(). + , m_isColorBitmapFont(CTFontGetSymbolicTraits(toCTFontRef(nsFont)) & kCTFontColorGlyphsTrait) +#else + , m_isColorBitmapFont(false) +#endif +{ + ASSERT_ARG(nsFont, nsFont); + + CGFontRef cgFont = 0; + loadFont(nsFont, size, m_font, cgFont); + + if (m_font) + CFRetain(m_font); + + m_cgFont.adoptCF(cgFont); +} + +FontPlatformData:: ~FontPlatformData() +{ + if (m_font && m_font != reinterpret_cast<NSFont *>(-1)) + CFRelease(m_font); +} + +void FontPlatformData::platformDataInit(const FontPlatformData& f) +{ + 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; + + m_cgFont = f.m_cgFont; + m_CTFont = f.m_CTFont; + +#if PLATFORM(CHROMIUM) && OS(DARWIN) + m_inMemoryFont = f.m_inMemoryFont; +#endif +} + +const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& f) +{ + m_cgFont = f.m_cgFont; + if (m_font == f.m_font) + return *this; + if (f.m_font && f.m_font != reinterpret_cast<NSFont *>(-1)) + CFRetain(f.m_font); + if (m_font && m_font != reinterpret_cast<NSFont *>(-1)) + CFRelease(m_font); + m_font = f.m_font; + m_CTFont = f.m_CTFont; +#if PLATFORM(CHROMIUM) && OS(DARWIN) + m_inMemoryFont = f.m_inMemoryFont; +#endif + return *this; +} + +bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const +{ + if (m_font || other.m_font) + return m_font == other.m_font; + return m_cgFont == other.m_cgFont; +} + +void FontPlatformData::setFont(NSFont *font) +{ + ASSERT_ARG(font, font); + ASSERT(m_font != reinterpret_cast<NSFont *>(-1)); + + if (m_font == font) + return; + + CFRetain(font); + if (m_font) + CFRelease(m_font); + m_font = font; + m_size = [font pointSize]; + + CGFontRef cgFont = 0; + NSFont* loadedFont = 0; + loadFont(m_font, m_size, loadedFont, cgFont); + +#if PLATFORM(CHROMIUM) && OS(DARWIN) + // If loadFont replaced m_font with a fallback font, then release the + // previous font to counter the retain above. Then retain the new font. + if (loadedFont != m_font) { + CFRelease(m_font); + CFRetain(loadedFont); + m_font = loadedFont; + } +#endif + + m_cgFont.adoptCF(cgFont); +#if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD) + m_isColorBitmapFont = CTFontGetSymbolicTraits(toCTFontRef(m_font)) & kCTFontColorGlyphsTrait; +#endif + m_CTFont = 0; +} + +bool FontPlatformData::roundsGlyphAdvances() const +{ + return [m_font renderingMode] == NSFontAntialiasedIntegerAdvancementsRenderingMode; +} + +bool FontPlatformData::allowsLigatures() const +{ + return ![[m_font coveredCharacterSet] characterIsMember:'a']; +} + +inline int mapFontWidthVariantToCTFeatureSelector(FontWidthVariant variant) +{ + switch(variant) { + case RegularWidth: + return TextSpacingProportional; + + case HalfWidth: + return TextSpacingHalfWidth; + + case ThirdWidth: + return TextSpacingThirdWidth; + + case QuarterWidth: + return TextSpacingQuarterWidth; + } + + ASSERT_NOT_REACHED(); + return TextSpacingProportional; +} + +static CFDictionaryRef createFeatureSettingDictionary(int featureTypeIdentifier, int featureSelectorIdentifier) +{ + RetainPtr<CFNumberRef> featureTypeIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeIdentifier)); + RetainPtr<CFNumberRef> featureSelectorIdentifierNumber(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorIdentifier)); + + const void* settingKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey }; + const void* settingValues[] = { featureTypeIdentifierNumber.get(), featureSelectorIdentifierNumber.get() }; + + return CFDictionaryCreate(kCFAllocatorDefault, settingKeys, settingValues, WTF_ARRAY_LENGTH(settingKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); +} + +static CTFontDescriptorRef cascadeToLastResortFontDescriptor() +{ + static CTFontDescriptorRef descriptor; + if (descriptor) + return descriptor; + + const void* keys[] = { kCTFontCascadeListAttribute }; + const void* descriptors[] = { CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0) }; + const void* values[] = { CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks) }; + RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); + + descriptor = CTFontDescriptorCreateWithAttributes(attributes.get()); + + return descriptor; +} + +static CTFontDescriptorRef cascadeToLastResortAndDisableSwashesFontDescriptor() +{ + static CTFontDescriptorRef descriptor; + if (descriptor) + return descriptor; + + RetainPtr<CFDictionaryRef> lineInitialSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineInitialSwashesOffSelector)); + RetainPtr<CFDictionaryRef> lineFinalSwashesOffSetting(AdoptCF, createFeatureSettingDictionary(kSmartSwashType, kLineFinalSwashesOffSelector)); + + const void* settingDictionaries[] = { lineInitialSwashesOffSetting.get(), lineFinalSwashesOffSetting.get() }; + RetainPtr<CFArrayRef> featureSettings(AdoptCF, CFArrayCreate(kCFAllocatorDefault, settingDictionaries, WTF_ARRAY_LENGTH(settingDictionaries), &kCFTypeArrayCallBacks)); + + const void* keys[] = { kCTFontFeatureSettingsAttribute }; + const void* values[] = { featureSettings.get() }; + RetainPtr<CFDictionaryRef> attributes(AdoptCF, CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); + + descriptor = CTFontDescriptorCreateCopyWithAttributes(cascadeToLastResortFontDescriptor(), attributes.get()); + + return descriptor; +} + +// Adding a cascade list breaks the font on Leopard +static bool canSetCascadeListForCustomFont() +{ +#if PLATFORM(CHROMIUM) + static SInt32 systemVersion; + if (!systemVersion) { + if (Gestalt(gestaltSystemVersion, &systemVersion) != noErr) + return false; + } + + return systemVersion >= 0x1060; +#elif !defined(BUILDING_ON_LEOPARD) + return true; +#else + return false; +#endif +} + +CTFontRef FontPlatformData::ctFont() const +{ + if (m_CTFont) + return m_CTFont.get(); + + m_CTFont = toCTFontRef(m_font); + if (m_CTFont) { + CTFontDescriptorRef fontDescriptor; + RetainPtr<CFStringRef> postScriptName(AdoptCF, CTFontCopyPostScriptName(m_CTFont.get())); + // Hoefler Text Italic has line-initial and -final swashes enabled by default, so disable them. + if (CFEqual(postScriptName.get(), CFSTR("HoeflerText-Italic")) || CFEqual(postScriptName.get(), CFSTR("HoeflerText-BlackItalic"))) + fontDescriptor = cascadeToLastResortAndDisableSwashesFontDescriptor(); + else + fontDescriptor = cascadeToLastResortFontDescriptor(); + m_CTFont.adoptCF(CTFontCreateCopyWithAttributes(m_CTFont.get(), m_size, 0, fontDescriptor)); + } else + m_CTFont.adoptCF(CTFontCreateWithGraphicsFont(m_cgFont.get(), m_size, 0, canSetCascadeListForCustomFont() ? cascadeToLastResortFontDescriptor() : 0)); + + if (m_widthVariant != RegularWidth) { + int featureTypeValue = kTextSpacingType; + int featureSelectorValue = mapFontWidthVariantToCTFeatureSelector(m_widthVariant); + RetainPtr<CTFontDescriptorRef> sourceDescriptor(AdoptCF, CTFontCopyFontDescriptor(m_CTFont.get())); + RetainPtr<CFNumberRef> featureType(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureTypeValue)); + RetainPtr<CFNumberRef> featureSelector(AdoptCF, CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &featureSelectorValue)); + RetainPtr<CTFontDescriptorRef> newDescriptor(AdoptCF, CTFontDescriptorCreateCopyWithFeature(sourceDescriptor.get(), featureType.get(), featureSelector.get())); + RetainPtr<CTFontRef> newFont(AdoptCF, CTFontCreateWithFontDescriptor(newDescriptor.get(), m_size, 0)); + + if (newFont) + m_CTFont = newFont; + } + + return m_CTFont.get(); +} + +#ifndef NDEBUG +String FontPlatformData::description() const +{ + RetainPtr<CFStringRef> cgFontDescription(AdoptCF, CFCopyDescription(cgFont())); + return String(cgFontDescription.get()) + " " + String::number(m_size) + + (m_syntheticBold ? " synthetic bold" : "") + (m_syntheticOblique ? " synthetic oblique" : "") + (m_orientation ? " vertical orientation" : ""); +} +#endif + +} // namespace WebCore |