From f6c1cd2bb6a2253e9547c091671c98ed28300685 Mon Sep 17 00:00:00 2001 From: steimel Date: Mon, 14 Nov 2016 16:40:55 -0800 Subject: [PATCH] Add skia extension to allow setting default fontmgr on linux. Use it to allow the linux blimp client to use android fonts BUG=617821 Review-Url: https://codereview.chromium.org/2500643002 Cr-Commit-Position: refs/heads/master@{#432023} --- blimp/client/app/linux/blimp_main.cc | 49 +++++++++++++++++++ .../core/switches/blimp_client_switches.cc | 2 + .../core/switches/blimp_client_switches.h | 3 ++ skia/BUILD.gn | 3 +- skia/ext/fontmgr_default_linux.cc | 25 ++++++++++ skia/ext/fontmgr_default_linux.h | 15 ++++++ 6 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 skia/ext/fontmgr_default_linux.cc create mode 100644 skia/ext/fontmgr_default_linux.h diff --git a/blimp/client/app/linux/blimp_main.cc b/blimp/client/app/linux/blimp_main.cc index 2223836e230bfd..9f7aaac5df97d4 100644 --- a/blimp/client/app/linux/blimp_main.cc +++ b/blimp/client/app/linux/blimp_main.cc @@ -14,6 +14,7 @@ #include "blimp/client/app/linux/blimp_display_manager.h" #include "blimp/client/app/linux/blimp_display_manager_delegate_main.h" #include "blimp/client/core/settings/settings_prefs.h" +#include "blimp/client/core/switches/blimp_client_switches.h" #include "blimp/client/public/blimp_client_context.h" #include "blimp/client/public/contents/blimp_navigation_controller.h" #include "blimp/client/support/compositor/compositor_dependencies_impl.h" @@ -22,6 +23,10 @@ #include "components/prefs/in_memory_pref_store.h" #include "components/prefs/pref_service.h" #include "components/prefs/pref_service_factory.h" +#include "skia/ext/fontmgr_default_linux.h" +#include "third_party/skia/include/ports/SkFontConfigInterface.h" +#include "third_party/skia/include/ports/SkFontMgr.h" +#include "third_party/skia/include/ports/SkFontMgr_android.h" #include "ui/gfx/x/x11_connection.h" namespace { @@ -39,11 +44,55 @@ class BlimpShellCommandLinePrefStore : public CommandLinePrefStore { protected: ~BlimpShellCommandLinePrefStore() override = default; }; + +bool HasAndroidFontSwitch() { + return base::CommandLine::ForCurrentProcess()->HasSwitch( + blimp::switches::kAndroidFontsPath); +} + +std::string GetAndroidFontsDirectory() { + std::string android_fonts_dir = + base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( + blimp::switches::kAndroidFontsPath); + if (android_fonts_dir.size() > 0 && android_fonts_dir.back() != '/') { + android_fonts_dir += '/'; + } + return android_fonts_dir; +} + +sk_sp CreateAndroidFontMgr(std::string android_fonts_dir) { + SkFontMgr_Android_CustomFonts custom; + custom.fSystemFontUse = + SkFontMgr_Android_CustomFonts::SystemFontUse::kOnlyCustom; + custom.fBasePath = android_fonts_dir.c_str(); + + std::string font_config; + std::string fallback_font_config; + if (android_fonts_dir.find("kitkat") != std::string::npos) { + font_config = android_fonts_dir + "system_fonts.xml"; + fallback_font_config = android_fonts_dir + "fallback_fonts.xml"; + custom.fFallbackFontsXml = fallback_font_config.c_str(); + } else { + font_config = android_fonts_dir + "fonts.xml"; + custom.fFallbackFontsXml = nullptr; + } + custom.fFontsXml = font_config.c_str(); + custom.fIsolated = true; + + return sk_sp(SkFontMgr_New_Android(&custom)); +} + +void SetupAndroidFontManager() { + if (HasAndroidFontSwitch()) { + SetDefaultSkiaFactory(CreateAndroidFontMgr(GetAndroidFontsDirectory())); + } +} } // namespace int main(int argc, const char**argv) { base::AtExitManager at_exit; base::CommandLine::Init(argc, argv); + SetupAndroidFontManager(); CHECK(gfx::InitializeThreadedX11()); diff --git a/blimp/client/core/switches/blimp_client_switches.cc b/blimp/client/core/switches/blimp_client_switches.cc index 3f56f5f7c8d8c5..6692d361352ab9 100644 --- a/blimp/client/core/switches/blimp_client_switches.cc +++ b/blimp/client/core/switches/blimp_client_switches.cc @@ -23,5 +23,7 @@ const char kEngineTransport[] = "engine-transport"; const char kDownloadWholeDocument[] = "download-whole-document"; +const char kAndroidFontsPath[] = "android-fonts-path"; + } // namespace switches } // namespace blimp diff --git a/blimp/client/core/switches/blimp_client_switches.h b/blimp/client/core/switches/blimp_client_switches.h index 4a0658f16307d6..d376a9684da1db 100644 --- a/blimp/client/core/switches/blimp_client_switches.h +++ b/blimp/client/core/switches/blimp_client_switches.h @@ -36,6 +36,9 @@ extern const char kEngineTransport[]; // Enables downloading the complete page from the engine. extern const char kDownloadWholeDocument[]; +// Specifies directory where android fonts are stored for use in Linux client. +extern const char kAndroidFontsPath[]; + } // namespace switches } // namespace blimp diff --git a/skia/BUILD.gn b/skia/BUILD.gn index e74662610d4597..44ae7853b176c0 100644 --- a/skia/BUILD.gn +++ b/skia/BUILD.gn @@ -209,6 +209,7 @@ component("skia") { "ext/benchmarking_canvas.cc", "ext/convolver.cc", "ext/event_tracer_impl.cc", + "ext/fontmgr_default_linux.cc", "ext/fontmgr_default_win.cc", "ext/google_logging.cc", "ext/image_operations.cc", @@ -251,7 +252,6 @@ component("skia") { "//third_party/skia/src/ports/SkFontHost_mac.cpp", "//third_party/skia/src/ports/SkFontHost_win.cpp", "//third_party/skia/src/ports/SkFontMgr_FontConfigInterface.cpp", - "//third_party/skia/src/ports/SkFontMgr_FontConfigInterface_factory.cpp", "//third_party/skia/src/ports/SkFontMgr_android.cpp", "//third_party/skia/src/ports/SkFontMgr_android_factory.cpp", "//third_party/skia/src/ports/SkFontMgr_android_parser.cpp", @@ -368,7 +368,6 @@ component("skia") { "//third_party/skia/src/ports/SkFontConfigInterface_direct.cpp", "//third_party/skia/src/ports/SkFontConfigInterface_direct_factory.cpp", "//third_party/skia/src/ports/SkFontMgr_FontConfigInterface.cpp", - "//third_party/skia/src/ports/SkFontMgr_FontConfigInterface_factory.cpp", ] } diff --git a/skia/ext/fontmgr_default_linux.cc b/skia/ext/fontmgr_default_linux.cc new file mode 100644 index 00000000000000..8ec030fe61d710 --- /dev/null +++ b/skia/ext/fontmgr_default_linux.cc @@ -0,0 +1,25 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/fontmgr_default_linux.h" + +#include "third_party/skia/include/ports/SkFontConfigInterface.h" +#include "third_party/skia/include/ports/SkFontMgr.h" +#include "third_party/skia/include/ports/SkFontMgr_FontConfigInterface.h" + +namespace { +sk_sp g_default_fontmgr; +} // namespace + +void SetDefaultSkiaFactory(sk_sp fontmgr) { + g_default_fontmgr = fontmgr; +} + +SK_API SkFontMgr* SkFontMgr::Factory() { + if (g_default_fontmgr) { + return SkRef(g_default_fontmgr.get()); + } + sk_sp fci(SkFontConfigInterface::RefGlobal()); + return fci ? SkFontMgr_New_FCI(std::move(fci)) : nullptr; +} diff --git a/skia/ext/fontmgr_default_linux.h b/skia/ext/fontmgr_default_linux.h new file mode 100644 index 00000000000000..dc39b139e0329f --- /dev/null +++ b/skia/ext/fontmgr_default_linux.h @@ -0,0 +1,15 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_FONTMGR_DEFAULT_LINUX_H_ +#define SKIA_EXT_FONTMGR_DEFAULT_LINUX_H_ + +#include "third_party/skia/include/core/SkRefCnt.h" +#include "third_party/skia/include/core/SkTypes.h" + +class SkFontMgr; + +void SK_API SetDefaultSkiaFactory(sk_sp fontmgr); + +#endif // SKIA_EXT_FONTMGR_DEFAULT_LINUX_H_