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.
32
#include "FontCache.h"
34
#include "FontDescription.h"
35
#include "FontFamily.h"
36
#include "FontPlatformData.h"
38
#include "NotImplemented.h"
39
#include "SimpleFontData.h"
41
#include "SkTypeface.h"
43
#include <unicode/locid.h>
44
#include <wtf/Assertions.h>
45
#include <wtf/text/AtomicString.h>
46
#include <wtf/text/CString.h>
50
void FontCache::platformInit()
54
PassRefPtr<SimpleFontData> FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
56
icu::Locale locale = icu::Locale::getDefault();
57
FontCache::SimpleFontFamily family;
58
FontCache::getFontFamilyForCharacters(characters, length, locale.getLanguage(), &family);
59
if (family.name.isEmpty())
62
AtomicString atomicFamily(family.name);
63
// Changes weight and/or italic of given FontDescription depends on
64
// the result of fontconfig so that keeping the correct font mapping
65
// of the given characters. See http://crbug.com/32109 for details.
66
bool shouldSetFakeBold = false;
67
bool shouldSetFakeItalic = false;
68
FontDescription description(font.fontDescription());
69
if (family.isBold && description.weight() < FontWeightBold)
70
description.setWeight(FontWeightBold);
71
if (!family.isBold && description.weight() >= FontWeightBold) {
72
shouldSetFakeBold = true;
73
description.setWeight(FontWeightNormal);
75
if (family.isItalic && description.italic() == FontItalicOff)
76
description.setItalic(FontItalicOn);
77
if (!family.isItalic && description.italic() == FontItalicOn) {
78
shouldSetFakeItalic = true;
79
description.setItalic(FontItalicOff);
82
FontPlatformData* substitutePlatformData = getCachedFontPlatformData(description, atomicFamily, DoNotRetain);
83
if (!substitutePlatformData)
85
FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
86
platformData.setFakeBold(shouldSetFakeBold);
87
platformData.setFakeItalic(shouldSetFakeItalic);
88
return getCachedFontData(&platformData, DoNotRetain);
91
PassRefPtr<SimpleFontData> FontCache::getSimilarFontPlatformData(const Font& font)
96
PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
98
DEFINE_STATIC_LOCAL(const AtomicString, sansStr, ("Sans", AtomicString::ConstructFromLiteral));
99
DEFINE_STATIC_LOCAL(const AtomicString, serifStr, ("Serif", AtomicString::ConstructFromLiteral));
100
DEFINE_STATIC_LOCAL(const AtomicString, monospaceStr, ("Monospace", AtomicString::ConstructFromLiteral));
102
FontPlatformData* fontPlatformData = 0;
103
switch (description.genericFamily()) {
104
case FontDescription::SerifFamily:
105
fontPlatformData = getCachedFontPlatformData(description, serifStr);
107
case FontDescription::MonospaceFamily:
108
fontPlatformData = getCachedFontPlatformData(description, monospaceStr);
110
case FontDescription::SansSerifFamily:
112
fontPlatformData = getCachedFontPlatformData(description, sansStr);
116
if (!fontPlatformData) {
117
// we should at least have Arial; this is the SkFontHost_fontconfig last resort fallback
118
DEFINE_STATIC_LOCAL(const AtomicString, arialStr, ("Arial", AtomicString::ConstructFromLiteral));
119
fontPlatformData = getCachedFontPlatformData(description, arialStr);
122
ASSERT(fontPlatformData);
123
return getCachedFontData(fontPlatformData, shouldRetain);
126
void FontCache::getTraitsInFamily(const AtomicString& familyName,
127
Vector<unsigned>& traitsMasks)
132
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription,
133
const AtomicString& family)
135
const char* name = 0;
138
// If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into
139
// the fallback name (like "monospace") that fontconfig understands.
140
if (!family.length() || family.startsWith("-webkit-")) {
141
static const struct {
142
FontDescription::GenericFamilyType mType;
144
} fontDescriptions[] = {
145
{ FontDescription::SerifFamily, "serif" },
146
{ FontDescription::SansSerifFamily, "sans-serif" },
147
{ FontDescription::MonospaceFamily, "monospace" },
148
{ FontDescription::CursiveFamily, "cursive" },
149
{ FontDescription::FantasyFamily, "fantasy" }
152
FontDescription::GenericFamilyType type = fontDescription.genericFamily();
153
for (unsigned i = 0; i < SK_ARRAY_COUNT(fontDescriptions); i++) {
154
if (type == fontDescriptions[i].mType) {
155
name = fontDescriptions[i].mName;
162
// convert the name to utf8
163
s = family.string().utf8();
167
int style = SkTypeface::kNormal;
168
if (fontDescription.weight() >= FontWeightBold)
169
style |= SkTypeface::kBold;
170
if (fontDescription.italic())
171
style |= SkTypeface::kItalic;
173
SkTypeface* tf = SkTypeface::CreateFromName(name, static_cast<SkTypeface::Style>(style));
177
FontPlatformData* result =
178
new FontPlatformData(tf,
180
fontDescription.computedSize(),
181
(style & SkTypeface::kBold) && !tf->isBold(),
182
(style & SkTypeface::kItalic) && !tf->isItalic(),
183
fontDescription.orientation(),
184
fontDescription.textOrientation());
189
} // namespace WebCore