blob: 83e01c1f1a2f6e293b0490c20e65124b2d8ade83 [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
199inline unsigned FileFaceIdToFileId(unsigned filefaceid)
200{
201 return filefaceid >> 4;
202}
203
204inline unsigned FileIdAndFaceIndexToFileFaceId(unsigned fileid, int face_index)
205{
206 SkASSERT((face_index & 0xfu) == face_index);
207 return (fileid << 4) | face_index;
208}
209
210// Normally we only return exactly the font asked for. In last-resort
211// cases, the request either doesn't specify a font or is one of the
212// basic font names like "Sans", "Serif" or "Monospace". This function
213// tells you whether a given request is for such a fallback.
214bool IsFallbackFontAllowed(const std::string& family) {
215 const char* family_cstr = family.c_str();
216 return family.empty() ||
217 strcasecmp(family_cstr, "sans") == 0 ||
218 strcasecmp(family_cstr, "serif") == 0 ||
219 strcasecmp(family_cstr, "monospace") == 0;
220}
221
222// Find matching font from |font_set| for the given font family.
223FcPattern* MatchFont(FcFontSet* font_set,
224 FcChar8* post_config_family,
225 const std::string& family) {
226 // Older versions of fontconfig have a bug where they cannot select
227 // only scalable fonts so we have to manually filter the results.
228 FcPattern* match = NULL;
229 for (int i = 0; i < font_set->nfont; ++i) {
230 FcPattern* current = font_set->fonts[i];
231 FcBool is_scalable;
232
233 if (FcPatternGetBool(current, FC_SCALABLE, 0,
234 &is_scalable) != FcResultMatch ||
235 !is_scalable) {
236 continue;
237 }
238
239 // fontconfig can also return fonts which are unreadable
240 FcChar8* c_filename;
241 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != FcResultMatch)
242 continue;
243
244 if (access(reinterpret_cast<char*>(c_filename), R_OK) != 0)
245 continue;
246
247 match = current;
248 break;
249 }
250
251 if (match && !IsFallbackFontAllowed(family)) {
252 bool acceptable_substitute = false;
253 for (int id = 0; id < 255; ++id) {
254 FcChar8* post_match_family;
255 if (FcPatternGetString(match, FC_FAMILY, id, &post_match_family) !=
256 FcResultMatch)
257 break;
258 acceptable_substitute =
259 (strcasecmp(reinterpret_cast<char*>(post_config_family),
260 reinterpret_cast<char*>(post_match_family)) == 0 ||
261 // Workaround for Issue 12530:
262 // requested family: "Bitstream Vera Sans"
263 // post_config_family: "Arial"
264 // post_match_family: "Bitstream Vera Sans"
265 // -> We should treat this case as a good match.
266 strcasecmp(family.c_str(),
267 reinterpret_cast<char*>(post_match_family)) == 0) ||
268 IsMetricCompatibleReplacement(family.c_str(),
269 reinterpret_cast<char*>(post_match_family));
270 if (acceptable_substitute)
271 break;
272 }
273 if (!acceptable_substitute)
274 return NULL;
275 }
276
277 return match;
278}
279
280// Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
reed@google.comf71a2332013-02-27 19:06:30 +0000281SkTypeface::Style GetFontStyle(FcPattern* font) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000282 int resulting_bold;
283 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
284 resulting_bold = FC_WEIGHT_NORMAL;
285
286 int resulting_italic;
287 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
288 resulting_italic = FC_SLANT_ROMAN;
289
290 // If we ask for an italic font, fontconfig might take a roman font and set
291 // the undocumented property FC_MATRIX to a skew matrix. It'll then say
292 // that the font is italic or oblique. So, if we see a matrix, we don't
293 // believe that it's italic.
294 FcValue matrix;
295 const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0;
296
297 // If we ask for an italic font, fontconfig might take a roman font and set
298 // FC_EMBOLDEN.
299 FcValue embolden;
300 const bool have_embolden = FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) == 0;
301
302 int styleBits = 0;
303 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) {
304 styleBits |= SkTypeface::kBold;
305 }
306 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) {
307 styleBits |= SkTypeface::kItalic;
308 }
309
reed@google.comf71a2332013-02-27 19:06:30 +0000310 return (SkTypeface::Style)styleBits;
reed@google.comb1c65b62013-02-26 15:50:51 +0000311}
312
313} // anonymous namespace
314
315///////////////////////////////////////////////////////////////////////////////
316
reed@google.comf71a2332013-02-27 19:06:30 +0000317#define kMaxFontFamilyLength 2048
reed@google.comb1c65b62013-02-26 15:50:51 +0000318
reed@google.comf71a2332013-02-27 19:06:30 +0000319SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() {
reed@google.comb1c65b62013-02-26 15:50:51 +0000320 FcInit();
321}
322
323SkFontConfigInterfaceDirect::~SkFontConfigInterfaceDirect() {
324}
325
reed@google.comf71a2332013-02-27 19:06:30 +0000326bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
327 SkTypeface::Style style,
328 FontIdentity* outIdentity,
329 SkString* outFamilyName,
330 SkTypeface::Style* outStyle) {
reed@google.comee619a02013-02-26 22:58:09 +0000331 std::string familyStr(familyName ? familyName : "");
332 if (familyStr.length() > kMaxFontFamilyLength) {
reed@google.comb1c65b62013-02-26 15:50:51 +0000333 return false;
334 }
335
336 SkAutoMutexAcquire ac(mutex_);
337
reed@google.comb1c65b62013-02-26 15:50:51 +0000338 FcPattern* pattern = FcPatternCreate();
339
reed@google.comee619a02013-02-26 22:58:09 +0000340 if (familyName) {
341 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
342 }
reed@google.comb1c65b62013-02-26 15:50:51 +0000343 FcPatternAddInteger(pattern, FC_WEIGHT,
344 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD
345 : FC_WEIGHT_NORMAL);
346 FcPatternAddInteger(pattern, FC_SLANT,
347 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC
348 : FC_SLANT_ROMAN);
349 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
350
351 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
352 FcDefaultSubstitute(pattern);
353
354 // Font matching:
355 // CSS often specifies a fallback list of families:
356 // font-family: a, b, c, serif;
357 // However, fontconfig will always do its best to find *a* font when asked
358 // for something so we need a way to tell if the match which it has found is
359 // "good enough" for us. Otherwise, we can return NULL which gets piped up
360 // and lets WebKit know to try the next CSS family name. However, fontconfig
361 // configs allow substitutions (mapping "Arial -> Helvetica" etc) and we
362 // wish to support that.
363 //
364 // Thus, if a specific family is requested we set @family_requested. Then we
365 // record two strings: the family name after config processing and the
366 // family name after resolving. If the two are equal, it's a good match.
367 //
368 // So consider the case where a user has mapped Arial to Helvetica in their
369 // config.
370 // requested family: "Arial"
371 // post_config_family: "Helvetica"
372 // post_match_family: "Helvetica"
373 // -> good match
374 //
375 // and for a missing font:
376 // requested family: "Monaco"
377 // post_config_family: "Monaco"
378 // post_match_family: "Times New Roman"
379 // -> BAD match
380 //
381 // However, we special-case fallback fonts; see IsFallbackFontAllowed().
382 FcChar8* post_config_family;
383 FcPatternGetString(pattern, FC_FAMILY, 0, &post_config_family);
384
385 FcResult result;
386 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result);
387 if (!font_set) {
388 FcPatternDestroy(pattern);
389 return false;
390 }
391
reed@google.comee619a02013-02-26 22:58:09 +0000392 FcPattern* match = MatchFont(font_set, post_config_family, familyStr);
reed@google.comb1c65b62013-02-26 15:50:51 +0000393 if (!match) {
394 FcPatternDestroy(pattern);
395 FcFontSetDestroy(font_set);
396 return false;
397 }
398
399 FcPatternDestroy(pattern);
400
reed@google.comf71a2332013-02-27 19:06:30 +0000401 // From here out we just extract our results from 'match'
402
403 if (FcPatternGetString(match, FC_FAMILY, 0, &post_config_family) != FcResultMatch) {
404 FcFontSetDestroy(font_set);
405 return false;
406 }
407
reed@google.comb1c65b62013-02-26 15:50:51 +0000408 FcChar8* c_filename;
409 if (FcPatternGetString(match, FC_FILE, 0, &c_filename) != FcResultMatch) {
410 FcFontSetDestroy(font_set);
411 return false;
412 }
reed@google.comf71a2332013-02-27 19:06:30 +0000413
reed@google.comb1c65b62013-02-26 15:50:51 +0000414 int face_index;
415 if (FcPatternGetInteger(match, FC_INDEX, 0, &face_index) != FcResultMatch) {
416 FcFontSetDestroy(font_set);
417 return false;
418 }
419
reed@google.comb1c65b62013-02-26 15:50:51 +0000420 FcFontSetDestroy(font_set);
421
reed@google.comf71a2332013-02-27 19:06:30 +0000422 if (outIdentity) {
423 outIdentity->fIntPtr = face_index;
424 outIdentity->fString.set((const char*)c_filename);
reed@google.comb1c65b62013-02-26 15:50:51 +0000425 }
reed@google.comf71a2332013-02-27 19:06:30 +0000426 if (outFamilyName) {
427 outFamilyName->set((const char*)post_config_family);
reed@google.comb1c65b62013-02-26 15:50:51 +0000428 }
reed@google.comf71a2332013-02-27 19:06:30 +0000429 if (outStyle) {
430 *outStyle = GetFontStyle(match);
reed@google.comb1c65b62013-02-26 15:50:51 +0000431 }
reed@google.comee619a02013-02-26 22:58:09 +0000432 return true;
reed@google.comb1c65b62013-02-26 15:50:51 +0000433}
434
reed@google.comf71a2332013-02-27 19:06:30 +0000435SkStream* SkFontConfigInterfaceDirect::openStream(const FontIdentity& identity) {
436 int fd = open(identity.fString.c_str(), O_RDONLY);
reed@google.comb1c65b62013-02-26 15:50:51 +0000437 return (fd >= 0) ? SkNEW_ARGS(SkFDStream, (fd, true)) : NULL;
438}