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/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
15 const char* const kSystemFontsForFontconfig[] = {
16 "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
17 "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
18 "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
19 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
20 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
21 "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
22 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
23 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
24 "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
25 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
26 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
27 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
28 "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
29 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
30 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
31 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
32 "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
33 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
34 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
35 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
36 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
37 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
38 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
39 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
40 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
41 "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
42 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
43 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
44 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
45 // The DejaVuSans font is used by the css2.1 tests.
46 "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
47 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf",
48 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
49 "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
52 const size_t kNumSystemFontsForFontconfig =
53 arraysize(kSystemFontsForFontconfig);
55 const char kFontconfigFileHeader[] =
56 "<?xml version=\"1.0\"?>\n"
57 "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
59 const char kFontconfigFileFooter[] = "</fontconfig>";
60 const char kFontconfigMatchHeader[] = " <match>\n";
61 const char kFontconfigMatchFooter[] = " </match>\n";
63 void SetUpFontconfig() {
66 // A primer on undocumented FcConfig reference-counting:
68 // - FcConfigCreate() creates a config with a refcount of 1.
69 // - FcConfigReference() increments a config's refcount.
70 // - FcConfigDestroy() decrements a config's refcount, deallocating the
71 // config when the count reaches 0.
72 // - FcConfigSetCurrent() calls FcConfigDestroy() on the old config, but
73 // interestingly does not call FcConfigReference() on the new config.
74 CHECK(FcConfigSetCurrent(FcConfigCreate()));
77 void TearDownFontconfig() {
81 bool LoadFontIntoFontconfig(const base::FilePath& path) {
82 if (!base::PathExists(path)) {
83 LOG(ERROR) << "You are missing " << path.value() << ". Try re-running "
84 << "build/install-build-deps.sh. Also see "
85 << "http://code.google.com/p/chromium/wiki/LayoutTestsLinux";
89 if (!FcConfigAppFontAddFile(
90 NULL, reinterpret_cast<const FcChar8*>(path.value().c_str()))) {
91 LOG(ERROR) << "Failed to load font " << path.value();
98 bool LoadConfigFileIntoFontconfig(const base::FilePath& path) {
99 // Unlike other FcConfig functions, FcConfigParseAndLoad() doesn't default to
100 // the current config when passed NULL. So that's cool.
101 if (!FcConfigParseAndLoad(FcConfigGetCurrent(),
102 reinterpret_cast<const FcChar8*>(path.value().c_str()), FcTrue)) {
103 LOG(ERROR) << "Fontconfig failed to load " << path.value();
109 bool LoadConfigDataIntoFontconfig(const base::FilePath& temp_dir,
110 const std::string& data) {
112 if (!CreateTemporaryFileInDir(temp_dir, &path)) {
113 PLOG(ERROR) << "Unable to create temporary file in " << temp_dir.value();
116 if (base::WriteFile(path, data.data(), data.size()) !=
117 static_cast<int>(data.size())) {
118 PLOG(ERROR) << "Unable to write config data to " << path.value();
121 return LoadConfigFileIntoFontconfig(path);
124 std::string CreateFontconfigEditStanza(const std::string& name,
125 const std::string& type,
126 const std::string& value) {
127 return base::StringPrintf(
128 " <edit name=\"%s\" mode=\"assign\">\n"
131 name.c_str(), type.c_str(), value.c_str(), type.c_str());
134 std::string CreateFontconfigTestStanza(const std::string& name,
135 const std::string& op,
136 const std::string& type,
137 const std::string& value) {
138 return base::StringPrintf(
139 " <test name=\"%s\" compare=\"%s\" qual=\"any\">\n"
142 name.c_str(), op.c_str(), type.c_str(), value.c_str(), type.c_str());
145 std::string CreateFontconfigAliasStanza(const std::string& original_family,
146 const std::string& preferred_family) {
147 return base::StringPrintf(
149 " <family>%s</family>\n"
150 " <prefer><family>%s</family></prefer>\n"
152 original_family.c_str(), preferred_family.c_str());