Move locale information into FontDescription
[WebKit-https.git] / Source / WebCore / platform / graphics / FontCache.h
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 #ifndef FontCache_h
31 #define FontCache_h
32
33 #include "FontDescription.h"
34 #include "Timer.h"
35 #include <array>
36 #include <limits.h>
37 #include <wtf/Forward.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/Vector.h>
41 #include <wtf/text/AtomicStringHash.h>
42 #include <wtf/text/WTFString.h>
43
44 #if PLATFORM(COCOA)
45 #include <CoreText/CTFont.h>
46 #endif
47
48 #if OS(WINDOWS)
49 #include <windows.h>
50 #include <objidl.h>
51 #include <mlang.h>
52 #endif
53
54 namespace WebCore {
55
56 class FontCascade;
57 class FontPlatformData;
58 class FontSelector;
59 class OpenTypeVerticalData;
60 class Font;
61
62 #if PLATFORM(WIN)
63 #if USE(IMLANG_FONT_LINK2)
64 typedef IMLangFontLink2 IMLangFontLinkType;
65 #else
66 typedef IMLangFontLink IMLangFontLinkType;
67 #endif
68 #endif
69
70 // This key contains the FontDescription fields other than family that matter when fetching FontDatas (platform fonts).
71 struct FontDescriptionKey {
72     FontDescriptionKey() = default;
73
74     FontDescriptionKey(const FontDescription& description)
75         : m_size(description.computedPixelSize())
76         , m_weight(description.weight())
77         , m_flags(makeFlagsKey(description))
78         , m_featureSettings(description.featureSettings())
79     { }
80
81     explicit FontDescriptionKey(WTF::HashTableDeletedValueType)
82         : m_size(cHashTableDeletedSize)
83     { }
84
85     bool operator==(const FontDescriptionKey& other) const
86     {
87         return m_size == other.m_size && m_weight == other.m_weight && m_flags == other.m_flags
88             && m_featureSettings == other.m_featureSettings;
89     }
90
91     bool operator!=(const FontDescriptionKey& other) const
92     {
93         return !(*this == other);
94     }
95
96     bool isHashTableDeletedValue() const { return m_size == cHashTableDeletedSize; }
97
98     inline unsigned computeHash() const
99     {
100         IntegerHasher hasher;
101         hasher.add(m_size);
102         hasher.add(m_weight);
103         for (unsigned flagItem : m_flags)
104             hasher.add(flagItem);
105         hasher.add(m_featureSettings.hash());
106         return hasher.hash();
107     }
108
109 private:
110     static std::array<unsigned, 2> makeFlagsKey(const FontDescription& description)
111     {
112         static_assert(USCRIPT_CODE_LIMIT < 0x1000, "Script code must fit in an unsigned along with the other flags");
113         unsigned first = static_cast<unsigned>(description.script()) << 11
114             | static_cast<unsigned>(description.textRenderingMode()) << 9
115             | static_cast<unsigned>(description.smallCaps()) << 8
116             | static_cast<unsigned>(description.fontSynthesis()) << 6
117             | static_cast<unsigned>(description.widthVariant()) << 4
118             | static_cast<unsigned>(description.nonCJKGlyphOrientation()) << 3
119             | static_cast<unsigned>(description.orientation()) << 2
120             | static_cast<unsigned>(description.italic()) << 1
121             | static_cast<unsigned>(description.renderingMode());
122         unsigned second = static_cast<unsigned>(description.variantEastAsianRuby()) << 27
123             | static_cast<unsigned>(description.variantEastAsianWidth()) << 25
124             | static_cast<unsigned>(description.variantEastAsianVariant()) << 22
125             | static_cast<unsigned>(description.variantAlternates()) << 21
126             | static_cast<unsigned>(description.variantNumericSlashedZero()) << 20
127             | static_cast<unsigned>(description.variantNumericOrdinal()) << 19
128             | static_cast<unsigned>(description.variantNumericFraction()) << 17
129             | static_cast<unsigned>(description.variantNumericSpacing()) << 15
130             | static_cast<unsigned>(description.variantNumericFigure()) << 13
131             | static_cast<unsigned>(description.variantCaps()) << 10
132             | static_cast<unsigned>(description.variantPosition()) << 8
133             | static_cast<unsigned>(description.variantContextualAlternates()) << 6
134             | static_cast<unsigned>(description.variantHistoricalLigatures()) << 4
135             | static_cast<unsigned>(description.variantDiscretionaryLigatures()) << 2
136             | static_cast<unsigned>(description.variantCommonLigatures());
137         return {{ first, second }};
138     }
139
140     static const unsigned cHashTableDeletedSize = 0xFFFFFFFFU;
141
142     // FontCascade::locale() is explicitly not included in this struct.
143     unsigned m_size { 0 };
144     unsigned m_weight { 0 };
145     std::array<unsigned, 2> m_flags {{ 0, 0 }};
146     FontFeatureSettings m_featureSettings;
147 };
148
149 struct FontDescriptionKeyHash {
150     static unsigned hash(const FontDescriptionKey& key)
151     {
152         return key.computeHash();
153     }
154
155     static bool equal(const FontDescriptionKey& a, const FontDescriptionKey& b)
156     {
157         return a == b;
158     }
159
160     static const bool safeToCompareToEmptyOrDeleted = true;
161 };
162
163 class FontCache {
164     friend class WTF::NeverDestroyed<FontCache>;
165
166     WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED;
167 public:
168     WEBCORE_EXPORT static FontCache& singleton();
169
170     // These methods are implemented by the platform.
171     RefPtr<Font> systemFallbackForCharacters(const FontDescription&, const Font* originalFontData, bool isPlatformFont, const UChar* characters, unsigned length);
172     Vector<String> systemFontFamilies();
173     void platformInit();
174
175 #if PLATFORM(IOS)
176     static float weightOfCTFont(CTFontRef);
177 #endif
178 #if PLATFORM(COCOA)
179     WEBCORE_EXPORT static void setFontWhitelist(const Vector<String>&);
180 #endif
181 #if PLATFORM(WIN)
182     IMLangFontLinkType* getFontLinkInterface();
183     static void comInitialize();
184     static void comUninitialize();
185     static IMultiLanguage* getMultiLanguageInterface();
186 #endif
187
188     // This function exists so CSSFontSelector can have a unified notion of preinstalled fonts and @font-face.
189     // It comes into play when you create an @font-face which shares a family name as a preinstalled font.
190     Vector<FontTraitsMask> getTraitsInFamily(const AtomicString&);
191
192     WEBCORE_EXPORT RefPtr<Font> fontForFamily(const FontDescription&, const AtomicString&, bool checkingAlternateName = false);
193     WEBCORE_EXPORT Ref<Font> lastResortFallbackFont(const FontDescription&);
194     WEBCORE_EXPORT Ref<Font> fontForPlatformData(const FontPlatformData&);
195     RefPtr<Font> similarFont(const FontDescription&, const AtomicString& family);
196
197     void addClient(FontSelector*);
198     void removeClient(FontSelector*);
199
200     unsigned short generation();
201     WEBCORE_EXPORT void invalidate();
202
203     WEBCORE_EXPORT size_t fontCount();
204     WEBCORE_EXPORT size_t inactiveFontCount();
205     WEBCORE_EXPORT void purgeInactiveFontData(unsigned count = UINT_MAX);
206     void platformPurgeInactiveFontData();
207
208 #if PLATFORM(WIN)
209     RefPtr<Font> fontFromDescriptionAndLogFont(const FontDescription&, const LOGFONT&, AtomicString& outFontFamilyName);
210 #endif
211
212 #if ENABLE(OPENTYPE_VERTICAL)
213     typedef AtomicString FontFileKey;
214     PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&);
215 #endif
216
217 private:
218     FontCache();
219     ~FontCache() = delete;
220
221     WEBCORE_EXPORT void purgeInactiveFontDataIfNeeded();
222
223     // FIXME: This method should eventually be removed.
224     FontPlatformData* getCachedFontPlatformData(const FontDescription&, const AtomicString& family, bool checkingAlternateName = false);
225
226     // These methods are implemented by each platform.
227 #if PLATFORM(COCOA)
228     FontPlatformData* getCustomFallbackFont(const UInt32, const FontDescription&);
229 #endif
230     std::unique_ptr<FontPlatformData> createFontPlatformData(const FontDescription&, const AtomicString& family);
231
232     Timer m_purgeTimer;
233
234 #if PLATFORM(COCOA)
235     friend class ComplexTextController;
236 #endif
237     friend class Font;
238 };
239
240 #if PLATFORM(COCOA)
241
242 struct SynthesisPair {
243     SynthesisPair(bool needsSyntheticBold, bool needsSyntheticOblique)
244         : needsSyntheticBold(needsSyntheticBold)
245         , needsSyntheticOblique(needsSyntheticOblique)
246     {
247     }
248
249     std::pair<bool, bool> boldObliquePair() const
250     {
251         return std::make_pair(needsSyntheticBold, needsSyntheticOblique);
252     }
253
254     bool needsSyntheticBold;
255     bool needsSyntheticOblique;
256 };
257
258 RetainPtr<CTFontRef> preparePlatformFont(CTFontRef, TextRenderingMode, const FontFeatureSettings* fontFaceFeatures, const FontVariantSettings* fontFaceVariantSettings, const FontFeatureSettings& features, const FontVariantSettings&);
259 FontWeight fontWeightFromCoreText(CGFloat weight);
260 uint16_t toCoreTextFontWeight(FontWeight);
261 bool isFontWeightBold(FontWeight);
262 void platformInvalidateFontCache();
263 SynthesisPair computeNecessarySynthesis(CTFontRef, const FontDescription&, bool isPlatformFont = false);
264 RetainPtr<CTFontRef> platformFontWithFamilySpecialCase(const AtomicString& family, FontWeight, CTFontSymbolicTraits, float size);
265 RetainPtr<CTFontRef> platformFontWithFamily(const AtomicString& family, CTFontSymbolicTraits, FontWeight, TextRenderingMode, float size);
266 RetainPtr<CTFontRef> platformLookupFallbackFont(CTFontRef, FontWeight, const AtomicString& locale, const UChar* characters, unsigned length);
267 bool requiresCustomFallbackFont(UChar32 character);
268
269 #else
270
271 inline void FontCache::platformPurgeInactiveFontData()
272 {
273 }
274
275 #endif
276
277 }
278
279 #endif