Skip to content

Commit

Permalink
Move legacyCreateTypeface to SkFontStyle.
Browse files Browse the repository at this point in the history
This allows Chromium and Blink to ask for arbitrary font
weights now, making the transition to SkFontMgr easier.

Review URL: https://codereview.chromium.org/1877673002

Cr-Commit-Position: refs/heads/master@{#386914}
  • Loading branch information
bungeman authored and Commit bot committed Apr 13, 2016
1 parent 8198303 commit 079490e
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 72 deletions.
38 changes: 21 additions & 17 deletions components/font_service/font_service_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@
#include "mojo/platform_handle/platform_handle_functions.h"
#include "services/shell/public/cpp/connection.h"

static_assert(static_cast<uint32_t>(SkTypeface::kNormal) ==
static_cast<uint32_t>(font_service::TypefaceStyle::NORMAL),
static_assert(static_cast<uint32_t>(SkFontStyle::kUpright_Slant) ==
static_cast<uint32_t>(font_service::TypefaceSlant::ROMAN),
"Skia and font service flags must match");
static_assert(static_cast<uint32_t>(SkTypeface::kBold) ==
static_cast<uint32_t>(font_service::TypefaceStyle::BOLD),
static_assert(static_cast<uint32_t>(SkFontStyle::kItalic_Slant) ==
static_cast<uint32_t>(font_service::TypefaceSlant::ITALIC),
"Skia and font service flags must match");
static_assert(static_cast<uint32_t>(SkTypeface::kItalic) ==
static_cast<uint32_t>(font_service::TypefaceStyle::ITALIC),
"Skia and font service flags must match");
static_assert(
static_cast<uint32_t>(SkTypeface::kBoldItalic) ==
static_cast<uint32_t>(font_service::TypefaceStyle::BOLD_ITALIC),
"Skia and font service flags must match");

namespace {

Expand Down Expand Up @@ -75,19 +68,26 @@ void FontServiceApp::Create(mojo::Connection* connection,
}

void FontServiceApp::MatchFamilyName(const mojo::String& family_name,
TypefaceStyle requested_style,
TypefaceStylePtr requested_style,
const MatchFamilyNameCallback& callback) {
SkFontConfigInterface::FontIdentity result_identity;
SkString result_family;
SkTypeface::Style result_style;
SkFontStyle result_style;
SkFontConfigInterface* fc =
SkFontConfigInterface::GetSingletonDirectInterface();
const bool r = fc->matchFamilyName(
family_name.data(), static_cast<SkTypeface::Style>(requested_style),
family_name.data(),
SkFontStyle(requested_style->weight,
requested_style->width,
static_cast<SkFontStyle::Slant>(requested_style->slant)),
&result_identity, &result_family, &result_style);

if (!r) {
callback.Run(nullptr, "", TypefaceStyle::NORMAL);
TypefaceStylePtr style(TypefaceStyle::New());
style->weight = SkFontStyle().weight();
style->width = SkFontStyle().width();
style->slant = static_cast<TypefaceSlant>(SkFontStyle().slant());
callback.Run(nullptr, "", std::move(style));
return;
}

Expand All @@ -100,8 +100,12 @@ void FontServiceApp::MatchFamilyName(const mojo::String& family_name,
identity->ttc_index = result_identity.fTTCIndex;
identity->str_representation = result_identity.fString.c_str();

callback.Run(std::move(identity), result_family.c_str(),
static_cast<TypefaceStyle>(result_style));
TypefaceStylePtr style(TypefaceStyle::New());
style->weight = result_style.weight();
style->width = result_style.width();
style->slant = static_cast<TypefaceSlant>(result_style.slant());

callback.Run(std::move(identity), result_family.c_str(), std::move(style));
}

void FontServiceApp::OpenStream(uint32_t id_number,
Expand Down
2 changes: 1 addition & 1 deletion components/font_service/font_service_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FontServiceApp : public mojo::ShellClient,

// FontService:
void MatchFamilyName(const mojo::String& family_name,
TypefaceStyle requested_style,
TypefaceStylePtr requested_style,
const MatchFamilyNameCallback& callback) override;
void OpenStream(uint32_t id_number,
const OpenStreamCallback& callback) override;
Expand Down
4 changes: 2 additions & 2 deletions components/font_service/public/cpp/font_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ void FontLoader::Shutdown() {
}

bool FontLoader::matchFamilyName(const char family_name[],
SkTypeface::Style requested,
SkFontStyle requested,
FontIdentity* out_font_identifier,
SkString* out_family_name,
SkTypeface::Style* out_style) {
SkFontStyle* out_style) {
TRACE_EVENT1("font_service", "FontServiceThread::MatchFamilyName",
"family_name", family_name);
return thread_->MatchFamilyName(family_name, requested, out_font_identifier,
Expand Down
4 changes: 2 additions & 2 deletions components/font_service/public/cpp/font_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class FontLoader : public SkFontConfigInterface,

// SkFontConfigInterface:
bool matchFamilyName(const char family_name[],
SkTypeface::Style requested,
SkFontStyle requested,
FontIdentity* out_font_identifier,
SkString* out_family_name,
SkTypeface::Style* out_style) override;
SkFontStyle* out_style) override;
SkStreamAsset* openStream(const FontIdentity& identity) override;

private:
Expand Down
23 changes: 15 additions & 8 deletions components/font_service/public/cpp/font_service_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ FontServiceThread::FontServiceThread(FontServicePtr font_service)

bool FontServiceThread::MatchFamilyName(
const char family_name[],
SkTypeface::Style requested_style,
SkFontStyle requested_style,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style) {
SkFontStyle* out_style) {
DCHECK_NE(GetThreadId(), base::PlatformThread::CurrentId());

bool out_valid = false;
Expand Down Expand Up @@ -85,15 +85,20 @@ FontServiceThread::~FontServiceThread() {
void FontServiceThread::MatchFamilyNameImpl(
base::WaitableEvent* done_event,
const char family_name[],
SkTypeface::Style requested_style,
SkFontStyle requested_style,
bool* out_valid,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style) {
SkFontStyle* out_style) {
DCHECK_EQ(GetThreadId(), base::PlatformThread::CurrentId());

TypefaceStylePtr style(TypefaceStyle::New());
style->weight = requested_style.weight();
style->width = requested_style.width();
style->slant = static_cast<TypefaceSlant>(requested_style.slant());

font_service_->MatchFamilyName(
mojo::String(family_name), static_cast<TypefaceStyle>(requested_style),
mojo::String(family_name), std::move(style),
base::Bind(&FontServiceThread::OnMatchFamilyNameComplete, this,
done_event, out_valid, out_font_identity, out_family_name,
out_style));
Expand All @@ -104,10 +109,10 @@ void FontServiceThread::OnMatchFamilyNameComplete(
bool* out_valid,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style,
SkFontStyle* out_style,
FontIdentityPtr font_identity,
mojo::String family_name,
TypefaceStyle style) {
TypefaceStylePtr style) {
DCHECK_EQ(GetThreadId(), base::PlatformThread::CurrentId());

*out_valid = font_identity;
Expand All @@ -119,7 +124,9 @@ void FontServiceThread::OnMatchFamilyNameComplete(
// behaviour of the current Linux IPC version.

*out_family_name = family_name.data();
*out_style = static_cast<SkTypeface::Style>(style);
*out_style = SkFontStyle(style->weight,
style->width,
static_cast<SkFontStyle::Slant>(style->slant));
}

done_event->Signal();
Expand Down
12 changes: 6 additions & 6 deletions components/font_service/public/cpp/font_service_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class FontServiceThread : public base::Thread,
// These methods are proxies which run on your thread, post a blocking task
// to the FontServiceThread, and wait on an event signaled from the callback.
bool MatchFamilyName(const char family_name[],
SkTypeface::Style requested_style,
SkFontStyle requested_style,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style);
SkFontStyle* out_style);
scoped_refptr<MappedFontFile> OpenStream(
const SkFontConfigInterface::FontIdentity& identity);

Expand All @@ -52,11 +52,11 @@ class FontServiceThread : public base::Thread,
void MatchFamilyNameImpl(
base::WaitableEvent* done_event,
const char family_name[],
SkTypeface::Style requested_style,
SkFontStyle requested_style,
bool* out_valid,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style);
SkFontStyle* out_style);

// Called on the FontServiceThread in response to receiving a message from
// our MatchFamily mojo IPC. This writes the data returned by mojo, and then
Expand All @@ -66,10 +66,10 @@ class FontServiceThread : public base::Thread,
bool* out_valid,
SkFontConfigInterface::FontIdentity* out_font_identity,
SkString* out_family_name,
SkTypeface::Style* out_style,
SkFontStyle* out_style,
FontIdentityPtr font_identity,
mojo::String family_name,
TypefaceStyle style);
TypefaceStylePtr style);

// Implementation of OpenStream; same threading restrictions as MatchFamily.
void OpenStreamImpl(base::WaitableEvent* done_event,
Expand Down
14 changes: 9 additions & 5 deletions components/font_service/public/interfaces/font_service.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

module font_service;

enum TypefaceStyle {
NORMAL = 0,
BOLD = 0x01,
ITALIC = 0x02,
BOLD_ITALIC = 0x03
enum TypefaceSlant {
ROMAN = 0,
ITALIC = 1,
};

struct TypefaceStyle {
uint16 weight;
uint8 width;
TypefaceSlant slant;
};

// A reference to specific font on the font service.
Expand Down
11 changes: 6 additions & 5 deletions content/browser/renderer_host/sandbox_ipc_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,20 @@ void SandboxIPCHandler::HandleFontMatchRequest(
int fd,
base::PickleIterator iter,
const std::vector<base::ScopedFD>& fds) {
uint32_t requested_style;
SkFontStyle requested_style;
std::string family;
if (!iter.ReadString(&family) || !iter.ReadUInt32(&requested_style))
if (!iter.ReadString(&family) ||
!skia::ReadSkFontStyle(&iter, &requested_style))
return;

SkFontConfigInterface::FontIdentity result_identity;
SkString result_family;
SkTypeface::Style result_style;
SkFontStyle result_style;
SkFontConfigInterface* fc =
SkFontConfigInterface::GetSingletonDirectInterface();
const bool r =
fc->matchFamilyName(family.c_str(),
static_cast<SkTypeface::Style>(requested_style),
requested_style,
&result_identity,
&result_family,
&result_style);
Expand All @@ -202,7 +203,7 @@ void SandboxIPCHandler::HandleFontMatchRequest(
reply.WriteBool(true);
skia::WriteSkString(&reply, result_family);
skia::WriteSkFontIdentity(&reply, result_identity);
reply.WriteUInt32(result_style);
skia::WriteSkFontStyle(&reply, result_style);
}
SendRendererReply(fds, reply, -1);
}
Expand Down
2 changes: 1 addition & 1 deletion content/child/font_warmup_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class TestSkFontMgr : public SkFontMgr {
}

SkTypeface* onLegacyCreateTypeface(const char familyName[],
unsigned styleBits) const override {
SkFontStyle style) const override {
ADD_FAILURE();
return nullptr;
}
Expand Down
12 changes: 6 additions & 6 deletions content/common/font_config_ipc_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ FontConfigIPC::~FontConfigIPC() {
}

bool FontConfigIPC::matchFamilyName(const char familyName[],
SkTypeface::Style requestedStyle,
SkFontStyle requestedStyle,
FontIdentity* outFontIdentity,
SkString* outFamilyName,
SkTypeface::Style* outStyle) {
SkFontStyle* outStyle) {
TRACE_EVENT0("sandbox_ipc", "FontConfigIPC::matchFamilyName");
size_t familyNameLen = familyName ? strlen(familyName) : 0;
if (familyNameLen > kMaxFontFamilyLength)
Expand All @@ -76,7 +76,7 @@ bool FontConfigIPC::matchFamilyName(const char familyName[],
base::Pickle request;
request.WriteInt(METHOD_MATCH);
request.WriteData(familyName, familyNameLen);
request.WriteUInt32(requestedStyle);
skia::WriteSkFontStyle(&request, requestedStyle);

uint8_t reply_buf[2048];
const ssize_t r = base::UnixDomainSocket::SendRecvMsg(
Expand All @@ -94,10 +94,10 @@ bool FontConfigIPC::matchFamilyName(const char familyName[],

SkString reply_family;
FontIdentity reply_identity;
uint32_t reply_style;
SkFontStyle reply_style;
if (!skia::ReadSkString(&iter, &reply_family) ||
!skia::ReadSkFontIdentity(&iter, &reply_identity) ||
!iter.ReadUInt32(&reply_style)) {
!skia::ReadSkFontStyle(&iter, &reply_style)) {
return false;
}

Expand All @@ -106,7 +106,7 @@ bool FontConfigIPC::matchFamilyName(const char familyName[],
if (outFamilyName)
*outFamilyName = reply_family;
if (outStyle)
*outStyle = static_cast<SkTypeface::Style>(reply_style);
*outStyle = reply_style;

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions content/common/font_config_ipc_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class FontConfigIPC : public SkFontConfigInterface {
~FontConfigIPC() override;

bool matchFamilyName(const char familyName[],
SkTypeface::Style requested,
SkFontStyle requested,
FontIdentity* outFontIdentifier,
SkString* outFamilyName,
SkTypeface::Style* outStyle) override;
SkFontStyle* outStyle) override;

// Returns a new SkTypeface instance or a ref'ed one from the cache. The
// caller should adopt the pointer.
Expand Down
1 change: 0 additions & 1 deletion skia/chromium_skia_defines.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# made to remove these defines as soon as practical. This is in contrast to
# defines in SkUserConfig.h which are normally more permanent.
'chromium_skia_defines': [
'SK_VERY_LEGACY_CREATE_TYPEFACE',
],
},
}
24 changes: 24 additions & 0 deletions skia/ext/skia_utils_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ bool ReadSkFontIdentity(base::PickleIterator* iter,
return true;
}

bool ReadSkFontStyle(base::PickleIterator* iter, SkFontStyle* style) {
uint16_t reply_weight;
uint16_t reply_width;
uint16_t reply_slant;

if (!iter->ReadUInt16(&reply_weight) ||
!iter->ReadUInt16(&reply_width) ||
!iter->ReadUInt16(&reply_slant))
return false;

if (style) {
*style = SkFontStyle(reply_weight,
reply_width,
static_cast<SkFontStyle::Slant>(reply_slant));
}
return true;
}

bool WriteSkString(base::Pickle* pickle, const SkString& str) {
return pickle->WriteData(str.c_str(), str.size());
}
Expand All @@ -51,6 +69,12 @@ bool WriteSkFontIdentity(base::Pickle* pickle,
WriteSkString(pickle, identity.fString);
}

bool WriteSkFontStyle(base::Pickle* pickle, SkFontStyle style) {
return pickle->WriteUInt16(style.weight()) &&
pickle->WriteUInt16(style.width()) &&
pickle->WriteUInt16(style.slant());
}

SkPixelGeometry ComputeDefaultPixelGeometry() {
SkFontHost::LCDOrder order = SkFontHost::GetSubpixelOrder();
if (SkFontHost::kNONE_LCDOrder == order) {
Expand Down
7 changes: 7 additions & 0 deletions skia/ext/skia_utils_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ SK_API bool ReadSkString(base::PickleIterator* iter, SkString* str);
SK_API bool ReadSkFontIdentity(base::PickleIterator* iter,
SkFontConfigInterface::FontIdentity* identity);

// Return true if the pickle/iterator contains a SkFontStyle. If so, and if
// style is not null, copy it into style.
SK_API bool ReadSkFontStyle(base::PickleIterator* iter, SkFontStyle* style);

// Return true if str can be written into the request pickle.
SK_API bool WriteSkString(base::Pickle* pickle, const SkString& str);

Expand All @@ -29,6 +33,9 @@ SK_API bool WriteSkFontIdentity(
base::Pickle* pickle,
const SkFontConfigInterface::FontIdentity& identity);

// Return true if str can be written into the request pickle.
SK_API bool WriteSkFontStyle(base::Pickle* pickle, SkFontStyle style);

// Determine the default pixel geometry (for LCD) by querying the font host
SK_API SkPixelGeometry ComputeDefaultPixelGeometry();

Expand Down
Loading

0 comments on commit 079490e

Please sign in to comment.