Title: [275420] trunk
Revision
275420
Author
[email protected]
Date
2021-04-02 07:06:16 -0700 (Fri, 02 Apr 2021)

Log Message

Implement text rendering on OffscreenCanvas in a Worker
https://bugs.webkit.org/show_bug.cgi?id=202793

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

Adjust test expectations.

* web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.basic.worker-expected.txt:
* web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.complex.worker-expected.txt:
* web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.invalid.worker-expected.txt:
* web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.tiny.worker-expected.txt:

Source/WebCore:

Add a CSSFontSelector and FontCache to WorkerGlobalScope and via
interface changes to ScriptExecutionContext and FontSelector, use them
as appropriate after enabling text functions on Worker threads with
OffscreenCanvas.

No new tests, rebaselined existing tests.

* css/CSSFontSelector.cpp:
(WebCore::CSSFontSelector::CSSFontSelector):
(WebCore::m_version):
(WebCore::CSSFontSelector::~CSSFontSelector):
(WebCore::CSSFontSelector::fontRangesForFamily):
(WebCore::CSSFontSelector::fallbackFontAt):
* css/CSSFontSelector.h:
* css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::genericFontFamily):
(WebCore::CSSPropertyParserHelpers::genericFontFamilyIndex):
* css/parser/CSSPropertyParserHelpers.h:
* dom/Document.h:
* dom/ScriptExecutionContext.cpp:
(WebCore::ScriptExecutionContext::fontCache):
* dom/ScriptExecutionContext.h:
(WebCore::ScriptExecutionContext::cssFontSelector):
* dom/make_names.pl:
* html/canvas/CanvasRenderingContext2DBase.cpp:
(WebCore::CanvasRenderingContext2DBase::canDrawText):
* html/canvas/OffscreenCanvasRenderingContext2D.cpp:
(WebCore::OffscreenCanvasRenderingContext2D::setFont):
* platform/graphics/FontCache.cpp:
(WebCore::FontDataCacheKeyHash::hash):
(WebCore::FontDataCacheKeyHash::equal):
(WebCore::FontDataCacheKeyTraits::emptyValue):
(WebCore::FontDataCacheKeyTraits::constructDeletedValue):
(WebCore::FontDataCacheKeyTraits::isDeletedValue):
(WebCore::FontCache::create):
(WebCore::FontCache::singleton):
(WebCore::FontCache::FontCache):
(WebCore::FontCache::getCachedFontPlatformData):
(WebCore::FontCache::fontForPlatformData):
(WebCore::FontCache::purgeInactiveFontDataIfNeeded):
(WebCore::FontCache::purgeInactiveFontData):
(WebCore::FontCache::verticalData):
(WebCore::FontCache::fontCount):
(WebCore::FontCache::inactiveFontCount):
(WebCore::FontCache::invalidate):
* platform/graphics/FontCache.h:
(WebCore::FontCache::fontCacheFallbackToSingleton):
* platform/graphics/FontCascade.cpp:
(WebCore::FontCascade::isCurrent const):
(WebCore::FontCascade::update const):
* platform/graphics/FontCascadeFonts.cpp:
(WebCore::FontCascadeFonts::FontCascadeFonts):
(WebCore::realizeNextFallback):
(WebCore::FontCascadeFonts::realizeFallbackRangesAt):
* platform/graphics/FontSelector.h:
* platform/graphics/cairo/FontCairoHarfbuzzNG.cpp:
(WebCore::FontCascade::fontForCombiningCharacterSequence const):
* platform/graphics/freetype/FontCacheFreeType.cpp:
(WebCore::getFamilyNameStringFromFamily):
* style/StyleBuilderCustom.h:
(WebCore::Style::BuilderCustom::applyValueFontFamily):
* style/StyleResolveForFontRaw.cpp:
(WebCore::Style::useFixedDefaultSize):
(WebCore::Style::resolveForFontRaw):
* style/StyleResolveForFontRaw.h:
* workers/WorkerGlobalScope.cpp:
(WebCore::WorkerGlobalScope::~WorkerGlobalScope):
(WebCore::WorkerGlobalScope::cssFontSelector):
(WebCore::WorkerGlobalScope::fontCache):
* workers/WorkerGlobalScope.h:

LayoutTests:

Adjust expectations for manual OffscreenCanvas worker text test.

* platform/glib/TestExpectations:

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (275419 => 275420)


--- trunk/LayoutTests/ChangeLog	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/ChangeLog	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,14 @@
+2021-04-02  Chris Lord  <[email protected]>
+
+        Implement text rendering on OffscreenCanvas in a Worker
+        https://bugs.webkit.org/show_bug.cgi?id=202793
+
+        Reviewed by Darin Adler.
+
+        Adjust expectations for manual OffscreenCanvas worker text test.
+
+        * platform/glib/TestExpectations:
+
 2021-04-02  Youenn Fablet  <[email protected]>
 
         No audio output when unplugged wired headset during a call

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (275419 => 275420)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,17 @@
+2021-04-02  Chris Lord  <[email protected]>
+
+        Implement text rendering on OffscreenCanvas in a Worker
+        https://bugs.webkit.org/show_bug.cgi?id=202793
+
+        Reviewed by Darin Adler.
+
+        Adjust test expectations.
+
+        * web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.basic.worker-expected.txt:
+        * web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.complex.worker-expected.txt:
+        * web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.invalid.worker-expected.txt:
+        * web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.tiny.worker-expected.txt:
+
 2021-04-02  Sergio Villar Senin  <[email protected]>
 
         [css-flexbox] align-content issues

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.basic.worker-expected.txt (275419 => 275420)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.basic.worker-expected.txt	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.basic.worker-expected.txt	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,3 @@
 
-FAIL 2d assert_equals: ctx.font === '20px serif' (got 10px sans-serif[string], expected 20px serif[string]) expected "20px serif" but got "10px sans-serif"
+PASS 2d
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.complex.worker-expected.txt (275419 => 275420)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.complex.worker-expected.txt	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.complex.worker-expected.txt	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,3 @@
 
-FAIL 2d assert_equals: ctx.font === 'italic small-caps 12px "Unknown Font", sans-serif' (got 10px sans-serif[string], expected italic small-caps 12px "Unknown Font", sans-serif[string]) expected "italic small-caps 12px \"Unknown Font\", sans-serif" but got "10px sans-serif"
+PASS 2d
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.invalid.worker-expected.txt (275419 => 275420)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.invalid.worker-expected.txt	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.invalid.worker-expected.txt	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,3 @@
 
-FAIL 2d assert_equals: ctx.font === '20px serif' (got 10px sans-serif[string], expected 20px serif[string]) expected "20px serif" but got "10px sans-serif"
+PASS 2d
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.tiny.worker-expected.txt (275419 => 275420)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.tiny.worker-expected.txt	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/offscreen/text/2d.text.font.parse.tiny.worker-expected.txt	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,3 @@
 
-FAIL 2d assert_equals: ctx.font === '1px sans-serif' (got 10px sans-serif[string], expected 1px sans-serif[string]) expected "1px sans-serif" but got "10px sans-serif"
+PASS 2d
 

Modified: trunk/LayoutTests/platform/glib/TestExpectations (275419 => 275420)


