Title: [188795] trunk/Source/WebCore
Revision
188795
Author
mmaxfi...@apple.com
Date
2015-08-21 16:28:08 -0700 (Fri, 21 Aug 2015)

Log Message

[Cocoa] Unify two more FontCache functions
https://bugs.webkit.org/show_bug.cgi?id=148217

Reviewed by Dean Jackson.

Move duplicated code from FontCacheMac and FontCacheIOS into
FontCacheCoreText.

No new tests because there is no behavior change.

* platform/graphics/cocoa/FontCacheCoreText.cpp:
(WebCore::toTraitsMask):
(WebCore::isFontWeightBold):
(WebCore::FontCache::similarFont):
(WebCore::FontCache::getTraitsInFamily):
* platform/graphics/ios/FontCacheIOS.mm:
(WebCore::FontCache::similarFont): Deleted.
(WebCore::toTraitsMask): Deleted.
(WebCore::FontCache::getTraitsInFamily): Deleted.
* platform/graphics/mac/FontCacheMac.mm:
(WebCore::toTraitsMask): Deleted.
(WebCore::FontCache::similarFont): Deleted.
(WebCore::FontCache::getTraitsInFamily): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (188794 => 188795)


--- trunk/Source/WebCore/ChangeLog	2015-08-21 23:16:48 UTC (rev 188794)
+++ trunk/Source/WebCore/ChangeLog	2015-08-21 23:28:08 UTC (rev 188795)
@@ -1,3 +1,29 @@
+2015-08-21  Myles C. Maxfield  <mmaxfi...@apple.com>
+
+        [Cocoa] Unify two more FontCache functions
+        https://bugs.webkit.org/show_bug.cgi?id=148217
+
+        Reviewed by Dean Jackson.
+
+        Move duplicated code from FontCacheMac and FontCacheIOS into
+        FontCacheCoreText.
+
+        No new tests because there is no behavior change.
+
+        * platform/graphics/cocoa/FontCacheCoreText.cpp:
+        (WebCore::toTraitsMask):
+        (WebCore::isFontWeightBold):
+        (WebCore::FontCache::similarFont):
+        (WebCore::FontCache::getTraitsInFamily):
+        * platform/graphics/ios/FontCacheIOS.mm:
+        (WebCore::FontCache::similarFont): Deleted.
+        (WebCore::toTraitsMask): Deleted.
+        (WebCore::FontCache::getTraitsInFamily): Deleted.
+        * platform/graphics/mac/FontCacheMac.mm:
+        (WebCore::toTraitsMask): Deleted.
+        (WebCore::FontCache::similarFont): Deleted.
+        (WebCore::FontCache::getTraitsInFamily): Deleted.
+
 2015-08-21  Anders Carlsson  <ander...@apple.com>
 
         Stop using the old callOnMainThread variant on Windows

Modified: trunk/Source/WebCore/platform/graphics/FontCache.h (188794 => 188795)


--- trunk/Source/WebCore/platform/graphics/FontCache.h	2015-08-21 23:16:48 UTC (rev 188794)
+++ trunk/Source/WebCore/platform/graphics/FontCache.h	2015-08-21 23:28:08 UTC (rev 188795)
@@ -164,6 +164,8 @@
     static IMultiLanguage* getMultiLanguageInterface();
 #endif
 
+    // This function exists so CSSFontSelector can have a unified notion of preinstalled fonts and @font-face.
+    // It comes into play when you create an @font-face which shares a family name as a preinstalled font.
     void getTraitsInFamily(const AtomicString&, Vector<unsigned>&);
 
     WEBCORE_EXPORT RefPtr<Font> fontForFamily(const FontDescription&, const AtomicString&, bool checkingAlternateName = false);

Modified: trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp (188794 => 188795)


--- trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp	2015-08-21 23:16:48 UTC (rev 188794)
+++ trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp	2015-08-21 23:28:08 UTC (rev 188795)
@@ -27,6 +27,8 @@
 #include "FontCache.h"
 
 #include "CoreTextSPI.h"
+#include "Font.h"
+
 #include <CoreText/SFNTLayoutTypes.h>
 
 #include <wtf/NeverDestroyed.h>
@@ -161,4 +163,106 @@
     return adoptCF(CTFontCreateCopyWithAttributes(originalFont, CTFontGetSize(originalFont), nullptr, descriptor.get()));
 }
 
