Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: ui/gfx/font_fallback_mac.mm

Issue 971673002: Mac: Implement GetFallbackFontFamilies for Harfbuzz (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@20150129-MacViews-Bringup5-RTHB-in-Label
Patch Set: Move to font_fallback_mac_unittest Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/font_fallback_mac.cc ('k') | ui/gfx/font_fallback_mac_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/font_fallback.h"
6
7 #import <Foundation/Foundation.h>
8 #include <dlfcn.h>
9 #include <string>
10 #include <vector>
11
12 #import "base/strings/sys_string_conversions.h"
13 #include "base/mac/foundation_util.h"
14 #import "base/mac/mac_util.h"
15
16 // CTFontCopyDefaultCascadeListForLanguages() doesn't exist in the 10.6 SDK.
17 // There is only the following. It doesn't exist in the public header files,
18 // but is an exported symbol so should always link.
19 extern "C" CFArrayRef CTFontCopyDefaultCascadeList(CTFontRef font_ref);
20
21 namespace {
22
23 // Wrapper for CTFontCopyDefaultCascadeListForLanguages() which should appear in
24 // CoreText.h from 10.8 onwards.
25 // TODO(tapted): Delete this wrapper when only 10.8+ is supported.
26 CFArrayRef CTFontCopyDefaultCascadeListForLanguagesWrapper(
27 CTFontRef font_ref,
28 CFArrayRef language_pref_list) {
29 typedef CFArrayRef (*MountainLionPrototype)(CTFontRef, CFArrayRef);
30 static const MountainLionPrototype cascade_with_languages_function =
31 reinterpret_cast<MountainLionPrototype>(
32 dlsym(RTLD_DEFAULT, "CTFontCopyDefaultCascadeListForLanguages"));
33 if (cascade_with_languages_function)
34 return cascade_with_languages_function(font_ref, language_pref_list);
35
36 // Fallback to the 10.6 Private API.
37 DCHECK(base::mac::IsOSLionOrEarlier());
38 return CTFontCopyDefaultCascadeList(font_ref);
39 }
40
41 } // namespace
42
43 namespace gfx {
44
45 std::vector<std::string> GetFallbackFontFamilies(
46 const std::string& font_family) {
47 // 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
49 // Guide.
50 // The CoreText APIs provide CTFontCreateForString(font, string, range), but
51 // it requires a text string "hint", and the returned font can't be
52 // represented by name for easy retrieval later.
53 // In 10.8, CTFontCopyDefaultCascadeListForLanguages(font, language_list)
54 // showed up which is a good fit GetFallbackFontFamilies().
55
56 // Size doesn't matter for querying the cascade list.
57 const CGFloat font_size = 10.0;
58 base::ScopedCFTypeRef<CFStringRef> cfname(
59 base::SysUTF8ToCFStringRef(font_family));
60
61 // Using CTFontCreateWithName here works, but CoreText emits stderr spam along
62 // the lines of `CTFontCreateWithName() using name "Arial" and got font with
63 // PostScript name "ArialMT".` Instead, create a descriptor.
64 const void* attribute_keys[] = {kCTFontFamilyNameAttribute};
65 const void* attribute_values[] = {cfname.get()};
66 base::ScopedCFTypeRef<CFDictionaryRef> attributes(CFDictionaryCreate(
67 kCFAllocatorDefault, attribute_keys, attribute_values, 1,
68 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
69 base::ScopedCFTypeRef<CTFontDescriptorRef> descriptor(
70 CTFontDescriptorCreateWithAttributes(attributes));
71 base::ScopedCFTypeRef<CTFontRef> base_font(
72 CTFontCreateWithFontDescriptor(descriptor, font_size, nullptr));
73
74 if (!base_font)
75 return std::vector<std::string>(1, font_family);
76
77 NSArray* languages = [[NSUserDefaults standardUserDefaults]
78 stringArrayForKey:@"AppleLanguages"];
79 CFArrayRef languages_cf = base::mac::NSToCFCast(languages);
80 base::ScopedCFTypeRef<CFArrayRef> cascade_list(
81 CTFontCopyDefaultCascadeListForLanguagesWrapper(base_font, languages_cf));
82
83 std::vector<std::string> fallback_fonts;
84
85 const CFIndex fallback_count = CFArrayGetCount(cascade_list);
86 for (CFIndex i = 0; i < fallback_count; ++i) {
87 CTFontDescriptorRef descriptor =
88 base::mac::CFCastStrict<CTFontDescriptorRef>(
89 CFArrayGetValueAtIndex(cascade_list, i));
90 base::ScopedCFTypeRef<CFStringRef> font_name(
91 base::mac::CFCastStrict<CFStringRef>(CTFontDescriptorCopyAttribute(
92 descriptor, kCTFontFamilyNameAttribute)));
93 fallback_fonts.push_back(base::SysCFStringRefToUTF8(font_name));
94 }
95
96 if (fallback_fonts.empty())
97 return std::vector<std::string>(1, font_family);
98
99 return fallback_fonts;
100 }
101
102 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_fallback_mac.cc ('k') | ui/gfx/font_fallback_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698