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