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 "core/fetch/FontResource.h"
30 #include "core/fetch/ResourceClientWalker.h"
31 #include "core/html/parser/TextResourceDecoder.h"
32 #include "platform/SharedBuffer.h"
33 #include "platform/fonts/FontCustomPlatformData.h"
34 #include "platform/fonts/FontPlatformData.h"
35 #include "public/platform/Platform.h"
36 #include "wtf/CurrentTime.h"
40 #include "core/html/HTMLCollection.h"
41 #include "core/svg/SVGDocument.h"
42 #include "core/svg/SVGFontElement.h"
47 FontResource::FontResource(const ResourceRequest& resourceRequest)
48 : Resource(resourceRequest, Font)
49 , m_loadInitiated(false)
53 FontResource::~FontResource()
57 void FontResource::load(ResourceFetcher*, const ResourceLoaderOptions& options)
59 // Don't load the file yet. Wait for an access before triggering the load.
64 void FontResource::didAddClient(ResourceClient* c)
66 ASSERT(c->resourceClientType() == FontResourceClient::expectedType());
67 Resource::didAddClient(c);
69 static_cast<FontResourceClient*>(c)->fontLoaded(this);
72 void FontResource::beginLoadIfNeeded(ResourceFetcher* dl)
74 if (!m_loadInitiated) {
75 m_loadInitiated = true;
76 Resource::load(dl, m_options);
78 ResourceClientWalker<FontResourceClient> walker(m_clients);
79 while (FontResourceClient* client = walker.next())
80 client->didStartFontLoad(this);
84 bool FontResource::ensureCustomFontData()
86 if (!m_fontData && !errorOccurred() && !isLoading()) {
88 m_fontData = FontCustomPlatformData::create(m_data.get());
90 setStatus(DecodeError);
95 FontPlatformData FontResource::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, FontWidthVariant widthVariant)
98 if (m_externalSVGDocument)
99 return FontPlatformData(size, bold, italic);
102 return m_fontData->fontPlatformData(size, bold, italic, orientation, widthVariant);
105 #if ENABLE(SVG_FONTS)
106 bool FontResource::ensureSVGFontData()
108 if (!m_externalSVGDocument && !errorOccurred() && !isLoading()) {
110 m_externalSVGDocument = SVGDocument::create();
112 OwnPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml");
113 String svgSource = decoder->decode(m_data->data(), m_data->size());
114 svgSource = svgSource + decoder->flush();
116 m_externalSVGDocument->setContent(svgSource);
118 if (decoder->sawError())
119 m_externalSVGDocument = 0;
121 if (!m_externalSVGDocument)
122 setStatus(DecodeError);
125 return m_externalSVGDocument;
128 SVGFontElement* FontResource::getSVGFontById(const String& fontName) const
130 RefPtr<HTMLCollection> collection = m_externalSVGDocument->getElementsByTagNameNS(SVGNames::fontTag.namespaceURI(), SVGNames::fontTag.localName());
134 unsigned collectionLength = collection->length();
135 if (!collectionLength)
139 for (unsigned i = 0; i < collectionLength; ++i) {
140 ASSERT(collection->item(i));
141 ASSERT(collection->item(i)->hasTagName(SVGNames::fontTag));
145 if (fontName.isEmpty())
146 return toSVGFontElement(collection->item(0));
148 for (unsigned i = 0; i < collectionLength; ++i) {
149 SVGFontElement* element = toSVGFontElement(collection->item(i));
150 if (element->getIdAttribute() == fontName)
158 void FontResource::allClientsRemoved()
161 Resource::allClientsRemoved();
164 void FontResource::checkNotify()
166 ResourceClientWalker<FontResourceClient> w(m_clients);
167 while (FontResourceClient* c = w.next())