OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/mac/font_loader.h" | 5 #include "content/common/mac/font_loader.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/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
14 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
15 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
16 #include "base/sys_string_conversions.h" | 16 #include "base/sys_string_conversions.h" |
17 #include "content/common/mac/font_descriptor.h" | |
17 | 18 |
18 extern "C" { | 19 extern "C" { |
19 | 20 |
20 // Work around http://crbug.com/93191, a really nasty memory smasher bug. | 21 // Work around http://crbug.com/93191, a really nasty memory smasher bug. |
21 // On Mac OS X 10.7 ("Lion"), ATS writes to memory it doesn't own. | 22 // On Mac OS X 10.7 ("Lion"), ATS writes to memory it doesn't own. |
22 // SendDeactivateFontsInContainerMessage, called by ATSFontDeactivate, | 23 // SendDeactivateFontsInContainerMessage, called by ATSFontDeactivate, |
23 // may trash memory whenever dlsym(RTLD_DEFAULT, | 24 // may trash memory whenever dlsym(RTLD_DEFAULT, |
24 // "_CTFontManagerUnregisterFontForData") returns NULL. In that case, it tries | 25 // "_CTFontManagerUnregisterFontForData") returns NULL. In that case, it tries |
25 // to locate that symbol in the CoreText framework, doing some extremely | 26 // to locate that symbol in the CoreText framework, doing some extremely |
26 // sloppy string handling resulting in a likelihood that the string | 27 // sloppy string handling resulting in a likelihood that the string |
(...skipping 18 matching lines...) Expand all Loading... | |
45 // SendDeactivateFontsInContainerMessage on 10.6 ("Snow Leopard") appears to | 46 // SendDeactivateFontsInContainerMessage on 10.6 ("Snow Leopard") appears to |
46 // share this bug but this sort of memory corruption wasn't detected until | 47 // share this bug but this sort of memory corruption wasn't detected until |
47 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem. | 48 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem. |
48 __attribute__((visibility("default"))) | 49 __attribute__((visibility("default"))) |
49 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { | 50 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { |
50 } | 51 } |
51 | 52 |
52 } // extern "C" | 53 } // extern "C" |
53 | 54 |
54 // static | 55 // static |
55 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, | 56 void FontLoader::LoadFont(const FontDescriptor& font, |
56 base::SharedMemory* font_data, | 57 FontLoader::Result* result) { |
57 uint32* font_data_size, | 58 DCHECK(result); |
jeremy
2012/04/04 08:35:38
Can you DCHECK that this is being called on the fi
bashi
2012/04/04 09:36:22
Done.
| |
58 uint32* font_id) { | 59 result->font_data_size = 0; |
59 DCHECK(font_data); | 60 result->font_id = 0; |
60 DCHECK(font_data_size); | |
61 DCHECK(font_id); | |
62 *font_data_size = 0; | |
63 *font_id = 0; | |
64 | 61 |
62 NSFont* font_to_encode = font.ToNSFont(); | |
65 // Used only for logging. | 63 // Used only for logging. |
66 std::string font_name([[font_to_encode fontName] UTF8String]); | 64 std::string font_name([[font_to_encode fontName] UTF8String]); |
67 | 65 |
68 // Load appropriate NSFont. | 66 // Load appropriate NSFont. |
69 if (!font_to_encode) { | 67 if (!font_to_encode) { |
70 DLOG(ERROR) << "Failed to load font " << font_name; | 68 DLOG(ERROR) << "Failed to load font " << font_name; |
71 return false; | 69 return; |
72 } | 70 } |
73 | 71 |
74 // NSFont -> ATSFontRef. | 72 // NSFont -> ATSFontRef. |
75 ATSFontRef ats_font = | 73 ATSFontRef ats_font = |
76 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); | 74 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); |
77 if (!ats_font) { | 75 if (!ats_font) { |
78 DLOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; | 76 DLOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; |
79 return false; | 77 return; |
80 } | 78 } |
81 | 79 |
82 // Retrieve the ATSFontContainerRef corresponding to the font file we want to | 80 // Retrieve the ATSFontContainerRef corresponding to the font file we want to |
83 // load. This is a unique identifier that allows the caller determine if the | 81 // load. This is a unique identifier that allows the caller determine if the |
84 // font file in question is already loaded. | 82 // font file in question is already loaded. |
85 COMPILE_ASSERT(sizeof(ATSFontContainerRef) == sizeof(font_id), | 83 COMPILE_ASSERT(sizeof(ATSFontContainerRef) == sizeof(&result->font_id), |
86 uint32_cant_hold_fontcontainer_ref); | 84 uint32_cant_hold_fontcontainer_ref); |
87 ATSFontContainerRef fontContainer = kATSFontContainerRefUnspecified; | 85 ATSFontContainerRef fontContainer = kATSFontContainerRefUnspecified; |
88 if (ATSFontGetContainer(ats_font, 0, &fontContainer) != noErr) { | 86 if (ATSFontGetContainer(ats_font, 0, &fontContainer) != noErr) { |
89 DLOG(ERROR) << "Failed to get font container ref for " << font_name; | 87 DLOG(ERROR) << "Failed to get font container ref for " << font_name; |
90 return false; | 88 return; |
91 } | 89 } |
92 | 90 |
93 // ATSFontRef -> File path. | 91 // ATSFontRef -> File path. |
94 // Warning: Calling this function on a font activated from memory will result | 92 // Warning: Calling this function on a font activated from memory will result |
95 // in failure with a -50 - paramErr. This may occur if | 93 // in failure with a -50 - paramErr. This may occur if |
96 // CreateCGFontFromBuffer() is called in the same process as this function | 94 // CreateCGFontFromBuffer() is called in the same process as this function |
97 // e.g. when writing a unit test that exercises these two functions together. | 95 // e.g. when writing a unit test that exercises these two functions together. |
98 // If said unit test were to load a system font and activate it from memory | 96 // If said unit test were to load a system font and activate it from memory |
99 // it becomes impossible for the system to the find the original file ref | 97 // it becomes impossible for the system to the find the original file ref |
100 // since the font now lives in memory as far as it's concerned. | 98 // since the font now lives in memory as far as it's concerned. |
101 FSRef font_fsref; | 99 FSRef font_fsref; |
102 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) { | 100 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) { |
103 DLOG(ERROR) << "Failed to find font file for " << font_name; | 101 DLOG(ERROR) << "Failed to find font file for " << font_name; |
104 return false; | 102 return; |
105 } | 103 } |
106 FilePath font_path = FilePath(base::mac::PathFromFSRef(font_fsref)); | 104 FilePath font_path = FilePath(base::mac::PathFromFSRef(font_fsref)); |
107 | 105 |
108 // Load file into shared memory buffer. | 106 // Load file into shared memory buffer. |
109 int64 font_file_size_64 = -1; | 107 int64 font_file_size_64 = -1; |
110 if (!file_util::GetFileSize(font_path, &font_file_size_64)) { | 108 if (!file_util::GetFileSize(font_path, &font_file_size_64)) { |
111 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); | 109 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); |
112 return false; | 110 return; |
113 } | 111 } |
114 | 112 |
115 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { | 113 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { |
116 DLOG(ERROR) << "Bad size for font file " << font_path.value(); | 114 DLOG(ERROR) << "Bad size for font file " << font_path.value(); |
117 return false; | 115 return; |
118 } | 116 } |
119 | 117 |
120 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); | 118 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); |
121 if (!font_data->CreateAndMapAnonymous(font_file_size_32)) { | 119 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) { |
jeremy
2012/04/04 08:35:38
If this or the case below fails do you leak this?
bashi
2012/04/04 09:36:22
Added result->font_data.Close().
| |
122 DLOG(ERROR) << "Failed to create shmem area for " << font_name; | 120 DLOG(ERROR) << "Failed to create shmem area for " << font_name; |
123 return false; | 121 return; |
124 } | 122 } |
125 | 123 |
126 int32 amt_read = file_util::ReadFile(font_path, | 124 int32 amt_read = file_util::ReadFile(font_path, |
127 reinterpret_cast<char*>(font_data->memory()), | 125 reinterpret_cast<char*>(result->font_data.memory()), |
128 font_file_size_32); | 126 font_file_size_32); |
129 if (amt_read != font_file_size_32) { | 127 if (amt_read != font_file_size_32) { |
130 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); | 128 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); |
131 return false; | 129 return; |
132 } | 130 } |
133 | 131 |
134 *font_data_size = font_file_size_32; | 132 result->font_data_size = font_file_size_32; |
135 *font_id = fontContainer; | 133 result->font_id = fontContainer; |
136 return true; | |
137 } | 134 } |
138 | 135 |
139 // static | 136 // static |
140 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, | 137 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, |
141 uint32 font_data_size, | 138 uint32 font_data_size, |
142 CGFontRef* out) { | 139 CGFontRef* out) { |
143 *out = NULL; | 140 *out = NULL; |
144 | 141 |
145 using base::SharedMemory; | 142 using base::SharedMemory; |
146 DCHECK(SharedMemory::IsHandleValid(font_data)); | 143 DCHECK(SharedMemory::IsHandleValid(font_data)); |
(...skipping 10 matching lines...) Expand all Loading... | |
157 if (!provider) | 154 if (!provider) |
158 return false; | 155 return false; |
159 | 156 |
160 *out = CGFontCreateWithDataProvider(provider.get()); | 157 *out = CGFontCreateWithDataProvider(provider.get()); |
161 | 158 |
162 if (*out == NULL) | 159 if (*out == NULL) |
163 return false; | 160 return false; |
164 | 161 |
165 return true; | 162 return true; |
166 } | 163 } |
OLD | NEW |