Title: [266904] trunk/Source/WebCore
Revision
266904
Author
mmaxfi...@apple.com
Date
2020-09-10 18:51:37 -0700 (Thu, 10 Sep 2020)

Log Message

Small cleanup in RenderTheme
https://bugs.webkit.org/show_bug.cgi?id=216293

Reviewed by Darin Adler.

See the discussion in https://bugs.webkit.org/show_bug.cgi?id=213332 for background.
More comments below.

No new tests because there is no behavior change.

* platform/graphics/cocoa/FontCocoa.mm:
(WebCore::Font::platformCreateScaledFont const): Use auto instead of RetainPtr.
* platform/graphics/cocoa/FontDescriptionCocoa.cpp:
(WebCore::matchSystemFontUse): Use std::binary_search instead of std::find,
because it's faster and we expect this function to be called very often.
* platform/graphics/mac/FontCustomPlatformData.cpp:
(WebCore::FontCustomPlatformData::fontPlatformData): Use auto instead of RetainPtr.
* rendering/RenderTheme.cpp:
(WebCore::RenderTheme::cachedSystemFontDescription const): Move the local variables
into an array.
* rendering/RenderThemeCocoa.mm:
(WebCore::RenderThemeCocoa::cachedSystemFontDescription const): Ditto.
(WebCore::cssWeightOfSystemFont):
(WebCore::RenderThemeCocoa::updateCachedSystemFontDescription const): Use auto
instead of RetainPtr.
* rendering/RenderThemeIOS.mm:
(WebCore::attachmentTitleFont): Ditto.
* rendering/RenderThemeMac.mm:
(WebCore::AttachmentLayout::layOutTitle): Ditto.
(WebCore::AttachmentLayout::layOutSubtitle): Ditto.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (266903 => 266904)


--- trunk/Source/WebCore/ChangeLog	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/ChangeLog	2020-09-11 01:51:37 UTC (rev 266904)
@@ -1,3 +1,36 @@
+2020-09-10  Myles C. Maxfield  <mmaxfi...@apple.com>
+
+        Small cleanup in RenderTheme
+        https://bugs.webkit.org/show_bug.cgi?id=216293
+
+        Reviewed by Darin Adler.
+
+        See the discussion in https://bugs.webkit.org/show_bug.cgi?id=213332 for background.
+        More comments below.
+
+        No new tests because there is no behavior change.
+
+        * platform/graphics/cocoa/FontCocoa.mm:
+        (WebCore::Font::platformCreateScaledFont const): Use auto instead of RetainPtr.
+        * platform/graphics/cocoa/FontDescriptionCocoa.cpp:
+        (WebCore::matchSystemFontUse): Use std::binary_search instead of std::find,
+        because it's faster and we expect this function to be called very often.
+        * platform/graphics/mac/FontCustomPlatformData.cpp:
+        (WebCore::FontCustomPlatformData::fontPlatformData): Use auto instead of RetainPtr.
+        * rendering/RenderTheme.cpp:
+        (WebCore::RenderTheme::cachedSystemFontDescription const): Move the local variables
+        into an array.
+        * rendering/RenderThemeCocoa.mm:
+        (WebCore::RenderThemeCocoa::cachedSystemFontDescription const): Ditto.
+        (WebCore::cssWeightOfSystemFont):
+        (WebCore::RenderThemeCocoa::updateCachedSystemFontDescription const): Use auto
+        instead of RetainPtr.
+        * rendering/RenderThemeIOS.mm:
+        (WebCore::attachmentTitleFont): Ditto.
+        * rendering/RenderThemeMac.mm:
+        (WebCore::AttachmentLayout::layOutTitle): Ditto.
+        (WebCore::AttachmentLayout::layOutSubtitle): Ditto.
+
 2020-09-10  Joonghun Park  <jh718.p...@samsung.com>
 
         Unreviewed. Remove the build warning below since r266885.

Modified: trunk/Source/WebCore/platform/graphics/cocoa/FontCocoa.mm (266903 => 266904)


--- trunk/Source/WebCore/platform/graphics/cocoa/FontCocoa.mm	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/platform/graphics/cocoa/FontCocoa.mm	2020-09-11 01:51:37 UTC (rev 266904)
@@ -556,7 +556,7 @@
 {
     float size = m_platformData.size() * scaleFactor;
     CTFontSymbolicTraits fontTraits = CTFontGetSymbolicTraits(m_platformData.font());
-    RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontCopyFontDescriptor(m_platformData.font()));
+    auto fontDescriptor = adoptCF(CTFontCopyFontDescriptor(m_platformData.font()));
     RetainPtr<CTFontRef> scaledFont = adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), size, nullptr));
 
     return createDerivativeFont(scaledFont.get(), size, m_platformData.orientation(), fontTraits, m_platformData.syntheticBold(), m_platformData.syntheticOblique());

