Use "= default" to denote default constructor or destructor
[WebKit-https.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 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 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 #include "CachedFontClient.h"
31 #include "CachedResourceClientWalker.h"
32 #include "CachedResourceLoader.h"
33 #include "FontCustomPlatformData.h"
34 #include "FontDescription.h"
35 #include "FontPlatformData.h"
36 #include "SharedBuffer.h"
37 #include "TextResourceDecoder.h"
38 #include "TypedElementDescendantIterator.h"
39 #include "WOFFFileFormat.h"
40 #include <wtf/Vector.h>
41
42 #if USE(DIRECT2D)
43 #include <dwrite.h>
44 #endif
45
46 namespace WebCore {
47
48 CachedFont::CachedFont(CachedResourceRequest&& request, PAL::SessionID sessionID, Type type)
49     : CachedResource(WTFMove(request), type, sessionID)
50     , m_loadInitiated(false)
51     , m_hasCreatedFontDataWrappingResource(false)
52 {
53 }
54
55 CachedFont::~CachedFont() = default;
56
57 void CachedFont::load(CachedResourceLoader&)
58 {
59     // Don't load the file yet.  Wait for an access before triggering the load.
60     setLoading(true);
61 }
62
63 void CachedFont::didAddClient(CachedResourceClient& client)
64 {
65     ASSERT(client.resourceClientType() == CachedFontClient::expectedType());
66     if (!isLoading())
67         static_cast<CachedFontClient&>(client).fontLoaded(*this);
68 }
69
70 void CachedFont::finishLoading(SharedBuffer* data)
71 {
72     m_data = data;
73     setEncodedSize(m_data.get() ? m_data->size() : 0);
74     setLoading(false);
75     checkNotify();
76 }
77
78 void CachedFont::beginLoadIfNeeded(CachedResourceLoader& loader)
79 {
80     if (!m_loadInitiated) {
81         m_loadInitiated = true;
82         CachedResource::load(loader);
83     }
84 }
85
86 bool CachedFont::ensureCustomFontData(const AtomicString&)
87 {
88     return ensureCustomFontData(m_data.get());
89 }
90
91 bool CachedFont::ensureCustomFontData(SharedBuffer* data)
92 {
93     if (!m_fontCustomPlatformData && !errorOccurred() && !isLoading() && data) {
94         bool wrapping;
95         m_fontCustomPlatformData = createCustomFontData(*data, wrapping);
96         m_hasCreatedFontDataWrappingResource = m_fontCustomPlatformData && wrapping;
97         if (!m_fontCustomPlatformData)
98             setStatus(DecodeError);
99     }
100
101     return m_fontCustomPlatformData.get();
102 }
103
104 std::unique_ptr<FontCustomPlatformData> CachedFont::createCustomFontData(SharedBuffer& bytes, bool& wrapping)
105 {
106     wrapping = true;
107
108 #if !PLATFORM(COCOA)
109     if (isWOFF(bytes)) {
110         wrapping = false;
111         Vector<char> convertedFont;
112         if (!convertWOFFToSfnt(bytes, convertedFont))
113             return nullptr;
114
115         auto buffer = SharedBuffer::create(WTFMove(convertedFont));
116         return createFontCustomPlatformData(buffer);
117     }
118 #endif
119
120     return createFontCustomPlatformData(bytes);
121 }
122
123 RefPtr<Font> CachedFont::createFont(const FontDescription& fontDescription, const AtomicString&, bool syntheticBold, bool syntheticItalic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities)
124 {
125     return Font::create(platformDataFromCustomData(fontDescription, syntheticBold, syntheticItalic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities), Font::Origin::Remote);
126 }
127
128 FontPlatformData CachedFont::platformDataFromCustomData(const FontDescription& fontDescription, bool bold, bool italic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities)
129 {
130     ASSERT(m_fontCustomPlatformData);
131     return platformDataFromCustomData(*m_fontCustomPlatformData, fontDescription, bold, italic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities);
132 }
133
134 FontPlatformData CachedFont::platformDataFromCustomData(FontCustomPlatformData& fontCustomPlatformData, const FontDescription& fontDescription, bool bold, bool italic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities)
135 {
136 #if PLATFORM(COCOA)
137     return fontCustomPlatformData.fontPlatformData(fontDescription, bold, italic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities);
138 #else
139     UNUSED_PARAM(fontFaceFeatures);
140     UNUSED_PARAM(fontFaceVariantSettings);
141     UNUSED_PARAM(fontFaceCapabilities);
142     return fontCustomPlatformData.fontPlatformData(fontDescription, bold, italic);
143 #endif
144 }
145
146 void CachedFont::allClientsRemoved()
147 {
148     m_fontCustomPlatformData = nullptr;
149 }
150
151 void CachedFont::checkNotify()
152 {
153     if (isLoading())
154         return;
155     
156     CachedResourceClientWalker<CachedFontClient> walker(m_clients);
157     while (CachedFontClient* client = walker.next())
158         client->fontLoaded(*this);
159 }
160
161 bool CachedFont::mayTryReplaceEncodedData() const
162 {
163     // If a FontCustomPlatformData has ever been constructed to wrap the internal resource buffer then it still might be in use somewhere.
164     // That platform font object might directly reference the encoded data buffer behind this CachedFont,
165     // so replacing it is unsafe.
166
167     return !m_hasCreatedFontDataWrappingResource;
168 }
169
170 }