Allow targetting the SVG->OTF font converter with ENABLE(SVG_OTF_CONVERTER)
https://bugs.webkit.org/show_bug.cgi?id=136769

Reviewed by Antti Koivisto.

Source/JavaScriptCore:

* Configurations/FeatureDefines.xcconfig:

Source/WebCore:

If ENABLE(SVG_OTF_CONVERTER) is defined, use the converter. It can be defined at the same
time as ENABLE(SVG_FONTS) but, if so, the SVG font code will be dead code.

No new tests because the define is off by default. Tests will come soon, I promise.

* Configurations/FeatureDefines.xcconfig:
* css/CSSFontFaceSource.cpp:
(WebCore::CSSFontFaceSource::getFontData): When creating a font, if the ENABLE is on,
do the transcode and take the non-SVG path.
(WebCore::CSSFontFaceSource::ensureFontData): Pass extra arguments to
CachedFont::ensureCustomFontData()
* css/CSSFontFaceSource.h: For the case of in-document SVG fonts, keep the transcoded
bytes around.
* loader/cache/CachedFont.cpp:
(WebCore::CachedFont::ensureCustomFontData): For out-of-document SVG fonts, do the
transcode if the ENABLE is on, then treat as if the font is any old webfont.
(WebCore::CachedFont::getSVGFontById): This function looks up the relevant <font>
element. Modify it to take a pointer to a (possibly external) document within which
to search.
* loader/cache/CachedFont.h: Extra arguments to CachedFont::ensureCustomFontData()
and CachedFont::getSVGFontById()

Source/WebKit/mac:

* Configurations/FeatureDefines.xcconfig:

Source/WebKit2:

* Configurations/FeatureDefines.xcconfig:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@178292 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/loader/cache/CachedFont.cpp b/Source/WebCore/loader/cache/CachedFont.cpp
index 2026e57..4ec3e11 100644
--- a/Source/WebCore/loader/cache/CachedFont.cpp
+++ b/Source/WebCore/loader/cache/CachedFont.cpp
@@ -40,10 +40,6 @@
 #include "WOFFFileFormat.h"
 #include <wtf/Vector.h>
 
-#if ENABLE(SVG_OTF_CONVERTER)
-#include "SVGToOTFFontConversion.h"
-#endif
-
 #if ENABLE(SVG_FONTS)
 #include "NodeList.h"
 #include "SVGDocument.h"
@@ -96,26 +92,28 @@
     }
 }
 
