Flutter Engine
txt_test_utils.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Google, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "txt_test_utils.h"
18 
19 #include <sstream>
20 
21 #include "third_party/skia/include/core/SkTypeface.h"
22 #include "txt/asset_font_manager.h"
24 #include "utils/MacUtils.h"
25 #include "utils/WindowsUtils.h"
26 
27 #if !defined(_WIN32)
28 #include <dirent.h>
29 #endif
30 
31 namespace txt {
32 
33 static std::string gFontDir;
35 
36 const std::string& GetFontDir() {
37  return gFontDir;
38 }
39 
40 void SetFontDir(const std::string& dir) {
41  gFontDir = dir;
42 }
43 
45  return gCommandLine;
46 }
47 
49  gCommandLine = std::move(cmd);
50 }
51 
53  std::string directory_path) {
54 #if defined(_WIN32)
55  std::string path = directory_path + "\\*";
56  WIN32_FIND_DATAA ffd;
57  HANDLE directory = FindFirstFileA(path.c_str(), &ffd);
58  if (directory == INVALID_HANDLE_VALUE) {
59  return;
60  }
61 
62  do {
63  if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
64  continue;
65  }
66 
67  std::string file_name(ffd.cFileName);
68 
69  std::stringstream file_path;
70  file_path << directory_path << "/" << file_name;
71 
72  font_provider.RegisterTypeface(
73  SkTypeface::MakeFromFile(file_path.str().c_str()));
74  } while (FindNextFileA(directory, &ffd) != 0);
75 
76  // TODO(bkonyi): check for error here?
77  FindClose(directory);
78 #else
79  auto directory_closer = [](DIR* directory) {
80  if (directory != nullptr) {
81  ::closedir(directory);
82  }
83  };
84 
85  std::unique_ptr<DIR, decltype(directory_closer)> directory(
86  ::opendir(directory_path.c_str()), directory_closer);
87 
88  if (directory == nullptr) {
89  return;
90  }
91 
92  for (struct dirent* entry = ::readdir(directory.get()); entry != nullptr;
93  entry = ::readdir(directory.get())) {
94  if (entry->d_type != DT_REG) {
95  continue;
96  }
97 
98  std::string file_name(entry->d_name);
99 
100  std::stringstream file_path;
101  file_path << directory_path << "/" << file_name;
102 
103  font_provider.RegisterTypeface(
104  SkTypeface::MakeFromFile(file_path.str().c_str()));
105  }
106 #endif
107 }
108 
109 std::shared_ptr<FontCollection> GetTestFontCollection() {
110  std::unique_ptr<TypefaceFontAssetProvider> font_provider =
111  std::make_unique<TypefaceFontAssetProvider>();
112  RegisterFontsFromPath(*font_provider, GetFontDir());
113 
114  std::shared_ptr<FontCollection> collection =
115  std::make_shared<FontCollection>();
116  collection->SetAssetFontManager(
117  sk_make_sp<AssetFontManager>(std::move(font_provider)));
118 
119  return collection;
120 }
121 
122 // Build a paragraph and return it as a ParagraphTxt usable by tests that need
123 // access to ParagraphTxt internals.
124 std::unique_ptr<ParagraphTxt> BuildParagraph(
125  txt::ParagraphBuilderTxt& builder) {
126  return std::unique_ptr<txt::ParagraphTxt>(
127  static_cast<txt::ParagraphTxt*>(builder.Build().release()));
128 }
129 
130 } // namespace txt
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static std::string gFontDir
std::shared_ptr< FontCollection > GetTestFontCollection()
const fml::CommandLine & GetCommandLineForProcess()
void SetFontDir(const std::string &dir)
void RegisterFontsFromPath(TypefaceFontAssetProvider &font_provider, std::string directory_path)
std::unique_ptr< ParagraphTxt > BuildParagraph(txt::ParagraphBuilderTxt &builder)
static fml::CommandLine gCommandLine
const std::string & GetFontDir()
virtual std::unique_ptr< Paragraph > Build() override
void RegisterTypeface(sk_sp< SkTypeface > typeface)
void SetCommandLine(fml::CommandLine cmd)