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, const PAL::SessionID& sessionID, const CookieJar* cookieJar, Type type) |
49 | : CachedResource(WTFMove(request), type, sessionID, cookieJar) |
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 | String CachedFont::calculateItemInCollection() const |
92 | { |
93 | auto& url = this->url(); |
94 | if (!url.hasFragmentIdentifier()) |
95 | return String(); |
96 | return url.fragmentIdentifier(); |
97 | } |
98 | |
99 | bool CachedFont::ensureCustomFontData(SharedBuffer* data) |
100 | { |
101 | if (!m_fontCustomPlatformData && !errorOccurred() && !isLoading() && data) { |
102 | bool wrapping; |
103 | m_fontCustomPlatformData = createCustomFontData(*data, calculateItemInCollection(), wrapping); |
104 | m_hasCreatedFontDataWrappingResource = m_fontCustomPlatformData && wrapping; |
105 | if (!m_fontCustomPlatformData) |
106 | setStatus(DecodeError); |
107 | } |
108 | |
109 | return m_fontCustomPlatformData.get(); |
110 | } |
111 | |
112 | std::unique_ptr<FontCustomPlatformData> CachedFont::createCustomFontData(SharedBuffer& bytes, const String& itemInCollection, bool& wrapping) |
113 | { |
114 | wrapping = true; |
115 | |
116 | #if !PLATFORM(COCOA) |
117 | if (isWOFF(bytes)) { |
118 | wrapping = false; |
119 | Vector<char> convertedFont; |
120 | if (!convertWOFFToSfnt(bytes, convertedFont)) |
121 | return nullptr; |
122 | |
123 | auto buffer = SharedBuffer::create(WTFMove(convertedFont)); |
124 | return createFontCustomPlatformData(buffer, itemInCollection); |
125 | } |
126 | #endif |
127 | |
128 | return createFontCustomPlatformData(bytes, itemInCollection); |
129 | } |
130 | |
131 | RefPtr<Font> CachedFont::createFont(const FontDescription& fontDescription, const AtomicString&, bool syntheticBold, bool syntheticItalic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities) |
132 | { |
133 | return Font::create(platformDataFromCustomData(fontDescription, syntheticBold, syntheticItalic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities), Font::Origin::Remote); |
134 | } |
135 | |
136 | FontPlatformData CachedFont::platformDataFromCustomData(const FontDescription& fontDescription, bool bold, bool italic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities) |
137 | { |
138 | ASSERT(m_fontCustomPlatformData); |
139 | return platformDataFromCustomData(*m_fontCustomPlatformData, fontDescription, bold, italic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities); |
140 | } |
141 | |
142 | FontPlatformData CachedFont::platformDataFromCustomData(FontCustomPlatformData& fontCustomPlatformData, const FontDescription& fontDescription, bool bold, bool italic, const FontFeatureSettings& fontFaceFeatures, const FontVariantSettings& fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities) |
143 | { |
144 | return fontCustomPlatformData.fontPlatformData(fontDescription, bold, italic, fontFaceFeatures, fontFaceVariantSettings, fontFaceCapabilities); |
145 | } |
146 | |
147 | void CachedFont::allClientsRemoved() |
148 | { |
149 | m_fontCustomPlatformData = nullptr; |
150 | } |
151 | |
152 | void CachedFont::checkNotify() |
153 | { |
154 | if (isLoading()) |
155 | return; |
156 | |
157 | CachedResourceClientWalker<CachedFontClient> walker(m_clients); |
158 | while (CachedFontClient* client = walker.next()) |
159 | client->fontLoaded(*this); |
160 | } |
161 | |
162 | bool CachedFont::mayTryReplaceEncodedData() const |
163 | { |
164 | // If a FontCustomPlatformData has ever been constructed to wrap the internal resource buffer then it still might be in use somewhere. |
165 | // That platform font object might directly reference the encoded data buffer behind this CachedFont, |
166 | // so replacing it is unsafe. |
167 | |
168 | return !m_hasCreatedFontDataWrappingResource; |
169 | } |
170 | |
171 | } |
172 | |