--- trunk/LayoutTests/platform/glib/TestExpectations	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/LayoutTests/platform/glib/TestExpectations	2021-04-02 14:06:16 UTC (rev 275420)
@@ -654,7 +654,6 @@
 webkit.org/b/203146 imported/w3c/web-platform-tests/html/canvas/offscreen [ Pass ]
 webkit.org/b/206055 imported/w3c/web-platform-tests/html/canvas/offscreen/drawing-images-to-the-canvas/2d.drawImage.svg.worker.html [ Skip ]
 webkit.org/b/206055 imported/w3c/web-platform-tests/html/canvas/offscreen/drawing-images-to-the-canvas/2d.drawImage.zerosource.image.worker.html [ Skip ]
-webkit.org/b/223671 imported/w3c/web-platform-tests/html/canvas/offscreen/manual/draw-generic-family/2d.text.draw.generic.family.w.html [ Failure ]
 
 # Console log lines may appear in a different order so we silence them.
 imported/w3c/web-platform-tests/html/canvas/offscreen/convert-to-blob/offscreencanvas.convert.to.blob.w.html [ DumpJSConsoleLogInStdErr ]

Modified: trunk/Source/WebCore/ChangeLog (275419 => 275420)


--- trunk/Source/WebCore/ChangeLog	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/ChangeLog	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1,3 +1,81 @@
+2021-04-02  Chris Lord  <[email protected]>
+
+        Implement text rendering on OffscreenCanvas in a Worker
+        https://bugs.webkit.org/show_bug.cgi?id=202793
+
+        Reviewed by Darin Adler.
+
+        Add a CSSFontSelector and FontCache to WorkerGlobalScope and via
+        interface changes to ScriptExecutionContext and FontSelector, use them
+        as appropriate after enabling text functions on Worker threads with
+        OffscreenCanvas.
+
+        No new tests, rebaselined existing tests.
+
+        * css/CSSFontSelector.cpp:
+        (WebCore::CSSFontSelector::CSSFontSelector):
+        (WebCore::m_version):
+        (WebCore::CSSFontSelector::~CSSFontSelector):
+        (WebCore::CSSFontSelector::fontRangesForFamily):
+        (WebCore::CSSFontSelector::fallbackFontAt):
+        * css/CSSFontSelector.h:
+        * css/parser/CSSPropertyParserHelpers.cpp:
+        (WebCore::CSSPropertyParserHelpers::genericFontFamily):
+        (WebCore::CSSPropertyParserHelpers::genericFontFamilyIndex):
+        * css/parser/CSSPropertyParserHelpers.h:
+        * dom/Document.h:
+        * dom/ScriptExecutionContext.cpp:
+        (WebCore::ScriptExecutionContext::fontCache):
+        * dom/ScriptExecutionContext.h:
+        (WebCore::ScriptExecutionContext::cssFontSelector):
+        * dom/make_names.pl:
+        * html/canvas/CanvasRenderingContext2DBase.cpp:
+        (WebCore::CanvasRenderingContext2DBase::canDrawText):
+        * html/canvas/OffscreenCanvasRenderingContext2D.cpp:
+        (WebCore::OffscreenCanvasRenderingContext2D::setFont):
+        * platform/graphics/FontCache.cpp:
+        (WebCore::FontDataCacheKeyHash::hash):
+        (WebCore::FontDataCacheKeyHash::equal):
+        (WebCore::FontDataCacheKeyTraits::emptyValue):
+        (WebCore::FontDataCacheKeyTraits::constructDeletedValue):
+        (WebCore::FontDataCacheKeyTraits::isDeletedValue):
+        (WebCore::FontCache::create):
+        (WebCore::FontCache::singleton):
+        (WebCore::FontCache::FontCache):
+        (WebCore::FontCache::getCachedFontPlatformData):
+        (WebCore::FontCache::fontForPlatformData):
+        (WebCore::FontCache::purgeInactiveFontDataIfNeeded):
+        (WebCore::FontCache::purgeInactiveFontData):
+        (WebCore::FontCache::verticalData):
+        (WebCore::FontCache::fontCount):
+        (WebCore::FontCache::inactiveFontCount):
+        (WebCore::FontCache::invalidate):
+        * platform/graphics/FontCache.h:
+        (WebCore::FontCache::fontCacheFallbackToSingleton):
+        * platform/graphics/FontCascade.cpp:
+        (WebCore::FontCascade::isCurrent const):
+        (WebCore::FontCascade::update const):
+        * platform/graphics/FontCascadeFonts.cpp:
+        (WebCore::FontCascadeFonts::FontCascadeFonts):
+        (WebCore::realizeNextFallback):
+        (WebCore::FontCascadeFonts::realizeFallbackRangesAt):
+        * platform/graphics/FontSelector.h:
+        * platform/graphics/cairo/FontCairoHarfbuzzNG.cpp:
+        (WebCore::FontCascade::fontForCombiningCharacterSequence const):
+        * platform/graphics/freetype/FontCacheFreeType.cpp:
+        (WebCore::getFamilyNameStringFromFamily):
+        * style/StyleBuilderCustom.h:
+        (WebCore::Style::BuilderCustom::applyValueFontFamily):
+        * style/StyleResolveForFontRaw.cpp:
+        (WebCore::Style::useFixedDefaultSize):
+        (WebCore::Style::resolveForFontRaw):
+        * style/StyleResolveForFontRaw.h:
+        * workers/WorkerGlobalScope.cpp:
+        (WebCore::WorkerGlobalScope::~WorkerGlobalScope):
+        (WebCore::WorkerGlobalScope::cssFontSelector):
+        (WebCore::WorkerGlobalScope::fontCache):
+        * workers/WorkerGlobalScope.h:
+
 2021-04-02  Youenn Fablet  <[email protected]>
 
         No audio output when unplugged wired headset during a call

Modified: trunk/Source/WebCore/css/CSSFontSelector.cpp (275419 => 275420)


--- trunk/Source/WebCore/css/CSSFontSelector.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/css/CSSFontSelector.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -52,7 +52,6 @@
 #include "StyleProperties.h"
 #include "StyleResolver.h"
 #include "StyleRule.h"
-#include "WebKitFontFamilyNames.h"
 #include <wtf/Ref.h>
 #include <wtf/SetForScope.h>
 #include <wtf/text/AtomString.h>
@@ -66,6 +65,7 @@
 CSSFontSelector::CSSFontSelector(ScriptExecutionContext& context)
     : ActiveDOMObject(&context)
     , m_context(makeWeakPtr(context))
+    , m_fontCache(makeRef(context.fontCache()))
     , m_cssFontFaceSet(CSSFontFaceSet::create(this))
     , m_fontModifiedObserver([this] { fontModified(); })
     , m_fontLoadingTimer(*this, &CSSFontSelector::fontLoadingTimerFired)
@@ -82,7 +82,7 @@
             m_fontFamilyNames.uncheckedAppend(familyName);
     }
 
-    FontCache::singleton().addClient(*this);
+    m_fontCache->addClient(*this);
     m_cssFontFaceSet->addFontModifiedObserver(m_fontModifiedObserver);
     LOG(Fonts, "CSSFontSelector %p ctor", this);
 
@@ -94,7 +94,7 @@
     LOG(Fonts, "CSSFontSelector %p dtor", this);
 
     stopLoadingAndClearFonts();
-    FontCache::singleton().removeClient(*this);
+    m_fontCache->removeClient(*this);
 }
 
 FontFaceSet* CSSFontSelector::fontFaceSetIfExists()
@@ -308,7 +308,7 @@
     ASSERT(!m_buildIsUnderway || m_computingRootStyleFontCount);
 
     // FIXME: The spec (and Firefox) says user specified generic families (sans-serif etc.) should be resolved before the @font-face lookup too.
