[Cocoa] Expand system-ui to include every item in the Core Text cascade list
[WebKit-https.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 #import "SoftLinking.h"
52
53 namespace WebCore {
54
55 #if PLATFORM(MAC)
56
57 #if !USE_PLATFORM_SYSTEM_FALLBACK_LIST
58 static CGFloat toNSFontWeight(FontSelectionValue fontWeight)
59 {
60     if (fontWeight < FontSelectionValue(150))
61         return NSFontWeightUltraLight;
62     if (fontWeight < FontSelectionValue(250))
63         return NSFontWeightThin;
64     if (fontWeight < FontSelectionValue(350))
65         return NSFontWeightLight;
66     if (fontWeight < FontSelectionValue(450))
67         return NSFontWeightRegular;
68     if (fontWeight < FontSelectionValue(550))
69         return NSFontWeightMedium;
70     if (fontWeight < FontSelectionValue(650))
71         return NSFontWeightSemibold;
72     if (fontWeight < FontSelectionValue(750))
73         return NSFontWeightBold;
74     if (fontWeight < FontSelectionValue(850))
75         return NSFontWeightHeavy;
76     return NSFontWeightBlack;
77 }
78 #endif
79
80 RetainPtr<CTFontRef> platformFontWithFamilySpecialCase(const AtomicString& family, FontSelectionRequest request, float size)
81 {
82     if (equalLettersIgnoringASCIICase(family, "-webkit-system-font") || equalLettersIgnoringASCIICase(family, "-apple-system") || equalLettersIgnoringASCIICase(family, "-apple-system-font") || equalLettersIgnoringASCIICase(family, "system-ui")) {
83 #if !USE_PLATFORM_SYSTEM_FALLBACK_LIST
84         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size weight:toNSFontWeight(request.weight)]);
85         if (isItalic(request.slope)) {
86             CTFontSymbolicTraits desiredTraits = kCTFontItalicTrait;
87             if (isFontWeightBold(request.weight))
88                 desiredTraits |= kCTFontBoldTrait;
89             if (auto italicizedFont = adoptCF(CTFontCreateCopyWithSymbolicTraits(result.get(), size, nullptr, desiredTraits, desiredTraits)))
90                 result = italicizedFont;
91         }
92         return result;
93 #else
94         UNUSED_PARAM(request);
95         ASSERT_NOT_REACHED();
96 #endif
97     }
98
99     if (equalLettersIgnoringASCIICase(family, "-apple-system-monospaced-numbers")) {
100         int numberSpacingType = kNumberSpacingType;
101         int monospacedNumbersSelector = kMonospacedNumbersSelector;
102         RetainPtr<CFNumberRef> numberSpacingNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &numberSpacingType));
103         RetainPtr<CFNumberRef> monospacedNumbersNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &monospacedNumbersSelector));
104         CFTypeRef featureKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
105         CFTypeRef featureValues[] = { numberSpacingNumber.get(), monospacedNumbersNumber.get() };
106         RetainPtr<CFDictionaryRef> featureIdentifier = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, featureKeys, featureValues, WTF_ARRAY_LENGTH(featureKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
107         CFTypeRef featureIdentifiers[] = { featureIdentifier.get() };
108         RetainPtr<CFArrayRef> featureArray = adoptCF(CFArrayCreate(kCFAllocatorDefault, featureIdentifiers, 1, &kCFTypeArrayCallBacks));
109         CFTypeRef attributesKeys[] = { kCTFontFeatureSettingsAttribute };
110         CFTypeRef attributesValues[] = { featureArray.get() };
111         RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, attributesKeys, attributesValues, WTF_ARRAY_LENGTH(attributesKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
112
113         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size]);
114         return adoptCF(CTFontCreateCopyWithAttributes(result.get(), size, nullptr, adoptCF(CTFontDescriptorCreateWithAttributes(attributes.get())).get()));
115     }
116
117     if (equalLettersIgnoringASCIICase(family, "-apple-menu"))
118         return toCTFont([NSFont menuFontOfSize:size]);
119
120     if (equalLettersIgnoringASCIICase(family, "-apple-status-bar"))
121         return toCTFont([NSFont labelFontOfSize:size]);
122
123     if (equalLettersIgnoringASCIICase(family, "lastresort")) {
124 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
125         static NeverDestroyed<RetainPtr<CTFontDescriptorRef>> lastResort = adoptCF(CTFontDescriptorCreateLastResort());
126         return adoptCF(CTFontCreateWithFontDescriptor(lastResort.get().get(), size, nullptr));
127 #else
128         // LastResort is special, so it's important to look this exact string up, and not some case-folded version.
129         // We handle this here so any caching and case folding we do in our general text codepath is bypassed.
130         return adoptCF(CTFontCreateWithName(CFSTR("LastResort"), size, nullptr));
131 #endif
132     }
133
134     return nullptr;
135 }
136
137 Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
138 {
139     // FIXME: Would be even better to somehow get the user's default font here.  For now we'll pick
140     // the default that the user would get without changing any prefs.
141     if (RefPtr<Font> font = fontForFamily(fontDescription, AtomicString("Times", AtomicString::ConstructFromLiteral)))
142         return *font;
143
144     // The Times fallback will almost always work, but in the highly unusual case where
145     // the user doesn't have it, we fall back on Lucida Grande because that's
146     // guaranteed to be there, according to Nathan Taylor. This is good enough
147     // to avoid a crash at least.
148     return *fontForFamily(fontDescription, AtomicString("Lucida Grande", AtomicString::ConstructFromLiteral), nullptr, nullptr, { }, false);
149 }
150
151 #endif // PLATFORM(MAC)
152
153 } // namespace WebCore