⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cssfontselector.cpp

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#if ENABLE(SVG_FONTS)            source->setSVGFontFaceElement(item->svgFontFaceElement());#endif            fontFace->addSource(source);        }    }    ASSERT(fontFace);    if (fontFace && !fontFace->isValid())        return;    if (rangeList) {        unsigned numRanges = rangeList->length();        for (unsigned i = 0; i < numRanges; i++) {            CSSUnicodeRangeValue* range = static_cast<CSSUnicodeRangeValue*>(rangeList->itemWithoutBoundsCheck(i));            fontFace->addRange(range->from(), range->to());        }    }    // Hash under every single family name.    int familyLength = familyList->length();    for (int i = 0; i < familyLength; i++) {        CSSPrimitiveValue* item = static_cast<CSSPrimitiveValue*>(familyList->itemWithoutBoundsCheck(i));        String familyName;        if (item->primitiveType() == CSSPrimitiveValue::CSS_STRING)            familyName = static_cast<FontFamilyValue*>(item)->familyName();        else if (item->primitiveType() == CSSPrimitiveValue::CSS_IDENT) {            // We need to use the raw text for all the generic family types, since @font-face is a way of actually            // defining what font to use for those types.            String familyName;            switch (item->getIdent()) {                case CSSValueSerif:                    familyName = "-webkit-serif";                    break;                case CSSValueSansSerif:                    familyName = "-webkit-sans-serif";                    break;                case CSSValueCursive:                    familyName = "-webkit-cursive";                    break;                case CSSValueFantasy:                    familyName = "-webkit-fantasy";                    break;                case CSSValueMonospace:                    familyName = "-webkit-monospace";                    break;                default:                    break;            }        }        if (familyName.isEmpty())            continue;#if ENABLE(SVG_FONTS)        // SVG allows several <font> elements with the same font-family, differing only        // in ie. font-variant. Be sure to pick up the right one - in getFontData below.        if (foundSVGFont && (traitsMask & FontVariantSmallCapsMask))            familyName += "-webkit-svg-small-caps";#endif        Vector<RefPtr<CSSFontFace> >* familyFontFaces = m_fontFaces.get(familyName);        if (!familyFontFaces) {            familyFontFaces = new Vector<RefPtr<CSSFontFace> >;            m_fontFaces.set(familyName, familyFontFaces);            ASSERT(!m_locallyInstalledFontFaces.contains(familyName));            Vector<RefPtr<CSSFontFace> >* familyLocallyInstalledFaces;            Vector<unsigned> locallyInstalledFontsTraitsMasks;            fontCache()->getTraitsInFamily(familyName, locallyInstalledFontsTraitsMasks);            unsigned numLocallyInstalledFaces = locallyInstalledFontsTraitsMasks.size();            if (numLocallyInstalledFaces) {                familyLocallyInstalledFaces = new Vector<RefPtr<CSSFontFace> >;                m_locallyInstalledFontFaces.set(familyName, familyLocallyInstalledFaces);                for (unsigned i = 0; i < numLocallyInstalledFaces; ++i) {                    RefPtr<CSSFontFace> locallyInstalledFontFace = CSSFontFace::create(static_cast<FontTraitsMask>(locallyInstalledFontsTraitsMasks[i]));                    locallyInstalledFontFace->addSource(new CSSFontFaceSource(familyName));                    ASSERT(locallyInstalledFontFace->isValid());                    familyLocallyInstalledFaces->append(locallyInstalledFontFace);                }            }        }        familyFontFaces->append(fontFace);    }}void CSSFontSelector::fontLoaded(){    if (!m_document || m_document->inPageCache() || !m_document->renderer())        return;    m_document->recalcStyle(Document::Force);    m_document->renderer()->setNeedsLayoutAndPrefWidthsRecalc();}void CSSFontSelector::fontCacheInvalidated(){    if (!m_document || m_document->inPageCache() || !m_document->renderer())        return;    m_document->recalcStyle(Document::Force);    m_document->renderer()->setNeedsLayoutAndPrefWidthsRecalc();}static FontData* fontDataForGenericFamily(Document* document, const FontDescription& fontDescription, const AtomicString& familyName){    if (!document || !document->frame())        return 0;    const Settings* settings = document->frame()->settings();    if (!settings)        return 0;        AtomicString genericFamily;    if (familyName == "-webkit-serif")        genericFamily = settings->serifFontFamily();    else if (familyName == "-webkit-sans-serif")        genericFamily = settings->sansSerifFontFamily();    else if (familyName == "-webkit-cursive")        genericFamily = settings->cursiveFontFamily();    else if (familyName == "-webkit-fantasy")        genericFamily = settings->fantasyFontFamily();    else if (familyName == "-webkit-monospace")        genericFamily = settings->fixedFontFamily();    else if (familyName == "-webkit-standard")        genericFamily = settings->standardFontFamily();    if (!genericFamily.isEmpty())        return fontCache()->getCachedFontData(fontCache()->getCachedFontPlatformData(fontDescription, genericFamily));    return 0;}static FontTraitsMask desiredTraitsMaskForComparison;static inline bool compareFontFaces(CSSFontFace* first, CSSFontFace* second){    FontTraitsMask firstTraitsMask = first->traitsMask();    FontTraitsMask secondTraitsMask = second->traitsMask();    bool firstHasDesiredVariant = firstTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;    bool secondHasDesiredVariant = secondTraitsMask & desiredTraitsMaskForComparison & FontVariantMask;    if (firstHasDesiredVariant != secondHasDesiredVariant)        return firstHasDesiredVariant;    bool firstHasDesiredStyle = firstTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;    bool secondHasDesiredStyle = secondTraitsMask & desiredTraitsMaskForComparison & FontStyleMask;    if (firstHasDesiredStyle != secondHasDesiredStyle)        return firstHasDesiredStyle;    if (secondTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)        return false;    if (firstTraitsMask & desiredTraitsMaskForComparison & FontWeightMask)        return true;    // http://www.w3.org/TR/2002/WD-css3-webfonts-20020802/#q46 says: "If there are fewer then 9 weights in the family, the default algorithm    // for filling the "holes" is as follows. If '500' is unassigned, it will be assigned the same font as '400'. If any of the values '600',    // '700', '800', or '900' remains unassigned, they are assigned to the same face as the next darker assigned keyword, if any, or the next    // lighter one otherwise. If any of '300', '200', or '100' remains unassigned, it is assigned to the next lighter assigned keyword, if any,    // or the next darker otherwise."    // For '400', we made up our own rule (which then '500' follows).    static const FontTraitsMask weightFallbackRules[9][8] = {        { FontWeight200Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },        { FontWeight100Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },        { FontWeight200Mask, FontWeight100Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },        { FontWeight500Mask, FontWeight300Mask, FontWeight600Mask, FontWeight200Mask, FontWeight700Mask, FontWeight100Mask, FontWeight800Mask, FontWeight900Mask },        { FontWeight400Mask, FontWeight300Mask, FontWeight600Mask, FontWeight200Mask, FontWeight700Mask, FontWeight100Mask, FontWeight800Mask, FontWeight900Mask },        { FontWeight700Mask, FontWeight800Mask, FontWeight900Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },        { FontWeight800Mask, FontWeight900Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },        { FontWeight900Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },        { FontWeight800Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask }    };    const FontTraitsMask* weightFallbackRule = weightFallbackRules[0];    unsigned w = FontWeight100Bit;    while (!(desiredTraitsMaskForComparison & (1 << w))) {        w++;        weightFallbackRule += 8;    }    for (unsigned i = 0; i < 8; ++i) {        if (secondTraitsMask & weightFallbackRule[i])            return false;        if (firstTraitsMask & weightFallbackRule[i])            return true;    }    return false;}FontData* CSSFontSelector::getFontData(const FontDescription& fontDescription, const AtomicString& familyName){    if (m_fontFaces.isEmpty()) {        if (familyName.startsWith("-webkit-"))            return fontDataForGenericFamily(m_document, fontDescription, familyName);        return 0;    }    String family = familyName.string();#if ENABLE(SVG_FONTS)    if (fontDescription.smallCaps())        family += "-webkit-svg-small-caps";#endif    Vector<RefPtr<CSSFontFace> >* familyFontFaces = m_fontFaces.get(family);    // If no face was found, then return 0 and let the OS come up with its best match for the name.    if (!familyFontFaces || familyFontFaces->isEmpty()) {        // If we were handed a generic family, but there was no match, go ahead and return the correct font based off our        // settings.        return fontDataForGenericFamily(m_document, fontDescription, familyName);    }    HashMap<unsigned, RefPtr<CSSSegmentedFontFace> >* segmentedFontFaceCache = m_fonts.get(family);    if (!segmentedFontFaceCache) {        segmentedFontFaceCache = new HashMap<unsigned, RefPtr<CSSSegmentedFontFace> >;        m_fonts.set(family, segmentedFontFaceCache);    }    FontTraitsMask traitsMask = fontDescription.traitsMask();    RefPtr<CSSSegmentedFontFace> face = segmentedFontFaceCache->get(traitsMask);    if (!face) {        face = CSSSegmentedFontFace::create(this);        segmentedFontFaceCache->set(traitsMask, face);        // Collect all matching faces and sort them in order of preference.        Vector<CSSFontFace*, 32> candidateFontFaces;        for (int i = familyFontFaces->size() - 1; i >= 0; --i) {            CSSFontFace* candidate = familyFontFaces->at(i).get();            unsigned candidateTraitsMask = candidate->traitsMask();            if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))                continue;            if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))                continue;            candidateFontFaces.append(candidate);        }        if (Vector<RefPtr<CSSFontFace> >* familyLocallyInstalledFontFaces = m_locallyInstalledFontFaces.get(family)) {            unsigned numLocallyInstalledFontFaces = familyLocallyInstalledFontFaces->size();            for (unsigned i = 0; i < numLocallyInstalledFontFaces; ++i) {                CSSFontFace* candidate = familyLocallyInstalledFontFaces->at(i).get();                unsigned candidateTraitsMask = candidate->traitsMask();                if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))                    continue;                if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))                    continue;                candidateFontFaces.append(candidate);            }        }        desiredTraitsMaskForComparison = traitsMask;        std::stable_sort(candidateFontFaces.begin(), candidateFontFaces.end(), compareFontFaces);        unsigned numCandidates = candidateFontFaces.size();        for (unsigned i = 0; i < numCandidates; ++i)            face->appendFontFace(candidateFontFaces[i]);    }    // We have a face.  Ask it for a font data.  If it cannot produce one, it will fail, and the OS will take over.    return face->getFontData(fontDescription);}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -