Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2007, 2008, 2010, 2011 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/CSSFontFaceSource.h"
     28 
     29 #include "core/css/CSSFontFace.h"
     30 #include "core/css/CSSFontSelector.h"
     31 #include "core/loader/cache/FontResource.h"
     32 #include "core/platform/HistogramSupport.h"
     33 #include "core/platform/graphics/FontCache.h"
     34 #include "core/platform/graphics/FontDescription.h"
     35 #include "core/platform/graphics/SimpleFontData.h"
     36 #include "wtf/CurrentTime.h"
     37 
     38 #if ENABLE(SVG_FONTS)
     39 #include "SVGNames.h"
     40 #include "core/svg/SVGFontData.h"
     41 #include "core/svg/SVGFontElement.h"
     42 #include "core/svg/SVGFontFaceElement.h"
     43 #endif
     44 
     45 namespace WebCore {
     46 
     47 CSSFontFaceSource::CSSFontFaceSource(const String& str, FontResource* font)
     48     : m_string(str)
     49     , m_font(font)
     50     , m_face(0)
     51 #if ENABLE(SVG_FONTS)
     52     , m_hasExternalSVGFont(false)
     53 #endif
     54 {
     55     if (m_font)
     56         m_font->addClient(this);
     57 }
     58 
     59 CSSFontFaceSource::~CSSFontFaceSource()
     60 {
     61     if (m_font)
     62         m_font->removeClient(this);
     63     pruneTable();
     64 }
     65 
     66 void CSSFontFaceSource::pruneTable()
     67 {
     68     if (m_fontDataTable.isEmpty())
     69         return;
     70 
     71     m_fontDataTable.clear();
     72 }
     73 
     74 bool CSSFontFaceSource::isLoaded() const
     75 {
     76     if (m_font)
     77         return m_font->isLoaded();
     78     return true;
     79 }
     80 
     81 bool CSSFontFaceSource::isValid() const
     82 {
     83     if (m_font)
     84         return !m_font->errorOccurred();
     85     return true;
     86 }
     87 
     88 void CSSFontFaceSource::didStartFontLoad(FontResource*)
     89 {
     90     // Avoid duplicated reports when multiple CSSFontFaceSource are registered
     91     // at this FontResource.
     92     if (!m_fontDataTable.isEmpty())
     93         m_histograms.loadStarted();
     94 }
     95 
     96 void CSSFontFaceSource::fontLoaded(FontResource*)
     97 {
     98     if (!m_fontDataTable.isEmpty())
     99         m_histograms.recordRemoteFont(m_font.get());
    100 
    101     pruneTable();
    102     if (m_face)
    103         m_face->fontLoaded(this);
    104 }
    105 
    106 PassRefPtr<SimpleFontData> CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
    107 {
    108     // If the font hasn't loaded or an error occurred, then we've got nothing.
    109     if (!isValid())
    110         return 0;
    111 
    112     if (!m_font
    113 #if ENABLE(SVG_FONTS)
    114             && !m_svgFontFaceElement
    115 #endif
    116     ) {
    117         // We're local. Just return a SimpleFontData from the normal cache.
    118         // We don't want to check alternate font family names here, so pass true as the checkingAlternateName parameter.
    119         RefPtr<SimpleFontData> fontData = fontCache()->getFontResourceData(fontDescription, m_string, true);
    120         m_histograms.recordLocalFont(fontData);
    121         return fontData;
    122     }
    123 
    124     // See if we have a mapping in our FontData cache.
    125     unsigned hashKey = (fontDescription.computedPixelSize() + 1) << 5 | fontDescription.widthVariant() << 3
    126                        | (fontDescription.orientation() == Vertical ? 4 : 0) | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
    127 
    128     RefPtr<SimpleFontData>& fontData = m_fontDataTable.add(hashKey, 0).iterator->value;
    129     if (fontData)
    130         return fontData; // No release, because fontData is a reference to a RefPtr that is held in the m_fontDataTable.
    131 
    132     // If we are still loading, then we let the system pick a font.
    133     if (isLoaded()) {
    134         if (m_font) {
    135 #if ENABLE(SVG_FONTS)
    136             if (m_hasExternalSVGFont) {
    137                 // For SVG fonts parse the external SVG document, and extract the <font> element.
    138                 if (!m_font->ensureSVGFontData())
    139                     return 0;
    140 
    141                 if (!m_externalSVGFontElement) {
    142                     String fragmentIdentifier;
    143                     size_t start = m_string.find('#');
    144                     if (start != notFound)
    145                         fragmentIdentifier = m_string.string().substring(start + 1);
    146                     m_externalSVGFontElement = m_font->getSVGFontById(fragmentIdentifier);
    147                 }
    148 
    149                 if (!m_externalSVGFontElement)
    150                     return 0;
    151 
    152                 SVGFontFaceElement* fontFaceElement = 0;
    153 
    154                 // Select first <font-face> child
    155                 for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
    156                     if (fontChild->hasTagName(SVGNames::font_faceTag)) {
    157                         fontFaceElement = toSVGFontFaceElement(fontChild);
    158                         break;
    159                     }
    160                 }
    161 
    162                 if (fontFaceElement) {
    163                     if (!m_svgFontFaceElement) {
    164                         // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
    165                         // Use the imported <font-face> tag as referencing font-face element for these cases.
    166                         m_svgFontFaceElement = fontFaceElement;
    167                     }
    168 
    169                     fontData = SimpleFontData::create(SVGFontData::create(fontFaceElement), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic);
    170                 }
    171             } else
    172 #endif
    173             {
    174                 // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef.
    175                 if (!m_font->ensureCustomFontData())
    176                     return 0;
    177 
    178                 fontData = SimpleFontData::create(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic,
    179                     fontDescription.orientation(), fontDescription.widthVariant()), true, false);
    180             }
    181         } else {
    182 #if ENABLE(SVG_FONTS)
    183             // In-Document SVG Fonts
    184             if (m_svgFontFaceElement)
    185                 fontData = SimpleFontData::create(SVGFontData::create(m_svgFontFaceElement.get()), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic);
    186 #endif
    187         }
    188     } else {
    189         // Kick off the load. Do it soon rather than now, because we may be in the middle of layout,
    190         // and the loader may invoke arbitrary delegate or event handler code.
    191         fontSelector->beginLoadingFontSoon(m_font.get());
    192 
    193         // This temporary font is not retained and should not be returned.
    194         FontCachePurgePreventer fontCachePurgePreventer;
    195         SimpleFontData* temporaryFont = fontCache()->getNonRetainedLastResortFallbackFont(fontDescription);
    196         fontData = SimpleFontData::create(temporaryFont->platformData(), true, true);
    197     }
    198 
    199     return fontData; // No release, because fontData is a reference to a RefPtr that is held in the m_fontDataTable.
    200 }
    201 
    202 #if ENABLE(SVG_FONTS)
    203 SVGFontFaceElement* CSSFontFaceSource::svgFontFaceElement() const
    204 {
    205     return m_svgFontFaceElement.get();
    206 }
    207 
    208 void CSSFontFaceSource::setSVGFontFaceElement(PassRefPtr<SVGFontFaceElement> element)
    209 {
    210     m_svgFontFaceElement = element;
    211 }
    212 
    213 bool CSSFontFaceSource::isSVGFontFaceSource() const
    214 {
    215     return m_svgFontFaceElement || m_hasExternalSVGFont;
    216 }
    217 #endif
    218 
    219 bool CSSFontFaceSource::isDecodeError() const
    220 {
    221     if (m_font)
    222         return m_font->status() == Resource::DecodeError;
    223     return false;
    224 }
    225 
    226 bool CSSFontFaceSource::ensureFontData()
    227 {
    228     if (!m_font)
    229         return false;
    230 #if ENABLE(SVG_FONTS)
    231     if (m_hasExternalSVGFont)
    232         return m_font->ensureSVGFontData();
    233 #endif
    234     return m_font->ensureCustomFontData();
    235 }
    236 
    237 void CSSFontFaceSource::FontLoadHistograms::loadStarted()
    238 {
    239     if (!m_loadStartTime)
    240         m_loadStartTime = currentTimeMS();
    241 }
    242 
    243 void CSSFontFaceSource::FontLoadHistograms::recordLocalFont(bool loadSuccess)
    244 {
    245     if (!m_loadStartTime) {
    246         HistogramSupport::histogramEnumeration("WebFont.LocalFontUsed", loadSuccess ? 1 : 0, 2);
    247         m_loadStartTime = -1; // Do not count this font again.
    248     }
    249 }
    250 
    251 void CSSFontFaceSource::FontLoadHistograms::recordRemoteFont(const FontResource* font)
    252 {
    253     if (m_loadStartTime > 0 && font && !font->isLoading()) {
    254         int duration = static_cast<int>(currentTimeMS() - m_loadStartTime);
    255         HistogramSupport::histogramCustomCounts(histogramName(font), duration, 0, 10000, 50);
    256         m_loadStartTime = -1;
    257     }
    258 }
    259 
    260 const char* CSSFontFaceSource::FontLoadHistograms::histogramName(const FontResource* font)
    261 {
    262     if (font->errorOccurred())
    263         return "WebFont.DownloadTime.LoadError";
    264 
    265     unsigned size = font->encodedSize();
    266     if (size < 10 * 1024)
    267         return "WebFont.DownloadTime.0.Under10KB";
    268     if (size < 50 * 1024)
    269         return "WebFont.DownloadTime.1.10KBTo50KB";
    270     if (size < 100 * 1024)
    271         return "WebFont.DownloadTime.2.50KBTo100KB";
    272     if (size < 1024 * 1024)
    273         return "WebFont.DownloadTime.3.100KBTo1MB";
    274     return "WebFont.DownloadTime.4.Over1MB";
    275 }
    276 
    277 }
    278