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