Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2013 Google Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 
     28 #include "config.h"
     29 #include "CSSSegmentedFontFaceCache.h"
     30 
     31 #include "FontFamilyNames.h"
     32 #include "core/css/CSSFontFace.h"
     33 #include "core/css/CSSFontFaceSource.h"
     34 #include "core/css/CSSFontSelector.h"
     35 #include "core/css/CSSSegmentedFontFace.h"
     36 #include "core/css/CSSValueList.h"
     37 #include "core/css/StyleRule.h"
     38 #include "core/fetch/FontResource.h"
     39 #include "core/fetch/ResourceFetcher.h"
     40 #include "platform/fonts/FontDescription.h"
     41 #include "wtf/text/AtomicString.h"
     42 
     43 namespace WebCore {
     44 
     45 CSSSegmentedFontFaceCache::CSSSegmentedFontFaceCache()
     46     : m_version(0)
     47 {
     48 }
     49 
     50 void CSSSegmentedFontFaceCache::add(CSSFontSelector* cssFontSelector, const StyleRuleFontFace* fontFaceRule, PassRefPtr<CSSFontFace> prpCssFontFace)
     51 {
     52     RefPtr<CSSFontFace> cssFontFace = prpCssFontFace;
     53 
     54     if (!m_styleRuleToFontFace.add(fontFaceRule, cssFontFace).isNewEntry)
     55         return;
     56 
     57     FontFace* fontFace = cssFontFace->fontFace();
     58 
     59     OwnPtr<TraitsMap>& familyFontFaces = m_fontFaces.add(fontFace->family(), nullptr).iterator->value;
     60     if (!familyFontFaces)
     61         familyFontFaces = adoptPtr(new TraitsMap);
     62 
     63     RefPtr<CSSSegmentedFontFace>& segmentedFontFace = familyFontFaces->add(fontFace->traitsMask(), 0).iterator->value;
     64     if (!segmentedFontFace)
     65         segmentedFontFace = CSSSegmentedFontFace::create(cssFontSelector, static_cast<FontTraitsMask>(fontFace->traitsMask()));
     66 
     67     segmentedFontFace->appendFontFace(cssFontFace);
     68 
     69     ++m_version;
     70 }
     71 
     72 void CSSSegmentedFontFaceCache::remove(const StyleRuleFontFace* fontFaceRule)
     73 {
     74     StyleRuleToFontFace::iterator styleRuleToFontFaceIter = m_styleRuleToFontFace.find(fontFaceRule);
     75     if (styleRuleToFontFaceIter == m_styleRuleToFontFace.end())
     76         return;
     77     RefPtr<CSSFontFace> cssFontFace = styleRuleToFontFaceIter->value;
     78 
     79     FamilyToTraitsMap::iterator fontFacesIter = m_fontFaces.find(cssFontFace->fontFace()->family());
     80     if (fontFacesIter == m_fontFaces.end())
     81         return;
     82     TraitsMap* familyFontFaces = fontFacesIter->value.get();
     83 
     84     TraitsMap::iterator familyFontFacesIter = familyFontFaces->find(cssFontFace->fontFace()->traitsMask());
     85     if (familyFontFacesIter == familyFontFaces->end())
     86         return;
     87     RefPtr<CSSSegmentedFontFace> segmentedFontFace = familyFontFacesIter->value;
     88 
     89     segmentedFontFace->removeFontFace(cssFontFace);
     90     if (segmentedFontFace->isEmpty()) {
     91         familyFontFaces->remove(familyFontFacesIter);
     92         if (familyFontFaces->isEmpty())
     93             m_fontFaces.remove(fontFacesIter);
     94     }
     95     m_styleRuleToFontFace.remove(styleRuleToFontFaceIter);
     96     m_fonts.clear();
     97     ++m_version;
     98 }
     99 
    100 static inline bool compareFontFaces(CSSSegmentedFontFace* first, CSSSegmentedFontFace* second, FontTraitsMask desiredTraitsMask)
    101 {
    102     FontTraitsMask firstTraitsMask = first->traitsMask();
    103     FontTraitsMask secondTraitsMask = second->traitsMask();
    104 
    105     bool firstHasDesiredVariant = firstTraitsMask & desiredTraitsMask & FontVariantMask;
    106     bool secondHasDesiredVariant = secondTraitsMask & desiredTraitsMask & FontVariantMask;
    107 
    108     if (firstHasDesiredVariant != secondHasDesiredVariant)
    109         return firstHasDesiredVariant;
    110 
    111     // We need to check font-variant css property for CSS2.1 compatibility.
    112     if (desiredTraitsMask & FontVariantSmallCapsMask) {
    113         // Prefer a font that has indicated that it can only support small-caps to a font that claims to support
    114         // all variants. The specialized font is more likely to be true small-caps and not require synthesis.
    115         bool firstRequiresSmallCaps = (firstTraitsMask & FontVariantSmallCapsMask) && !(firstTraitsMask & FontVariantNormalMask);
    116         bool secondRequiresSmallCaps = (secondTraitsMask & FontVariantSmallCapsMask) && !(secondTraitsMask & FontVariantNormalMask);
    117         if (firstRequiresSmallCaps != secondRequiresSmallCaps)
    118             return firstRequiresSmallCaps;
    119     }
    120 
    121     bool firstHasDesiredStyle = firstTraitsMask & desiredTraitsMask & FontStyleMask;
    122     bool secondHasDesiredStyle = secondTraitsMask & desiredTraitsMask & FontStyleMask;
    123 
    124     if (firstHasDesiredStyle != secondHasDesiredStyle)
    125         return firstHasDesiredStyle;
    126 
    127     if (desiredTraitsMask & FontStyleItalicMask) {
    128         // Prefer a font that has indicated that it can only support italics to a font that claims to support
    129         // all styles. The specialized font is more likely to be the one the author wants used.
    130         bool firstRequiresItalics = (firstTraitsMask & FontStyleItalicMask) && !(firstTraitsMask & FontStyleNormalMask);
    131         bool secondRequiresItalics = (secondTraitsMask & FontStyleItalicMask) && !(secondTraitsMask & FontStyleNormalMask);
    132         if (firstRequiresItalics != secondRequiresItalics)
    133             return firstRequiresItalics;
    134     }
    135 
    136     if (secondTraitsMask & desiredTraitsMask & FontWeightMask)
    137         return false;
    138     if (firstTraitsMask & desiredTraitsMask & FontWeightMask)
    139         return true;
    140 
    141     // http://www.w3.org/TR/2011/WD-css3-fonts-20111004/#font-matching-algorithm says :
    142     //   - If the desired weight is less than 400, weights below the desired weight are checked in descending order followed by weights above the desired weight in ascending order until a match is found.
    143     //   - If the desired weight is greater than 500, weights above the desired weight are checked in ascending order followed by weights below the desired weight in descending order until a match is found.
    144     //   - If the desired weight is 400, 500 is checked first and then the rule for desired weights less than 400 is used.
    145     //   - If the desired weight is 500, 400 is checked first and then the rule for desired weights less than 400 is used.
    146 
    147     static const unsigned fallbackRuleSets = 9;
    148     static const unsigned rulesPerSet = 8;
    149     static const FontTraitsMask weightFallbackRuleSets[fallbackRuleSets][rulesPerSet] = {
    150         { FontWeight200Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
    151         { FontWeight100Mask, FontWeight300Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
    152         { FontWeight200Mask, FontWeight100Mask, FontWeight400Mask, FontWeight500Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
    153         { FontWeight500Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
    154         { FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask, FontWeight600Mask, FontWeight700Mask, FontWeight800Mask, FontWeight900Mask },
    155         { FontWeight700Mask, FontWeight800Mask, FontWeight900Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
    156         { FontWeight800Mask, FontWeight900Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
    157         { FontWeight900Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask },
    158         { FontWeight800Mask, FontWeight700Mask, FontWeight600Mask, FontWeight500Mask, FontWeight400Mask, FontWeight300Mask, FontWeight200Mask, FontWeight100Mask }
    159     };
    160 
    161     unsigned ruleSetIndex = 0;
    162     unsigned w = FontWeight100Bit;
    163     while (!(desiredTraitsMask & (1 << w))) {
    164         w++;
    165         ruleSetIndex++;
    166     }
    167 
    168     ASSERT(ruleSetIndex < fallbackRuleSets);
    169     const FontTraitsMask* weightFallbackRule = weightFallbackRuleSets[ruleSetIndex];
    170     for (unsigned i = 0; i < rulesPerSet; ++i) {
    171         if (secondTraitsMask & weightFallbackRule[i])
    172             return false;
    173         if (firstTraitsMask & weightFallbackRule[i])
    174             return true;
    175     }
    176 
    177     return false;
    178 }
    179 
    180 CSSSegmentedFontFace* CSSSegmentedFontFaceCache::get(const FontDescription& fontDescription, const AtomicString& family)
    181 {
    182     TraitsMap* familyFontFaces = m_fontFaces.get(family);
    183     if (!familyFontFaces || familyFontFaces->isEmpty())
    184         return 0;
    185 
    186     OwnPtr<TraitsMap>& segmentedFontFaceCache = m_fonts.add(family, nullptr).iterator->value;
    187     if (!segmentedFontFaceCache)
    188         segmentedFontFaceCache = adoptPtr(new TraitsMap);
    189 
    190     FontTraitsMask traitsMask = fontDescription.traitsMask();
    191 
    192     RefPtr<CSSSegmentedFontFace>& face = segmentedFontFaceCache->add(traitsMask, 0).iterator->value;
    193     if (!face) {
    194         for (TraitsMap::const_iterator i = familyFontFaces->begin(); i != familyFontFaces->end(); ++i) {
    195             CSSSegmentedFontFace* candidate = i->value.get();
    196             unsigned candidateTraitsMask = candidate->traitsMask();
    197             if ((traitsMask & FontStyleNormalMask) && !(candidateTraitsMask & FontStyleNormalMask))
    198                 continue;
    199             if ((traitsMask & FontVariantNormalMask) && !(candidateTraitsMask & FontVariantNormalMask))
    200                 continue;
    201             if (!face || compareFontFaces(candidate, face.get(), traitsMask))
    202                 face = candidate;
    203         }
    204     }
    205     return face.get();
    206 }
    207 
    208 }
    209