Modified: trunk/Source/WebCore/platform/graphics/cocoa/FontDescriptionCocoa.cpp (266903 => 266904)


--- trunk/Source/WebCore/platform/graphics/cocoa/FontDescriptionCocoa.cpp	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/platform/graphics/cocoa/FontDescriptionCocoa.cpp	2020-09-11 01:51:37 UTC (rev 266904)
@@ -78,9 +78,16 @@
         kCTUIFontTextStyleTitle0,
         kCTUIFontTextStyleTitle4,
     };
-    
-    static auto strings { makeNeverDestroyed(convertArray<AtomString>(styles)) };
-    if (std::find(strings.get().begin(), strings.get().end(), string) != strings.get().end())
+
+    auto compareAsPointer = [](const AtomString& lhs, const AtomString& rhs) {
+        return lhs.impl() < rhs.impl();
+    };
+    static auto strings = makeNeverDestroyed([&compareAsPointer] {
+        auto result = convertArray<AtomString>(styles);
+        std::sort(result.begin(), result.end(), compareAsPointer);
+        return result;
+    }());
+    if (std::binary_search(strings.get().begin(), strings.get().end(), string, compareAsPointer))
         return SystemFontKind::TextStyle;
 
     return WTF::nullopt;

Modified: trunk/Source/WebCore/platform/graphics/mac/FontCustomPlatformData.cpp (266903 => 266904)


--- trunk/Source/WebCore/platform/graphics/mac/FontCustomPlatformData.cpp	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/platform/graphics/mac/FontCustomPlatformData.cpp	2020-09-11 01:51:37 UTC (rev 266904)
@@ -45,7 +45,7 @@
     int size = fontDescription.computedPixelSize();
     FontOrientation orientation = fontDescription.orientation();
     FontWidthVariant widthVariant = fontDescription.widthVariant();
-    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithFontDescriptor(modifiedFontDescriptor.get(), size, nullptr));
+    auto font = adoptCF(CTFontCreateWithFontDescriptor(modifiedFontDescriptor.get(), size, nullptr));
     font = preparePlatformFont(font.get(), fontDescription, &fontFaceFeatures, fontFaceCapabilities);
     ASSERT(font);
     return FontPlatformData(font.get(), size, bold, italic, orientation, widthVariant, fontDescription.textRenderingMode());

Modified: trunk/Source/WebCore/rendering/RenderTheme.cpp (266903 => 266904)


--- trunk/Source/WebCore/rendering/RenderTheme.cpp	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/rendering/RenderTheme.cpp	2020-09-11 01:51:37 UTC (rev 266904)
@@ -1283,41 +1283,32 @@
 
 FontCascadeDescription& RenderTheme::cachedSystemFontDescription(CSSValueID systemFontID) const
 {
-    static NeverDestroyed<FontCascadeDescription> caption;
-    static NeverDestroyed<FontCascadeDescription> icon;
-    static NeverDestroyed<FontCascadeDescription> menu;
-    static NeverDestroyed<FontCascadeDescription> messageBox;
-    static NeverDestroyed<FontCascadeDescription> smallCaption;
-    static NeverDestroyed<FontCascadeDescription> statusBar;
-    static NeverDestroyed<FontCascadeDescription> webkitMiniControl;
-    static NeverDestroyed<FontCascadeDescription> webkitSmallControl;
-    static NeverDestroyed<FontCascadeDescription> webkitControl;
-    static NeverDestroyed<FontCascadeDescription> defaultDescription;
+    static auto fontDescriptions = makeNeverDestroyed<std::array<FontCascadeDescription, 10>>({ });
 
     switch (systemFontID) {
     case CSSValueCaption:
-        return caption;
+        return fontDescriptions.get()[0];
     case CSSValueIcon:
-        return icon;
+        return fontDescriptions.get()[1];
     case CSSValueMenu:
-        return menu;
+        return fontDescriptions.get()[2];
     case CSSValueMessageBox:
-        return messageBox;
+        return fontDescriptions.get()[3];
     case CSSValueSmallCaption:
-        return smallCaption;
+        return fontDescriptions.get()[4];
     case CSSValueStatusBar:
-        return statusBar;
+        return fontDescriptions.get()[5];
     case CSSValueWebkitMiniControl:
-        return webkitMiniControl;
+        return fontDescriptions.get()[6];
     case CSSValueWebkitSmallControl:
-        return webkitSmallControl;
+        return fontDescriptions.get()[7];
     case CSSValueWebkitControl:
-        return webkitControl;
+        return fontDescriptions.get()[8];
     case CSSValueNone:
-        return defaultDescription;
+        return fontDescriptions.get()[9];
     default:
         ASSERT_NOT_REACHED();
-        return defaultDescription;
+        return fontDescriptions.get()[9];
     }
 }
 

Modified: trunk/Source/WebCore/rendering/RenderThemeCocoa.mm (266903 => 266904)


--- trunk/Source/WebCore/rendering/RenderThemeCocoa.mm	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/rendering/RenderThemeCocoa.mm	2020-09-11 01:51:37 UTC (rev 266904)
@@ -29,6 +29,7 @@
 #import "GraphicsContextCG.h"
 #import "HTMLInputElement.h"
 #import "RenderText.h"
+#import <algorithm>
 #import <pal/spi/cocoa/CoreTextSPI.h>
 
 #if ENABLE(VIDEO)
@@ -178,81 +179,47 @@
 
 FontCascadeDescription& RenderThemeCocoa::cachedSystemFontDescription(CSSValueID valueID) const
 {
-    static NeverDestroyed<FontCascadeDescription> systemFont;
-    static NeverDestroyed<FontCascadeDescription> headlineFont;
-    static NeverDestroyed<FontCascadeDescription> bodyFont;
-    static NeverDestroyed<FontCascadeDescription> subheadlineFont;
-    static NeverDestroyed<FontCascadeDescription> footnoteFont;
-    static NeverDestroyed<FontCascadeDescription> caption1Font;
-    static NeverDestroyed<FontCascadeDescription> caption2Font;
-    static NeverDestroyed<FontCascadeDescription> shortHeadlineFont;
-    static NeverDestroyed<FontCascadeDescription> shortBodyFont;
-    static NeverDestroyed<FontCascadeDescription> shortSubheadlineFont;
-    static NeverDestroyed<FontCascadeDescription> shortFootnoteFont;
-    static NeverDestroyed<FontCascadeDescription> shortCaption1Font;
-    static NeverDestroyed<FontCascadeDescription> tallBodyFont;
-    static NeverDestroyed<FontCascadeDescription> title0Font;
-    static NeverDestroyed<FontCascadeDescription> title1Font;
-    static NeverDestroyed<FontCascadeDescription> title2Font;
-    static NeverDestroyed<FontCascadeDescription> title3Font;
-    static NeverDestroyed<FontCascadeDescription> title4Font;
+    static auto fontDescriptions = makeNeverDestroyed<std::array<FontCascadeDescription, 17>>({ });
 
-    static CFStringRef userTextSize = contentSizeCategory();
+    ASSERT(std::all_of(std::begin(fontDescriptions.get()), std::end(fontDescriptions.get()), [](auto& description) {
+        return !description.isAbsoluteSize();
+    }));
 
-    if (userTextSize != contentSizeCategory()) {
-        userTextSize = contentSizeCategory();
-
-        headlineFont.get().setIsAbsoluteSize(false);
-        bodyFont.get().setIsAbsoluteSize(false);
-        subheadlineFont.get().setIsAbsoluteSize(false);
-        footnoteFont.get().setIsAbsoluteSize(false);
-        caption1Font.get().setIsAbsoluteSize(false);
-        caption2Font.get().setIsAbsoluteSize(false);
-        shortHeadlineFont.get().setIsAbsoluteSize(false);
-        shortBodyFont.get().setIsAbsoluteSize(false);
-        shortSubheadlineFont.get().setIsAbsoluteSize(false);
-        shortFootnoteFont.get().setIsAbsoluteSize(false);
-        shortCaption1Font.get().setIsAbsoluteSize(false);
-        tallBodyFont.get().setIsAbsoluteSize(false);
-    }
-
     switch (valueID) {
     case CSSValueAppleSystemHeadline:
-        return headlineFont;
+        return fontDescriptions.get()[0];
     case CSSValueAppleSystemBody:
-        return bodyFont;
+        return fontDescriptions.get()[1];
     case CSSValueAppleSystemTitle0:
-        return title0Font;
+        return fontDescriptions.get()[2];
     case CSSValueAppleSystemTitle1:
-        return title1Font;
+        return fontDescriptions.get()[3];
     case CSSValueAppleSystemTitle2:
-        return title2Font;
+        return fontDescriptions.get()[4];
     case CSSValueAppleSystemTitle3:
-        return title3Font;
+        return fontDescriptions.get()[5];
     case CSSValueAppleSystemTitle4:
-        return title4Font;
+        return fontDescriptions.get()[6];
     case CSSValueAppleSystemSubheadline:
-        return subheadlineFont;
+        return fontDescriptions.get()[7];
     case CSSValueAppleSystemFootnote:
-        return footnoteFont;
+        return fontDescriptions.get()[8];
     case CSSValueAppleSystemCaption1:
-        return caption1Font;
+        return fontDescriptions.get()[9];
     case CSSValueAppleSystemCaption2:
-        return caption2Font;
-        // Short version.
+        return fontDescriptions.get()[10];
     case CSSValueAppleSystemShortHeadline:
-        return shortHeadlineFont;
+        return fontDescriptions.get()[11];
     case CSSValueAppleSystemShortBody:
-        return shortBodyFont;
+        return fontDescriptions.get()[12];
     case CSSValueAppleSystemShortSubheadline:
-        return shortSubheadlineFont;
+        return fontDescriptions.get()[13];
     case CSSValueAppleSystemShortFootnote:
-        return shortFootnoteFont;
+        return fontDescriptions.get()[14];
     case CSSValueAppleSystemShortCaption1:
-        return shortCaption1Font;
-        // Tall version.
+        return fontDescriptions.get()[15];
     case CSSValueAppleSystemTallBody:
-        return tallBodyFont;
+        return fontDescriptions.get()[16];
     default:
         return RenderTheme::cachedSystemFontDescription(valueID);
     }
@@ -265,7 +232,7 @@
     float result = 0;
     CFNumberGetValue(resultRef, kCFNumberFloatType, &result);
     // These numbers were experimentally gathered from weights of the system font.
-    static constexpr const float weightThresholds[] = { -0.6, -0.365, -0.115, 0.130, 0.235, 0.350, 0.5, 0.7 };
+    static constexpr float weightThresholds[] = { -0.6, -0.365, -0.115, 0.130, 0.235, 0.350, 0.5, 0.7 };
     for (unsigned i = 0; i < WTF_ARRAY_LENGTH(weightThresholds); ++i) {
         if (result < weightThresholds[i])
             return FontSelectionValue((static_cast<int>(i) + 1) * 100);
@@ -409,7 +376,7 @@
         style = textStyle;
 
     ASSERT(fontDescriptor);
-    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), 0, nullptr));
+    auto font = adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), 0, nullptr));
     fontDescription.setIsAbsoluteSize(true);
     fontDescription.setOneFamily(style);
     fontDescription.setSpecifiedSize(CTFontGetSize(font.get()));

