1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright (c) 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FontCache.h"
#include "Font.h"
#include "FontDescription.h"
#include "FontPlatformData.h"
#include "NotImplemented.h"
#include "SimpleFontData.h"
#include "SkPaint.h"
#include "SkTypeface_android.h"
#include "SkUtils.h"
#include <unicode/locid.h>
#include <wtf/Assertions.h>
#include <wtf/text/AtomicString.h>
#include <wtf/text/CString.h>
namespace WebCore {
static const char* getFallbackFontName(const FontDescription& fontDescription)
{
switch (fontDescription.genericFamily()) {
case FontDescription::StandardFamily:
case FontDescription::SerifFamily:
return "serif";
case FontDescription::SansSerifFamily:
return "sans-serif";
case FontDescription::MonospaceFamily:
return "monospace";
case FontDescription::CursiveFamily:
return "cursive";
case FontDescription::FantasyFamily:
return "fantasy";
case FontDescription::NoFamily:
default:
return "";
}
}
static bool isFallbackFamily(String family)
{
return family.startsWith("-webkit-")
|| equalIgnoringCase(family, "serif")
|| equalIgnoringCase(family, "sans-serif")
|| equalIgnoringCase(family, "sans")
|| equalIgnoringCase(family, "monospace")
|| equalIgnoringCase(family, "cursive")
|| equalIgnoringCase(family, "fantasy")
|| equalIgnoringCase(family, "times") // Skia aliases for serif
|| equalIgnoringCase(family, "times new roman")
|| equalIgnoringCase(family, "palatino")
|| equalIgnoringCase(family, "georgia")
|| equalIgnoringCase(family, "baskerville")
|| equalIgnoringCase(family, "goudy")
|| equalIgnoringCase(family, "ITC Stone Serif")
|| equalIgnoringCase(family, "arial") // Skia aliases for sans-serif
|| equalIgnoringCase(family, "helvetica")
|| equalIgnoringCase(family, "tahoma")
|| equalIgnoringCase(family, "verdana")
|| equalIgnoringCase(family, "courier") // Skia aliases for monospace
|| equalIgnoringCase(family, "courier new")
|| equalIgnoringCase(family, "monaco");
}
void FontCache::platformInit()
{
}
PassRefPtr<SimpleFontData> FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
{
if (!length)
return 0;
SkUnichar skiaChar;
if (U16_IS_LEAD(characters[0])) {
ASSERT(length >= 2);
skiaChar = U16_GET_SUPPLEMENTARY(characters[0], characters[1]);
} else
skiaChar = characters[0];
SkString skiaFamilyName;
if (!SkGetFallbackFamilyNameForChar(skiaChar, &skiaFamilyName) || skiaFamilyName.isEmpty())
return 0;
return getCachedFontData(getCachedFontPlatformData(font.fontDescription(), AtomicString(skiaFamilyName.c_str()), DoNotRetain), DoNotRetain);
}
PassRefPtr<SimpleFontData> FontCache::getSimilarFontPlatformData(const Font& font)
{
return 0;
}
PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
{
DEFINE_STATIC_LOCAL(const AtomicString, serif, ("Serif", AtomicString::ConstructFromLiteral));
DEFINE_STATIC_LOCAL(const AtomicString, monospace, ("Monospace", AtomicString::ConstructFromLiteral));
DEFINE_STATIC_LOCAL(const AtomicString, sans, ("Sans", AtomicString::ConstructFromLiteral));
FontPlatformData* fontPlatformData = 0;
switch (description.genericFamily()) {
case FontDescription::SerifFamily:
fontPlatformData = getCachedFontPlatformData(description, serif);
break;
case FontDescription::MonospaceFamily:
fontPlatformData = getCachedFontPlatformData(description, monospace);
break;
case FontDescription::SansSerifFamily:
default:
fontPlatformData = getCachedFontPlatformData(description, sans);
break;
}
ASSERT(fontPlatformData);
return getCachedFontData(fontPlatformData, shouldRetain);
}
void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
{
notImplemented();
}
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
{
const char* name = 0;
CString nameString; // Keeps name valid within scope of this function in case that name is from a family.
// If a fallback font is being created (e.g. "-webkit-monospace"), convert
// it in to the fallback name (e.g. "monospace").
if (!family.length() || family.startsWith("-webkit-"))
name = getFallbackFontName(fontDescription);
else {
nameString = family.string().utf8();
name = nameString.data();
}
int style = SkTypeface::kNormal;
if (fontDescription.weight() >= FontWeightBold)
style |= SkTypeface::kBold;
if (fontDescription.italic())
style |= SkTypeface::kItalic;
SkTypeface* typeface = 0;
FontPlatformData* result = 0;
FallbackScripts fallbackScript = SkGetFallbackScriptFromID(name);
if (SkTypeface_ValidScript(fallbackScript)) {
typeface = SkCreateTypefaceForScript(fallbackScript);
if (typeface)
result = new FontPlatformData(typeface, name, fontDescription.computedSize(),
(style & SkTypeface::kBold) && !typeface->isBold(),
(style & SkTypeface::kItalic) && !typeface->isItalic(),
fontDescription.orientation(),
fontDescription.textOrientation());
} else {
typeface = SkTypeface::CreateFromName(name, SkTypeface::kNormal);
// CreateFromName always returns a typeface, falling back to a default font
// if the one requested could not be found. Calling Equal() with a null
// pointer will compare the returned font against the default, with the
// caveat that the default is always of normal style. When that happens,
// ignore the default font and allow WebCore to provide the next font on the
// CSS fallback list. The only exception to this occurs when the family name
// is a commonly used generic family, which is the case when called by
// getSimilarFontPlatformData() or getLastResortFallbackFont(). In that case
// the default font is an acceptable result.
if (!SkTypeface::Equal(typeface, 0) || isFallbackFamily(family.string())) {
// We had to use normal styling to see if this was a default font. If
// we need bold or italic, replace with the corrected typeface.
if (style != SkTypeface::kNormal) {
typeface->unref();
typeface = SkTypeface::CreateFromName(name, static_cast<SkTypeface::Style>(style));
}
result = new FontPlatformData(typeface, name, fontDescription.computedSize(),
(style & SkTypeface::kBold) && !typeface->isBold(),
(style & SkTypeface::kItalic) && !typeface->isItalic(),
fontDescription.orientation(),
fontDescription.textOrientation());
}
}
SkSafeUnref(typeface);
return result;
}
} // namespace WebCore
|