OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/font_loader_mac.h" | 5 #include "chrome/common/font_loader_mac.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/mac_util.h" | 13 #include "base/mac_util.h" |
14 #include "base/scoped_cftyperef.h" | 14 #include "base/scoped_cftyperef.h" |
15 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
16 | 16 |
17 // static | 17 // static |
18 bool FontLoader::LoadFontIntoBuffer(const string16& font_name, | 18 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, |
19 float font_point_size, | |
20 base::SharedMemory* font_data, | 19 base::SharedMemory* font_data, |
21 uint32* font_data_size) { | 20 uint32* font_data_size) { |
22 CHECK(font_data && font_data_size); | 21 CHECK(font_data && font_data_size); |
23 *font_data_size = 0; | 22 *font_data_size = 0; |
24 | 23 |
24 // Used only for logging. | |
25 std::string font_name([[font_to_encode fontName] UTF8String]); | |
26 | |
25 // Load appropriate NSFont. | 27 // Load appropriate NSFont. |
26 NSString* font_name_ns = base::SysUTF16ToNSString(font_name); | |
27 NSFont* font_to_encode = | |
28 [NSFont fontWithName:font_name_ns size:font_point_size]; | |
29 if (!font_to_encode) { | 28 if (!font_to_encode) { |
30 LOG(ERROR) << "Failed to load font " << font_name; | 29 LOG(ERROR) << "Failed to load font " << font_name; |
31 return false; | 30 return false; |
32 } | 31 } |
33 | 32 |
34 // NSFont -> ATSFontRef. | 33 // NSFont -> ATSFontRef. |
35 ATSFontRef ats_font = | 34 ATSFontRef ats_font = |
36 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); | 35 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); |
37 if (!ats_font) { | 36 if (!ats_font) { |
38 LOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; | 37 LOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 if (amt_read != font_file_size_32) { | 82 if (amt_read != font_file_size_32) { |
84 LOG(ERROR) << "Failed to read font data for " << font_path.value(); | 83 LOG(ERROR) << "Failed to read font data for " << font_path.value(); |
85 return false; | 84 return false; |
86 } | 85 } |
87 | 86 |
88 *font_data_size = font_file_size_32; | 87 *font_data_size = font_file_size_32; |
89 return true; | 88 return true; |
90 } | 89 } |
91 | 90 |
92 // static | 91 // static |
93 bool FontLoader::CreateCGFontFromBuffer(base::SharedMemoryHandle font_data, | 92 bool FontLoader::ATSFontContainerFromBuffer(base::SharedMemoryHandle font_data, |
94 uint32 font_data_size, | 93 uint32 font_data_size, |
95 CGFontRef *font) { | 94 ATSFontContainerRef* font_container) |
95 { | |
96 CHECK(font_container); | |
97 | |
96 using base::SharedMemory; | 98 using base::SharedMemory; |
97 DCHECK(SharedMemory::IsHandleValid(font_data)); | 99 DCHECK(SharedMemory::IsHandleValid(font_data)); |
98 DCHECK_GT(font_data_size, 0U); | 100 DCHECK_GT(font_data_size, 0U); |
99 | 101 |
100 SharedMemory shm(font_data, true); | 102 SharedMemory shm(font_data, true); |
101 if (!shm.Map(font_data_size)) | 103 if (!shm.Map(font_data_size)) |
102 return false; | 104 return false; |
103 | 105 |
104 ATSFontContainerRef font_container = 0; | 106 // A value of 3 means the font is private and can't be seen by anyone else. |
107 // This is the value used by WebKit when activating remote fonts. | |
Avi (use Gerrit)
2010/06/15 13:57:48
Nice comment; I like it.
| |
108 const ATSFontContext kFontContextPrivate = 3; | |
105 OSStatus err = ATSFontActivateFromMemory(shm.memory(), font_data_size, | 109 OSStatus err = ATSFontActivateFromMemory(shm.memory(), font_data_size, |
106 kATSFontContextLocal, kATSFontFormatUnspecified, | 110 kFontContextPrivate, kATSFontFormatUnspecified, NULL, |
107 NULL, kATSOptionFlagsDefault, &font_container ); | 111 kATSOptionFlagsDefault, font_container); |
108 if (err != noErr || !font_container) | 112 if (err != noErr || !font_container) |
109 return false; | 113 return false; |
110 | 114 |
111 // Count the number of fonts that were loaded. | |
112 ItemCount fontCount = 0; | |
113 err = ATSFontFindFromContainer(font_container, kATSOptionFlagsDefault, 0, | |
114 NULL, &fontCount); | |
115 | |
116 if (err != noErr || fontCount < 1) { | |
117 ATSFontDeactivate(font_container, NULL, kATSOptionFlagsDefault); | |
118 return false; | |
119 } | |
120 | |
121 // Load font from container. | |
122 ATSFontRef font_ref_ats = 0; | |
123 ATSFontFindFromContainer(font_container, kATSOptionFlagsDefault, 1, | |
124 &font_ref_ats, NULL); | |
125 | |
126 if (!font_ref_ats) { | |
127 ATSFontDeactivate(font_container, NULL, kATSOptionFlagsDefault); | |
128 return false; | |
129 } | |
130 | |
131 // Convert to cgFont. | |
132 CGFontRef font_ref_cg = CGFontCreateWithPlatformFont(&font_ref_ats); | |
133 | |
134 if (!font_ref_cg) { | |
135 ATSFontDeactivate(font_container, NULL, kATSOptionFlagsDefault); | |
136 return false; | |
137 } | |
138 | |
139 *font = font_ref_cg; | |
140 return true; | 115 return true; |
141 } | 116 } |
OLD | NEW |