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