| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // static | 86 // static |
| 87 void FontLoader::LoadFont(const FontDescriptor& font, | 87 void FontLoader::LoadFont(const FontDescriptor& font, |
| 88 FontLoader::Result* result) { | 88 FontLoader::Result* result) { |
| 89 base::ThreadRestrictions::AssertIOAllowed(); | 89 base::ThreadRestrictions::AssertIOAllowed(); |
| 90 | 90 |
| 91 DCHECK(result); | 91 DCHECK(result); |
| 92 result->font_data_size = 0; | 92 result->font_data_size = 0; |
| 93 result->font_id = 0; | 93 result->font_id = 0; |
| 94 | 94 |
| 95 NSFont* font_to_encode = font.ToNSFont(); | 95 NSFont* font_to_encode = font.ToNSFont(); |
| 96 // Used only for logging. | |
| 97 std::string font_name([[font_to_encode fontName] UTF8String]); | |
| 98 | 96 |
| 99 // Load appropriate NSFont. | 97 // Load appropriate NSFont. |
| 100 if (!font_to_encode) { | 98 if (!font_to_encode) { |
| 101 DLOG(ERROR) << "Failed to load font " << font_name; | 99 DLOG(ERROR) << "Failed to load font " << font.font_name; |
| 102 return; | 100 return; |
| 103 } | 101 } |
| 104 | 102 |
| 105 // NSFont -> File path. | 103 // NSFont -> File path. |
| 106 // Warning: Calling this function on a font activated from memory will result | 104 // Warning: Calling this function on a font activated from memory will result |
| 107 // in failure with a -50 - paramErr. This may occur if | 105 // in failure with a -50 - paramErr. This may occur if |
| 108 // CreateCGFontFromBuffer() is called in the same process as this function | 106 // CreateCGFontFromBuffer() is called in the same process as this function |
| 109 // e.g. when writing a unit test that exercises these two functions together. | 107 // e.g. when writing a unit test that exercises these two functions together. |
| 110 // If said unit test were to load a system font and activate it from memory | 108 // If said unit test were to load a system font and activate it from memory |
| 111 // it becomes impossible for the system to the find the original file ref | 109 // it becomes impossible for the system to the find the original file ref |
| 112 // since the font now lives in memory as far as it's concerned. | 110 // since the font now lives in memory as far as it's concerned. |
| 113 CTFontRef ct_font_to_encode = (CTFontRef)font_to_encode; | 111 CTFontRef ct_font_to_encode = (CTFontRef)font_to_encode; |
| 114 base::scoped_nsobject<NSURL> font_url( | 112 base::scoped_nsobject<NSURL> font_url( |
| 115 base::mac::CFToNSCast(base::mac::CFCastStrict<CFURLRef>( | 113 base::mac::CFToNSCast(base::mac::CFCastStrict<CFURLRef>( |
| 116 CTFontCopyAttribute(ct_font_to_encode, kCTFontURLAttribute)))); | 114 CTFontCopyAttribute(ct_font_to_encode, kCTFontURLAttribute)))); |
| 117 if (![font_url isFileURL]) { | 115 if (![font_url isFileURL]) { |
| 118 DLOG(ERROR) << "Failed to find font file for " << font_name; | 116 DLOG(ERROR) << "Failed to find font file for " << font.font_name; |
| 119 return; | 117 return; |
| 120 } | 118 } |
| 121 | 119 |
| 122 base::FilePath font_path = base::mac::NSStringToFilePath([font_url path]); | 120 base::FilePath font_path = base::mac::NSStringToFilePath([font_url path]); |
| 123 | 121 |
| 124 // Load file into shared memory buffer. | 122 // Load file into shared memory buffer. |
| 125 int64 font_file_size_64 = -1; | 123 int64 font_file_size_64 = -1; |
| 126 if (!base::GetFileSize(font_path, &font_file_size_64)) { | 124 if (!base::GetFileSize(font_path, &font_file_size_64)) { |
| 127 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); | 125 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); |
| 128 return; | 126 return; |
| 129 } | 127 } |
| 130 | 128 |
| 131 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { | 129 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { |
| 132 DLOG(ERROR) << "Bad size for font file " << font_path.value(); | 130 DLOG(ERROR) << "Bad size for font file " << font_path.value(); |
| 133 return; | 131 return; |
| 134 } | 132 } |
| 135 | 133 |
| 136 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); | 134 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); |
| 137 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) { | 135 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) { |
| 138 DLOG(ERROR) << "Failed to create shmem area for " << font_name; | 136 DLOG(ERROR) << "Failed to create shmem area for " << font.font_name; |
| 139 return; | 137 return; |
| 140 } | 138 } |
| 141 | 139 |
| 142 int32 amt_read = base::ReadFile(font_path, | 140 int32 amt_read = base::ReadFile(font_path, |
| 143 reinterpret_cast<char*>(result->font_data.memory()), | 141 reinterpret_cast<char*>(result->font_data.memory()), |
| 144 font_file_size_32); | 142 font_file_size_32); |
| 145 if (amt_read != font_file_size_32) { | 143 if (amt_read != font_file_size_32) { |
| 146 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); | 144 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); |
| 147 return; | 145 return; |
| 148 } | 146 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 172 if (!provider) | 170 if (!provider) |
| 173 return false; | 171 return false; |
| 174 | 172 |
| 175 *out = CGFontCreateWithDataProvider(provider.get()); | 173 *out = CGFontCreateWithDataProvider(provider.get()); |
| 176 | 174 |
| 177 if (*out == NULL) | 175 if (*out == NULL) |
| 178 return false; | 176 return false; |
| 179 | 177 |
| 180 return true; | 178 return true; |
| 181 } | 179 } |
| OLD | NEW |