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 "SkFontStyle.h" 12 #include "SkRefCnt.h" 13 #include "SkScalar.h" 14 #include "SkTypes.h" 15 16 class SkData; 17 class SkFontData; 18 class SkStreamAsset; 19 class SkString; 20 class SkTypeface; 21 22 class SK_API SkFontStyleSet : public SkRefCnt { 23 public: 24 virtual int count() = 0; 25 virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0; 26 virtual SkTypeface* createTypeface(int index) = 0; 27 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0; 28 29 static SkFontStyleSet* CreateEmpty(); 30 31 protected: 32 SkTypeface* matchStyleCSS3(const SkFontStyle& pattern); 33 34 private: 35 typedef SkRefCnt INHERITED; 36 }; 37 38 class SK_API SkFontMgr : public SkRefCnt { 39 public: 40 int countFamilies() const; 41 void getFamilyName(int index, SkString* familyName) const; 42 SkFontStyleSet* createStyleSet(int index) const; 43 44 /** 45 * The caller must call unref() on the returned object. 46 * Never returns NULL; will return an empty set if the name is not found. 47 * 48 * Passing |nullptr| as the parameter will return the default system font. 49 * 50 * It is possible that this will return a style set not accessible from 51 * createStyleSet(int) due to hidden or auto-activated fonts. 52 */ 53 SkFontStyleSet* matchFamily(const char familyName[]) const; 54 55 /** 56 * Find the closest matching typeface to the specified familyName and style 57 * and return a ref to it. The caller must call unref() on the returned 58 * object. Will never return NULL, as it will return the default font if 59 * no matching font is found. 60 * 61 * Passing |nullptr| as the parameter for |familyName| will return the 62 * default system font. 63 * 64 * It is possible that this will return a style set not accessible from 65 * createStyleSet(int) or matchFamily(const char[]) due to hidden or 66 * auto-activated fonts. 67 */ 68 SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const; 69 70 /** 71 * Use the system fallback to find a typeface for the given character. 72 * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes, 73 * so it is fine to just pass a ISO 639 here. 74 * 75 * Will return NULL if no family can be found for the character 76 * in the system fallback. 77 * 78 * Passing |nullptr| as the parameter for |familyName| will return the 79 * default system font. 80 * 81 * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the 82 * most significant. If no specified bcp47 codes match, any font with the 83 * requested character will be matched. 84 */ 85 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 86 const char* bcp47[], int bcp47Count, 87 SkUnichar character) const; 88 89 SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const; 90 91 /** 92 * Create a typeface for the specified data and TTC index (pass 0 for none) 93 * or NULL if the data is not recognized. The caller must call unref() on 94 * the returned object if it is not null. 95 */ 96 SkTypeface* createFromData(SkData*, int ttcIndex = 0) const; 97 98 /** 99 * Create a typeface for the specified stream and TTC index 100 * (pass 0 for none) or NULL if the stream is not recognized. The caller 101 * must call unref() on the returned object if it is not null. 102 */ 103 SkTypeface* createFromStream(SkStreamAsset*, int ttcIndex = 0) const; 104 105 struct FontParameters { 106 struct Axis { 107 SkFourByteTag fTag; 108 SkScalar fStyleValue; 109 }; 110 FontParametersFontParameters111 FontParameters() : fCollectionIndex(0), fAxisCount(0), fAxes(nullptr) {} 112 113 /** Specify the index of the desired font. 114 * 115 * Font formats like ttc, dfont, cff, cid, pfr, t42, t1, and fon may actually be indexed 116 * collections of fonts. 117 */ setCollectionIndexFontParameters118 FontParameters& setCollectionIndex(int collectionIndex) { 119 fCollectionIndex = collectionIndex; 120 return *this; 121 } 122 123 /** Specify the GX variation axis values. 124 * 125 * Any axes not specified will use the default value. Specified axes not present in the 126 * font will be ignored. 127 * 128 * @param axes not copied. This pointer must remain valid for life of FontParameters. 129 */ setAxesFontParameters130 FontParameters& setAxes(const Axis* axes, int axisCount) { 131 fAxisCount = axisCount; 132 fAxes = axes; 133 return *this; 134 } 135 getCollectionIndexFontParameters136 int getCollectionIndex() const { 137 return fCollectionIndex; 138 } getAxesFontParameters139 const Axis* getAxes(int* axisCount) const { 140 *axisCount = fAxisCount; 141 return fAxes; 142 } 143 private: 144 int fCollectionIndex; 145 int fAxisCount; 146 const Axis* fAxes; 147 }; 148 /* Experimental, API subject to change. */ 149 SkTypeface* createFromStream(SkStreamAsset*, const FontParameters&) const; 150 151 /** 152 * Create a typeface from the specified font data. 153 * Takes ownership of the font data, so the caller should not reference it again. 154 * Will return NULL if the typeface could not be created. 155 * The caller must call unref() on the returned object if it is not null. 156 */ 157 SkTypeface* createFromFontData(SkFontData*) const; 158 159 /** 160 * Create a typeface for the specified fileName and TTC index 161 * (pass 0 for none) or NULL if the file is not found, or its contents are 162 * not recognized. The caller must call unref() on the returned object 163 * if it is not null. 164 */ 165 SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const; 166 167 SkTypeface* legacyCreateTypeface(const char familyName[], 168 unsigned typefaceStyleBits) const; 169 170 /** 171 * Return a ref to the default fontmgr. The caller must call unref() on 172 * the returned object. 173 */ 174 static SkFontMgr* RefDefault(); 175 176 protected: 177 virtual int onCountFamilies() const = 0; 178 virtual void onGetFamilyName(int index, SkString* familyName) const = 0; 179 virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0; 180 181 /** May return NULL if the name is not found. */ 182 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0; 183 184 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], 185 const SkFontStyle&) const = 0; 186 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, 187 const char* bcp47[], int bcp47Count, 188 SkUnichar character) const = 0; 189 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, 190 const SkFontStyle&) const = 0; 191 192 virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0; 193 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const = 0; 194 // TODO: make pure virtual. 195 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const FontParameters&) const; 196 virtual SkTypeface* onCreateFromFontData(SkFontData*) const; 197 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0; 198 199 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], 200 unsigned styleBits) const = 0; 201 private: 202 static SkFontMgr* Factory(); // implemented by porting layer 203 friend SkFontMgr* sk_fontmgr_create_default(); 204 205 typedef SkRefCnt INHERITED; 206 }; 207 208 #endif 209