• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
23 #include "txt/typeface_font_asset_provider.h"
24 #include "utils/WindowsUtils.h"
25 
26 #if !defined(_WIN32)
27 #include <dirent.h>
28 #endif
29 
30 namespace txt {
31 
32 static std::string gFontDir;
33 static fml::CommandLine gCommandLine;
34 
GetFontDir()35 const std::string& GetFontDir() {
36   return gFontDir;
37 }
38 
SetFontDir(const std::string & dir)39 void SetFontDir(const std::string& dir) {
40   gFontDir = dir;
41 }
42 
GetCommandLineForProcess()43 const fml::CommandLine& GetCommandLineForProcess() {
44   return gCommandLine;
45 }
46 
SetCommandLine(fml::CommandLine cmd)47 void SetCommandLine(fml::CommandLine cmd) {
48   gCommandLine = std::move(cmd);
49 }
50 
RegisterFontsFromPath(TypefaceFontAssetProvider & font_provider,std::string directory_path)51 void RegisterFontsFromPath(TypefaceFontAssetProvider& font_provider,
52                            std::string directory_path) {
53 #if defined(_WIN32)
54   std::string path = directory_path + "\\*";
55   WIN32_FIND_DATAA ffd;
56   HANDLE directory = FindFirstFileA(path.c_str(), &ffd);
57   if (directory == INVALID_HANDLE_VALUE) {
58     return;
59   }
60 
61   do {
62     if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
63       continue;
64     }
65 
66     std::string file_name(ffd.cFileName);
67 
68     std::stringstream file_path;
69     file_path << directory_path << "/" << file_name;
70 
71     font_provider.RegisterTypeface(
72         SkTypeface::MakeFromFile(file_path.str().c_str()));
73   } while (FindNextFileA(directory, &ffd) != 0);
74 
75   // TODO(bkonyi): check for error here?
76   FindClose(directory);
77 #else
78   auto directory_closer = [](DIR* directory) {
79     if (directory != nullptr) {
80       ::closedir(directory);
81     }
82   };
83 
84   std::unique_ptr<DIR, decltype(directory_closer)> directory(
85       ::opendir(directory_path.c_str()), directory_closer);
86 
87   if (directory == nullptr) {
88     return;
89   }
90 
91   for (struct dirent* entry = ::readdir(directory.get()); entry != nullptr;
92        entry = ::readdir(directory.get())) {
93     if (entry->d_type != DT_REG) {
94       continue;
95     }
96 
97     std::string file_name(entry->d_name);
98 
99     std::stringstream file_path;
100     file_path << directory_path << "/" << file_name;
101 
102     font_provider.RegisterTypeface(
103         SkTypeface::MakeFromFile(file_path.str().c_str()));
104   }
105 #endif
106 }
107 
GetTestFontCollection()108 std::shared_ptr<FontCollection> GetTestFontCollection() {
109   std::unique_ptr<TypefaceFontAssetProvider> font_provider =
110       std::make_unique<TypefaceFontAssetProvider>();
111   RegisterFontsFromPath(*font_provider, GetFontDir());
112 
113   std::shared_ptr<FontCollection> collection =
114       std::make_shared<FontCollection>();
115   collection->SetAssetFontManager(
116       sk_make_sp<AssetFontManager>(std::move(font_provider)));
117 
118   return collection;
119 }
120 
121 // Build a paragraph and return it as a ParagraphTxt usable by tests that need
122 // access to ParagraphTxt internals.
BuildParagraph(txt::ParagraphBuilderTxt & builder)123 std::unique_ptr<ParagraphTxt> BuildParagraph(
124     txt::ParagraphBuilderTxt& builder) {
125   return std::unique_ptr<txt::ParagraphTxt>(
126       static_cast<txt::ParagraphTxt*>(builder.Build().release()));
127 }
128 
129 }  // namespace txt
130