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/font_loader_mac.h" | 5 #include "content/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/mac_util.h" | 13 #include "base/mac/mac_util.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 | 15 |
16 // static | 16 // static |
17 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, | 17 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, |
18 base::SharedMemory* font_data, | 18 base::SharedMemory* font_data, |
19 uint32* font_data_size) { | 19 uint32* font_data_size, |
20 CHECK(font_data && font_data_size); | 20 uint32* font_id) { |
| 21 CHECK(font_data); |
| 22 CHECK(font_data_size); |
| 23 CHECK(font_id); |
21 *font_data_size = 0; | 24 *font_data_size = 0; |
| 25 *font_id = 0; |
22 | 26 |
23 // Used only for logging. | 27 // Used only for logging. |
24 std::string font_name([[font_to_encode fontName] UTF8String]); | 28 std::string font_name([[font_to_encode fontName] UTF8String]); |
25 | 29 |
26 // Load appropriate NSFont. | 30 // Load appropriate NSFont. |
27 if (!font_to_encode) { | 31 if (!font_to_encode) { |
28 LOG(ERROR) << "Failed to load font " << font_name; | 32 LOG(ERROR) << "Failed to load font " << font_name; |
29 return false; | 33 return false; |
30 } | 34 } |
31 | 35 |
32 // NSFont -> ATSFontRef. | 36 // NSFont -> ATSFontRef. |
33 ATSFontRef ats_font = | 37 ATSFontRef ats_font = |
34 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); | 38 CTFontGetPlatformFont(reinterpret_cast<CTFontRef>(font_to_encode), NULL); |
35 if (!ats_font) { | 39 if (!ats_font) { |
36 LOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; | 40 LOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; |
37 return false; | 41 return false; |
38 } | 42 } |
39 | 43 |
| 44 // Retrieve the ATSFontContainerRef corresponding to the font file we want to |
| 45 // load. This is a unique identifier that allows the caller determine if the |
| 46 // font file in question is already loaded. |
| 47 COMPILE_ASSERT(sizeof(ATSFontContainerRef) == sizeof(font_id), |
| 48 uint32_cant_hold_fontcontainer_ref); |
| 49 ATSFontContainerRef fontContainer = kATSFontContainerRefUnspecified; |
| 50 if (ATSFontGetContainer(ats_font, 0, &fontContainer) != noErr) { |
| 51 LOG(ERROR) << "Failed to get font container ref for " << font_name; |
| 52 return false; |
| 53 } |
| 54 |
40 // ATSFontRef -> File path. | 55 // ATSFontRef -> File path. |
41 // Warning: Calling this function on a font activated from memory will result | 56 // Warning: Calling this function on a font activated from memory will result |
42 // in failure with a -50 - paramErr. This may occur if | 57 // in failure with a -50 - paramErr. This may occur if |
43 // CreateCGFontFromBuffer() is called in the same process as this function | 58 // CreateCGFontFromBuffer() is called in the same process as this function |
44 // e.g. when writing a unit test that exercises these two functions together. | 59 // e.g. when writing a unit test that exercises these two functions together. |
45 // If said unit test were to load a system font and activate it from memory | 60 // If said unit test were to load a system font and activate it from memory |
46 // it becomes impossible for the system to the find the original file ref | 61 // it becomes impossible for the system to the find the original file ref |
47 // since the font now lives in memory as far as it's concerned. | 62 // since the font now lives in memory as far as it's concerned. |
48 FSRef font_fsref; | 63 FSRef font_fsref; |
49 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) { | 64 if (ATSFontGetFileReference(ats_font, &font_fsref) != noErr) { |
(...skipping 22 matching lines...) Expand all Loading... |
72 | 87 |
73 int32 amt_read = file_util::ReadFile(font_path, | 88 int32 amt_read = file_util::ReadFile(font_path, |
74 reinterpret_cast<char*>(font_data->memory()), | 89 reinterpret_cast<char*>(font_data->memory()), |
75 font_file_size_32); | 90 font_file_size_32); |
76 if (amt_read != font_file_size_32) { | 91 if (amt_read != font_file_size_32) { |
77 LOG(ERROR) << "Failed to read font data for " << font_path.value(); | 92 LOG(ERROR) << "Failed to read font data for " << font_path.value(); |
78 return false; | 93 return false; |
79 } | 94 } |
80 | 95 |
81 *font_data_size = font_file_size_32; | 96 *font_data_size = font_file_size_32; |
| 97 *font_id = fontContainer; |
82 return true; | 98 return true; |
83 } | 99 } |
84 | 100 |
85 // static | 101 // static |
86 bool FontLoader::ATSFontContainerFromBuffer(base::SharedMemoryHandle font_data, | 102 bool FontLoader::ATSFontContainerFromBuffer(base::SharedMemoryHandle font_data, |
87 uint32 font_data_size, | 103 uint32 font_data_size, |
88 ATSFontContainerRef* font_container) | 104 ATSFontContainerRef* font_container) |
89 { | 105 { |
90 CHECK(font_container); | 106 CHECK(font_container); |
91 | 107 |
92 using base::SharedMemory; | 108 using base::SharedMemory; |
93 DCHECK(SharedMemory::IsHandleValid(font_data)); | 109 DCHECK(SharedMemory::IsHandleValid(font_data)); |
94 DCHECK_GT(font_data_size, 0U); | 110 DCHECK_GT(font_data_size, 0U); |
95 | 111 |
96 SharedMemory shm(font_data, true); | 112 SharedMemory shm(font_data, true); |
97 if (!shm.Map(font_data_size)) | 113 if (!shm.Map(font_data_size)) |
98 return false; | 114 return false; |
99 | 115 |
100 // A value of 3 means the font is private and can't be seen by anyone else. | 116 // A value of 3 means the font is private and can't be seen by anyone else. |
101 // This is the value used by WebKit when activating remote fonts. | 117 // This is the value used by WebKit when activating remote fonts. |
102 const ATSFontContext kFontContextPrivate = 3; | 118 const ATSFontContext kFontContextPrivate = 3; |
103 OSStatus err = ATSFontActivateFromMemory(shm.memory(), font_data_size, | 119 OSStatus err = ATSFontActivateFromMemory(shm.memory(), font_data_size, |
104 kFontContextPrivate, kATSFontFormatUnspecified, NULL, | 120 kFontContextPrivate, kATSFontFormatUnspecified, NULL, |
105 kATSOptionFlagsDefault, font_container); | 121 kATSOptionFlagsDefault, font_container); |
106 if (err != noErr || !font_container) | 122 if (err != noErr || !font_container) |
107 return false; | 123 return false; |
108 | 124 |
109 return true; | 125 return true; |
110 } | 126 } |
OLD | NEW |