+static inline FontTraitsMask toTraitsMask(CTFontSymbolicTraits ctFontTraits, CGFloat weight)
+{
+    FontTraitsMask weightMask = FontWeight100Mask;
+    if (weight < -0.6)
+        weightMask = FontWeight100Mask;
+    else if (weight < -0.365)
+        weightMask = FontWeight200Mask;
+    else if (weight < -0.115)
+        weightMask = FontWeight300Mask;
+    else if (weight <  0.130)
+        weightMask = FontWeight400Mask;
+    else if (weight <  0.235)
+        weightMask = FontWeight500Mask;
+    else if (weight <  0.350)
+        weightMask = FontWeight600Mask;
+    else if (weight <  0.500)
+        weightMask = FontWeight700Mask;
+    else if (weight <  0.700)
+        weightMask = FontWeight800Mask;
+    else
+        weightMask = FontWeight900Mask;
+    return static_cast<FontTraitsMask>(((ctFontTraits & kCTFontTraitItalic) ? FontStyleItalicMask : FontStyleNormalMask)
+        | FontVariantNormalMask | weightMask);
 }
+
+static inline bool isFontWeightBold(FontWeight fontWeight)
+{
+    return fontWeight >= FontWeight600;
+}
+
+RefPtr<Font> FontCache::similarFont(const FontDescription& description)
+{
+    // Attempt to find an appropriate font using a match based on the presence of keywords in
+    // the requested names. For example, we'll match any name that contains "Arabic" to Geeza Pro.
+    RefPtr<Font> font;
+    for (unsigned i = 0; i < description.familyCount(); ++i) {
+        const AtomicString& family = description.familyAt(i);
+        if (family.isEmpty())
+            continue;
+
+#if PLATFORM(IOS)
+        // Substitute the default monospace font for well-known monospace fonts.
+        static NeverDestroyed<AtomicString> monaco("monaco", AtomicString::ConstructFromLiteral);
+        static NeverDestroyed<AtomicString> menlo("menlo", AtomicString::ConstructFromLiteral);
+        static NeverDestroyed<AtomicString> courier("courier", AtomicString::ConstructFromLiteral);
+        if (equalIgnoringCase(family, monaco) || equalIgnoringCase(family, menlo)) {
+            font = fontForFamily(description, courier);
+            continue;
+        }
+
+        // Substitute Verdana for Lucida Grande.
+        static NeverDestroyed<AtomicString> lucidaGrande("lucida grande", AtomicString::ConstructFromLiteral);
+        static NeverDestroyed<AtomicString> verdana("verdana", AtomicString::ConstructFromLiteral);
+        if (equalIgnoringCase(family, lucidaGrande)) {
+            font = fontForFamily(description, verdana);
+            continue;
+        }
+#endif
+
+        static NeverDestroyed<String> arabic(ASCIILiteral("Arabic"));
+        static NeverDestroyed<String> pashto(ASCIILiteral("Pashto"));
+        static NeverDestroyed<String> urdu(ASCIILiteral("Urdu"));
+        static String* matchWords[3] = { &arabic.get(), &pashto.get(), &urdu.get() };
+        static NeverDestroyed<AtomicString> geezaPlain("GeezaPro", AtomicString::ConstructFromLiteral);
+        static NeverDestroyed<AtomicString> geezaBold("GeezaPro-Bold", AtomicString::ConstructFromLiteral);
+        for (String* matchWord : matchWords) {
+            if (family.contains(*matchWord, false))
+                font = fontForFamily(description, isFontWeightBold(description.weight()) ? geezaBold : geezaPlain);
+        }
+    }
+
+    return font.release();
+}
+
+void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
+{
+    RetainPtr<CFStringRef> familyNameStr = familyName.string().createCFString();
+    CFStringRef familyNameStrPtr = familyNameStr.get();
+    RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, (const void**)&kCTFontFamilyNameAttribute, (const void**)&familyNameStrPtr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
+    RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithAttributes(attributes.get()));
+    RetainPtr<CFArrayRef> matchedDescriptors = adoptCF(CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor.get(), nullptr));
+    if (!matchedDescriptors)
+        return;
+
+    CFIndex numMatches = CFArrayGetCount(matchedDescriptors.get());
+    if (!numMatches)
+        return;
+
+    for (CFIndex i = 0; i < numMatches; ++i) {
+        RetainPtr<CFDictionaryRef> traits = adoptCF((CFDictionaryRef)CTFontDescriptorCopyAttribute((CTFontDescriptorRef)CFArrayGetValueAtIndex(matchedDescriptors.get(), i), kCTFontTraitsAttribute));
+        CFNumberRef resultRef = (CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait);
+        CFNumberRef weightRef = (CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontWeightTrait);
+        if (resultRef && weightRef) {
+            CTFontSymbolicTraits symbolicTraits;
+            CFNumberGetValue(resultRef, kCFNumberIntType, &symbolicTraits);
+            CGFloat weight = 0;
+            CFNumberGetValue(weightRef, kCFNumberCGFloatType, &weight);
+            traitsMasks.append(toTraitsMask(symbolicTraits, weight));
+        }
+    }
+}
+
+}

Modified: trunk/Source/WebCore/platform/graphics/ios/FontCacheIOS.mm (188794 => 188795)


--- trunk/Source/WebCore/platform/graphics/ios/FontCacheIOS.mm	2015-08-21 23:16:48 UTC (rev 188794)
+++ trunk/Source/WebCore/platform/graphics/ios/FontCacheIOS.mm	2015-08-21 23:28:08 UTC (rev 188795)
@@ -150,48 +150,6 @@
     return fontFamilies;
 }
 
-RefPtr<Font> FontCache::similarFont(const FontDescription& description)
-{
-    // Attempt to find an appropriate font using a match based on the presence of keywords in
-    // the requested names. For example, we'll match any name that contains "Arabic" to Geeza Pro.
-    RefPtr<Font> font;
-    for (unsigned i = 0; i < description.familyCount(); ++i) {
-        const AtomicString& family = description.familyAt(i);
-        if (family.isEmpty())
-            continue;
-
-        // Substitute the default monospace font for well-known monospace fonts.
-        static NeverDestroyed<AtomicString> monaco("monaco", AtomicString::ConstructFromLiteral);
-        static NeverDestroyed<AtomicString> menlo("menlo", AtomicString::ConstructFromLiteral);
-        static NeverDestroyed<AtomicString> courier("courier", AtomicString::ConstructFromLiteral);
-        if (equalIgnoringCase(family, monaco) || equalIgnoringCase(family, menlo)) {
-            font = fontForFamily(description, courier);
-            continue;
-        }
-
-        // Substitute Verdana for Lucida Grande.
-        static NeverDestroyed<AtomicString> lucidaGrande("lucida grande", AtomicString::ConstructFromLiteral);
-        static NeverDestroyed<AtomicString> verdana("verdana", AtomicString::ConstructFromLiteral);
-        if (equalIgnoringCase(family, lucidaGrande)) {
-            font = fontForFamily(description, verdana);
-            continue;
-        }
-
-        static NeverDestroyed<String> arabic(ASCIILiteral("Arabic"));
-        static NeverDestroyed<String> pashto(ASCIILiteral("Pashto"));
-        static NeverDestroyed<String> urdu(ASCIILiteral("Urdu"));
-        static String* matchWords[3] = { &arabic.get(), &pashto.get(), &urdu.get() };
-        static NeverDestroyed<AtomicString> geezaPlain("GeezaPro", AtomicString::ConstructFromLiteral);
-        static NeverDestroyed<AtomicString> geezaBold("GeezaPro-Bold", AtomicString::ConstructFromLiteral);
-        for (unsigned j = 0; j < 3 && !font; ++j) {
-            if (family.contains(*matchWords[j], false))
-                font = fontForFamily(description, isFontWeightBold(description.weight()) ? geezaBold : geezaPlain);
-        }
-    }
-
-    return font.release();
-}
-
 Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
 {
     static NeverDestroyed<AtomicString> fallbackFontFamily(".PhoneFallback", AtomicString::ConstructFromLiteral);
@@ -227,37 +185,6 @@
     return getCachedFontPlatformData(description, *family);
 }
 
