Bug 1433098 - Don't rename downloaded fonts (replacing the original 'name' table) when using DirectWrite backend. r=jrmuizel
authorJonathan Kew <jkew@mozilla.com>
Fri, 26 Jan 2018 17:26:19 +0000
changeset 453518 0cd5065a76e9f351f47fec93a93c514ce4732b5b
parent 453517 617df9c9618b2e754029585556f3150766922d16
child 453519 1ee8edcdd50daaec5fd6c61dd1fd5c2548c5043f
push id8799
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 16:46:23 +0000
treeherdermozilla-beta@15334014dc67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel
bugs1433098
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1433098 - Don't rename downloaded fonts (replacing the original 'name' table) when using DirectWrite backend. r=jrmuizel
gfx/thebes/gfxDWriteCommon.cpp
gfx/thebes/gfxDWriteCommon.h
gfx/thebes/gfxDWriteFontList.cpp
--- a/gfx/thebes/gfxDWriteCommon.cpp
+++ b/gfx/thebes/gfxDWriteCommon.cpp
@@ -21,17 +21,18 @@ public:
   /**
   * Used by the FontFileLoader to create a new font stream,
   * this font stream is created from data in memory. The memory
   * passed may be released after object creation, it will be
   * copied internally.
   *
   * @param aData Font data
   */
-  gfxDWriteFontFileStream(FallibleTArray<uint8_t> *aData,
+  gfxDWriteFontFileStream(const uint8_t* aData,
+                          uint32_t aLength,
                           uint64_t aFontFileKey);
   ~gfxDWriteFontFileStream();
 
   // IUnknown interface
   IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject)
   {
     if (iid == __uuidof(IDWriteFontFileStream)) {
       *ppObject = static_cast<IDWriteFontFileStream*>(this);
@@ -77,21 +78,26 @@ public:
   virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime);
 
 private:
   FallibleTArray<uint8_t> mData;
   nsAutoRefCnt mRefCnt;
   uint64_t mFontFileKey;
 };
 
-gfxDWriteFontFileStream::gfxDWriteFontFileStream(FallibleTArray<uint8_t> *aData,
+gfxDWriteFontFileStream::gfxDWriteFontFileStream(const uint8_t* aData,
+                                                 uint32_t aLength,
                                                  uint64_t aFontFileKey)
   : mFontFileKey(aFontFileKey)
 {
-  mData.SwapElements(*aData);
+  // If this fails, mData will remain empty. That's OK: GetFileSize()
+  // will then return 0, etc., and the font just won't load.
+  if (!mData.AppendElements(aData, aLength, mozilla::fallible_t())) {
+    NS_WARNING("Failed to store data in gfxDWriteFontFileStream");
+  }
 }
 
 gfxDWriteFontFileStream::~gfxDWriteFontFileStream()
 {
   sFontFileStreams.erase(mFontFileKey);
 }
 
 HRESULT STDMETHODCALLTYPE
@@ -146,31 +152,33 @@ gfxDWriteFontFileLoader::CreateStreamFro
 
     found->second->AddRef();
     *fontFileStream = found->second;
     return S_OK;
 }
 
 /* static */
 HRESULT
-gfxDWriteFontFileLoader::CreateCustomFontFile(FallibleTArray<uint8_t>& aFontData,
+gfxDWriteFontFileLoader::CreateCustomFontFile(const uint8_t* aFontData,
+                                              uint32_t aLength,
                                               IDWriteFontFile** aFontFile,
                                               IDWriteFontFileStream** aFontFileStream)
 {
   MOZ_ASSERT(aFontFile);
   MOZ_ASSERT(aFontFileStream);
 
   RefPtr<IDWriteFactory> factory = mozilla::gfx::Factory::GetDWriteFactory();
   if (!factory) {
     gfxCriticalError() << "Failed to get DWrite Factory in CreateCustomFontFile.";
     return E_FAIL;
   }
 
   uint64_t fontFileKey = sNextFontFileKey++;
-  RefPtr<IDWriteFontFileStream> ffsRef = new gfxDWriteFontFileStream(&aFontData, fontFileKey);
+  RefPtr<IDWriteFontFileStream> ffsRef =
+      new gfxDWriteFontFileStream(aFontData, aLength, fontFileKey);
   sFontFileStreams[fontFileKey] = ffsRef;
 
   RefPtr<IDWriteFontFile> fontFile;
   HRESULT hr = factory->CreateCustomFontFileReference(&fontFileKey, sizeof(fontFileKey), Instance(), getter_AddRefs(fontFile));
   if (FAILED(hr)) {
     NS_WARNING("Failed to load font file from data!");
     return hr;
   }
--- a/gfx/thebes/gfxDWriteCommon.h
+++ b/gfx/thebes/gfxDWriteCommon.h
@@ -130,24 +130,27 @@ public:
             mozilla::gfx::Factory::GetDWriteFactory()->
                 RegisterFontFileLoader(mInstance);
         }
         return mInstance;
     }
 
     /**
      * Creates a IDWriteFontFile and IDWriteFontFileStream from aFontData.
-     * aFontData will be empty on return as it swaps out the data.
+     * The data from aFontData will be copied internally, so the caller
+     * is free to dispose of it once this method returns.
      *
      * @param aFontData the font data for the custom font file
+     * @param aLength length of the font data
      * @param aFontFile out param for the created font file
      * @param aFontFileStream out param for the corresponding stream
      * @return HRESULT of internal calls
      */
