2 * Copyright (c) 2006, 2007, 2008, 2009 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
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
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.
33 #if !OS(WIN) && !OS(ANDROID)
34 #include "SkFontConfigInterface.h"
36 #include "SkFontMgr.h"
38 #include "SkTypeface.h"
39 #include "platform/fonts/AlternateFontFamily.h"
40 #include "platform/fonts/FontCache.h"
41 #include "platform/fonts/FontDescription.h"
42 #include "platform/fonts/FontFaceCreationParams.h"
43 #include "platform/fonts/SimpleFontData.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/linux/WebSandboxSupport.h"
46 #include "wtf/Assertions.h"
47 #include "wtf/text/AtomicString.h"
48 #include "wtf/text/CString.h"
49 #include <unicode/locid.h>
51 #if !OS(WIN) && !OS(ANDROID)
52 static SkStream* streamForFontconfigInterfaceId(int fontconfigInterfaceId)
54 SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal());
55 SkFontConfigInterface::FontIdentity fontIdentity;
56 fontIdentity.fID = fontconfigInterfaceId;
57 return fci->openStream(fontIdentity);
63 void FontCache::platformInit()
67 PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle(
68 const FontDescription& fontDescription, UChar32 character)
70 FontDescription substituteDescription(fontDescription);
71 substituteDescription.setStyle(FontStyleNormal);
72 substituteDescription.setWeight(FontWeightNormal);
74 FontFaceCreationParams creationParams(substituteDescription.family().family());
75 FontPlatformData* substitutePlatformData = getFontPlatformData(substituteDescription, creationParams);
76 if (substitutePlatformData && substitutePlatformData->fontContainsCharacter(character)) {
77 FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
78 platformData.setSyntheticBold(fontDescription.weight() >= FontWeight600);
79 platformData.setSyntheticItalic(fontDescription.style() == FontStyleItalic);
80 return fontDataFromFontPlatformData(&platformData, DoNotRetain);
86 #if !OS(WIN) && !OS(ANDROID)
87 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
89 // First try the specified font with standard style & weight.
90 if (fontDescription.style() == FontStyleItalic
91 || fontDescription.weight() >= FontWeight600) {
92 RefPtr<SimpleFontData> fontData = fallbackOnStandardFontStyle(
98 FontCache::PlatformFallbackFont fallbackFont;
99 FontCache::getFontForCharacter(c, fontDescription.locale().ascii().data(), &fallbackFont);
100 if (fallbackFont.name.isEmpty())
103 FontFaceCreationParams creationParams;
104 creationParams = FontFaceCreationParams(fallbackFont.filename, fallbackFont.fontconfigInterfaceId, fallbackFont.ttcIndex);
106 // Changes weight and/or italic of given FontDescription depends on
107 // the result of fontconfig so that keeping the correct font mapping
108 // of the given character. See http://crbug.com/32109 for details.
109 bool shouldSetSyntheticBold = false;
110 bool shouldSetSyntheticItalic = false;
111 FontDescription description(fontDescription);
112 if (fallbackFont.isBold && description.weight() < FontWeightBold)
113 description.setWeight(FontWeightBold);
114 if (!fallbackFont.isBold && description.weight() >= FontWeightBold) {
115 shouldSetSyntheticBold = true;
116 description.setWeight(FontWeightNormal);
118 if (fallbackFont.isItalic && description.style() == FontStyleNormal)
119 description.setStyle(FontStyleItalic);
120 if (!fallbackFont.isItalic && description.style() == FontStyleItalic) {
121 shouldSetSyntheticItalic = true;
122 description.setStyle(FontStyleNormal);
125 FontPlatformData* substitutePlatformData = getFontPlatformData(description, creationParams);
126 if (!substitutePlatformData)
128 FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
129 platformData.setSyntheticBold(shouldSetSyntheticBold);
130 platformData.setSyntheticItalic(shouldSetSyntheticItalic);
131 return fontDataFromFontPlatformData(&platformData, DoNotRetain);
134 #endif // !OS(WIN) && !OS(ANDROID)
136 PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
138 const FontFaceCreationParams fallbackCreationParams(getFallbackFontFamily(description));
139 const FontPlatformData* fontPlatformData = getFontPlatformData(description, fallbackCreationParams);
141 // We should at least have Sans or Arial which is the last resort fallback of SkFontHost ports.
142 if (!fontPlatformData) {
143 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, sansCreationParams, (AtomicString("Sans", AtomicString::ConstructFromLiteral)));
144 fontPlatformData = getFontPlatformData(description, sansCreationParams);
146 if (!fontPlatformData) {
147 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, arialCreationParams, (AtomicString("Arial", AtomicString::ConstructFromLiteral)));
148 fontPlatformData = getFontPlatformData(description, arialCreationParams);
151 // Try some more Windows-specific fallbacks.
152 if (!fontPlatformData) {
153 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, msuigothicCreationParams, (AtomicString("MS UI Gothic", AtomicString::ConstructFromLiteral)));
154 fontPlatformData = getFontPlatformData(description, msuigothicCreationParams);
156 if (!fontPlatformData) {
157 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationParams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral)));
158 fontPlatformData = getFontPlatformData(description, mssansserifCreationParams);
162 ASSERT(fontPlatformData);
163 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain);
167 static inline SkFontStyle fontStyle(const FontDescription& fontDescription)
169 int width = static_cast<int>(fontDescription.stretch());
170 int weight = (fontDescription.weight() - FontWeight100 + 1) * 100;
171 SkFontStyle::Slant slant = fontDescription.style() == FontStyleItalic
172 ? SkFontStyle::kItalic_Slant
173 : SkFontStyle::kUpright_Slant;
174 return SkFontStyle(weight, width, slant);
177 COMPILE_ASSERT(static_cast<int>(FontStretchUltraCondensed) == static_cast<int>(SkFontStyle::kUltraCondensed_Width),
178 FontStretchUltraCondensedMapsTokUltraCondensed_Width);
179 COMPILE_ASSERT(static_cast<int>(FontStretchNormal) == static_cast<int>(SkFontStyle::kNormal_Width),
180 FontStretchNormalMapsTokNormal_Width);
181 COMPILE_ASSERT(static_cast<int>(FontStretchUltraExpanded) == static_cast<int>(SkFontStyle::kUltaExpanded_Width),
182 FontStretchUltraExpandedMapsTokUltaExpanded_Width);
185 PassRefPtr<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, CString& name)
187 #if !OS(WIN) && !OS(ANDROID)
188 if (creationParams.creationType() == CreateFontByFciIdAndTtcIndex) {
189 // TODO(dro): crbug.com/381620 Use creationParams.ttcIndex() after
190 // https://code.google.com/p/skia/issues/detail?id=1186 gets fixed.
191 SkTypeface* typeface = nullptr;
192 if (Platform::current()->sandboxSupport())
193 typeface = SkTypeface::CreateFromStream(streamForFontconfigInterfaceId(creationParams.fontconfigInterfaceId()));
195 typeface = SkTypeface::CreateFromFile(creationParams.filename().data());
198 return adoptRef(typeface);
204 AtomicString family = creationParams.family();
205 // If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into
206 // the fallback name (like "monospace") that fontconfig understands.
207 if (!family.length() || family.startsWith("-webkit-")) {
208 name = getFallbackFontFamily(fontDescription).string().utf8();
210 // convert the name to utf8
211 name = family.utf8();
214 int style = SkTypeface::kNormal;
215 if (fontDescription.weight() >= FontWeight600)
216 style |= SkTypeface::kBold;
217 if (fontDescription.style())
218 style |= SkTypeface::kItalic;
221 if (s_sideloadedFonts) {
222 HashMap<String, RefPtr<SkTypeface> >::iterator sideloadedFont =
223 s_sideloadedFonts->find(name.data());
224 if (sideloadedFont != s_sideloadedFonts->end())
225 return sideloadedFont->value;
229 return adoptRef(useDirectWrite()
230 ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescription))
231 : m_fontManager->legacyCreateTypeface(name.data(), style)
236 // FIXME: Use m_fontManager, SkFontStyle and matchFamilyStyle instead of
237 // CreateFromName on all platforms.
238 return adoptRef(SkTypeface::CreateFromName(name.data(), static_cast<SkTypeface::Style>(style)));
242 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, float fontSize)
245 RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name));
249 FontPlatformData* result = new FontPlatformData(tf,
252 (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDescription.isSyntheticBold(),
253 (fontDescription.style() && !tf->isItalic()) || fontDescription.isSyntheticItalic(),
254 fontDescription.orientation(),
255 fontDescription.useSubpixelPositioning());