-bool CachedFont::ensureCustomFontData(bool)
+bool CachedFont::ensureCustomFontData(bool, const AtomicString&)
 {
-    if (!m_fontData && !errorOccurred() && !isLoading() && m_data) {
-        RefPtr<SharedBuffer> buffer = m_data;
-        bool fontIsWOFF = false;
+    return ensureCustomFontData(RefPtr<SharedBuffer>(m_data));
+}
+
+bool CachedFont::ensureCustomFontData(RefPtr<SharedBuffer>&& data)
+{
+    if (!m_fontData && !errorOccurred() && !isLoading() && data) {
+        RefPtr<SharedBuffer> buffer = data;
 
 #if (!PLATFORM(MAC) || __MAC_OS_X_VERSION_MIN_REQUIRED <= 1090) && (!PLATFORM(IOS) || __IPHONE_OS_VERSION_MIN_REQUIRED < 80000)
         if (isWOFF(buffer.get())) {
             Vector<char> convertedFont;
             if (!convertWOFFToSfnt(buffer.get(), convertedFont))
                 buffer = nullptr;
-            else {
+            else
                 buffer = SharedBuffer::adoptVector(convertedFont);
-                fontIsWOFF = true;
-            }
         }
 #endif
 
         m_fontData = buffer ? createFontCustomPlatformData(*buffer) : nullptr;
-        m_hasCreatedFontDataWrappingResource = m_fontData && !fontIsWOFF;
+        m_hasCreatedFontDataWrappingResource = m_fontData && (buffer == m_data);
         if (!m_fontData)
             setStatus(DecodeError);
     }
diff --git a/Source/WebCore/loader/cache/CachedFont.h b/Source/WebCore/loader/cache/CachedFont.h
index e5c5545..c2c6066 100644
--- a/Source/WebCore/loader/cache/CachedFont.h
+++ b/Source/WebCore/loader/cache/CachedFont.h
@@ -50,13 +50,15 @@
     void beginLoadIfNeeded(CachedResourceLoader* dl);
     virtual bool stillNeedsLoad() const override { return !m_loadInitiated; }
 
-    virtual bool ensureCustomFontData(bool externalSVG);
+    virtual bool ensureCustomFontData(bool externalSVG, const AtomicString& remoteURI);
 
     virtual PassRefPtr<SimpleFontData> getFontData(const FontDescription&, const AtomicString& remoteURI, bool syntheticBold, bool syntheticItalic, bool externalSVG);
 
 protected:
     FontPlatformData platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation = Horizontal, FontWidthVariant = RegularWidth, FontRenderingMode = NormalRenderingMode);
 
+    bool ensureCustomFontData(RefPtr<SharedBuffer>&& data);
+
 private:
     virtual void checkNotify() override;
     virtual bool mayTryReplaceEncodedData() const override;
diff --git a/Source/WebCore/loader/cache/CachedSVGFont.cpp b/Source/WebCore/loader/cache/CachedSVGFont.cpp
index 812fbf6..ed24eaf 100644
--- a/Source/WebCore/loader/cache/CachedSVGFont.cpp
+++ b/Source/WebCore/loader/cache/CachedSVGFont.cpp
@@ -39,6 +39,10 @@
 #include "TextResourceDecoder.h"
 #include "TypedElementDescendantIterator.h"
 
+#if ENABLE(SVG_OTF_CONVERTER)
+#include "SVGToOTFFontConversion.h"
+#endif
+
 namespace WebCore {
 
 CachedSVGFont::CachedSVGFont(const ResourceRequest& resourceRequest, SessionID sessionID)
@@ -49,7 +53,9 @@
 
 PassRefPtr<SimpleFontData> CachedSVGFont::getFontData(const FontDescription& fontDescription, const AtomicString& remoteURI, bool syntheticBold, bool syntheticItalic, bool externalSVG)
 {
+#if !ENABLE(SVG_OTF_CONVERTER)
     if (!externalSVG)
+#endif
         return CachedFont::getFontData(fontDescription, remoteURI, syntheticBold, syntheticItalic, externalSVG);
 
     if (SVGFontFaceElement* firstFontFace = this->firstFontFace(remoteURI))
@@ -64,10 +70,10 @@
     return CachedFont::platformDataFromCustomData(size, bold, italic, orientation, widthVariant, renderingMode);
 }
 
-bool CachedSVGFont::ensureCustomFontData(bool externalSVG)
+bool CachedSVGFont::ensureCustomFontData(bool externalSVG, const AtomicString& remoteURI)
 {
     if (!externalSVG)
-        return CachedFont::ensureCustomFontData(externalSVG);
+        return CachedFont::ensureCustomFontData(externalSVG, remoteURI);
 
     if (!m_externalSVGDocument && !errorOccurred() && !isLoading() && m_data) {
         m_externalSVGDocument = SVGDocument::create(nullptr, URL());
@@ -75,6 +81,13 @@
         m_externalSVGDocument->setContent(decoder->decodeAndFlush(m_data->data(), m_data->size()));
         if (decoder->sawError())
             m_externalSVGDocument = nullptr;
+#if ENABLE(SVG_OTF_CONVERTER)
+        firstFontFace(remoteURI); // Sets m_externalSVGFontElement
+        if (m_externalSVGFontElement) {
+            Vector<char> convertedFont = convertSVGToOTFFont(*m_externalSVGFontElement);
+            return CachedFont::ensureCustomFontData(SharedBuffer::adoptVector(convertedFont));
+        }
+#endif
     }
     return m_externalSVGDocument;
 }
diff --git a/Source/WebCore/loader/cache/CachedSVGFont.h b/Source/WebCore/loader/cache/CachedSVGFont.h
index 68c0cf3..a83af37 100644
--- a/Source/WebCore/loader/cache/CachedSVGFont.h
+++ b/Source/WebCore/loader/cache/CachedSVGFont.h
@@ -38,7 +38,7 @@
 public:
     CachedSVGFont(const ResourceRequest&, SessionID);
 
-    virtual bool ensureCustomFontData(bool externalSVG) override;
+    virtual bool ensureCustomFontData(bool externalSVG, const AtomicString& remoteURI) override;
     
     virtual PassRefPtr<SimpleFontData> getFontData(const FontDescription&, const AtomicString& remoteURI, bool syntheticBold, bool syntheticItalic, bool externalSVG) override;