1 | /* |
2 | * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2007-2008 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 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
15 | * its contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #pragma once |
31 | |
32 | #include "FontDescription.h" |
33 | #include "FontPlatformData.h" |
34 | #include "FontTaggedSettings.h" |
35 | #include "Timer.h" |
36 | #include <array> |
37 | #include <limits.h> |
38 | #include <wtf/Forward.h> |
39 | #include <wtf/ListHashSet.h> |
40 | #include <wtf/RefPtr.h> |
41 | #include <wtf/Vector.h> |
42 | #include <wtf/WorkQueue.h> |
43 | #include <wtf/text/AtomicStringHash.h> |
44 | #include <wtf/text/WTFString.h> |
45 | |
46 | #if PLATFORM(COCOA) |
47 | #include "FontCacheCoreText.h" |
48 | #endif |
49 | |
50 | #if OS(WINDOWS) |
51 | #include <windows.h> |
52 | #include <objidl.h> |
53 | #include <mlang.h> |
54 | #endif |
55 | |
56 | namespace WebCore { |
57 | |
58 | class FontCascade; |
59 | class FontPlatformData; |
60 | class FontSelector; |
61 | class OpenTypeVerticalData; |
62 | class Font; |
63 | enum class IsForPlatformFont : uint8_t; |
64 | |
65 | #if PLATFORM(WIN) |
66 | #if USE(IMLANG_FONT_LINK2) |
67 | typedef IMLangFontLink2 IMLangFontLinkType; |
68 | #else |
69 | typedef IMLangFontLink IMLangFontLinkType; |
70 | #endif |
71 | #endif |
72 | |
73 | // This key contains the FontDescription fields other than family that matter when fetching FontDatas (platform fonts). |
74 | struct FontDescriptionKey { |
75 | FontDescriptionKey() = default; |
76 | |
77 | FontDescriptionKey(const FontDescription& description) |
78 | : m_size(description.computedPixelSize()) |
79 | , m_fontSelectionRequest(description.fontSelectionRequest()) |
80 | , m_flags(makeFlagsKey(description)) |
81 | , m_locale(description.locale()) |
82 | , m_featureSettings(description.featureSettings()) |
83 | #if ENABLE(VARIATION_FONTS) |
84 | , m_variationSettings(description.variationSettings()) |
85 | #endif |
86 | { } |
87 | |
88 | explicit FontDescriptionKey(WTF::HashTableDeletedValueType) |
89 | : m_size(cHashTableDeletedSize) |
90 | { } |
91 | |
92 | bool operator==(const FontDescriptionKey& other) const |
93 | { |
94 | return m_size == other.m_size |
95 | && m_fontSelectionRequest == other.m_fontSelectionRequest |
96 | && m_flags == other.m_flags |
97 | && m_locale == other.m_locale |
98 | #if ENABLE(VARIATION_FONTS) |
99 | && m_variationSettings == other.m_variationSettings |
100 | #endif |
101 | && m_featureSettings == other.m_featureSettings; |
102 | } |
103 | |
104 | bool operator!=(const FontDescriptionKey& other) const |
105 | { |
106 | return !(*this == other); |
107 | } |
108 | |
109 | bool isHashTableDeletedValue() const { return m_size == cHashTableDeletedSize; } |
110 | |
111 | inline unsigned computeHash() const |
112 | { |
113 | IntegerHasher hasher; |
114 | hasher.add(m_size); |
115 | hasher.add(m_fontSelectionRequest.weight); |
116 | hasher.add(m_fontSelectionRequest.width); |
117 | hasher.add(m_fontSelectionRequest.slope.valueOr(normalItalicValue())); |
118 | hasher.add(m_locale.existingHash()); |
119 | for (unsigned flagItem : m_flags) |
120 | hasher.add(flagItem); |
121 | hasher.add(m_featureSettings.hash()); |
122 | #if ENABLE(VARIATION_FONTS) |
123 | hasher.add(m_variationSettings.hash()); |
124 | #endif |
125 | return hasher.hash(); |
126 | } |
127 | |
128 | private: |
129 | static std::array<unsigned, 2> makeFlagsKey(const FontDescription& description) |
130 | { |
131 | unsigned first = static_cast<unsigned>(description.script()) << 14 |
132 | | static_cast<unsigned>(description.shouldAllowUserInstalledFonts()) << 13 |
133 | | static_cast<unsigned>(description.fontStyleAxis() == FontStyleAxis::slnt) << 12 |
134 | | static_cast<unsigned>(description.opticalSizing()) << 11 |
135 | | static_cast<unsigned>(description.textRenderingMode()) << 9 |
136 | | static_cast<unsigned>(description.fontSynthesis()) << 6 |
137 | | static_cast<unsigned>(description.widthVariant()) << 4 |
138 | | static_cast<unsigned>(description.nonCJKGlyphOrientation()) << 3 |
139 | | static_cast<unsigned>(description.orientation()) << 2 |
140 | | static_cast<unsigned>(description.renderingMode()); |
141 | unsigned second = static_cast<unsigned>(description.variantEastAsianRuby()) << 27 |
142 | | static_cast<unsigned>(description.variantEastAsianWidth()) << 25 |
143 | | static_cast<unsigned>(description.variantEastAsianVariant()) << 22 |
144 | | static_cast<unsigned>(description.variantAlternates()) << 21 |
145 | | static_cast<unsigned>(description.variantNumericSlashedZero()) << 20 |
146 | | static_cast<unsigned>(description.variantNumericOrdinal()) << 19 |
147 | | static_cast<unsigned>(description.variantNumericFraction()) << 17 |
148 | | static_cast<unsigned>(description.variantNumericSpacing()) << 15 |
149 | | static_cast<unsigned>(description.variantNumericFigure()) << 13 |
150 | | static_cast<unsigned>(description.variantCaps()) << 10 |
151 | | static_cast<unsigned>(description.variantPosition()) << 8 |
152 | | static_cast<unsigned>(description.variantContextualAlternates()) << 6 |
153 | | static_cast<unsigned>(description.variantHistoricalLigatures()) << 4 |
154 | | static_cast<unsigned>(description.variantDiscretionaryLigatures()) << 2 |
155 | | static_cast<unsigned>(description.variantCommonLigatures()); |
156 | return {{ first, second }}; |
157 | } |
158 | |
159 | static const unsigned cHashTableDeletedSize = 0xFFFFFFFFU; |
160 | |
161 | // FontCascade::locale() is explicitly not included in this struct. |
162 | unsigned m_size { 0 }; |
163 | FontSelectionRequest m_fontSelectionRequest; |
164 | std::array<unsigned, 2> m_flags {{ 0, 0 }}; |
165 | AtomicString m_locale; |
166 | FontFeatureSettings m_featureSettings; |
167 | #if ENABLE(VARIATION_FONTS) |
168 | FontVariationSettings m_variationSettings; |
169 | #endif |
170 | }; |
171 | |
172 | struct FontDescriptionKeyHash { |
173 | static unsigned hash(const FontDescriptionKey& key) |
174 | { |
175 | return key.computeHash(); |
176 | } |
177 | |
178 | static bool equal(const FontDescriptionKey& a, const FontDescriptionKey& b) |
179 | { |
180 | return a == b; |
181 | } |
182 | |
183 | static const bool safeToCompareToEmptyOrDeleted = true; |
184 | }; |
185 | |
186 | class FontCache { |
187 | friend class WTF::NeverDestroyed<FontCache>; |
188 | |
189 | WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED; |
190 | public: |
191 | WEBCORE_EXPORT static FontCache& singleton(); |
192 | |
193 | // These methods are implemented by the platform. |
194 | enum class PreferColoredFont : uint8_t { No, Yes }; |
195 | RefPtr<Font> systemFallbackForCharacters(const FontDescription&, const Font* originalFontData, IsForPlatformFont, PreferColoredFont, const UChar* characters, unsigned length); |
196 | Vector<String> systemFontFamilies(); |
197 | void platformInit(); |
198 | |
199 | #if PLATFORM(COCOA) |
200 | WEBCORE_EXPORT static void setFontWhitelist(const Vector<String>&); |
201 | #endif |
202 | #if PLATFORM(WIN) |
203 | IMLangFontLinkType* getFontLinkInterface(); |
204 | static void comInitialize(); |
205 | static void comUninitialize(); |
206 | static IMultiLanguage* getMultiLanguageInterface(); |
207 | #endif |
208 | |
209 | // This function exists so CSSFontSelector can have a unified notion of preinstalled fonts and @font-face. |
210 | // It comes into play when you create an @font-face which shares a family name as a preinstalled font. |
211 | Vector<FontSelectionCapabilities> getFontSelectionCapabilitiesInFamily(const AtomicString&, AllowUserInstalledFonts); |
212 | |
213 | WEBCORE_EXPORT RefPtr<Font> fontForFamily(const FontDescription&, const AtomicString&, const FontFeatureSettings* fontFaceFeatures = nullptr, const FontVariantSettings* fontFaceVariantSettings = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false); |
214 | WEBCORE_EXPORT Ref<Font> lastResortFallbackFont(const FontDescription&); |
215 | WEBCORE_EXPORT Ref<Font> fontForPlatformData(const FontPlatformData&); |
216 | RefPtr<Font> similarFont(const FontDescription&, const AtomicString& family); |
217 | |
218 | void addClient(FontSelector&); |
219 | void removeClient(FontSelector&); |
220 | |
221 | unsigned short generation(); |
222 | WEBCORE_EXPORT void invalidate(); |
223 | |
224 | WEBCORE_EXPORT size_t fontCount(); |
225 | WEBCORE_EXPORT size_t inactiveFontCount(); |
226 | WEBCORE_EXPORT void purgeInactiveFontData(unsigned count = UINT_MAX); |
227 | void platformPurgeInactiveFontData(); |
228 | |
229 | #if PLATFORM(WIN) |
230 | RefPtr<Font> fontFromDescriptionAndLogFont(const FontDescription&, const LOGFONT&, AtomicString& outFontFamilyName); |
231 | #endif |
232 | |
233 | #if ENABLE(OPENTYPE_VERTICAL) |
234 | RefPtr<OpenTypeVerticalData> verticalData(const FontPlatformData&); |
235 | #endif |
236 | |
237 | std::unique_ptr<FontPlatformData> createFontPlatformDataForTesting(const FontDescription&, const AtomicString& family); |
238 | |
239 | bool shouldMockBoldSystemFontForAccessibility() const { return m_shouldMockBoldSystemFontForAccessibility; } |
240 | void setShouldMockBoldSystemFontForAccessibility(bool shouldMockBoldSystemFontForAccessibility) { m_shouldMockBoldSystemFontForAccessibility = shouldMockBoldSystemFontForAccessibility; } |
241 | |
242 | struct PrewarmInformation { |
243 | Vector<String> seenFamilies; |
244 | Vector<String> fontNamesRequiringSystemFallback; |
245 | |
246 | bool isEmpty() const; |
247 | PrewarmInformation isolatedCopy() const; |
248 | |
249 | template<class Encoder> void encode(Encoder&) const; |
250 | template<class Decoder> static Optional<PrewarmInformation> decode(Decoder&); |
251 | }; |
252 | PrewarmInformation collectPrewarmInformation() const; |
253 | void prewarm(const PrewarmInformation&); |
254 | |
255 | private: |
256 | FontCache(); |
257 | ~FontCache() = delete; |
258 | |
259 | WEBCORE_EXPORT void purgeInactiveFontDataIfNeeded(); |
260 | |
261 | // FIXME: This method should eventually be removed. |
262 | FontPlatformData* getCachedFontPlatformData(const FontDescription&, const AtomicString& family, const FontFeatureSettings* fontFaceFeatures = nullptr, const FontVariantSettings* fontFaceVariantSettings = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false); |
263 | |
264 | // These methods are implemented by each platform. |
265 | #if PLATFORM(COCOA) |
266 | FontPlatformData* getCustomFallbackFont(const UInt32, const FontDescription&); |
267 | #endif |
268 | WEBCORE_EXPORT std::unique_ptr<FontPlatformData> createFontPlatformData(const FontDescription&, const AtomicString& family, const FontFeatureSettings* fontFaceFeatures, const FontVariantSettings* fontFaceVariantSettings, FontSelectionSpecifiedCapabilities fontFaceCapabilities); |
269 | |
270 | static const AtomicString& alternateFamilyName(const AtomicString&); |
271 | static const AtomicString& platformAlternateFamilyName(const AtomicString&); |
272 | |
273 | Timer m_purgeTimer; |
274 | |
275 | bool m_shouldMockBoldSystemFontForAccessibility { false }; |
276 | |
277 | #if PLATFORM(COCOA) |
278 | ListHashSet<String> m_seenFamiliesForPrewarming; |
279 | ListHashSet<String> m_fontNamesRequiringSystemFallbackForPrewarming; |
280 | RefPtr<WorkQueue> m_prewarmQueue; |
281 | |
282 | friend class ComplexTextController; |
283 | #endif |
284 | friend class Font; |
285 | }; |
286 | |
287 | inline std::unique_ptr<FontPlatformData> FontCache::createFontPlatformDataForTesting(const FontDescription& fontDescription, const AtomicString& family) |
288 | { |
289 | return createFontPlatformData(fontDescription, family, nullptr, nullptr, { }); |
290 | } |
291 | |
292 | #if !PLATFORM(COCOA) |
293 | |
294 | inline void FontCache::platformPurgeInactiveFontData() |
295 | { |
296 | } |
297 | |
298 | #endif |
299 | |
300 | |
301 | inline bool FontCache::PrewarmInformation::isEmpty() const |
302 | { |
303 | return seenFamilies.isEmpty() && fontNamesRequiringSystemFallback.isEmpty(); |
304 | } |
305 | |
306 | inline FontCache::PrewarmInformation FontCache::PrewarmInformation::isolatedCopy() const |
307 | { |
308 | return { seenFamilies.isolatedCopy(), fontNamesRequiringSystemFallback.isolatedCopy() }; |
309 | } |
310 | |
311 | template<class Encoder> |
312 | void FontCache::PrewarmInformation::encode(Encoder& encoder) const |
313 | { |
314 | encoder << seenFamilies; |
315 | encoder << fontNamesRequiringSystemFallback; |
316 | } |
317 | |
318 | template<class Decoder> |
319 | Optional<FontCache::PrewarmInformation> FontCache::PrewarmInformation::decode(Decoder& decoder) |
320 | { |
321 | PrewarmInformation prewarmInformation; |
322 | if (!decoder.decode(prewarmInformation.seenFamilies)) |
323 | return { }; |
324 | if (!decoder.decode(prewarmInformation.fontNamesRequiringSystemFallback)) |
325 | return { }; |
326 | |
327 | return prewarmInformation; |
328 | } |
329 | |
330 | } |
331 | |