This source file includes following definitions.
- add
- addFontFace
- remove
- removeFontFace
- clear
- compareFontFaces
- get
#include "config.h"
#include "FontFaceCache.h"
#include "FontFamilyNames.h"
#include "core/css/CSSFontSelector.h"
#include "core/css/CSSSegmentedFontFace.h"
#include "core/css/CSSValueList.h"
#include "core/css/FontFace.h"
#include "core/css/StyleRule.h"
#include "core/fetch/FontResource.h"
#include "core/fetch/ResourceFetcher.h"
#include "platform/fonts/FontDescription.h"
#include "wtf/text/AtomicString.h"
namespace WebCore {
FontFaceCache::FontFaceCache()
: m_version(0)
{
}
void FontFaceCache::add(CSSFontSelector* cssFontSelector, const StyleRuleFontFace* fontFaceRule, PassRefPtr<FontFace> prpFontFace)
{
RefPtr<FontFace> fontFace = prpFontFace;
if (!m_styleRuleToFontFace.add(fontFaceRule, fontFace).isNewEntry)
return;
addFontFace(cssFontSelector, fontFace, true);
}
void FontFaceCache::addFontFace(CSSFontSelector* cssFontSelector, PassRefPtr<FontFace> prpFontFace, bool cssConnected)
{
RefPtr<FontFace> fontFace = prpFontFace;
OwnPtr<TraitsMap>& familyFontFaces = m_fontFaces.add(fontFace->family(), nullptr).storedValue->value;
if (!familyFontFaces)
familyFontFaces = adoptPtr(new TraitsMap);
RefPtr<CSSSegmentedFontFace>& segmentedFontFace = familyFontFaces->add(fontFace->traits().mask(), nullptr).storedValue->value;
if (!segmentedFontFace)
segmentedFontFace = CSSSegmentedFontFace::create(cssFontSelector, fontFace->traits());
segmentedFontFace->addFontFace(fontFace, cssConnected);
if (cssConnected)
m_cssConnectedFontFaces.add(fontFace);
++m_version;
}
void FontFaceCache::remove(const StyleRuleFontFace* fontFaceRule)
{
StyleRuleToFontFace::iterator it = m_styleRuleToFontFace.find(fontFaceRule);
if (it != m_styleRuleToFontFace.end()) {
removeFontFace(it->value.get(), true);
m_styleRuleToFontFace.remove(it);
}
}
void FontFaceCache::removeFontFace(FontFace* fontFace, bool cssConnected)
{
FamilyToTraitsMap::iterator fontFacesIter = m_fontFaces.find(fontFace->family());
if (fontFacesIter == m_fontFaces.end())
return;
TraitsMap* familyFontFaces = fontFacesIter->value.get();
TraitsMap::iterator familyFontFacesIter = familyFontFaces->find(fontFace->traits().mask());
if (familyFontFacesIter == familyFontFaces->end())
return;
RefPtr<CSSSegmentedFontFace> segmentedFontFace = familyFontFacesIter->value;
segmentedFontFace->removeFontFace(fontFace);
if (segmentedFontFace->isEmpty()) {
familyFontFaces->remove(familyFontFacesIter);
if (familyFontFaces->isEmpty())
m_fontFaces.remove(fontFacesIter);
}
m_fonts.clear();
if (cssConnected)
m_cssConnectedFontFaces.remove(fontFace);
++m_version;
}
void FontFaceCache::clear()
{
for (StyleRuleToFontFace::iterator it = m_styleRuleToFontFace.begin(); it != m_styleRuleToFontFace.end(); ++it)
removeFontFace(it->value.get(), true);
m_styleRuleToFontFace.clear();
}
static inline bool compareFontFaces(CSSSegmentedFontFace* first, CSSSegmentedFontFace* second, FontTraits desiredTraits)
{
const FontTraits& firstTraits = first->traits();
const FontTraits& secondTraits = second->traits();
bool firstHasDesiredVariant = firstTraits.variant() == desiredTraits.variant();
bool secondHasDesiredVariant = secondTraits.variant() == desiredTraits.variant();
if (firstHasDesiredVariant != secondHasDesiredVariant)
return firstHasDesiredVariant;
if (desiredTraits.variant() == FontVariantSmallCaps) {
bool firstRequiresSmallCaps = firstTraits.variant() == FontVariantSmallCaps;
bool secondRequiresSmallCaps = secondTraits.variant() == FontVariantSmallCaps;
if (firstRequiresSmallCaps != secondRequiresSmallCaps)
return firstRequiresSmallCaps;
}
bool firstHasDesiredStyle = firstTraits.style() == desiredTraits.style();
bool secondHasDesiredStyle = secondTraits.style() == desiredTraits.style();
if (firstHasDesiredStyle != secondHasDesiredStyle)
return firstHasDesiredStyle;
if (desiredTraits.style() == FontStyleItalic) {
bool firstRequiresItalics = firstTraits.style() == FontStyleItalic;
bool secondRequiresItalics = secondTraits.style() == FontStyleItalic;
if (firstRequiresItalics != secondRequiresItalics)
return firstRequiresItalics;
}
if (secondTraits.weight() == desiredTraits.weight())
return false;
if (firstTraits.weight() == desiredTraits.weight())
return true;
static const unsigned fallbackRuleSets = 9;
static const unsigned rulesPerSet = 8;
static const FontWeight weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
{ FontWeight200, FontWeight300, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
{ FontWeight100, FontWeight300, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
{ FontWeight200, FontWeight100, FontWeight400, FontWeight500, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
{ FontWeight500, FontWeight300, FontWeight200, FontWeight100, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
{ FontWeight400, FontWeight300, FontWeight200, FontWeight100, FontWeight600, FontWeight700, FontWeight800, FontWeight900 },
{ FontWeight700, FontWeight800, FontWeight900, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
{ FontWeight800, FontWeight900, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
{ FontWeight900, FontWeight700, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 },
{ FontWeight800, FontWeight700, FontWeight600, FontWeight500, FontWeight400, FontWeight300, FontWeight200, FontWeight100 }
};
unsigned ruleSetIndex = static_cast<unsigned>(desiredTraits.weight());
ASSERT(ruleSetIndex < fallbackRuleSets);
const FontWeight* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
for (unsigned i = 0; i < rulesPerSet; ++i) {
if (secondTraits.weight() == weightFallbackRule[i])
return false;
if (firstTraits.weight() == weightFallbackRule[i])
return true;
}
return false;
}
CSSSegmentedFontFace* FontFaceCache::get(const FontDescription& fontDescription, const AtomicString& family)
{
TraitsMap* familyFontFaces = m_fontFaces.get(family);
if (!familyFontFaces || familyFontFaces->isEmpty())
return 0;
OwnPtr<TraitsMap>& segmentedFontFaceCache = m_fonts.add(family, nullptr).storedValue->value;
if (!segmentedFontFaceCache)
segmentedFontFaceCache = adoptPtr(new TraitsMap);
FontTraits traits = fontDescription.traits();
RefPtr<CSSSegmentedFontFace>& face = segmentedFontFaceCache->add(traits.mask(), nullptr).storedValue->value;
if (!face) {
for (TraitsMap::const_iterator i = familyFontFaces->begin(); i != familyFontFaces->end(); ++i) {
CSSSegmentedFontFace* candidate = i->value.get();
FontTraits candidateTraits = candidate->traits();
if (traits.style() == FontStyleNormal && candidateTraits.style() != FontStyleNormal)
continue;
if (traits.variant() == FontVariantNormal && candidateTraits.variant() != FontVariantNormal)
continue;
if (!face || compareFontFaces(candidate, face.get(), traits))
face = candidate;
}
}
return face.get();
}
}