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