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