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