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