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/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>
52 #if !OS(WIN) && !OS(ANDROID)
53 static SkStream* streamForFontconfigInterfaceId(int fontconfigInterfaceId)
55 SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal());
56 SkFontConfigInterface::FontIdentity fontIdentity;
57 fontIdentity.fID = fontconfigInterfaceId;
58 return fci->openStream(fontIdentity);
64 void FontCache::platformInit()
68 PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle(
69 const FontDescription& fontDescription, UChar32 character)
71 FontDescription substituteDescription(fontDescription);
72 substituteDescription.setStyle(FontStyleNormal);
73 substituteDescription.setWeight(FontWeightNormal);
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);
87 #if !OS(WIN) && !OS(ANDROID)
88 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
90 // First try the specified font with standard style & weight.
91 if (fontDescription.style() == FontStyleItalic
92 || fontDescription.weight() >= FontWeight600) {
93 RefPtr<SimpleFontData> fontData = fallbackOnStandardFontStyle(
99 FontCache::PlatformFallbackFont fallbackFont;
100 FontCache::getFontForCharacter(c, fontDescription.locale().ascii().data(), &fallbackFont);
101 if (fallbackFont.name.isEmpty())
104 FontFaceCreationParams creationParams;
105 creationParams = FontFaceCreationParams(fallbackFont.filename, fallbackFont.fontconfigInterfaceId, fallbackFont.ttcIndex);
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);
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);
126 FontPlatformData* substitutePlatformData = getFontPlatformData(description, creationParams);
127 if (!substitutePlatformData)
129 FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
130 platformData.setSyntheticBold(shouldSetSyntheticBold);
131 platformData.setSyntheticItalic(shouldSetSyntheticItalic);
132 return fontDataFromFontPlatformData(&platformData, DoNotRetain);
135 #endif // !OS(WIN) && !OS(ANDROID)
137 PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
139 const FontFaceCreationParams fallbackCreationParams(getFallbackFontFamily(description));
140 const FontPlatformData* fontPlatformData = getFontPlatformData(description, fallbackCreationParams);
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);
147 if (!fontPlatformData) {
148 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, arialCreationParams, (AtomicString("Arial", AtomicString::ConstructFromLiteral)));
149 fontPlatformData = getFontPlatformData(description, arialCreationParams);
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);
157 if (!fontPlatformData) {
158 DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationParams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral)));
159 fontPlatformData = getFontPlatformData(description, mssansserifCreationParams);
163 ASSERT(fontPlatformData);
164 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain);
168 static inline SkFontStyle fontStyle(const FontDescription& fontDescription)
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);
178 COMPILE_ASSERT(static_cast<int>(FontStretchUltraCondensed) == static_cast<int>(SkFontStyle::kUltraCondensed_Width),
179 FontStretchUltraCondensedMapsTokUltraCondensed_Width);
180 COMPILE_ASSERT(static_cast<int>(FontStretchNormal) == static_cast<int>(SkFontStyle::kNormal_Width),
181 FontStretchNormalMapsTokNormal_Width);
182 COMPILE_ASSERT(static_cast<int>(FontStretchUltraExpanded) == static_cast<int>(SkFontStyle::kUltaExpanded_Width),
183 FontStretchUltraExpandedMapsTokUltaExpanded_Width);
186 PassRefPtr<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, CString& name)
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()));
196 typeface = SkTypeface::CreateFromFile(creationParams.filename().data());
199 return adoptRef(typeface);
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();
211 // convert the name to utf8
212 name = family.utf8();
215 int style = SkTypeface::kNormal;
216 if (fontDescription.weight() >= FontWeight600)
217 style |= SkTypeface::kBold;
218 if (fontDescription.style())
219 style |= SkTypeface::kItalic;
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;
230 return adoptRef(useDirectWrite()
231 ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescription))
232 : m_fontManager->legacyCreateTypeface(name.data(), style)
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)));
243 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, float fontSize)
246 RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name));
250 FontPlatformData* result = new FontPlatformData(tf,
253 (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDescription.isSyntheticBold(),
254 (fontDescription.style() && !tf->isItalic()) || fontDescription.isSyntheticItalic(),
255 fontDescription.orientation(),
256 fontDescription.useSubpixelPositioning());