Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2007, 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 "CSSFontFaceSource.h"
     28 
     29 #include "CachedFont.h"
     30 #include "CSSFontFace.h"
     31 #include "CSSFontSelector.h"
     32 #include "DocLoader.h"
     33 #include "FontCache.h"
     34 #include "FontDescription.h"
     35 #include "GlyphPageTreeNode.h"
     36 #include "SimpleFontData.h"
     37 
     38 #if ENABLE(SVG_FONTS)
     39 #include "FontCustomPlatformData.h"
     40 #include "HTMLNames.h"
     41 #include "SVGFontData.h"
     42 #include "SVGFontElement.h"
     43 #include "SVGURIReference.h"
     44 #endif
     45 
     46 namespace WebCore {
     47 
     48 CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font)
     49     : m_string(str)
     50     , m_font(font)
     51     , m_face(0)
     52 #if ENABLE(SVG_FONTS)
     53     , m_svgFontFaceElement(0)
     54 #endif
     55 {
     56     if (m_font)
     57         m_font->addClient(this);
     58 }
     59 
     60 CSSFontFaceSource::~CSSFontFaceSource()
     61 {
     62     if (m_font)
     63         m_font->removeClient(this);
     64     pruneTable();
     65 }
     66 
     67 void CSSFontFaceSource::pruneTable()
     68 {
     69     if (m_fontDataTable.isEmpty())
     70         return;
     71     HashMap<unsigned, SimpleFontData*>::iterator end = m_fontDataTable.end();
     72     for (HashMap<unsigned, SimpleFontData*>::iterator it = m_fontDataTable.begin(); it != end; ++it)
     73         GlyphPageTreeNode::pruneTreeCustomFontData(it->second);
     74     deleteAllValues(m_fontDataTable);
     75     m_fontDataTable.clear();
     76 }
     77 
     78 bool CSSFontFaceSource::isLoaded() const
     79 {
     80     if (m_font)
     81         return m_font->isLoaded();
     82     return true;
     83 }
     84 
     85 bool CSSFontFaceSource::isValid() const
     86 {
     87     if (m_font)
     88         return !m_font->errorOccurred();
     89     return true;
     90 }
     91 
     92 void CSSFontFaceSource::fontLoaded(CachedFont*)
     93 {
     94     pruneTable();
     95     if (m_face)
     96         m_face->fontLoaded(this);
     97 }
     98 
     99 SimpleFontData* CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
    100 {
    101     // If the font hasn't loaded or an error occurred, then we've got nothing.
    102     if (!isValid())
    103         return 0;
    104 
    105 #if ENABLE(SVG_FONTS)
    106     if (!m_font && !m_svgFontFaceElement) {
    107 #else
    108     if (!m_font) {
    109 #endif
    110         SimpleFontData* fontData = fontCache()->getCachedFontData(fontDescription, m_string);
    111 
    112         // We're local. Just return a SimpleFontData from the normal cache.
    113         return fontData;
    114     }
    115 
    116     // See if we have a mapping in our FontData cache.
    117     unsigned hashKey = fontDescription.computedPixelSize() << 2 | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
    118     if (SimpleFontData* cachedData = m_fontDataTable.get(hashKey))
    119         return cachedData;
    120 
    121     OwnPtr<SimpleFontData> fontData;
    122 
    123     // If we are still loading, then we let the system pick a font.
    124     if (isLoaded()) {
    125         if (m_font) {
    126 #if ENABLE(SVG_FONTS)
    127             if (m_font->isSVGFont()) {
    128                 // For SVG fonts parse the external SVG document, and extract the <font> element.
    129                 if (!m_font->ensureSVGFontData())
    130                     return 0;
    131 
    132                 if (!m_externalSVGFontElement)
    133                     m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
    134 
    135                 if (!m_externalSVGFontElement)
    136                     return 0;
    137 
    138                 SVGFontFaceElement* fontFaceElement = 0;
    139 
    140                 // Select first <font-face> child
    141                 for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
    142                     if (fontChild->hasTagName(SVGNames::font_faceTag)) {
    143                         fontFaceElement = static_cast<SVGFontFaceElement*>(fontChild);
    144                         break;
    145                     }
    146                 }
    147 
    148                 if (fontFaceElement) {
    149                     if (!m_svgFontFaceElement) {
    150                         // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
    151                         // Use the imported <font-face> tag as referencing font-face element for these cases.
    152                         m_svgFontFaceElement = fontFaceElement;
    153                     }
    154 
    155                     SVGFontData* svgFontData = new SVGFontData(fontFaceElement);
    156                     fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false, svgFontData));
    157                 }
    158             } else
    159 #endif
    160             {
    161                 // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef.
    162                 if (!m_font->ensureCustomFontData())
    163                     return 0;
    164 
    165                 fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false));
    166             }
    167         } else {
    168 #if ENABLE(SVG_FONTS)
    169             // In-Document SVG Fonts
    170             if (m_svgFontFaceElement) {
    171                 SVGFontData* svgFontData = new SVGFontData(m_svgFontFaceElement);
    172                 fontData.set(new SimpleFontData(FontPlatformData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic), true, false, svgFontData));
    173             }
    174 #endif
    175         }
    176     } else {
    177         // Kick off the load now.
    178         if (DocLoader* docLoader = fontSelector->docLoader())
    179             m_font->beginLoadIfNeeded(docLoader);
    180         // FIXME: m_string is a URL so it makes no sense to pass it as a family name.
    181         SimpleFontData* tempData = fontCache()->getCachedFontData(fontDescription, m_string);
    182         if (!tempData)
    183             tempData = fontCache()->getLastResortFallbackFont(fontDescription);
    184 
    185         fontData.set(new SimpleFontData(tempData->platformData(), true, true));
    186     }
    187 
    188     m_fontDataTable.set(hashKey, fontData.get());
    189     return fontData.release();
    190 }
    191 
    192 }
    193 
    194