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 #include "SkFontMgr.h"
34 #include "SkTypeface.h"
35 #include "platform/NotImplemented.h"
36 #include "platform/fonts/AlternateFontFamily.h"
37 #include "platform/fonts/FontCache.h"
38 #include "platform/fonts/FontDescription.h"
39 #include "platform/fonts/SimpleFontData.h"
40 #include "wtf/Assertions.h"
41 #include "wtf/text/AtomicString.h"
42 #include "wtf/text/CString.h"
43 #include <unicode/locid.h>
47 void FontCache::platformInit()
51 #if !OS(WIN) && !OS(ANDROID)
52 PassRefPtr<SimpleFontData> FontCache::platformFallbackForCharacter(const FontDescription& fontDescription, UChar32 c, const SimpleFontData*)
54 icu::Locale locale = icu::Locale::getDefault();
55 FontCache::SimpleFontFamily family;
56 FontCache::getFontFamilyForCharacter(c, locale.getLanguage(), &family);
57 if (family.name.isEmpty())
60 AtomicString atomicFamily(family.name);
61 // Changes weight and/or italic of given FontDescription depends on
62 // the result of fontconfig so that keeping the correct font mapping
63 // of the given character. See http://crbug.com/32109 for details.
64 bool shouldSetSyntheticBold = false;
65 bool shouldSetSyntheticItalic = false;
66 FontDescription description(fontDescription);
67 if (family.isBold && description.weight() < FontWeightBold)
68 description.setWeight(FontWeightBold);
69 if (!family.isBold && description.weight() >= FontWeightBold) {
70 shouldSetSyntheticBold = true;
71 description.setWeight(FontWeightNormal);
73 if (family.isItalic && description.style() == FontStyleNormal)
74 description.setStyle(FontStyleItalic);
75 if (!family.isItalic && description.style() == FontStyleItalic) {
76 shouldSetSyntheticItalic = true;
77 description.setStyle(FontStyleNormal);
80 FontPlatformData* substitutePlatformData = getFontPlatformData(description, atomicFamily);
81 if (!substitutePlatformData)
83 FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
84 platformData.setSyntheticBold(shouldSetSyntheticBold);
85 platformData.setSyntheticItalic(shouldSetSyntheticItalic);
86 return fontDataFromFontPlatformData(&platformData, DoNotRetain);
89 #endif // !OS(WIN) && !OS(ANDROID)
91 PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
93 const AtomicString fallbackFontFamily = getFallbackFontFamily(description);
94 const FontPlatformData* fontPlatformData = getFontPlatformData(description, fallbackFontFamily);
96 // We should at least have Sans or Arial which is the last resort fallback of SkFontHost ports.
97 if (!fontPlatformData) {
98 DEFINE_STATIC_LOCAL(const AtomicString, sansStr, ("Sans", AtomicString::ConstructFromLiteral));
99 fontPlatformData = getFontPlatformData(description, sansStr);
101 if (!fontPlatformData) {
102 DEFINE_STATIC_LOCAL(const AtomicString, arialStr, ("Arial", AtomicString::ConstructFromLiteral));
103 fontPlatformData = getFontPlatformData(description, arialStr);
106 ASSERT(fontPlatformData);
107 return fontDataFromFontPlatformData(fontPlatformData, shouldRetain);
110 PassRefPtr<SkTypeface> FontCache::createTypeface(const FontDescription& fontDescription, const AtomicString& family, CString& name)
112 // If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into
113 // the fallback name (like "monospace") that fontconfig understands.
114 if (!family.length() || family.startsWith("-webkit-")) {
115 name = getFallbackFontFamily(fontDescription).string().utf8();
117 // convert the name to utf8
118 name = family.utf8();
121 int style = SkTypeface::kNormal;
122 if (fontDescription.weight() >= FontWeightBold)
123 style |= SkTypeface::kBold;
124 if (fontDescription.style())
125 style |= SkTypeface::kItalic;
128 if (s_sideloadedFonts) {
129 HashMap<String, SkTypeface*>::iterator sideloadedFont = s_sideloadedFonts->find(name.data());
130 if (sideloadedFont != s_sideloadedFonts->end()) {
131 return adoptRef(sideloadedFont->value);
134 // FIXME: Use SkFontStyle and matchFamilyStyle instead of legacyCreateTypeface.
136 return adoptRef(m_fontManager->legacyCreateTypeface(name.data(), style));
139 return adoptRef(SkTypeface::CreateFromName(name.data(), static_cast<SkTypeface::Style>(style)));
143 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family, float fontSize)
146 RefPtr<SkTypeface> tf(createTypeface(fontDescription, family, name));
150 FontPlatformData* result = new FontPlatformData(tf,
153 (fontDescription.weight() >= FontWeightBold && !tf->isBold()) || fontDescription.isSyntheticBold(),
154 (fontDescription.style() && !tf->isItalic()) || fontDescription.isSyntheticItalic(),
155 fontDescription.orientation(),
156 fontDescription.useSubpixelPositioning());
161 } // namespace WebCore