1 /* 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Torch Mobile, Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 25 */ 26 27 #include "config.h" 28 #include "core/loader/cache/FontResource.h" 29 30 #include "core/loader/TextResourceDecoder.h" 31 #include "core/loader/cache/ResourceClient.h" 32 #include "core/loader/cache/ResourceClientWalker.h" 33 #include "core/platform/SharedBuffer.h" 34 #include "core/platform/graphics/FontCustomPlatformData.h" 35 #include "core/platform/graphics/FontPlatformData.h" 36 37 #if ENABLE(SVG_FONTS) 38 #include "SVGNames.h" 39 #include "core/dom/NodeList.h" 40 #include "core/svg/SVGDocument.h" 41 #include "core/svg/SVGFontElement.h" 42 #endif 43 44 namespace WebCore { 45 46 FontResource::FontResource(const ResourceRequest& resourceRequest) 47 : Resource(resourceRequest, Font) 48 , m_loadInitiated(false) 49 { 50 } 51 52 FontResource::~FontResource() 53 { 54 } 55 56 void FontResource::load(ResourceFetcher*, const ResourceLoaderOptions& options) 57 { 58 // Don't load the file yet. Wait for an access before triggering the load. 59 setLoading(true); 60 m_options = options; 61 } 62 63 void FontResource::didAddClient(ResourceClient* c) 64 { 65 ASSERT(c->resourceClientType() == FontResourceClient::expectedType()); 66 if (!isLoading()) 67 static_cast<FontResourceClient*>(c)->fontLoaded(this); 68 } 69 70 void FontResource::beginLoadIfNeeded(ResourceFetcher* dl) 71 { 72 if (!m_loadInitiated) { 73 m_loadInitiated = true; 74 Resource::load(dl, m_options); 75 76 ResourceClientWalker<FontResourceClient> walker(m_clients); 77 while (FontResourceClient* client = walker.next()) 78 client->didStartFontLoad(this); 79 } 80 } 81 82 bool FontResource::ensureCustomFontData() 83 { 84 if (!m_fontData && !errorOccurred() && !isLoading() && m_data) { 85 m_fontData = FontCustomPlatformData::create(m_data.get()); 86 if (!m_fontData) 87 setStatus(DecodeError); 88 } 89 return m_fontData; 90 } 91 92 FontPlatformData FontResource::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, FontWidthVariant widthVariant) 93 { 94 #if ENABLE(SVG_FONTS) 95 if (m_externalSVGDocument) 96 return FontPlatformData(size, bold, italic); 97 #endif 98 ASSERT(m_fontData); 99 return m_fontData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, widthVariant); 100 } 101 102 #if ENABLE(SVG_FONTS) 103 bool FontResource::ensureSVGFontData() 104 { 105 if (!m_externalSVGDocument && !errorOccurred() && !isLoading() && m_data) { 106 m_externalSVGDocument = SVGDocument::create(); 107 108 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml"); 109 String svgSource = decoder->decode(m_data->data(), m_data->size()); 110 svgSource.append(decoder->flush()); 111 112 m_externalSVGDocument->setContent(svgSource); 113 114 if (decoder->sawError()) 115 m_externalSVGDocument = 0; 116 } 117 118 return m_externalSVGDocument; 119 } 120 121 SVGFontElement* FontResource::getSVGFontById(const String& fontName) const 122 { 123 RefPtr<NodeList> list = m_externalSVGDocument->getElementsByTagNameNS(SVGNames::fontTag.namespaceURI(), SVGNames::fontTag.localName()); 124 if (!list) 125 return 0; 126 127 unsigned listLength = list->length(); 128 if (!listLength) 129 return 0; 130 131 #ifndef NDEBUG 132 for (unsigned i = 0; i < listLength; ++i) { 133 ASSERT(list->item(i)); 134 ASSERT(list->item(i)->hasTagName(SVGNames::fontTag)); 135 } 136 #endif 137 138 if (fontName.isEmpty()) 139 return toSVGFontElement(list->item(0)); 140 141 for (unsigned i = 0; i < listLength; ++i) { 142 SVGFontElement* element = toSVGFontElement(list->item(i)); 143 if (element->getIdAttribute() == fontName) 144 return element; 145 } 146 147 return 0; 148 } 149 #endif 150 151 void FontResource::allClientsRemoved() 152 { 153 m_fontData.clear(); 154 Resource::allClientsRemoved(); 155 } 156 157 void FontResource::checkNotify() 158 { 159 ResourceClientWalker<FontResourceClient> w(m_clients); 160 while (FontResourceClient* c = w.next()) 161 c->fontLoaded(this); 162 } 163 164 } 165