diff options
Diffstat (limited to 'Source/WebCore/css/CSSFontFaceSource.cpp')
-rw-r--r-- | Source/WebCore/css/CSSFontFaceSource.cpp | 250 |
1 files changed, 119 insertions, 131 deletions
diff --git a/Source/WebCore/css/CSSFontFaceSource.cpp b/Source/WebCore/css/CSSFontFaceSource.cpp index 23ee019b6..293f8f7fd 100644 --- a/Source/WebCore/css/CSSFontFaceSource.cpp +++ b/Source/WebCore/css/CSSFontFaceSource.cpp @@ -10,10 +10,10 @@ * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR @@ -32,187 +32,175 @@ #include "CachedResourceLoader.h" #include "Document.h" #include "ElementIterator.h" +#include "Font.h" #include "FontCache.h" +#include "FontCustomPlatformData.h" #include "FontDescription.h" -#include "SimpleFontData.h" +#include "SVGToOTFFontConversion.h" #if ENABLE(SVG_FONTS) +#include "CachedSVGFont.h" #include "FontCustomPlatformData.h" -#include "SVGFontData.h" #include "SVGFontElement.h" #include "SVGFontFaceElement.h" -#include "SVGNames.h" #include "SVGURIReference.h" #endif +#if USE(DIRECT2D) +#include <dwrite.h> +#endif + namespace WebCore { -CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font) - : m_string(str) +inline void CSSFontFaceSource::setStatus(Status newStatus) +{ + switch (newStatus) { + case Status::Pending: + ASSERT_NOT_REACHED(); + break; + case Status::Loading: + ASSERT(status() == Status::Pending); + break; + case Status::Success: + ASSERT(status() == Status::Loading); + break; + case Status::Failure: + ASSERT(status() == Status::Loading); + break; + } + + m_status = newStatus; +} + +CSSFontFaceSource::CSSFontFaceSource(CSSFontFace& owner, const String& familyNameOrURI, CachedFont* font, SVGFontFaceElement* fontFace, RefPtr<JSC::ArrayBufferView>&& arrayBufferView) + : m_familyNameOrURI(familyNameOrURI) , m_font(font) - , m_face(0) + , m_face(owner) + , m_immediateSource(WTFMove(arrayBufferView)) #if ENABLE(SVG_FONTS) - , m_hasExternalSVGFont(false) + , m_svgFontFaceElement(fontFace) #endif { +#if !ENABLE(SVG_FONTS) + UNUSED_PARAM(fontFace); +#endif + + // This may synchronously call fontLoaded(). if (m_font) - m_font->addClient(this); + m_font->addClient(*this); + + if (status() == Status::Pending && (!m_font || m_font->isLoaded())) { + setStatus(Status::Loading); + if (m_font && m_font->errorOccurred()) + setStatus(Status::Failure); + else + setStatus(Status::Success); + } } CSSFontFaceSource::~CSSFontFaceSource() { if (m_font) - m_font->removeClient(this); - pruneTable(); + m_font->removeClient(*this); } -void CSSFontFaceSource::pruneTable() +void CSSFontFaceSource::fontLoaded(CachedFont& loadedFont) { - if (m_fontDataTable.isEmpty()) + ASSERT_UNUSED(loadedFont, &loadedFont == m_font.get()); + + // If the font is in the cache, this will be synchronously called from CachedFont::addClient(). + if (m_status == Status::Pending) + setStatus(Status::Loading); + else if (m_status == Status::Failure) { + // This function may be called twice if loading was cancelled. + ASSERT(m_font->errorOccurred()); return; + } - m_fontDataTable.clear(); -} + if (m_face.webFontsShouldAlwaysFallBack()) + return; -bool CSSFontFaceSource::isLoaded() const -{ - if (m_font) - return m_font->isLoaded(); - return true; -} + if (m_font->errorOccurred()) + setStatus(Status::Failure); + else + setStatus(Status::Success); -bool CSSFontFaceSource::isValid() const -{ - if (m_font) - return !m_font->errorOccurred(); - return true; + m_face.fontLoaded(*this); } -void CSSFontFaceSource::fontLoaded(CachedFont*) +void CSSFontFaceSource::load(CSSFontSelector& fontSelector) { - pruneTable(); - if (m_face) - m_face->fontLoaded(this); + setStatus(Status::Loading); + + ASSERT(m_font); + fontSelector.beginLoadingFontSoon(*m_font); } -PassRefPtr<SimpleFontData> CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector) +RefPtr<Font> CSSFontFaceSource::font(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings) { - // If the font hasn't loaded or an error occurred, then we've got nothing. - if (!isValid()) - return 0; + ASSERT(status() == Status::Success); - if (!m_font + SVGFontFaceElement* fontFaceElement = nullptr; #if ENABLE(SVG_FONTS) - && !m_svgFontFaceElement + fontFaceElement = m_svgFontFaceElement.get(); #endif - ) { - // We're local. Just return a SimpleFontData from the normal cache. + + if (!m_font && !fontFaceElement) { + if (m_immediateSource) { + if (!m_immediateFontCustomPlatformData) { + bool wrapping; + RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<const char*>(m_immediateSource->baseAddress()), m_immediateSource->byteLength()); + ASSERT(buffer); + m_immediateFontCustomPlatformData = CachedFont::createCustomFontData(*buffer, wrapping); + } if (!m_immediateFontCustomPlatformData) + return nullptr; + return Font::create(CachedFont::platformDataFromCustomData(*m_immediateFontCustomPlatformData, fontDescription, syntheticBold, syntheticItalic, fontFaceFeatures, fontFaceVariantSettings), true); + } + + // We're local. Just return a Font from the normal cache. // We don't want to check alternate font family names here, so pass true as the checkingAlternateName parameter. - return fontCache()->getCachedFontData(fontDescription, m_string, true); + return FontCache::singleton().fontForFamily(fontDescription, m_familyNameOrURI, &fontFaceFeatures, &fontFaceVariantSettings, true); } - // See if we have a mapping in our FontData cache. - unsigned hashKey = (fontDescription.computedPixelSize() + 1) << 5 | fontDescription.widthVariant() << 3 - | (fontDescription.orientation() == Vertical ? 4 : 0) | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0); + if (m_font) { + if (!m_font->ensureCustomFontData(m_familyNameOrURI)) + return nullptr; - RefPtr<SimpleFontData> fontData = m_fontDataTable.add(hashKey, nullptr).iterator->value; - if (fontData) - return fontData.release(); + return m_font->createFont(fontDescription, m_familyNameOrURI, syntheticBold, syntheticItalic, fontFaceFeatures, fontFaceVariantSettings); + } + + // In-Document SVG Fonts + if (!fontFaceElement) + return nullptr; - // If we are still loading, then we let the system pick a font. - if (isLoaded()) { - if (m_font) { #if ENABLE(SVG_FONTS) - if (m_hasExternalSVGFont) { - // For SVG fonts parse the external SVG document, and extract the <font> element. - if (!m_font->ensureSVGFontData()) - return 0; - - if (!m_externalSVGFontElement) { - String fragmentIdentifier; - size_t start = m_string.find('#'); - if (start != notFound) - fragmentIdentifier = m_string.string().substring(start + 1); - m_externalSVGFontElement = m_font->getSVGFontById(fragmentIdentifier); - } - - if (!m_externalSVGFontElement) - return 0; - - if (auto firstFontFace = childrenOfType<SVGFontFaceElement>(*m_externalSVGFontElement).first()) { - if (!m_svgFontFaceElement) { - // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement. - // Use the imported <font-face> tag as referencing font-face element for these cases. - m_svgFontFaceElement = firstFontFace; - } - - fontData = SimpleFontData::create(std::make_unique<SVGFontData>(firstFontFace), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic); - } - } else + if (!is<SVGFontElement>(m_svgFontFaceElement->parentNode())) + return nullptr; + if (!m_inDocumentCustomPlatformData) { + SVGFontElement& fontElement = downcast<SVGFontElement>(*m_svgFontFaceElement->parentNode()); + if (auto otfFont = convertSVGToOTFFont(fontElement)) + m_generatedOTFBuffer = SharedBuffer::adoptVector(otfFont.value()); + if (!m_generatedOTFBuffer) + return nullptr; + m_inDocumentCustomPlatformData = createFontCustomPlatformData(*m_generatedOTFBuffer); + } + if (!m_inDocumentCustomPlatformData) + return nullptr; +#if PLATFORM(COCOA) + return Font::create(m_inDocumentCustomPlatformData->fontPlatformData(fontDescription, syntheticBold, syntheticItalic, fontFaceFeatures, fontFaceVariantSettings), true, false); +#else + return Font::create(m_inDocumentCustomPlatformData->fontPlatformData(fontDescription, syntheticBold, syntheticItalic), true, false); #endif - { - // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef. - if (!m_font->ensureCustomFontData()) - return 0; - - fontData = SimpleFontData::create(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, - fontDescription.orientation(), fontDescription.widthVariant(), fontDescription.renderingMode()), true, false); - } - } else { -#if ENABLE(SVG_FONTS) - // In-Document SVG Fonts - if (m_svgFontFaceElement) - fontData = SimpleFontData::create(std::make_unique<SVGFontData>(m_svgFontFaceElement.get()), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic); #endif - } - } else { - // Kick off the load. Do it soon rather than now, because we may be in the middle of layout, - // and the loader may invoke arbitrary delegate or event handler code. - fontSelector->beginLoadingFontSoon(m_font.get()); - - // This temporary font is not retained and should not be returned. - FontCachePurgePreventer fontCachePurgePreventer; - SimpleFontData* temporaryFont = fontCache()->getNonRetainedLastResortFallbackFont(fontDescription); - fontData = SimpleFontData::create(temporaryFont->platformData(), true, true); - } - return fontData.release(); + ASSERT_NOT_REACHED(); + return nullptr; } #if ENABLE(SVG_FONTS) -SVGFontFaceElement* CSSFontFaceSource::svgFontFaceElement() const -{ - return m_svgFontFaceElement.get(); -} - -void CSSFontFaceSource::setSVGFontFaceElement(PassRefPtr<SVGFontFaceElement> element) -{ - m_svgFontFaceElement = element; -} - bool CSSFontFaceSource::isSVGFontFaceSource() const { - return m_svgFontFaceElement || m_hasExternalSVGFont; -} -#endif - -#if ENABLE(FONT_LOAD_EVENTS) -bool CSSFontFaceSource::isDecodeError() const -{ - if (m_font) - return m_font->status() == CachedResource::DecodeError; - return false; -} - -bool CSSFontFaceSource::ensureFontData() -{ - if (!m_font) - return false; -#if ENABLE(SVG_FONTS) - if (m_hasExternalSVGFont) - return m_font->ensureSVGFontData(); -#endif - return m_font->ensureCustomFontData(); + return m_svgFontFaceElement || is<CachedSVGFont>(m_font.get()); } #endif |