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/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 kFontconfigMatchFontHeader
[] = " <match target=\"font\">\n";
61 const char kFontconfigMatchPatternHeader
[] = " <match target=\"pattern\">\n";
62 const char kFontconfigMatchFooter
[] = " </match>\n";
64 void SetUpFontconfig() {
67 // A primer on undocumented FcConfig reference-counting:
69 // - FcConfigCreate() creates a config with a refcount of 1.
70 // - FcConfigReference() increments a config's refcount.
71 // - FcConfigDestroy() decrements a config's refcount, deallocating the
72 // config when the count reaches 0.
73 // - FcConfigSetCurrent() calls FcConfigDestroy() on the old config, but
74 // interestingly does not call FcConfigReference() on the new config.
75 CHECK(FcConfigSetCurrent(FcConfigCreate()));
78 void TearDownFontconfig() {
82 bool LoadFontIntoFontconfig(const base::FilePath
& path
) {
83 if (!base::PathExists(path
)) {
84 LOG(ERROR
) << "You are missing " << path
.value() << ". Try re-running "
85 << "build/install-build-deps.sh. Also see "
86 << "http://code.google.com/p/chromium/wiki/LayoutTestsLinux";
90 if (!FcConfigAppFontAddFile(
91 NULL
, reinterpret_cast<const FcChar8
*>(path
.value().c_str()))) {
92 LOG(ERROR
) << "Failed to load font " << path
.value();
99 bool LoadConfigFileIntoFontconfig(const base::FilePath
& path
) {
100 // Unlike other FcConfig functions, FcConfigParseAndLoad() doesn't default to
101 // the current config when passed NULL. So that's cool.
102 if (!FcConfigParseAndLoad(FcConfigGetCurrent(),
103 reinterpret_cast<const FcChar8
*>(path
.value().c_str()), FcTrue
)) {
104 LOG(ERROR
) << "Fontconfig failed to load " << path
.value();
110 bool LoadConfigDataIntoFontconfig(const base::FilePath
& temp_dir
,
111 const std::string
& data
) {
113 if (!CreateTemporaryFileInDir(temp_dir
, &path
)) {
114 PLOG(ERROR
) << "Unable to create temporary file in " << temp_dir
.value();
117 if (base::WriteFile(path
, data
.data(), data
.size()) !=
118 static_cast<int>(data
.size())) {
119 PLOG(ERROR
) << "Unable to write config data to " << path
.value();
122 return LoadConfigFileIntoFontconfig(path
);
125 std::string
CreateFontconfigEditStanza(const std::string
& name
,
126 const std::string
& type
,
127 const std::string
& value
) {
128 return base::StringPrintf(
129 " <edit name=\"%s\" mode=\"assign\">\n"
132 name
.c_str(), type
.c_str(), value
.c_str(), type
.c_str());
135 std::string
CreateFontconfigTestStanza(const std::string
& name
,
136 const std::string
& op
,
137 const std::string
& type
,
138 const std::string
& value
) {
139 return base::StringPrintf(
140 " <test name=\"%s\" compare=\"%s\" qual=\"any\">\n"
143 name
.c_str(), op
.c_str(), type
.c_str(), value
.c_str(), type
.c_str());
146 std::string
CreateFontconfigAliasStanza(const std::string
& original_family
,
147 const std::string
& preferred_family
) {
148 return base::StringPrintf(
150 " <family>%s</family>\n"
151 " <prefer><family>%s</family></prefer>\n"
153 original_family
.c_str(), preferred_family
.c_str());