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

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: Feature Detection 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/gfx.gyp » ('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 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
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, 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.
28 typedef CFArrayRef (*MountainLionPrototype)(CTFontRef, CFArrayRef);
29 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`
30 reinterpret_cast<MountainLionPrototype>(
31 dlsym(RTLD_DEFAULT, "CTFontCopyDefaultCascadeListForLanguages"));
32 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.
33 if (prototype)
34 return prototype(font_ref, language_pref_list);
35
36 // Fallback to the 10.6 Private API.
37 return CTFontCopyDefaultCascadeList(font_ref);
38 }
39
40 } // namespace
41
42 namespace gfx {
43
44 std::vector<std::string> GetFallbackFontFamilies(
45 const std::string& font_family) {
46 // On Mac "There is a system default cascade list (which is polymorphic, based
47 // on the user's language setting and current font)" - CoreText Programming
48 // Guide.
49 // The CoreText APIs provide CTFontCreateForString(font, string, range), but
50 // it requires a text string "hint", and the returned font can't be
51 // represented by name for easy retrieval later.
52 // In 10.8, CTFontCopyDefaultCascadeListForLanguages(font, language_list)
53 // showed up which is a good fit GetFallbackFontFamilies().
54
55 // Size doesn't matter for querying the cascade list.
56 const CGFloat font_size = 10.0;
57 base::ScopedCFTypeRef<CFStringRef> cfname(
58 base::SysUTF8ToCFStringRef(font_family));
59
60 // Using CTFontCreateWithName here works, but CoreText emits stderr spam along
61 // the lines of `CTFontCreateWithName() using name "Arial" and got font with
62 // PostScript name "ArialMT".` Instead, create a descriptor.
63 const void* attribute_keys[] = {kCTFontFamilyNameAttribute};
64 const void* attribute_values[] = {cfname.get()};
65 base::ScopedCFTypeRef<CFDictionaryRef> attributes(CFDictionaryCreate(
66 kCFAllocatorDefault, attribute_keys, attribute_values, 1,
67 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
68 base::ScopedCFTypeRef<CTFontDescriptorRef> descriptor(
69 CTFontDescriptorCreateWithAttributes(attributes));
70 base::ScopedCFTypeRef<CTFontRef> base_font(
71 CTFontCreateWithFontDescriptor(descriptor, font_size, nullptr));
72
73 if (!base_font)
74 return std::vector<std::string>(1, font_family);
75
76 NSArray* languages = [[NSUserDefaults standardUserDefaults]
77 stringArrayForKey:@"AppleLanguages"];
78 CFArrayRef languages_cf = base::mac::NSToCFCast(languages);
79 base::ScopedCFTypeRef<CFArrayRef> cascade_list(
80 CTFontCopyDefaultCascadeListForLanguagesWrapper(base_font, languages_cf));
81
82 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.
83 std::vector<std::string> fallback_fonts;
84
85 for (CFIndex i = 0; i < fallback_count; ++i) {
86 CTFontDescriptorRef descriptor =
87 base::mac::CFCastStrict<CTFontDescriptorRef>(
88 CFArrayGetValueAtIndex(cascade_list, i));
89 base::ScopedCFTypeRef<CFStringRef> font_name(
90 base::mac::CFCastStrict<CFStringRef>(CTFontDescriptorCopyAttribute(
91 descriptor, kCTFontFamilyNameAttribute)));
92 fallback_fonts.push_back(base::SysCFStringRefToUTF8(font_name));
93 }
94
95 if (fallback_fonts.empty())
96 return std::vector<std::string>(1, font_family);
97
98 return fallback_fonts;
99 }
100
101 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_fallback_mac.cc ('k') | ui/gfx/gfx.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698