Bug 1249600 - Lookup font and font family from font face when requesting SkTypeface. r=bas r=ritu
authorMason Chang <mchang@mozilla.com>
Tue, 08 Mar 2016 07:37:16 -0800
changeset 323471 cd661d54ee73de897099fae295e871be31271bfe
parent 323470 2fd77fb72e36e32844715012c6bae54b0e9d34aa
child 323472 c1af4f3c15704b3a59ee373c05bdd561beaaa177
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbas, ritu
bugs1249600
milestone47.0a2
Bug 1249600 - Lookup font and font family from font face when requesting SkTypeface. r=bas r=ritu
gfx/2d/ScaledFontDWrite.cpp
gfx/2d/ScaledFontDWrite.h
--- a/gfx/2d/ScaledFontDWrite.cpp
+++ b/gfx/2d/ScaledFontDWrite.cpp
@@ -110,22 +110,52 @@ ScaledFontDWrite::GetPathForGlyphs(const
 
   CopyGlyphsToSink(aBuffer, pathBuilderD2D->GetSink());
 
   return pathBuilder->Finish();
 }
 
 
 #ifdef USE_SKIA
+// This can happen if we have mixed backends which create DWrite
+// fonts in a mixed environment. e.g. a cairo content backend
+// but Skia canvas backend.
+void
+ScaledFontDWrite::GetFontDataFromSystemFonts(IDWriteFactory* aFactory)
+{
+  MOZ_ASSERT(mFontFace);
+  RefPtr<IDWriteFontCollection> systemFonts;
+  HRESULT hr = aFactory->GetSystemFontCollection(getter_AddRefs(systemFonts));
+  if (FAILED(hr)) {
+    gfxWarning() << "Failed to get system font collection from file data. Code: " << hexa(hr);
+    return;
+  }
+
+  hr = systemFonts->GetFontFromFontFace(mFontFace, getter_AddRefs(mFont));
+  if (FAILED(hr)) {
+    gfxWarning() << "Failed to get system font from font face. Code: " << hexa(hr);
+    return;
+  }
+
+  hr = mFont->GetFontFamily(getter_AddRefs(mFontFamily));
+  if (FAILED(hr)) {
+    gfxWarning() << "Failed to get font family from font face. Code: " << hexa(hr);
+    return;
+  }
+}
+
 SkTypeface*
 ScaledFontDWrite::GetSkTypeface()
 {
-  MOZ_ASSERT(mFont);
   if (!mTypeface) {
     IDWriteFactory *factory = DrawTargetD2D1::GetDWriteFactory();
+    if (!mFont || !mFontFamily) {
+      GetFontDataFromSystemFonts(factory);
+    }
+
     mTypeface = SkCreateTypefaceFromDWriteFont(factory, mFontFace, mFont, mFontFamily);
   }
   return mTypeface;
 }
 #endif
 
 void
 ScaledFontDWrite::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
--- a/gfx/2d/ScaledFontDWrite.h
+++ b/gfx/2d/ScaledFontDWrite.h
@@ -41,18 +41,20 @@ public:
   void CopyGlyphsToSink(const GlyphBuffer &aBuffer, ID2D1GeometrySink *aSink);
 
   virtual bool GetFontFileData(FontFileDataOutput aDataCallback, void *aBaton);
 
   virtual AntialiasMode GetDefaultAAMode();
 
 #ifdef USE_SKIA
   virtual SkTypeface* GetSkTypeface();
+  void GetFontDataFromSystemFonts(IDWriteFactory* aFactory);
 #endif
 
+  // The font and font family are only used with Skia
   RefPtr<IDWriteFont> mFont;
   RefPtr<IDWriteFontFamily> mFontFamily;
   RefPtr<IDWriteFontFace> mFontFace;
 
 protected:
 #ifdef USE_CAIRO_SCALED_FONT
   cairo_font_face_t* GetCairoFontFace() override;
 #endif