2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "CachedFont.h"
30 // FIXME: This should really be a blacklist instead of a whitelist
31 #if USE(CG) || PLATFORM(QT) || PLATFORM(GTK) || (PLATFORM(CHROMIUM) && (!OS(DARWIN) || USE(SKIA_ON_MAC_CHROMIUM))) || OS(WINCE) || PLATFORM(EFL)
32 #define STORE_FONT_CUSTOM_PLATFORM_DATA
35 #include "CachedResourceClient.h"
36 #include "CachedResourceClientWalker.h"
37 #include "CachedResourceLoader.h"
38 #include "FontPlatformData.h"
39 #include "MemoryCache.h"
40 #include "SharedBuffer.h"
41 #include "TextResourceDecoder.h"
42 #include <wtf/Vector.h>
44 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
45 #include "FontCustomPlatformData.h"
50 #include "SVGDocument.h"
51 #include "SVGElement.h"
52 #include "SVGFontElement.h"
53 #include "SVGGElement.h"
59 CachedFont::CachedFont(const ResourceRequest& resourceRequest)
60 : CachedResource(resourceRequest, FontResource)
62 , m_loadInitiated(false)
66 CachedFont::~CachedFont()
68 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
73 void CachedFont::load(CachedResourceLoader*, const ResourceLoaderOptions& options)
75 // Don't load the file yet. Wait for an access before triggering the load.
80 void CachedFont::didAddClient(CachedResourceClient* c)
82 ASSERT(c->resourceClientType() == CachedFontClient::expectedType());
84 static_cast<CachedFontClient*>(c)->fontLoaded(this);
87 void CachedFont::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
93 setEncodedSize(m_data.get() ? m_data->size() : 0);
98 void CachedFont::beginLoadIfNeeded(CachedResourceLoader* dl)
100 if (!m_loadInitiated) {
101 m_loadInitiated = true;
102 CachedResource::load(dl, m_options);
106 bool CachedFont::ensureCustomFontData()
108 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
109 if (!m_fontData && !errorOccurred() && !isLoading() && m_data) {
110 m_fontData = createFontCustomPlatformData(m_data.get());
112 setStatus(DecodeError);
118 FontPlatformData CachedFont::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, TextOrientation textOrientation, FontWidthVariant widthVariant, FontRenderingMode renderingMode)
120 #if ENABLE(SVG_FONTS)
121 if (m_externalSVGDocument)
122 return FontPlatformData(size, bold, italic);
124 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
126 return m_fontData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, textOrientation, widthVariant, renderingMode);
128 return FontPlatformData();
132 #if ENABLE(SVG_FONTS)
133 bool CachedFont::ensureSVGFontData()
135 if (!m_externalSVGDocument && !errorOccurred() && !isLoading() && m_data) {
136 m_externalSVGDocument = SVGDocument::create(0, KURL());
138 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml");
139 String svgSource = decoder->decode(m_data->data(), m_data->size());
140 svgSource += decoder->flush();
142 m_externalSVGDocument->setContent(svgSource);
144 if (decoder->sawError())
145 m_externalSVGDocument = 0;
148 return m_externalSVGDocument;
151 SVGFontElement* CachedFont::getSVGFontById(const String& fontName) const
153 RefPtr<NodeList> list = m_externalSVGDocument->getElementsByTagNameNS(SVGNames::fontTag.namespaceURI(), SVGNames::fontTag.localName());
157 unsigned listLength = list->length();
162 for (unsigned i = 0; i < listLength; ++i) {
163 ASSERT(list->item(i));
164 ASSERT(list->item(i)->hasTagName(SVGNames::fontTag));
168 if (fontName.isEmpty())
169 return static_cast<SVGFontElement*>(list->item(0));
171 for (unsigned i = 0; i < listLength; ++i) {
172 SVGFontElement* element = static_cast<SVGFontElement*>(list->item(i));
173 if (element->getIdAttribute() == fontName)
181 void CachedFont::allClientsRemoved()
183 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
191 void CachedFont::checkNotify()
196 CachedResourceClientWalker<CachedFontClient> w(m_clients);
197 while (CachedFontClient* c = w.next())
202 void CachedFont::error(CachedResource::Status status)
205 ASSERT(errorOccurred());