1 /* 2 * Copyright 2013 Google Inc. 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_DEFINED 9 #define SkFontMgr_DEFINED 10 11 #include "include/core/SkFontArguments.h" 12 #include "include/core/SkFontStyle.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkTypes.h" 15 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \ 16 defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX) 17 #include <string> 18 #endif 19 20 class SkData; 21 class SkFontData; 22 class SkStreamAsset; 23 class SkString; 24 class SkTypeface; 25 26 class SK_API SkFontStyleSet : public SkRefCnt { 27 public: 28 virtual int count() = 0; 29 virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0; 30 virtual SkTypeface* createTypeface(int index) = 0; 31 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0; 32 33 static SkFontStyleSet* CreateEmpty(); 34 35 protected: 36 SkTypeface* matchStyleCSS3(const SkFontStyle& pattern); 37 38 private: 39 using INHERITED = SkRefCnt; 40 }; 41 42 class SK_API SkFontMgr : public SkRefCnt { 43 public: 44 int countFamilies() const; 45 void getFamilyName(int index, SkString* familyName) const; 46 SkFontStyleSet* createStyleSet(int index) const; 47 48 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \ 49 defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX) 50 /** 51 * OHOS_Container font base path. It is empty when using OpenHarmony fonts. 52 */ 53 static std::string containerFontPath; 54 /** 55 * Indicate the runtimeOS of preview(OHOS_Container and OHOS) 56 */ 57 static std::string runtimeOS; 58 #endif 59 /** 60 * The caller must call unref() on the returned object. 61 * Never returns NULL; will return an empty set if the name is not found. 62 * 63 * Passing nullptr as the parameter will return the default system family. 64 * Note that most systems don't have a default system family, so passing nullptr will often 65 * result in the empty set. 66 * 67 * It is possible that this will return a style set not accessible from 68 * createStyleSet(int) due to hidden or auto-activated fonts. 69 */ 70 SkFontStyleSet* matchFamily(const char familyName[]) const; 71 72 /** 73 * Find the closest matching typeface to the specified familyName and style 74 * and return a ref to it. The caller must call unref() on the returned 75 * object. Will return nullptr if no 'good' match is found. 76 * 77 * Passing |nullptr| as the parameter for |familyName| will return the 78 * default system font. 79 * 80 * It is possible that this will return a style set not accessible from 81 * createStyleSet(int) or matchFamily(const char[]) due to hidden or 82 * auto-activated fonts. 83 */ 84 SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const; 85 86 /** 87 * Use the system fallback to find a typeface for the given character. 88 * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes, 89 * so it is fine to just pass a ISO 639 here. 90 * 91 * Will return NULL if no family can be found for the character 92 * in the system fallback. 93 * 94 * Passing |nullptr| as the parameter for |familyName| will return the 95 * default system font. 96 * 97 * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the 98 * most significant. If no specified bcp47 codes match, any font with the 99 * requested character will be matched. 100 */ 101 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 102 const char* bcp47[], int bcp47Count, 103 SkUnichar character) const; 104 105 /** 106 * Create a typeface for the specified data and TTC index (pass 0 for none) 107 * or NULL if the data is not recognized. The caller must call unref() on 108 * the returned object if it is not null. 109 */ 110 sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const; 111 112 /** 113 * Create a typeface for the specified stream and TTC index 114 * (pass 0 for none) or NULL if the stream is not recognized. The caller 115 * must call unref() on the returned object if it is not null. 116 */ 117 sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const; 118 119 /* Experimental, API subject to change. */ 120 sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const; 121 122 /** 123 * Create a typeface for the specified fileName and TTC index 124 * (pass 0 for none) or NULL if the file is not found, or its contents are 125 * not recognized. The caller must call unref() on the returned object 126 * if it is not null. 127 */ 128 sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const; 129 130 sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const; 131 132 /** Return the default fontmgr. */ 133 static sk_sp<SkFontMgr> RefDefault(); 134 135 /* Returns an empty font manager without any typeface dependencies */ 136 static sk_sp<SkFontMgr> RefEmpty(); 137 138 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \ 139 defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX) 140 /** Set the runtimeOS and container font base path */ SetFontMgrConfig(const std::string runtime,const std::string containerFontBasePath)141 static void SetFontMgrConfig(const std::string runtime, const std::string containerFontBasePath) 142 { 143 containerFontPath = containerFontBasePath; 144 runtimeOS = runtime; 145 } 146 #endif 147 148 protected: 149 virtual int onCountFamilies() const = 0; 150 virtual void onGetFamilyName(int index, SkString* familyName) const = 0; 151 virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0; 152 153 /** May return NULL if the name is not found. */ 154 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0; 155 156 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], 157 const SkFontStyle&) const = 0; 158 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 159 const char* bcp47[], int bcp47Count, 160 SkUnichar character) const = 0; 161 162 virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0; 163 virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, 164 int ttcIndex) const = 0; 165 virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>, 166 const SkFontArguments&) const = 0; 167 virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0; 168 169 virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0; 170 171 // this method is never called -- will be removed onMatchFaceStyle(const SkTypeface *,const SkFontStyle &)172 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, 173 const SkFontStyle&) const { 174 return nullptr; 175 } 176 177 private: 178 179 /** Implemented by porting layer to return the default factory. */ 180 static sk_sp<SkFontMgr> Factory(); 181 182 using INHERITED = SkRefCnt; 183 }; 184 185 #endif 186