Web Inspector: Slow open time enumerating system fonts (FontCache::systemFontFamilies)
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jan 2018 20:17:12 +0000 (20:17 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jan 2018 20:17:12 +0000 (20:17 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180979
<rdar://problem/36146670>

Patch by Joseph Pecoraro <pecoraro@apple.com> on 2018-01-03
Reviewed by Matt Baker.

Source/WebCore:

* platform/graphics/cocoa/FontCacheCoreText.cpp:
(fontNameIsSystemFont):
(WebCore::FontCache::systemFontFamilies):
Switch to the original Mac algorithm before r180979 that uses
CTFontManagerCopyAvailableFontFamilyNames. Previously this wasn't
available on iOS but now it is. This is a performance improvement on
both platforms, but significantly so on macOS. It also finds more,
valid, family names.

LayoutTests:

* inspector/css/get-system-fonts.html:
Cleanup the test a bit.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@226371 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/css/get-system-fonts.html
Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp

index aa437a3..4304f20 100644 (file)
@@ -1,3 +1,14 @@
+2018-01-03  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: Slow open time enumerating system fonts (FontCache::systemFontFamilies)
+        https://bugs.webkit.org/show_bug.cgi?id=180979
+        <rdar://problem/36146670>
+
+        Reviewed by Matt Baker.
+
+        * inspector/css/get-system-fonts.html:
+        Cleanup the test a bit.
+
 2018-01-03  Simon Fraser  <simon.fraser@apple.com>
 
         SVG lighting filter lights are in the wrong coordinate system
index 75e9c15..3f51b21 100644 (file)
@@ -28,7 +28,6 @@ function test() {
 </script>
 </head>
 <body onload="runTest()">
-  <p>This test ensures that the inspector can enumerate system font families, and checks for the
-     existence of common fonts.</p>
+<p>This test ensures that the inspector can enumerate system font families, and checks for the existence of common fonts.</p>
 </body>
 </html>
index 670a268..bf997a2 100644 (file)
@@ -1,3 +1,20 @@
+2018-01-03  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: Slow open time enumerating system fonts (FontCache::systemFontFamilies)
+        https://bugs.webkit.org/show_bug.cgi?id=180979
+        <rdar://problem/36146670>
+
+        Reviewed by Matt Baker.
+
+        * platform/graphics/cocoa/FontCacheCoreText.cpp:
+        (fontNameIsSystemFont):
+        (WebCore::FontCache::systemFontFamilies):
+        Switch to the original Mac algorithm before r180979 that uses
+        CTFontManagerCopyAvailableFontFamilyNames. Previously this wasn't
+        available on iOS but now it is. This is a performance improvement on
+        both platforms, but significantly so on macOS. It also finds more,
+        valid, family names.
+
 2018-01-03  Michael Catanzaro  <mcatanzaro@igalia.com>
 
         ASSERTION FAILED: !source || is<Target>(*source) in CoordinatedGraphicsLayer::removeFromParent
index 38e5083..4d397bc 100644 (file)
@@ -367,6 +367,11 @@ static FeaturesMap computeFeatureSettingsFromVariants(const FontVariantSettings&
     return result;
 }
 
+static inline bool fontNameIsSystemFont(CFStringRef fontName)
+{
+    return CFStringGetLength(fontName) > 0 && CFStringGetCharacterAtIndex(fontName, 0) == '.';
+}
+
 #if ENABLE(VARIATION_FONTS)
 struct VariationDefaults {
     float defaultValue;
@@ -417,8 +422,9 @@ static inline bool fontIsSystemFont(CTFontRef font)
 {
     if (CTFontDescriptorIsSystemUIFont(adoptCF(CTFontCopyFontDescriptor(font)).get()))
         return true;
+
     auto name = adoptCF(CTFontCopyPostScriptName(font));
-    return CFStringGetLength(name.get()) > 0 && CFStringGetCharacterAtIndex(name.get(), 0) == '.';
+    return fontNameIsSystemFont(name.get());
 }
 
 // These values were calculated by performing a linear regression on the CSS weights/widths/slopes and Core Text weights/widths/slopes of San Francisco.
@@ -731,25 +737,24 @@ void FontCache::platformInit()
 
 Vector<String> FontCache::systemFontFamilies()
 {
-    // FIXME: <rdar://problem/21890188>
-    auto attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, nullptr, nullptr, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
-    auto emptyFontDescriptor = adoptCF(CTFontDescriptorCreateWithAttributes(attributes.get()));
-    auto matchedDescriptors = adoptCF(CTFontDescriptorCreateMatchingFontDescriptors(emptyFontDescriptor.get(), nullptr));
-    if (!matchedDescriptors)
-        return { };
+    Vector<String> fontFamilies;
+
+    auto availableFontFamilies = adoptCF(CTFontManagerCopyAvailableFontFamilyNames());
+    CFIndex count = CFArrayGetCount(availableFontFamilies.get());
+    for (CFIndex i = 0; i < count; ++i) {
+        CFStringRef fontName = static_cast<CFStringRef>(CFArrayGetValueAtIndex(availableFontFamilies.get(), i));
+        if (CFGetTypeID(fontName) != CFStringGetTypeID()) {
+            ASSERT_NOT_REACHED();
+            continue;
+        }
 
-    CFIndex numMatches = CFArrayGetCount(matchedDescriptors.get());
-    if (!numMatches)
-        return { };
+        if (fontNameIsSystemFont(fontName))
+            continue;
 
-    HashSet<String> visited;
-    for (CFIndex i = 0; i < numMatches; ++i) {
-        auto fontDescriptor = static_cast<CTFontDescriptorRef>(CFArrayGetValueAtIndex(matchedDescriptors.get(), i));
-        if (auto familyName = adoptCF(static_cast<CFStringRef>(CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontFamilyNameAttribute))))
-            visited.add(familyName.get());
+        fontFamilies.append(fontName);
     }
 
-    return copyToVector(visited);
+    return fontFamilies;
 }
 
 static CTFontSymbolicTraits computeTraits(const FontDescription& fontDescription)