blob: fcb309ea61b3e185045bae991ec5268b4d0bcc6c [file] [log] [blame]
reed@google.comb1c65b62013-02-26 15:50:51 +00001/*
2 * Copyright 2009 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8/* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */
9
reed@google.comb1c65b62013-02-26 15:50:51 +000010#include <string>
11#include <unistd.h>
12#include <fcntl.h>
13
14#include <fontconfig/fontconfig.h>
15
16#include "SkFontConfigInterface.h"
reed@google.comb1c65b62013-02-26 15:50:51 +000017#include "SkStream.h"
18
19class SkFontConfigInterfaceDirect : public SkFontConfigInterface {
20public:
21 SkFontConfigInterfaceDirect();
22 virtual ~SkFontConfigInterfaceDirect();
23
reed@google.comf71a2332013-02-27 19:06:30 +000024 virtual bool matchFamilyName(const char familyName[],
25 SkTypeface::Style requested,
26 FontIdentity* outFontIdentifier,
27 SkString* outFamilyName,
28 SkTypeface::Style* outStyle) SK_OVERRIDE;
29 virtual SkStream* openStream(const FontIdentity&) SK_OVERRIDE;
reed@google.comb1c65b62013-02-26 15:50:51 +000030
31private:
32 SkMutex mutex_;
reed@google.comb1c65b62013-02-26 15:50:51 +000033};
34
reed@google.comd66045e2013-03-04 19:07:02 +000035SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
36 static SkFontConfigInterface* gDirect;
37 if (NULL == gDirect) {
38 gDirect = new SkFontConfigInterfaceDirect;
39 }
40 return gDirect;
reed@google.comb1c65b62013-02-26 15:50:51 +000041}
42
robertphillips@google.com21db1db2013-04-09 23:56:51 +000043#if 0
reed@google.com54c69142013-04-09 15:54:52 +000044int SkFontConfigInterface::countFamilies() { return 0; }
45
46int SkFontConfigInterface::getFamilySet(int index, SkString* outFamilyName,
47 FontIdentity outIdentities[],
48 int maxCount) {
49 return 0;
50}
51
52int SkFontConfigInterface::matchFamilySet(const char familyName[],
53 SkString* outFamilyName,
54 FontIdentity outIdentities[],
55 int maxCount) {
56 return 0;
57}
robertphillips@google.com21db1db2013-04-09 23:56:51 +000058#endif
reed@google.com54c69142013-04-09 15:54:52 +000059
60///////////////////////////////////////////////////////////////////////////////
61
reed@google.comb1c65b62013-02-26 15:50:51 +000062namespace {
63
64// Equivalence classes, used to match the Liberation and other fonts
65// with their metric-compatible replacements. See the discussion in
66// GetFontEquivClass().
67enum FontEquivClass
68{
69 OTHER,
70 SANS,
71 SERIF,
72 MONO,
73 SYMBOL,
74 PGOTHIC,
75 GOTHIC,
76 PMINCHO,
77 MINCHO,
78 SIMSUN,
79 NSIMSUN,
80 SIMHEI,
81 PMINGLIU,
82 MINGLIU,
83 PMINGLIUHK,
84 MINGLIUHK,
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +000085 CAMBRIA,
reed@google.comb1c65b62013-02-26 15:50:51 +000086};
87
88// Match the font name against a whilelist of fonts, returning the equivalence
89// class.
90FontEquivClass GetFontEquivClass(const char* fontname)
91{
92 // It would be nice for fontconfig to tell us whether a given suggested
93 // replacement is a "strong" match (that is, an equivalent font) or
94 // a "weak" match (that is, fontconfig's next-best attempt at finding a
95 // substitute). However, I played around with the fontconfig API for
96 // a good few hours and could not make it reveal this information.
97 //
98 // So instead, we hardcode. Initially this function emulated
99 // /etc/fonts/conf.d/30-metric-aliases.conf
100 // from my Ubuntu system, but we're better off being very conservative.
101
102 // Arimo, Tinos and Cousine are a set of fonts metric-compatible with
103 // Arial, Times New Roman and Courier New with a character repertoire
104 // much larger than Liberation. Note that Cousine is metrically
105 // compatible with Courier New, but the former is sans-serif while
106 // the latter is serif.
107
108
109 struct FontEquivMap {
110 FontEquivClass clazz;
111 const char name[40];
112 };
113
114 static const FontEquivMap kFontEquivMap[] = {
115 { SANS, "Arial" },
116 { SANS, "Arimo" },
117 { SANS, "Liberation Sans" },
118
119 { SERIF, "Times New Roman" },
120 { SERIF, "Tinos" },
121 { SERIF, "Liberation Serif" },
122
123 { MONO, "Courier New" },
124 { MONO, "Cousine" },
125 { MONO, "Liberation Mono" },
126
127 { SYMBOL, "Symbol" },
128 { SYMBOL, "Symbol Neu" },
129
130 // MS Pゴシック
131 { PGOTHIC, "MS PGothic" },
132 { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
133 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
134 { PGOTHIC, "IPAPGothic" },
135 { PGOTHIC, "MotoyaG04Gothic" },
136
137 // MS ゴシック
138 { GOTHIC, "MS Gothic" },
139 { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 "
140 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
141 { GOTHIC, "IPAGothic" },
142 { GOTHIC, "MotoyaG04GothicMono" },
143
144 // MS P明朝
145 { PMINCHO, "MS PMincho" },
146 { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
147 "\xe6\x98\x8e\xe6\x9c\x9d"},
148 { PMINCHO, "IPAPMincho" },
149 { PMINCHO, "MotoyaG04Mincho" },
150
151 // MS 明朝
152 { MINCHO, "MS Mincho" },
153 { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" },
154 { MINCHO, "IPAMincho" },
155 { MINCHO, "MotoyaG04MinchoMono" },
156
157 // 宋体
158 { SIMSUN, "Simsun" },
159 { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" },
160 { SIMSUN, "MSung GB18030" },
161 { SIMSUN, "Song ASC" },
162
163 // 新宋体
164 { NSIMSUN, "NSimsun" },
165 { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" },
166 { NSIMSUN, "MSung GB18030" },
167 { NSIMSUN, "N Song ASC" },
168
169 // 黑体
170 { SIMHEI, "Simhei" },
171 { SIMHEI, "\xe9\xbb\x91\xe4\xbd\x93" },
172 { SIMHEI, "MYingHeiGB18030" },
173 { SIMHEI, "MYingHeiB5HK" },
174
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000175 // 新細明體
176 { PMINGLIU, "PMingLiU"},
177 { PMINGLIU, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
178 { PMINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000179
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000180 // 細明體
181 { MINGLIU, "MingLiU"},
182 { MINGLIU, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
183 { MINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000184
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000185 // 新細明體
186 { PMINGLIUHK, "PMingLiU_HKSCS"},
187 { PMINGLIUHK, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
188 { PMINGLIUHK, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000189
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000190 // 細明體
191 { MINGLIUHK, "MingLiU_HKSCS"},
192 { MINGLIUHK, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
193 { MINGLIUHK, "MSung B5HK"},
194
195 // Cambria
196 { CAMBRIA, "Cambria" },
197 { CAMBRIA, "Caladea" },
reed@google.comb1c65b62013-02-26 15:50:51 +0000198 };
199
200 static const size_t kFontCount =
201 sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]);
202
203 // TODO(jungshik): If this loop turns out to be hot, turn
204 // the array to a static (hash)map to speed it up.
205 for (size_t i = 0; i < kFontCount; ++i) {
206 if (strcasecmp(kFontEquivMap[i].name, fontname) == 0)
207 return kFontEquivMap[i].clazz;
208 }
209 return OTHER;
210}
211
212
213// Return true if |font_a| and |font_b| are visually and at the metrics
214// level interchangeable.
215bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b)
216{
217 FontEquivClass class_a = GetFontEquivClass(font_a);
218 FontEquivClass class_b = GetFontEquivClass(font_b);
219
220 return class_a != OTHER && class_a == class_b;
221}
222
reed@google.comb1c65b62013-02-26 15:50:51 +0000223// Normally we only return exactly the font asked for. In last-resort
224// cases, the request either doesn't specify a font or is one of the
225// basic font names like "Sans", "Serif" or "Monospace". This function
226// tells you whether a given request is for such a fallback.
227bool IsFallbackFontAllowed(const std::string& family) {
228 const char* family_cstr = family.c_str();
229 return family.empty() ||
230 strcasecmp(family_cstr, "sans") == 0 ||
231 strcasecmp(family_cstr, "serif") == 0 ||
232 strcasecmp(family_cstr, "monospace") == 0;
233}
234
235// Find matching font from |font_set| for the given font family.
236FcPattern* MatchFont(FcFontSet* font_set,
237 FcChar8* post_config_family,
238 const std::string& family) {
239 // Older versions of fontconfig have a bug where they cannot select
240 // only scalable fonts so we have to manually filter the results.
241 FcPattern* match = NULL;
242 for (int i = 0; i < font_set->nfont; ++i) {
243 FcPattern* current = font_set->fonts[i];
244 FcBool is_scalable;
245
246 if (FcPatternGetBool(current, FC_SCALABLE, 0,
247 &is_scalable) != FcResultMatch ||
248 !is_scalable) {
249 continue;
250 }
251
252 // fontconfig can also return fonts which are unreadable
253 FcChar8* c_filename;
254 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
255 continue;
256
257 if (access(reinterpret_cast<char*>(c_filename), R_OK) != 0)
258 continue;
259
260 match = current;
261 break;
262 }
263
264 if (match && !IsFallbackFontAllowed(family)) {
265 bool acceptable_substitute = false;
266 for (int id = 0; id < 255; ++id) {
267 FcChar8* post_match_family;
268 if (FcPatternGetString(match, FC_FAMILY, id, &post_match_family) !=
269 FcResultMatch)
270 break;
271 acceptable_substitute =
272 (strcasecmp(reinterpret_cast<char*>(post_config_family),
273 reinterpret_cast<char*>(post_match_family)) == 0 ||
274 // Workaround for Issue 12530:
275 // requested family: "Bitstream Vera Sans"
276 // post_config_family: "Arial"
277 // post_match_family: "Bitstream Vera Sans"
278 // -> We should treat this case as a good match.
279 strcasecmp(family.c_str(),
280 reinterpret_cast<char*>(post_match_family)) == 0) ||
281 IsMetricCompatibleReplacement(family.c_str(),
282 reinterpret_cast<char*>(post_match_family));
283 if (acceptable_substitute)
284 break;
285 }
286 if (!acceptable_substitute)
287 return NULL;
288 }
289
290 return match;
291}
292
293// Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
reed@google.comf71a2332013-02-27 19:06:30 +0000294SkTypeface::Style GetFontStyle(FcPattern* font) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000295 int resulting_bold;
296 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
297 resulting_bold = FC_WEIGHT_NORMAL;
298
299 int resulting_italic;
300 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
301 resulting_italic = FC_SLANT_ROMAN;
302
303 // If we ask for an italic font, fontconfig might take a roman font and set
304 // the undocumented property FC_MATRIX to a skew matrix. It'll then say
305 // that the font is italic or oblique. So, if we see a matrix, we don't
306 // believe that it's italic.
307 FcValue matrix;
308 const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0;
309
310 // If we ask for an italic font, fontconfig might take a roman font and set
311 // FC_EMBOLDEN.
312 FcValue embolden;
313 const bool have_embolden = FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) == 0;
314
315 int styleBits = 0;
316 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) {
317 styleBits |= SkTypeface::kBold;
318 }
319 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) {
320 styleBits |= SkTypeface::kItalic;
321 }
322
reed@google.comf71a2332013-02-27 19:06:30 +0000323 return (SkTypeface::Style)styleBits;
reed@google.comb1c65b62013-02-26 15:50:51 +0000324}
325
326} // anonymous namespace
327
328///////////////////////////////////////////////////////////////////////////////
329
reed@google.comf71a2332013-02-27 19:06:30 +0000330#define kMaxFontFamilyLength 2048
reed@google.comb1c65b62013-02-26 15:50:51 +0000331
reed@google.comf71a2332013-02-27 19:06:30 +0000332SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() {
reed@google.comb1c65b62013-02-26 15:50:51 +0000333 FcInit();
334}
335
336SkFontConfigInterfaceDirect::~SkFontConfigInterfaceDirect() {
337}
338
reed@google.comf71a2332013-02-27 19:06:30 +0000339bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
340 SkTypeface::Style style,
341 FontIdentity* outIdentity,
342 SkString* outFamilyName,
343 SkTypeface::Style* outStyle) {
reed@google.comee619a02013-02-26 22:58:09 +0000344 std::string familyStr(familyName ? familyName : "");
345 if (familyStr.length() > kMaxFontFamilyLength) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000346 return false;
347 }
348
349 SkAutoMutexAcquire ac(mutex_);
350
reed@google.comb1c65b62013-02-26 15:50:51 +0000351 FcPattern* pattern = FcPatternCreate();
352
reed@google.comee619a02013-02-26 22:58:09 +0000353 if (familyName) {
354 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
355 }
reed@google.comb1c65b62013-02-26 15:50:51 +0000356 FcPatternAddInteger(pattern, FC_WEIGHT,
357 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD
358 : FC_WEIGHT_NORMAL);
359 FcPatternAddInteger(pattern, FC_SLANT,
360 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC
361 : FC_SLANT_ROMAN);
362 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
363
364 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
365 FcDefaultSubstitute(pattern);
366
367 // Font matching:
368 // CSS often specifies a fallback list of families:
369 // font-family: a, b, c, serif;
370 // However, fontconfig will always do its best to find *a* font when asked
371 // for something so we need a way to tell if the match which it has found is
372 // "good enough" for us. Otherwise, we can return NULL which gets piped up
373 // and lets WebKit know to try the next CSS family name. However, fontconfig
374 // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we
375 // wish to support that.
376 //
377 // Thus, if a specific family is requested we set @family_requested. Then we
378 // record two strings: the family name after config processing and the
379 // family name after resolving. If the two are equal, it's a good match.
380 //
381 // So consider the case where a user has mapped Arial to Helvetica in their
382 // config.
383 // requested family: "Arial"
384 // post_config_family: "Helvetica"
385 // post_match_family: "Helvetica"
386 // -> good match
387 //
388 // and for a missing font:
389 // requested family: "Monaco"
390 // post_config_family: "Monaco"
391 // post_match_family: "Times New Roman"
392 // -> BAD match
393 //
394 // However, we special-case fallback fonts; see IsFallbackFontAllowed().
395 FcChar8* post_config_family;
396 FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family);
397
398 FcResult result;
399 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
400 if (!font_set) {
401 FcPatternDestroy(pattern);
402 return false;
403 }
404
reed@google.comee619a02013-02-26 22:58:09 +0000405 FcPattern* match = MatchFont(font_set, post_config_family, familyStr);
reed@google.comb1c65b62013-02-26 15:50:51 +0000406 if (!match) {
407 FcPatternDestroy(pattern);
408 FcFontSetDestroy(font_set);
409 return false;
410 }
411
412 FcPatternDestroy(pattern);
413
reed@google.comf71a2332013-02-27 19:06:30 +0000414 // From here out we just extract our results from 'match'
415
416 if (FcPatternGetString(match, FC_FAMILY, 0, &post_config_family) != FcResultMatch) {
417 FcFontSetDestroy(font_set);
418 return false;
419 }
420
reed@google.comb1c65b62013-02-26 15:50:51 +0000421 FcChar8* c_filename;
422 if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) {
423 FcFontSetDestroy(font_set);
424 return false;
425 }
reed@google.comf71a2332013-02-27 19:06:30 +0000426
reed@google.comb1c65b62013-02-26 15:50:51 +0000427 int face_index;
428 if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) {
429 FcFontSetDestroy(font_set);
430 return false;
431 }
432
reed@google.comb1c65b62013-02-26 15:50:51 +0000433 FcFontSetDestroy(font_set);
434
reed@google.comf71a2332013-02-27 19:06:30 +0000435 if (outIdentity) {
reed@google.com8c9737e2013-03-06 13:06:03 +0000436 outIdentity->fTTCIndex = face_index;
reed@google.comf71a2332013-02-27 19:06:30 +0000437 outIdentity->fString.set((const char*)c_filename);
reed@google.comb1c65b62013-02-26 15:50:51 +0000438 }
reed@google.comf71a2332013-02-27 19:06:30 +0000439 if (outFamilyName) {
440 outFamilyName->set((const char*)post_config_family);
reed@google.comb1c65b62013-02-26 15:50:51 +0000441 }
reed@google.comf71a2332013-02-27 19:06:30 +0000442 if (outStyle) {
443 *outStyle = GetFontStyle(match);
reed@google.comb1c65b62013-02-26 15:50:51 +0000444 }
reed@google.comee619a02013-02-26 22:58:09 +0000445 return true;
reed@google.comb1c65b62013-02-26 15:50:51 +0000446}
447
reed@google.comf71a2332013-02-27 19:06:30 +0000448SkStream* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) {
reed@google.com8c3f84d2013-03-19 13:34:55 +0000449 return SkStream::NewFromFile(identity.fString.c_str());
reed@google.comb1c65b62013-02-26 15:50:51 +0000450}