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