Index: ui/gfx/font_fallback_mac.mm |
diff --git a/ui/gfx/font_fallback_mac.mm b/ui/gfx/font_fallback_mac.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..beb8761573b307549fd3625567235788a909d95c |
--- /dev/null |
+++ b/ui/gfx/font_fallback_mac.mm |
@@ -0,0 +1,101 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
Alexei Svitkine (slow)
2015/03/03 23:12:33
2015
tapted
2015/03/03 23:41:14
Done (I guess it's pretty different to font_fallba
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/font_fallback.h" |
+ |
+#import <Foundation/Foundation.h> |
+#include <dlfcn.h> |
+#include <string> |
+#include <vector> |
+ |
+#import "base/strings/sys_string_conversions.h" |
+#include "base/mac/foundation_util.h" |
+#import "base/mac/mac_util.h" |
+ |
+// CTFontCopyDefaultCascadeListForLanguages() doesn't exist in the 10.6 SDK. |
+// There is only the following. It doesn't exist in the public header files, |
+// but is an exported symbol so should always link. |
+extern "C" CFArrayRef CTFontCopyDefaultCascadeList(CTFontRef font_ref); |
+ |
+namespace { |
+ |
+// Wrapper for CTFontCopyDefaultCascadeListForLanguages() which should appear in |
+// CoreText.h from 10.8 onwards. |
+// TODO(tapted): Delete this wrapper when only 10.8+ is supported. |
+CFArrayRef CTFontCopyDefaultCascadeListForLanguagesWrapper( |
+ CTFontRef font_ref, CFArrayRef language_pref_list) { |
Alexei Svitkine (slow)
2015/03/03 23:12:33
Nit: 1 param per line if first param wraps.
tapted
2015/03/03 23:41:14
Done.
|
+ typedef CFArrayRef (*MountainLionPrototype)(CTFontRef, CFArrayRef); |
+ static const MountainLionPrototype prototype = |
Alexei Svitkine (slow)
2015/03/03 23:12:33
Nit: This isn't really a prototype - it's a functi
tapted
2015/03/03 23:41:14
Done. Went with `cascade_with_languages_function`
|
+ reinterpret_cast<MountainLionPrototype>( |
+ dlsym(RTLD_DEFAULT, "CTFontCopyDefaultCascadeListForLanguages")); |
+ DCHECK(prototype || base::mac::IsOSLionOrEarlier()); |
Alexei Svitkine (slow)
2015/03/03 23:12:33
Nit: Can't this just be moved below the if and jus
tapted
2015/03/03 23:41:14
Yep - that has the same effect.
|
+ if (prototype) |
+ return prototype(font_ref, language_pref_list); |
+ |
+ // Fallback to the 10.6 Private API. |
+ return CTFontCopyDefaultCascadeList(font_ref); |
+} |
+ |
+} // namespace |
+ |
+namespace gfx { |
+ |
+std::vector<std::string> GetFallbackFontFamilies( |
+ const std::string& font_family) { |
+ // On Mac "There is a system default cascade list (which is polymorphic, based |
+ // on the user's language setting and current font)" - CoreText Programming |
+ // Guide. |
+ // The CoreText APIs provide CTFontCreateForString(font, string, range), but |
+ // it requires a text string "hint", and the returned font can't be |
+ // represented by name for easy retrieval later. |
+ // In 10.8, CTFontCopyDefaultCascadeListForLanguages(font, language_list) |
+ // showed up which is a good fit GetFallbackFontFamilies(). |
+ |
+ // Size doesn't matter for querying the cascade list. |
+ const CGFloat font_size = 10.0; |
+ base::ScopedCFTypeRef<CFStringRef> cfname( |
+ base::SysUTF8ToCFStringRef(font_family)); |
+ |
+ // Using CTFontCreateWithName here works, but CoreText emits stderr spam along |
+ // the lines of `CTFontCreateWithName() using name "Arial" and got font with |
+ // PostScript name "ArialMT".` Instead, create a descriptor. |
+ const void* attribute_keys[] = {kCTFontFamilyNameAttribute}; |
+ const void* attribute_values[] = {cfname.get()}; |
+ base::ScopedCFTypeRef<CFDictionaryRef> attributes(CFDictionaryCreate( |
+ kCFAllocatorDefault, attribute_keys, attribute_values, 1, |
+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); |
+ base::ScopedCFTypeRef<CTFontDescriptorRef> descriptor( |
+ CTFontDescriptorCreateWithAttributes(attributes)); |
+ base::ScopedCFTypeRef<CTFontRef> base_font( |
+ CTFontCreateWithFontDescriptor(descriptor, font_size, nullptr)); |
+ |
+ if (!base_font) |
+ return std::vector<std::string>(1, font_family); |
+ |
+ NSArray* languages = [[NSUserDefaults standardUserDefaults] |
+ stringArrayForKey:@"AppleLanguages"]; |
+ CFArrayRef languages_cf = base::mac::NSToCFCast(languages); |
+ base::ScopedCFTypeRef<CFArrayRef> cascade_list( |
+ CTFontCopyDefaultCascadeListForLanguagesWrapper(base_font, languages_cf)); |
+ |
+ const CFIndex fallback_count = CFArrayGetCount(cascade_list); |
Alexei Svitkine (slow)
2015/03/03 23:12:33
Nit: Move this to be right above the loop.
tapted
2015/03/03 23:41:14
Done.
|
+ std::vector<std::string> fallback_fonts; |
+ |
+ for (CFIndex i = 0; i < fallback_count; ++i) { |
+ CTFontDescriptorRef descriptor = |
+ base::mac::CFCastStrict<CTFontDescriptorRef>( |
+ CFArrayGetValueAtIndex(cascade_list, i)); |
+ base::ScopedCFTypeRef<CFStringRef> font_name( |
+ base::mac::CFCastStrict<CFStringRef>(CTFontDescriptorCopyAttribute( |
+ descriptor, kCTFontFamilyNameAttribute))); |
+ fallback_fonts.push_back(base::SysCFStringRefToUTF8(font_name)); |
+ } |
+ |
+ if (fallback_fonts.empty()) |
+ return std::vector<std::string>(1, font_family); |
+ |
+ return fallback_fonts; |
+} |
+ |
+} // namespace gfx |