diff options
Diffstat (limited to 'content/common')
-rw-r--r-- | content/common/font_loader_mac.h | 16 | ||||
-rw-r--r-- | content/common/font_loader_mac.mm | 32 | ||||
-rw-r--r-- | content/common/sandbox_mac_fontloading_unittest.mm | 57 |
3 files changed, 46 insertions, 59 deletions
diff --git a/content/common/font_loader_mac.h b/content/common/font_loader_mac.h index 4043dce..410e076 100644 --- a/content/common/font_loader_mac.h +++ b/content/common/font_loader_mac.h @@ -19,7 +19,7 @@ class NSFont; // Provides functionality to transmit fonts over IPC. // // Note about font formats: .dfont (datafork suitcase) fonts are currently not -// supported by this code since ATSFontActivateFromMemory() can't handle them +// supported by this code since CGFontCreateWithDataProvider() can't handle them // directly. class FontLoader { @@ -39,16 +39,22 @@ class FontLoader { uint32* font_id); // Given a shared memory buffer containing the raw data for a font file, load - // the font and return a container ref. + // the font and return a CGFontRef. // // |data| - A shared memory handle pointing to the raw data from a font file. // |data_size| - Size of |data|. // // On return: // returns true on success, false on failure. - // |font_container| - A font container corresponding to the designated font. - // The caller is responsible for releasing this value via ATSFontDeactivate() - // when done + // |out| - A CGFontRef corresponding to the designated font. + // The caller is responsible for releasing this value via CGFontRelease() + // when done. + static bool CGFontRefFromBuffer(base::SharedMemoryHandle font_data, + uint32 font_data_size, + CGFontRef* out); + + + // TODO(jeremy): Remove once http://webk.it/66935 lands. static bool ATSFontContainerFromBuffer(base::SharedMemoryHandle font_data, uint32 font_data_size, ATSFontContainerRef* font_container); diff --git a/content/common/font_loader_mac.mm b/content/common/font_loader_mac.mm index b2ff8f4..867e1d3 100644 --- a/content/common/font_loader_mac.mm +++ b/content/common/font_loader_mac.mm @@ -10,7 +10,9 @@ #include "base/file_path.h" #include "base/file_util.h" #include "base/logging.h" +#include "base/mac/foundation_util.h" #include "base/mac/mac_util.h" +#include "base/mac/scoped_cftyperef.h" #include "base/sys_string_conversions.h" extern "C" { @@ -135,6 +137,36 @@ bool FontLoader::LoadFontIntoBuffer(NSFont* font_to_encode, } // static +bool FontLoader::CGFontRefFromBuffer(base::SharedMemoryHandle font_data, + uint32 font_data_size, + CGFontRef* out) { + *out = NULL; + + using base::SharedMemory; + DCHECK(SharedMemory::IsHandleValid(font_data)); + DCHECK_GT(font_data_size, 0U); + + SharedMemory shm(font_data, /*read_only=*/true); + if (!shm.Map(font_data_size)) + return false; + + NSData* data = [NSData dataWithBytes:shm.memory() + length:font_data_size]; + base::mac::ScopedCFTypeRef<CGDataProviderRef> provider( + CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data))); + if (!provider) + return false; + + *out = CGFontCreateWithDataProvider(provider.get()); + + if (*out == NULL) + return false; + + return true; +} + +// TODO(jeremy): Remove once http://webk.it/66935 lands. +// static bool FontLoader::ATSFontContainerFromBuffer(base::SharedMemoryHandle font_data, uint32 font_data_size, ATSFontContainerRef* font_container) diff --git a/content/common/sandbox_mac_fontloading_unittest.mm b/content/common/sandbox_mac_fontloading_unittest.mm index b4b1a88..bca576d 100644 --- a/content/common/sandbox_mac_fontloading_unittest.mm +++ b/content/common/sandbox_mac_fontloading_unittest.mm @@ -18,48 +18,6 @@ namespace { using sandboxtest::MacSandboxTest; using sandbox::Sandbox; -bool CGFontFromFontContainer(ATSFontContainerRef container, CGFontRef* out) { - // Count the number of fonts that were loaded. - ItemCount fontCount = 0; - OSStatus err = ATSFontFindFromContainer(container, kATSOptionFlagsDefault, 0, - NULL, &fontCount); - - if (err != noErr || fontCount < 1) { - return false; - } - - // Load font from container. - ATSFontRef font_ref_ats = 0; - ATSFontFindFromContainer(container, kATSOptionFlagsDefault, 1, - &font_ref_ats, NULL); - - if (!font_ref_ats) { - return false; - } - - // Convert to cgFont. - CGFontRef font_ref_cg = CGFontCreateWithPlatformFont(&font_ref_ats); - - if (!font_ref_cg) { - return false; - } - - *out = font_ref_cg; - return true; -} - -class ScopedFontContainer { - public: - explicit ScopedFontContainer(ATSFontContainerRef ref) - : container_ref(ref) {} - - ~ScopedFontContainer() { - ATSFontDeactivate(container_ref, NULL, kATSOptionFlagsDefault); - } - - ATSFontContainerRef container_ref; -}; - class FontLoadingTestCase : public sandboxtest::MacSandboxTestCase { public: FontLoadingTestCase() : font_data_length_(-1) {} @@ -112,19 +70,10 @@ bool FontLoadingTestCase::SandboxedTest() { return false; } - ATSFontContainerRef font_container; - if (!FontLoader::ATSFontContainerFromBuffer(shmem_handle, font_data_length_, - &font_container)) { - LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; - return false; - } - - // Unload the font container when done. - ScopedFontContainer scoped_unloader(font_container); - CGFontRef cg_font_ref; - if (!CGFontFromFontContainer(font_container, &cg_font_ref)) { - LOG(ERROR) << "CGFontFromFontContainer failed"; + if (!FontLoader::CGFontRefFromBuffer(shmem_handle, font_data_length_, + &cg_font_ref)) { + LOG(ERROR) << "Call to CreateCGFontFromBuffer() failed"; return false; } |