From 3186821c7e4448c4b586dde590760e22b46cf166 Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Mon, 6 Nov 2017 12:02:47 -0500 Subject: add a SkFontMgr to DM that returns portable fonts Controlled by --[no]nativeFonts, and still defaults to native fonts. Change-Id: Ib2879e69fadb63ddb5a17a7e4ae227941893b8cf Reviewed-on: https://skia-review.googlesource.com/67806 Reviewed-by: Ben Wagner Commit-Queue: Mike Klein --- BUILD.gn | 1 + dm/DM.cpp | 13 +++++ dm/DMFontMgr.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ dm/DMFontMgr.h | 61 ++++++++++++++++++++++++ src/core/SkFontMgr.cpp | 6 ++- 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 dm/DMFontMgr.cpp create mode 100644 dm/DMFontMgr.h diff --git a/BUILD.gn b/BUILD.gn index 3cf235236c..e7744c9488 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1340,6 +1340,7 @@ if (skia_enable_tools) { test_app("dm") { sources = [ "dm/DM.cpp", + "dm/DMFontMgr.cpp", "dm/DMGpuTestProcs.cpp", "dm/DMJsonWriter.cpp", "dm/DMSrcSink.cpp", diff --git a/dm/DM.cpp b/dm/DM.cpp index cb5332d77a..b7513939be 100644 --- a/dm/DM.cpp +++ b/dm/DM.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "DMFontMgr.h" #include "DMJsonWriter.h" #include "DMSrcSink.h" #include "ProcStats.h" @@ -101,6 +102,9 @@ DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution"); DEFINE_string(dont_write, "", "File extensions to skip writing to --writePath."); // See skia:6821 +DEFINE_bool(nativeFonts, true, "If true, use native font manager and rendering. " + "If false, fonts will draw as portably as possible."); + using namespace DM; using sk_gpu_test::GrContextFactory; using sk_gpu_test::GLTestContext; @@ -1307,9 +1311,18 @@ static sk_sp create_from_name(const char familyName[], SkFontStyle s extern sk_sp (*gCreateTypefaceDelegate)(const char [], SkFontStyle ); +extern sk_sp (*gSkFontMgr_DefaultFactory)(); + + int main(int argc, char** argv) { SkCommandLineFlags::Parse(argc, argv); + if (!FLAGS_nativeFonts) { + gSkFontMgr_DefaultFactory = []() -> sk_sp { + return sk_make_sp(); + }; + } + initializeEventTracingForTools(); #if !defined(GOOGLE3) && defined(SK_BUILD_FOR_IOS) diff --git a/dm/DMFontMgr.cpp b/dm/DMFontMgr.cpp new file mode 100644 index 0000000000..51ee6122d0 --- /dev/null +++ b/dm/DMFontMgr.cpp @@ -0,0 +1,125 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "DMFontMgr.h" +#include "SkFontDescriptor.h" +#include "sk_tool_utils.h" + +namespace DM { + + static constexpr const char* kFamilyNames[] = { + "Toy Liberation Sans", + "Toy Liberation Serif", + "Toy Liberation Mono", + }; + + FontStyleSet::FontStyleSet(int familyIndex) : fFamilyName(kFamilyNames[familyIndex]) {} + + // Each font family has 4 styles: Normal, Bold, Italic, BoldItalic. + int FontStyleSet::count() { return 4; } + void FontStyleSet::getStyle(int index, SkFontStyle* style, SkString* name) { + switch (index) { + default: + case 0: if (style) { *style = SkFontStyle::Normal(); } + if (name) { *name = "Normal"; } + break; + case 1: if (style) { *style = SkFontStyle::Bold(); } + if (name) { *name = "Bold"; } + break; + case 2: if (style) { *style = SkFontStyle::Italic(); } + if (name) { *name = "Italic"; } + break; + case 3: if (style) { *style = SkFontStyle::BoldItalic(); } + if (name) { *name = "BoldItalic"; } + break; + } + } + + SkTypeface* FontStyleSet::matchStyle(const SkFontStyle& style) { + return this->matchStyleCSS3(style); + } + + SkTypeface* FontStyleSet::createTypeface(int index) { + SkFontStyle style; + this->getStyle(index, &style, nullptr); + + return sk_tool_utils::create_portable_typeface(fFamilyName, style).release(); + } + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // + + int FontMgr::onCountFamilies() const { return SK_ARRAY_COUNT(kFamilyNames); } + + void FontMgr::onGetFamilyName(int index, SkString* familyName) const { + *familyName = kFamilyNames[index]; + } + + SkFontStyleSet* FontMgr::onCreateStyleSet(int index) const { + return new FontStyleSet(index); + } + + SkFontStyleSet* FontMgr::onMatchFamily(const char familyName[]) const { + if (familyName) { + if (strstr(familyName, "ans")) { return this->createStyleSet(0); } + if (strstr(familyName, "erif")) { return this->createStyleSet(1); } + if (strstr(familyName, "ono")) { return this->createStyleSet(2); } + } + return this->createStyleSet(0); + } + + SkTypeface* FontMgr::onMatchFamilyStyle(const char familyName[], + const SkFontStyle& style) const { + sk_sp styleSet(this->matchFamily(familyName)); + return styleSet->matchStyle(style); + } + + SkTypeface* FontMgr::onMatchFamilyStyleCharacter(const char familyName[], + const SkFontStyle& style, + const char* bcp47[], + int bcp47Count, + SkUnichar character) const { + (void)bcp47; + (void)bcp47Count; + (void)character; + return this->matchFamilyStyle(familyName, style); + } + + SkTypeface* FontMgr::onMatchFaceStyle(const SkTypeface* tf, + const SkFontStyle& style) const { + SkString familyName; + tf->getFamilyName(&familyName); + return this->matchFamilyStyle(familyName.c_str(), style); + } + + sk_sp FontMgr::onMakeFromData(sk_sp, int ttcIndex) const { + return sk_sp(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); + } + + sk_sp FontMgr::onMakeFromStreamIndex(std::unique_ptr, + int ttcIndex) const { + return sk_sp(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); + } + + sk_sp FontMgr::onMakeFromStreamArgs(std::unique_ptr, + const SkFontArguments&) const { + return sk_sp(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); + } + + sk_sp FontMgr::onMakeFromFontData(std::unique_ptr) const { + return sk_sp(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); + } + + sk_sp FontMgr::onMakeFromFile(const char path[], int ttcIndex) const { + return sk_sp(this->matchFamilyStyle("Sans", SkFontStyle::Normal())); + } + + sk_sp FontMgr::onLegacyMakeTypeface(const char familyName[], + SkFontStyle style) const { + return sk_sp(this->matchFamilyStyle(familyName, style)); + } + +} // namespace DM diff --git a/dm/DMFontMgr.h b/dm/DMFontMgr.h new file mode 100644 index 0000000000..b2eb0ac864 --- /dev/null +++ b/dm/DMFontMgr.h @@ -0,0 +1,61 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef DMFontMgr_DEFINED +#define DMFontMgr_DEFINED + +#include "SkFontMgr.h" + +// An SkFontMgr that always uses sk_tool_utils::create_portable_typeface(). + +namespace DM { + + // Returned by DM::FontMgr below. + class FontStyleSet final : public SkFontStyleSet { + public: + explicit FontStyleSet(int familyIndex); + + int count() override; + void getStyle(int index, SkFontStyle* style, SkString* name) override; + SkTypeface* createTypeface(int index) override; + SkTypeface* matchStyle(const SkFontStyle& pattern) override; + + private: + const char* fFamilyName; + }; + + struct FontMgr final : public SkFontMgr { + + int onCountFamilies() const override; + void onGetFamilyName(int index, SkString* familyName) const override; + + SkFontStyleSet* onCreateStyleSet(int index) const override; + SkFontStyleSet* onMatchFamily(const char familyName[]) const override; + + SkTypeface* onMatchFamilyStyle(const char familyName[], + const SkFontStyle&) const override; + SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], + const SkFontStyle&, + const char* bcp47[], int bcp47Count, + SkUnichar character) const override; + SkTypeface* onMatchFaceStyle(const SkTypeface*, + const SkFontStyle&) const override; + + sk_sp onMakeFromData(sk_sp, int ttcIndex) const override; + sk_sp onMakeFromStreamIndex(std::unique_ptr, + int ttcIndex) const override; + sk_sp onMakeFromStreamArgs(std::unique_ptr, + const SkFontArguments&) const override; + sk_sp onMakeFromFontData(std::unique_ptr) const override; + sk_sp onMakeFromFile(const char path[], int ttcIndex) const override; + + sk_sp onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override; + }; + +} // namespace DM + +#endif//DMFontMgr_DEFINED diff --git a/src/core/SkFontMgr.cpp b/src/core/SkFontMgr.cpp index 33eb2301bd..4e64de1186 100644 --- a/src/core/SkFontMgr.cpp +++ b/src/core/SkFontMgr.cpp @@ -172,12 +172,16 @@ sk_sp SkFontMgr::onMakeFromFontData(std::unique_ptr data return this->makeFromStream(data->detachStream(), data->getIndex()); } +// A global function pointer that's not declared, but can be overriden at startup by test tools. +sk_sp (*gSkFontMgr_DefaultFactory)() = nullptr; + sk_sp SkFontMgr::RefDefault() { static SkOnce once; static sk_sp singleton; once([]{ - sk_sp fm = SkFontMgr::Factory(); + sk_sp fm = gSkFontMgr_DefaultFactory ? gSkFontMgr_DefaultFactory() + : SkFontMgr::Factory(); singleton = fm ? std::move(fm) : sk_make_sp(); }); return singleton; -- cgit v1.2.3