Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / test / fontconfig_util_linux.cc
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.
4
5 #include "ui/gfx/test/fontconfig_util_linux.h"
6
7 #include <fontconfig/fontconfig.h>
8
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12
13 namespace gfx {
14
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",
50 };
51
52 const size_t kNumSystemFontsForFontconfig =
53     arraysize(kSystemFontsForFontconfig);
54
55 const char kFontconfigFileHeader[] =
56     "<?xml version=\"1.0\"?>\n"
57     "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
58     "<fontconfig>\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";
63
64 void SetUpFontconfig() {
65   FcInit();
66
67   // A primer on undocumented FcConfig reference-counting:
68   //
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()));
76 }
77
78 void TearDownFontconfig() {
79   FcFini();
80 }
81
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";
87     return false;
88   }
89
90   if (!FcConfigAppFontAddFile(
91           NULL, reinterpret_cast<const FcChar8*>(path.value().c_str()))) {
92     LOG(ERROR) << "Failed to load font " << path.value();
93     return false;
94   }
95
96   return true;
97 }
98
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();
105     return false;
106   }
107   return true;
108 }
109
110 bool LoadConfigDataIntoFontconfig(const base::FilePath& temp_dir,
111                                   const std::string& data) {
112   base::FilePath path;
113   if (!CreateTemporaryFileInDir(temp_dir, &path)) {
114     PLOG(ERROR) << "Unable to create temporary file in " << temp_dir.value();
115     return false;
116   }
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();
120     return false;
121   }
122   return LoadConfigFileIntoFontconfig(path);
123 }
124
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"
130       "      <%s>%s</%s>\n"
131       "    </edit>\n",
132       name.c_str(), type.c_str(), value.c_str(), type.c_str());
133 }
134
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"
141       "      <%s>%s</%s>\n"
142       "    </test>\n",
143       name.c_str(), op.c_str(), type.c_str(), value.c_str(), type.c_str());
144 }
145
146 std::string CreateFontconfigAliasStanza(const std::string& original_family,
147                                         const std::string& preferred_family) {
148   return base::StringPrintf(
149       "  <alias>\n"
150       "    <family>%s</family>\n"
151       "    <prefer><family>%s</family></prefer>\n"
152       "  </alias>\n",
153       original_family.c_str(), preferred_family.c_str());
154 }
155
156 }  // namespace gfx