tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / loader / cache / CachedFont.cpp
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 "CachedFont.h"
29
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
33 #endif
34
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>
43
44 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
45 #include "FontCustomPlatformData.h"
46 #endif
47
48 #if ENABLE(SVG_FONTS)
49 #include "NodeList.h"
50 #include "SVGDocument.h"
51 #include "SVGElement.h"
52 #include "SVGFontElement.h"
53 #include "SVGGElement.h"
54 #include "SVGNames.h"
55 #endif
56
57 namespace WebCore {
58
59 CachedFont::CachedFont(const ResourceRequest& resourceRequest)
60     : CachedResource(resourceRequest, FontResource)
61     , m_fontData(0)
62     , m_loadInitiated(false)
63 {
64 }
65
66 CachedFont::~CachedFont()
67 {
68 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
69     delete m_fontData;
70 #endif
71 }
72
73 void CachedFont::load(CachedResourceLoader*, const ResourceLoaderOptions& options)
74 {
75     // Don't load the file yet.  Wait for an access before triggering the load.
76     setLoading(true);
77     m_options = options;
78 }
79
80 void CachedFont::didAddClient(CachedResourceClient* c)
81 {
82     ASSERT(c->resourceClientType() == CachedFontClient::expectedType());
83     if (!isLoading())
84         static_cast<CachedFontClient*>(c)->fontLoaded(this);
85 }
86
87 void CachedFont::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
88 {
89     if (!allDataReceived)
90         return;
91
92     m_data = data;     
93     setEncodedSize(m_data.get() ? m_data->size() : 0);
94     setLoading(false);
95     checkNotify();
96 }
97
98 void CachedFont::beginLoadIfNeeded(CachedResourceLoader* dl)
99 {
100     if (!m_loadInitiated) {
101         m_loadInitiated = true;
102         CachedResource::load(dl, m_options);
103     }
104 }
105
106 bool CachedFont::ensureCustomFontData()
107 {
108 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
109     if (!m_fontData && !errorOccurred() && !isLoading() && m_data) {
110         m_fontData = createFontCustomPlatformData(m_data.get());
111         if (!m_fontData)
112             setStatus(DecodeError);
113     }
114 #endif
115     return m_fontData;
116 }
117
118 FontPlatformData CachedFont::platformDataFromCustomData(float size, bool bold, bool italic, FontOrientation orientation, TextOrientation textOrientation, FontWidthVariant widthVariant, FontRenderingMode renderingMode)
119 {
120 #if ENABLE(SVG_FONTS)
121     if (m_externalSVGDocument)
122         return FontPlatformData(size, bold, italic);
123 #endif
124 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
125     ASSERT(m_fontData);
126     return m_fontData->fontPlatformData(static_cast<int>(size), bold, italic, orientation, textOrientation, widthVariant, renderingMode);
127 #else
128     return FontPlatformData();
129 #endif
130 }
131
132 #if ENABLE(SVG_FONTS)
133 bool CachedFont::ensureSVGFontData()
134 {
135     if (!m_externalSVGDocument && !errorOccurred() && !isLoading() && m_data) {
136         m_externalSVGDocument = SVGDocument::create(0, KURL());
137
138         RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("application/xml");
139         String svgSource = decoder->decode(m_data->data(), m_data->size());
140         svgSource += decoder->flush();
141         
142         m_externalSVGDocument->setContent(svgSource);
143         
144         if (decoder->sawError())
145             m_externalSVGDocument = 0;
146     }
147
148     return m_externalSVGDocument;
149 }
150
151 SVGFontElement* CachedFont::getSVGFontById(const String& fontName) const
152 {
153     RefPtr<NodeList> list = m_externalSVGDocument->getElementsByTagNameNS(SVGNames::fontTag.namespaceURI(), SVGNames::fontTag.localName());
154     if (!list)
155         return 0;
156
157     unsigned listLength = list->length();
158     if (!listLength)
159         return 0;
160
161 #ifndef NDEBUG
162     for (unsigned i = 0; i < listLength; ++i) {
163         ASSERT(list->item(i));
164         ASSERT(list->item(i)->hasTagName(SVGNames::fontTag));
165     }
166 #endif
167
168     if (fontName.isEmpty())
169         return static_cast<SVGFontElement*>(list->item(0));
170
171     for (unsigned i = 0; i < listLength; ++i) {
172         SVGFontElement* element = static_cast<SVGFontElement*>(list->item(i));
173         if (element->getIdAttribute() == fontName)
174             return element;
175     }
176
177     return 0;
178 }
179 #endif
180
181 void CachedFont::allClientsRemoved()
182 {
183 #ifdef STORE_FONT_CUSTOM_PLATFORM_DATA
184     if (m_fontData) {
185         delete m_fontData;
186         m_fontData = 0;
187     }
188 #endif
189 }
190
191 void CachedFont::checkNotify()
192 {
193     if (isLoading())
194         return;
195     
196     CachedResourceClientWalker<CachedFontClient> w(m_clients);
197     while (CachedFontClient* c = w.next())
198          c->fontLoaded(this);
199 }
200
201
202 void CachedFont::error(CachedResource::Status status)
203 {
204     setStatus(status);
205     ASSERT(errorOccurred());
206     setLoading(false);
207     checkNotify();
208 }
209
210 }