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.
6 #include <fontconfig/fontconfig.h>
8 #include <sys/socket.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"
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.
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
:
51 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("en"));
54 // The three baltic languages.
56 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("et"));
57 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("lv"));
58 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("lt"));
60 case NPCharsetChineseBIG5
:
61 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("zh-tw"));
64 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("zh-cn"));
66 case NPCharsetEastEurope
:
67 // A scattering of eastern European languages.
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"));
77 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("el"));
82 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("ko"));
84 case NPCharsetRussian
:
86 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("ru"));
88 case NPCharsetShiftJIS
:
90 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("ja"));
92 case NPCharsetTurkish
:
94 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("tr"));
96 case NPCharsetVietnamese
:
98 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("vi"));
100 case NPCharsetArabic
:
101 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("ar"));
103 case NPCharsetHebrew
:
104 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("he"));
107 FcLangSetAdd(langset
, reinterpret_cast<const FcChar8
*>("th"));
110 // Don't add any languages in that case that we don't recognise the
120 int MatchFontFaceWithFallback(const std::string
& face
,
124 uint32 fallback_family
) {
125 FcLangSet
* langset
= FcLangSetCreate();
126 bool is_lgc
= MSCharSetToFontconfig(langset
, charset
);
127 FcPattern
* pattern
= FcPatternCreate();
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
;
135 switch (fallback_family
) {
136 case PP_BROWSERFONT_TRUSTED_FAMILY_SERIF
:
137 generic_font_name
= "Times New Roman";
139 case PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF
:
140 generic_font_name
= "Arial";
142 case PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE
:
143 generic_font_name
= "Courier New";
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
);
154 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
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
);
163 FcFontSet
* font_set
= FcFontSort(0, pattern
, 0, 0, &result
);
165 int good_enough_index
= -1;
166 bool good_enough_index_set
= false;
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.
175 if (FcPatternGetBool(current
, FC_SCALABLE
, 0, &is_scalable
) !=
182 if (FcPatternGetString(current
, FC_FILE
, 0, &c_filename
) !=
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
190 bool is_sfnt
= false;
191 static const char kSFNTExtensions
[][5] = {".ttf", ".otc", ".TTF", ".ttc",
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.
199 const size_t ext_len
= strlen(kSFNTExtensions
[j
]);
200 if (filename_len
> ext_len
&&
201 memcmp(c_filename
+ filename_len
- ext_len
,
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;
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.
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.
239 HANDLE_EINTR(open(reinterpret_cast<char*>(c_filename
), O_RDONLY
));
245 if (font_fd
== -1 && good_enough_index_set
) {
246 // We didn't find a font that we liked, so we fallback to something
248 FcPattern
* current
= font_set
->fonts
[good_enough_index
];
250 FcPatternGetString(current
, FC_FILE
, 0, &c_filename
);
251 font_fd
= HANDLE_EINTR(open(reinterpret_cast<char*>(c_filename
), O_RDONLY
));
255 FcFontSetDestroy(font_set
);
256 FcPatternDestroy(pattern
);
261 } // namespace content