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 <limits> |
| 10 |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/mac/foundation_util.h" | 15 #include "base/mac/foundation_util.h" |
14 #include "base/mac/scoped_cftyperef.h" | 16 #include "base/mac/scoped_cftyperef.h" |
15 #include "base/mac/scoped_nsobject.h" | 17 #include "base/mac/scoped_nsobject.h" |
16 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
17 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
18 #include "content/common/mac/font_descriptor.h" | 20 #include "content/common/mac/font_descriptor.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // share this bug but this sort of memory corruption wasn't detected until | 52 // share this bug but this sort of memory corruption wasn't detected until |
51 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem. | 53 // 10.7. The implementation in 10.5 ("Leopard") does not have this problem. |
52 __attribute__((visibility("default"))) | 54 __attribute__((visibility("default"))) |
53 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { | 55 void _CTFontManagerUnregisterFontForData(NSUInteger, int) { |
54 } | 56 } |
55 | 57 |
56 } // extern "C" | 58 } // extern "C" |
57 | 59 |
58 namespace { | 60 namespace { |
59 | 61 |
60 uint32 GetFontIDForFont(const base::FilePath& font_path) { | 62 uint32_t GetFontIDForFont(const base::FilePath& font_path) { |
61 // content/common can't depend on content/browser, so this cannot call | 63 // content/common can't depend on content/browser, so this cannot call |
62 // BrowserThread::CurrentlyOn(). Check this is always called on the same | 64 // BrowserThread::CurrentlyOn(). Check this is always called on the same |
63 // thread. | 65 // thread. |
64 static pthread_t thread_id = pthread_self(); | 66 static pthread_t thread_id = pthread_self(); |
65 DCHECK_EQ(pthread_self(), thread_id); | 67 DCHECK_EQ(pthread_self(), thread_id); |
66 | 68 |
67 // Font loading used to call ATSFontGetContainer() | 69 // Font loading used to call ATSFontGetContainer() |
68 // and used that as font id. | 70 // and used that as font id. |
69 // ATS is deprecated and CTFont doesn't seem to have a obvious fixed id for a | 71 // ATS is deprecated and CTFont doesn't seem to have a obvious fixed id for a |
70 // font. Since this function is only called from a single thread, use a static | 72 // font. Since this function is only called from a single thread, use a static |
71 // map to store ids. | 73 // map to store ids. |
72 typedef std::map<base::FilePath, uint32> FontIdMap; | 74 typedef std::map<base::FilePath, uint32_t> FontIdMap; |
73 CR_DEFINE_STATIC_LOCAL(FontIdMap, font_ids, ()); | 75 CR_DEFINE_STATIC_LOCAL(FontIdMap, font_ids, ()); |
74 | 76 |
75 auto it = font_ids.find(font_path); | 77 auto it = font_ids.find(font_path); |
76 if (it != font_ids.end()) | 78 if (it != font_ids.end()) |
77 return it->second; | 79 return it->second; |
78 | 80 |
79 uint32 font_id = font_ids.size() + 1; | 81 uint32_t font_id = font_ids.size() + 1; |
80 font_ids[font_path] = font_id; | 82 font_ids[font_path] = font_id; |
81 return font_id; | 83 return font_id; |
82 } | 84 } |
83 | 85 |
84 } // namespace | 86 } // namespace |
85 | 87 |
86 // static | 88 // static |
87 void FontLoader::LoadFont(const FontDescriptor& font, | 89 void FontLoader::LoadFont(const FontDescriptor& font, |
88 FontLoader::Result* result) { | 90 FontLoader::Result* result) { |
89 base::ThreadRestrictions::AssertIOAllowed(); | 91 base::ThreadRestrictions::AssertIOAllowed(); |
(...skipping 23 matching lines...) Expand all Loading... |
113 base::mac::CFToNSCast(base::mac::CFCastStrict<CFURLRef>( | 115 base::mac::CFToNSCast(base::mac::CFCastStrict<CFURLRef>( |
114 CTFontCopyAttribute(ct_font_to_encode, kCTFontURLAttribute)))); | 116 CTFontCopyAttribute(ct_font_to_encode, kCTFontURLAttribute)))); |
115 if (![font_url isFileURL]) { | 117 if (![font_url isFileURL]) { |
116 DLOG(ERROR) << "Failed to find font file for " << font.font_name; | 118 DLOG(ERROR) << "Failed to find font file for " << font.font_name; |
117 return; | 119 return; |
118 } | 120 } |
119 | 121 |
120 base::FilePath font_path = base::mac::NSStringToFilePath([font_url path]); | 122 base::FilePath font_path = base::mac::NSStringToFilePath([font_url path]); |
121 | 123 |
122 // Load file into shared memory buffer. | 124 // Load file into shared memory buffer. |
123 int64 font_file_size_64 = -1; | 125 int64_t font_file_size_64 = -1; |
124 if (!base::GetFileSize(font_path, &font_file_size_64)) { | 126 if (!base::GetFileSize(font_path, &font_file_size_64)) { |
125 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); | 127 DLOG(ERROR) << "Couldn't get font file size for " << font_path.value(); |
126 return; | 128 return; |
127 } | 129 } |
128 | 130 |
129 if (font_file_size_64 <= 0 || font_file_size_64 >= kint32max) { | 131 if (font_file_size_64 <= 0 || |
| 132 font_file_size_64 >= std::numeric_limits<int32_t>::max()) { |
130 DLOG(ERROR) << "Bad size for font file " << font_path.value(); | 133 DLOG(ERROR) << "Bad size for font file " << font_path.value(); |
131 return; | 134 return; |
132 } | 135 } |
133 | 136 |
134 int32 font_file_size_32 = static_cast<int32>(font_file_size_64); | 137 int32_t font_file_size_32 = static_cast<int32_t>(font_file_size_64); |
135 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) { | 138 if (!result->font_data.CreateAndMapAnonymous(font_file_size_32)) { |
136 DLOG(ERROR) << "Failed to create shmem area for " << font.font_name; | 139 DLOG(ERROR) << "Failed to create shmem area for " << font.font_name; |
137 return; | 140 return; |
138 } | 141 } |
139 | 142 |
140 int32 amt_read = base::ReadFile(font_path, | 143 int32_t amt_read = base::ReadFile( |
141 reinterpret_cast<char*>(result->font_data.memory()), | 144 font_path, reinterpret_cast<char*>(result->font_data.memory()), |
142 font_file_size_32); | 145 font_file_size_32); |
143 if (amt_read != font_file_size_32) { | 146 if (amt_read != font_file_size_32) { |
144 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); | 147 DLOG(ERROR) << "Failed to read font data for " << font_path.value(); |
145 return; | 148 return; |
146 } | 149 } |
147 | 150 |
148 result->font_data_size = font_file_size_32; | 151 result->font_data_size = font_file_size_32; |
149 result->font_id = GetFontIDForFont(font_path); | 152 result->font_id = GetFontIDForFont(font_path); |
150 } | 153 } |
151 | 154 |
152 // static | 155 // static |
153 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, | 156 bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, |
154 uint32 font_data_size, | 157 uint32_t font_data_size, |
155 CGFontRef* out) { | 158 CGFontRef* out) { |
156 *out = NULL; | 159 *out = NULL; |
157 | 160 |
158 using base::SharedMemory; | 161 using base::SharedMemory; |
159 DCHECK(SharedMemory::IsHandleValid(font_data)); | 162 DCHECK(SharedMemory::IsHandleValid(font_data)); |
160 DCHECK_GT(font_data_size, 0U); | 163 DCHECK_GT(font_data_size, 0U); |
161 | 164 |
162 SharedMemory shm(font_data, /*read_only=*/true); | 165 SharedMemory shm(font_data, /*read_only=*/true); |
163 if (!shm.Map(font_data_size)) | 166 if (!shm.Map(font_data_size)) |
164 return false; | 167 return false; |
165 | 168 |
166 NSData* data = [NSData dataWithBytes:shm.memory() | 169 NSData* data = [NSData dataWithBytes:shm.memory() |
167 length:font_data_size]; | 170 length:font_data_size]; |
168 base::ScopedCFTypeRef<CGDataProviderRef> provider( | 171 base::ScopedCFTypeRef<CGDataProviderRef> provider( |
169 CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data))); | 172 CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data))); |
170 if (!provider) | 173 if (!provider) |
171 return false; | 174 return false; |
172 | 175 |
173 *out = CGFontCreateWithDataProvider(provider.get()); | 176 *out = CGFontCreateWithDataProvider(provider.get()); |
174 | 177 |
175 if (*out == NULL) | 178 if (*out == NULL) |
176 return false; | 179 return false; |
177 | 180 |
178 return true; | 181 return true; |
179 } | 182 } |
OLD | NEW |