-    bool resolveGenericFamilyFirst = familyName == m_fontFamilyNames[static_cast<int>(FamilyNamesIndex::StandardFamily)];
+    bool resolveGenericFamilyFirst = familyName == m_fontFamilyNames.at(FamilyNamesIndex::StandardFamily);
 
     AtomString familyForLookup = familyName;
     Optional<FontDescription> overrideFontDescription;
@@ -330,7 +330,7 @@
 
     if (!resolveGenericFamilyFirst)
         resolveAndAssignGenericFamily();
-    auto font = FontCache::singleton().fontForFamily(*fontDescriptionForLookup, familyForLookup);
+    auto font = m_fontCache->fontForFamily(*fontDescriptionForLookup, familyForLookup);
     if (document && RuntimeEnabledFeatures::sharedFeatures().webAPIStatisticsEnabled())
         ResourceLoadObserver::shared().logFontLoad(*document, familyForLookup.string(), !!font);
     return FontRanges { WTFMove(font) };
@@ -442,7 +442,7 @@
     if (!m_context->settingsValues().fontFallbackPrefersPictographs)
         return nullptr;
     auto& pictographFontFamily = m_context->settingsValues().fontGenericFamilies.pictographFontFamily();
-    auto font = FontCache::singleton().fontForFamily(fontDescription, pictographFontFamily);
+    auto font = m_fontCache->fontForFamily(fontDescription, pictographFontFamily);
     if (RuntimeEnabledFeatures::sharedFeatures().webAPIStatisticsEnabled() && is<Document>(m_context.get()))
         ResourceLoadObserver::shared().logFontLoad(downcast<Document>(*m_context), pictographFontFamily, !!font);
 

Modified: trunk/Source/WebCore/css/CSSFontSelector.h (275419 => 275420)


--- trunk/Source/WebCore/css/CSSFontSelector.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/css/CSSFontSelector.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -32,6 +32,7 @@
 #include "Font.h"
 #include "FontSelector.h"
 #include "Timer.h"
+#include "WebKitFontFamilyNames.h"
 #include <memory>
 #include <wtf/Forward.h>
 #include <wtf/HashSet.h>
@@ -70,6 +71,7 @@
 
     void addFontFaceRule(StyleRuleFontFace&, bool isInitiatingElementInUserAgentShadowTree);
 
+    FontCache& fontCache() const final { return m_fontCache.get(); }
     void fontCacheInvalidated() final;
 
     bool isEmpty() const;
@@ -123,6 +125,7 @@
     Vector<PendingFontFaceRule> m_stagingArea;
 
     WeakPtr<ScriptExecutionContext> m_context;
+    Ref<FontCache> m_fontCache;
     RefPtr<FontFaceSet> m_fontFaceSet;
     Ref<CSSFontFaceSet> m_cssFontFaceSet;
     HashSet<FontSelectorClient*> m_clients;
@@ -143,7 +146,7 @@
     bool m_buildIsUnderway { false };
     bool m_isStopped { false };
 
-    WTF::Vector<AtomString> m_fontFamilyNames;
+    WebKitFontFamilyNames::FamilyNamesList<AtomString> m_fontFamilyNames;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp (275419 => 275420)


--- trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -46,6 +46,7 @@
 #include "Pair.h"
 #include "RuntimeEnabledFeatures.h"
 #include "StyleColor.h"
+#include "WebKitFontFamilyNames.h"
 #include <wtf/text/StringConcatenateNumbers.h>
 
 namespace WebCore {
@@ -3061,7 +3062,7 @@
     return result;
 }
 
-const AtomString& genericFontFamilyFromValueID(CSSValueID ident)
+const AtomString& genericFontFamily(CSSValueID ident)
 {
     switch (ident) {
     case CSSValueSerif:
@@ -3083,6 +3084,29 @@
     }
 }
 
+WebKitFontFamilyNames::FamilyNamesIndex genericFontFamilyIndex(CSSValueID ident)
+{
+    switch (ident) {
+    case CSSValueSerif:
+        return WebKitFontFamilyNames::FamilyNamesIndex::SerifFamily;
+    case CSSValueSansSerif:
+        return WebKitFontFamilyNames::FamilyNamesIndex::SansSerifFamily;
+    case CSSValueCursive:
+        return WebKitFontFamilyNames::FamilyNamesIndex::CursiveFamily;
+    case CSSValueFantasy:
+        return WebKitFontFamilyNames::FamilyNamesIndex::FantasyFamily;
+    case CSSValueMonospace:
+        return WebKitFontFamilyNames::FamilyNamesIndex::MonospaceFamily;
+    case CSSValueWebkitPictograph:
+        return WebKitFontFamilyNames::FamilyNamesIndex::PictographFamily;
+    case CSSValueSystemUi:
+        return WebKitFontFamilyNames::FamilyNamesIndex::SystemUiFamily;
+    default:
+        ASSERT_NOT_REACHED();
+        return WebKitFontFamilyNames::FamilyNamesIndex::StandardFamily;
+    }
+}
+
 } // namespace CSSPropertyParserHelpers
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.h (275419 => 275420)


--- trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -42,6 +42,10 @@
 
 namespace WebCore {
 
+namespace WebKitFontFamilyNames {
+enum class FamilyNamesIndex;
+}
+
 // When these functions are successful, they will consume all the relevant
 // tokens from the range and also consume any whitespace which follows. When
 // the start of the range doesn't match the type we're looking for, the range
@@ -169,7 +173,8 @@
 Optional<FontSizeRaw> consumeFontSizeRaw(CSSParserTokenRange&, CSSParserMode, UnitlessQuirk = UnitlessQuirk::Forbid);
 Optional<LineHeightRaw> consumeLineHeightRaw(CSSParserTokenRange&, CSSParserMode);
 Optional<FontRaw> consumeFontWorkerSafe(CSSParserTokenRange&, CSSParserMode);
-const AtomString& genericFontFamilyFromValueID(CSSValueID);
+const AtomString& genericFontFamily(CSSValueID);
+WebKitFontFamilyNames::FamilyNamesIndex genericFontFamilyIndex(CSSValueID);
 
 bool isFontStyleAngleInRange(double angleInDegrees);
 

Modified: trunk/Source/WebCore/dom/Document.h (275419 => 275420)


--- trunk/Source/WebCore/dom/Document.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/dom/Document.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1645,6 +1645,9 @@
     void createRenderTree();
     void detachParser();
 
+    // ScriptExecutionContext
+    CSSFontSelector* cssFontSelector() final { return m_fontSelector.ptr(); }
+
     // FontSelectorClient
     void fontsNeedUpdate(FontSelector&) final;
 

Modified: trunk/Source/WebCore/dom/ScriptExecutionContext.cpp (275419 => 275420)


--- trunk/Source/WebCore/dom/ScriptExecutionContext.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/dom/ScriptExecutionContext.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -35,6 +35,7 @@
 #include "DatabaseContext.h"
 #include "Document.h"
 #include "ErrorEvent.h"
+#include "FontCache.h"
 #include "JSDOMExceptionHandling.h"
 #include "JSDOMWindow.h"
 #include "JSWorkerGlobalScope.h"
@@ -226,6 +227,11 @@
 {
 }
 
+FontCache& ScriptExecutionContext::fontCache()
+{
+    return FontCache::singleton();
+}
+
 void ScriptExecutionContext::forEachActiveDOMObject(const Function<ShouldContinue(ActiveDOMObject&)>& apply) const
 {
     // It is not allowed to run arbitrary script or construct new ActiveDOMObjects while we are iterating over ActiveDOMObjects.

Modified: trunk/Source/WebCore/dom/ScriptExecutionContext.h (275419 => 275420)


--- trunk/Source/WebCore/dom/ScriptExecutionContext.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/dom/ScriptExecutionContext.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -60,10 +60,12 @@
 
 class EventLoop;
 class CachedScript;
+class CSSFontSelector;
 class DatabaseContext;
 class EventQueue;
 class EventLoopTaskGroup;
 class EventTarget;
+class FontCache;
 class MessagePort;
 class PublicURLManager;
 class RejectedPromiseTracker;
@@ -161,6 +163,9 @@
 
     virtual void didLoadResourceSynchronously(const URL&);
 
+    virtual FontCache& fontCache();
+    virtual CSSFontSelector* cssFontSelector() { return nullptr; }
+
     void ref() { refScriptExecutionContext(); }
     void deref() { derefScriptExecutionContext(); }
 

Modified: trunk/Source/WebCore/dom/make_names.pl (275419 => 275420)


--- trunk/Source/WebCore/dom/make_names.pl	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/dom/make_names.pl	2021-04-02 14:06:16 UTC (rev 275420)
@@ -105,15 +105,24 @@
 #include <wtf/text/AtomString.h>
 END
 
-    print F "extern LazyNeverDestroyed<Vector<const StaticStringImpl*, ", scalar(keys %parameters), ">> familyNamesData;\n";
-    print F "extern MainThreadLazyNeverDestroyed<Vector<AtomStringImpl*, ", scalar(keys %parameters), ">> familyNames;\n\n";
-    printMacros($F, "extern MainThreadLazyNeverDestroyed<const AtomString>", "", \%parameters);
-    print F "\n";
     print F "enum class FamilyNamesIndex {\n";
     for my $name (sort keys %parameters) {
         print F "    ", ucfirst(${name}), ",\n";
     }
     print F "};\n\n";
+
+    print F "template<typename T, size_t inlineCapacity = 0>\n";
+    print F "class FamilyNamesList : public Vector<T, inlineCapacity> {\n";
+    print F "public:\n";
+    print F "    T& at(FamilyNamesIndex i)\n";
+    print F "    {\n";
+    print F "        return Vector<T, inlineCapacity>::at(static_cast<size_t>(i));\n";
+    print F "    }\n";
+    print F "};\n\n";
+    print F "extern LazyNeverDestroyed<FamilyNamesList<const StaticStringImpl*, ", scalar(keys %parameters), ">> familyNamesData;\n";
+    print F "extern MainThreadLazyNeverDestroyed<FamilyNamesList<AtomStringImpl*, ", scalar(keys %parameters), ">> familyNames;\n\n";
+    printMacros($F, "extern MainThreadLazyNeverDestroyed<const AtomString>", "", \%parameters);
+    print F "\n";
     print F "#endif\n\n";
 
     printInit($F, 1);
@@ -127,8 +136,8 @@
 
     print F StaticString::GenerateStrings(\%parameters);
 
-    print F "LazyNeverDestroyed<Vector<const StaticStringImpl*, ", scalar(keys %parameters), ">> familyNamesData;\n";
-    print F "MainThreadLazyNeverDestroyed<Vector<AtomStringImpl*, ", scalar(keys %parameters), ">> familyNames;\n\n";
+    print F "LazyNeverDestroyed<FamilyNamesList<const StaticStringImpl*, ", scalar(keys %parameters), ">> familyNamesData;\n";
+    print F "MainThreadLazyNeverDestroyed<FamilyNamesList<AtomStringImpl*, ", scalar(keys %parameters), ">> familyNames;\n\n";
 
     printMacros($F, "MainThreadLazyNeverDestroyed<const AtomString>", "", \%parameters);
 

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp (275419 => 275420)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -2366,8 +2366,6 @@
     if (!fontProxy()->realized())
         return false;
 
-    ScriptDisallowedScope::InMainThread scriptDisallowedScope;
-
     auto* c = drawingContext();
     if (!c)
         return false;

Modified: trunk/Source/WebCore/html/canvas/OffscreenCanvasRenderingContext2D.cpp (275419 => 275420)


--- trunk/Source/WebCore/html/canvas/OffscreenCanvasRenderingContext2D.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/html/canvas/OffscreenCanvasRenderingContext2D.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -38,8 +38,8 @@
 #include "CSSFontSelector.h"
 #include "CSSParser.h"
 #include "CSSPropertyParserHelpers.h"
-#include "Document.h"
 #include "RenderStyle.h"
+#include "ScriptExecutionContext.h"
 #include "StyleResolveForFontRaw.h"
 #include "TextMetrics.h"
 #include <wtf/IsoMallocInlines.h>
@@ -63,8 +63,6 @@
 void OffscreenCanvasRenderingContext2D::setFont(const String& newFont)
 {
     auto& context = *canvasBase().scriptExecutionContext();
-    if (!is<Document>(context))
-        return;
 
     if (newFont.isEmpty())
         return;
@@ -90,11 +88,10 @@
     fontDescription.setSpecifiedSize(DefaultFontSize);
     fontDescription.setComputedSize(DefaultFontSize);
 
-    auto& document = downcast<Document>(context);
-    auto fontCascade = Style::resolveForFontRaw(*fontRaw, WTFMove(fontDescription), document);
-
-    if (fontCascade)
-        modifiableState().font.initialize(document.fontSelector(), *fontCascade);
+    if (auto fontCascade = Style::resolveForFontRaw(*fontRaw, WTFMove(fontDescription), context)) {
+        ASSERT(context.cssFontSelector());
+        modifiableState().font.initialize(*context.cssFontSelector(), *fontCascade);
+    }
 }
 
 CanvasDirection OffscreenCanvasRenderingContext2D::direction() const

Modified: trunk/Source/WebCore/platform/graphics/FontCache.cpp (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -49,22 +49,6 @@
 namespace WebCore {
 using namespace WTF;
 
-FontCache& FontCache::singleton()
-{
-    static NeverDestroyed<FontCache> globalFontCache;
-    return globalFontCache;
-}
-
-FontCache::FontCache()
-    : m_purgeTimer(*this, &FontCache::purgeInactiveFontDataIfNeeded)
-    , m_fontPlatformDataCache(makeUniqueRef<FontPlatformDataCache>())
-    , m_fontDataCache(makeUniqueRef<FontDataCache>())
-#if ENABLE(OPENTYPE_VERTICAL)
-    , m_fontVerticalDataCache(makeUniqueRef<FontVerticalDataCache>())
-#endif
-{
-}
-
 struct FontPlatformDataCacheKey {
     WTF_MAKE_FAST_ALLOCATED;
 public:
@@ -127,13 +111,80 @@
         return a == b;
     }
 
-    static const bool safeToCompareToEmptyOrDeleted = true;
+    static constexpr bool safeToCompareToEmptyOrDeleted = true;
 };
 
 struct FontPlatformDataCacheKeyHashTraits : public SimpleClassHashTraits<FontPlatformDataCacheKey> {
-    static const bool emptyValueIsZero = false;
+    static constexpr bool emptyValueIsZero = false;
 };
 
+struct FontDataCacheKeyHash {
+    static unsigned hash(const FontPlatformData& platformData)
+    {
+        return platformData.hash();
+    }
+
+    static bool equal(const FontPlatformData& a, const FontPlatformData& b)
+    {
+        return a == b;
+    }
+
+    static constexpr bool safeToCompareToEmptyOrDeleted = true;
+};
+
+struct FontDataCacheKeyTraits : WTF::GenericHashTraits<FontPlatformData> {
+    static constexpr bool emptyValueIsZero = true;
+    static const FontPlatformData& emptyValue()
+    {
+        static NeverDestroyed<FontPlatformData> key(0.f, false, false);
+        return key;
+    }
+    static void constructDeletedValue(FontPlatformData& slot)
+    {
+        new (NotNull, &slot) FontPlatformData(HashTableDeletedValue);
+    }
+    static bool isDeletedValue(const FontPlatformData& value)
+    {
+        return value.isHashTableDeletedValue();
+    }
+};
+
+using FontPlatformDataCache = HashMap<FontPlatformDataCacheKey, std::unique_ptr<FontPlatformData>, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyHashTraits>;
+using FontDataCache = HashMap<FontPlatformData, Ref<Font>, FontDataCacheKeyHash, FontDataCacheKeyTraits>;
+#if ENABLE(OPENTYPE_VERTICAL)
+using FontVerticalDataCache = HashMap<FontPlatformData, RefPtr<OpenTypeVerticalData>, FontDataCacheKeyHash, FontDataCacheKeyTraits>;
+#endif
+
+struct FontCache::FontDataCaches {
+    WTF_MAKE_STRUCT_FAST_ALLOCATED;
+
+    FontDataCache data;
+    FontPlatformDataCache platformData;
+#if ENABLE(OPENTYPE_VERTICAL)
+    FontVerticalDataCache verticalData;
+#endif
+};
+
+Ref<FontCache> FontCache::create()
+{
+    ASSERT(!isMainThread());
+    return adoptRef(*new FontCache());
+}
+
+FontCache& FontCache::singleton()
+{
+    static MainThreadNeverDestroyed<FontCache> globalFontCache;
+    return globalFontCache;
+}
+
+FontCache::~FontCache() = default;
+
+FontCache::FontCache()
+    : m_purgeTimer(*this, &FontCache::purgeInactiveFontDataIfNeeded)
+    , m_fontDataCaches(makeUniqueRef<FontDataCaches>())
+{
+}
+
 Optional<ASCIILiteral> FontCache::alternateFamilyName(const String& familyName)
 {
     if (auto platformSpecificAlternate = platformAlternateFamilyName(familyName))
@@ -197,7 +248,7 @@
 
     FontPlatformDataCacheKey key(familyName, fontDescription, fontFaceFeatures, fontFaceCapabilities);
 
-    auto addResult = m_fontPlatformDataCache->add(key, nullptr);
+    auto addResult = m_fontDataCaches->platformData.add(key, nullptr);
     FontPlatformDataCache::iterator it = addResult.iterator;
     if (addResult.isNewEntry) {
         it->value = createFontPlatformData(fontDescription, familyName, fontFaceFeatures, fontFaceCapabilities);
@@ -209,8 +260,8 @@
                 auto* fontPlatformDataForAlternateName = getCachedFontPlatformData(fontDescription, *alternateName, fontFaceFeatures, fontFaceCapabilities, true);
                 // Lookup the key in the hash table again as the previous iterator may have
                 // been invalidated by the recursive call to getCachedFontPlatformData().
-                it = m_fontPlatformDataCache->find(key);
-                ASSERT(it != m_fontPlatformDataCache->end());
+                it = m_fontDataCaches->platformData.find(key);
+                ASSERT(it != m_fontDataCaches->platformData.end());
                 if (fontPlatformDataForAlternateName)
                     it->value = makeUnique<FontPlatformData>(*fontPlatformDataForAlternateName);
             }
@@ -220,47 +271,6 @@
     return it->value.get();
 }
 
-struct FontDataCacheKeyHash {
-    static unsigned hash(const FontPlatformData& platformData)
-    {
-        return platformData.hash();
-    }
-
-    static bool equal(const FontPlatformData& a, const FontPlatformData& b)
-    {
-        return a == b;
-    }
-
-    static const bool safeToCompareToEmptyOrDeleted = true;
-};
-
-struct FontDataCacheKeyTraits : WTF::GenericHashTraits<FontPlatformData> {
-    static const bool emptyValueIsZero = true;
-    static const FontPlatformData& emptyValue()
-    {
-        static NeverDestroyed<FontPlatformData> key(0.f, false, false);
-        return key;
-    }
-    static void constructDeletedValue(FontPlatformData& slot)
-    {
-        new (NotNull, &slot) FontPlatformData(HashTableDeletedValue);
-    }
-    static bool isDeletedValue(const FontPlatformData& value)
-    {
-        return value.isHashTableDeletedValue();
-    }
-};
-
-#if ENABLE(OPENTYPE_VERTICAL)
-RefPtr<OpenTypeVerticalData> FontCache::verticalData(const FontPlatformData& platformData)
-{
-    auto addResult = m_fontVerticalDataCache->ensure(platformData, [&platformData] {
-        return OpenTypeVerticalData::create(platformData);
-    });
-    return addResult.iterator->value;
-}
-#endif
-
 #if PLATFORM(IOS_FAMILY)
 const unsigned cMaxInactiveFontData = 120;
 const unsigned cTargetInactiveFontData = 100;
@@ -289,7 +299,7 @@
     auto locker = holdLock(m_fontLock);
 #endif
 
-    auto addResult = m_fontDataCache->ensure(platformData, [&] {
+    auto addResult = m_fontDataCaches->data.ensure(platformData, [&] {
         return Font::create(platformData);
     });
 
@@ -305,7 +315,7 @@
 
     LOG(Fonts, "FontCache::purgeInactiveFontDataIfNeeded() - underMemoryPressure %d, inactiveFontDataLimit %u", underMemoryPressure, inactiveFontDataLimit);
 
-    if (m_fontDataCache->size() < inactiveFontDataLimit)
+    if (m_fontDataCaches->data.size() < inactiveFontDataLimit)
         return;
     unsigned inactiveCount = inactiveFontCount();
     if (inactiveCount <= inactiveFontDataLimit)
@@ -328,7 +338,7 @@
 
     while (purgeCount) {
         Vector<Ref<Font>, 20> fontsToDelete;
-        for (auto& font : m_fontDataCache->values()) {
+        for (auto& font : m_fontDataCaches->data.values()) {
             LOG(Fonts, " trying to purge font %s (has one ref %d)", font->platformData().description().utf8().data(), font->hasOneRef());
             if (!font->hasOneRef())
                 continue;
@@ -340,18 +350,18 @@
         if (fontsToDelete.isEmpty())
             break;
         for (auto& font : fontsToDelete) {
-            bool success = m_fontDataCache->remove(font->platformData());
+            bool success = m_fontDataCaches->data.remove(font->platformData());
             ASSERT_UNUSED(success, success);
 #if ENABLE(OPENTYPE_VERTICAL)
-            m_fontVerticalDataCache->remove(font->platformData());
+            m_fontDataCaches->verticalData.remove(font->platformData());
 #endif
         }
     };
 
     Vector<FontPlatformDataCacheKey> keysToRemove;
-    keysToRemove.reserveInitialCapacity(m_fontPlatformDataCache->size());
-    for (auto& entry : m_fontPlatformDataCache.get()) {
-        if (entry.value && !m_fontDataCache->contains(*entry.value))
+    keysToRemove.reserveInitialCapacity(m_fontDataCaches->platformData.size());
+    for (auto& entry : m_fontDataCaches->platformData) {
+        if (entry.value && !m_fontDataCaches->data.contains(*entry.value))
             keysToRemove.uncheckedAppend(entry.key);
     }
 
@@ -358,7 +368,7 @@
     LOG(Fonts, " removing %lu keys", keysToRemove.size());
 
     for (auto& key : keysToRemove)
-        m_fontPlatformDataCache->remove(key);
+        m_fontDataCaches->platformData.remove(key);
 
     platformPurgeInactiveFontData();
 }
@@ -402,6 +412,16 @@
         value->fonts.get().widthCache().clear();
 }
 
+#if ENABLE(OPENTYPE_VERTICAL)
+RefPtr<OpenTypeVerticalData> FontCache::verticalData(const FontPlatformData& platformData)
+{
+    auto addResult = m_fontDataCaches->verticalData.ensure(platformData, [&platformData] {
+        return OpenTypeVerticalData::create(platformData);
+    });
+    return addResult.iterator->value;
+}
+#endif
+
 static FontCascadeCacheKey makeFontCascadeCacheKey(const FontCascadeDescription& description, FontSelector* fontSelector)
 {
     FontCascadeCacheKey key;
@@ -458,7 +478,7 @@
 
 size_t FontCache::fontCount()
 {
-    return m_fontDataCache->size();
+    return m_fontDataCaches->data.size();
 }
 
 size_t FontCache::inactiveFontCount()
@@ -467,7 +487,7 @@
     auto locker = holdLock(m_fontLock);
 #endif
     unsigned count = 0;
-    for (auto& font : m_fontDataCache->values()) {
+    for (auto& font : m_fontDataCaches->data.values()) {
         if (font->hasOneRef())
             ++count;
     }
@@ -489,9 +509,9 @@
 
 void FontCache::invalidate()
 {
-    m_fontPlatformDataCache->clear();
+    m_fontDataCaches->platformData.clear();
 #if ENABLE(OPENTYPE_VERTICAL)
-    m_fontVerticalDataCache->clear();
+    m_fontDataCaches->verticalData.clear();
 #endif
     invalidateFontCascadeCache();
 

Modified: trunk/Source/WebCore/platform/graphics/FontCache.h (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/FontCache.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/FontCache.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -32,6 +32,7 @@
 #include "FontCascadeFonts.h"
 #include "FontDescription.h"
 #include "FontPlatformData.h"
+#include "FontSelector.h"
 #include "FontTaggedSettings.h"
 #include "Timer.h"
 #include <array>
@@ -64,15 +65,9 @@
 namespace WebCore {
 
 class FontCascade;
-class FontSelector;
 class OpenTypeVerticalData;
 class Font;
 enum class IsForPlatformFont : uint8_t;
-struct FontDataCacheKeyHash;
-struct FontDataCacheKeyTraits;
-struct FontPlatformDataCacheKey;
-struct FontPlatformDataCacheKeyHash;
-struct FontPlatformDataCacheKeyHashTraits;
 
 #if PLATFORM(WIN)
 #if USE(IMLANG_FONT_LINK2)
@@ -218,21 +213,17 @@
 
 using FontCascadeCache = HashMap<FontCascadeCacheKey, std::unique_ptr<FontCascadeCacheEntry>, FontCascadeCacheKeyHash, FontCascadeCacheKeyHashTraits>;
 
-typedef HashMap<FontPlatformDataCacheKey, std::unique_ptr<FontPlatformData>, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyHashTraits> FontPlatformDataCache;
-typedef HashMap<FontPlatformData, Ref<Font>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontDataCache;
-#if ENABLE(OPENTYPE_VERTICAL)
-typedef HashMap<FontPlatformData, RefPtr<OpenTypeVerticalData>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontVerticalDataCache;
-#endif
-
 class FontCache : public RefCounted<FontCache> {
-    friend class WTF::NeverDestroyed<FontCache>;
+    friend class WTF::NeverDestroyed<FontCache, MainThreadAccessTraits>;
 
     WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED;
 public:
-    FontCache();
-
+    static Ref<FontCache> create();
     WEBCORE_EXPORT static FontCache& singleton();
+    static FontCache& fontCacheFallbackToSingleton(RefPtr<FontSelector>);
 
+    ~FontCache();
+
     // These methods are implemented by the platform.
     enum class PreferColoredFont : uint8_t { No, Yes };
     RefPtr<Font> systemFallbackForCharacters(const FontDescription&, const Font* originalFontData, IsForPlatformFont, PreferColoredFont, const UChar* characters, unsigned length);
@@ -303,6 +294,8 @@
     void prewarmGlobally();
 
 private:
+    FontCache();
+
     WEBCORE_EXPORT void purgeInactiveFontDataIfNeeded();
     void pruneUnreferencedEntriesFromFontCascadeCache();
     void pruneSystemFallbackFonts();
@@ -322,11 +315,8 @@
     bool m_shouldMockBoldSystemFontForAccessibility { false };
 
     HashSet<FontSelector*> m_clients;
-    UniqueRef<FontPlatformDataCache> m_fontPlatformDataCache;
-    UniqueRef<FontDataCache> m_fontDataCache;
-#if ENABLE(OPENTYPE_VERTICAL)
-    UniqueRef<FontVerticalDataCache> m_fontVerticalDataCache;
-#endif
+    struct FontDataCaches;
+    UniqueRef<FontDataCaches> m_fontDataCaches;
     FontCascadeCache m_fontCascadeCache;
 
     unsigned short m_generation { 0 };
@@ -388,4 +378,9 @@
     return prewarmInformation;
 }
 
+inline FontCache& FontCache::fontCacheFallbackToSingleton(RefPtr<FontSelector> fontSelector)
+{
+    return fontSelector ? fontSelector->fontCache() : FontCache::singleton();
 }
+
+}

Modified: trunk/Source/WebCore/platform/graphics/FontCascade.cpp (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/FontCascade.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/FontCascade.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -151,7 +151,7 @@
 {
     if (!m_fonts)
         return false;
-    if (m_fonts->generation() != FontCache::singleton().generation())
+    if (m_fonts->generation() != fontSelector.fontCache().generation())
         return false;
     if (m_fonts->fontSelectorVersion() != fontSelector.version())
         return false;
@@ -169,7 +169,7 @@
 
 void FontCascade::update(RefPtr<FontSelector>&& fontSelector) const
 {
-    FontCache::singleton().updateFontCascade(*this, WTFMove(fontSelector));
+    FontCache::fontCacheFallbackToSingleton(fontSelector).updateFontCascade(*this, WTFMove(fontSelector));
 }
 
 GlyphBuffer FontCascade::layoutText(CodePath codePathToUse, const TextRun& run, unsigned from, unsigned to, ForTextEmphasisOrNot forTextEmphasis) const

Modified: trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -102,7 +102,7 @@
     : m_cachedPrimaryFont(nullptr)
     , m_fontSelector(fontSelector)
     , m_fontSelectorVersion(m_fontSelector ? m_fontSelector->version() : 0)
-    , m_generation(FontCache::singleton().generation())
+    , m_generation(FontCache::fontCacheFallbackToSingleton(m_fontSelector).generation())
 {
 #if ASSERT_ENABLED
     if (!isMainThread())
@@ -144,7 +144,7 @@
 {
     ASSERT(index < description.effectiveFamilyCount());
 
-    auto& fontCache = FontCache::singleton();
+    auto& fontCache = FontCache::fontCacheFallbackToSingleton(fontSelector);
     while (index < description.effectiveFamilyCount()) {
         auto visitor = WTF::makeVisitor([&](const AtomString& family) -> FontRanges {
             if (family.isEmpty())
@@ -181,7 +181,7 @@
         return m_realizedFallbackRanges[index];
 
     ASSERT(index == m_realizedFallbackRanges.size());
-    ASSERT(FontCache::singleton().generation() == m_generation);
+    ASSERT(FontCache::fontCacheFallbackToSingleton(m_fontSelector).generation() == m_generation);
 
     m_realizedFallbackRanges.append(FontRanges());
     auto& fontRanges = m_realizedFallbackRanges.last();
@@ -191,7 +191,7 @@
         if (fontRanges.isNull() && m_fontSelector)
             fontRanges = m_fontSelector->fontRangesForFamily(description, standardFamily);
         if (fontRanges.isNull())
-            fontRanges = FontRanges(FontCache::singleton().lastResortFallbackFont(description));
+            fontRanges = FontRanges(FontCache::fontCacheFallbackToSingleton(m_fontSelector).lastResortFallbackFont(description));
         return fontRanges;
     }
 

Modified: trunk/Source/WebCore/platform/graphics/FontSelector.h (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/FontSelector.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/FontSelector.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -31,6 +31,7 @@
 
 namespace WebCore {
 
+class FontCache;
 class FontCascadeDescription;
 class FontDescription;
 class FontSelectorClient;
@@ -54,6 +55,7 @@
 
     virtual void opportunisticallyStartFontDataURLLoading(const FontCascadeDescription&, const AtomString& family) = 0;
 
+    virtual FontCache& fontCache() const = 0;
     virtual void fontCacheInvalidated() { }
 
     virtual void registerForInvalidationCallbacks(FontSelectorClient&) = 0;

Modified: trunk/Source/WebCore/platform/graphics/cairo/FontCairoHarfbuzzNG.cpp (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/cairo/FontCairoHarfbuzzNG.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/cairo/FontCairoHarfbuzzNG.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -138,7 +138,7 @@
             return fallbackFont;
     }
 
-    if (auto systemFallback = FontCache::singleton().systemFallbackForCharacters(m_fontDescription, baseFont, IsForPlatformFont::No, preferColoredFont ? FontCache::PreferColoredFont::Yes : FontCache::PreferColoredFont::No, characters, length)) {
+    if (auto systemFallback = FontCache::fontCacheFallbackToSingleton(fontSelector()).systemFallbackForCharacters(m_fontDescription, baseFont, IsForPlatformFont::No, preferColoredFont ? FontCache::PreferColoredFont::Yes : FontCache::PreferColoredFont::No, characters, length)) {
         if (systemFallback->canRenderCombiningCharacterSequence(characters, length) && (!preferColoredFont || systemFallback->platformData().isColorBitmapFont()))
             return systemFallback.get();
 

Modified: trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp (275419 => 275420)


--- trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -338,19 +338,19 @@
     if (family.length() && !family.startsWith("-webkit-"))
         return family;
 
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::StandardFamily)) || family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SerifFamily)))
+    if (family == familyNamesData->at(FamilyNamesIndex::StandardFamily) || family == familyNamesData->at(FamilyNamesIndex::SerifFamily))
         return "serif";
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SansSerifFamily)))
+    if (family == familyNamesData->at(FamilyNamesIndex::SansSerifFamily))
         return "sans-serif";
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::MonospaceFamily)))
+    if (family == familyNamesData->at(FamilyNamesIndex::MonospaceFamily))
         return "monospace";
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::CursiveFamily)))
+    if (family == familyNamesData->at(FamilyNamesIndex::CursiveFamily))
         return "cursive";
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::FantasyFamily)))
+    if (family == familyNamesData->at(FamilyNamesIndex::FantasyFamily))
         return "fantasy";
 
 #if PLATFORM(GTK)
-    if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SystemUiFamily)) || family == "-webkit-system-font")
+    if (family == familyNamesData->at(FamilyNamesIndex::SystemUiFamily) || family == "-webkit-system-font")
         return defaultGtkSystemFont();
 #endif
 

