<http://webkit.org/b/91015> Remove BUILDING_ON / TARGETING macros in favor of system...
[WebKit-https.git] / Source / WebCore / platform / graphics / mac / FontCustomPlatformData.cpp
index 4bc6819..b192684 100644 (file)
 #include "WOFFFileFormat.h"
 #include <ApplicationServices/ApplicationServices.h>
 
+#if USE(SKIA_ON_MAC_CHROMIUM)
+#include "SkStream.h"
+#include "SkTypeface.h"
+#endif
+
 namespace WebCore {
 
+#if USE(SKIA_ON_MAC_CHROMIUM)
+class RemoteFontStream : public SkStream {
+public:
+    explicit RemoteFontStream(PassRefPtr<SharedBuffer> buffer)
+        : m_buffer(buffer)
+        , m_offset(0)
+    {
+    }
+
+    virtual ~RemoteFontStream()
+    {
+    }
+
+    virtual bool rewind()
+    {
+        m_offset = 0;
+        return true;
+    }
+
+    virtual size_t read(void* buffer, size_t size)
+    {
+        if (!buffer && !size) {
+            // This is request for the length of the stream.
+            return m_buffer->size();
+        }
+        // This is a request to read bytes or skip bytes (when buffer is 0).
+        if (!m_buffer->data() || !m_buffer->size())
+            return 0;
+        size_t left = m_buffer->size() - m_offset;
+        size_t bytesToConsume = std::min(left, size);
+        if (buffer)
+            std::memcpy(buffer, m_buffer->data() + m_offset, bytesToConsume);
+        m_offset += bytesToConsume;
+        return bytesToConsume;
+    }
+
+private:
+    RefPtr<SharedBuffer> m_buffer;
+    size_t m_offset;
+};
+#endif
+
 FontCustomPlatformData::~FontCustomPlatformData()
 {
-#ifdef BUILDING_ON_LEOPARD
+#if __MAC_OS_X_VERSION_MIN_REQUIRED == 1050
     if (m_atsContainer)
         ATSFontDeactivate(m_atsContainer, NULL, kATSOptionFlagsDefault);
 #endif
+#if USE(SKIA_ON_MAC_CHROMIUM)
+    SkSafeUnref(m_typeface);
+#endif
     CGFontRelease(m_cgFont);
 }
 
@@ -47,7 +97,7 @@ FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
 {
     ASSERT_ARG(buffer, buffer);
 
-#if ENABLE(OPENTYPE_SANITIZER)
+#if USE(OPENTYPE_SANITIZER)
     OpenTypeSanitizer sanitizer(buffer);
     RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize();
     if (!transcodeBuffer)
@@ -69,7 +119,7 @@ FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
 
     RetainPtr<CGFontRef> cgFontRef;
 
-#ifndef BUILDING_ON_LEOPARD
+#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
     RetainPtr<CFDataRef> bufferData(AdoptCF, buffer->createCFData());
     RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(bufferData.get()));
 
@@ -107,9 +157,15 @@ FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
         ATSFontDeactivate(containerRef, NULL, kATSOptionFlagsDefault);
         return 0;
     }
-#endif // !defined(BUILDING_ON_LEOPARD)
+#endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
 
-    return new FontCustomPlatformData(containerRef, cgFontRef.releaseRef());
+    FontCustomPlatformData* fontCustomPlatformData = new FontCustomPlatformData(containerRef, cgFontRef.leakRef());
+#if USE(SKIA_ON_MAC_CHROMIUM)
+    RemoteFontStream* stream = new RemoteFontStream(buffer);
+    fontCustomPlatformData->m_typeface = SkTypeface::CreateFromStream(stream);
+    stream->unref();
+#endif
+    return fontCustomPlatformData;
 }
 
 bool FontCustomPlatformData::supportsFormat(const String& format)