[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gfx / test / fontconfig_util_linux.cc
blob609a3fca8bdae1d6acdc58557769b65b4ea423fe
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/test/fontconfig_util_linux.h"
7 #include <fontconfig/fontconfig.h>
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
14 namespace gfx {
16 const char* const kSystemFontsForFontconfig[] = {
17 "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
18 "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
19 "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
20 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
21 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
22 "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
23 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
24 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
25 "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
26 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
27 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
28 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
29 "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
30 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
31 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
32 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
33 "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
34 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
35 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
36 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
37 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
38 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
39 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
40 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
41 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
42 "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
43 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
44 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
45 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
46 // The DejaVuSans font is used by the css2.1 tests.
47 "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
48 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf",
49 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
50 "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
53 const size_t kNumSystemFontsForFontconfig =
54 arraysize(kSystemFontsForFontconfig);
56 const char kFontconfigFileHeader[] =
57 "<?xml version=\"1.0\"?>\n"
58 "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
59 "<fontconfig>\n";
60 const char kFontconfigFileFooter[] = "</fontconfig>";
61 const char kFontconfigMatchFontHeader[] = " <match target=\"font\">\n";
62 const char kFontconfigMatchPatternHeader[] = " <match target=\"pattern\">\n";
63 const char kFontconfigMatchFooter[] = " </match>\n";
65 void SetUpFontconfig() {
66 FcInit();
68 // A primer on undocumented FcConfig reference-counting:
70 // - FcConfigCreate() creates a config with a refcount of 1.
71 // - FcConfigReference() increments a config's refcount.
72 // - FcConfigDestroy() decrements a config's refcount, deallocating the
73 // config when the count reaches 0.
74 // - FcConfigSetCurrent() calls FcConfigDestroy() on the old config, but
75 // interestingly does not call FcConfigReference() on the new config.
76 CHECK(FcConfigSetCurrent(FcConfigCreate()));
79 void TearDownFontconfig() {
80 FcFini();
83 bool LoadFontIntoFontconfig(const base::FilePath& path) {
84 if (!base::PathExists(path)) {
85 LOG(ERROR) << "You are missing " << path.value() << ". Try re-running "
86 << "build/install-build-deps.sh. Also see "
87 << "http://code.google.com/p/chromium/wiki/LayoutTestsLinux";
88 return false;
91 if (!FcConfigAppFontAddFile(
92 NULL, reinterpret_cast<const FcChar8*>(path.value().c_str()))) {
93 LOG(ERROR) << "Failed to load font " << path.value();
94 return false;
97 return true;
100 bool LoadSystemFontIntoFontconfig(const std::string& basename) {
101 for (size_t i = 0; i < kNumSystemFontsForFontconfig; ++i) {
102 base::FilePath path(kSystemFontsForFontconfig[i]);
103 if (base::EqualsCaseInsensitiveASCII(path.BaseName().value(), basename))
104 return LoadFontIntoFontconfig(path);
106 LOG(ERROR) << "Unable to find system font named " << basename;
107 return false;
110 bool LoadConfigFileIntoFontconfig(const base::FilePath& path) {
111 // Unlike other FcConfig functions, FcConfigParseAndLoad() doesn't default to
112 // the current config when passed NULL. So that's cool.
113 if (!FcConfigParseAndLoad(FcConfigGetCurrent(),
114 reinterpret_cast<const FcChar8*>(path.value().c_str()), FcTrue)) {
115 LOG(ERROR) << "Fontconfig failed to load " << path.value();
116 return false;
118 return true;
121 bool LoadConfigDataIntoFontconfig(const base::FilePath& temp_dir,
122 const std::string& data) {
123 base::FilePath path;
124 if (!CreateTemporaryFileInDir(temp_dir, &path)) {
125 PLOG(ERROR) << "Unable to create temporary file in " << temp_dir.value();
126 return false;
128 if (base::WriteFile(path, data.data(), data.size()) !=
129 static_cast<int>(data.size())) {
130 PLOG(ERROR) << "Unable to write config data to " << path.value();
131 return false;
133 return LoadConfigFileIntoFontconfig(path);
136 std::string CreateFontconfigEditStanza(const std::string& name,
137 const std::string& type,
138 const std::string& value) {
139 return base::StringPrintf(
140 " <edit name=\"%s\" mode=\"assign\">\n"
141 " <%s>%s</%s>\n"
142 " </edit>\n",
143 name.c_str(), type.c_str(), value.c_str(), type.c_str());
146 std::string CreateFontconfigTestStanza(const std::string& name,
147 const std::string& op,
148 const std::string& type,
149 const std::string& value) {
150 return base::StringPrintf(
151 " <test name=\"%s\" compare=\"%s\" qual=\"any\">\n"
152 " <%s>%s</%s>\n"
153 " </test>\n",
154 name.c_str(), op.c_str(), type.c_str(), value.c_str(), type.c_str());
157 std::string CreateFontconfigAliasStanza(const std::string& original_family,
158 const std::string& preferred_family) {
159 return base::StringPrintf(
160 " <alias>\n"
161 " <family>%s</family>\n"
162 " <prefer><family>%s</family></prefer>\n"
163 " </alias>\n",
164 original_family.c_str(), preferred_family.c_str());
167 } // namespace gfx