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