bug 642093 - map commonly-used bitmap font names to truetype equivalents for dwrite. r=bas
authorJonathan Kew <jfkthame@gmail.com>
Thu, 24 Mar 2011 20:11:38 +0000
changeset 63898 d48ac7574e445ca28e9a2e8b2bd78bf6ec8c3dd8
parent 63897 8faf4e6c62f7e8cc5860f849f13c91f84154d3fc
child 63899 8887ebd9a6c86542499b8952bba1287f60d5d281
push idunknown
push userunknown
push dateunknown
reviewersbas
bugs642093
milestone2.2a1pre
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 642093 - map commonly-used bitmap font names to truetype equivalents for dwrite. r=bas
gfx/thebes/gfxDWriteFontList.cpp
gfx/thebes/gfxDWriteFontList.h
--- a/gfx/thebes/gfxDWriteFontList.cpp
+++ b/gfx/thebes/gfxDWriteFontList.cpp
@@ -886,16 +886,23 @@ gfxDWriteFontList::DelayedInitFontList()
 
         // at this point, all family names have been read in
         fam->SetOtherFamilyNamesInitialized();
     }
 
     mOtherFamilyNamesInitialized = PR_TRUE;
     GetFontSubstitutes();
 
+    // bug 642093 - DirectWrite does not support old bitmap (.fon)
+    // font files, but a few of these such as "Courier" and "MS Sans Serif"
+    // are frequently specified in shoddy CSS, without appropriate fallbacks.
+    // By mapping these to TrueType equivalents, we provide better consistency
+    // with both pre-DW systems and with IE9, which appears to do the same.
+    GetDirectWriteSubstitutes();
+
     // bug 551313 - DirectWrite creates a Gill Sans family out of 
     // poorly named members of the Gill Sans MT family containing
     // only Ultra Bold weights.  This causes big problems for pages
     // using Gill Sans which is usually only available on OSX
 
     nsAutoString nameGillSans(L"Gill Sans");
     nsAutoString nameGillSansMT(L"Gill Sans MT");
     BuildKeyNameFromFontName(nameGillSans);
@@ -1030,16 +1037,53 @@ gfxDWriteFontList::GetFontSubstitutes()
             mFontSubstitutes.Put(substituteName, ff);
         } else {
             mNonExistingFonts.AppendElement(substituteName);
         }
     }
     return NS_OK;
 }
 
+struct FontSubstitution {
+    const WCHAR* aliasName;
+    const WCHAR* actualName;
+};
+
+static const FontSubstitution sDirectWriteSubs[] = {
+    { L"MS Sans Serif", L"Microsoft Sans Serif" },
+    { L"MS Serif", L"Times New Roman" },
+    { L"Courier", L"Courier New" },
+    { L"Small Fonts", L"Arial" },
+    { L"Roman", L"Times New Roman" },
+    { L"Script", L"Mistral" }
+};
+
+void
+gfxDWriteFontList::GetDirectWriteSubstitutes()
+{
+    for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(sDirectWriteSubs); ++i) {
+        const FontSubstitution& sub(sDirectWriteSubs[i]);
+        nsAutoString substituteName((PRUnichar*)sub.aliasName);
+        BuildKeyNameFromFontName(substituteName);
+        if (nsnull != mFontFamilies.GetWeak(substituteName)) {
+            // don't do the substitution if user actually has a usable font
+            // with this name installed
+            continue;
+        }
+        nsAutoString actualFontName((PRUnichar*)sub.actualName);
+        BuildKeyNameFromFontName(actualFontName);
+        gfxFontFamily *ff;
+        if (nsnull != (ff = mFontFamilies.GetWeak(actualFontName))) {
+            mFontSubstitutes.Put(substituteName, ff);
+        } else {
+            mNonExistingFonts.AppendElement(substituteName);
+        }
+    }
+}
+
 PRBool
 gfxDWriteFontList::GetStandardFamilyName(const nsAString& aFontName,
                                          nsAString& aFamilyName)
 {
     gfxFontFamily *family = FindFamily(aFontName);
     if (family) {
         family->LocalizedName(aFamilyName);
         return PR_TRUE;
--- a/gfx/thebes/gfxDWriteFontList.h
+++ b/gfx/thebes/gfxDWriteFontList.h
@@ -225,16 +225,18 @@ public:
 
     virtual gfxFontFamily* FindFamily(const nsAString& aFamily);
 
 private:
     friend class gfxDWriteFontFamily;
 
     nsresult GetFontSubstitutes();
 
+    void GetDirectWriteSubstitutes();
+
     /**
      * Fonts listed in the registry as substitutes but for which no actual
      * font family is found.
      */
     nsTArray<nsString> mNonExistingFonts;
 
     typedef nsDataHashtable<nsStringHashKey, nsRefPtr<gfxFontFamily> > FontTable;