-static inline FontTraitsMask toTraitsMask(CTFontSymbolicTraits ctFontTraits)
-{
-    return static_cast<FontTraitsMask>(((ctFontTraits & kCTFontTraitItalic) ? FontStyleItalicMask : FontStyleNormalMask)
-        | FontVariantNormalMask
-        // FontWeight600 or higher is bold for CTFonts, so choose middle values for
-        // bold (600-900) and non-bold (100-500)
-        | ((ctFontTraits & kCTFontTraitBold) ? FontWeight700Mask : FontWeight300Mask));
-}
-
-void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
-{
-    RetainPtr<CFStringRef> familyNameStr = familyName.string().createCFString();
-    NSDictionary *attributes = @{ (id)kCTFontFamilyNameAttribute: (NSString*)familyNameStr.get() };
-    RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes));
-    RetainPtr<NSArray> matchedDescriptors = adoptNS((NSArray *)CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor.get(), nullptr));
-
-    NSInteger numMatches = [matchedDescriptors.get() count];
-    if (!matchedDescriptors || !numMatches)
-        return;
-
-    for (NSInteger i = 0; i < numMatches; ++i) {
-        RetainPtr<CFDictionaryRef> traits = adoptCF((CFDictionaryRef)CTFontDescriptorCopyAttribute((CTFontDescriptorRef)[matchedDescriptors.get() objectAtIndex:i], kCTFontTraitsAttribute));
-        CFNumberRef resultRef = (CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait);
-        if (resultRef) {
-            CTFontSymbolicTraits symbolicTraits;
-            CFNumberGetValue(resultRef, kCFNumberIntType, &symbolicTraits);
-            traitsMasks.append(toTraitsMask(symbolicTraits));
-        }
-    }
-}
-
 float FontCache::weightOfCTFont(CTFontRef font)
 {
     float result = 0;

Modified: trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm (188794 => 188795)


--- trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm	2015-08-21 23:16:48 UTC (rev 188794)
+++ trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm	2015-08-21 23:28:08 UTC (rev 188795)
@@ -114,21 +114,6 @@
 
 #endif
 
-static inline FontTraitsMask toTraitsMask(NSFontTraitMask appKitTraits, NSInteger appKitWeight)
-{
-    return static_cast<FontTraitsMask>(((appKitTraits & NSFontItalicTrait) ? FontStyleItalicMask : FontStyleNormalMask)
-        | FontVariantNormalMask
-        | (appKitWeight == 1 ? FontWeight100Mask :
-            appKitWeight == 2 ? FontWeight200Mask :
-            appKitWeight <= 4 ? FontWeight300Mask :
-            appKitWeight == 5 ? FontWeight400Mask :
-            appKitWeight == 6 ? FontWeight500Mask :
-            appKitWeight <= 8 ? FontWeight600Mask :
-            appKitWeight == 9 ? FontWeight700Mask :
-            appKitWeight <= 11 ? FontWeight800Mask :
-                FontWeight900Mask));
-}
-
 #if !ENABLE(PLATFORM_FONT_LOOKUP)
 // Keep a cache for mapping desired font families to font families actually available on the system for performance.
 using AvailableFamilyMap = HashMap<std::pair<AtomicString, NSFontTraitMask>, AtomicString>;
@@ -607,26 +592,6 @@
     return fontFamilies;
 }
 
-RefPtr<Font> FontCache::similarFont(const FontDescription& description)
-{
-    // Attempt to find an appropriate font using a match based on 
-    // the presence of keywords in the the requested names.  For example, we'll
-    // match any name that contains "Arabic" to Geeza Pro.
-    RefPtr<Font> font;
-    for (unsigned i = 0; i < description.familyCount(); ++i) {
-        const AtomicString& family = description.familyAt(i);
-        if (family.isEmpty())
-            continue;
-        static String* matchWords[3] = { new String("Arabic"), new String("Pashto"), new String("Urdu") };
-        DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, geezaStr, ("Geeza Pro", AtomicString::ConstructFromLiteral));
-        for (unsigned j = 0; j < 3 && !font; ++j) {
-            if (family.contains(*matchWords[j], false))
-                font = fontForFamily(description, geezaStr);
-        }
-    }
-    return font.release();
-}
-
 Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
 {
     DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, timesStr, ("Times", AtomicString::ConstructFromLiteral));
@@ -645,39 +610,6 @@
     return *fontForFamily(fontDescription, lucidaGrandeStr, false);
 }
 
-void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
-{
-    NSFontManager *fontManager = [NSFontManager sharedFontManager];
-
-    NSString *availableFamily;
-    for (availableFamily in [fontManager availableFontFamilies]) {
-        if ([familyName caseInsensitiveCompare:availableFamily] == NSOrderedSame)
-            break;
-    }
-
-    if (!availableFamily) {
-        // Match by PostScript name.
-        for (NSString *availableFont in [fontManager availableFonts]) {
-            if ([familyName caseInsensitiveCompare:availableFont] == NSOrderedSame) {
-                NSFont *font = [NSFont fontWithName:availableFont size:10];
-                NSInteger weight = [fontManager weightOfFont:font];
-                traitsMasks.append(toTraitsMask([fontManager traitsOfFont:font], weight));
-                break;
-            }
-        }
-        return;
-    }
-
-    NSArray *fonts = [fontManager availableMembersOfFontFamily:availableFamily];
-    traitsMasks.reserveCapacity([fonts count]);
-    for (NSArray *fontInfo in fonts) {
-        // Array indices must be hard coded because of lame AppKit API.
-        NSInteger fontWeight = [[fontInfo objectAtIndex:2] intValue];
-        NSFontTraitMask fontTraits = [[fontInfo objectAtIndex:3] unsignedIntValue];
-        traitsMasks.uncheckedAppend(toTraitsMask(fontTraits, fontWeight));
-    }
-}
-
 std::unique_ptr<FontPlatformData> FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
 {
     NSFontTraitMask traits = fontDescription.italic() ? NSFontItalicTrait : 0;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to