Modified: trunk/Source/WebCore/rendering/RenderThemeIOS.mm (266903 => 266904)


--- trunk/Source/WebCore/rendering/RenderThemeIOS.mm	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/rendering/RenderThemeIOS.mm	2020-09-11 01:51:37 UTC (rev 266904)
@@ -1419,7 +1419,7 @@
 
 static RetainPtr<CTFontRef> attachmentTitleFont()
 {
-    RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithTextStyle(kCTUIFontTextStyleShortCaption1, RenderThemeIOS::singleton().contentSizeCategory(), 0));
+    auto fontDescriptor = adoptCF(CTFontDescriptorCreateWithTextStyle(kCTUIFontTextStyleShortCaption1, RenderThemeIOS::singleton().contentSizeCategory(), 0));
     return adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), 0, nullptr));
 }
 

Modified: trunk/Source/WebCore/rendering/RenderThemeMac.mm (266903 => 266904)


--- trunk/Source/WebCore/rendering/RenderThemeMac.mm	2020-09-11 00:50:21 UTC (rev 266903)
+++ trunk/Source/WebCore/rendering/RenderThemeMac.mm	2020-09-11 01:51:37 UTC (rev 266904)
@@ -2559,7 +2559,7 @@
 void AttachmentLayout::layOutTitle(const RenderAttachment& attachment)
 {
     CFStringRef language = 0; // By not specifying a language we use the system language.
-    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
+    auto font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
     baseline = CGRound(attachmentIconBackgroundSize + attachmentIconToTitleMargin + CTFontGetAscent(font.get()));
 
     String title = attachment.attachmentElement().attachmentTitleForDisplay();
@@ -2624,7 +2624,7 @@
 
     Color subtitleColor = attachment.style().colorByApplyingColorFilter(attachmentSubtitleTextColor);
     CFStringRef language = 0; // By not specifying a language we use the system language.
-    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
+    auto font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
     NSDictionary *textAttributes = @{
         (__bridge id)kCTFontAttributeName: (__bridge id)font.get(),
         (__bridge id)kCTForegroundColorAttributeName: (__bridge NSColor *)cachedCGColor(subtitleColor)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to