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 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override; 72 73 private: 74 const std::unique_ptr<const SkFontData> fData; 75 76 using INHERITED = SkTypeface_Custom; 77 }; 78 79 /** The file SkTypeface implementation for the custom font manager. */ 80 class SkTypeface_File : public SkTypeface_Custom { 81 public: 82 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, 83 const SkString familyName, const char path[], int index); 84 85 protected: 86 std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; 87 sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; 88 std::unique_ptr<SkFontData> onMakeFontData() const override; 89 90 private: 91 SkString fPath; 92 93 using INHERITED = SkTypeface_Custom; 94 }; 95 96 /////////////////////////////////////////////////////////////////////////////// 97 98 /** 99 * SkFontStyleSet_Custom 100 * 101 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. 102 */ 103 class SkFontStyleSet_Custom : public SkFontStyleSet { 104 public: 105 explicit SkFontStyleSet_Custom(const SkString familyName); 106 107 /** Should only be called during the initial build phase. */ 108 void appendTypeface(sk_sp<SkTypeface_Custom> typeface); 109 int count() override; 110 void getStyle(int index, SkFontStyle* style, SkString* name) override; 111 SkTypeface* createTypeface(int index) override; 112 SkTypeface* matchStyle(const SkFontStyle& pattern) override; 113 SkString getFamilyName(); 114 115 private: 116 SkTArray<sk_sp<SkTypeface_Custom>> fStyles; 117 SkString fFamilyName; 118 119 friend class SkFontMgr_Custom; 120 }; 121 122 /** 123 * SkFontMgr_Custom 124 * 125 * This class is essentially a collection of SkFontStyleSet_Custom, 126 * one SkFontStyleSet_Custom for each family. This class may be modified 127 * to load fonts from any source by changing the initialization. 128 */ 129 class SkFontMgr_Custom : public SkFontMgr { 130 public: 131 typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families; 132 class SystemFontLoader { 133 public: ~SystemFontLoader()134 virtual ~SystemFontLoader() { } 135 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0; 136 }; 137 explicit SkFontMgr_Custom(const SystemFontLoader& loader); 138 139 protected: 140 int onCountFamilies() const override; 141 void onGetFamilyName(int index, SkString* familyName) const override; 142 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override; 143 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override; 144 SkTypeface* onMatchFamilyStyle(const char familyName[], 145 const SkFontStyle& fontStyle) const override; 146 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 147 const char* bcp47[], int bcp47Count, 148 SkUnichar character) const override; 149 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override; 150 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override; 151 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const override; 152 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override; 153 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override; 154 155 private: 156 Families fFamilies; 157 SkFontStyleSet_Custom* fDefaultFamily; 158 SkTypeface_FreeType::Scanner fScanner; 159 }; 160 161 #endif 162