Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / win / FontFallbackWin.cpp
index 6f4d91d..d8afcdc 100644 (file)
@@ -31,7 +31,8 @@
 #include "config.h"
 #include "platform/fonts/win/FontFallbackWin.h"
 
-#include "platform/win/HWndDC.h"
+#include "SkFontMgr.h"
+#include "SkTypeface.h"
 #include "wtf/HashMap.h"
 #include "wtf/text/StringHash.h"
 #include "wtf/text/WTFString.h"
@@ -43,29 +44,48 @@ namespace WebCore {
 
 namespace {
 
-bool isFontPresent(const UChar* fontName)
+static inline bool isFontPresent(const UChar* fontName, SkFontMgr* fontManager)
 {
-    HFONT hfont = CreateFont(12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, fontName);
-    if (!hfont)
+    String family = fontName;
+    RefPtr<SkTypeface> tf = adoptRef(fontManager->legacyCreateTypeface(family.utf8().data(), SkTypeface::kNormal));
+    if (!tf)
         return false;
-    HWndDC dc(0);
-    HGDIOBJ oldFont = static_cast<HFONT>(SelectObject(dc, hfont));
-    WCHAR actualFontName[LF_FACESIZE];
-    GetTextFace(dc, LF_FACESIZE, actualFontName);
-    actualFontName[LF_FACESIZE - 1] = 0;
-    SelectObject(dc, oldFont);
-    DeleteObject(hfont);
-    // We don't have to worry about East Asian fonts with locale-dependent
-    // names here for now.
-    // FIXME: Why not?
-    return !wcscmp(fontName, actualFontName);
+
+    SkTypeface::LocalizedStrings* actualFamilies = tf->createFamilyNameIterator();
+    bool matchesRequestedFamily = false;
+    SkTypeface::LocalizedString actualFamily;
+    while (actualFamilies->next(&actualFamily)) {
+        if (equalIgnoringCase(family, AtomicString::fromUTF8(actualFamily.fString.c_str()))) {
+            matchesRequestedFamily = true;
+            break;
+        }
+    }
+    actualFamilies->unref();
+
+    return matchesRequestedFamily;
 }
 
 // A simple mapping from UScriptCode to family name. This is a sparse array,
 // which works well since the range of UScriptCode values is small.
 typedef const UChar* ScriptToFontMap[USCRIPT_CODE_LIMIT];
 
-void initializeScriptFontMap(ScriptToFontMap& scriptFontMap)
+void initializeScriptMonospaceFontMap(ScriptToFontMap& scriptFontMap, SkFontMgr* fontManager)
+{
+    struct FontMap {
+        UScriptCode script;
+        const UChar* family;
+    };
+
+    static const FontMap fontMap[] = {
+        { USCRIPT_HEBREW, L"courier new" },
+        { USCRIPT_ARABIC, L"courier new" },
+    };
+
+    for (size_t i = 0; i < WTF_ARRAY_LENGTH(fontMap); ++i)
+        scriptFontMap[fontMap[i].script] = fontMap[i].family;
+}
+
+void initializeScriptFontMap(ScriptToFontMap& scriptFontMap, SkFontMgr* fontManager)
 {
     struct FontMap {
         UScriptCode script;
@@ -160,7 +180,7 @@ void initializeScriptFontMap(ScriptToFontMap& scriptFontMap)
         scriptFontMap[script] = 0;
         const UChar** familyPtr = scriptToFontFamilies[i].families;
         while (*familyPtr) {
-            if (isFontPresent(*familyPtr)) {
+            if (isFontPresent(*familyPtr, fontManager)) {
                 scriptFontMap[script] = *familyPtr;
                 break;
             }
@@ -251,17 +271,22 @@ UScriptCode getScript(int ucs4)
 //  - Update script_font_cache in response to WM_FONTCHANGE
 
 const UChar* getFontFamilyForScript(UScriptCode script,
-    FontDescription::GenericFamilyType generic)
+    FontDescription::GenericFamilyType generic,
+    SkFontMgr* fontManager)
 {
     static ScriptToFontMap scriptFontMap;
+    static ScriptToFontMap scriptMonospaceFontMap;
     static bool initialized = false;
     if (!initialized) {
-        initializeScriptFontMap(scriptFontMap);
+        initializeScriptFontMap(scriptFontMap, fontManager);
+        initializeScriptMonospaceFontMap(scriptMonospaceFontMap, fontManager);
         initialized = true;
     }
     if (script == USCRIPT_INVALID_CODE)
         return 0;
     ASSERT(script < USCRIPT_CODE_LIMIT);
+    if (generic == FontDescription::MonospaceFamily && scriptMonospaceFontMap[script])
+        return scriptMonospaceFontMap[script];
     return scriptFontMap[script];
 }
 
@@ -275,7 +300,8 @@ const UChar* getFontFamilyForScript(UScriptCode script,
 //    font can cover) need to be taken into account
 const UChar* getFallbackFamily(UChar32 character,
     FontDescription::GenericFamilyType generic,
-    UScriptCode* scriptChecked)
+    UScriptCode* scriptChecked,
+    SkFontMgr* fontManager)
 {
     ASSERT(character);
     UScriptCode script = getScript(character);
@@ -290,7 +316,7 @@ const UChar* getFallbackFamily(UChar32 character,
     if (script == USCRIPT_COMMON)
         script = getScriptBasedOnUnicodeBlock(character);
 
-    const UChar* family = getFontFamilyForScript(script, generic);
+    const UChar* family = getFontFamilyForScript(script, generic, fontManager);
     // Another lame work-around to cover non-BMP characters.
     // If the font family for script is not found or the character is
     // not in BMP (> U+FFFF), we resort to the hard-coded list of
@@ -322,27 +348,4 @@ const UChar* getFallbackFamily(UChar32 character,
     return family;
 }
 
-
-const UChar* getFallbackFamilyForFirstNonCommonCharacter(const UChar* characters,
-    int length,
-    FontDescription::GenericFamilyType generic)
-{
-    ASSERT(characters && characters[0] && length > 0);
-    UScriptCode script = USCRIPT_COMMON;
-
-    // Sometimes characters common to script (e.g. space) is at
-    // the beginning of a string so that we need to skip them
-    // to get a font required to render the string.
-    int i = 0;
-    UChar32 ucs4 = 0;
-    while (i < length && script == USCRIPT_COMMON) {
-        U16_NEXT(characters, i, length, ucs4);
-        script = getScript(ucs4);
-    }
-
-    const UChar* family = getFallbackFamily(ucs4, generic, 0);
-
-    return family;
-}
-
 } // namespace WebCore