1/* 2 * Copyright (c) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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. 17 * 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. 29 */ 30 31#include "config.h" 32 33#if !OS(WIN) && !OS(ANDROID) 34#include "SkFontConfigInterface.h" 35#endif 36#include "SkFontMgr.h" 37#include "SkStream.h" 38#include "SkTypeface.h" 39#include "platform/NotImplemented.h" 40#include "platform/fonts/AlternateFontFamily.h" 41#include "platform/fonts/FontCache.h" 42#include "platform/fonts/FontDescription.h" 43#include "platform/fonts/FontFaceCreationParams.h" 44#include "platform/fonts/SimpleFontData.h" 45#include "public/platform/Platform.h" 46#include "public/platform/linux/WebSandboxSupport.h" 47#include "wtf/Assertions.h" 48#include "wtf/text/AtomicString.h" 49#include "wtf/text/CString.h" 50#include <unicode/locid.h> 51 52#if !OS(WIN) && !OS(ANDROID) 53static SkStream* streamForFontconfigInterfaceId(int fontconfigInterfaceId) 54{ 55 SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal()); 56 SkFontConfigInterface::FontIdentity fontIdentity; 57 fontIdentity.fID = fontconfigInterfaceId; 58 return fci->openStream(fontIdentity); 59} 60#endif 61 62namespace blink { 63 64void FontCache::platformInit() 65{ 66} 67 68PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle( 69 const FontDescription& fontDescription, UChar32 character) 70{ 71 FontDescription substituteDescription(fontDescription); 72 substituteDescription.setStyle(FontStyleNormal); 73 substituteDescription.setWeight(FontWeightNormal); 74 75 FontFaceCreationParams creationParams(substituteDescription.family().family()); 76 FontPlatformData* substitutePlatformData = getFontPlatformData(substituteDescription, creationParams); 77 if (substitutePlatformData && substitutePlatformData->fontContainsCharacter(character)) { 78 FontPlatformData platformData = FontPlatformData(*substitutePlatformData); 79 platformData.setSyntheticBold(fontDescription.weight() >= FontWeight600); 80 platformData.setSyntheticItalic(fontDescription.style() == FontStyleItalic); 81 return fontDataFromFontPlatformData(&platformData, DoNotRetain); 82 } 83 84 return nullptr; 85} 86 87#if !OS(WIN) && !OS(ANDROID) 88PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*) 89{ 90 // First try the specified font with standard style & weight. 91 if (fontDescription.style() == FontStyleItalic 92 || fontDescription.weight() >= FontWeight600) { 93 RefPtr<SimpleFontData> fontData = fallbackOnStandardFontStyle( 94 fontDescription, c); 95 if (fontData) 96 return fontData; 97 } 98 99 FontCache::PlatformFallbackFont fallbackFont; 100 FontCache::getFontForCharacter(c, fontDescription.locale().ascii().data(), &fallbackFont); 101 if (fallbackFont.name.isEmpty()) 102 return nullptr; 103 104 FontFaceCreationParams creationParams; 105 creationParams = FontFaceCreationParams(fallbackFont.filename, fallbackFont.fontconfigInterfaceId, fallbackFont.ttcIndex); 106 107 // Changes weight and/or italic of given FontDescription depends on 108 // the result of fontconfig so that keeping the correct font mapping 109 // of the given character. See http://crbug.com/32109 for details. 110 bool shouldSetSyntheticBold = false; 111 bool shouldSetSyntheticItalic = false; 112 FontDescription description(fontDescription); 113 if (fallbackFont.isBold && description.weight() < FontWeightBold) 114 description.setWeight(FontWeightBold); 115 if (!fallbackFont.isBold && description.weight() >= FontWeightBold) { 116 shouldSetSyntheticBold = true; 117 description.setWeight(FontWeightNormal); 118 } 119 if (fallbackFont.isItalic && description.style() == FontStyleNormal) 120 description.setStyle(FontStyleItalic); 121 if (!fallbackFont.isItalic && description.style() == FontStyleItalic) { 122 shouldSetSyntheticItalic = true; 123 description.setStyle(FontStyleNormal); 124 } 125 126 FontPlatformData* substitutePlatformData = getFontPlatformData(description, creationParams); 127 if (!substitutePlatformData) 128 return nullptr; 129 FontPlatformData platformData = FontPlatformData(*substitutePlatformData); 130 platformData.setSyntheticBold(shouldSetSyntheticBold); 131 platformData.setSyntheticItalic(shouldSetSyntheticItalic); 132 return fontDataFromFontPlatformData(&platformData, DoNotRetain); 133} 134 135#endif // !OS(WIN) && !OS(ANDROID) 136 137PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain) 138{ 139 const FontFaceCreationParams fallbackCreationParams(getFallbackFontFamily(description)); 140 const FontPlatformData* fontPlatformData = getFontPlatformData(description, fallbackCreationParams); 141 142 // We should at least have Sans or Arial which is the last resort fallback of SkFontHost ports. 143 if (!fontPlatformData) { 144 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, sansCreationParams, (AtomicString("Sans", AtomicString::ConstructFromLiteral))); 145 fontPlatformData = getFontPlatformData(description, sansCreationParams); 146 } 147 if (!fontPlatformData) { 148 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, arialCreationParams, (AtomicString("Arial", AtomicString::ConstructFromLiteral))); 149 fontPlatformData = getFontPlatformData(description, arialCreationParams); 150 } 151#if OS(WIN) 152 // Try some more Windows-specific fallbacks. 153 if (!fontPlatformData) { 154 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, msuigothicCreationParams, (AtomicString("MS UI Gothic", AtomicString::ConstructFromLiteral))); 155 fontPlatformData = getFontPlatformData(description, msuigothicCreationParams); 156 } 157 if (!fontPlatformData) { 158 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationParams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral))); 159 fontPlatformData = getFontPlatformData(description, mssansserifCreationParams); 160 } 161#endif 162 163 ASSERT(fontPlatformData); 164 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain); 165} 166 167#if OS(WIN) 168static inline SkFontStyle fontStyle(const FontDescription& fontDescription) 169{ 170 int width = static_cast<int>(fontDescription.stretch()); 171 int weight = (fontDescription.weight() - FontWeight100 + 1) * 100; 172 SkFontStyle::Slant slant = fontDescription.style() == FontStyleItalic 173 ? SkFontStyle::kItalic_Slant 174 : SkFontStyle::kUpright_Slant; 175 return SkFontStyle(weight, width, slant); 176} 177 178COMPILE_ASSERT(static_cast<int>(FontStretchUltraCondensed) == static_cast<int>(SkFontStyle::kUltraCondensed_Width), 179 FontStretchUltraCondensedMapsTokUltraCondensed_Width); 180COMPILE_ASSERT(static_cast<int>(FontStretchNormal) == static_cast<int>(SkFontStyle::kNormal_Width), 181 FontStretchNormalMapsTokNormal_Width); 182COMPILE_ASSERT(static_cast<int>(FontStretchUltraExpanded) == static_cast<int>(SkFontStyle::kUltaExpanded_Width), 183 FontStretchUltraExpandedMapsTokUltaExpanded_Width); 184#endif 185 186PassRefPtr<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, CString& name) 187{ 188#if !OS(WIN) && !OS(ANDROID) 189 if (creationParams.creationType() == CreateFontByFciIdAndTtcIndex) { 190 // TODO(dro): crbug.com/381620 Use creationParams.ttcIndex() after 191 // https://code.google.com/p/skia/issues/detail?id=1186 gets fixed. 192 SkTypeface* typeface = nullptr; 193 if (Platform::current()->sandboxSupport()) 194 typeface = SkTypeface::CreateFromStream(streamForFontconfigInterfaceId(creationParams.fontconfigInterfaceId())); 195 else 196 typeface = SkTypeface::CreateFromFile(creationParams.filename().data()); 197 198 if (typeface) 199 return adoptRef(typeface); 200 else 201 return nullptr; 202 } 203#endif 204 205 AtomicString family = creationParams.family(); 206 // If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into 207 // the fallback name (like "monospace") that fontconfig understands. 208 if (!family.length() || family.startsWith("-webkit-")) { 209 name = getFallbackFontFamily(fontDescription).string().utf8(); 210 } else { 211 // convert the name to utf8 212 name = family.utf8(); 213 } 214 215 int style = SkTypeface::kNormal; 216 if (fontDescription.weight() >= FontWeight600) 217 style |= SkTypeface::kBold; 218 if (fontDescription.style()) 219 style |= SkTypeface::kItalic; 220 221#if OS(WIN) 222 if (s_sideloadedFonts) { 223 HashMap<String, RefPtr<SkTypeface> >::iterator sideloadedFont = 224 s_sideloadedFonts->find(name.data()); 225 if (sideloadedFont != s_sideloadedFonts->end()) 226 return sideloadedFont->value; 227 } 228 229 if (m_fontManager) { 230 return adoptRef(useDirectWrite() 231 ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescription)) 232 : m_fontManager->legacyCreateTypeface(name.data(), style) 233 ); 234 } 235#endif 236 237 // FIXME: Use m_fontManager, SkFontStyle and matchFamilyStyle instead of 238 // CreateFromName on all platforms. 239 return adoptRef(SkTypeface::CreateFromName(name.data(), static_cast<SkTypeface::Style>(style))); 240} 241 242#if !OS(WIN) 243FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, float fontSize) 244{ 245 CString name; 246 RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name)); 247 if (!tf) 248 return 0; 249 250 FontPlatformData* result = new FontPlatformData(tf, 251 name.data(), 252 fontSize, 253 (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDescription.isSyntheticBold(), 254 (fontDescription.style() && !tf->isItalic()) || fontDescription.isSyntheticItalic(), 255 fontDescription.orientation(), 256 fontDescription.useSubpixelPositioning()); 257 return result; 258} 259#endif // !OS(WIN) 260 261} // namespace blink 262