| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 Google Inc. All rights reserved. | 2 * Copyright (c) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "platform/fonts/AcceptLanguagesResolver.h" | 34 #include "platform/fonts/AcceptLanguagesResolver.h" |
| 35 #include "platform/fonts/SimpleFontData.h" | 35 #include "platform/fonts/SimpleFontData.h" |
| 36 #include "platform/fonts/FontDescription.h" | 36 #include "platform/fonts/FontDescription.h" |
| 37 #include "platform/fonts/FontFaceCreationParams.h" | 37 #include "platform/fonts/FontFaceCreationParams.h" |
| 38 #include "platform/text/LocaleToScriptMapping.h" | 38 #include "platform/text/LocaleToScriptMapping.h" |
| 39 #include "third_party/skia/include/core/SkTypeface.h" | 39 #include "third_party/skia/include/core/SkTypeface.h" |
| 40 #include "third_party/skia/include/ports/SkFontMgr.h" | 40 #include "third_party/skia/include/ports/SkFontMgr.h" |
| 41 | 41 |
| 42 namespace blink { | 42 namespace blink { |
| 43 | 43 |
| 44 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", | 44 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescrip
tion& fontDescription, UChar32 c, const SimpleFontData*) |
| 45 // instead of "zh-CN" and "zh-TW". | |
| 46 static CString toSkFontMgrLocale(const String& locale) | |
| 47 { | |
| 48 if (!locale.startsWith("zh", TextCaseInsensitive)) | |
| 49 return locale.ascii(); | |
| 50 | |
| 51 switch (localeToScriptCodeForFontSelection(locale)) { | |
| 52 case USCRIPT_SIMPLIFIED_HAN: | |
| 53 return "zh-Hans"; | |
| 54 case USCRIPT_TRADITIONAL_HAN: | |
| 55 return "zh-Hant"; | |
| 56 default: | |
| 57 return locale.ascii(); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 static AtomicString getFamilyNameForCharacter(UChar32 c, const FontDescription&
fontDescription) | |
| 62 { | 45 { |
| 63 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); | 46 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); |
| 64 const char* bcp47Locales[3]; | 47 AtomicString familyName = getFamilyNameForCharacter(fm.get(), c, fontDescrip
tion); |
| 65 int localeCount = 0; | |
| 66 if (const char* hanLocale = AcceptLanguagesResolver::preferredHanSkFontMgrLo
cale()) | |
| 67 bcp47Locales[localeCount++] = hanLocale; | |
| 68 CString defaultLocale = toSkFontMgrLocale(defaultLanguage()); | |
| 69 bcp47Locales[localeCount++] = defaultLocale.data(); | |
| 70 CString fontLocale; | |
| 71 if (!fontDescription.locale().isEmpty()) { | |
| 72 fontLocale = toSkFontMgrLocale(fontDescription.locale()); | |
| 73 bcp47Locales[localeCount++] = fontLocale.data(); | |
| 74 } | |
| 75 RefPtr<SkTypeface> typeface = adoptRef(fm->matchFamilyStyleCharacter(0, SkFo
ntStyle(), bcp47Locales, localeCount, c)); | |
| 76 if (!typeface) | |
| 77 return emptyAtom; | |
| 78 | |
| 79 SkString skiaFamilyName; | |
| 80 typeface->getFamilyName(&skiaFamilyName); | |
| 81 return skiaFamilyName.c_str(); | |
| 82 } | |
| 83 | |
| 84 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescrip
tion& fontDescription, UChar32 c, const SimpleFontData*) | |
| 85 { | |
| 86 AtomicString familyName = getFamilyNameForCharacter(c, fontDescription); | |
| 87 if (familyName.isEmpty()) | 48 if (familyName.isEmpty()) |
| 88 return getLastResortFallbackFont(fontDescription, DoNotRetain); | 49 return getLastResortFallbackFont(fontDescription, DoNotRetain); |
| 89 return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, Fon
tFaceCreationParams(familyName)), DoNotRetain); | 50 return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, Fon
tFaceCreationParams(familyName)), DoNotRetain); |
| 90 } | 51 } |
| 91 | 52 |
| 92 // static | 53 // static |
| 93 AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& family
Name, const FontDescription& fontDescription) | 54 AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& family
Name, const FontDescription& fontDescription) |
| 94 { | 55 { |
| 95 // This is a hack to use the preferred font for CJK scripts. | 56 // This is a hack to use the preferred font for CJK scripts. |
| 96 // FIXME: Use new Skia API once Android system supports per-family and per-s
cript fallback fonts. | 57 // FIXME: Use new Skia API once Android system supports per-family and per-s
cript fallback fonts. |
| 97 UChar32 examplerChar; | 58 UChar32 examplerChar; |
| 98 switch (fontDescription.script()) { | 59 switch (fontDescription.script()) { |
| 99 case USCRIPT_SIMPLIFIED_HAN: | 60 case USCRIPT_SIMPLIFIED_HAN: |
| 100 case USCRIPT_TRADITIONAL_HAN: | 61 case USCRIPT_TRADITIONAL_HAN: |
| 101 case USCRIPT_KATAKANA_OR_HIRAGANA: | 62 case USCRIPT_KATAKANA_OR_HIRAGANA: |
| 102 examplerChar = 0x4E00; // A common character in Japanese and Chinese. | 63 examplerChar = 0x4E00; // A common character in Japanese and Chinese. |
| 103 break; | 64 break; |
| 104 case USCRIPT_HANGUL: | 65 case USCRIPT_HANGUL: |
| 105 examplerChar = 0xAC00; | 66 examplerChar = 0xAC00; |
| 106 break; | 67 break; |
| 107 default: | 68 default: |
| 108 // For other scripts, use the default generic family mapping logic. | 69 // For other scripts, use the default generic family mapping logic. |
| 109 return familyName; | 70 return familyName; |
| 110 } | 71 } |
| 111 | 72 |
| 112 return getFamilyNameForCharacter(examplerChar, fontDescription); | 73 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); |
| 74 return getFamilyNameForCharacter(fm.get(), examplerChar, fontDescription); |
| 113 } | 75 } |
| 114 | 76 |
| 115 } // namespace blink | 77 } // namespace blink |
| OLD | NEW |