1 /* 2 * Copyright 2018 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 SkFontPriv_DEFINED 9 #define SkFontPriv_DEFINED 10 11 #include "SkFont.h" 12 #include "SkMatrix.h" 13 #include "SkTypeface.h" 14 15 class SkReadBuffer; 16 class SkWriteBuffer; 17 18 class SkFontPriv { 19 public: 20 /* This is the size we use when we ask for a glyph's path. We then 21 * post-transform it as we draw to match the request. 22 * This is done to try to re-use cache entries for the path. 23 * 24 * This value is somewhat arbitrary. In theory, it could be 1, since 25 * we store paths as floats. However, we get the path from the font 26 * scaler, and it may represent its paths as fixed-point (or 26.6), 27 * so we shouldn't ask for something too big (might overflow 16.16) 28 * or too small (underflow 26.6). 29 * 30 * This value could track kMaxSizeForGlyphCache, assuming the above 31 * constraints, but since we ask for unhinted paths, the two values 32 * need not match per-se. 33 */ 34 static constexpr int kCanonicalTextSizeForPaths = 64; 35 36 static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit); 37 38 static SkScalar MaxCacheSize2(SkScalar maxLimit); 39 40 /** 41 * Return a matrix that applies the paint's text values: size, scale, skew 42 */ MakeTextMatrix(SkScalar size,SkScalar scaleX,SkScalar skewX)43 static SkMatrix MakeTextMatrix(SkScalar size, SkScalar scaleX, SkScalar skewX) { 44 SkMatrix m = SkMatrix::MakeScale(size * scaleX, size); 45 if (skewX) { 46 m.postSkew(skewX, 0); 47 } 48 return m; 49 } 50 MakeTextMatrix(const SkFont & font)51 static SkMatrix MakeTextMatrix(const SkFont& font) { 52 return MakeTextMatrix(font.getSize(), font.getScaleX(), font.getSkewX()); 53 } 54 55 static void ScaleFontMetrics(SkFontMetrics*, SkScalar); 56 57 /** 58 Returns the union of bounds of all glyphs. 59 Returned dimensions are computed by font manager from font data, 60 ignoring SkPaint::Hinting. Includes font metrics, but not fake bold or SkPathEffect. 61 62 If text size is large, text scale is one, and text skew is zero, 63 returns the bounds as: 64 { SkFontMetrics::fXMin, SkFontMetrics::fTop, SkFontMetrics::fXMax, SkFontMetrics::fBottom }. 65 66 @return union of bounds of all glyphs 67 */ 68 static SkRect GetFontBounds(const SkFont&); 69 IsFinite(const SkFont & font)70 static bool IsFinite(const SkFont& font) { 71 return SkScalarIsFinite(font.getSize()) && 72 SkScalarIsFinite(font.getScaleX()) && 73 SkScalarIsFinite(font.getSkewX()); 74 } 75 76 // Returns the number of elements (characters or glyphs) in the array. 77 static int CountTextElements(const void* text, size_t byteLength, SkTextEncoding); 78 79 static void GlyphsToUnichars(const SkFont&, const uint16_t glyphs[], int count, SkUnichar[]); 80 81 static void Flatten(const SkFont&, SkWriteBuffer& buffer); 82 static bool Unflatten(SkFont*, SkReadBuffer& buffer); 83 }; 84 85 class SkAutoToGlyphs { 86 public: SkAutoToGlyphs(const SkFont & font,const void * text,size_t length,SkTextEncoding encoding)87 SkAutoToGlyphs(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding) { 88 if (encoding == kGlyphID_SkTextEncoding || length == 0) { 89 fGlyphs = reinterpret_cast<const uint16_t*>(text); 90 fCount = length >> 1; 91 } else { 92 fCount = font.countText(text, length, encoding); 93 fStorage.reset(fCount); 94 font.textToGlyphs(text, length, encoding, fStorage.get(), fCount); 95 fGlyphs = fStorage.get(); 96 } 97 } 98 count()99 int count() const { return fCount; } glyphs()100 const uint16_t* glyphs() const { return fGlyphs; } 101 102 private: 103 SkAutoSTArray<32, uint16_t> fStorage; 104 const uint16_t* fGlyphs; 105 int fCount; 106 }; 107 108 #endif 109