Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
SkFontMgr_fontconfig Class Reference
Inheritance diagram for SkFontMgr_fontconfig:
SkFontMgr SkRefCnt SkRefCntBase

Public Member Functions

 SkFontMgr_fontconfig (FcConfig *config)
 
 ~SkFontMgr_fontconfig () override
 
- Public Member Functions inherited from SkFontMgr
int countFamilies () const
 
void getFamilyName (int index, SkString *familyName) const
 
sk_sp< SkFontStyleSetcreateStyleSet (int index) const
 
sk_sp< SkFontStyleSetmatchFamily (const char familyName[]) const
 
sk_sp< SkTypefacematchFamilyStyle (const char familyName[], const SkFontStyle &) const
 
sk_sp< SkTypefacematchFamilyStyleCharacter (const char familyName[], const SkFontStyle &, const char *bcp47[], int bcp47Count, SkUnichar character) const
 
sk_sp< SkTypefacemakeFromData (sk_sp< SkData >, int ttcIndex=0) const
 
sk_sp< SkTypefacemakeFromStream (std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
 
sk_sp< SkTypefacemakeFromStream (std::unique_ptr< SkStreamAsset >, const SkFontArguments &) const
 
sk_sp< SkTypefacemakeFromFile (const char path[], int ttcIndex=0) const
 
sk_sp< SkTypefacelegacyMakeTypeface (const char familyName[], SkFontStyle style) const
 
- Public Member Functions inherited from SkRefCntBase
 SkRefCntBase ()
 
virtual ~SkRefCntBase ()
 
bool unique () const
 
void ref () const
 
void unref () const
 

Protected Member Functions

int onCountFamilies () const override
 
void onGetFamilyName (int index, SkString *familyName) const override
 
sk_sp< SkFontStyleSetonCreateStyleSet (int index) const override
 
bool FontAccessible (FcPattern *font) const
 
sk_sp< SkFontStyleSetonMatchFamily (const char familyName[]) const override
 
sk_sp< SkTypefaceonMatchFamilyStyle (const char familyName[], const SkFontStyle &style) const override
 
sk_sp< SkTypefaceonMatchFamilyStyleCharacter (const char familyName[], const SkFontStyle &style, const char *bcp47[], int bcp47Count, SkUnichar character) const override
 
sk_sp< SkTypefaceonMakeFromStreamIndex (std::unique_ptr< SkStreamAsset > stream, int ttcIndex) const override
 
sk_sp< SkTypefaceonMakeFromStreamArgs (std::unique_ptr< SkStreamAsset > stream, const SkFontArguments &args) const override
 
sk_sp< SkTypefaceonMakeFromData (sk_sp< SkData > data, int ttcIndex) const override
 
sk_sp< SkTypefaceonMakeFromFile (const char path[], int ttcIndex) const override
 
sk_sp< SkTypefaceonLegacyMakeTypeface (const char familyName[], SkFontStyle style) const override
 

Static Protected Member Functions

static bool AnyStringMatching (FcPattern *font, FcPattern *pattern, const char *object)
 
static bool FontFamilyNameMatches (FcPattern *font, FcPattern *pattern)
 
static bool FontContainsCharacter (FcPattern *font, uint32_t character)
 

Additional Inherited Members

- Static Public Member Functions inherited from SkFontMgr
static sk_sp< SkFontMgrRefEmpty ()
 

Detailed Description

Definition at line 535 of file SkFontMgr_fontconfig.cpp.

Constructor & Destructor Documentation

◆ SkFontMgr_fontconfig()

SkFontMgr_fontconfig::SkFontMgr_fontconfig ( FcConfig *  config)
inlineexplicit

Takes control of the reference to 'config'.

Definition at line 691 of file SkFontMgr_fontconfig.cpp.

692 : fFC(config ? config : FcInitLoadConfigAndFonts())
693 , fSysroot(reinterpret_cast<const char*>(FcConfigGetSysRoot(fFC)))
694 , fFamilyNames(GetFamilyNames(fFC)) { }

◆ ~SkFontMgr_fontconfig()

SkFontMgr_fontconfig::~SkFontMgr_fontconfig ( )
inlineoverride

Definition at line 696 of file SkFontMgr_fontconfig.cpp.

696 {
697 // Hold the lock while unrefing the config.
698 FCLocker lock;
699 fFC.reset();
700 }

Member Function Documentation

◆ AnyStringMatching()

static bool SkFontMgr_fontconfig::AnyStringMatching ( FcPattern *  font,
FcPattern *  pattern,
const char *  object 
)
inlinestaticprotected

True if any string object value in the font is the same as a string object value in the pattern.

Definition at line 718 of file SkFontMgr_fontconfig.cpp.

718 {
719 auto getStrings = [](FcPattern* p, const char* object, STArray<32, FcChar8*>& strings) {
720 // Set an arbitrary (but high) limit on the number of pattern object values to consider.
721 static constexpr const int maxId = 65536;
722 for (int patternId = 0; patternId < maxId; ++patternId) {
723 FcChar8* patternString;
724 FcResult result = FcPatternGetString(p, object, patternId, &patternString);
725 if (result == FcResultNoId) {
726 break;
727 }
728 if (result == FcResultMatch) {
729 strings.push_back(patternString);
730 }
731 }
732 };
733 auto compareStrings = [](FcChar8* a, FcChar8* b) -> bool {
734 return FcStrCmpIgnoreCase(a, b) < 0;
735 };
736
737 STArray<32, FcChar8*> fontStrings;
738 STArray<32, FcChar8*> patternStrings;
739 getStrings(font, object, fontStrings);
740 getStrings(pattern, object, patternStrings);
741
742 SkTQSort(fontStrings.begin(), fontStrings.end(), compareStrings);
743 SkTQSort(patternStrings.begin(), patternStrings.end(), compareStrings);
744
745 FcChar8** fontString = fontStrings.begin();
746 FcChar8** patternString = patternStrings.begin();
747 while (fontString != fontStrings.end() && patternString != patternStrings.end()) {
748 int cmp = FcStrCmpIgnoreCase(*fontString, *patternString);
749 if (cmp < 0) {
750 ++fontString;
751 } else if (cmp > 0) {
752 ++patternString;
753 } else {
754 return true;
755 }
756 }
757 return false;
758 }
void SkTQSort(T *begin, T *end, const C &lessThan)
Definition SkTSort.h:194
static bool b
struct MyStruct a[10]
GAsyncResult * result

◆ FontAccessible()

bool SkFontMgr_fontconfig::FontAccessible ( FcPattern *  font) const
inlineprotected

Definition at line 760 of file SkFontMgr_fontconfig.cpp.

760 {
761 // FontConfig can return fonts which are unreadable.
762 const char* filename = get_string(font, FC_FILE, nullptr);
763 if (nullptr == filename) {
764 return false;
765 }
766
767 // When sysroot was implemented in e96d7760886a3781a46b3271c76af99e15cb0146 (before 2.11.0)
768 // it was broken; mostly fixed in d17f556153fbaf8fe57fdb4fc1f0efa4313f0ecf (after 2.11.1).
769 // This leaves Debian 8 and 9 with broken support for this feature.
770 // As a result, this feature should not be used until at least 2.11.91.
771 // The broken support is mostly around not making all paths relative to the sysroot.
772 // However, even at 2.13.1 it is possible to get a mix of sysroot and non-sysroot paths,
773 // as any added file path not lexically starting with the sysroot will be unchanged.
774 // To allow users to add local app files outside the sysroot,
775 // prefer the sysroot but also look without the sysroot.
776 if (!fSysroot.isEmpty()) {
777 SkString resolvedFilename;
778 resolvedFilename = fSysroot;
779 resolvedFilename += filename;
780 if (sk_exists(resolvedFilename.c_str(), kRead_SkFILE_Flag)) {
781 return true;
782 }
783 }
784 return sk_exists(filename, kRead_SkFILE_Flag);
785 }
static const char * get_string(FcPattern *pattern, const char object[], const char *missing="")
bool sk_exists(const char *path, SkFILE_Flags=(SkFILE_Flags) 0)
@ kRead_SkFILE_Flag
Definition SkOSFile.h:20
bool isEmpty() const
Definition SkString.h:130
const char * c_str() const
Definition SkString.h:133

◆ FontContainsCharacter()

static bool SkFontMgr_fontconfig::FontContainsCharacter ( FcPattern *  font,
uint32_t  character 
)
inlinestaticprotected

Definition at line 791 of file SkFontMgr_fontconfig.cpp.

791 {
792 FcResult result;
793 FcCharSet* matchCharSet;
794 for (int charSetId = 0; ; ++charSetId) {
795 result = FcPatternGetCharSet(font, FC_CHARSET, charSetId, &matchCharSet);
796 if (FcResultNoId == result) {
797 break;
798 }
799 if (FcResultMatch != result) {
800 continue;
801 }
802 if (FcCharSetHasChar(matchCharSet, character)) {
803 return true;
804 }
805 }
806 return false;
807 }

◆ FontFamilyNameMatches()

static bool SkFontMgr_fontconfig::FontFamilyNameMatches ( FcPattern *  font,
FcPattern *  pattern 
)
inlinestaticprotected

Definition at line 787 of file SkFontMgr_fontconfig.cpp.

787 {
788 return AnyStringMatching(font, pattern, FC_FAMILY);
789 }
static bool AnyStringMatching(FcPattern *font, FcPattern *pattern, const char *object)

◆ onCountFamilies()

int SkFontMgr_fontconfig::onCountFamilies ( ) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 703 of file SkFontMgr_fontconfig.cpp.

703 {
704 return fFamilyNames->count();
705 }
int count() const
Definition SkDataTable.h:33

◆ onCreateStyleSet()

sk_sp< SkFontStyleSet > SkFontMgr_fontconfig::onCreateStyleSet ( int  index) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 711 of file SkFontMgr_fontconfig.cpp.

711 {
712 return this->onMatchFamily(fFamilyNames->atStr(index));
713 }
const char * atStr(int index) const
Definition SkDataTable.h:59
sk_sp< SkFontStyleSet > onMatchFamily(const char familyName[]) const override

◆ onGetFamilyName()

void SkFontMgr_fontconfig::onGetFamilyName ( int  index,
SkString familyName 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 707 of file SkFontMgr_fontconfig.cpp.

707 {
708 familyName->set(fFamilyNames->atStr(index));
709 }
void set(const SkString &src)
Definition SkString.h:186

◆ onLegacyMakeTypeface()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onLegacyMakeTypeface ( const char  familyName[],
SkFontStyle  style 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 960 of file SkFontMgr_fontconfig.cpp.

960 {
961 sk_sp<SkTypeface> typeface(this->matchFamilyStyle(familyName, style));
962 if (typeface) {
963 return typeface;
964 }
965
966 return sk_sp<SkTypeface>(this->matchFamilyStyle(nullptr, style));
967 }
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const

◆ onMakeFromData()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMakeFromData ( sk_sp< SkData data,
int  ttcIndex 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 952 of file SkFontMgr_fontconfig.cpp.

952 {
953 return this->makeFromStream(std::make_unique<SkMemoryStream>(std::move(data)), ttcIndex);
954 }
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const

◆ onMakeFromFile()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMakeFromFile ( const char  path[],
int  ttcIndex 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 956 of file SkFontMgr_fontconfig.cpp.

956 {
957 return this->makeFromStream(SkStream::MakeFromFile(path), ttcIndex);
958 }
static std::unique_ptr< SkStreamAsset > MakeFromFile(const char path[])
Definition SkStream.cpp:922

◆ onMakeFromStreamArgs()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMakeFromStreamArgs ( std::unique_ptr< SkStreamAsset stream,
const SkFontArguments args 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 943 of file SkFontMgr_fontconfig.cpp.

944 {
945 const size_t length = stream->getLength();
946 if (length <= 0 || (1u << 30) < length) {
947 return nullptr;
948 }
949 return SkTypeface_FreeType::MakeFromStream(std::move(stream), args);
950 }
static sk_sp< SkTypeface > MakeFromStream(std::unique_ptr< SkStreamAsset >, const SkFontArguments &)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
size_t length

◆ onMakeFromStreamIndex()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMakeFromStreamIndex ( std::unique_ptr< SkStreamAsset stream,
int  ttcIndex 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 937 of file SkFontMgr_fontconfig.cpp.

938 {
939 return this->makeFromStream(std::move(stream),
940 SkFontArguments().setCollectionIndex(ttcIndex));
941 }

◆ onMatchFamily()

sk_sp< SkFontStyleSet > SkFontMgr_fontconfig::onMatchFamily ( const char  familyName[]) const
inlineoverrideprotectedvirtual

May return NULL if the name is not found.

Implements SkFontMgr.

Definition at line 809 of file SkFontMgr_fontconfig.cpp.

809 {
810 if (!familyName) {
811 return nullptr;
812 }
813 FCLocker lock;
814
815 SkAutoFcPattern pattern;
816 FcPatternAddString(pattern, FC_FAMILY, (const FcChar8*)familyName);
817 FcConfigSubstitute(fFC, pattern, FcMatchPattern);
818 FcDefaultSubstitute(pattern);
819
820 FcPattern* matchPattern;
821 SkAutoFcPattern strongPattern(nullptr);
822 if (familyName) {
823 strongPattern.reset(FcPatternDuplicate(pattern));
824 remove_weak(strongPattern, FC_FAMILY);
825 matchPattern = strongPattern;
826 } else {
827 matchPattern = pattern;
828 }
829
831 // TODO: Some families have 'duplicates' due to symbolic links.
832 // The patterns are exactly the same except for the FC_FILE.
833 // It should be possible to collapse these patterns by normalizing.
834 static const FcSetName fcNameSet[] = { FcSetSystem, FcSetApplication };
835 for (int setIndex = 0; setIndex < (int)std::size(fcNameSet); ++setIndex) {
836 // Return value of FcConfigGetFonts must not be destroyed.
837 FcFontSet* allFonts(FcConfigGetFonts(fFC, fcNameSet[setIndex]));
838 if (nullptr == allFonts) {
839 continue;
840 }
841
842 for (int fontIndex = 0; fontIndex < allFonts->nfont; ++fontIndex) {
843 FcPattern* font = allFonts->fonts[fontIndex];
844 if (FontAccessible(font) && FontFamilyNameMatches(font, matchPattern)) {
845 FcFontSetAdd(matches, FcFontRenderPrepare(fFC, pattern, font));
846 }
847 }
848 }
849
850 return sk_sp<SkFontStyleSet>(new StyleSet(sk_ref_sp(this), std::move(matches)));
851 }
static void remove_weak(FcPattern *pattern, const char object[])
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
Type::kYUV Type::kRGBA() int(0.7 *637)
bool FontAccessible(FcPattern *font) const
static bool FontFamilyNameMatches(FcPattern *font, FcPattern *pattern)
font
Font Metadata and Metrics.

◆ onMatchFamilyStyle()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMatchFamilyStyle ( const char  familyName[],
const SkFontStyle style 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 853 of file SkFontMgr_fontconfig.cpp.

855 {
856 SkAutoFcPattern font([this, &familyName, &style]() {
857 FCLocker lock;
858
859 SkAutoFcPattern pattern;
860 FcPatternAddString(pattern, FC_FAMILY, (const FcChar8*)familyName);
861 fcpattern_from_skfontstyle(style, pattern);
862 FcConfigSubstitute(fFC, pattern, FcMatchPattern);
863 FcDefaultSubstitute(pattern);
864
865 // We really want to match strong (preferred) and same (acceptable) only here.
866 // If a family name was specified, assume that any weak matches after the last strong
867 // match are weak (default) and ignore them.
868 // After substitution the pattern for 'sans-serif' looks like "wwwwwwwwwwwwwwswww" where
869 // there are many weak but preferred names, followed by defaults.
870 // So it is possible to have weakly matching but preferred names.
871 // In aliases, bindings are weak by default, so this is easy and common.
872 // If no family name was specified, we'll probably only get weak matches, but that's ok.
873 FcPattern* matchPattern;
874 SkAutoFcPattern strongPattern(nullptr);
875 if (familyName) {
876 strongPattern.reset(FcPatternDuplicate(pattern));
877 remove_weak(strongPattern, FC_FAMILY);
878 matchPattern = strongPattern;
879 } else {
880 matchPattern = pattern;
881 }
882
883 FcResult result;
884 SkAutoFcPattern font(FcFontMatch(fFC, pattern, &result));
885 if (!font || !FontAccessible(font) || !FontFamilyNameMatches(font, matchPattern)) {
886 font.reset();
887 }
888 return font;
889 }());
890 return createTypefaceFromFcPattern(std::move(font));
891 }
static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern *pattern)

◆ onMatchFamilyStyleCharacter()

sk_sp< SkTypeface > SkFontMgr_fontconfig::onMatchFamilyStyleCharacter ( const char  familyName[],
const SkFontStyle style,
const char *  bcp47[],
int  bcp47Count,
SkUnichar  character 
) const
inlineoverrideprotectedvirtual

Implements SkFontMgr.

Definition at line 893 of file SkFontMgr_fontconfig.cpp.

898 {
899 SkAutoFcPattern font([&](){
900 FCLocker lock;
901
902 SkAutoFcPattern pattern;
903 if (familyName) {
904 FcValue familyNameValue;
905 familyNameValue.type = FcTypeString;
906 familyNameValue.u.s = reinterpret_cast<const FcChar8*>(familyName);
907 FcPatternAddWeak(pattern, FC_FAMILY, familyNameValue, FcFalse);
908 }
909 fcpattern_from_skfontstyle(style, pattern);
910
911 SkAutoFcCharSet charSet;
912 FcCharSetAddChar(charSet, character);
913 FcPatternAddCharSet(pattern, FC_CHARSET, charSet);
914
915 if (bcp47Count > 0) {
916 SkASSERT(bcp47);
917 SkAutoFcLangSet langSet;
918 for (int i = bcp47Count; i --> 0;) {
919 FcLangSetAdd(langSet, (const FcChar8*)bcp47[i]);
920 }
921 FcPatternAddLangSet(pattern, FC_LANG, langSet);
922 }
923
924 FcConfigSubstitute(fFC, pattern, FcMatchPattern);
925 FcDefaultSubstitute(pattern);
926
927 FcResult result;
928 SkAutoFcPattern font(FcFontMatch(fFC, pattern, &result));
929 if (!font || !FontAccessible(font) || !FontContainsCharacter(font, character)) {
930 font.reset();
931 }
932 return font;
933 }());
934 return createTypefaceFromFcPattern(std::move(font));
935 }
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool FontContainsCharacter(FcPattern *font, uint32_t character)

The documentation for this class was generated from the following file: