blob: 1e6f2ba9ddd7b5d0cbe83cb97cad7afaccb8135d [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
reed@google.com027fd202013-04-19 20:45:30 +000031 // new APIs
32 virtual SkDataTable* getFamilyNames() SK_OVERRIDE;
33 virtual bool matchFamilySet(const char inFamilyName[],
34 SkString* outFamilyName,
35 SkTArray<FontIdentity>*) SK_OVERRIDE;
36
reed@google.comb1c65b62013-02-26 15:50:51 +000037private:
38 SkMutex mutex_;
reed@google.comb1c65b62013-02-26 15:50:51 +000039};
40
reed@google.comd66045e2013-03-04 19:07:02 +000041SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
42 static SkFontConfigInterface* gDirect;
43 if (NULL == gDirect) {
44 gDirect = new SkFontConfigInterfaceDirect;
45 }
46 return gDirect;
reed@google.comb1c65b62013-02-26 15:50:51 +000047}
48
robertphillips@google.com21db1db2013-04-09 23:56:51 +000049#if 0
reed@google.com54c69142013-04-09 15:54:52 +000050int SkFontConfigInterface::countFamilies() { return 0; }
51
52int SkFontConfigInterface::getFamilySet(int index, SkString* outFamilyName,
53 FontIdentity outIdentities[],
54 int maxCount) {
55 return 0;
56}
57
58int SkFontConfigInterface::matchFamilySet(const char familyName[],
59 SkString* outFamilyName,
60 FontIdentity outIdentities[],
61 int maxCount) {
62 return 0;
63}
robertphillips@google.com21db1db2013-04-09 23:56:51 +000064#endif
reed@google.com54c69142013-04-09 15:54:52 +000065
66///////////////////////////////////////////////////////////////////////////////
67
reed@google.comb1c65b62013-02-26 15:50:51 +000068namespace {
69
70// Equivalence classes, used to match the Liberation and other fonts
71// with their metric-compatible replacements. See the discussion in
72// GetFontEquivClass().
73enum FontEquivClass
74{
75 OTHER,
76 SANS,
77 SERIF,
78 MONO,
79 SYMBOL,
80 PGOTHIC,
81 GOTHIC,
82 PMINCHO,
83 MINCHO,
84 SIMSUN,
85 NSIMSUN,
86 SIMHEI,
87 PMINGLIU,
88 MINGLIU,
89 PMINGLIUHK,
90 MINGLIUHK,
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +000091 CAMBRIA,
reed@google.comb1c65b62013-02-26 15:50:51 +000092};
93
94// Match the font name against a whilelist of fonts, returning the equivalence
95// class.
96FontEquivClass GetFontEquivClass(const char* fontname)
97{
98 // It would be nice for fontconfig to tell us whether a given suggested
99 // replacement is a "strong" match (that is, an equivalent font) or
100 // a "weak" match (that is, fontconfig's next-best attempt at finding a
101 // substitute). However, I played around with the fontconfig API for
102 // a good few hours and could not make it reveal this information.
103 //
104 // So instead, we hardcode. Initially this function emulated
105 // /etc/fonts/conf.d/30-metric-aliases.conf
106 // from my Ubuntu system, but we're better off being very conservative.
107
108 // Arimo, Tinos and Cousine are a set of fonts metric-compatible with
109 // Arial, Times New Roman and Courier New with a character repertoire
110 // much larger than Liberation. Note that Cousine is metrically
111 // compatible with Courier New, but the former is sans-serif while
112 // the latter is serif.
113
114
115 struct FontEquivMap {
116 FontEquivClass clazz;
117 const char name[40];
118 };
119
120 static const FontEquivMap kFontEquivMap[] = {
121 { SANS, "Arial" },
122 { SANS, "Arimo" },
123 { SANS, "Liberation Sans" },
124
125 { SERIF, "Times New Roman" },
126 { SERIF, "Tinos" },
127 { SERIF, "Liberation Serif" },
128
129 { MONO, "Courier New" },
130 { MONO, "Cousine" },
131 { MONO, "Liberation Mono" },
132
133 { SYMBOL, "Symbol" },
134 { SYMBOL, "Symbol Neu" },
135
136 // MS Pゴシック
137 { PGOTHIC, "MS PGothic" },
138 { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
139 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
140 { PGOTHIC, "IPAPGothic" },
141 { PGOTHIC, "MotoyaG04Gothic" },
142
143 // MS ゴシック
144 { GOTHIC, "MS Gothic" },
145 { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 "
146 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
147 { GOTHIC, "IPAGothic" },
148 { GOTHIC, "MotoyaG04GothicMono" },
149
150 // MS P明朝
151 { PMINCHO, "MS PMincho" },
152 { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
153 "\xe6\x98\x8e\xe6\x9c\x9d"},
154 { PMINCHO, "IPAPMincho" },
155 { PMINCHO, "MotoyaG04Mincho" },
156
157 // MS 明朝
158 { MINCHO, "MS Mincho" },
159 { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" },
160 { MINCHO, "IPAMincho" },
161 { MINCHO, "MotoyaG04MinchoMono" },
162
163 // 宋体
164 { SIMSUN, "Simsun" },
165 { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" },
166 { SIMSUN, "MSung GB18030" },
167 { SIMSUN, "Song ASC" },
168
169 // 新宋体
170 { NSIMSUN, "NSimsun" },
171 { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" },
172 { NSIMSUN, "MSung GB18030" },
173 { NSIMSUN, "N Song ASC" },
174
175 // 黑体
176 { SIMHEI, "Simhei" },
177 { SIMHEI, "\xe9\xbb\x91\xe4\xbd\x93" },
178 { SIMHEI, "MYingHeiGB18030" },
179 { SIMHEI, "MYingHeiB5HK" },
180
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000181 // 新細明體
182 { PMINGLIU, "PMingLiU"},
183 { PMINGLIU, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
184 { PMINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000185
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000186 // 細明體
187 { MINGLIU, "MingLiU"},
188 { MINGLIU, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
189 { MINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000190
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000191 // 新細明體
192 { PMINGLIUHK, "PMingLiU_HKSCS"},
193 { PMINGLIUHK, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
194 { PMINGLIUHK, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000195
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000196 // 細明體
197 { MINGLIUHK, "MingLiU_HKSCS"},
198 { MINGLIUHK, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
199 { MINGLIUHK, "MSung B5HK"},
200
201 // Cambria
202 { CAMBRIA, "Cambria" },
203 { CAMBRIA, "Caladea" },
reed@google.comb1c65b62013-02-26 15:50:51 +0000204 };
205
206 static const size_t kFontCount =
207 sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]);
208
209 // TODO(jungshik): If this loop turns out to be hot, turn
210 // the array to a static (hash)map to speed it up.
211 for (size_t i = 0; i < kFontCount; ++i) {
212 if (strcasecmp(kFontEquivMap[i].name, fontname) == 0)
213 return kFontEquivMap[i].clazz;
214 }
215 return OTHER;
216}
217
218
219// Return true if |font_a| and |font_b| are visually and at the metrics
220// level interchangeable.
221bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b)
222{
223 FontEquivClass class_a = GetFontEquivClass(font_a);
224 FontEquivClass class_b = GetFontEquivClass(font_b);
225
226 return class_a != OTHER && class_a == class_b;
227}
228
reed@google.comb1c65b62013-02-26 15:50:51 +0000229// Normally we only return exactly the font asked for. In last-resort
230// cases, the request either doesn't specify a font or is one of the
231// basic font names like "Sans", "Serif" or "Monospace". This function
232// tells you whether a given request is for such a fallback.
233bool IsFallbackFontAllowed(const std::string& family) {
234 const char* family_cstr = family.c_str();
235 return family.empty() ||
236 strcasecmp(family_cstr, "sans") == 0 ||
237 strcasecmp(family_cstr, "serif") == 0 ||
238 strcasecmp(family_cstr, "monospace") == 0;
239}
240
241// Find matching font from |font_set| for the given font family.
242FcPattern* MatchFont(FcFontSet* font_set,
243 FcChar8* post_config_family,
244 const std::string& family) {
245 // Older versions of fontconfig have a bug where they cannot select
246 // only scalable fonts so we have to manually filter the results.
247 FcPattern* match = NULL;
248 for (int i = 0; i < font_set->nfont; ++i) {
249 FcPattern* current = font_set->fonts[i];
250 FcBool is_scalable;
251
252 if (FcPatternGetBool(current, FC_SCALABLE, 0,
253 &is_scalable) != FcResultMatch ||
254 !is_scalable) {
255 continue;
256 }
257
258 // fontconfig can also return fonts which are unreadable
259 FcChar8* c_filename;
260 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
261 continue;
262
263 if (access(reinterpret_cast<char*>(c_filename), R_OK) != 0)
264 continue;
265
266 match = current;
267 break;
268 }
269
270 if (match && !IsFallbackFontAllowed(family)) {
271 bool acceptable_substitute = false;
272 for (int id = 0; id < 255; ++id) {
273 FcChar8* post_match_family;
274 if (FcPatternGetString(match, FC_FAMILY, id, &post_match_family) !=
275 FcResultMatch)
276 break;
277 acceptable_substitute =
278 (strcasecmp(reinterpret_cast<char*>(post_config_family),
279 reinterpret_cast<char*>(post_match_family)) == 0 ||
280 // Workaround for Issue 12530:
281 // requested family: "Bitstream Vera Sans"
282 // post_config_family: "Arial"
283 // post_match_family: "Bitstream Vera Sans"
284 // -> We should treat this case as a good match.
285 strcasecmp(family.c_str(),
286 reinterpret_cast<char*>(post_match_family)) == 0) ||
287 IsMetricCompatibleReplacement(family.c_str(),
288 reinterpret_cast<char*>(post_match_family));
289 if (acceptable_substitute)
290 break;
291 }
292 if (!acceptable_substitute)
293 return NULL;
294 }
295
296 return match;
297}
298
299// Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
reed@google.comf71a2332013-02-27 19:06:30 +0000300SkTypeface::Style GetFontStyle(FcPattern* font) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000301 int resulting_bold;
302 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
303 resulting_bold = FC_WEIGHT_NORMAL;
304
305 int resulting_italic;
306 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
307 resulting_italic = FC_SLANT_ROMAN;
308
309 // If we ask for an italic font, fontconfig might take a roman font and set
310 // the undocumented property FC_MATRIX to a skew matrix. It'll then say
311 // that the font is italic or oblique. So, if we see a matrix, we don't
312 // believe that it's italic.
313 FcValue matrix;
314 const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0;
315
316 // If we ask for an italic font, fontconfig might take a roman font and set
317 // FC_EMBOLDEN.
318 FcValue embolden;
319 const bool have_embolden = FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) == 0;
320
321 int styleBits = 0;
322 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) {
323 styleBits |= SkTypeface::kBold;
324 }
325 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) {
326 styleBits |= SkTypeface::kItalic;
327 }
328
reed@google.comf71a2332013-02-27 19:06:30 +0000329 return (SkTypeface::Style)styleBits;
reed@google.comb1c65b62013-02-26 15:50:51 +0000330}
331
332} // anonymous namespace
333
334///////////////////////////////////////////////////////////////////////////////
335
reed@google.comf71a2332013-02-27 19:06:30 +0000336#define kMaxFontFamilyLength 2048
reed@google.comb1c65b62013-02-26 15:50:51 +0000337
reed@google.comf71a2332013-02-27 19:06:30 +0000338SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() {
reed@google.comb1c65b62013-02-26 15:50:51 +0000339 FcInit();
340}
341
342SkFontConfigInterfaceDirect::~SkFontConfigInterfaceDirect() {
343}
344
reed@google.comf71a2332013-02-27 19:06:30 +0000345bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
346 SkTypeface::Style style,
347 FontIdentity* outIdentity,
348 SkString* outFamilyName,
349 SkTypeface::Style* outStyle) {
reed@google.comee619a02013-02-26 22:58:09 +0000350 std::string familyStr(familyName ? familyName : "");
351 if (familyStr.length() > kMaxFontFamilyLength) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000352 return false;
353 }
354
355 SkAutoMutexAcquire ac(mutex_);
356
reed@google.comb1c65b62013-02-26 15:50:51 +0000357 FcPattern* pattern = FcPatternCreate();
358
reed@google.comee619a02013-02-26 22:58:09 +0000359 if (familyName) {
360 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
361 }
reed@google.comb1c65b62013-02-26 15:50:51 +0000362 FcPatternAddInteger(pattern, FC_WEIGHT,
363 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD
364 : FC_WEIGHT_NORMAL);
365 FcPatternAddInteger(pattern, FC_SLANT,
366 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC
367 : FC_SLANT_ROMAN);
368 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
369
370 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
371 FcDefaultSubstitute(pattern);
372
373 // Font matching:
374 // CSS often specifies a fallback list of families:
375 // font-family: a, b, c, serif;
376 // However, fontconfig will always do its best to find *a* font when asked
377 // for something so we need a way to tell if the match which it has found is
378 // "good enough" for us. Otherwise, we can return NULL which gets piped up
379 // and lets WebKit know to try the next CSS family name. However, fontconfig
380 // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we
381 // wish to support that.
382 //
383 // Thus, if a specific family is requested we set @family_requested. Then we
384 // record two strings: the family name after config processing and the
385 // family name after resolving. If the two are equal, it's a good match.
386 //
387 // So consider the case where a user has mapped Arial to Helvetica in their
388 // config.
389 // requested family: "Arial"
390 // post_config_family: "Helvetica"
391 // post_match_family: "Helvetica"
392 // -> good match
393 //
394 // and for a missing font:
395 // requested family: "Monaco"
396 // post_config_family: "Monaco"
397 // post_match_family: "Times New Roman"
398 // -> BAD match
399 //
400 // However, we special-case fallback fonts; see IsFallbackFontAllowed().
401 FcChar8* post_config_family;
402 FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family);
403
404 FcResult result;
405 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
406 if (!font_set) {
407 FcPatternDestroy(pattern);
408 return false;
409 }
410
reed@google.comee619a02013-02-26 22:58:09 +0000411 FcPattern* match = MatchFont(font_set, post_config_family, familyStr);
reed@google.comb1c65b62013-02-26 15:50:51 +0000412 if (!match) {
413 FcPatternDestroy(pattern);
414 FcFontSetDestroy(font_set);
415 return false;
416 }
417
418 FcPatternDestroy(pattern);
419
reed@google.comf71a2332013-02-27 19:06:30 +0000420 // From here out we just extract our results from 'match'
421
422 if (FcPatternGetString(match, FC_FAMILY, 0, &post_config_family) != FcResultMatch) {
423 FcFontSetDestroy(font_set);
424 return false;
425 }
426
reed@google.comb1c65b62013-02-26 15:50:51 +0000427 FcChar8* c_filename;
428 if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) {
429 FcFontSetDestroy(font_set);
430 return false;
431 }
reed@google.comf71a2332013-02-27 19:06:30 +0000432
reed@google.comb1c65b62013-02-26 15:50:51 +0000433 int face_index;
434 if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) {
435 FcFontSetDestroy(font_set);
436 return false;
437 }
438
reed@google.comb1c65b62013-02-26 15:50:51 +0000439 FcFontSetDestroy(font_set);
440
reed@google.comf71a2332013-02-27 19:06:30 +0000441 if (outIdentity) {
reed@google.com8c9737e2013-03-06 13:06:03 +0000442 outIdentity->fTTCIndex = face_index;
reed@google.comf71a2332013-02-27 19:06:30 +0000443 outIdentity->fString.set((const char*)c_filename);
reed@google.comb1c65b62013-02-26 15:50:51 +0000444 }
reed@google.comf71a2332013-02-27 19:06:30 +0000445 if (outFamilyName) {
446 outFamilyName->set((const char*)post_config_family);
reed@google.comb1c65b62013-02-26 15:50:51 +0000447 }
reed@google.comf71a2332013-02-27 19:06:30 +0000448 if (outStyle) {
449 *outStyle = GetFontStyle(match);
reed@google.comb1c65b62013-02-26 15:50:51 +0000450 }
reed@google.comee619a02013-02-26 22:58:09 +0000451 return true;
reed@google.comb1c65b62013-02-26 15:50:51 +0000452}
453
reed@google.comf71a2332013-02-27 19:06:30 +0000454SkStream* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) {
reed@google.com8c3f84d2013-03-19 13:34:55 +0000455 return SkStream::NewFromFile(identity.fString.c_str());
reed@google.comb1c65b62013-02-26 15:50:51 +0000456}
reed@google.com027fd202013-04-19 20:45:30 +0000457
458///////////////////////////////////////////////////////////////////////////////
459
460static const char* get_name(FcPattern* pattern, const char field[]) {
461 const char* name;
462 if (FcPatternGetString(pattern, field, 0, (FcChar8**)&name) != FcResultMatch) {
463 name = "";
464 }
465 return name;
466}
467
468static bool find_name(const SkTDArray<const char*>& list, const char* str) {
469 int count = list.count();
470 for (int i = 0; i < count; ++i) {
471 if (!strcmp(list[i], str)) {
472 return true;
473 }
474 }
475 return false;
476}
477
478SkDataTable* SkFontConfigInterfaceDirect::getFamilyNames() {
479 FcPattern* pat = FcPatternCreate();
480 FcObjectSet* os = FcObjectSetBuild (FC_FAMILY, (char *) 0);
481 if (NULL == os) {
reed@google.comdf798b32013-04-19 20:52:50 +0000482 return NULL;
reed@google.com027fd202013-04-19 20:45:30 +0000483 }
484 FcFontSet* fs = FcFontList(NULL, pat, os);
485 if (NULL == fs) {
486 FcObjectSetDestroy(os);
reed@google.comdf798b32013-04-19 20:52:50 +0000487 return NULL;
reed@google.com027fd202013-04-19 20:45:30 +0000488 }
489
490 SkTDArray<const char*> names;
491 SkTDArray<size_t> sizes;
492 for (int i = 0; i < fs->nfont; ++i) {
493 FcPattern* match = fs->fonts[i];
494 const char* famName = get_name(match, FC_FAMILY);
495 if (!find_name(names, famName)) {
496 *names.append() = famName;
497 *sizes.append() = strlen(famName) + 1;
498 }
499 }
500
501 FcFontSetDestroy(fs);
502 FcObjectSetDestroy(os);
503 FcPatternDestroy(pat);
504
505 return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
506 sizes.begin(), names.count());
507}
508
509bool SkFontConfigInterfaceDirect::matchFamilySet(const char inFamilyName[],
510 SkString* outFamilyName,
511 SkTArray<FontIdentity>* ids) {
512 return false;
513}