Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / skia / FontCacheSkia.cpp
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/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>
50
51 #if !OS(WIN) && !OS(ANDROID)
52 static SkStream* streamForFontconfigInterfaceId(int fontconfigInterfaceId)
53 {
54     SkAutoTUnref<SkFontConfigInterface> fci(SkFontConfigInterface::RefGlobal());
55     SkFontConfigInterface::FontIdentity fontIdentity;
56     fontIdentity.fID = fontconfigInterfaceId;
57     return fci->openStream(fontIdentity);
58 }
59 #endif
60
61 namespace blink {
62
63 void FontCache::platformInit()
64 {
65 }
66
67 PassRefPtr<SimpleFontData> FontCache::fallbackOnStandardFontStyle(
68     const FontDescription& fontDescription, UChar32 character)
69 {
70     FontDescription substituteDescription(fontDescription);
71     substituteDescription.setStyle(FontStyleNormal);
72     substituteDescription.setWeight(FontWeightNormal);
73
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);
81     }
82
83     return nullptr;
84 }
85
86 #if !OS(WIN) && !OS(ANDROID)
87 PassRefPtr<SimpleFontData> FontCache::fallbackFontForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
88 {
89     // First try the specified font with standard style & weight.
90     if (fontDescription.style() == FontStyleItalic
91         || fontDescription.weight() >= FontWeight600) {
92         RefPtr<SimpleFontData> fontData = fallbackOnStandardFontStyle(
93             fontDescription, c);
94         if (fontData)
95             return fontData;
96     }
97
98     FontCache::PlatformFallbackFont fallbackFont;
99     FontCache::getFontForCharacter(c, fontDescription.locale().ascii().data(), &fallbackFont);
100     if (fallbackFont.name.isEmpty())
101         return nullptr;
102
103     FontFaceCreationParams creationParams;
104     creationParams = FontFaceCreationParams(fallbackFont.filename, fallbackFont.fontconfigInterfaceId, fallbackFont.ttcIndex);
105
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);
117     }
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);
123     }
124
125     FontPlatformData* substitutePlatformData = getFontPlatformData(description, creationParams);
126     if (!substitutePlatformData)
127         return nullptr;
128     FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
129     platformData.setSyntheticBold(shouldSetSyntheticBold);
130     platformData.setSyntheticItalic(shouldSetSyntheticItalic);
131     return fontDataFromFontPlatformData(&platformData, DoNotRetain);
132 }
133
134 #endif // !OS(WIN) && !OS(ANDROID)
135
136 PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
137 {
138     const FontFaceCreationParams fallbackCreationParams(getFallbackFontFamily(description));
139     const FontPlatformData* fontPlatformData = getFontPlatformData(description, fallbackCreationParams);
140
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);
145     }
146     if (!fontPlatformData) {
147         DEFINE_STATIC_LOCAL(const FontFaceCreationParams, arialCreationParams, (AtomicString("Arial", AtomicString::ConstructFromLiteral)));
148         fontPlatformData = getFontPlatformData(description, arialCreationParams);
149     }
150 #if OS(WIN)
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);
155     }
156     if (!fontPlatformData) {
157         DEFINE_STATIC_LOCAL(const FontFaceCreationParams, mssansserifCreationParams, (AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral)));
158         fontPlatformData = getFontPlatformData(description, mssansserifCreationParams);
159     }
160 #endif
161
162     ASSERT(fontPlatformData);
163     return fontDataFromFontPlatformData(fontPlatformData, shouldRetain);
164 }
165
166 #if OS(WIN)
167 static inline SkFontStyle fontStyle(const FontDescription& fontDescription)
168 {
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);
175 }
176
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);
183 #endif
184
185 PassRefPtr<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, CString& name)
186 {
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()));
194         else
195             typeface = SkTypeface::CreateFromFile(creationParams.filename().data());
196
197         if (typeface)
198             return adoptRef(typeface);
199         else
200             return nullptr;
201     }
202 #endif
203
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();
209     } else {
210         // convert the name to utf8
211         name = family.utf8();
212     }
213
214     int style = SkTypeface::kNormal;
215     if (fontDescription.weight() >= FontWeight600)
216         style |= SkTypeface::kBold;
217     if (fontDescription.style())
218         style |= SkTypeface::kItalic;
219
220 #if OS(WIN)
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;
226     }
227
228     if (m_fontManager) {
229         return adoptRef(useDirectWrite()
230             ? m_fontManager->matchFamilyStyle(name.data(), fontStyle(fontDescription))
231             : m_fontManager->legacyCreateTypeface(name.data(), style)
232             );
233     }
234 #endif
235
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)));
239 }
240
241 #if !OS(WIN)
242 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const FontFaceCreationParams& creationParams, float fontSize)
243 {
244     CString name;
245     RefPtr<SkTypeface> tf(createTypeface(fontDescription, creationParams, name));
246     if (!tf)
247         return 0;
248
249     FontPlatformData* result = new FontPlatformData(tf,
250         name.data(),
251         fontSize,
252         (fontDescription.weight() >= FontWeight600 && !tf->isBold()) || fontDescription.isSyntheticBold(),
253         (fontDescription.style() && !tf->isItalic()) || fontDescription.isSyntheticItalic(),
254         fontDescription.orientation(),
255         fontDescription.useSubpixelPositioning());
256     return result;
257 }
258 #endif // !OS(WIN)
259
260 } // namespace blink