• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CSSFontFaceSource(const String & str,CachedFont * font)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 
~CSSFontFaceSource()60 CSSFontFaceSource::~CSSFontFaceSource()
61 {
62     if (m_font)
63         m_font->removeClient(this);
64     pruneTable();
65 }
66 
pruneTable()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 
isLoaded() const78 bool CSSFontFaceSource::isLoaded() const
79 {
80     if (m_font)
81         return m_font->isLoaded();
82     return true;
83 }
84 
isValid() const85 bool CSSFontFaceSource::isValid() const
86 {
87     if (m_font)
88         return !m_font->errorOccurred();
89     return true;
90 }
91 
fontLoaded(CachedFont *)92 void CSSFontFaceSource::fontLoaded(CachedFont*)
93 {
94     pruneTable();
95     if (m_face)
96         m_face->fontLoaded(this);
97 }
98 
getFontData(const FontDescription & fontDescription,bool syntheticBold,bool syntheticItalic,CSSFontSelector * fontSelector)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         FontPlatformData* data = fontCache()->getCachedFontPlatformData(fontDescription, m_string);
111         SimpleFontData* fontData = fontCache()->getCachedFontData(data);
112 
113         // We're local. Just return a SimpleFontData from the normal cache.
114         return fontData;
115     }
116 
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))
120         return cachedData;
121 
122     OwnPtr<SimpleFontData> fontData;
123 
124     // If we are still loading, then we let the system pick a font.
125     if (isLoaded()) {
126         if (m_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())
131                     return 0;
132 
133                 if (!m_externalSVGFontElement)
134                     m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
135 
136                 if (!m_externalSVGFontElement)
137                     return 0;
138 
139                 SVGFontFaceElement* fontFaceElement = 0;
140 
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);
145                         break;
146                     }
147                 }
148 
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;
154                     }
155 
156                     SVGFontData* svgFontData = new SVGFontData(fontFaceElement);
157                     fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false, svgFontData));
158                 }
159             } else
160 #endif
161             {
162                 // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef.
163                 if (!m_font->ensureCustomFontData())
164                     return 0;
165 
166                 fontData.set(new SimpleFontData(m_font->platformDataFromCustomData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic, fontDescription.renderingMode()), true, false));
167             }
168         } else {
169 #if ENABLE(SVG_FONTS)
170             // In-Document SVG Fonts
171             if (m_svgFontFaceElement) {
172                 SVGFontData* svgFontData = new SVGFontData(m_svgFontFaceElement);
173                 fontData.set(new SimpleFontData(FontPlatformData(fontDescription.computedPixelSize(), syntheticBold, syntheticItalic), true, false, svgFontData));
174             }
175 #endif
176         }
177     } else {
178         // Kick off the load now.
179         if (DocLoader* docLoader = fontSelector->docLoader())
180             m_font->beginLoadIfNeeded(docLoader);
181         // FIXME: m_string is a URL so it makes no sense to pass it as a family name.
182         FontPlatformData* tempData = fontCache()->getCachedFontPlatformData(fontDescription, m_string);
183         if (!tempData)
184             tempData = fontCache()->getLastResortFallbackFont(fontDescription);
185         fontData.set(new SimpleFontData(*tempData, true, true));
186     }
187 
188     m_fontDataTable.set(hashKey, fontData.get());
189     return fontData.release();
190 }
191 
192 }
193 
194