1 /* 2 * Copyright 2006 The Android Open Source Project 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 #ifndef SkFontMgr_custom_DEFINED 9 #define SkFontMgr_custom_DEFINED 10 11 #include "include/core/SkFontMgr.h" 12 #include "include/core/SkFontStyle.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkString.h" 15 #include "include/core/SkTypes.h" 16 #include "include/private/SkTArray.h" 17 #include "src/ports/SkFontHost_FreeType_common.h" 18 19 class SkData; 20 class SkFontDescriptor; 21 class SkStreamAsset; 22 class SkTypeface; 23 24 /** The base SkTypeface implementation for the custom font manager. */ 25 class SkTypeface_Custom : public SkTypeface_FreeType { 26 public: 27 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch, 28 bool sysFont, const SkString familyName, int index); 29 bool isSysFont() const; 30 31 protected: 32 void onGetFamilyName(SkString* familyName) const override; 33 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; 34 int getIndex() const; 35 36 private: 37 const bool fIsSysFont; 38 const SkString fFamilyName; 39 const int fIndex; 40 41 using INHERITED = SkTypeface_FreeType; 42 }; 43 44 /** The empty SkTypeface implementation for the custom font manager. 45 * Used as the last resort fallback typeface. 46 */ 47 class SkTypeface_Empty : public SkTypeface_Custom { 48 public: 49 SkTypeface_Empty() ; 50 51 protected: 52 std::unique_ptr<SkStreamAsset> onOpenStream(int*) const override; 53 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; 54 std::unique_ptr<SkFontData> onMakeFontData() const override; 55 56 private: 57 using INHERITED = SkTypeface_Custom; 58 }; 59 60 /** The stream SkTypeface implementation for the custom font manager. */ 61 class SkTypeface_Stream : public SkTypeface_Custom { 62 public: 63 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData, 64 const SkFontStyle& style, bool isFixedPitch, bool sysFont, 65 const SkString familyName); 66 67 protected: 68 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; 69 std::unique_ptr<SkFontData> onMakeFontData() const override; 70 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; 71 72 private: 73 const std::unique_ptr<const SkFontData> fData; 74 75 using INHERITED = SkTypeface_Custom; 76 }; 77 78 /** The file SkTypeface implementation for the custom font manager. */ 79 class SkTypeface_File : public SkTypeface_Custom { 80 public: 81 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, 82 const SkString familyName, const char path[], int index); 83 84 protected: 85 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; 86 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; 87 std::unique_ptr<SkFontData> onMakeFontData() const override; 88 89 private: 90 SkString fPath; 91 92 using INHERITED = SkTypeface_Custom; 93 }; 94 95 /////////////////////////////////////////////////////////////////////////////// 96 97 /** 98 * SkFontStyleSet_Custom 99 * 100 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. 101 */ 102 class SkFontStyleSet_Custom : public SkFontStyleSet { 103 public: 104 explicit SkFontStyleSet_Custom(const SkString familyName); 105 106 /** Should only be called during the initial build phase. */ 107 void appendTypeface(sk_sp<SkTypeface_Custom> typeface); 108 int count() override; 109 void getStyle(int index, SkFontStyle* style, SkString* name) override; 110 SkTypeface* createTypeface(int index) override; 111 SkTypeface* matchStyle(const SkFontStyle& pattern) override; 112 SkString getFamilyName(); 113 114 private: 115 SkTArray<sk_sp<SkTypeface_Custom>> fStyles; 116 SkString fFamilyName; 117 118 friend class SkFontMgr_Custom; 119 }; 120 121 /** 122 * SkFontMgr_Custom 123 * 124 * This class is essentially a collection of SkFontStyleSet_Custom, 125 * one SkFontStyleSet_Custom for each family. This class may be modified 126 * to load fonts from any source by changing the initialization. 127 */ 128 class SkFontMgr_Custom : public SkFontMgr { 129 public: 130 typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families; 131 class SystemFontLoader { 132 public: ~SystemFontLoader()133 virtual ~SystemFontLoader() { } 134 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0; 135 }; 136 explicit SkFontMgr_Custom(const SystemFontLoader& loader); 137 138 protected: 139 int onCountFamilies() const override; 140 void onGetFamilyName(int index, SkString* familyName) const override; 141 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override; 142 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override; 143 SkTypeface* onMatchFamilyStyle(const char familyName[], 144 const SkFontStyle& fontStyle) const override; 145 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 146 const char* bcp47[], int bcp47Count, 147 SkUnichar character) const override; 148 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override; 149 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override; 150 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override; 151 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override; 152 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override; 153 154 private: 155 Families fFamilies; 156 SkFontStyleSet_Custom* fDefaultFamily; 157 SkTypeface_FreeType::Scanner fScanner; 158 }; 159 160 #endif 161