1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkTypeface_DEFINED 11 #define SkTypeface_DEFINED 12 13 #include "SkAdvancedTypefaceMetrics.h" 14 #include "SkWeakRefCnt.h" 15 16 class SkDescriptor; 17 class SkFontDescriptor; 18 class SkScalerContext; 19 struct SkScalerContextRec; 20 class SkStream; 21 class SkAdvancedTypefaceMetrics; 22 class SkWStream; 23 24 typedef uint32_t SkFontID; 25 /** Machine endian. */ 26 typedef uint32_t SkFontTableTag; 27 28 /** \class SkTypeface 29 30 The SkTypeface class specifies the typeface and intrinsic style of a font. 31 This is used in the paint, along with optionally algorithmic settings like 32 textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify 33 how text appears when drawn (and measured). 34 35 Typeface objects are immutable, and so they can be shared between threads. 36 */ 37 class SK_API SkTypeface : public SkWeakRefCnt { 38 public: 39 SK_DECLARE_INST_COUNT(SkTypeface) 40 41 /** Style specifies the intrinsic style attributes of a given typeface 42 */ 43 enum Style { 44 kNormal = 0, 45 kBold = 0x01, 46 kItalic = 0x02, 47 48 // helpers 49 kBoldItalic = 0x03 50 }; 51 52 /** Returns the typeface's intrinsic style attributes 53 */ 54 Style style() const { return fStyle; } 55 56 /** Returns true if getStyle() has the kBold bit set. 57 */ 58 bool isBold() const { return (fStyle & kBold) != 0; } 59 60 /** Returns true if getStyle() has the kItalic bit set. 61 */ 62 bool isItalic() const { return (fStyle & kItalic) != 0; } 63 64 /** Returns true if the typeface claims to be fixed-pitch. 65 * This is a style bit, advance widths may vary even if this returns true. 66 */ 67 bool isFixedPitch() const { return fIsFixedPitch; } 68 69 /** Return a 32bit value for this typeface, unique for the underlying font 70 data. Will never return 0. 71 */ 72 SkFontID uniqueID() const { return fUniqueID; } 73 74 /** Return the uniqueID for the specified typeface. If the face is null, 75 resolve it to the default font and return its uniqueID. Will never 76 return 0. 77 */ 78 static SkFontID UniqueID(const SkTypeface* face); 79 80 /** Returns true if the two typefaces reference the same underlying font, 81 handling either being null (treating null as the default font) 82 */ 83 static bool Equal(const SkTypeface* facea, const SkTypeface* faceb); 84 85 /** 86 * Returns a ref() to the default typeface. The caller must call unref() 87 * when they are done referencing the object. Never returns NULL. 88 */ 89 static SkTypeface* RefDefault(Style style = SkTypeface::kNormal); 90 91 /** Return a new reference to the typeface that most closely matches the 92 requested familyName and style. Pass null as the familyName to return 93 the default font for the requested style. Will never return null 94 95 @param familyName May be NULL. The name of the font family. 96 @param style The style (normal, bold, italic) of the typeface. 97 @return reference to the closest-matching typeface. Call must call 98 unref() when they are done. 99 */ 100 static SkTypeface* CreateFromName(const char familyName[], Style style); 101 102 /** Return a new reference to the typeface that most closely matches the 103 requested typeface and specified Style. Use this call if you want to 104 pick a new style from the same family of the existing typeface. 105 If family is NULL, this selects from the default font's family. 106 107 @param family May be NULL. The name of the existing type face. 108 @param s The style (normal, bold, italic) of the type face. 109 @return reference to the closest-matching typeface. Call must call 110 unref() when they are done. 111 */ 112 static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s); 113 114 /** Return a new typeface given a file. If the file does not exist, or is 115 not a valid font file, returns null. 116 */ 117 static SkTypeface* CreateFromFile(const char path[]); 118 119 /** Return a new typeface given a stream. If the stream is 120 not a valid font file, returns null. Ownership of the stream is 121 transferred, so the caller must not reference it again. 122 */ 123 static SkTypeface* CreateFromStream(SkStream* stream); 124 125 /** Write a unique signature to a stream, sufficient to reconstruct a 126 typeface referencing the same font when Deserialize is called. 127 */ 128 void serialize(SkWStream*) const; 129 130 /** Given the data previously written by serialize(), return a new instance 131 to a typeface referring to the same font. If that font is not available, 132 return null. If an instance is returned, the caller is responsible for 133 calling unref() when they are done with it. 134 */ 135 static SkTypeface* Deserialize(SkStream*); 136 137 /** Retrieve detailed typeface metrics. Used by the PDF backend. 138 @param perGlyphInfo Indicate what glyph specific information (advances, 139 names, etc.) should be populated. 140 @param glyphIDs For per-glyph info, specify subset of the font by 141 giving glyph ids. Each integer represents a glyph 142 id. Passing NULL means all glyphs in the font. 143 @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if 144 glyphIDs is NULL. 145 @return The returned object has already been referenced. 146 */ 147 SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics( 148 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, 149 const uint32_t* glyphIDs = NULL, 150 uint32_t glyphIDsCount = 0) const; 151 152 enum Encoding { 153 kUTF8_Encoding, 154 kUTF16_Encoding, 155 kUTF32_Encoding 156 }; 157 158 /** 159 * Given an array of character codes, of the specified encoding, 160 * optionally return their corresponding glyph IDs (if glyphs is not NULL). 161 * 162 * @param chars pointer to the array of character codes 163 * @param encoding how the characteds are encoded 164 * @param glyphs (optional) returns the corresponding glyph IDs for each 165 * character code, up to glyphCount values. If a character code is 166 * not found in the typeface, the corresponding glyph ID will be 0. 167 * @param glyphCount number of code points in 'chars' to process. If glyphs 168 * is not NULL, then it must point sufficient memory to write 169 * glyphCount values into it. 170 * @return the number of number of continuous non-zero glyph IDs computed 171 * from the beginning of chars. This value is valid, even if the 172 * glyphs parameter is NULL. 173 */ 174 int charsToGlyphs(const void* chars, Encoding encoding, uint16_t glyphs[], 175 int glyphCount) const; 176 177 /** 178 * Return the number of glyphs in the typeface. 179 */ 180 int countGlyphs() const; 181 182 // Table getters -- may fail if the underlying font format is not organized 183 // as 4-byte tables. 184 185 /** Return the number of tables in the font. */ 186 int countTables() const; 187 188 /** Copy into tags[] (allocated by the caller) the list of table tags in 189 * the font, and return the number. This will be the same as CountTables() 190 * or 0 if an error occured. If tags == NULL, this only returns the count 191 * (the same as calling countTables()). 192 */ 193 int getTableTags(SkFontTableTag tags[]) const; 194 195 /** Given a table tag, return the size of its contents, or 0 if not present 196 */ 197 size_t getTableSize(SkFontTableTag) const; 198 199 /** Copy the contents of a table into data (allocated by the caller). Note 200 * that the contents of the table will be in their native endian order 201 * (which for most truetype tables is big endian). If the table tag is 202 * not found, or there is an error copying the data, then 0 is returned. 203 * If this happens, it is possible that some or all of the memory pointed 204 * to by data may have been written to, even though an error has occured. 205 * 206 * @param fontID the font to copy the table from 207 * @param tag The table tag whose contents are to be copied 208 * @param offset The offset in bytes into the table's contents where the 209 * copy should start from. 210 * @param length The number of bytes, starting at offset, of table data 211 * to copy. 212 * @param data storage address where the table contents are copied to 213 * @return the number of bytes actually copied into data. If offset+length 214 * exceeds the table's size, then only the bytes up to the table's 215 * size are actually copied, and this is the value returned. If 216 * offset > the table's size, or tag is not a valid table, 217 * then 0 is returned. 218 */ 219 size_t getTableData(SkFontTableTag tag, size_t offset, size_t length, 220 void* data) const; 221 222 /** 223 * Return the units-per-em value for this typeface, or zero if there is an 224 * error. 225 */ 226 int getUnitsPerEm() const; 227 228 struct LocalizedString { 229 SkString fString; 230 SkString fLanguage; 231 }; 232 class LocalizedStrings : ::SkNoncopyable { 233 public: 234 virtual ~LocalizedStrings() { } 235 virtual bool next(LocalizedString* localizedString) = 0; 236 void unref() { SkDELETE(this); } 237 }; 238 /** 239 * Returns an iterator which will attempt to enumerate all of the 240 * family names specified by the font. 241 * It is the caller's responsibility to unref() the returned pointer. 242 */ 243 LocalizedStrings* createFamilyNameIterator() const; 244 245 /** 246 * Return the family name for this typeface. It will always be returned 247 * encoded as UTF8, but the language of the name is whatever the host 248 * platform chooses. 249 */ 250 void getFamilyName(SkString* name) const; 251 252 /** 253 * Return a stream for the contents of the font data, or NULL on failure. 254 * If ttcIndex is not null, it is set to the TrueTypeCollection index 255 * of this typeface within the stream, or 0 if the stream is not a 256 * collection. 257 */ 258 SkStream* openStream(int* ttcIndex) const; 259 260 /** 261 * Search within this typeface's family for a best match to the 262 * specified style, and return a ref to that typeface. Note: the 263 * returned object could be this, if it is the best match, or it 264 * could be a different typeface. Either way, the caller must balance 265 * this call with unref() on the returned object. 266 * 267 * Will never return NULL. 268 */ 269 SkTypeface* refMatchingStyle(Style) const; 270 271 /** 272 * Return a scalercontext for the given descriptor. If this fails, then 273 * if allowFailure is true, this returns NULL, else it returns a 274 * dummy scalercontext that will not crash, but will draw nothing. 275 */ 276 SkScalerContext* createScalerContext(const SkDescriptor*, 277 bool allowFailure = false) const; 278 279 // PRIVATE / EXPERIMENTAL -- do not call 280 void filterRec(SkScalerContextRec* rec) const { 281 this->onFilterRec(rec); 282 } 283 // PRIVATE / EXPERIMENTAL -- do not call 284 void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const { 285 this->onGetFontDescriptor(desc, isLocal); 286 } 287 288 protected: 289 /** uniqueID must be unique and non-zero 290 */ 291 SkTypeface(Style style, SkFontID uniqueID, bool isFixedPitch = false); 292 virtual ~SkTypeface(); 293 294 /** Sets the fixedPitch bit. If used, must be called in the constructor. */ 295 void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; } 296 297 friend class SkScalerContext; 298 static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal); 299 300 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const = 0; 301 virtual void onFilterRec(SkScalerContextRec*) const = 0; 302 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 303 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, 304 const uint32_t* glyphIDs, 305 uint32_t glyphIDsCount) const = 0; 306 virtual SkStream* onOpenStream(int* ttcIndex) const = 0; 307 virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0; 308 309 virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[], 310 int glyphCount) const; 311 virtual int onCountGlyphs() const = 0; 312 313 virtual int onGetUPEM() const = 0; 314 315 virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0; 316 317 virtual int onGetTableTags(SkFontTableTag tags[]) const = 0; 318 virtual size_t onGetTableData(SkFontTableTag, size_t offset, 319 size_t length, void* data) const = 0; 320 321 virtual SkTypeface* onRefMatchingStyle(Style styleBits) const = 0; 322 323 private: 324 SkFontID fUniqueID; 325 Style fStyle; 326 bool fIsFixedPitch; 327 328 friend class SkPaint; 329 friend class SkGlyphCache; // GetDefaultTypeface 330 // just so deprecated fonthost can call protected methods 331 friend class SkFontHost; 332 333 typedef SkWeakRefCnt INHERITED; 334 }; 335 336 #endif 337