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