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 "SkFontStyle.h" 14 #include "SkLazyPtr.h" 15 #include "SkRect.h" 16 #include "SkString.h" 17 #include "SkWeakRefCnt.h" 18 19 class SkDescriptor; 20 class SkFontDescriptor; 21 class SkScalerContext; 22 struct SkScalerContextRec; 23 class SkStream; 24 class SkStreamAsset; 25 class SkAdvancedTypefaceMetrics; 26 class SkWStream; 27 28 typedef uint32_t SkFontID; 29 /** Machine endian. */ 30 typedef uint32_t SkFontTableTag; 31 32 /** \class SkTypeface 33 34 The SkTypeface class specifies the typeface and intrinsic style of a font. 35 This is used in the paint, along with optionally algorithmic settings like 36 textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify 37 how text appears when drawn (and measured). 38 39 Typeface objects are immutable, and so they can be shared between threads. 40 */ 41 class SK_API SkTypeface : public SkWeakRefCnt { 42 public: 43 SK_DECLARE_INST_COUNT(SkTypeface) 44 45 /** Style specifies the intrinsic style attributes of a given typeface 46 */ 47 enum Style { 48 kNormal = 0, 49 kBold = 0x01, 50 kItalic = 0x02, 51 52 // helpers 53 kBoldItalic = 0x03 54 }; 55 56 /** Returns the typeface's intrinsic style attributes. */ 57 SkFontStyle fontStyle() const { 58 return fStyle; 59 } 60 61 /** Returns the typeface's intrinsic style attributes. 62 * @deprecated use fontStyle() instead. 63 */ 64 Style style() const { 65 return static_cast<Style>( 66 (fStyle.weight() >= SkFontStyle::kSemiBold_Weight ? kBold : kNormal) | 67 (fStyle.slant() != SkFontStyle::kUpright_Slant ? kItalic : kNormal)); 68 } 69 70 /** Returns true if style() has the kBold bit set. */ 71 bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; } 72 73 /** Returns true if style() has the kItalic bit set. */ 74 bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; } 75 76 /** Returns true if the typeface claims to be fixed-pitch. 77 * This is a style bit, advance widths may vary even if this returns true. 78 */ 79 bool isFixedPitch() const { return fIsFixedPitch; } 80 81 /** Return a 32bit value for this typeface, unique for the underlying font 82 data. Will never return 0. 83 */ 84 SkFontID uniqueID() const { return fUniqueID; } 85 86 /** Return the uniqueID for the specified typeface. If the face is null, 87 resolve it to the default font and return its uniqueID. Will never 88 return 0. 89 */ 90 static SkFontID UniqueID(const SkTypeface* face); 91 92 /** Returns true if the two typefaces reference the same underlying font, 93 handling either being null (treating null as the default font) 94 */ 95 static bool Equal(const SkTypeface* facea, const SkTypeface* faceb); 96 97 /** 98 * Returns a ref() to the default typeface. The caller must call unref() 99 * when they are done referencing the object. Never returns NULL. 100 */ 101 static SkTypeface* RefDefault(Style style = SkTypeface::kNormal); 102 103 /** Return a new reference to the typeface that most closely matches the 104 requested familyName and style. Pass null as the familyName to return 105 the default font for the requested style. Will never return null 106 107 @param familyName May be NULL. The name of the font family. 108 @param style The style (normal, bold, italic) of the typeface. 109 @return reference to the closest-matching typeface. Call must call 110 unref() when they are done. 111 */ 112 static SkTypeface* CreateFromName(const char familyName[], Style style); 113 114 /** Return a new reference to the typeface that most closely matches the 115 requested typeface and specified Style. Use this call if you want to 116 pick a new style from the same family of the existing typeface. 117 If family is NULL, this selects from the default font's family. 118 119 @param family May be NULL. The name of the existing type face. 120 @param s The style (normal, bold, italic) of the type face. 121 @return reference to the closest-matching typeface. Call must call 122 unref() when they are done. 123 */ 124 static SkTypeface* CreateFromTypeface(const SkTypeface* family, Style s); 125 126 /** Return a new typeface given a file. If the file does not exist, or is 127 not a valid font file, returns null. 128 */ 129 static SkTypeface* CreateFromFile(const char path[], int index = 0); 130 131 /** Return a new typeface given a stream. If the stream is 132 not a valid font file, returns null. Ownership of the stream is 133 transferred, so the caller must not reference it again. 134 */ 135 static SkTypeface* CreateFromStream(SkStreamAsset* stream, int index = 0); 136 137 /** Write a unique signature to a stream, sufficient to reconstruct a 138 typeface referencing the same font when Deserialize is called. 139 */ 140 void serialize(SkWStream*) const; 141 142 /** Like serialize, but write the whole font (not just a signature) if possible. 143 */ 144 void serializeForcingEmbedding(SkWStream*) const; 145 146 /** Given the data previously written by serialize(), return a new instance 147 to a typeface referring to the same font. If that font is not available, 148 return null. If an instance is returned, the caller is responsible for 149 calling unref() when they are done with it. 150 Does not affect ownership of SkStream. 151 */ 152 static SkTypeface* Deserialize(SkStream*); 153 154 enum Encoding { 155 kUTF8_Encoding, 156 kUTF16_Encoding, 157 kUTF32_Encoding 158 }; 159 160 /** 161 * Given an array of character codes, of the specified encoding, 162 * optionally return their corresponding glyph IDs (if glyphs is not NULL). 163 * 164 * @param chars pointer to the array of character codes 165 * @param encoding how the characters are encoded 166 * @param glyphs (optional) returns the corresponding glyph IDs for each 167 * character code, up to glyphCount values. If a character code is 168 * not found in the typeface, the corresponding glyph ID will be 0. 169 * @param glyphCount number of code points in 'chars' to process. If glyphs 170 * is not NULL, then it must point sufficient memory to write 171 * glyphCount values into it. 172 * @return the number of number of continuous non-zero glyph IDs computed 173 * from the beginning of chars. This value is valid, even if the 174 * glyphs parameter is NULL. 175 */ 176 int charsToGlyphs(const void* chars, Encoding encoding, uint16_t glyphs[], 177 int glyphCount) const; 178 179 /** 180 * Return the number of glyphs in the typeface. 181 */ 182 int countGlyphs() const; 183 184 // Table getters -- may fail if the underlying font format is not organized 185 // as 4-byte tables. 186 187 /** Return the number of tables in the font. */ 188 int countTables() const; 189 190 /** Copy into tags[] (allocated by the caller) the list of table tags in 191 * the font, and return the number. This will be the same as CountTables() 192 * or 0 if an error occured. If tags == NULL, this only returns the count 193 * (the same as calling countTables()). 194 */ 195 int getTableTags(SkFontTableTag tags[]) const; 196 197 /** Given a table tag, return the size of its contents, or 0 if not present 198 */ 199 size_t getTableSize(SkFontTableTag) const; 200 201 /** Copy the contents of a table into data (allocated by the caller). Note 202 * that the contents of the table will be in their native endian order 203 * (which for most truetype tables is big endian). If the table tag is 204 * not found, or there is an error copying the data, then 0 is returned. 205 * If this happens, it is possible that some or all of the memory pointed 206 * to by data may have been written to, even though an error has occured. 207 * 208 * @param fontID the font to copy the table from 209 * @param tag The table tag whose contents are to be copied 210 * @param offset The offset in bytes into the table's contents where the 211 * copy should start from. 212 * @param length The number of bytes, starting at offset, of table data 213 * to copy. 214 * @param data storage address where the table contents are copied to 215 * @return the number of bytes actually copied into data. If offset+length 216 * exceeds the table's size, then only the bytes up to the table's 217 * size are actually copied, and this is the value returned. If 218 * offset > the table's size, or tag is not a valid table, 219 * then 0 is returned. 220 */ 221 size_t getTableData(SkFontTableTag tag, size_t offset, size_t length, 222 void* data) const; 223 224 /** 225 * Return the units-per-em value for this typeface, or zero if there is an 226 * error. 227 */ 228 int getUnitsPerEm() const; 229 230 /** 231 * Given a run of glyphs, return the associated horizontal adjustments. 232 * Adjustments are in "design units", which are integers relative to the 233 * typeface's units per em (see getUnitsPerEm). 234 * 235 * Some typefaces are known to never support kerning. Calling this method 236 * with all zeros (e.g. getKerningPairAdustments(NULL, 0, NULL)) returns 237 * a boolean indicating if the typeface might support kerning. If it 238 * returns false, then it will always return false (no kerning) for all 239 * possible glyph runs. If it returns true, then it *may* return true for 240 * somne glyph runs. 241 * 242 * If count is non-zero, then the glyphs parameter must point to at least 243 * [count] valid glyph IDs, and the adjustments parameter must be 244 * sized to at least [count - 1] entries. If the method returns true, then 245 * [count-1] entries in the adjustments array will be set. If the method 246 * returns false, then no kerning should be applied, and the adjustments 247 * array will be in an undefined state (possibly some values may have been 248 * written, but none of them should be interpreted as valid values). 249 */ 250 bool getKerningPairAdjustments(const uint16_t glyphs[], int count, 251 int32_t adjustments[]) const; 252 253 struct LocalizedString { 254 SkString fString; 255 SkString fLanguage; 256 }; 257 class LocalizedStrings : ::SkNoncopyable { 258 public: 259 virtual ~LocalizedStrings() { } 260 virtual bool next(LocalizedString* localizedString) = 0; 261 void unref() { SkDELETE(this); } 262 }; 263 /** 264 * Returns an iterator which will attempt to enumerate all of the 265 * family names specified by the font. 266 * It is the caller's responsibility to unref() the returned pointer. 267 */ 268 LocalizedStrings* createFamilyNameIterator() const; 269 270 /** 271 * Return the family name for this typeface. It will always be returned 272 * encoded as UTF8, but the language of the name is whatever the host 273 * platform chooses. 274 */ 275 void getFamilyName(SkString* name) const; 276 277 /** 278 * Return a stream for the contents of the font data, or NULL on failure. 279 * If ttcIndex is not null, it is set to the TrueTypeCollection index 280 * of this typeface within the stream, or 0 if the stream is not a 281 * collection. 282 * The caller is responsible for deleting the stream. 283 */ 284 SkStreamAsset* openStream(int* ttcIndex) const; 285 286 /** 287 * Return a scalercontext for the given descriptor. If this fails, then 288 * if allowFailure is true, this returns NULL, else it returns a 289 * dummy scalercontext that will not crash, but will draw nothing. 290 */ 291 SkScalerContext* createScalerContext(const SkDescriptor*, 292 bool allowFailure = false) const; 293 294 /** 295 * Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all 296 * of the glyphs, but each one positioned at (0,). This may be conservatively large, and 297 * will not take into account any hinting or other size-specific adjustments. 298 */ 299 SkRect getBounds() const; 300 301 // PRIVATE / EXPERIMENTAL -- do not call 302 void filterRec(SkScalerContextRec* rec) const { 303 this->onFilterRec(rec); 304 } 305 // PRIVATE / EXPERIMENTAL -- do not call 306 void getFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const { 307 this->onGetFontDescriptor(desc, isLocal); 308 } 309 310 protected: 311 // The type of advance data wanted. 312 enum PerGlyphInfo { 313 kNo_PerGlyphInfo = 0x0, // Don't populate any per glyph info. 314 kHAdvance_PerGlyphInfo = 0x1, // Populate horizontal advance data. 315 kVAdvance_PerGlyphInfo = 0x2, // Populate vertical advance data. 316 kGlyphNames_PerGlyphInfo = 0x4, // Populate glyph names (Type 1 only). 317 kToUnicode_PerGlyphInfo = 0x8 // Populate ToUnicode table, ignored 318 // for Type 1 fonts 319 }; 320 321 /** uniqueID must be unique and non-zero 322 */ 323 SkTypeface(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch = false); 324 virtual ~SkTypeface(); 325 326 /** Sets the fixedPitch bit. If used, must be called in the constructor. */ 327 void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; } 328 329 friend class SkScalerContext; 330 static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal); 331 332 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const = 0; 333 virtual void onFilterRec(SkScalerContextRec*) const = 0; 334 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 335 PerGlyphInfo, 336 const uint32_t* glyphIDs, 337 uint32_t glyphIDsCount) const = 0; 338 339 virtual SkStreamAsset* onOpenStream(int* ttcIndex) const = 0; 340 virtual void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const = 0; 341 342 virtual int onCharsToGlyphs(const void* chars, Encoding, uint16_t glyphs[], 343 int glyphCount) const = 0; 344 virtual int onCountGlyphs() const = 0; 345 346 virtual int onGetUPEM() const = 0; 347 virtual bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count, 348 int32_t adjustments[]) const; 349 350 /** Returns the family name of the typeface as known by its font manager. 351 * This name may or may not be produced by the family name iterator. 352 */ 353 virtual void onGetFamilyName(SkString* familyName) const = 0; 354 355 /** Returns an iterator over the family names in the font. */ 356 virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0; 357 358 virtual int onGetTableTags(SkFontTableTag tags[]) const = 0; 359 virtual size_t onGetTableData(SkFontTableTag, size_t offset, 360 size_t length, void* data) const = 0; 361 362 virtual bool onComputeBounds(SkRect*) const; 363 364 private: 365 friend class SkGTypeface; 366 friend class SkPDFFont; 367 friend class SkPDFCIDFont; 368 friend class GrPathRendering; 369 friend class GrGLPathRendering; 370 371 /** Retrieve detailed typeface metrics. Used by the PDF backend. 372 @param perGlyphInfo Indicate what glyph specific information (advances, 373 names, etc.) should be populated. 374 @param glyphIDs For per-glyph info, specify subset of the font by 375 giving glyph ids. Each integer represents a glyph 376 id. Passing NULL means all glyphs in the font. 377 @param glyphIDsCount Number of elements in subsetGlyphIds. Ignored if 378 glyphIDs is NULL. 379 @return The returned object has already been referenced. 380 */ 381 SkAdvancedTypefaceMetrics* getAdvancedTypefaceMetrics( 382 PerGlyphInfo, 383 const uint32_t* glyphIDs = NULL, 384 uint32_t glyphIDsCount = 0) const; 385 386 private: 387 static SkTypeface* CreateDefault(int style); // SkLazyPtr requires an int, not a Style. 388 static void DeleteDefault(SkTypeface*); 389 390 struct BoundsComputer; 391 // friend struct BoundsComputer; 392 393 SkLazyPtr<SkRect> fLazyBounds; 394 SkFontID fUniqueID; 395 SkFontStyle fStyle; 396 bool fIsFixedPitch; 397 398 friend class SkPaint; 399 friend class SkGlyphCache; // GetDefaultTypeface 400 401 typedef SkWeakRefCnt INHERITED; 402 }; 403 404 #endif 405