OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 #include "SkDataTable.h" | 8 #include "SkDataTable.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 375 |
376 class SkTypeface_stream : public SkTypeface_FreeType { | 376 class SkTypeface_stream : public SkTypeface_FreeType { |
377 public: | 377 public: |
378 /** @param stream does not take ownership of the reference, does take owners
hip of the stream.*/ | 378 /** @param stream does not take ownership of the reference, does take owners
hip of the stream.*/ |
379 SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkSt
reamAsset* stream) | 379 SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkSt
reamAsset* stream) |
380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) | 380 : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) |
381 , fStream(stream) | 381 , fStream(stream) |
382 , fIndex(index) | 382 , fIndex(index) |
383 { }; | 383 { }; |
384 | 384 |
385 void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { | 385 void onGetFamilyName(SkString* familyName) const override { |
386 familyName->reset(); | 386 familyName->reset(); |
387 } | 387 } |
388 | 388 |
389 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_O
VERRIDE { | 389 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
390 desc->setFontIndex(fIndex); | 390 desc->setFontIndex(fIndex); |
391 *serialize = true; | 391 *serialize = true; |
392 } | 392 } |
393 | 393 |
394 SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 394 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
395 *ttcIndex = fIndex; | 395 *ttcIndex = fIndex; |
396 return fStream->duplicate(); | 396 return fStream->duplicate(); |
397 } | 397 } |
398 | 398 |
399 private: | 399 private: |
400 SkAutoTDelete<SkStreamAsset> fStream; | 400 SkAutoTDelete<SkStreamAsset> fStream; |
401 int fIndex; | 401 int fIndex; |
402 | 402 |
403 typedef SkTypeface_FreeType INHERITED; | 403 typedef SkTypeface_FreeType INHERITED; |
404 }; | 404 }; |
405 | 405 |
406 class SkTypeface_fontconfig : public SkTypeface_FreeType { | 406 class SkTypeface_fontconfig : public SkTypeface_FreeType { |
407 public: | 407 public: |
408 /** @param pattern takes ownership of the reference. */ | 408 /** @param pattern takes ownership of the reference. */ |
409 static SkTypeface_fontconfig* Create(FcPattern* pattern) { | 409 static SkTypeface_fontconfig* Create(FcPattern* pattern) { |
410 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); | 410 return SkNEW_ARGS(SkTypeface_fontconfig, (pattern)); |
411 } | 411 } |
412 mutable SkAutoFcPattern fPattern; | 412 mutable SkAutoFcPattern fPattern; |
413 | 413 |
414 void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { | 414 void onGetFamilyName(SkString* familyName) const override { |
415 *familyName = get_string(fPattern, FC_FAMILY); | 415 *familyName = get_string(fPattern, FC_FAMILY); |
416 } | 416 } |
417 | 417 |
418 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_O
VERRIDE { | 418 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
419 FCLocker lock; | 419 FCLocker lock; |
420 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); | 420 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); |
421 desc->setFullName(get_string(fPattern, FC_FULLNAME)); | 421 desc->setFullName(get_string(fPattern, FC_FULLNAME)); |
422 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); | 422 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); |
423 desc->setFontFileName(get_string(fPattern, FC_FILE)); | 423 desc->setFontFileName(get_string(fPattern, FC_FILE)); |
424 desc->setFontIndex(get_int(fPattern, FC_INDEX, 0)); | 424 desc->setFontIndex(get_int(fPattern, FC_INDEX, 0)); |
425 *serialize = false; | 425 *serialize = false; |
426 } | 426 } |
427 | 427 |
428 SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 428 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
429 FCLocker lock; | 429 FCLocker lock; |
430 *ttcIndex = get_int(fPattern, FC_INDEX, 0); | 430 *ttcIndex = get_int(fPattern, FC_INDEX, 0); |
431 return SkStream::NewFromFile(get_string(fPattern, FC_FILE)); | 431 return SkStream::NewFromFile(get_string(fPattern, FC_FILE)); |
432 } | 432 } |
433 | 433 |
434 virtual ~SkTypeface_fontconfig() { | 434 virtual ~SkTypeface_fontconfig() { |
435 // Hold the lock while unrefing the pattern. | 435 // Hold the lock while unrefing the pattern. |
436 FCLocker lock; | 436 FCLocker lock; |
437 fPattern.reset(); | 437 fPattern.reset(); |
438 } | 438 } |
(...skipping 23 matching lines...) Expand all Loading... |
462 StyleSet(const SkFontMgr_fontconfig* parent, FcFontSet* fontSet) | 462 StyleSet(const SkFontMgr_fontconfig* parent, FcFontSet* fontSet) |
463 : fFontMgr(SkRef(parent)), fFontSet(fontSet) | 463 : fFontMgr(SkRef(parent)), fFontSet(fontSet) |
464 { } | 464 { } |
465 | 465 |
466 virtual ~StyleSet() { | 466 virtual ~StyleSet() { |
467 // Hold the lock while unrefing the font set. | 467 // Hold the lock while unrefing the font set. |
468 FCLocker lock; | 468 FCLocker lock; |
469 fFontSet.reset(); | 469 fFontSet.reset(); |
470 } | 470 } |
471 | 471 |
472 int count() SK_OVERRIDE { return fFontSet->nfont; } | 472 int count() override { return fFontSet->nfont; } |
473 | 473 |
474 void getStyle(int index, SkFontStyle* style, SkString* styleName) SK_OVE
RRIDE { | 474 void getStyle(int index, SkFontStyle* style, SkString* styleName) overri
de { |
475 if (index < 0 || fFontSet->nfont <= index) { | 475 if (index < 0 || fFontSet->nfont <= index) { |
476 return; | 476 return; |
477 } | 477 } |
478 | 478 |
479 FCLocker lock; | 479 FCLocker lock; |
480 if (style) { | 480 if (style) { |
481 *style = skfontstyle_from_fcpattern(fFontSet->fonts[index]); | 481 *style = skfontstyle_from_fcpattern(fFontSet->fonts[index]); |
482 } | 482 } |
483 if (styleName) { | 483 if (styleName) { |
484 *styleName = get_string(fFontSet->fonts[index], FC_STYLE); | 484 *styleName = get_string(fFontSet->fonts[index], FC_STYLE); |
485 } | 485 } |
486 } | 486 } |
487 | 487 |
488 SkTypeface* createTypeface(int index) SK_OVERRIDE { | 488 SkTypeface* createTypeface(int index) override { |
489 FCLocker lock; | 489 FCLocker lock; |
490 | 490 |
491 FcPattern* match = fFontSet->fonts[index]; | 491 FcPattern* match = fFontSet->fonts[index]; |
492 return fFontMgr->createTypefaceFromFcPattern(match); | 492 return fFontMgr->createTypefaceFromFcPattern(match); |
493 } | 493 } |
494 | 494 |
495 SkTypeface* matchStyle(const SkFontStyle& style) SK_OVERRIDE { | 495 SkTypeface* matchStyle(const SkFontStyle& style) override { |
496 FCLocker lock; | 496 FCLocker lock; |
497 | 497 |
498 SkAutoFcPattern pattern; | 498 SkAutoFcPattern pattern; |
499 fcpattern_from_skfontstyle(style, pattern); | 499 fcpattern_from_skfontstyle(style, pattern); |
500 FcConfigSubstitute(fFontMgr->fFC, pattern, FcMatchPattern); | 500 FcConfigSubstitute(fFontMgr->fFC, pattern, FcMatchPattern); |
501 FcDefaultSubstitute(pattern); | 501 FcDefaultSubstitute(pattern); |
502 | 502 |
503 FcResult result; | 503 FcResult result; |
504 FcFontSet* fontSets[1] = { fFontSet }; | 504 FcFontSet* fontSets[1] = { fFontSet }; |
505 SkAutoFcPattern match(FcFontSetMatch(fFontMgr->fFC, | 505 SkAutoFcPattern match(FcFontSetMatch(fFontMgr->fFC, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 : fFC(config) | 600 : fFC(config) |
601 , fFamilyNames(GetFamilyNames(fFC)) { } | 601 , fFamilyNames(GetFamilyNames(fFC)) { } |
602 | 602 |
603 virtual ~SkFontMgr_fontconfig() { | 603 virtual ~SkFontMgr_fontconfig() { |
604 // Hold the lock while unrefing the config. | 604 // Hold the lock while unrefing the config. |
605 FCLocker lock; | 605 FCLocker lock; |
606 fFC.reset(); | 606 fFC.reset(); |
607 } | 607 } |
608 | 608 |
609 protected: | 609 protected: |
610 int onCountFamilies() const SK_OVERRIDE { | 610 int onCountFamilies() const override { |
611 return fFamilyNames->count(); | 611 return fFamilyNames->count(); |
612 } | 612 } |
613 | 613 |
614 void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { | 614 void onGetFamilyName(int index, SkString* familyName) const override { |
615 familyName->set(fFamilyNames->atStr(index)); | 615 familyName->set(fFamilyNames->atStr(index)); |
616 } | 616 } |
617 | 617 |
618 SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE { | 618 SkFontStyleSet* onCreateStyleSet(int index) const override { |
619 return this->onMatchFamily(fFamilyNames->atStr(index)); | 619 return this->onMatchFamily(fFamilyNames->atStr(index)); |
620 } | 620 } |
621 | 621 |
622 /** True if any string object value in the font is the same | 622 /** True if any string object value in the font is the same |
623 * as a string object value in the pattern. | 623 * as a string object value in the pattern. |
624 */ | 624 */ |
625 static bool AnyMatching(FcPattern* font, FcPattern* pattern, const char* obj
ect) { | 625 static bool AnyMatching(FcPattern* font, FcPattern* pattern, const char* obj
ect) { |
626 FcChar8* fontString; | 626 FcChar8* fontString; |
627 FcChar8* patternString; | 627 FcChar8* patternString; |
628 FcResult result; | 628 FcResult result; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 if (FcResultMatch != result) { | 677 if (FcResultMatch != result) { |
678 continue; | 678 continue; |
679 } | 679 } |
680 if (FcCharSetHasChar(matchCharSet, character)) { | 680 if (FcCharSetHasChar(matchCharSet, character)) { |
681 return true; | 681 return true; |
682 } | 682 } |
683 } | 683 } |
684 return false; | 684 return false; |
685 } | 685 } |
686 | 686 |
687 SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE { | 687 SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
688 FCLocker lock; | 688 FCLocker lock; |
689 | 689 |
690 SkAutoFcPattern pattern; | 690 SkAutoFcPattern pattern; |
691 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); | 691 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); |
692 FcConfigSubstitute(fFC, pattern, FcMatchPattern); | 692 FcConfigSubstitute(fFC, pattern, FcMatchPattern); |
693 FcDefaultSubstitute(pattern); | 693 FcDefaultSubstitute(pattern); |
694 | 694 |
695 FcPattern* matchPattern; | 695 FcPattern* matchPattern; |
696 SkAutoFcPattern strongPattern(NULL); | 696 SkAutoFcPattern strongPattern(NULL); |
697 if (familyName) { | 697 if (familyName) { |
(...skipping 21 matching lines...) Expand all Loading... |
719 if (FontAccessible(font) && FontFamilyNameMatches(font, matchPat
tern)) { | 719 if (FontAccessible(font) && FontFamilyNameMatches(font, matchPat
tern)) { |
720 FcFontSetAdd(matches, FcFontRenderPrepare(fFC, pattern, font
)); | 720 FcFontSetAdd(matches, FcFontRenderPrepare(fFC, pattern, font
)); |
721 } | 721 } |
722 } | 722 } |
723 } | 723 } |
724 | 724 |
725 return SkNEW_ARGS(StyleSet, (this, matches.detach())); | 725 return SkNEW_ARGS(StyleSet, (this, matches.detach())); |
726 } | 726 } |
727 | 727 |
728 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 728 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
729 const SkFontStyle& style) const SK_OV
ERRIDE | 729 const SkFontStyle& style) const overr
ide |
730 { | 730 { |
731 FCLocker lock; | 731 FCLocker lock; |
732 | 732 |
733 SkAutoFcPattern pattern; | 733 SkAutoFcPattern pattern; |
734 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); | 734 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); |
735 fcpattern_from_skfontstyle(style, pattern); | 735 fcpattern_from_skfontstyle(style, pattern); |
736 FcConfigSubstitute(fFC, pattern, FcMatchPattern); | 736 FcConfigSubstitute(fFC, pattern, FcMatchPattern); |
737 FcDefaultSubstitute(pattern); | 737 FcDefaultSubstitute(pattern); |
738 | 738 |
739 // We really want to match strong (prefered) and same (acceptable) only
here. | 739 // We really want to match strong (prefered) and same (acceptable) only
here. |
(...skipping 20 matching lines...) Expand all Loading... |
760 return NULL; | 760 return NULL; |
761 } | 761 } |
762 | 762 |
763 return createTypefaceFromFcPattern(font); | 763 return createTypefaceFromFcPattern(font); |
764 } | 764 } |
765 | 765 |
766 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], | 766 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
767 const SkFontStyle& style, | 767 const SkFontStyle& style, |
768 const char* bcp47[], | 768 const char* bcp47[], |
769 int bcp47Count, | 769 int bcp47Count, |
770 SkUnichar character) const S
K_OVERRIDE | 770 SkUnichar character) const o
verride |
771 { | 771 { |
772 FCLocker lock; | 772 FCLocker lock; |
773 | 773 |
774 SkAutoFcPattern pattern; | 774 SkAutoFcPattern pattern; |
775 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); | 775 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); |
776 fcpattern_from_skfontstyle(style, pattern); | 776 fcpattern_from_skfontstyle(style, pattern); |
777 | 777 |
778 SkAutoFcCharSet charSet; | 778 SkAutoFcCharSet charSet; |
779 FcCharSetAddChar(charSet, character); | 779 FcCharSetAddChar(charSet, character); |
780 FcPatternAddCharSet(pattern, FC_CHARSET, charSet); | 780 FcPatternAddCharSet(pattern, FC_CHARSET, charSet); |
(...skipping 13 matching lines...) Expand all Loading... |
794 FcResult result; | 794 FcResult result; |
795 SkAutoFcPattern font(FcFontMatch(fFC, pattern, &result)); | 795 SkAutoFcPattern font(FcFontMatch(fFC, pattern, &result)); |
796 if (NULL == font || !FontAccessible(font) || !FontContainsCharacter(font
, character)) { | 796 if (NULL == font || !FontAccessible(font) || !FontContainsCharacter(font
, character)) { |
797 return NULL; | 797 return NULL; |
798 } | 798 } |
799 | 799 |
800 return createTypefaceFromFcPattern(font); | 800 return createTypefaceFromFcPattern(font); |
801 } | 801 } |
802 | 802 |
803 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, | 803 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
804 const SkFontStyle& style) const SK_OVER
RIDE | 804 const SkFontStyle& style) const overrid
e |
805 { | 805 { |
806 //TODO: should the SkTypeface_fontconfig know its family? | 806 //TODO: should the SkTypeface_fontconfig know its family? |
807 const SkTypeface_fontconfig* fcTypeface = | 807 const SkTypeface_fontconfig* fcTypeface = |
808 static_cast<const SkTypeface_fontconfig*>(typeface); | 808 static_cast<const SkTypeface_fontconfig*>(typeface); |
809 return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY
), style); | 809 return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY
), style); |
810 } | 810 } |
811 | 811 |
812 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t SK_OVERRIDE { | 812 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
813 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 813 SkAutoTDelete<SkStreamAsset> stream(bareStream); |
814 const size_t length = stream->getLength(); | 814 const size_t length = stream->getLength(); |
815 if (length <= 0 || (1u << 30) < length) { | 815 if (length <= 0 || (1u << 30) < length) { |
816 return NULL; | 816 return NULL; |
817 } | 817 } |
818 | 818 |
819 SkFontStyle style; | 819 SkFontStyle style; |
820 bool isFixedWidth = false; | 820 bool isFixedWidth = false; |
821 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) { | 821 if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) { |
822 return NULL; | 822 return NULL; |
823 } | 823 } |
824 | 824 |
825 return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex, | 825 return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex, |
826 static_cast<SkStreamAsset*>(stream
.detach()))); | 826 static_cast<SkStreamAsset*>(stream
.detach()))); |
827 } | 827 } |
828 | 828 |
829 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE { | 829 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
830 return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcInd
ex); | 830 return this->createFromStream(SkNEW_ARGS(SkMemoryStream, (data)), ttcInd
ex); |
831 } | 831 } |
832 | 832 |
833 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERR
IDE { | 833 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
834 return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); | 834 return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); |
835 } | 835 } |
836 | 836 |
837 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 837 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
838 unsigned styleBits) const SK_OVER
RIDE { | 838 unsigned styleBits) const overrid
e { |
839 bool bold = styleBits & SkTypeface::kBold; | 839 bool bold = styleBits & SkTypeface::kBold; |
840 bool italic = styleBits & SkTypeface::kItalic; | 840 bool italic = styleBits & SkTypeface::kItalic; |
841 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight | 841 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight |
842 : SkFontStyle::kNormal_Weight, | 842 : SkFontStyle::kNormal_Weight, |
843 SkFontStyle::kNormal_Width, | 843 SkFontStyle::kNormal_Width, |
844 italic ? SkFontStyle::kItalic_Slant | 844 italic ? SkFontStyle::kItalic_Slant |
845 : SkFontStyle::kUpright_Slant); | 845 : SkFontStyle::kUpright_Slant); |
846 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); | 846 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); |
847 if (typeface.get()) { | 847 if (typeface.get()) { |
848 return typeface.detach(); | 848 return typeface.detach(); |
849 } | 849 } |
850 | 850 |
851 return this->matchFamilyStyle(NULL, style); | 851 return this->matchFamilyStyle(NULL, style); |
852 } | 852 } |
853 }; | 853 }; |
854 | 854 |
855 SkFontMgr* SkFontMgr::Factory() { | 855 SkFontMgr* SkFontMgr::Factory() { |
856 return SkNEW(SkFontMgr_fontconfig); | 856 return SkNEW(SkFontMgr_fontconfig); |
857 } | 857 } |
OLD | NEW |