Modified: trunk/Source/WebCore/style/StyleBuilderCustom.h (275419 => 275420)


--- trunk/Source/WebCore/style/StyleBuilderCustom.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/style/StyleBuilderCustom.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -1061,7 +1061,7 @@
                 family = builderState.document().settings().standardFontFamily();
             else {
                 isGenericFamily = true;
-                family = CSSPropertyParserHelpers::genericFontFamilyFromValueID(contentValue.valueID());
+                family = CSSPropertyParserHelpers::genericFontFamily(contentValue.valueID());
             }
         }
 

Modified: trunk/Source/WebCore/style/StyleResolveForFontRaw.cpp (275419 => 275420)


--- trunk/Source/WebCore/style/StyleResolveForFontRaw.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/style/StyleResolveForFontRaw.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -32,13 +32,16 @@
 #include "StyleResolveForFontRaw.h"
 
 #include "CSSFontSelector.h"
+#include "CSSHelper.h"
 #include "CSSPropertyParserHelpers.h"
 #include "Document.h"
 #include "FontCascade.h"
 #include "FontCascadeDescription.h"
 #include "RenderStyle.h"
+#include "ScriptExecutionContext.h"
 #include "Settings.h"
 #include "StyleFontSizeFunctions.h"
+#include "WebKitFontFamilyNames.h"
 
 namespace WebCore {
 
@@ -45,15 +48,23 @@
 namespace Style {
 
 using namespace CSSPropertyParserHelpers;
+using namespace WebKitFontFamilyNames;
 
-Optional<FontCascade> resolveForFontRaw(const FontRaw& fontRaw, FontCascadeDescription&& fontDescription, Document& document)
+static bool useFixedDefaultSize(const FontCascadeDescription& fontDescription)
 {
+    return fontDescription.familyCount() == 1 && fontDescription.firstFamily() == familyNamesData->at(FamilyNamesIndex::MonospaceFamily);
+}
+
+Optional<FontCascade> resolveForFontRaw(const FontRaw& fontRaw, FontCascadeDescription&& fontDescription, ScriptExecutionContext& context)
+{
+    ASSERT(context.cssFontSelector());
+
     // Map the font property longhands into the style.
     float parentSize = fontDescription.specifiedSize();
 
     // Font family applied in the same way as StyleBuilderCustom::applyValueFontFamily
     // Before mapping in a new font-family property, we should reset the generic family.
-    bool oldFamilyUsedFixedDefaultSize = fontDescription.useFixedDefaultSize();
+    bool oldFamilyUsedFixedDefaultSize = useFixedDefaultSize(fontDescription);
 
     Vector<AtomString> families;
     families.reserveInitialCapacity(fontRaw.family.size());
@@ -63,7 +74,10 @@
         bool isGenericFamily = false;
         switchOn(item, [&] (CSSValueID ident) {
             isGenericFamily = ident != CSSValueWebkitBody;
-            family = isGenericFamily ? CSSPropertyParserHelpers::genericFontFamilyFromValueID(ident) : AtomString(document.settings().standardFontFamily());
+            if (isGenericFamily)
+                family = familyNamesData->at(CSSPropertyParserHelpers::genericFontFamilyIndex(ident));
+            else
+                family = AtomString(context.settingsValues().fontGenericFamilies.standardFontFamily());
         }, [&] (const String& familyString) {
             family = familyString;
         });
@@ -79,11 +93,11 @@
         return WTF::nullopt;
     fontDescription.setFamilies(families);
 
-    if (fontDescription.useFixedDefaultSize() != oldFamilyUsedFixedDefaultSize) {
+    if (useFixedDefaultSize(fontDescription) != oldFamilyUsedFixedDefaultSize) {
         if (CSSValueID sizeIdentifier = fontDescription.keywordSizeAsIdentifier()) {
-            auto size = Style::fontSizeForKeyword(sizeIdentifier, !oldFamilyUsedFixedDefaultSize, document);
+            auto size = Style::fontSizeForKeyword(sizeIdentifier, !oldFamilyUsedFixedDefaultSize, context.settingsValues());
             fontDescription.setSpecifiedSize(size);
-            fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(size, fontDescription.isAbsoluteSize(), 1.0, MinimumFontSizeRule::None, document.settingsValues()));
+            fontDescription.setComputedSize(Style::computedFontSizeFromSpecifiedSize(size, fontDescription.isAbsoluteSize(), 1.0, MinimumFontSizeRule::None, context.settingsValues()));
         }
     }
 
@@ -176,7 +190,7 @@
         case CSSValueXxLarge:
         case CSSValueWebkitXxxLarge:
             fontDescription.setKeywordSizeFromIdentifier(ident);
-            return Style::fontSizeForKeyword(ident, fontDescription.useFixedDefaultSize(), document);
+            return Style::fontSizeForKeyword(ident, fontDescription.useFixedDefaultSize(), context.settingsValues());
         case CSSValueLarger:
             return parentSize * 1.2f;
         case CSSValueSmaller:
@@ -187,8 +201,13 @@
     }, [&] (const CSSPropertyParserHelpers::LengthOrPercentRaw& lengthOrPercent) {
         return switchOn(lengthOrPercent, [&] (const CSSPropertyParserHelpers::LengthRaw& length) {
             auto fontCascade = FontCascade(FontCascadeDescription(fontDescription));
-            fontCascade.update(&document.fontSelector());
-            return static_cast<float>(CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(length.type, length.value, CSSPropertyFontSize, &fontCascade.fontMetrics(), &fontCascade.fontDescription(), &fontCascade.fontDescription(), document.renderView()));
+            fontCascade.update(context.cssFontSelector());
+            // FIXME: Passing null for the RenderView parameter means that vw and vh units will evaluate to
+            //        zero and vmin and vmax units will evaluate as if they were px units.
+            //        It's unclear in the specification if they're expected to work on OffscreenCanvas, given
+            //        that it's off-screen and therefore doesn't strictly have an associated viewport.
+            //        This needs clarification and possibly fixing.
+            return static_cast<float>(CSSPrimitiveValue::computeUnzoomedNonCalcLengthDouble(length.type, length.value, CSSPropertyFontSize, &fontCascade.fontMetrics(), &fontCascade.fontDescription(), &fontCascade.fontDescription(), is<Document>(context) ? downcast<Document>(context).renderView() : nullptr));
         }, [&] (double percentage) {
             return static_cast<float>((parentSize * percentage) / 100.0);
         });
@@ -203,7 +222,7 @@
     // it, even though there is line-height information on FontRaw.
 
     auto fontCascade = FontCascade(WTFMove(fontDescription));
-    fontCascade.update(&document.fontSelector());
+    fontCascade.update(context.cssFontSelector());
     return fontCascade;
 }
 

Modified: trunk/Source/WebCore/style/StyleResolveForFontRaw.h (275419 => 275420)


--- trunk/Source/WebCore/style/StyleResolveForFontRaw.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/style/StyleResolveForFontRaw.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -32,9 +32,9 @@
 
 namespace WebCore {
 
-class Document;
 class FontCascade;
 class FontCascadeDescription;
+class ScriptExecutionContext;
 
 namespace CSSPropertyParserHelpers {
 struct FontRaw;
@@ -42,7 +42,7 @@
 
 namespace Style {
 
-Optional<FontCascade> resolveForFontRaw(const CSSPropertyParserHelpers::FontRaw&, FontCascadeDescription&&, Document&);
+Optional<FontCascade> resolveForFontRaw(const CSSPropertyParserHelpers::FontRaw&, FontCascadeDescription&&, ScriptExecutionContext&);
 
 }
 }

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.cpp (275419 => 275420)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.cpp	2021-04-02 14:06:16 UTC (rev 275420)
@@ -28,10 +28,12 @@
 #include "config.h"
 #include "WorkerGlobalScope.h"
 
+#include "CSSFontSelector.h"
 #include "CSSValueList.h"
 #include "CSSValuePool.h"
 #include "ContentSecurityPolicy.h"
 #include "Crypto.h"
+#include "FontCache.h"
 #include "IDBConnectionProxy.h"
 #include "ImageBitmapOptions.h"
 #include "InspectorInstrumentation.h"
@@ -90,6 +92,8 @@
     // We need to remove from the contexts map very early in the destructor so that calling postTask() on this WorkerGlobalScope from another thread is safe.
     removeFromContextsMap();
 
+    if (m_cssFontSelector)
+        m_cssFontSelector->stopLoadingAndClearFonts();
     m_performance = nullptr;
     m_crypto = nullptr;
 
@@ -478,6 +482,20 @@
     return *m_cssValuePool;
 }
 
+CSSFontSelector* WorkerGlobalScope::cssFontSelector()
+{
+    if (!m_cssFontSelector)
+        m_cssFontSelector = CSSFontSelector::create(*this);
+    return m_cssFontSelector.get();
+}
+
+FontCache& WorkerGlobalScope::fontCache()
+{
+    if (!m_fontCache)
+        m_fontCache = FontCache::create();
+    return *m_fontCache;
+}
+
 ReferrerPolicy WorkerGlobalScope::referrerPolicy() const
 {
     return m_referrerPolicy;

Modified: trunk/Source/WebCore/workers/WorkerGlobalScope.h (275419 => 275420)


--- trunk/Source/WebCore/workers/WorkerGlobalScope.h	2021-04-02 14:01:07 UTC (rev 275419)
+++ trunk/Source/WebCore/workers/WorkerGlobalScope.h	2021-04-02 14:06:16 UTC (rev 275420)
@@ -42,6 +42,7 @@
 
 namespace WebCore {
 
+class CSSFontSelector;
 class CSSValuePool;
 class ContentSecurityPolicyResponseHeaders;
 class Crypto;
@@ -116,6 +117,8 @@
     void createImageBitmap(ImageBitmap::Source&&, int sx, int sy, int sw, int sh, ImageBitmapOptions&&, ImageBitmap::Promise&&);
 
     CSSValuePool& cssValuePool();
+    CSSFontSelector* cssFontSelector() final;
+    FontCache& fontCache() final;
 
     ReferrerPolicy referrerPolicy() const final;
 
@@ -180,6 +183,8 @@
     RefPtr<WorkerSWClientConnection> m_swClientConnection;
 #endif
     std::unique_ptr<CSSValuePool> m_cssValuePool;
+    RefPtr<CSSFontSelector> m_cssFontSelector;
+    RefPtr<FontCache> m_fontCache;
     ReferrerPolicy m_referrerPolicy;
     Settings::Values m_settingsValues;
     WorkerType m_workerType;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to