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) {
font_shmem->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY);
CHECK(shmem_handle.is_valid());
base::ScopedCFTypeRef<CGFontRef> cgfont;
CHECK(FontLoader::CGFontRefFromBuffer(
std::move(shmem_handle), font_data_length, cgfont.InitializeInto()));
CHECK(cgfont);
base::ScopedCFTypeRef<CTFontRef> ctfont_base;
CHECK(FontLoader::CTFontRefFromBuffer(
std::move(shmem_handle), font_data_length, ctfont_base.InitializeInto()));
CHECK(ctfont_base);
base::ScopedCFTypeRef<CTFontRef> ctfont(
CTFontCreateWithGraphicsFont(cgfont.get(), 16.0, NULL, NULL));
CHECK(ctfont);
base::ScopedCFTypeRef<CTFontRef> sized_ctfont(CTFontCreateCopyWithAttributes(
ctfont_base.get(), 16.0, nullptr, nullptr));
CHECK(sized_ctfont);
// 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);
return 0;
......
......@@ -30,7 +30,7 @@ WebSandboxSupportMac::WebSandboxSupportMac() {
WebSandboxSupportMac::~WebSandboxSupportMac() = default;
bool WebSandboxSupportMac::LoadFont(CTFontRef font,
CGFontRef* out,
CTFontRef* out,
uint32_t* font_id) {
if (!sandbox_support_)
return false;
......@@ -55,7 +55,7 @@ bool WebSandboxSupportMac::LoadFont(CTFontRef font,
// 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
// 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);
}
......
......@@ -24,7 +24,7 @@ class WebSandboxSupportMac : public blink::WebSandboxSupport {
~WebSandboxSupportMac() override;
// 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;
private:
......
......@@ -5,7 +5,7 @@
#ifndef 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 <memory>
......@@ -49,20 +49,19 @@ class FontLoader {
LoadedCallback callback);
// 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_size| - Size of |data|.
//
// On return:
// returns true on success, false on failure.
// |out| - A CGFontRef corresponding to the designated font.
// The caller is responsible for releasing this value via CGFontRelease()
// when done.
// |out| - A CTFontRef corresponding to the designated font.
// The caller is responsible for releasing this value via CFRelease().
CONTENT_EXPORT
static bool CGFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
static bool CTFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
uint32_t font_data_size,
CGFontRef* out);
CTFontRef* out);
CONTENT_EXPORT
static std::unique_ptr<ResultInternal> LoadFontForTesting(
......
......@@ -141,21 +141,23 @@ void FontLoader::LoadFont(const base::string16& font_name,
}
// static
bool FontLoader::CGFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
bool FontLoader::CTFontRefFromBuffer(mojo::ScopedSharedBufferHandle font_data,
uint32_t font_data_size,
CGFontRef* out) {
CTFontRef* out) {
*out = NULL;
mojo::ScopedSharedBufferMapping mapping = font_data->Map(font_data_size);
if (!mapping)
return false;
NSData* data = [NSData dataWithBytes:mapping.get() length:font_data_size];
base::ScopedCFTypeRef<CTFontDescriptorRef> data_descriptor(
CTFontManagerCreateFontDescriptorFromData(base::mac::NSToCFCast(data)));
base::ScopedCFTypeRef<CGDataProviderRef> provider(
CGDataProviderCreateWithCFData(base::mac::NSToCFCast(data)));
if (!provider)
return false;
*out = CGFontCreateWithDataProvider(provider.get());
*out = CTFontCreateWithFontDescriptor(data_descriptor.get(), 0, nullptr);
if (*out == NULL)
return false;
......
......@@ -49,11 +49,11 @@ class WebSandboxSupport {
// to the on-disk font file.
//
// 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.
virtual bool LoadFont(CTFontRef src_font,
CGFontRef* out,
CTFontRef* out,
uint32_t* font_id) = 0;
// Returns the system's preferred value for a named color.
......
......@@ -94,20 +94,20 @@ static sk_sp<SkTypeface> LoadFromBrowserProcess(NSFont* ns_font,
return nullptr;
}
CGFontRef loaded_cg_font;
CTFontRef loaded_ct_font;
uint32_t font_id;
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?
DLOG(ERROR)
<< "Loading user font \"" << [[ns_font familyName] UTF8String]
<< "\" from non system location failed. Corrupt or missing font file?";
return nullptr;
}
base::ScopedCFTypeRef<CGFontRef> cg_font(loaded_cg_font);
base::ScopedCFTypeRef<CTFontRef> ct_font(CTFontCreateWithGraphicsFont(
cg_font, text_size, 0, CascadeToLastResortFontDescriptor()));
sk_sp<SkTypeface> return_font(SkCreateTypefaceFromCTFont(ct_font, cg_font));
base::ScopedCFTypeRef<CTFontRef> ct_font_base(loaded_ct_font);
base::ScopedCFTypeRef<CTFontRef> ct_font(CTFontCreateCopyWithAttributes(
ct_font_base, text_size, 0, CascadeToLastResortFontDescriptor()));
sk_sp<SkTypeface> return_font(SkCreateTypefaceFromCTFont(ct_font));
if (!return_font.get())
// 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