2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "CSSFontFaceSource.h"
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"
39 #include "FontCustomPlatformData.h"
40 #include "HTMLNames.h"
41 #include "SVGFontData.h"
42 #include "SVGFontElement.h"
43 #include "SVGFontFaceElement.h"
44 #include "SVGURIReference.h"
49 CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font)
54 , m_svgFontFaceElement(0)
58 m_font->addClient(this);
61 CSSFontFaceSource::~CSSFontFaceSource()
64 m_font->removeClient(this);
68 void CSSFontFaceSource::pruneTable()
70 if (m_fontDataTable.isEmpty())
72 HashMap<unsigned, SimpleFontData*>::iterator end = m_fontDataTable.end();
73 for (HashMap<unsigned, SimpleFontData*>::iterator it = m_fontDataTable.begin(); it != end; ++it)
74 GlyphPageTreeNode::pruneTreeCustomFontData(it->second);
75 deleteAllValues(m_fontDataTable);
76 m_fontDataTable.clear();
79 bool CSSFontFaceSource::isLoaded() const
82 return m_font->isLoaded();
86 bool CSSFontFaceSource::isValid() const
89 return !m_font->errorOccurred();
93 void CSSFontFaceSource::fontLoaded(CachedFont*)
97 m_face->fontLoaded(this);
100 SimpleFontData* CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
102 // If the font hasn't loaded or an error occurred, then we've got nothing.
106 #if ENABLE(SVG_FONTS)
107 if (!m_font && !m_svgFontFaceElement) {
111 SimpleFontData* fontData = fontCache()->getCachedFontData(fontDescription, m_string);
113 // We're local. Just return a SimpleFontData from the normal cache.
117 // See if we have a mapping in our FontData cache.
118 unsigned hashKey = fontDescription.computedPixelSize() << 2 | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
119 if (SimpleFontData* cachedData = m_fontDataTable.get(hashKey))
122 OwnPtr<SimpleFontData> fontData;
124 // If we are still loading, then we let the system pick a font.
127 #if ENABLE(SVG_FONTS)
128 if (m_font->isSVGFont()) {
129 // For SVG fonts parse the external SVG document, and extract the <font> element.
130 if (!m_font->ensureSVGFontData())
133 if (!m_externalSVGFontElement)
134 m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
136 if (!m_externalSVGFontElement)
139 SVGFontFaceElement* fontFaceElement = 0;
141 // Select first <font-face> child
142 for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
143 if (fontChild->hasTagName(SVGNames::font_faceTag)) {
144 fontFaceElement = static_cast<SVGFontFaceElement*>(fontChild);
149 if (fontFaceElement) {
150 if (!m_svgFontFaceElement) {
151 // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
152 // Use the imported <font-face> tag as referencing font-face element for these cases.
153 m_svgFontFaceElement = fontFaceElement;
156 fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(fontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
161 // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef.
162 if (!m_font->ensureCustomFontData())
165 fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false));
168 #if ENABLE(SVG_FONTS)
169 // In-Document SVG Fonts
170 if (m_svgFontFaceElement)
171 fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(m_svgFontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
175 // Kick off the load now.
176 if (DocLoader* docLoader = fontSelector->docLoader())
177 m_font->beginLoadIfNeeded(docLoader);
178 // FIXME: m_string is a URL so it makes no sense to pass it as a family name.
179 SimpleFontData* tempData = fontCache()->getCachedFontData(fontDescription, m_string);
181 tempData = fontCache()->getLastResortFallbackFont(fontDescription);
183 fontData.set(new SimpleFontData(tempData->platformData(), true, true));
186 SimpleFontData* fontDataRawPtr = fontData.leakPtr();
187 m_fontDataTable.set(hashKey, fontDataRawPtr);
189 return fontDataRawPtr;