summaryrefslogtreecommitdiffstats
path: root/ui/gfx/test/fontconfig_util_linux.cc
blob: 3c62bd1a6b0d1a5afd63c2a7eb05f44c062a9dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/test/fontconfig_util_linux.h"

#include <fontconfig/fontconfig.h>

#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"

namespace gfx {

const char* const kSystemFontsForFontconfig[] = {
  "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
  "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
  "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
  // The DejaVuSans font is used by the css2.1 tests.
  "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
  "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf",
  "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
  "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
};

const size_t kNumSystemFontsForFontconfig =
    arraysize(kSystemFontsForFontconfig);

const char kFontconfigFileHeader[] =
    "<?xml version=\"1.0\"?>\n"
    "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
    "<fontconfig>\n";
const char kFontconfigFileFooter[] = "</fontconfig>";
const char kFontconfigMatchFontHeader[] = "  <match target=\"font\">\n";
const char kFontconfigMatchPatternHeader[] = "  <match target=\"pattern\">\n";
const char kFontconfigMatchFooter[] = "  </match>\n";

void SetUpFontconfig() {
  FcInit();

  // A primer on undocumented FcConfig reference-counting:
  //
  // - FcConfigCreate() creates a config with a refcount of 1.
  // - FcConfigReference() increments a config's refcount.
  // - FcConfigDestroy() decrements a config's refcount, deallocating the
  //   config when the count reaches 0.
  // - FcConfigSetCurrent() calls FcConfigDestroy() on the old config, but
  //   interestingly does not call FcConfigReference() on the new config.
  CHECK(FcConfigSetCurrent(FcConfigCreate()));
}

void TearDownFontconfig() {
  FcFini();
}

bool LoadFontIntoFontconfig(const base::FilePath& path) {
  if (!base::PathExists(path)) {
    LOG(ERROR) << "You are missing " << path.value() << ". Try re-running "
               << "build/install-build-deps.sh. Also see "
               << "http://code.google.com/p/chromium/wiki/LayoutTestsLinux";
    return false;
  }

  if (!FcConfigAppFontAddFile(
          NULL, reinterpret_cast<const FcChar8*>(path.value().c_str()))) {
    LOG(ERROR) << "Failed to load font " << path.value();
    return false;
  }

  return true;
}

bool LoadSystemFontIntoFontconfig(const std::string& basename) {
  for (size_t i = 0; i < kNumSystemFontsForFontconfig; ++i) {
    base::FilePath path(kSystemFontsForFontconfig[i]);
    if (base::EqualsCaseInsensitiveASCII(path.BaseName().value(), basename))
      return LoadFontIntoFontconfig(path);
  }
  LOG(ERROR) << "Unable to find system font named " << basename;
  return false;
}

bool LoadConfigFileIntoFontconfig(const base::FilePath& path) {
  // Unlike other FcConfig functions, FcConfigParseAndLoad() doesn't default to
  // the current config when passed NULL. So that's cool.
  if (!FcConfigParseAndLoad(FcConfigGetCurrent(),
          reinterpret_cast<const FcChar8*>(path.value().c_str()), FcTrue)) {
    LOG(ERROR) << "Fontconfig failed to load " << path.value();
    return false;
  }
  return true;
}

bool LoadConfigDataIntoFontconfig(const base::FilePath& temp_dir,
                                  const std::string& data) {
  base::FilePath path;
  if (!CreateTemporaryFileInDir(temp_dir, &path)) {
    PLOG(ERROR) << "Unable to create temporary file in " << temp_dir.value();
    return false;
  }
  if (base::WriteFile(path, data.data(), data.size()) !=
      static_cast<int>(data.size())) {
    PLOG(ERROR) << "Unable to write config data to " << path.value();
    return false;
  }
  return LoadConfigFileIntoFontconfig(path);
}

std::string CreateFontconfigEditStanza(const std::string& name,
                                       const std::string& type,
                                       const std::string& value) {
  return base::StringPrintf(
      "    <edit name=\"%s\" mode=\"assign\">\n"
      "      <%s>%s</%s>\n"
      "    </edit>\n",
      name.c_str(), type.c_str(), value.c_str(), type.c_str());
}

std::string CreateFontconfigTestStanza(const std::string& name,
                                       const std::string& op,
                                       const std::string& type,
                                       const std::string& value) {
  return base::StringPrintf(
      "    <test name=\"%s\" compare=\"%s\" qual=\"any\">\n"
      "      <%s>%s</%s>\n"
      "    </test>\n",
      name.c_str(), op.c_str(), type.c_str(), value.c_str(), type.c_str());
}

std::string CreateFontconfigAliasStanza(const std::string& original_family,
                                        const std::string& preferred_family) {
  return base::StringPrintf(
      "  <alias>\n"
      "    <family>%s</family>\n"
      "    <prefer><family>%s</family></prefer>\n"
      "  </alias>\n",
      original_family.c_str(), preferred_family.c_str());
}

}  // namespace gfx