2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #import "CoreGraphicsSPI.h"
33 #import "CoreTextSPI.h"
35 #import "RenderThemeIOS.h"
36 #import <wtf/NeverDestroyed.h>
37 #import <wtf/RetainPtr.h>
38 #import <wtf/text/CString.h>
42 void FontCache::platformInit()
46 static inline bool isFontWeightBold(NSInteger fontWeight)
48 return fontWeight >= FontWeight600;
51 static inline bool requiresCustomFallbackFont(const UInt32 character)
53 return character == AppleLogo || character == blackCircle || character == narrowNonBreakingSpace;
56 static CFCharacterSetRef copyFontCharacterSet(CFStringRef fontName)
58 // The size, 10, is arbitrary.
59 RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithNameAndSize(fontName, 10));
60 RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), 10, nullptr));
61 return (CFCharacterSetRef)CTFontDescriptorCopyAttribute(fontDescriptor.get(), kCTFontCharacterSetAttribute);
64 static CFCharacterSetRef appleColorEmojiCharacterSet()
66 static CFCharacterSetRef characterSet = copyFontCharacterSet(CFSTR("AppleColorEmoji"));
70 static CFCharacterSetRef phoneFallbackCharacterSet()
72 static CFCharacterSetRef characterSet = copyFontCharacterSet(CFSTR(".PhoneFallback"));
76 PassRefPtr<SimpleFontData> FontCache::getSystemFontFallbackForCharacters(const FontDescription& description, const SimpleFontData* originalFontData, const UChar* characters, int length)
78 const FontPlatformData& platformData = originalFontData->platformData();
79 CTFontRef ctFont = platformData.font();
81 CFIndex coveredLength = 0;
82 RetainPtr<CTFontRef> substituteFont = adoptCF(CTFontCreatePhysicalFontForCharactersWithLanguage(ctFont, (const UTF16Char*)characters, (CFIndex)length, 0, &coveredLength));
86 CTFontSymbolicTraits originalTraits = CTFontGetSymbolicTraits(ctFont);
87 CTFontSymbolicTraits actualTraits = 0;
88 if (isFontWeightBold(description.weight()) || description.italic())
89 actualTraits = CTFontGetSymbolicTraits(substituteFont.get());
91 bool syntheticBold = (originalTraits & kCTFontTraitBold) && !(actualTraits & kCTFontTraitBold);
92 bool syntheticOblique = (originalTraits & kCTFontTraitItalic) && !(actualTraits & kCTFontTraitItalic);
94 FontPlatformData alternateFont(substituteFont.get(), platformData.size(), platformData.isPrinterFont(), syntheticBold, syntheticOblique, platformData.m_orientation);
95 alternateFont.m_isEmoji = CTFontIsAppleColorEmoji(substituteFont.get());
97 return getCachedFontData(&alternateFont, DoNotRetain);
100 enum class LanguageSpecificFont {
111 CanadianAboriginalSyllabic,
116 static LanguageSpecificFont languageSpecificFallbackFont(UChar32 c)
118 static bool isGB18030ComplianceRequired = wkIsGB18030ComplianceRequired();
120 // The following ranges are Korean Hangul and should be rendered by AppleSDGothicNeo
125 // These are Cyrillic and should be rendered by Helvetica Neue
129 return LanguageSpecificFont::None;
131 return LanguageSpecificFont::Cyrillic;
133 return LanguageSpecificFont::None;
135 return LanguageSpecificFont::Hebrew;
137 return LanguageSpecificFont::Arabic;
139 return LanguageSpecificFont::None;
141 return LanguageSpecificFont::Indic;
143 return LanguageSpecificFont::Thai;
145 return LanguageSpecificFont::Lao;
147 return LanguageSpecificFont::Tibetan;
149 return LanguageSpecificFont::None;
151 return LanguageSpecificFont::Korean;
153 return LanguageSpecificFont::None;
155 return LanguageSpecificFont::CanadianAboriginalSyllabic;
157 return LanguageSpecificFont::Khmer;
159 return LanguageSpecificFont::None;
161 return LanguageSpecificFont::ChineseJapanese;
163 return LanguageSpecificFont::Korean;
165 return LanguageSpecificFont::ChineseJapanese;
167 return LanguageSpecificFont::Korean;
169 return LanguageSpecificFont::ChineseJapanese;
171 return isGB18030ComplianceRequired ? LanguageSpecificFont::ChineseJapanese : LanguageSpecificFont::Emoji;
172 if (c < 0xE865 && isGB18030ComplianceRequired)
173 return LanguageSpecificFont::ChineseJapanese;
175 return LanguageSpecificFont::None;
177 return LanguageSpecificFont::ChineseJapanese;
179 return LanguageSpecificFont::None;
181 return LanguageSpecificFont::Arabic;
183 return LanguageSpecificFont::None;
185 return LanguageSpecificFont::ChineseJapanese;
187 return LanguageSpecificFont::Arabic;
189 return LanguageSpecificFont::ChineseJapanese;
191 return LanguageSpecificFont::None;
193 return LanguageSpecificFont::ChineseJapanese;
194 return LanguageSpecificFont::None;
197 PassRefPtr<SimpleFontData> FontCache::systemFallbackForCharacters(const FontDescription& description, const SimpleFontData* originalFontData, bool, const UChar* characters, int length)
199 // Unlike OS X, our fallback font on iPhone is Arial Unicode, which doesn't have some apple-specific glyphs like F8FF.
200 // Fall back to the Apple Fallback font in this case.
201 if (length > 0 && requiresCustomFallbackFont(*characters))
202 return getCachedFontData(getCustomFallbackFont(*characters, description), DoNotRetain);
204 UChar32 c = *characters;
205 if (length > 1 && U16_IS_LEAD(c) && U16_IS_TRAIL(characters[1]))
206 c = U16_GET_SUPPLEMENTARY(c, characters[1]);
208 // For system fonts we use CoreText fallback mechanism.
210 RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontCopyFontDescriptor(originalFontData->getCTFont()));
211 if (CTFontDescriptorIsSystemUIFont(fontDescriptor.get()))
212 return getSystemFontFallbackForCharacters(description, originalFontData, characters, length);
215 LanguageSpecificFont languageSpecificFont = LanguageSpecificFont::None;
217 languageSpecificFont = languageSpecificFallbackFont(c);
219 RefPtr<SimpleFontData> simpleFontData;
221 switch (languageSpecificFont) {
222 case LanguageSpecificFont::ChineseJapanese: {
223 // By default, Chinese font is preferred, fall back on Japanese.
225 enum CJKFontVariant {
226 kCJKFontUseHiragino = 0,
227 kCJKFontUseSTHeitiSC,
228 kCJKFontUseSTHeitiTC,
234 static NeverDestroyed<AtomicString> plainHiragino("HiraKakuProN-W3", AtomicString::ConstructFromLiteral);
235 static NeverDestroyed<AtomicString> plainSTHeitiSC("STHeitiSC-Light", AtomicString::ConstructFromLiteral);
236 static NeverDestroyed<AtomicString> plainSTHeitiTC("STHeitiTC-Light", AtomicString::ConstructFromLiteral);
237 static NeverDestroyed<AtomicString> plainSTHeitiJ("STHeitiJ-Light", AtomicString::ConstructFromLiteral);
238 static NeverDestroyed<AtomicString> plainSTHeitiK("STHeitiK-Light", AtomicString::ConstructFromLiteral);
239 static NeverDestroyed<AtomicString> plainHKGPW3UI("HKGPW3UI", AtomicString::ConstructFromLiteral);
240 static AtomicString* cjkPlain[] = {
241 &plainHiragino.get(),
242 &plainSTHeitiSC.get(),
243 &plainSTHeitiTC.get(),
244 &plainSTHeitiJ.get(),
245 &plainSTHeitiK.get(),
246 &plainHKGPW3UI.get(),
249 static NeverDestroyed<AtomicString> boldHiragino("HiraKakuProN-W6", AtomicString::ConstructFromLiteral);
250 static NeverDestroyed<AtomicString> boldSTHeitiSC("STHeitiSC-Medium", AtomicString::ConstructFromLiteral);
251 static NeverDestroyed<AtomicString> boldSTHeitiTC("STHeitiTC-Medium", AtomicString::ConstructFromLiteral);
252 static NeverDestroyed<AtomicString> boldSTHeitiJ("STHeitiJ-Medium", AtomicString::ConstructFromLiteral);
253 static NeverDestroyed<AtomicString> boldSTHeitiK("STHeitiK-Medium", AtomicString::ConstructFromLiteral);
254 static NeverDestroyed<AtomicString> boldHKGPW3UI("HKGPW3UI", AtomicString::ConstructFromLiteral);
255 static AtomicString* cjkBold[] = {
257 &boldSTHeitiSC.get(),
258 &boldSTHeitiTC.get(),
264 // Default below is for Simplified Chinese user: zh-Hans - note that Hiragino is the
265 // the secondary font as we want its for Hiragana and Katakana. The other CJK fonts
266 // do not, and should not, contain Hiragana or Katakana glyphs.
267 static CJKFontVariant preferredCJKFont = kCJKFontUseSTHeitiSC;
268 static CJKFontVariant secondaryCJKFont = kCJKFontsUseHKGPW3UI;
270 static bool CJKFontInitialized;
271 if (!CJKFontInitialized) {
272 CJKFontInitialized = true;
273 // Testing: languageName = (CFStringRef)@"ja";
274 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
275 NSArray *languages = [defaults stringArrayForKey:@"AppleLanguages"];
278 for (NSString *language in languages) {
279 RetainPtr<CFStringRef> languageName = adoptCF(CFLocaleCreateCanonicalLanguageIdentifierFromString(nullptr, (CFStringRef)language));
280 if (CFEqual(languageName.get(), CFSTR("zh-Hans")))
281 break; // Simplified Chinese - default settings
282 else if (CFEqual(languageName.get(), CFSTR("ja"))) {
283 preferredCJKFont = kCJKFontUseHiragino; // Japanese - prefer Hiragino and STHeiti Japanse Variant
284 secondaryCJKFont = kCJKFontUseSTHeitiJ;
286 } else if (CFEqual(languageName.get(), CFSTR("ko"))) {
287 preferredCJKFont = kCJKFontUseSTHeitiK; // Korean - prefer STHeiti Korean Variant
289 } else if (CFEqual(languageName.get(), CFSTR("zh-Hant"))) {
290 preferredCJKFont = kCJKFontUseSTHeitiTC; // Traditional Chinese - prefer STHeiti Traditional Variant
297 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? *cjkBold[preferredCJKFont] : *cjkPlain[preferredCJKFont], false, DoNotRetain);
298 bool useSecondaryFont = true;
299 if (simpleFontData) {
301 // CGFontGetGlyphsForUnichars takes UTF-16 buffer. Should only be 1 codepoint but since we may pass in two UTF-16 characters,
302 // make room for 2 glyphs just to be safe.
303 CGFontGetGlyphsForUnichars(simpleFontData->platformData().cgFont(), characters, glyphs, length);
305 useSecondaryFont = (glyphs[0] == 0);
308 if (useSecondaryFont)
309 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? *cjkBold[secondaryCJKFont] : *cjkPlain[secondaryCJKFont], false, DoNotRetain);
312 case LanguageSpecificFont::Korean: {
313 static NeverDestroyed<AtomicString> koreanPlain("AppleSDGothicNeo-Medium", AtomicString::ConstructFromLiteral);
314 static NeverDestroyed<AtomicString> koreanBold("AppleSDGothicNeo-Bold", AtomicString::ConstructFromLiteral);
315 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? koreanBold : koreanPlain, false, DoNotRetain);
318 case LanguageSpecificFont::Cyrillic: {
319 static NeverDestroyed<AtomicString> cyrillicPlain("HelveticaNeue", AtomicString::ConstructFromLiteral);
320 static NeverDestroyed<AtomicString> cyrillicBold("HelveticaNeue-Bold", AtomicString::ConstructFromLiteral);
321 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? cyrillicBold : cyrillicPlain, false, DoNotRetain);
324 case LanguageSpecificFont::Arabic: {
325 static NeverDestroyed<AtomicString> arabicPlain("GeezaPro", AtomicString::ConstructFromLiteral);
326 static NeverDestroyed<AtomicString> arabicBold("GeezaPro-Bold", AtomicString::ConstructFromLiteral);
327 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? arabicBold : arabicPlain, false, DoNotRetain);
330 case LanguageSpecificFont::Hebrew: {
331 static NeverDestroyed<AtomicString> hebrewPlain("ArialHebrew", AtomicString::ConstructFromLiteral);
332 static NeverDestroyed<AtomicString> hebrewBold("ArialHebrew-Bold", AtomicString::ConstructFromLiteral);
333 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? hebrewBold : hebrewPlain, false, DoNotRetain);
336 case LanguageSpecificFont::Indic: {
337 static NeverDestroyed<AtomicString> devanagariFont("KohinoorDevanagari-Book", AtomicString::ConstructFromLiteral);
338 static NeverDestroyed<AtomicString> bengaliFont("BanglaSangamMN", AtomicString::ConstructFromLiteral);
339 static NeverDestroyed<AtomicString> gurmukhiFont("GurmukhiMN", AtomicString::ConstructFromLiteral); // Might be replaced in a future release with a Sangam version.
340 static NeverDestroyed<AtomicString> gujaratiFont("GujaratiSangamMN", AtomicString::ConstructFromLiteral);
341 static NeverDestroyed<AtomicString> oriyaFont("OriyaSangamMN", AtomicString::ConstructFromLiteral);
342 static NeverDestroyed<AtomicString> tamilFont("TamilSangamMN", AtomicString::ConstructFromLiteral);
343 static NeverDestroyed<AtomicString> teluguFont("TeluguSangamMN", AtomicString::ConstructFromLiteral);
344 static NeverDestroyed<AtomicString> kannadaFont("KannadaSangamMN", AtomicString::ConstructFromLiteral);
345 static NeverDestroyed<AtomicString> malayalamFont("MalayalamSangamMN", AtomicString::ConstructFromLiteral);
346 static NeverDestroyed<AtomicString> sinhalaFont("SinhalaSangamMN", AtomicString::ConstructFromLiteral);
348 static NeverDestroyed<AtomicString> devanagariFontBold("KohinoorDevanagari-Medium", AtomicString::ConstructFromLiteral);
349 static NeverDestroyed<AtomicString> bengaliFontBold("BanglaSangamMN-Bold", AtomicString::ConstructFromLiteral);
350 static NeverDestroyed<AtomicString> gurmukhiFontBold("GurmukhiMN-Bold", AtomicString::ConstructFromLiteral); // Might be replaced in a future release with a Sangam version.
351 static NeverDestroyed<AtomicString> gujaratiFontBold("GujaratiSangamMN-Bold", AtomicString::ConstructFromLiteral);
352 static NeverDestroyed<AtomicString> oriyaFontBold("OriyaSangamMN-Bold", AtomicString::ConstructFromLiteral);
353 static NeverDestroyed<AtomicString> tamilFontBold("TamilSangamMN-Bold", AtomicString::ConstructFromLiteral);
354 static NeverDestroyed<AtomicString> teluguFontBold("TeluguSangamMN-Bold", AtomicString::ConstructFromLiteral);
355 static NeverDestroyed<AtomicString> kannadaFontBold("KannadaSangamMN-Bold", AtomicString::ConstructFromLiteral);
356 static NeverDestroyed<AtomicString> malayalamFontBold("MalayalamSangamMN-Bold", AtomicString::ConstructFromLiteral);
357 static NeverDestroyed<AtomicString> sinhalaFontBold("SinhalaSangamMN-Bold", AtomicString::ConstructFromLiteral);
359 static AtomicString* indicUnicodePageFonts[] = {
360 &devanagariFont.get(),
368 &malayalamFont.get(),
372 static AtomicString* indicUnicodePageFontsBold[] = {
373 &devanagariFontBold.get(),
374 &bengaliFontBold.get(),
375 &gurmukhiFontBold.get(),
376 &gujaratiFontBold.get(),
377 &oriyaFontBold.get(),
378 &tamilFontBold.get(),
379 &teluguFontBold.get(),
380 &kannadaFontBold.get(),
381 &malayalamFontBold.get(),
382 &sinhalaFontBold.get()
385 uint32_t indicPageOrderIndex = (c - 0x0900) / 0x0080; // Indic scripts start at 0x0900 in Unicode. Each script is allocalted a block of 0x80 characters.
386 if (indicPageOrderIndex < (sizeof(indicUnicodePageFonts) / sizeof(AtomicString*))) {
387 AtomicString* indicFontString = isFontWeightBold(description.weight()) ? indicUnicodePageFontsBold[indicPageOrderIndex] : indicUnicodePageFonts[indicPageOrderIndex];
389 simpleFontData = getCachedFontData(description, *indicFontString, false, DoNotRetain);
393 case LanguageSpecificFont::Thai: {
394 static NeverDestroyed<AtomicString> thaiPlain("Thonburi", AtomicString::ConstructFromLiteral);
395 static NeverDestroyed<AtomicString> thaiBold("Thonburi-Bold", AtomicString::ConstructFromLiteral);
396 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? thaiBold : thaiPlain, false, DoNotRetain);
399 case LanguageSpecificFont::Tibetan: {
400 static NeverDestroyed<AtomicString> tibetanPlain("Kailasa", AtomicString::ConstructFromLiteral);
401 static NeverDestroyed<AtomicString> tibetanBold("Kailasa-Bold", AtomicString::ConstructFromLiteral);
402 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? tibetanBold : tibetanPlain, false, DoNotRetain);
405 case LanguageSpecificFont::CanadianAboriginalSyllabic: {
406 static NeverDestroyed<AtomicString> casPlain("EuphemiaUCAS", AtomicString::ConstructFromLiteral);
407 static NeverDestroyed<AtomicString> casBold("EuphemiaUCAS-Bold", AtomicString::ConstructFromLiteral);
408 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? casBold : casPlain, false, DoNotRetain);
411 case LanguageSpecificFont::Khmer: {
412 static NeverDestroyed<AtomicString> khmer("KhmerSangamMN", AtomicString::ConstructFromLiteral);
413 simpleFontData = getCachedFontData(description, khmer, false, DoNotRetain);
416 case LanguageSpecificFont::Lao: {
417 static NeverDestroyed<AtomicString> lao("LaoSangamMN", AtomicString::ConstructFromLiteral);
418 simpleFontData = getCachedFontData(description, lao, false, DoNotRetain);
422 static NeverDestroyed<AtomicString> appleColorEmoji("AppleColorEmoji", AtomicString::ConstructFromLiteral);
423 bool useEmojiFont = languageSpecificFont == LanguageSpecificFont::Emoji;
425 if (!CFCharacterSetIsLongCharacterMember(phoneFallbackCharacterSet(), c))
426 useEmojiFont = CFCharacterSetIsLongCharacterMember(appleColorEmojiCharacterSet(), c);
429 simpleFontData = getCachedFontData(description, appleColorEmoji, false, DoNotRetain);
431 RetainPtr<CTFontRef> fallbackFont = adoptCF(CTFontCreateForCharacters(originalFontData->getCTFont(), characters, length, nullptr));
432 if (RetainPtr<CFStringRef> foundFontName = adoptCF(CTFontCopyPostScriptName(fallbackFont.get())))
433 simpleFontData = getCachedFontData(description, foundFontName.get(), false, DoNotRetain);
440 return simpleFontData.release();
442 return getNonRetainedLastResortFallbackFont(description);
445 PassRefPtr<SimpleFontData> FontCache::similarFontPlatformData(const FontDescription& description)
447 // Attempt to find an appropriate font using a match based on the presence of keywords in
448 // the requested names. For example, we'll match any name that contains "Arabic" to Geeza Pro.
449 RefPtr<SimpleFontData> simpleFontData;
450 for (unsigned i = 0; i < description.familyCount(); ++i) {
451 const AtomicString& family = description.familyAt(i);
452 if (family.isEmpty())
455 // Substitute the default monospace font for well-known monospace fonts.
456 static NeverDestroyed<AtomicString> monaco("monaco", AtomicString::ConstructFromLiteral);
457 static NeverDestroyed<AtomicString> menlo("menlo", AtomicString::ConstructFromLiteral);
458 static NeverDestroyed<AtomicString> courier("courier", AtomicString::ConstructFromLiteral);
459 if (equalIgnoringCase(family, monaco) || equalIgnoringCase(family, menlo)) {
460 simpleFontData = getCachedFontData(description, courier);
464 // Substitute Verdana for Lucida Grande.
465 static NeverDestroyed<AtomicString> lucidaGrande("lucida grande", AtomicString::ConstructFromLiteral);
466 static NeverDestroyed<AtomicString> verdana("verdana", AtomicString::ConstructFromLiteral);
467 if (equalIgnoringCase(family, lucidaGrande)) {
468 simpleFontData = getCachedFontData(description, verdana);
472 static NeverDestroyed<String> arabic(ASCIILiteral("Arabic"));
473 static NeverDestroyed<String> pashto(ASCIILiteral("Pashto"));
474 static NeverDestroyed<String> urdu(ASCIILiteral("Urdu"));
475 static String* matchWords[3] = { &arabic.get(), &pashto.get(), &urdu.get() };
476 static NeverDestroyed<AtomicString> geezaPlain("GeezaPro", AtomicString::ConstructFromLiteral);
477 static NeverDestroyed<AtomicString> geezaBold("GeezaPro-Bold", AtomicString::ConstructFromLiteral);
478 for (int j = 0; j < 3 && !simpleFontData; ++j)
479 if (family.contains(*matchWords[j], false))
480 simpleFontData = getCachedFontData(description, isFontWeightBold(description.weight()) ? geezaBold : geezaPlain);
483 return simpleFontData.release();
486 PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& fontDescription, ShouldRetain shouldRetain)
488 static NeverDestroyed<AtomicString> fallbackFontFamily(".PhoneFallback", AtomicString::ConstructFromLiteral);
489 return getCachedFontData(fontDescription, fallbackFontFamily, false, shouldRetain);
492 FontPlatformData* FontCache::getCustomFallbackFont(const UInt32 c, const FontDescription& description)
494 ASSERT(requiresCustomFallbackFont(c));
496 static NeverDestroyed<AtomicString> helveticaFamily("Helvetica Neue", AtomicString::ConstructFromLiteral);
497 static NeverDestroyed<AtomicString> lockClockFamily("LockClock-Light", AtomicString::ConstructFromLiteral);
498 static NeverDestroyed<AtomicString> timesNewRomanPSMTFamily("TimesNewRomanPSMT", AtomicString::ConstructFromLiteral);
500 AtomicString* family = nullptr;
503 family = &helveticaFamily.get();
506 family = &lockClockFamily.get();
508 case narrowNonBreakingSpace:
509 family = ×NewRomanPSMTFamily.get();
512 ASSERT_NOT_REACHED();
518 return getCachedFontPlatformData(description, *family);
521 static inline FontTraitsMask toTraitsMask(CTFontSymbolicTraits ctFontTraits)
523 return static_cast<FontTraitsMask>(((ctFontTraits & kCTFontTraitItalic) ? FontStyleItalicMask : FontStyleNormalMask)
524 | FontVariantNormalMask
525 // FontWeight600 or higher is bold for CTFonts, so choose middle values for
526 // bold (600-900) and non-bold (100-500)
527 | ((ctFontTraits & kCTFontTraitBold) ? FontWeight700Mask : FontWeight300Mask));
530 void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
532 RetainPtr<CFStringRef> familyNameStr = familyName.string().createCFString();
533 NSDictionary *attributes = @{ (id)kCTFontFamilyNameAttribute: (NSString*)familyNameStr.get() };
534 RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes));
535 RetainPtr<NSArray> matchedDescriptors = adoptNS((NSArray *)CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor.get(), nullptr));
537 NSInteger numMatches = [matchedDescriptors.get() count];
538 if (!matchedDescriptors || !numMatches)
541 for (NSInteger i = 0; i < numMatches; ++i) {
542 RetainPtr<CFDictionaryRef> traits = adoptCF((CFDictionaryRef)CTFontDescriptorCopyAttribute((CTFontDescriptorRef)[matchedDescriptors.get() objectAtIndex:i], kCTFontTraitsAttribute));
543 CFNumberRef resultRef = (CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait);
545 CTFontSymbolicTraits symbolicTraits;
546 CFNumberGetValue(resultRef, kCFNumberIntType, &symbolicTraits);
547 traitsMasks.append(toTraitsMask(symbolicTraits));
552 float FontCache::weightOfCTFont(CTFontRef font)
555 RetainPtr<CFDictionaryRef> traits = adoptCF(CTFontCopyTraits(font));
559 CFNumberRef resultRef = (CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontWeightTrait);
561 CFNumberGetValue(resultRef, kCFNumberFloatType, &result);
566 static CTFontRef createCTFontWithTextStyle(const String& familyName, CTFontSymbolicTraits traits, CGFloat size)
568 if (familyName.isNull())
571 CTFontSymbolicTraits symbolicTraits = 0;
572 if (traits & kCTFontBoldTrait)
573 symbolicTraits |= kCTFontBoldTrait;
574 if (traits & kCTFontTraitItalic)
575 symbolicTraits |= kCTFontTraitItalic;
576 RetainPtr<CFStringRef> familyNameStr = familyName.createCFString();
577 RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateWithTextStyle(familyNameStr.get(), RenderThemeIOS::contentSizeCategory(), nullptr));
579 fontDescriptor = adoptCF(CTFontDescriptorCreateCopyWithSymbolicTraits(fontDescriptor.get(), symbolicTraits, symbolicTraits));
581 return CTFontCreateWithFontDescriptor(fontDescriptor.get(), size, nullptr);
584 static CTFontRef createCTFontWithFamilyNameAndWeight(const String& familyName, CTFontSymbolicTraits traits, float size, uint16_t weight)
586 if (familyName.isNull())
589 static NeverDestroyed<AtomicString> systemUIFontWithWebKitPrefix("-webkit-system-font", AtomicString::ConstructFromLiteral);
590 static NeverDestroyed<AtomicString> systemUIFontWithApplePrefix("-apple-system-font", AtomicString::ConstructFromLiteral);
591 if (equalIgnoringCase(familyName, systemUIFontWithWebKitPrefix) || equalIgnoringCase(familyName, systemUIFontWithApplePrefix)) {
592 CTFontUIFontType fontType = kCTFontUIFontSystem;
594 // The comment below has been copied from CoreText/UIFoundation. However, in WebKit we synthesize the oblique,
595 // so we should investigate the result <rdar://problem/14449340>:
596 // We don't do bold-italic for system fonts. If you ask for it, we'll assume that you're just kidding and that you really want bold. This is a feature.
597 if (traits & kCTFontTraitBold)
598 fontType = kCTFontUIFontEmphasizedSystem;
599 else if (traits & kCTFontTraitItalic)
600 fontType = static_cast<CTFontUIFontType>(kCTFontUIFontSystemItalic);
601 } else if (weight > 250)
602 fontType = static_cast<CTFontUIFontType>(kCTFontUIFontSystemLight);
603 else if (weight > 150)
604 fontType = static_cast<CTFontUIFontType>(kCTFontUIFontSystemThin);
606 fontType = static_cast<CTFontUIFontType>(kCTFontUIFontSystemUltraLight);
607 RetainPtr<CTFontDescriptorRef> fontDescriptor = adoptCF(CTFontDescriptorCreateForUIType(fontType, size, nullptr));
608 return CTFontCreateWithFontDescriptor(fontDescriptor.get(), size, nullptr);
611 RetainPtr<CFStringRef> familyNameStr = familyName.createCFString();
612 CTFontSymbolicTraits requestedTraits = (CTFontSymbolicTraits)(traits & (kCTFontBoldTrait | kCTFontItalicTrait));
613 return CTFontCreateForCSS(familyNameStr.get(), weight, requestedTraits, size);
616 static uint16_t toCTFontWeight(FontWeight fontWeight)
618 switch (fontWeight) {
642 std::unique_ptr<FontPlatformData> FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
644 // Special case for "Courier" font. We used to have only an oblique variant on iOS, so prior to
645 // iOS 6.0, we disallowed its use here. We'll fall back on "Courier New". <rdar://problem/5116477&10850227>
646 static NeverDestroyed<AtomicString> courier("Courier", AtomicString::ConstructFromLiteral);
647 static bool shouldDisallowCourier = !iosExecutableWasLinkedOnOrAfterVersion(wkIOSSystemVersion_6_0);
648 if (shouldDisallowCourier && equalIgnoringCase(family, courier))
651 CTFontSymbolicTraits traits = 0;
652 if (fontDescription.italic())
653 traits |= kCTFontTraitItalic;
654 if (isFontWeightBold(fontDescription.weight()))
655 traits |= kCTFontTraitBold;
656 float size = fontDescription.computedPixelSize();
658 RetainPtr<CTFontRef> ctFont;
659 if (family.startsWith("UICTFontTextStyle"))
660 ctFont = adoptCF(createCTFontWithTextStyle(family, traits, size));
662 ctFont = adoptCF(createCTFontWithFamilyNameAndWeight(family, traits, size, toCTFontWeight(fontDescription.weight())));
666 CTFontSymbolicTraits actualTraits = 0;
667 if (isFontWeightBold(fontDescription.weight()) || fontDescription.italic())
668 actualTraits = CTFontGetSymbolicTraits(ctFont.get());
670 bool isAppleColorEmoji = CTFontIsAppleColorEmoji(ctFont.get());
672 bool syntheticBold = (traits & kCTFontTraitBold) && !(actualTraits & kCTFontTraitBold) && !isAppleColorEmoji;
673 bool syntheticOblique = (traits & kCTFontTraitItalic) && !(actualTraits & kCTFontTraitItalic) && !isAppleColorEmoji;
675 auto result = std::make_unique<FontPlatformData>(ctFont.get(), size, fontDescription.usePrinterFont(), syntheticBold, syntheticOblique, fontDescription.orientation(), fontDescription.widthVariant());
676 if (isAppleColorEmoji)
677 result->m_isEmoji = true;
681 } // namespace WebCore