-    static HRESULT CreateCustomFontFile(FallibleTArray<uint8_t>& aFontData,
+    static HRESULT CreateCustomFontFile(const uint8_t* aFontData,
+                                        uint32_t aLength,
                                         IDWriteFontFile** aFontFile,
                                         IDWriteFontFileStream** aFontFileStream);
 
 private:
     static IDWriteFontFileLoader* mInstance;
 }; 
 
 #endif /* GFX_DWRITECOMMON_H */
--- a/gfx/thebes/gfxDWriteFontList.cpp
+++ b/gfx/thebes/gfxDWriteFontList.cpp
@@ -929,51 +929,40 @@ gfxDWriteFontList::LookupLocalFont(const
 gfxFontEntry *
 gfxDWriteFontList::MakePlatformFont(const nsAString& aFontName,
                                     uint16_t aWeight,
                                     int16_t aStretch,
                                     uint8_t aStyle,
                                     const uint8_t* aFontData,
                                     uint32_t aLength)
 {
-    nsresult rv;
-    nsAutoString uniqueName;
-    rv = gfxFontUtils::MakeUniqueUserFontName(uniqueName);
-    if (NS_FAILED(rv)) {
-        free((void*)aFontData);
+    RefPtr<IDWriteFontFileStream> fontFileStream;
+    RefPtr<IDWriteFontFile> fontFile;
+    HRESULT hr =
+      gfxDWriteFontFileLoader::CreateCustomFontFile(aFontData, aLength,
+                                                    getter_AddRefs(fontFile),
+                                                    getter_AddRefs(fontFileStream));
+    free((void*)aFontData);
+    if (FAILED(hr)) {
+        NS_WARNING("Failed to create custom font file reference.");
         return nullptr;
     }
 
-    FallibleTArray<uint8_t> newFontData;
-
-    rv = gfxFontUtils::RenameFont(uniqueName, aFontData, aLength, &newFontData);
-    free((void*)aFontData);
-
+    nsAutoString uniqueName;
+    nsresult rv = gfxFontUtils::MakeUniqueUserFontName(uniqueName);
     if (NS_FAILED(rv)) {
         return nullptr;
     }
-    
-    RefPtr<IDWriteFontFileStream> fontFileStream;
-    RefPtr<IDWriteFontFile> fontFile;
-    HRESULT hr =
-      gfxDWriteFontFileLoader::CreateCustomFontFile(newFontData,
-                                                    getter_AddRefs(fontFile),
-                                                    getter_AddRefs(fontFileStream));
-
-    if (FAILED(hr)) {
-        NS_WARNING("Failed to create custom font file reference.");
-        return nullptr;
-    }
 
     BOOL isSupported;
     DWRITE_FONT_FILE_TYPE fileType;
     UINT32 numFaces;
 
     gfxDWriteFontEntry *entry = 
-        new gfxDWriteFontEntry(uniqueName, 
+        new gfxDWriteFontEntry(uniqueName,
                                fontFile,
                                fontFileStream,
                                aWeight,
                                aStretch,
                                aStyle);
 
     fontFile->Analyze(&isSupported, &fileType, &entry->mFaceType, &numFaces);
     if (!isSupported || numFaces > 1) {