76571c8b665d934b94a7dc0791568554a056aabc
[WebKit.git] / Source / WebCore / platform / graphics / mac / FontCacheMac.mm
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
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 #import "config.h"
31 #import "FontCache.h"
32
33 #import "CoreGraphicsSPI.h"
34 #import "CoreTextSPI.h"
35 #import "Font.h"
36 #import "FontCascade.h"
37 #import "FontPlatformData.h"
38
39 #if PLATFORM(MAC)
40 #import "NSFontSPI.h"
41 #import "WebCoreNSStringExtras.h"
42 #import "WebCoreSystemInterface.h"
43 #import <AppKit/AppKit.h>
44 #import <wtf/MainThread.h>
45 #import <wtf/NeverDestroyed.h>
46 #import <wtf/StdLibExtras.h>
47 #import <wtf/Threading.h>
48 #import <wtf/text/AtomicStringHash.h>
49 #endif
50
51 namespace WebCore {
52
53 #if PLATFORM(MAC)
54
55 static CGFloat toNSFontWeight(FontWeight fontWeight)
56 {
57     static const CGFloat nsFontWeights[] = {
58         NSFontWeightUltraLight,
59         NSFontWeightThin,
60         NSFontWeightLight,
61         NSFontWeightRegular,
62         NSFontWeightMedium,
63         NSFontWeightSemibold,
64         NSFontWeightBold,
65         NSFontWeightHeavy,
66         NSFontWeightBlack
67     };
68     ASSERT(fontWeight >= 0 && fontWeight <= 8);
69     return nsFontWeights[fontWeight];
70 }
71
72 RetainPtr<CTFontRef> platformFontWithFamilySpecialCase(const AtomicString& family, FontWeight weight, CTFontSymbolicTraits desiredTraits, float size)
73 {
74     if (equalLettersIgnoringASCIICase(family, "-webkit-system-font") || equalLettersIgnoringASCIICase(family, "-apple-system") || equalLettersIgnoringASCIICase(family, "-apple-system-font") || equalLettersIgnoringASCIICase(family, "system-ui")) {
75         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size weight:toNSFontWeight(weight)]);
76         if (desiredTraits & kCTFontItalicTrait) {
77             if (auto italicizedFont = adoptCF(CTFontCreateCopyWithSymbolicTraits(result.get(), size, nullptr, desiredTraits, desiredTraits)))
78                 result = italicizedFont;
79         }
80         return result;
81     }
82
83     if (equalLettersIgnoringASCIICase(family, "-apple-system-monospaced-numbers")) {
84         int numberSpacingType = kNumberSpacingType;
85         int monospacedNumbersSelector = kMonospacedNumbersSelector;
86         RetainPtr<CFNumberRef> numberSpacingNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &numberSpacingType));
87         RetainPtr<CFNumberRef> monospacedNumbersNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &monospacedNumbersSelector));
88         CFTypeRef featureKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
89         CFTypeRef featureValues[] = { numberSpacingNumber.get(), monospacedNumbersNumber.get() };
90         RetainPtr<CFDictionaryRef> featureIdentifier = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, featureKeys, featureValues, WTF_ARRAY_LENGTH(featureKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
91         CFTypeRef featureIdentifiers[] = { featureIdentifier.get() };
92         RetainPtr<CFArrayRef> featureArray = adoptCF(CFArrayCreate(kCFAllocatorDefault, featureIdentifiers, 1, &kCFTypeArrayCallBacks));
93         CFTypeRef attributesKeys[] = { kCTFontFeatureSettingsAttribute };
94         CFTypeRef attributesValues[] = { featureArray.get() };
95         RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, attributesKeys, attributesValues, WTF_ARRAY_LENGTH(attributesKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
96
97         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size]);
98         return adoptCF(CTFontCreateCopyWithAttributes(result.get(), size, nullptr, adoptCF(CTFontDescriptorCreateWithAttributes(attributes.get())).get()));
99     }
100
101     if (equalLettersIgnoringASCIICase(family, "-apple-menu"))
102         return toCTFont([NSFont menuFontOfSize:size]);
103
104     if (equalLettersIgnoringASCIICase(family, "-apple-status-bar"))
105         return toCTFont([NSFont labelFontOfSize:size]);
106
107     return nullptr;
108 }
109
110 Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
111 {
112     // FIXME: Would be even better to somehow get the user's default font here.  For now we'll pick
113     // the default that the user would get without changing any prefs.
114     if (RefPtr<Font> font = fontForFamily(fontDescription, AtomicString("Times", AtomicString::ConstructFromLiteral)))
115         return *font;
116
117     // The Times fallback will almost always work, but in the highly unusual case where
118     // the user doesn't have it, we fall back on Lucida Grande because that's
119     // guaranteed to be there, according to Nathan Taylor. This is good enough
120     // to avoid a crash at least.
121     return *fontForFamily(fontDescription, AtomicString("Lucida Grande", AtomicString::ConstructFromLiteral), nullptr, nullptr, false);
122 }
123
124 #endif // PLATFORM(MAC)
125
126 Ref<Font> FontCache::lastResortFallbackFontForEveryCharacter(const FontDescription& fontDescription)
127 {
128     auto result = fontForFamily(fontDescription, AtomicString("LastResort", AtomicString::ConstructFromLiteral));
129     ASSERT(result);
130     return *result;
131 }
132
133 } // namespace WebCore