| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. | 2 * Copyright (c) 2006, 2007, 2008, 2009 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 "SkFontMgr.h" | 31 #include "SkFontMgr.h" |
| 32 #include "SkStream.h" | 32 #include "SkStream.h" |
| 33 #include "SkTypeface.h" | 33 #include "SkTypeface.h" |
| 34 #include "platform/Language.h" |
| 35 #include "platform/fonts/AcceptLanguagesResolver.h" |
| 34 #include "platform/fonts/AlternateFontFamily.h" | 36 #include "platform/fonts/AlternateFontFamily.h" |
| 35 #include "platform/fonts/FontCache.h" | 37 #include "platform/fonts/FontCache.h" |
| 36 #include "platform/fonts/FontDescription.h" | 38 #include "platform/fonts/FontDescription.h" |
| 37 #include "platform/fonts/FontFaceCreationParams.h" | 39 #include "platform/fonts/FontFaceCreationParams.h" |
| 38 #include "platform/fonts/SimpleFontData.h" | 40 #include "platform/fonts/SimpleFontData.h" |
| 39 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
| 40 #include "public/platform/linux/WebSandboxSupport.h" | 42 #include "public/platform/linux/WebSandboxSupport.h" |
| 41 #include "wtf/Assertions.h" | 43 #include "wtf/Assertions.h" |
| 42 #include "wtf/text/AtomicString.h" | 44 #include "wtf/text/AtomicString.h" |
| 43 #include "wtf/text/CString.h" | 45 #include "wtf/text/CString.h" |
| 44 #include <unicode/locid.h> | 46 #include <unicode/locid.h> |
| 45 | 47 |
| 46 #if !OS(WIN) && !OS(ANDROID) | 48 #if !OS(WIN) && !OS(ANDROID) |
| 47 #include "SkFontConfigInterface.h" | 49 #include "SkFontConfigInterface.h" |
| 48 | 50 |
| 49 static PassRefPtr<SkTypeface> typefaceForFontconfigInterfaceIdAndTtcIndex(int fo
ntconfigInterfaceId, int ttcIndex) | 51 static PassRefPtr<SkTypeface> typefaceForFontconfigInterfaceIdAndTtcIndex(int fo
ntconfigInterfaceId, int ttcIndex) |
| 50 { | 52 { |
| 51 SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal()); | 53 SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal()); |
| 52 SkFontConfigInterface::FontIdentity fontIdentity; | 54 SkFontConfigInterface::FontIdentity fontIdentity; |
| 53 fontIdentity.fID = fontconfigInterfaceId; | 55 fontIdentity.fID = fontconfigInterfaceId; |
| 54 fontIdentity.fTTCIndex = ttcIndex; | 56 fontIdentity.fTTCIndex = ttcIndex; |
| 55 return adoptRef(fci->createTypeface(fontIdentity)); | 57 return adoptRef(fci->createTypeface(fontIdentity)); |
| 56 } | 58 } |
| 57 #endif | 59 #endif |
| 58 | 60 |
| 59 namespace blink { | 61 namespace blink { |
| 60 | 62 |
| 63 // Android special locale for retrieving the color emoji font |
| 64 // based on the proposed changes in UTR #51 for introducing |
| 65 // an Emoji script code: |
| 66 // http://www.unicode.org/reports/tr51/proposed.html#Emoji_Script |
| 67 static const char* kAndroidColorEmojiLocale = "und-Zsye"; |
| 68 |
| 69 // SkFontMgr requires script-based locale names, like "zh-Hant" and "zh-Hans", |
| 70 // instead of "zh-CN" and "zh-TW". |
| 71 static CString toSkFontMgrLocale(const String& locale) |
| 72 { |
| 73 if (!locale.startsWith("zh", TextCaseInsensitive)) |
| 74 return locale.ascii(); |
| 75 |
| 76 switch (localeToScriptCodeForFontSelection(locale)) { |
| 77 case USCRIPT_SIMPLIFIED_HAN: |
| 78 return "zh-Hans"; |
| 79 case USCRIPT_TRADITIONAL_HAN: |
| 80 return "zh-Hant"; |
| 81 default: |
| 82 return locale.ascii(); |
| 83 } |
| 84 } |
| 85 |
| 86 #if OS(ANDROID) || OS(LINUX) |
| 87 // This function is called on android or when we are emulating android fonts on
linux and the |
| 88 // embedder has overriden the default fontManager with WebFontRendering::setSkia
FontMgr. |
| 89 // static |
| 90 AtomicString FontCache::getFamilyNameForCharacter(SkFontMgr* fm, UChar32 c, cons
t FontDescription& fontDescription, FontFallbackPriority fallbackPriority) |
| 91 { |
| 92 ASSERT(fm); |
| 93 |
| 94 const size_t kMaxLocales = 4; |
| 95 const char* bcp47Locales[kMaxLocales]; |
| 96 size_t localeCount = 0; |
| 97 |
| 98 if (fallbackPriority == FontFallbackPriority::EmojiEmoji) { |
| 99 bcp47Locales[localeCount++] = kAndroidColorEmojiLocale; |
| 100 } |
| 101 if (const char* hanLocale = AcceptLanguagesResolver::preferredHanSkFontMgrLo
cale()) |
| 102 bcp47Locales[localeCount++] = hanLocale; |
| 103 CString defaultLocale = toSkFontMgrLocale(defaultLanguage()); |
| 104 bcp47Locales[localeCount++] = defaultLocale.data(); |
| 105 CString fontLocale; |
| 106 if (!fontDescription.locale().isEmpty()) { |
| 107 fontLocale = toSkFontMgrLocale(fontDescription.locale()); |
| 108 bcp47Locales[localeCount++] = fontLocale.data(); |
| 109 } |
| 110 ASSERT_WITH_SECURITY_IMPLICATION(localeCount < kMaxLocales); |
| 111 RefPtr<SkTypeface> typeface = adoptRef(fm->matchFamilyStyleCharacter(0, SkFo
ntStyle(), bcp47Locales, localeCount, c)); |
| 112 if (!typeface) |
| 113 return emptyAtom; |
| 114 |
| 115 SkString skiaFamilyName; |
| 116 typeface->getFamilyName(&skiaFamilyName); |
| 117 return skiaFamilyName.c_str(); |
| 118 } |
| 119 #endif |
| 120 |
| 61 void FontCache::platformInit() | 121 void FontCache::platformInit() |
| 62 { | 122 { |
| 63 } | 123 } |
| 64 | 124 |
| 65 PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle( | 125 PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle( |
| 66 const FontDescription& fontDescription, UChar32 character) | 126 const FontDescription& fontDescription, UChar32 character) |
| 67 { | 127 { |
| 68 FontDescription substituteDescription(fontDescription); | 128 FontDescription substituteDescription(fontDescription); |
| 69 substituteDescription.setStyle(FontStyleNormal); | 129 substituteDescription.setStyle(FontStyleNormal); |
| 70 substituteDescription.setWeight(FontWeightNormal); | 130 substituteDescription.setWeight(FontWeightNormal); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 if (!fontPlatformData) { | 164 if (!fontPlatformData) { |
| 105 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationPar
ams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral)))
; | 165 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationPar
ams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral)))
; |
| 106 fontPlatformData = getFontPlatformData(description, mssansserifCreationP
arams); | 166 fontPlatformData = getFontPlatformData(description, mssansserifCreationP
arams); |
| 107 } | 167 } |
| 108 #endif | 168 #endif |
| 109 | 169 |
| 110 ASSERT(fontPlatformData); | 170 ASSERT(fontPlatformData); |
| 111 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain); | 171 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain); |
| 112 } | 172 } |
| 113 | 173 |
| 114 #if OS(WIN) | 174 #if OS(WIN) || OS(LINUX) |
| 115 static inline SkFontStyle fontStyle(const FontDescription& fontDescription) | 175 static inline SkFontStyle fontStyle(const FontDescription& fontDescription) |
| 116 { | 176 { |
| 117 int width = static_cast<int>(fontDescription.stretch()); | 177 int width = static_cast<int>(fontDescription.stretch()); |
| 118 int weight = (fontDescription.weight() - FontWeight100 + 1) * 100; | 178 int weight = (fontDescription.weight() - FontWeight100 + 1) * 100; |
| 119 SkFontStyle::Slant slant = fontDescription.style() == FontStyleItalic | 179 SkFontStyle::Slant slant = fontDescription.style() == FontStyleItalic |
| 120 ? SkFontStyle::kItalic_Slant | 180 ? SkFontStyle::kItalic_Slant |
| 121 : SkFontStyle::kUpright_Slant; | 181 : SkFontStyle::kUpright_Slant; |
| 122 return SkFontStyle(weight, width, slant); | 182 return SkFontStyle(weight, width, slant); |
| 123 } | 183 } |
| 124 | 184 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 225 } |
| 166 | 226 |
| 167 if (m_fontManager) { | 227 if (m_fontManager) { |
| 168 return adoptRef(useDirectWrite() | 228 return adoptRef(useDirectWrite() |
| 169 ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescrip
tion)) | 229 ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescrip
tion)) |
| 170 : m_fontManager->legacyCreateTypeface(name.data(), style) | 230 : m_fontManager->legacyCreateTypeface(name.data(), style) |
| 171 ); | 231 ); |
| 172 } | 232 } |
| 173 #endif | 233 #endif |
| 174 | 234 |
| 235 #if OS(LINUX) |
| 236 // On linux if the fontManager has been overridden then we should be calling
the embedder |
| 237 // provided font Manager rather than calling SkTypeface::CreateFromName whic
h may redirect the |
| 238 // call to the default font Manager. |
| 239 if (m_fontManager) |
| 240 return adoptRef(m_fontManager->matchFamilyStyle(name.data(), fontStyle(f
ontDescription))); |
| 241 #endif |
| 242 |
| 175 // FIXME: Use m_fontManager, SkFontStyle and matchFamilyStyle instead of | 243 // FIXME: Use m_fontManager, SkFontStyle and matchFamilyStyle instead of |
| 176 // CreateFromName on all platforms. | 244 // CreateFromName on all platforms. |
| 177 return adoptRef(SkTypeface::CreateFromName(name.data(), static_cast<SkTypefa
ce::Style>(style))); | 245 return adoptRef(SkTypeface::CreateFromName(name.data(), static_cast<SkTypefa
ce::Style>(style))); |
| 178 } | 246 } |
| 179 | 247 |
| 180 #if !OS(WIN) | 248 #if !OS(WIN) |
| 181 PassOwnPtr<FontPlatformData> FontCache::createFontPlatformData(const FontDescrip
tion& fontDescription, | 249 PassOwnPtr<FontPlatformData> FontCache::createFontPlatformData(const FontDescrip
tion& fontDescription, |
| 182 const FontFaceCreationParams& creationParams, float fontSize) | 250 const FontFaceCreationParams& creationParams, float fontSize) |
| 183 { | 251 { |
| 184 CString name; | 252 CString name; |
| 185 RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name))
; | 253 RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name))
; |
| 186 if (!tf) | 254 if (!tf) |
| 187 return nullptr; | 255 return nullptr; |
| 188 | 256 |
| 189 return adoptPtr(new FontPlatformData(tf, | 257 return adoptPtr(new FontPlatformData(tf, |
| 190 name.data(), | 258 name.data(), |
| 191 fontSize, | 259 fontSize, |
| 192 (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDesc
ription.isSyntheticBold(), | 260 (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDesc
ription.isSyntheticBold(), |
| 193 ((fontDescription.style() == FontStyleItalic || fontDescription.style()
== FontStyleOblique) && !tf->isItalic()) || fontDescription.isSyntheticItalic(), | 261 ((fontDescription.style() == FontStyleItalic || fontDescription.style()
== FontStyleOblique) && !tf->isItalic()) || fontDescription.isSyntheticItalic(), |
| 194 fontDescription.orientation(), | 262 fontDescription.orientation(), |
| 195 fontDescription.useSubpixelPositioning())); | 263 fontDescription.useSubpixelPositioning())); |
| 196 } | 264 } |
| 197 #endif // !OS(WIN) | 265 #endif // !OS(WIN) |
| 198 | 266 |
| 199 } // namespace blink | 267 } // namespace blink |
| OLD | NEW |