• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008, 2010 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 "CachedResourceLoader.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 "SVGFontData.h"
41 #include "SVGFontElement.h"
42 #include "SVGFontFaceElement.h"
43 #include "SVGNames.h"
44 #include "SVGURIReference.h"
45 #endif
46 
47 namespace WebCore {
48 
CSSFontFaceSource(const String & str,CachedFont * font)49 CSSFontFaceSource::CSSFontFaceSource(const String& str, CachedFont* font)
50     : m_string(str)
51     , m_font(font)
52     , m_face(0)
53 #if ENABLE(SVG_FONTS)
54     , m_hasExternalSVGFont(false)
55 #endif
56 {
57     if (m_font)
58         m_font->addClient(this);
59 }
60 
~CSSFontFaceSource()61 CSSFontFaceSource::~CSSFontFaceSource()
62 {
63     if (m_font)
64         m_font->removeClient(this);
65     pruneTable();
66 }
67 
pruneTable()68 void CSSFontFaceSource::pruneTable()
69 {
70     if (m_fontDataTable.isEmpty())
71         return;
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();
77 }
78 
isLoaded() const79 bool CSSFontFaceSource::isLoaded() const
80 {
81     if (m_font)
82         return m_font->isLoaded();
83     return true;
84 }
85 
isValid() const86 bool CSSFontFaceSource::isValid() const
87 {
88     if (m_font)
89         return !m_font->errorOccurred();
90     return true;
91 }
92 
fontLoaded(CachedFont *)93 void CSSFontFaceSource::fontLoaded(CachedFont*)
94 {
95     pruneTable();
96     if (m_face)
97         m_face->fontLoaded(this);
98 }
99 
getFontData(const FontDescription & fontDescription,bool syntheticBold,bool syntheticItalic,CSSFontSelector * fontSelector)100 SimpleFontData* CSSFontFaceSource::getFontData(const FontDescription& fontDescription, bool syntheticBold, bool syntheticItalic, CSSFontSelector* fontSelector)
101 {
102     // If the font hasn't loaded or an error occurred, then we've got nothing.
103     if (!isValid())
104         return 0;
105 
106 #if ENABLE(SVG_FONTS)
107     if (!m_font && !m_svgFontFaceElement) {
108 #else
109     if (!m_font) {
110 #endif
111         SimpleFontData* fontData = fontCache()->getCachedFontData(fontDescription, m_string);
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() + 1) << 6 | fontDescription.widthVariant() << 4
119                        | (fontDescription.textOrientation() == TextOrientationUpright ? 8 : 0) | (fontDescription.orientation() == Vertical ? 4 : 0) | (syntheticBold ? 2 : 0) | (syntheticItalic ? 1 : 0);
120     if (SimpleFontData* cachedData = m_fontDataTable.get(hashKey))
121         return cachedData;
122 
123     OwnPtr<SimpleFontData> fontData;
124 
125     // If we are still loading, then we let the system pick a font.
126     if (isLoaded()) {
127         if (m_font) {
128 #if ENABLE(SVG_FONTS)
129             if (m_hasExternalSVGFont) {
130                 // For SVG fonts parse the external SVG document, and extract the <font> element.
131                 if (!m_font->ensureSVGFontData())
132                     return 0;
133 
134                 if (!m_externalSVGFontElement)
135                     m_externalSVGFontElement = m_font->getSVGFontById(SVGURIReference::getTarget(m_string));
136 
137                 if (!m_externalSVGFontElement)
138                     return 0;
139 
140                 SVGFontFaceElement* fontFaceElement = 0;
141 
142                 // Select first <font-face> child
143                 for (Node* fontChild = m_externalSVGFontElement->firstChild(); fontChild; fontChild = fontChild->nextSibling()) {
144                     if (fontChild->hasTagName(SVGNames::font_faceTag)) {
145                         fontFaceElement = static_cast<SVGFontFaceElement*>(fontChild);
146                         break;
147                     }
148                 }
149 
150                 if (fontFaceElement) {
151                     if (!m_svgFontFaceElement) {
152                         // We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
153                         // Use the imported <font-face> tag as referencing font-face element for these cases.
154                         m_svgFontFaceElement = fontFaceElement;
155                     }
156 
157                     fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(fontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
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.orientation(),
167                                                                                    fontDescription.textOrientation(), fontDescription.widthVariant(), fontDescription.renderingMode()), true, false));
168             }
169         } else {
170 #if ENABLE(SVG_FONTS)
171             // In-Document SVG Fonts
172             if (m_svgFontFaceElement)
173                 fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(m_svgFontFaceElement.get())), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
174 #endif
175         }
176     } else {
177         // Kick off the load now.
178         if (CachedResourceLoader* cachedResourceLoader = fontSelector->cachedResourceLoader())
179             m_font->beginLoadIfNeeded(cachedResourceLoader);
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     SimpleFontData* fontDataRawPtr = fontData.leakPtr();
189     m_fontDataTable.set(hashKey, fontDataRawPtr);
190 
191     return fontDataRawPtr;
192 }
193 
194 #if ENABLE(SVG_FONTS)
195 SVGFontFaceElement* CSSFontFaceSource::svgFontFaceElement() const
196 {
197     return m_svgFontFaceElement.get();
198 }
199 
200 void CSSFontFaceSource::setSVGFontFaceElement(PassRefPtr<SVGFontFaceElement> element)
201 {
202     m_svgFontFaceElement = element;
203 }
204 
205 bool CSSFontFaceSource::isSVGFontFaceSource() const
206 {
207     return m_svgFontFaceElement || m_hasExternalSVGFont;
208 }
209 #endif
210 
211 }
212