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" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { | 49 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { |
50 } | 50 } |
51 | 51 |
52 } // extern "C" | 52 } // extern "C" |
53 | 53 |
54 // static | 54 // static |
55 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, | 55 bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, |
56 base::SharedMemory* font_data, | 56 base::SharedMemory* font_data, |
57 uint32* font_data_size, | 57 uint32* font_data_size, |
58 uint32* font_id) { | 58 uint32* font_id) { |
59 CHECK(font_data); | 59 DCHECK(font_data); |
60 CHECK(font_data_size); | 60 DCHECK(font_data_size); |
61 CHECK(font_id); | 61 DCHECK(font_id); |
62 *font_data_size = 0; | 62 *font_data_size = 0; |
63 *font_id = 0; | 63 *font_id = 0; |
64 | 64 |
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 LOG(ERROR) << "Failed to load font " << font_name; | 70 DLOG(ERROR) << "Failed to load font " << font_name; |
71 return false; | 71 return false; |
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 LOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; | 78 DLOG(ERROR) << "Conversion to ATSFontRef failed for " << font_name; |
79 return false; | 79 return false; |
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(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 LOG(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 false; |
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 LOG(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 false; |
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 LOG(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 false; |
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 LOG(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 false; |
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 (!font_data->CreateAndMapAnonymous(font_file_size_32)) { |
122 LOG(ERROR) << "Failed to create shmem area for " << font_name; | 122 DLOG(ERROR) << "Failed to create shmem area for " << font_name; |
123 return false; | 123 return false; |
124 } | 124 } |
125 | 125 |
126 int32 amt_read = file_util::ReadFile(font_path, | 126 int32 amt_read = file_util::ReadFile(font_path, |
127 reinterpret_cast<char*>(font_data->memory()), | 127 reinterpret_cast<char*>(font_data->memory()), |
128 font_file_size_32); | 128 font_file_size_32); |
129 if (amt_read != font_file_size_32) { | 129 if (amt_read != font_file_size_32) { |
130 LOG(ERROR) << "Failed to read font data for " << font_path.value(); | 130 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); |
131 return false; | 131 return false; |
132 } | 132 } |
133 | 133 |
134 *font_data_size = font_file_size_32; | 134 *font_data_size = font_file_size_32; |
135 *font_id = fontContainer; | 135 *font_id = fontContainer; |
136 return true; | 136 return true; |
137 } | 137 } |
138 | 138 |
139 // static | 139 // static |
140 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, | 140 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, |
(...skipping 16 matching lines...) Expand all Loading... |
157 if (!provider) | 157 if (!provider) |
158 return false; | 158 return false; |
159 | 159 |
160 *out = CGFontCreateWithDataProvider(provider.get()); | 160 *out = CGFontCreateWithDataProvider(provider.get()); |
161 | 161 |
162 if (*out == NULL) | 162 if (*out == NULL) |
163 return false; | 163 return false; |
164 | 164 |
165 return true; | 165 return true; |
166 } | 166 } |
OLD | NEW |