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