OSDN Git Service

am 3c54ece0: am 5dc34a85: activeDocumentLoader() causes crash in WebCoreFrameBridge.cpp
[android-x86/external-webkit.git] / WebCore / css / CSSFontFaceSource.cpp
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 "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 "SVGFontFaceElement.h"
44 #include "SVGURIReference.h"
45 #endif
46
47 namespace WebCore {
48
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_svgFontFaceElement(0)
55 #endif
56 {
57     if (m_font)
58         m_font->addClient(this);
59 }
60
61 CSSFontFaceSource::~CSSFontFaceSource()
62 {
63     if (m_font)
64         m_font->removeClient(this);
65     pruneTable();
66 }
67
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
79 bool CSSFontFaceSource::isLoaded() const
80 {
81     if (m_font)
82         return m_font->isLoaded();
83     return true;
84 }
85
86 bool CSSFontFaceSource::isValid() const
87 {
88     if (m_font)
89         return !m_font->errorOccurred();
90     return true;
91 }
92
93 void CSSFontFaceSource::fontLoaded(CachedFont*)
94 {
95     pruneTable();
96     if (m_face)
97         m_face->fontLoaded(this);
98 }
99
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() << 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                     fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(fontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
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                 fontData.set(new SimpleFontData(adoptPtr(new SVGFontData(m_svgFontFaceElement)), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic));
172 #endif
173         }
174     } else {
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);
180         if (!tempData)
181             tempData = fontCache()->getLastResortFallbackFont(fontDescription);
182
183         fontData.set(new SimpleFontData(tempData->platformData(), true, true));
184     }
185
186     SimpleFontData* fontDataRawPtr = fontData.leakPtr();
187     m_fontDataTable.set(hashKey, fontDataRawPtr);
188
189     return fontDataRawPtr;
190 }
191
192 }