Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "core/css/CSSSegmentedFontFace.h"
     28 
     29 #include "core/css/CSSFontFace.h"
     30 #include "core/css/CSSFontSelector.h"
     31 #include "platform/RuntimeEnabledFeatures.h"
     32 #include "platform/fonts/FontCache.h"
     33 #include "platform/fonts/FontDescription.h"
     34 #include "platform/fonts/SegmentedFontData.h"
     35 #include "platform/fonts/SimpleFontData.h"
     36 
     37 namespace WebCore {
     38 
     39 CSSSegmentedFontFace::CSSSegmentedFontFace(CSSFontSelector* fontSelector, FontTraits traits)
     40     : m_fontSelector(fontSelector)
     41     , m_traits(traits)
     42     , m_firstNonCssConnectedFace(m_fontFaces.end())
     43 {
     44 }
     45 
     46 CSSSegmentedFontFace::~CSSSegmentedFontFace()
     47 {
     48     pruneTable();
     49 #if !ENABLE(OILPAN)
     50     for (FontFaceList::iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it)
     51         (*it)->cssFontFace()->clearSegmentedFontFace();
     52 #endif
     53 }
     54 
     55 void CSSSegmentedFontFace::pruneTable()
     56 {
     57     // Make sure the glyph page tree prunes out all uses of this custom font.
     58     if (m_fontDataTable.isEmpty())
     59         return;
     60 
     61     m_fontDataTable.clear();
     62 }
     63 
     64 bool CSSSegmentedFontFace::isValid() const
     65 {
     66     // Valid if at least one font face is valid.
     67     for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) {
     68         if ((*it)->cssFontFace()->isValid())
     69             return true;
     70     }
     71     return false;
     72 }
     73 
     74 void CSSSegmentedFontFace::fontLoaded(CSSFontFace*)
     75 {
     76     pruneTable();
     77 }
     78 
     79 void CSSSegmentedFontFace::fontLoadWaitLimitExceeded(CSSFontFace*)
     80 {
     81     pruneTable();
     82 }
     83 
     84 void CSSSegmentedFontFace::addFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFontFace, bool cssConnected)
     85 {
     86     RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
     87     pruneTable();
     88     fontFace->cssFontFace()->setSegmentedFontFace(this);
     89     if (cssConnected) {
     90         m_fontFaces.insertBefore(m_firstNonCssConnectedFace, fontFace);
     91     } else {
     92         // This is the only place in Blink that is using addReturnIterator.
     93         FontFaceList::iterator iterator = m_fontFaces.addReturnIterator(fontFace);
     94         if (m_firstNonCssConnectedFace == m_fontFaces.end())
     95             m_firstNonCssConnectedFace = iterator;
     96     }
     97 }
     98 
     99 void CSSSegmentedFontFace::removeFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFontFace)
    100 {
    101     RefPtrWillBeRawPtr<FontFace> fontFace = prpFontFace;
    102     FontFaceList::iterator it = m_fontFaces.find(fontFace);
    103     if (it == m_fontFaces.end())
    104         return;
    105 
    106     if (it == m_firstNonCssConnectedFace)
    107         ++m_firstNonCssConnectedFace;
    108     m_fontFaces.remove(it);
    109 
    110     pruneTable();
    111     fontFace->cssFontFace()->clearSegmentedFontFace();
    112 }
    113 
    114 static void appendFontData(SegmentedFontData* newFontData, PassRefPtr<SimpleFontData> prpFaceFontData, const CSSFontFace::UnicodeRangeSet& ranges)
    115 {
    116     RefPtr<SimpleFontData> faceFontData = prpFaceFontData;
    117     unsigned numRanges = ranges.size();
    118     if (!numRanges) {
    119         newFontData->appendRange(FontDataRange(0, 0x7FFFFFFF, faceFontData));
    120         return;
    121     }
    122 
    123     for (unsigned j = 0; j < numRanges; ++j)
    124         newFontData->appendRange(FontDataRange(ranges.rangeAt(j).from(), ranges.rangeAt(j).to(), faceFontData));
    125 }
    126 
    127 PassRefPtr<FontData> CSSSegmentedFontFace::getFontData(const FontDescription& fontDescription)
    128 {
    129     if (!isValid())
    130         return nullptr;
    131 
    132     FontTraits desiredTraits = fontDescription.traits();
    133     AtomicString emptyFontFamily = "";
    134     FontCacheKey key = fontDescription.cacheKey(emptyFontFamily, desiredTraits);
    135 
    136     RefPtr<SegmentedFontData>& fontData = m_fontDataTable.add(key.hash(), nullptr).storedValue->value;
    137     if (fontData && fontData->numRanges())
    138         return fontData; // No release, we have a reference to an object in the cache which should retain the ref count it has.
    139 
    140     if (!fontData)
    141         fontData = SegmentedFontData::create();
    142 
    143     FontDescription requestedFontDescription(fontDescription);
    144     requestedFontDescription.setTraits(m_traits);
    145     requestedFontDescription.setSyntheticBold(m_traits.weight() < FontWeight600 && desiredTraits.weight() >= FontWeight600);
    146     requestedFontDescription.setSyntheticItalic(m_traits.style() == FontStyleNormal && desiredTraits.style() == FontStyleItalic);
    147 
    148     for (FontFaceList::reverse_iterator it = m_fontFaces.rbegin(); it != m_fontFaces.rend(); ++it) {
    149         if (!(*it)->cssFontFace()->isValid())
    150             continue;
    151         if (RefPtr<SimpleFontData> faceFontData = (*it)->cssFontFace()->getFontData(requestedFontDescription)) {
    152             ASSERT(!faceFontData->isSegmented());
    153 #if ENABLE(SVG_FONTS)
    154             // For SVG Fonts that specify that they only support the "normal" variant, we will assume they are incapable
    155             // of small-caps synthesis and just ignore the font face.
    156             if (faceFontData->isSVGFont() && desiredTraits.variant() == FontVariantSmallCaps && m_traits.variant() == FontVariantNormal)
    157                 continue;
    158 #endif
    159             appendFontData(fontData.get(), faceFontData.release(), (*it)->cssFontFace()->ranges());
    160         }
    161     }
    162     if (fontData->numRanges())
    163         return fontData; // No release, we have a reference to an object in the cache which should retain the ref count it has.
    164 
    165     return nullptr;
    166 }
    167 
    168 bool CSSSegmentedFontFace::isLoading() const
    169 {
    170     for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) {
    171         if ((*it)->loadStatus() == FontFace::Loading)
    172             return true;
    173     }
    174     return false;
    175 }
    176 
    177 bool CSSSegmentedFontFace::isLoaded() const
    178 {
    179     for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) {
    180         if ((*it)->loadStatus() != FontFace::Loaded)
    181             return false;
    182     }
    183     return true;
    184 }
    185 
    186 void CSSSegmentedFontFace::willUseFontData(const FontDescription& fontDescription, UChar32 character)
    187 {
    188     for (FontFaceList::reverse_iterator it = m_fontFaces.rbegin(); it != m_fontFaces.rend(); ++it) {
    189         if ((*it)->loadStatus() != FontFace::Unloaded)
    190             break;
    191         if ((*it)->cssFontFace()->maybeScheduleFontLoad(fontDescription, character))
    192             break;
    193     }
    194 }
    195 
    196 bool CSSSegmentedFontFace::checkFont(const String& text) const
    197 {
    198     for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) {
    199         if ((*it)->loadStatus() != FontFace::Loaded && (*it)->cssFontFace()->ranges().intersectsWith(text))
    200             return false;
    201     }
    202     return true;
    203 }
    204 
    205 void CSSSegmentedFontFace::match(const String& text, WillBeHeapVector<RefPtrWillBeMember<FontFace> >& faces) const
    206 {
    207     for (FontFaceList::const_iterator it = m_fontFaces.begin(); it != m_fontFaces.end(); ++it) {
    208         if ((*it)->cssFontFace()->ranges().intersectsWith(text))
    209             faces.append(*it);
    210     }
    211 }
    212 
    213 void CSSSegmentedFontFace::trace(Visitor* visitor)
    214 {
    215     visitor->trace(m_fontSelector);
    216     visitor->trace(m_fontFaces);
    217 }
    218 
    219 }
    220