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 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "platform/fonts/FontCache.h" | 31 #include "platform/fonts/FontCache.h" |
32 | 32 |
33 #include "platform/Language.h" | 33 #include "platform/Language.h" |
34 #include "platform/fonts/AcceptLanguagesResolver.h" | |
35 #include "platform/fonts/SimpleFontData.h" | 34 #include "platform/fonts/SimpleFontData.h" |
36 #include "platform/fonts/FontDescription.h" | 35 #include "platform/fonts/FontDescription.h" |
37 #include "platform/fonts/FontFaceCreationParams.h" | 36 #include "platform/fonts/FontFaceCreationParams.h" |
38 #include "platform/text/LocaleToScriptMapping.h" | 37 #include "platform/text/LocaleToScriptMapping.h" |
39 #include "third_party/skia/include/core/SkTypeface.h" | 38 #include "third_party/skia/include/core/SkTypeface.h" |
40 #include "third_party/skia/include/ports/SkFontMgr.h" | 39 #include "third_party/skia/include/ports/SkFontMgr.h" |
41 | 40 |
42 namespace blink { | 41 namespace blink { |
43 | 42 |
44 // Android special locale for retrieving the color emoji font | |
45 // based on the proposed changes in UTR #51 for introducing | |
46 // an Emoji script code: | |
47 // http://www.unicode.org/reports/tr51/proposed.html#Emoji_Script | |
48 static const char* kAndroidColorEmojiLocale = "und-Zsye"; | |
49 | |
50 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", | |
51 // instead of "zh-CN" and "zh-TW". | |
52 static CString toSkFontMgrLocale(const String& locale) | |
53 { | |
54 if (!locale.startsWith("zh", TextCaseInsensitive)) | |
55 return locale.ascii(); | |
56 | |
57 switch (localeToScriptCodeForFontSelection(locale)) { | |
58 case USCRIPT_SIMPLIFIED_HAN: | |
59 return "zh-Hans"; | |
60 case USCRIPT_TRADITIONAL_HAN: | |
61 return "zh-Hant"; | |
62 default: | |
63 return locale.ascii(); | |
64 } | |
65 } | |
66 | |
67 static AtomicString getFamilyNameForCharacter(UChar32 c, const FontDescription&
fontDescription, FontFallbackPriority fallbackPriority) | |
68 { | |
69 const size_t kMaxLocales = 4; | |
70 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); | |
71 const char* bcp47Locales[kMaxLocales]; | |
72 size_t localeCount = 0; | |
73 | |
74 if (fallbackPriority == FontFallbackPriority::EmojiEmoji) { | |
75 bcp47Locales[localeCount++] = kAndroidColorEmojiLocale; | |
76 } | |
77 if (const char* hanLocale = AcceptLanguagesResolver::preferredHanSkFontMgrLo
cale()) | |
78 bcp47Locales[localeCount++] = hanLocale; | |
79 CString defaultLocale = toSkFontMgrLocale(defaultLanguage()); | |
80 bcp47Locales[localeCount++] = defaultLocale.data(); | |
81 CString fontLocale; | |
82 if (!fontDescription.locale().isEmpty()) { | |
83 fontLocale = toSkFontMgrLocale(fontDescription.locale()); | |
84 bcp47Locales[localeCount++] = fontLocale.data(); | |
85 } | |
86 ASSERT_WITH_SECURITY_IMPLICATION(localeCount < kMaxLocales); | |
87 RefPtr<SkTypeface> typeface = adoptRef(fm->matchFamilyStyleCharacter(0, SkFo
ntStyle(), bcp47Locales, localeCount, c)); | |
88 if (!typeface) | |
89 return emptyAtom; | |
90 | |
91 SkString skiaFamilyName; | |
92 typeface->getFamilyName(&skiaFamilyName); | |
93 return skiaFamilyName.c_str(); | |
94 } | |
95 | |
96 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescrip
tion& fontDescription, UChar32 c, const SimpleFontData*, FontFallbackPriority fa
llbackPriority) | 43 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescrip
tion& fontDescription, UChar32 c, const SimpleFontData*, FontFallbackPriority fa
llbackPriority) |
97 { | 44 { |
98 AtomicString familyName = getFamilyNameForCharacter(c, fontDescription, fall
backPriority); | 45 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); |
| 46 AtomicString familyName = getFamilyNameForCharacter(fm.get(), c, fontDescrip
tion, fallbackPriority); |
99 if (familyName.isEmpty()) | 47 if (familyName.isEmpty()) |
100 return getLastResortFallbackFont(fontDescription, DoNotRetain); | 48 return getLastResortFallbackFont(fontDescription, DoNotRetain); |
101 return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, Fon
tFaceCreationParams(familyName)), DoNotRetain); | 49 return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, Fon
tFaceCreationParams(familyName)), DoNotRetain); |
102 } | 50 } |
103 | 51 |
104 // static | 52 // static |
105 AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& family
Name, const FontDescription& fontDescription) | 53 AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& family
Name, const FontDescription& fontDescription) |
106 { | 54 { |
107 // This is a hack to use the preferred font for CJK scripts. | 55 // This is a hack to use the preferred font for CJK scripts. |
108 // FIXME: Use new Skia API once Android system supports per-family and per-s
cript fallback fonts. | 56 // FIXME: Use new Skia API once Android system supports per-family and per-s
cript fallback fonts. |
109 UChar32 examplerChar; | 57 UChar32 examplerChar; |
110 switch (fontDescription.script()) { | 58 switch (fontDescription.script()) { |
111 case USCRIPT_SIMPLIFIED_HAN: | 59 case USCRIPT_SIMPLIFIED_HAN: |
112 case USCRIPT_TRADITIONAL_HAN: | 60 case USCRIPT_TRADITIONAL_HAN: |
113 case USCRIPT_KATAKANA_OR_HIRAGANA: | 61 case USCRIPT_KATAKANA_OR_HIRAGANA: |
114 examplerChar = 0x4E00; // A common character in Japanese and Chinese. | 62 examplerChar = 0x4E00; // A common character in Japanese and Chinese. |
115 break; | 63 break; |
116 case USCRIPT_HANGUL: | 64 case USCRIPT_HANGUL: |
117 examplerChar = 0xAC00; | 65 examplerChar = 0xAC00; |
118 break; | 66 break; |
119 default: | 67 default: |
120 // For other scripts, use the default generic family mapping logic. | 68 // For other scripts, use the default generic family mapping logic. |
121 return familyName; | 69 return familyName; |
122 } | 70 } |
123 | 71 |
124 return getFamilyNameForCharacter(examplerChar, fontDescription, FontFallback
Priority::Text); | 72 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault()); |
| 73 return getFamilyNameForCharacter(fm.get(), examplerChar, fontDescription, Fo
ntFallbackPriority::Text); |
125 } | 74 } |
126 | 75 |
127 } // namespace blink | 76 } // namespace blink |
OLD | NEW |