blob: 29cb49b9d1fbce7f066c4e8d12bac6b1257ae5c5 [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
43namespace {
44
45// Equivalence classes, used to match the Liberation and other fonts
46// with their metric-compatible replacements. See the discussion in
47// GetFontEquivClass().
48enum FontEquivClass
49{
50 OTHER,
51 SANS,
52 SERIF,
53 MONO,
54 SYMBOL,
55 PGOTHIC,
56 GOTHIC,
57 PMINCHO,
58 MINCHO,
59 SIMSUN,
60 NSIMSUN,
61 SIMHEI,
62 PMINGLIU,
63 MINGLIU,
64 PMINGLIUHK,
65 MINGLIUHK,
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +000066 CAMBRIA,
reed@google.comb1c65b62013-02-26 15:50:51 +000067};
68
69// Match the font name against a whilelist of fonts, returning the equivalence
70// class.
71FontEquivClass GetFontEquivClass(const char* fontname)
72{
73 // It would be nice for fontconfig to tell us whether a given suggested
74 // replacement is a "strong" match (that is, an equivalent font) or
75 // a "weak" match (that is, fontconfig's next-best attempt at finding a
76 // substitute). However, I played around with the fontconfig API for
77 // a good few hours and could not make it reveal this information.
78 //
79 // So instead, we hardcode. Initially this function emulated
80 // /etc/fonts/conf.d/30-metric-aliases.conf
81 // from my Ubuntu system, but we're better off being very conservative.
82
83 // Arimo, Tinos and Cousine are a set of fonts metric-compatible with
84 // Arial, Times New Roman and Courier New with a character repertoire
85 // much larger than Liberation. Note that Cousine is metrically
86 // compatible with Courier New, but the former is sans-serif while
87 // the latter is serif.
88
89
90 struct FontEquivMap {
91 FontEquivClass clazz;
92 const char name[40];
93 };
94
95 static const FontEquivMap kFontEquivMap[] = {
96 { SANS, "Arial" },
97 { SANS, "Arimo" },
98 { SANS, "Liberation Sans" },
99
100 { SERIF, "Times New Roman" },
101 { SERIF, "Tinos" },
102 { SERIF, "Liberation Serif" },
103
104 { MONO, "Courier New" },
105 { MONO, "Cousine" },
106 { MONO, "Liberation Mono" },
107
108 { SYMBOL, "Symbol" },
109 { SYMBOL, "Symbol Neu" },
110
111 // MS Pゴシック
112 { PGOTHIC, "MS PGothic" },
113 { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
114 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
115 { PGOTHIC, "IPAPGothic" },
116 { PGOTHIC, "MotoyaG04Gothic" },
117
118 // MS ゴシック
119 { GOTHIC, "MS Gothic" },
120 { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 "
121 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" },
122 { GOTHIC, "IPAGothic" },
123 { GOTHIC, "MotoyaG04GothicMono" },
124
125 // MS P明朝
126 { PMINCHO, "MS PMincho" },
127 { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0"
128 "\xe6\x98\x8e\xe6\x9c\x9d"},
129 { PMINCHO, "IPAPMincho" },
130 { PMINCHO, "MotoyaG04Mincho" },
131
132 // MS 明朝
133 { MINCHO, "MS Mincho" },
134 { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" },
135 { MINCHO, "IPAMincho" },
136 { MINCHO, "MotoyaG04MinchoMono" },
137
138 // 宋体
139 { SIMSUN, "Simsun" },
140 { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" },
141 { SIMSUN, "MSung GB18030" },
142 { SIMSUN, "Song ASC" },
143
144 // 新宋体
145 { NSIMSUN, "NSimsun" },
146 { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" },
147 { NSIMSUN, "MSung GB18030" },
148 { NSIMSUN, "N Song ASC" },
149
150 // 黑体
151 { SIMHEI, "Simhei" },
152 { SIMHEI, "\xe9\xbb\x91\xe4\xbd\x93" },
153 { SIMHEI, "MYingHeiGB18030" },
154 { SIMHEI, "MYingHeiB5HK" },
155
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000156 // 新細明體
157 { PMINGLIU, "PMingLiU"},
158 { PMINGLIU, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
159 { PMINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000160
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000161 // 細明體
162 { MINGLIU, "MingLiU"},
163 { MINGLIU, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94" },
164 { MINGLIU, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000165
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000166 // 新細明體
167 { PMINGLIUHK, "PMingLiU_HKSCS"},
168 { PMINGLIUHK, "\xe6\x96\xb0\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
169 { PMINGLIUHK, "MSung B5HK"},
reed@google.comb1c65b62013-02-26 15:50:51 +0000170
commit-bot@chromium.orgc4de7762013-03-20 13:33:33 +0000171 // 細明體
172 { MINGLIUHK, "MingLiU_HKSCS"},
173 { MINGLIUHK, "\xe7\xb4\xb0\xe6\x98\x8e\xe9\xab\x94_HKSCS" },
174 { MINGLIUHK, "MSung B5HK"},
175
176 // Cambria
177 { CAMBRIA, "Cambria" },
178 { CAMBRIA, "Caladea" },
reed@google.comb1c65b62013-02-26 15:50:51 +0000179 };
180
181 static const size_t kFontCount =
182 sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]);
183
184 // TODO(jungshik): If this loop turns out to be hot, turn
185 // the array to a static (hash)map to speed it up.
186 for (size_t i = 0; i < kFontCount; ++i) {
187 if (strcasecmp(kFontEquivMap[i].name, fontname) == 0)
188 return kFontEquivMap[i].clazz;
189 }
190 return OTHER;
191}
192
193
194// Return true if |font_a| and |font_b| are visually and at the metrics
195// level interchangeable.
196bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b)
197{
198 FontEquivClass class_a = GetFontEquivClass(font_a);
199 FontEquivClass class_b = GetFontEquivClass(font_b);
200
201 return class_a != OTHER && class_a == class_b;
202}
203
reed@google.comb1c65b62013-02-26 15:50:51 +0000204// Normally we only return exactly the font asked for. In last-resort
205// cases, the request either doesn't specify a font or is one of the
206// basic font names like "Sans", "Serif" or "Monospace". This function
207// tells you whether a given request is for such a fallback.
208bool IsFallbackFontAllowed(const std::string& family) {
209 const char* family_cstr = family.c_str();
210 return family.empty() ||
211 strcasecmp(family_cstr, "sans") == 0 ||
212 strcasecmp(family_cstr, "serif") == 0 ||
213 strcasecmp(family_cstr, "monospace") == 0;
214}
215
216// Find matching font from |font_set| for the given font family.
217FcPattern* MatchFont(FcFontSet* font_set,
218 FcChar8* post_config_family,
219 const std::string& family) {
220 // Older versions of fontconfig have a bug where they cannot select
221 // only scalable fonts so we have to manually filter the results.
222 FcPattern* match = NULL;
223 for (int i = 0; i < font_set->nfont; ++i) {
224 FcPattern* current = font_set->fonts[i];
225 FcBool is_scalable;
226
227 if (FcPatternGetBool(current, FC_SCALABLE, 0,
228 &is_scalable) != FcResultMatch ||
229 !is_scalable) {
230 continue;
231 }
232
233 // fontconfig can also return fonts which are unreadable
234 FcChar8* c_filename;
235 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
236 continue;
237
238 if (access(reinterpret_cast<char*>(c_filename), R_OK) != 0)
239 continue;
240
241 match = current;
242 break;
243 }
244
245 if (match && !IsFallbackFontAllowed(family)) {
246 bool acceptable_substitute = false;
247 for (int id = 0; id < 255; ++id) {
248 FcChar8* post_match_family;
249 if (FcPatternGetString(match, FC_FAMILY, id, &post_match_family) !=
250 FcResultMatch)
251 break;
252 acceptable_substitute =
253 (strcasecmp(reinterpret_cast<char*>(post_config_family),
254 reinterpret_cast<char*>(post_match_family)) == 0 ||
255 // Workaround for Issue 12530:
256 // requested family: "Bitstream Vera Sans"
257 // post_config_family: "Arial"
258 // post_match_family: "Bitstream Vera Sans"
259 // -> We should treat this case as a good match.
260 strcasecmp(family.c_str(),
261 reinterpret_cast<char*>(post_match_family)) == 0) ||
262 IsMetricCompatibleReplacement(family.c_str(),
263 reinterpret_cast<char*>(post_match_family));
264 if (acceptable_substitute)
265 break;
266 }
267 if (!acceptable_substitute)
268 return NULL;
269 }
270
271 return match;
272}
273
274// Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
reed@google.comf71a2332013-02-27 19:06:30 +0000275SkTypeface::Style GetFontStyle(FcPattern* font) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000276 int resulting_bold;
277 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
278 resulting_bold = FC_WEIGHT_NORMAL;
279
280 int resulting_italic;
281 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
282 resulting_italic = FC_SLANT_ROMAN;
283
284 // If we ask for an italic font, fontconfig might take a roman font and set
285 // the undocumented property FC_MATRIX to a skew matrix. It'll then say
286 // that the font is italic or oblique. So, if we see a matrix, we don't
287 // believe that it's italic.
288 FcValue matrix;
289 const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0;
290
291 // If we ask for an italic font, fontconfig might take a roman font and set
292 // FC_EMBOLDEN.
293 FcValue embolden;
294 const bool have_embolden = FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) == 0;
295
296 int styleBits = 0;
297 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) {
298 styleBits |= SkTypeface::kBold;
299 }
300 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) {
301 styleBits |= SkTypeface::kItalic;
302 }
303
reed@google.comf71a2332013-02-27 19:06:30 +0000304 return (SkTypeface::Style)styleBits;
reed@google.comb1c65b62013-02-26 15:50:51 +0000305}
306
307} // anonymous namespace
308
309///////////////////////////////////////////////////////////////////////////////
310
reed@google.comf71a2332013-02-27 19:06:30 +0000311#define kMaxFontFamilyLength 2048
reed@google.comb1c65b62013-02-26 15:50:51 +0000312
reed@google.comf71a2332013-02-27 19:06:30 +0000313SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() {
reed@google.comb1c65b62013-02-26 15:50:51 +0000314 FcInit();
315}
316
317SkFontConfigInterfaceDirect::~SkFontConfigInterfaceDirect() {
318}
319
reed@google.comf71a2332013-02-27 19:06:30 +0000320bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
321 SkTypeface::Style style,
322 FontIdentity* outIdentity,
323 SkString* outFamilyName,
324 SkTypeface::Style* outStyle) {
reed@google.comee619a02013-02-26 22:58:09 +0000325 std::string familyStr(familyName ? familyName : "");
326 if (familyStr.length() > kMaxFontFamilyLength) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000327 return false;
328 }
329
330 SkAutoMutexAcquire ac(mutex_);
331
reed@google.comb1c65b62013-02-26 15:50:51 +0000332 FcPattern* pattern = FcPatternCreate();
333
reed@google.comee619a02013-02-26 22:58:09 +0000334 if (familyName) {
335 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
336 }
reed@google.comb1c65b62013-02-26 15:50:51 +0000337 FcPatternAddInteger(pattern, FC_WEIGHT,
338 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD
339 : FC_WEIGHT_NORMAL);
340 FcPatternAddInteger(pattern, FC_SLANT,
341 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC
342 : FC_SLANT_ROMAN);
343 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
344
345 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
346 FcDefaultSubstitute(pattern);
347
348 // Font matching:
349 // CSS often specifies a fallback list of families:
350 // font-family: a, b, c, serif;
351 // However, fontconfig will always do its best to find *a* font when asked
352 // for something so we need a way to tell if the match which it has found is
353 // "good enough" for us. Otherwise, we can return NULL which gets piped up
354 // and lets WebKit know to try the next CSS family name. However, fontconfig
355 // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we
356 // wish to support that.
357 //
358 // Thus, if a specific family is requested we set @family_requested. Then we
359 // record two strings: the family name after config processing and the
360 // family name after resolving. If the two are equal, it's a good match.
361 //
362 // So consider the case where a user has mapped Arial to Helvetica in their
363 // config.
364 // requested family: "Arial"
365 // post_config_family: "Helvetica"
366 // post_match_family: "Helvetica"
367 // -> good match
368 //
369 // and for a missing font:
370 // requested family: "Monaco"
371 // post_config_family: "Monaco"
372 // post_match_family: "Times New Roman"
373 // -> BAD match
374 //
375 // However, we special-case fallback fonts; see IsFallbackFontAllowed().
376 FcChar8* post_config_family;
377 FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family);
378
379 FcResult result;
380 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
381 if (!font_set) {
382 FcPatternDestroy(pattern);
383 return false;
384 }
385
reed@google.comee619a02013-02-26 22:58:09 +0000386 FcPattern* match = MatchFont(font_set, post_config_family, familyStr);
reed@google.comb1c65b62013-02-26 15:50:51 +0000387 if (!match) {
388 FcPatternDestroy(pattern);
389 FcFontSetDestroy(font_set);
390 return false;
391 }
392
393 FcPatternDestroy(pattern);
394
reed@google.comf71a2332013-02-27 19:06:30 +0000395 // From here out we just extract our results from 'match'
396
397 if (FcPatternGetString(match, FC_FAMILY, 0, &post_config_family) != FcResultMatch) {
398 FcFontSetDestroy(font_set);
399 return false;
400 }
401
reed@google.comb1c65b62013-02-26 15:50:51 +0000402 FcChar8* c_filename;
403 if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) {
404 FcFontSetDestroy(font_set);
405 return false;
406 }
reed@google.comf71a2332013-02-27 19:06:30 +0000407
reed@google.comb1c65b62013-02-26 15:50:51 +0000408 int face_index;
409 if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) {
410 FcFontSetDestroy(font_set);
411 return false;
412 }
413
reed@google.comb1c65b62013-02-26 15:50:51 +0000414 FcFontSetDestroy(font_set);
415
reed@google.comf71a2332013-02-27 19:06:30 +0000416 if (outIdentity) {
reed@google.com8c9737e2013-03-06 13:06:03 +0000417 outIdentity->fTTCIndex = face_index;
reed@google.comf71a2332013-02-27 19:06:30 +0000418 outIdentity->fString.set((const char*)c_filename);
reed@google.comb1c65b62013-02-26 15:50:51 +0000419 }
reed@google.comf71a2332013-02-27 19:06:30 +0000420 if (outFamilyName) {
421 outFamilyName->set((const char*)post_config_family);
reed@google.comb1c65b62013-02-26 15:50:51 +0000422 }
reed@google.comf71a2332013-02-27 19:06:30 +0000423 if (outStyle) {
424 *outStyle = GetFontStyle(match);
reed@google.comb1c65b62013-02-26 15:50:51 +0000425 }
reed@google.comee619a02013-02-26 22:58:09 +0000426 return true;
reed@google.comb1c65b62013-02-26 15:50:51 +0000427}
428
reed@google.comf71a2332013-02-27 19:06:30 +0000429SkStream* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) {
reed@google.com8c3f84d2013-03-19 13:34:55 +0000430 return SkStream::NewFromFile(identity.fString.c_str());
reed@google.comb1c65b62013-02-26 15:50:51 +0000431}