Rename AtomicString to AtomString
[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 "Font.h"
34 #import "FontCascade.h"
35 #import "FontPlatformData.h"
36 #import <pal/spi/cg/CoreGraphicsSPI.h>
37 #import <pal/spi/cocoa/CoreTextSPI.h>
38
39 #if PLATFORM(MAC)
40 #import <AppKit/AppKit.h>
41 #import <pal/spi/mac/NSFontSPI.h>
42 #import <wtf/MainThread.h>
43 #import <wtf/NeverDestroyed.h>
44 #import <wtf/StdLibExtras.h>
45 #import <wtf/Threading.h>
46 #import <wtf/text/AtomStringHash.h>
47 #endif
48
49 #import <wtf/SoftLinking.h>
50
51 namespace WebCore {
52
53 #if PLATFORM(MAC)
54
55 static CGFloat toNSFontWeight(FontSelectionValue fontWeight)
56 {
57     if (fontWeight < FontSelectionValue(150))
58         return NSFontWeightUltraLight;
59     if (fontWeight < FontSelectionValue(250))
60         return NSFontWeightThin;
61     if (fontWeight < FontSelectionValue(350))
62         return NSFontWeightLight;
63     if (fontWeight < FontSelectionValue(450))
64         return NSFontWeightRegular;
65     if (fontWeight < FontSelectionValue(550))
66         return NSFontWeightMedium;
67     if (fontWeight < FontSelectionValue(650))
68         return NSFontWeightSemibold;
69     if (fontWeight < FontSelectionValue(750))
70         return NSFontWeightBold;
71     if (fontWeight < FontSelectionValue(850))
72         return NSFontWeightHeavy;
73     return NSFontWeightBlack;
74 }
75
76 RetainPtr<CTFontRef> platformFontWithFamilySpecialCase(const AtomString& family, FontSelectionRequest request, float size, AllowUserInstalledFonts allowUserInstalledFonts)
77 {
78     // FIXME: See comment in FontCascadeDescription::effectiveFamilyAt() in FontDescriptionCocoa.cpp
79     if (equalLettersIgnoringASCIICase(family, "-webkit-system-font") || equalLettersIgnoringASCIICase(family, "-apple-system") || equalLettersIgnoringASCIICase(family, "-apple-system-font") || equalLettersIgnoringASCIICase(family, "system-ui")) {
80         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size weight:toNSFontWeight(request.weight)]);
81         if (isItalic(request.slope)) {
82             CTFontSymbolicTraits desiredTraits = kCTFontItalicTrait;
83             if (isFontWeightBold(request.weight))
84                 desiredTraits |= kCTFontBoldTrait;
85             if (auto italicizedFont = adoptCF(CTFontCreateCopyWithSymbolicTraits(result.get(), size, nullptr, desiredTraits, desiredTraits)))
86                 result = italicizedFont;
87         }
88         return createFontForInstalledFonts(result.get(), allowUserInstalledFonts);
89     }
90
91     if (equalLettersIgnoringASCIICase(family, "-apple-system-monospaced-numbers")) {
92         int numberSpacingType = kNumberSpacingType;
93         int monospacedNumbersSelector = kMonospacedNumbersSelector;
94         RetainPtr<CFNumberRef> numberSpacingNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &numberSpacingType));
95         RetainPtr<CFNumberRef> monospacedNumbersNumber = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &monospacedNumbersSelector));
96         CFTypeRef featureKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey };
97         CFTypeRef featureValues[] = { numberSpacingNumber.get(), monospacedNumbersNumber.get() };
98         RetainPtr<CFDictionaryRef> featureIdentifier = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, featureKeys, featureValues, WTF_ARRAY_LENGTH(featureKeys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
99         CFTypeRef featureIdentifiers[] = { featureIdentifier.get() };
100         RetainPtr<CFArrayRef> featureArray = adoptCF(CFArrayCreate(kCFAllocatorDefault, featureIdentifiers, 1, &kCFTypeArrayCallBacks));
101         auto attributes = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
102         CFDictionaryAddValue(attributes.get(), kCTFontFeatureSettingsAttribute, featureArray.get());
103         addAttributesForInstalledFonts(attributes.get(), allowUserInstalledFonts);
104         RetainPtr<CTFontRef> result = toCTFont([NSFont systemFontOfSize:size]);
105         auto modification = adoptCF(CTFontDescriptorCreateWithAttributes(attributes.get()));
106         return adoptCF(CTFontCreateCopyWithAttributes(result.get(), size, nullptr, modification.get()));
107     }
108
109     if (equalLettersIgnoringASCIICase(family, "-apple-menu")) {
110         RetainPtr<CTFontRef> result = toCTFont([NSFont menuFontOfSize:size]);
111         return createFontForInstalledFonts(result.get(), allowUserInstalledFonts);
112     }
113
114     if (equalLettersIgnoringASCIICase(family, "-apple-status-bar")) {
115         RetainPtr<CTFontRef> result = toCTFont([NSFont labelFontOfSize:size]);
116         return createFontForInstalledFonts(result.get(), allowUserInstalledFonts);
117     }
118
119     if (equalLettersIgnoringASCIICase(family, "lastresort")) {
120 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
121         static const CTFontDescriptorRef lastResort = CTFontDescriptorCreateLastResort();
122         return adoptCF(CTFontCreateWithFontDescriptor(lastResort, size, nullptr));
123 #else
124         // LastResort is special, so it's important to look this exact string up, and not some case-folded version.
125         // We handle this here so any caching and case folding we do in our general text codepath is bypassed.
126         return adoptCF(CTFontCreateWithName(CFSTR("LastResort"), size, nullptr));
127 #endif
128     }
129
130     return nullptr;
131 }
132
133 #endif // PLATFORM(MAC)
134
135 } // namespace WebCore