OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/font_fallback.h" | 5 #include "ui/gfx/font_fallback.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
13 #import "base/mac/mac_util.h" | 13 #import "base/mac/mac_util.h" |
14 #import "base/strings/sys_string_conversions.h" | 14 #import "base/strings/sys_string_conversions.h" |
15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
16 | 16 |
17 // CTFontCopyDefaultCascadeListForLanguages() doesn't exist in the 10.6 SDK. | 17 // TODO(thakis): Remove this prototype once the deployment target is 10.8+. |
18 // There is only the following. It doesn't exist in the public header files, | 18 extern "C" CFArrayRef CTFontCopyDefaultCascadeListForLanguages( |
19 // but is an exported symbol so should always link. | 19 CTFontRef font, |
20 extern "C" CFArrayRef CTFontCopyDefaultCascadeList(CTFontRef font_ref); | 20 CFArrayRef languagePrefList); |
21 | |
22 namespace { | |
23 | |
24 // Wrapper for CTFontCopyDefaultCascadeListForLanguages() which should appear in | |
25 // CoreText.h from 10.8 onwards. | |
26 // TODO(tapted): Delete this wrapper when only 10.8+ is supported. | |
27 CFArrayRef CTFontCopyDefaultCascadeListForLanguagesWrapper( | |
28 CTFontRef font_ref, | |
29 CFArrayRef language_pref_list) { | |
30 typedef CFArrayRef (*MountainLionPrototype)(CTFontRef, CFArrayRef); | |
31 static const MountainLionPrototype cascade_with_languages_function = | |
32 reinterpret_cast<MountainLionPrototype>( | |
33 dlsym(RTLD_DEFAULT, "CTFontCopyDefaultCascadeListForLanguages")); | |
34 if (cascade_with_languages_function) | |
35 return cascade_with_languages_function(font_ref, language_pref_list); | |
36 | |
37 // Fallback to the 10.6 Private API. | |
38 DCHECK(base::mac::IsOSLionOrEarlier()); | |
39 return CTFontCopyDefaultCascadeList(font_ref); | |
40 } | |
41 | |
42 } // namespace | |
43 | 21 |
44 namespace gfx { | 22 namespace gfx { |
45 | 23 |
46 std::vector<Font> GetFallbackFonts(const Font& font) { | 24 std::vector<Font> GetFallbackFonts(const Font& font) { |
47 // On Mac "There is a system default cascade list (which is polymorphic, based | 25 // On Mac "There is a system default cascade list (which is polymorphic, based |
48 // on the user's language setting and current font)" - CoreText Programming | 26 // on the user's language setting and current font)" - CoreText Programming |
49 // Guide. | 27 // Guide. |
50 // The CoreText APIs provide CTFontCreateForString(font, string, range), but | 28 // The CoreText APIs provide CTFontCreateForString(font, string, range), but |
51 // it requires a text string "hint", and the returned font can't be | 29 // it requires a text string "hint", and the returned font can't be |
52 // represented by name for easy retrieval later. | 30 // represented by name for easy retrieval later. |
53 // In 10.8, CTFontCopyDefaultCascadeListForLanguages(font, language_list) | 31 // In 10.8, CTFontCopyDefaultCascadeListForLanguages(font, language_list) |
54 // showed up which is a good fit GetFallbackFonts(). | 32 // showed up which is a good fit GetFallbackFonts(). |
55 NSArray* languages = [[NSUserDefaults standardUserDefaults] | 33 NSArray* languages = [[NSUserDefaults standardUserDefaults] |
56 stringArrayForKey:@"AppleLanguages"]; | 34 stringArrayForKey:@"AppleLanguages"]; |
57 CFArrayRef languages_cf = base::mac::NSToCFCast(languages); | 35 CFArrayRef languages_cf = base::mac::NSToCFCast(languages); |
58 base::ScopedCFTypeRef<CFArrayRef> cascade_list( | 36 base::ScopedCFTypeRef<CFArrayRef> cascade_list( |
59 CTFontCopyDefaultCascadeListForLanguagesWrapper( | 37 CTFontCopyDefaultCascadeListForLanguages( |
60 static_cast<CTFontRef>(font.GetNativeFont()), languages_cf)); | 38 static_cast<CTFontRef>(font.GetNativeFont()), languages_cf)); |
61 | 39 |
62 std::vector<Font> fallback_fonts; | 40 std::vector<Font> fallback_fonts; |
63 | 41 |
64 const CFIndex fallback_count = CFArrayGetCount(cascade_list); | 42 const CFIndex fallback_count = CFArrayGetCount(cascade_list); |
65 for (CFIndex i = 0; i < fallback_count; ++i) { | 43 for (CFIndex i = 0; i < fallback_count; ++i) { |
66 CTFontDescriptorRef descriptor = | 44 CTFontDescriptorRef descriptor = |
67 base::mac::CFCastStrict<CTFontDescriptorRef>( | 45 base::mac::CFCastStrict<CTFontDescriptorRef>( |
68 CFArrayGetValueAtIndex(cascade_list, i)); | 46 CFArrayGetValueAtIndex(cascade_list, i)); |
69 base::ScopedCFTypeRef<CTFontRef> font( | 47 base::ScopedCFTypeRef<CTFontRef> font( |
70 CTFontCreateWithFontDescriptor(descriptor, 0.0, nullptr)); | 48 CTFontCreateWithFontDescriptor(descriptor, 0.0, nullptr)); |
71 if (font.get()) | 49 if (font.get()) |
72 fallback_fonts.push_back(Font(static_cast<NSFont*>(font.get()))); | 50 fallback_fonts.push_back(Font(static_cast<NSFont*>(font.get()))); |
73 } | 51 } |
74 | 52 |
75 if (fallback_fonts.empty()) | 53 if (fallback_fonts.empty()) |
76 return std::vector<Font>(1, font); | 54 return std::vector<Font>(1, font); |
77 | 55 |
78 return fallback_fonts; | 56 return fallback_fonts; |
79 } | 57 } |
80 | 58 |
81 } // namespace gfx | 59 } // namespace gfx |
OLD | NEW |