Commit 0f9ebc64 authored by Dominik Röttsches's avatar Dominik Röttsches Committed by Commit Bot

Load OOP Mac system fonts without CGFont API

Match Skia in not creating buffer-based fonts through CGFonts and
CTFontCreateWithGraphicsFont any more, but instead use
CTFontManagerCreateFontDescriptorFromData. This enables cleanup in Skia
removing the CGFont parameter from SkCreateTypefaceFromCTFont, after
Skia moved to using CTFontManagerCreateFontDescriptorFromData in [1].

[1] https://skia-review.googlesource.com/c/skia/+/257052

Bug: 1033478
Change-Id: I4048b683659e40eead9939697c4900a935623627
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1964073
Commit-Queue: Dominik Röttsches <drott@chromium.org>
Reviewed-by: default avatarBen Wagner <bungeman@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarMark Mentovai <mark@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734008}
parent eb17f4c2
...@@ -211,17 +211,17 @@ MULTIPROCESS_TEST_MAIN(FontLoadingProcess) { ...@@ -211,17 +211,17 @@ MULTIPROCESS_TEST_MAIN(FontLoadingProcess) {
font_shmem->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); font_shmem->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY);
CHECK(shmem_handle.is_valid()); CHECK(shmem_handle.is_valid());
base::ScopedCFTypeRef<CGFontRef> cgfont; base::ScopedCFTypeRef<CTFontRef> ctfont_base;
CHECK(FontLoader::CGFontRefFromBuffer( CHECK(FontLoader::CTFontRefFromBuffer(
std::move(shmem_handle), font_data_length, cgfont.InitializeInto())); std::move(shmem_handle), font_data_length, ctfont_base.InitializeInto()));
CHECK(cgfont); CHECK(ctfont_base);
base::ScopedCFTypeRef<CTFontRef> ctfont( base::ScopedCFTypeRef<CTFontRef> sized_ctfont(CTFontCreateCopyWithAttributes(
CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL)); ctfont_base.get(), 16.0, nullptr, nullptr));
CHECK(ctfont); CHECK(sized_ctfont);
// Do something with the font to make sure it's loaded. // Do something with the font to make sure it's loaded.
CGFloat cap_height = CTFontGetCapHeight(ctfont); CGFloat cap_height = CTFontGetCapHeight(sized_ctfont);
CHECK(cap_height > 0.0); CHECK(cap_height > 0.0);
return 0; return 0;
......
...@@ -30,7 +30,7 @@ WebSandboxSupportMac::WebSandboxSupportMac() { ...@@ -30,7 +30,7 @@ WebSandboxSupportMac::WebSandboxSupportMac() {
WebSandboxSupportMac::~WebSandboxSupportMac() = default; WebSandboxSupportMac::~WebSandboxSupportMac() = default;
bool WebSandboxSupportMac::LoadFont(CTFontRef font, bool WebSandboxSupportMac::LoadFont(CTFontRef font,
CGFontRef* out, CTFontRef* out,
uint32_t* font_id) { uint32_t* font_id) {
if (!sandbox_support_) if (!sandbox_support_)
return false; return false;
...@@ -55,7 +55,7 @@ bool WebSandboxSupportMac::LoadFont(CTFontRef font, ...@@ -55,7 +55,7 @@ bool WebSandboxSupportMac::LoadFont(CTFontRef font,
// TODO(jeremy): Need to call back into the requesting process to make sure // TODO(jeremy): Need to call back into the requesting process to make sure
// that the font isn't already activated, based on the font id. If it's // that the font isn't already activated, based on the font id. If it's
// already activated, don't reactivate it here - https://crbug.com/72727 . // already activated, don't reactivate it here - https://crbug.com/72727 .
return FontLoader::CGFontRefFromBuffer( return FontLoader::CTFontRefFromBuffer(
std::move(font_data), static_cast<uint32_t>(font_data_size), out); std::move(font_data), static_cast<uint32_t>(font_data_size), out);
} }
......
...@@ -24,7 +24,7 @@ class WebSandboxSupportMac : public blink::WebSandboxSupport { ...@@ -24,7 +24,7 @@ class WebSandboxSupportMac : public blink::WebSandboxSupport {
~WebSandboxSupportMac() override; ~WebSandboxSupportMac() override;
// blink::WebSandboxSupport: // blink::WebSandboxSupport:
bool LoadFont(CTFontRef font, CGFontRef* out, uint32_t* font_id) override; bool LoadFont(CTFontRef font, CTFontRef* out, uint32_t* font_id) override;
SkColor GetSystemColor(blink::MacSystemColorID color_id) override; SkColor GetSystemColor(blink::MacSystemColorID color_id) override;
private: private:
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#ifndef CONTENT_COMMON_MAC_FONT_LOADER_H_ #ifndef CONTENT_COMMON_MAC_FONT_LOADER_H_
#define CONTENT_COMMON_MAC_FONT_LOADER_H_ #define CONTENT_COMMON_MAC_FONT_LOADER_H_
#include <CoreGraphics/CoreGraphics.h> #include <CoreText/CoreText.h>
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
...@@ -49,20 +49,19 @@ class FontLoader { ...@@ -49,20 +49,19 @@ class FontLoader {
LoadedCallback callback); LoadedCallback callback);
// Given a shared memory buffer containing the raw data for a font file, load // Given a shared memory buffer containing the raw data for a font file, load
// the font and return a CGFontRef. // the font and return a CTFontRef.
// //
// |data| - A shared memory handle pointing to the raw data from a font file. // |data| - A shared memory handle pointing to the raw data from a font file.
// |data_size| - Size of |data|. // |data_size| - Size of |data|.
// //
// On return: // On return:
// returns true on success, false on failure. // returns true on success, false on failure.
// |out| - A CGFontRef corresponding to the designated font. // |out| - A CTFontRef corresponding to the designated font.
// The caller is responsible for releasing this value via CGFontRelease() // The caller is responsible for releasing this value via CFRelease().
// when done.
CONTENT_EXPORT CONTENT_EXPORT
static bool CGFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data, static bool CTFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
uint32_t font_data_size, uint32_t font_data_size,
CGFontRef* out); CTFontRef* out);
CONTENT_EXPORT CONTENT_EXPORT
static std::unique_ptr<ResultInternal> LoadFontForTesting( static std::unique_ptr<ResultInternal> LoadFontForTesting(
......
...@@ -141,21 +141,23 @@ void FontLoader::LoadFont(const base::string16& font_name, ...@@ -141,21 +141,23 @@ void FontLoader::LoadFont(const base::string16& font_name,
} }
// static // static
bool FontLoader::CGFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data, bool FontLoader::CTFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
uint32_t font_data_size, uint32_t font_data_size,
CGFontRef* out) { CTFontRef* out) {
*out = NULL; *out = NULL;
mojo::ScopedSharedBufferMapping mapping = font_data->Map(font_data_size); mojo::ScopedSharedBufferMapping mapping = font_data->Map(font_data_size);
if (!mapping) if (!mapping)
return false; return false;
NSData* data = [NSData dataWithBytes:mapping.get() length:font_data_size]; NSData* data = [NSData dataWithBytes:mapping.get() length:font_data_size];
base::ScopedCFTypeRef<CTFontDescriptorRef> data_descriptor(
CTFontManagerCreateFontDescriptorFromData(base::mac::NSToCFCast(data)));
base::ScopedCFTypeRef<CGDataProviderRef> provider( base::ScopedCFTypeRef<CGDataProviderRef> provider(
CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data))); CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data)));
if (!provider) if (!provider)
return false; return false;
*out = CGFontCreateWithDataProvider(provider.get()); *out = CTFontCreateWithFontDescriptor(data_descriptor.get(), 0, nullptr);
if (*out == NULL) if (*out == NULL)
return false; return false;
......
...@@ -49,11 +49,11 @@ class WebSandboxSupport { ...@@ -49,11 +49,11 @@ class WebSandboxSupport {
// to the on-disk font file. // to the on-disk font file.
// //
// If this function succeeds, the caller assumes ownership of the |out| // If this function succeeds, the caller assumes ownership of the |out|
// parameter and must call CGFontRelease() to unload it when done. // parameter and must call CFRelease() on the CTFontRef.
// //
// Returns: true on success, false on error. // Returns: true on success, false on error.
virtual bool LoadFont(CTFontRef src_font, virtual bool LoadFont(CTFontRef src_font,
CGFontRef* out, CTFontRef* out,
uint32_t* font_id) = 0; uint32_t* font_id) = 0;
// Returns the system's preferred value for a named color. // Returns the system's preferred value for a named color.
......
...@@ -94,20 +94,20 @@ static sk_sp<SkTypeface> LoadFromBrowserProcess(NSFont* ns_font, ...@@ -94,20 +94,20 @@ static sk_sp<SkTypeface> LoadFromBrowserProcess(NSFont* ns_font,
return nullptr; return nullptr;
} }
CGFontRef loaded_cg_font; CTFontRef loaded_ct_font;
uint32_t font_id; uint32_t font_id;
if (!sandbox_support->LoadFont(base::mac::NSToCFCast(ns_font), if (!sandbox_support->LoadFont(base::mac::NSToCFCast(ns_font),
&loaded_cg_font, &font_id)) { &loaded_ct_font, &font_id)) {
// TODO crbug.com/461279: Make this appear in the inspector console? // TODO crbug.com/461279: Make this appear in the inspector console?
DLOG(ERROR) DLOG(ERROR)
<< "Loading user font \"" << [[ns_font familyName] UTF8String] << "Loading user font \"" << [[ns_font familyName] UTF8String]
<< "\" from non system location failed. Corrupt or missing font file?"; << "\" from non system location failed. Corrupt or missing font file?";
return nullptr; return nullptr;
} }
base::ScopedCFTypeRef<CGFontRef> cg_font(loaded_cg_font); base::ScopedCFTypeRef<CTFontRef> ct_font_base(loaded_ct_font);
base::ScopedCFTypeRef<CTFontRef> ct_font(CTFontCreateWithGraphicsFont( base::ScopedCFTypeRef<CTFontRef> ct_font(CTFontCreateCopyWithAttributes(
cg_font, text_size, 0, CascadeToLastResortFontDescriptor())); ct_font_base, text_size, 0, CascadeToLastResortFontDescriptor()));
sk_sp<SkTypeface> return_font(SkCreateTypefaceFromCTFont(ct_font, cg_font)); sk_sp<SkTypeface> return_font(SkCreateTypefaceFromCTFont(ct_font));
if (!return_font.get()) if (!return_font.get())
// TODO crbug.com/461279: Make this appear in the inspector console? // TODO crbug.com/461279: Make this appear in the inspector console?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment