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 #include "CachedFontClient.h"
31 #include "CachedResourceClientWalker.h"
32 #include "CachedResourceLoader.h"
33 #include "FontCustomPlatformData.h"
34 #include "FontPlatformData.h"
35 #include "MemoryCache.h"
36 #include "ResourceBuffer.h"
37 #include "SharedBuffer.h"
38 #include "TextResourceDecoder.h"
39 #include "WOFFFileFormat.h"
40 #include <wtf/Vector.h>
44 #include "SVGDocument.h"
45 #include "SVGElement.h"
46 #include "SVGFontElement.h"
47 #include "SVGGElement.h"
53 CachedFont::CachedFont(const ResourceRequest& resourceRequest)
54 : CachedResource(resourceRequest, FontResource)
55 , m_loadInitiated(false)
56 , m_hasCreatedFontData(false)
60 CachedFont::~CachedFont()
64 void CachedFont::load(CachedResourceLoader*, const ResourceLoaderOptions& options)
66 // Don't load the file yet. Wait for an access before triggering the load.
71 void CachedFont::didAddClient(CachedResourceClient* c)
73 ASSERT(c->resourceClientType() == CachedFontClient::expectedType());
75 static_cast<CachedFontClient*>(c)->fontLoaded(this);
78 void CachedFont::finishLoading(ResourceBuffer* data)
81 setEncodedSize(m_data.get() ? m_data->size() : 0);
86 void CachedFont::beginLoadIfNeeded(CachedResourceLoader* dl)
88 if (!m_loadInitiated) {
89 m_loadInitiated = true;
90 CachedResource::load(dl, m_options);
94 bool CachedFont::ensureCustomFontData()
96 if (!m_fontData && !errorOccurred() && !isLoading() && m_data) {
97 SharedBuffer* buffer = m_data.get()->sharedBuffer();
100 RefPtr<SharedBuffer> sfntBuffer;
101 if (isWOFF(buffer)) {
103 if (convertWOFFToSfnt(buffer, sfnt)) {
104 sfntBuffer = SharedBuffer::adoptVector(sfnt);
105 buffer = sfntBuffer.get();
110 m_fontData = buffer ? createFontCustomPlatformData(*buffer) : nullptr;
112 m_hasCreatedFontData = true;
114 setStatus(DecodeError);
116 return m_fontData.get();
119 FontPlatformData CachedFont::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, FontWidthVariant widthVariant, FontRenderingMode renderingMode)
121 #if ENABLE(SVG_FONTS)
122 if (m_externalSVGDocument)
123 return FontPlatformData(size, bold, italic);
126 return m_fontData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, widthVariant, renderingMode);
129 #if ENABLE(SVG_FONTS)
130 bool CachedFont::ensureSVGFontData()
132 if (!m_externalSVGDocument && !errorOccurred() && !isLoading() && m_data) {
133 m_externalSVGDocument = SVGDocument::create(0, URL());
135 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml");
136 String svgSource = decoder->decode(m_data->data(), m_data->size());
137 svgSource.append(decoder->flush());
139 m_externalSVGDocument->setContent(svgSource);
141 if (decoder->sawError())
142 m_externalSVGDocument = 0;
145 return m_externalSVGDocument;
148 SVGFontElement* CachedFont::getSVGFontById(const String& fontName) const
150 RefPtr<NodeList> list = m_externalSVGDocument->getElementsByTagNameNS(SVGNames::fontTag.namespaceURI(), SVGNames::fontTag.localName());
154 unsigned listLength = list->length();
159 for (unsigned i = 0; i < listLength; ++i) {
160 ASSERT(list->item(i));
161 ASSERT(isSVGFontElement(list->item(i)));
165 if (fontName.isEmpty())
166 return toSVGFontElement(list->item(0));
168 for (unsigned i = 0; i < listLength; ++i) {
169 SVGFontElement* element = toSVGFontElement(list->item(i));
170 if (element->getIdAttribute() == fontName)
178 void CachedFont::allClientsRemoved()
180 m_fontData = nullptr;
183 void CachedFont::checkNotify()
188 CachedResourceClientWalker<CachedFontClient> w(m_clients);
189 while (CachedFontClient* c = w.next())
193 bool CachedFont::mayTryReplaceEncodedData() const
195 // If the FontCustomPlatformData has ever been constructed then it still might be in use somewhere.
196 // That platform font object might directly reference the encoded data buffer behind this CachedFont,
197 // so replacing it is unsafe.
199 return !m_hasCreatedFontData;