OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009-2015 Google Inc. | 2 * Copyright 2009-2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 /* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */ | 8 /* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */ |
9 | 9 |
10 #include "SkBuffer.h" | 10 #include "SkBuffer.h" |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 // tells you whether a given request is for such a fallback. | 298 // tells you whether a given request is for such a fallback. |
299 bool IsFallbackFontAllowed(const SkString& family) { | 299 bool IsFallbackFontAllowed(const SkString& family) { |
300 const char* family_cstr = family.c_str(); | 300 const char* family_cstr = family.c_str(); |
301 return family.isEmpty() || | 301 return family.isEmpty() || |
302 strcasecmp(family_cstr, "sans") == 0 || | 302 strcasecmp(family_cstr, "sans") == 0 || |
303 strcasecmp(family_cstr, "serif") == 0 || | 303 strcasecmp(family_cstr, "serif") == 0 || |
304 strcasecmp(family_cstr, "monospace") == 0; | 304 strcasecmp(family_cstr, "monospace") == 0; |
305 } | 305 } |
306 | 306 |
307 // Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|. | 307 // Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|. |
308 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE | |
309 SkTypeface::Style GetFontStyle(FcPattern* font) { | |
310 int resulting_bold; | |
311 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold)) | |
312 resulting_bold = FC_WEIGHT_NORMAL; | |
313 | |
314 int resulting_italic; | |
315 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic)) | |
316 resulting_italic = FC_SLANT_ROMAN; | |
317 | |
318 // If we ask for an italic font, fontconfig might take a roman font and set | |
319 // the undocumented property FC_MATRIX to a skew matrix. It'll then say | |
320 // that the font is italic or oblique. So, if we see a matrix, we don't | |
321 // believe that it's italic. | |
322 FcValue matrix; | |
323 const bool have_matrix = FcPatternGet(font, FC_MATRIX, 0, &matrix) == 0; | |
324 | |
325 // If we ask for an italic font, fontconfig might take a roman font and set | |
326 // FC_EMBOLDEN. | |
327 FcValue embolden; | |
328 const bool have_embolden = FcPatternGet(font, FC_EMBOLDEN, 0, &embolden) ==
0; | |
329 | |
330 int styleBits = 0; | |
331 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) { | |
332 styleBits |= SkTypeface::kBold; | |
333 } | |
334 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) { | |
335 styleBits |= SkTypeface::kItalic; | |
336 } | |
337 | |
338 return (SkTypeface::Style)styleBits; | |
339 } | |
340 #else | |
341 | |
342 static int get_int(FcPattern* pattern, const char object[], int missing) { | 308 static int get_int(FcPattern* pattern, const char object[], int missing) { |
343 int value; | 309 int value; |
344 if (FcPatternGetInteger(pattern, object, 0, &value) != FcResultMatch) { | 310 if (FcPatternGetInteger(pattern, object, 0, &value) != FcResultMatch) { |
345 return missing; | 311 return missing; |
346 } | 312 } |
347 return value; | 313 return value; |
348 } | 314 } |
349 | 315 |
350 static int map_range(SkFixed value, | 316 static int map_range(SkFixed value, |
351 SkFixed old_min, SkFixed old_max, | 317 SkFixed old_min, SkFixed old_max, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 { SkTFixed<SkFS::kExtraExpanded_Width>::value, SkTFixed<FC_WIDTH_EXTRAE
XPANDED>::value }, | 422 { SkTFixed<SkFS::kExtraExpanded_Width>::value, SkTFixed<FC_WIDTH_EXTRAE
XPANDED>::value }, |
457 { SkTFixed<SkFS::kUltaExpanded_Width>::value, SkTFixed<FC_WIDTH_ULTRAE
XPANDED>::value }, | 423 { SkTFixed<SkFS::kUltaExpanded_Width>::value, SkTFixed<FC_WIDTH_ULTRAE
XPANDED>::value }, |
458 }; | 424 }; |
459 int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRange
s)); | 425 int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRange
s)); |
460 | 426 |
461 FcPatternAddInteger(pattern, FC_WEIGHT, weight); | 427 FcPatternAddInteger(pattern, FC_WEIGHT, weight); |
462 FcPatternAddInteger(pattern, FC_WIDTH, width); | 428 FcPatternAddInteger(pattern, FC_WIDTH, width); |
463 FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC :
FC_SLANT_ROMAN); | 429 FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC :
FC_SLANT_ROMAN); |
464 } | 430 } |
465 | 431 |
466 SkFontStyle GetFontStyle(FcPattern* font) { | |
467 return skfontstyle_from_fcpattern(font); | |
468 } | |
469 #endif | |
470 | |
471 } // anonymous namespace | 432 } // anonymous namespace |
472 | 433 |
473 /////////////////////////////////////////////////////////////////////////////// | 434 /////////////////////////////////////////////////////////////////////////////// |
474 | 435 |
475 #define kMaxFontFamilyLength 2048 | 436 #define kMaxFontFamilyLength 2048 |
476 | 437 |
477 SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() { | 438 SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() { |
478 SkAutoMutexAcquire ac(mutex_); | 439 SkAutoMutexAcquire ac(mutex_); |
479 | 440 |
480 FcInit(); | 441 FcInit(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 if (acceptable_substitute) | 503 if (acceptable_substitute) |
543 break; | 504 break; |
544 } | 505 } |
545 if (!acceptable_substitute) | 506 if (!acceptable_substitute) |
546 return nullptr; | 507 return nullptr; |
547 } | 508 } |
548 | 509 |
549 return match; | 510 return match; |
550 } | 511 } |
551 | 512 |
552 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE | |
553 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[], | |
554 SkTypeface::Style style, | |
555 FontIdentity* outIdentity, | |
556 SkString* outFamilyName, | |
557 SkTypeface::Style* outStyle) { | |
558 #else | |
559 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[], | 513 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[], |
560 SkFontStyle style, | 514 SkFontStyle style, |
561 FontIdentity* outIdentity, | 515 FontIdentity* outIdentity, |
562 SkString* outFamilyName, | 516 SkString* outFamilyName, |
563 SkFontStyle* outStyle) { | 517 SkFontStyle* outStyle) { |
564 #endif | |
565 SkString familyStr(familyName ? familyName : ""); | 518 SkString familyStr(familyName ? familyName : ""); |
566 if (familyStr.size() > kMaxFontFamilyLength) { | 519 if (familyStr.size() > kMaxFontFamilyLength) { |
567 return false; | 520 return false; |
568 } | 521 } |
569 | 522 |
570 SkAutoMutexAcquire ac(mutex_); | 523 SkAutoMutexAcquire ac(mutex_); |
571 | 524 |
572 FcPattern* pattern = FcPatternCreate(); | 525 FcPattern* pattern = FcPatternCreate(); |
573 | 526 |
574 if (familyName) { | 527 if (familyName) { |
575 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); | 528 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); |
576 } | 529 } |
577 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE | |
578 FcPatternAddInteger(pattern, FC_WEIGHT, | |
579 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD | |
580 : FC_WEIGHT_NORMAL); | |
581 FcPatternAddInteger(pattern, FC_SLANT, | |
582 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC | |
583 : FC_SLANT_ROMAN); | |
584 #else | |
585 fcpattern_from_skfontstyle(style, pattern); | 530 fcpattern_from_skfontstyle(style, pattern); |
586 #endif | |
587 | 531 |
588 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); | 532 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); |
589 | 533 |
590 FcConfigSubstitute(nullptr, pattern, FcMatchPattern); | 534 FcConfigSubstitute(nullptr, pattern, FcMatchPattern); |
591 FcDefaultSubstitute(pattern); | 535 FcDefaultSubstitute(pattern); |
592 | 536 |
593 // Font matching: | 537 // Font matching: |
594 // CSS often specifies a fallback list of families: | 538 // CSS often specifies a fallback list of families: |
595 // font-family: a, b, c, serif; | 539 // font-family: a, b, c, serif; |
596 // However, fontconfig will always do its best to find *a* font when asked | 540 // However, fontconfig will always do its best to find *a* font when asked |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 FcFontSetDestroy(font_set); | 608 FcFontSetDestroy(font_set); |
665 | 609 |
666 if (outIdentity) { | 610 if (outIdentity) { |
667 outIdentity->fTTCIndex = face_index; | 611 outIdentity->fTTCIndex = face_index; |
668 outIdentity->fString.set(c_filename); | 612 outIdentity->fString.set(c_filename); |
669 } | 613 } |
670 if (outFamilyName) { | 614 if (outFamilyName) { |
671 outFamilyName->set(post_config_family); | 615 outFamilyName->set(post_config_family); |
672 } | 616 } |
673 if (outStyle) { | 617 if (outStyle) { |
674 *outStyle = GetFontStyle(match); | 618 *outStyle = skfontstyle_from_fcpattern(match); |
675 } | 619 } |
676 return true; | 620 return true; |
677 } | 621 } |
678 | 622 |
679 SkStreamAsset* SkFontConfigInterfaceDirect::openStream(const FontIdentity& ident
ity) { | 623 SkStreamAsset* SkFontConfigInterfaceDirect::openStream(const FontIdentity& ident
ity) { |
680 return SkStream::NewFromFile(identity.fString.c_str()); | 624 return SkStream::NewFromFile(identity.fString.c_str()); |
681 } | 625 } |
682 | 626 |
683 /////////////////////////////////////////////////////////////////////////////// | 627 /////////////////////////////////////////////////////////////////////////////// |
684 | 628 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 const char* famName = get_name(match, FC_FAMILY); | 664 const char* famName = get_name(match, FC_FAMILY); |
721 if (famName && !find_name(names, famName)) { | 665 if (famName && !find_name(names, famName)) { |
722 *names.append() = famName; | 666 *names.append() = famName; |
723 *sizes.append() = strlen(famName) + 1; | 667 *sizes.append() = strlen(famName) + 1; |
724 } | 668 } |
725 } | 669 } |
726 | 670 |
727 return SkDataTable::NewCopyArrays((const void*const*)names.begin(), | 671 return SkDataTable::NewCopyArrays((const void*const*)names.begin(), |
728 sizes.begin(), names.count()); | 672 sizes.begin(), names.count()); |
729 } | 673 } |
OLD | NEW |