예제 #1
0
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
{
    char*       storage = 0;
    const char* name = 0;

    if (family.length() == 0) {
        static const struct {
            FontDescription::GenericFamilyType  mType;
            const char*                         mName;
        } gNames[] = {
            { FontDescription::SerifFamily,     "serif" },
            { FontDescription::SansSerifFamily, "sans-serif" },
            { FontDescription::MonospaceFamily, "monospace" },
            { FontDescription::CursiveFamily,   "cursive" },
            { FontDescription::FantasyFamily,   "fantasy" }
        };

        FontDescription::GenericFamilyType type = fontDescription.genericFamily();
        for (unsigned i = 0; i < SK_ARRAY_COUNT(gNames); i++)
        {
            if (type == gNames[i].mType)
            {
                name = gNames[i].mName;
                break;
            }
        }
        // if we fall out of the loop, its ok for name to still be 0
    }
    else {    // convert the name to utf8
        storage = AtomicStringToUTF8String(family);
        name = storage;
    }

    int style = SkTypeface::kNormal;
    if (fontDescription.weight() >= FontWeightBold)
        style |= SkTypeface::kBold;
    if (fontDescription.italic())
        style |= SkTypeface::kItalic;

    SkTypeface* tf = SkTypeface::CreateFromName(name, (SkTypeface::Style)style);

    if (!tf)
        return 0;

    FontPlatformData* result = new FontPlatformData(tf,
                                                    name,
                                                    fontDescription.computedSize(),
                                                    (style & SkTypeface::kBold) && !tf->isBold(),
                                                    (style & SkTypeface::kItalic) && !tf->isItalic());
    tf->unref();

    sk_free(storage);
    return result;
}
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
{
    char* storage = 0;
    const char* name = 0;
    FontPlatformData* result = 0;

    if (family.length()) {
        storage = AtomicStringToUTF8String(family);
        name = storage;
    } else
        name = getFallbackFontName(fontDescription);

    int style = SkTypeface::kNormal;
#ifndef FONT_SOFTWARE_RENDER
    if (fontDescription.weight() >= FontWeightBold)
        style |= SkTypeface::kBold;
    if (fontDescription.italic())
        style |= SkTypeface::kItalic;
#endif

    // CreateFromName always returns a typeface, falling back to a default font
    // if the one requested is not found. Calling Equal() with a null pointer
    // serves to compare the returned font against the default, with the caveat
    // that the default is always of normal style. If we detect the default, we
    // ignore it and allow WebCore to give us the next font on the CSS fallback
    // list. The only exception is if the family name is a commonly used generic
    // family, as when called by getSimilarFontPlatformData() and
    // getLastResortFallbackFont(). In this case, the default font is an
    // acceptable result.

    SkTypeface* tf = SkTypeface::CreateFromName(name, SkTypeface::kNormal);

    if (!SkTypeface::Equal(tf, 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) {
            tf->unref();
            tf = SkTypeface::CreateFromName(name, (SkTypeface::Style)style);
        }

        result = new FontPlatformData(tf, fontDescription.computedSize(),
                            (style & SkTypeface::kBold),
                            (style & SkTypeface::kItalic) && !tf->isItalic(),
                            fontDescription.orientation(),
                            fontDescription.textOrientation());
    }

    tf->unref();
    sk_free(storage);
    return result;
}