Add a FrameHostMsg_BeginNavigation IPC
[chromium-blink-merge.git] / content / browser / renderer_host / font_utils_linux.cc
blobeea055127364641fd8337035824c3bb9647d266f
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 <fcntl.h>
6 #include <fontconfig/fontconfig.h>
7 #include <sys/poll.h>
8 #include <sys/socket.h>
9 #include <sys/stat.h>
11 #include "base/posix/eintr_wrapper.h"
12 #include "content/common/sandbox_linux/sandbox_linux.h"
13 #include "content/common/set_process_title.h"
14 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
15 #include "third_party/npapi/bindings/npapi_extensions.h"
17 namespace {
19 // MSCharSetToFontconfig translates a Microsoft charset identifier to a
20 // fontconfig language set by appending to |langset|.
21 // Returns true if |langset| is Latin/Greek/Cyrillic.
22 bool MSCharSetToFontconfig(FcLangSet* langset, unsigned fdwCharSet) {
23 // We have need to translate raw fdwCharSet values into terms that
24 // fontconfig can understand. (See the description of fdwCharSet in the MSDN
25 // documentation for CreateFont:
26 // http://msdn.microsoft.com/en-us/library/dd183499(VS.85).aspx )
28 // Although the argument is /called/ 'charset', the actual values conflate
29 // character sets (which are sets of Unicode code points) and character
30 // encodings (which are algorithms for turning a series of bits into a
31 // series of code points.) Sometimes the values will name a language,
32 // sometimes they'll name an encoding. In the latter case I'm assuming that
33 // they mean the set of code points in the domain of that encoding.
35 // fontconfig deals with ISO 639-1 language codes:
36 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
38 // So, for each of the documented fdwCharSet values I've had to take a
39 // guess at the set of ISO 639-1 languages intended.
41 bool is_lgc = false;
42 switch (fdwCharSet) {
43 case NPCharsetAnsi:
44 // These values I don't really know what to do with, so I'm going to map
45 // them to English also.
46 case NPCharsetDefault:
47 case NPCharsetMac:
48 case NPCharsetOEM:
49 case NPCharsetSymbol:
50 is_lgc = true;
51 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("en"));
52 break;
53 case NPCharsetBaltic:
54 // The three baltic languages.
55 is_lgc = true;
56 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("et"));
57 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lv"));
58 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lt"));
59 break;
60 case NPCharsetChineseBIG5:
61 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("zh-tw"));
62 break;
63 case NPCharsetGB2312:
64 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("zh-cn"));
65 break;
66 case NPCharsetEastEurope:
67 // A scattering of eastern European languages.
68 is_lgc = true;
69 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("pl"));
70 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("cs"));
71 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("sk"));
72 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hu"));
73 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hr"));
74 break;
75 case NPCharsetGreek:
76 is_lgc = true;
77 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("el"));
78 break;
79 case NPCharsetHangul:
80 case NPCharsetJohab:
81 // Korean
82 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ko"));
83 break;
84 case NPCharsetRussian:
85 is_lgc = true;
86 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ru"));
87 break;
88 case NPCharsetShiftJIS:
89 // Japanese
90 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ja"));
91 break;
92 case NPCharsetTurkish:
93 is_lgc = true;
94 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("tr"));
95 break;
96 case NPCharsetVietnamese:
97 is_lgc = true;
98 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("vi"));
99 break;
100 case NPCharsetArabic:
101 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ar"));
102 break;
103 case NPCharsetHebrew:
104 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("he"));
105 break;
106 case NPCharsetThai:
107 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("th"));
108 break;
109 // default:
110 // Don't add any languages in that case that we don't recognise the
111 // constant.
113 return is_lgc;
116 } // namespace
118 namespace content {
120 int MatchFontFaceWithFallback(const std::string& face,
121 bool is_bold,
122 bool is_italic,
123 uint32 charset,
124 uint32 fallback_family) {
125 FcLangSet* langset = FcLangSetCreate();
126 bool is_lgc = MSCharSetToFontconfig(langset, charset);
127 FcPattern* pattern = FcPatternCreate();
128 FcPatternAddString(
129 pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(face.c_str()));
131 // TODO(thestig) Check if we can access Chrome's per-script font preference
132 // here and select better default fonts for non-LGC case.
133 std::string generic_font_name;
134 if (is_lgc) {
135 switch (fallback_family) {
136 case PP_BROWSERFONT_TRUSTED_FAMILY_SERIF:
137 generic_font_name = "Times New Roman";
138 break;
139 case PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF:
140 generic_font_name = "Arial";
141 break;
142 case PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE:
143 generic_font_name = "Courier New";
144 break;
147 if (!generic_font_name.empty()) {
148 const FcChar8* fc_generic_font_name =
149 reinterpret_cast<const FcChar8*>(generic_font_name.c_str());
150 FcPatternAddString(pattern, FC_FAMILY, fc_generic_font_name);
153 if (is_bold)
154 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
155 if (is_italic)
156 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
157 FcPatternAddLangSet(pattern, FC_LANG, langset);
158 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
159 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
160 FcDefaultSubstitute(pattern);
162 FcResult result;
163 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
164 int font_fd = -1;
165 int good_enough_index = -1;
166 bool good_enough_index_set = false;
168 if (font_set) {
169 for (int i = 0; i < font_set->nfont; ++i) {
170 FcPattern* current = font_set->fonts[i];
172 // Older versions of fontconfig have a bug where they cannot select
173 // only scalable fonts so we have to manually filter the results.
174 FcBool is_scalable;
175 if (FcPatternGetBool(current, FC_SCALABLE, 0, &is_scalable) !=
176 FcResultMatch ||
177 !is_scalable) {
178 continue;
181 FcChar8* c_filename;
182 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) !=
183 FcResultMatch) {
184 continue;
187 // We only want to return sfnt (TrueType) based fonts. We don't have a
188 // very good way of detecting this so we'll filter based on the
189 // filename.
190 bool is_sfnt = false;
191 static const char kSFNTExtensions[][5] = {".ttf", ".otc", ".TTF", ".ttc",
192 ""};
193 const size_t filename_len = strlen(reinterpret_cast<char*>(c_filename));
194 for (unsigned j = 0;; j++) {
195 if (kSFNTExtensions[j][0] == 0) {
196 // None of the extensions matched.
197 break;
199 const size_t ext_len = strlen(kSFNTExtensions[j]);
200 if (filename_len > ext_len &&
201 memcmp(c_filename + filename_len - ext_len,
202 kSFNTExtensions[j],
203 ext_len) == 0) {
204 is_sfnt = true;
205 break;
209 if (!is_sfnt)
210 continue;
212 // This font is good enough to pass muster, but we might be able to do
213 // better with subsequent ones.
214 if (!good_enough_index_set) {
215 good_enough_index = i;
216 good_enough_index_set = true;
219 FcValue matrix;
220 bool have_matrix = FcPatternGet(current, FC_MATRIX, 0, &matrix) == 0;
222 if (is_italic && have_matrix) {
223 // we asked for an italic font, but fontconfig is giving us a
224 // non-italic font with a transformation matrix.
225 continue;
228 FcValue embolden;
229 const bool have_embolden =
230 FcPatternGet(current, FC_EMBOLDEN, 0, &embolden) == 0;
232 if (is_bold && have_embolden) {
233 // we asked for a bold font, but fontconfig gave us a non-bold font
234 // and asked us to apply fake bolding.
235 continue;
238 font_fd =
239 HANDLE_EINTR(open(reinterpret_cast<char*>(c_filename), O_RDONLY));
240 if (font_fd >= 0)
241 break;
245 if (font_fd == -1 && good_enough_index_set) {
246 // We didn't find a font that we liked, so we fallback to something
247 // acceptable.
248 FcPattern* current = font_set->fonts[good_enough_index];
249 FcChar8* c_filename;
250 FcPatternGetString(current, FC_FILE, 0, &c_filename);
251 font_fd = HANDLE_EINTR(open(reinterpret_cast<char*>(c_filename), O_RDONLY));
254 if (font_set)
255 FcFontSetDestroy(font_set);
256 FcPatternDestroy(pattern);
258 return font_fd;
261 } // namespace content