summaryrefslogtreecommitdiff
path: root/chromium/skia/ext/test_fonts_fuchsia.cc
blob: 509cfb435736be7ed6fcd9e90c5ff21a21bbce28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 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/test_fonts_fuchsia.h"

#include <fuchsia/fonts/cpp/fidl.h>
#include <fuchsia/io/cpp/fidl.h>
#include <fuchsia/sys/cpp/fidl.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <lib/sys/cpp/component_context.h>

#include "base/fuchsia/file_utils.h"
#include "base/fuchsia/process_context.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "skia/ext/fontmgr_default.h"
#include "skia/ext/test_fonts.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/ports/SkFontMgr_fuchsia.h"

namespace skia {

fuchsia::fonts::ProviderSyncPtr RunTestProviderWithTestFonts(
    fidl::InterfaceHandle<fuchsia::sys::ComponentController>* controller_out) {
  // Start a fuchsia.fonts.Provider instance and configure it to load the test
  // fonts, which must be bundled in the calling process' package.
  fuchsia::sys::LaunchInfo launch_info;
  launch_info.url = "fuchsia-pkg://fuchsia.com/fonts#meta/fonts.cmx";
  launch_info.arguments.emplace(
      {"--no-default-fonts",
       "--font-manifest=/test_fonts/fuchsia_test_fonts_manifest.json"});
  launch_info.flat_namespace = fuchsia::sys::FlatNamespace::New();
  launch_info.flat_namespace->paths.push_back("/test_fonts");

  base::FilePath assets_path;
  if (!base::PathService::Get(base::DIR_ASSETS, &assets_path))
    LOG(FATAL) << "Can't get DIR_ASSETS";
  launch_info.flat_namespace->directories.push_back(
      base::fuchsia::OpenDirectory(assets_path.AppendASCII("test_fonts"))
          .TakeChannel());

  fidl::InterfaceHandle<fuchsia::io::Directory> font_provider_services_dir;
  launch_info.directory_request =
      font_provider_services_dir.NewRequest().TakeChannel();

  fuchsia::sys::LauncherSyncPtr launcher;
  base::ComponentContextForProcess()->svc()->Connect(launcher.NewRequest());
  launcher->CreateComponent(std::move(launch_info),
                            controller_out->NewRequest());

  sys::ServiceDirectory font_provider_services_client(
      std::move(font_provider_services_dir));

  fuchsia::fonts::ProviderSyncPtr provider;
  font_provider_services_client.Connect(provider.NewRequest());
  return provider;
}

void ConfigureTestFont() {
  // ComponentController for the font provider service started below. It's a
  // static field to keep the service running until the test process is
  // destroyed.
  static base::NoDestructor<
      fidl::InterfaceHandle<fuchsia::sys::ComponentController>>
      test_font_provider_controller;
  DCHECK(!*test_font_provider_controller);

  skia::OverrideDefaultSkFontMgr(SkFontMgr_New_Fuchsia(
      RunTestProviderWithTestFonts(test_font_provider_controller.get())));
}

}  // namespace skia