FontCacheAndroid.cpp [plain text]
#include "config.h"
#include "FontCache.h"
#include "Font.h"
#include "FontDescription.h"
#include "FontPlatformData.h"
#include "NotImplemented.h"
#include "PlatformSupport.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") || 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") || equalIgnoringCase(family, "helvetica")
|| equalIgnoringCase(family, "tahoma")
|| equalIgnoringCase(family, "verdana")
|| equalIgnoringCase(family, "courier") || equalIgnoringCase(family, "courier new")
|| equalIgnoringCase(family, "monaco");
}
void FontCache::platformInit()
{
}
const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
{
icu::Locale locale = icu::Locale::getDefault();
PlatformSupport::FontFamily family;
PlatformSupport::getFontFamilyForCharacters(characters, length, locale.getLanguage(), &family);
if (family.name.isEmpty())
return 0;
AtomicString atomicFamily(family.name);
return getCachedFontData(getCachedFontPlatformData(font.fontDescription(), atomicFamily, DoNotRetain), DoNotRetain);
}
SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font)
{
return 0;
}
SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
{
DEFINE_STATIC_LOCAL(const AtomicString, serif, ("Serif"));
DEFINE_STATIC_LOCAL(const AtomicString, monospace, ("Monospace"));
DEFINE_STATIC_LOCAL(const AtomicString, sans, ("Sans"));
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;
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);
if (!SkTypeface::Equal(typeface, 0) || isFallbackFamily(family.string())) {
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;
}
}