Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / fonts / android / FontCacheAndroid.cpp
blob419e593c2dfb862deaa802f075f4d498902bd3d3
1 /*
2 * Copyright (c) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 #include "config.h"
32 #include "platform/fonts/FontCache.h"
34 #include "platform/Language.h"
35 #include "platform/fonts/SimpleFontData.h"
36 #include "platform/fonts/FontDescription.h"
37 #include "platform/fonts/FontFaceCreationParams.h"
38 #include "platform/text/LocaleToScriptMapping.h"
39 #include "third_party/skia/include/core/SkTypeface.h"
40 #include "third_party/skia/include/ports/SkFontMgr.h"
42 namespace blink {
44 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans",
45 // instead of "zh-CN" and "zh-TW".
46 static CString toSkFontMgrLocale(const String& locale)
48 if (!locale.startsWith("zh", TextCaseInsensitive))
49 return locale.ascii();
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();
61 static AtomicString getFamilyNameForCharacter(UChar32 c, const FontDescription& fontDescription)
63 RefPtr<SkFontMgr> fm = adoptRef(SkFontMgr::RefDefault());
64 const char* bcp47Locales[2];
65 int localeCount = 0;
66 CString defaultLocale = toSkFontMgrLocale(defaultLanguage());
67 bcp47Locales[localeCount++] = defaultLocale.data();
68 CString fontLocale;
69 if (!fontDescription.locale().isEmpty()) {
70 fontLocale = toSkFontMgrLocale(fontDescription.locale());
71 bcp47Locales[localeCount++] = fontLocale.data();
73 RefPtr<SkTypeface> typeface = adoptRef(fm->matchFamilyStyleCharacter(0, SkFontStyle(), bcp47Locales, localeCount, c));
74 if (!typeface)
75 return emptyAtom;
77 SkString skiaFamilyName;
78 typeface->getFamilyName(&skiaFamilyName);
79 return skiaFamilyName.c_str();
82 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
84 AtomicString familyName = getFamilyNameForCharacter(c, fontDescription);
85 if (familyName.isEmpty())
86 return getLastResortFallbackFont(fontDescription, DoNotRetain);
87 return fontDataFromFontPlatformData(getFontPlatformData(fontDescription, FontFaceCreationParams(familyName)), DoNotRetain);
90 // static
91 AtomicString FontCache::getGenericFamilyNameForScript(const AtomicString& familyName, const FontDescription& fontDescription)
93 // This is a hack to use the preferred font for CJK scripts.
94 // FIXME: Use new Skia API once Android system supports per-family and per-script fallback fonts.
95 UChar32 examplerChar;
96 switch (fontDescription.script()) {
97 case USCRIPT_SIMPLIFIED_HAN:
98 case USCRIPT_TRADITIONAL_HAN:
99 case USCRIPT_KATAKANA_OR_HIRAGANA:
100 examplerChar = 0x4E00; // A common character in Japanese and Chinese.
101 break;
102 case USCRIPT_HANGUL:
103 examplerChar = 0xAC00;
104 break;
105 default:
106 // For other scripts, use the default generic family mapping logic.
107 return familyName;
110 return getFamilyNameForCharacter(examplerChar, fontDescription);
113 } // namespace blink