1 /* 2 * Copyright 2020 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 sktext_gpu_SDFTControl_DEFINED 9 #define sktext_gpu_SDFTControl_DEFINED 10 11 #include "include/core/SkScalar.h" 12 #include "include/core/SkTypes.h" 13 14 #include <tuple> 15 16 class SkFont; 17 class SkMatrix; 18 class SkPaint; 19 class SkReadBuffer; 20 class SkWriteBuffer; 21 struct SkPoint; 22 23 namespace sktext::gpu { 24 25 #if !defined(SK_DISABLE_SDF_TEXT) 26 // Two numbers fMatrixMin and fMatrixMax such that if viewMatrix.getMaxScale() is between them then 27 // this SDFT size can be reused. 28 class SDFTMatrixRange { 29 public: SDFTMatrixRange(SkScalar min,SkScalar max)30 SDFTMatrixRange(SkScalar min, SkScalar max) : fMatrixMin{min}, fMatrixMax{max} {} 31 bool matrixInRange(const SkMatrix& matrix) const; 32 void flatten(SkWriteBuffer& buffer) const; 33 static SDFTMatrixRange MakeFromBuffer(SkReadBuffer& buffer); 34 35 private: 36 const SkScalar fMatrixMin, 37 fMatrixMax; 38 }; 39 #endif 40 41 class SDFTControl { 42 public: 43 #if !defined(SK_DISABLE_SDF_TEXT) 44 SDFTControl(bool ableToUseSDFT, bool useSDFTForSmallText, bool useSDFTForPerspectiveText, 45 SkScalar min, SkScalar max); 46 47 // Produce a font, a scale factor from the nominal size to the source space size, and matrix 48 // range where this font can be reused. 49 std::tuple<SkFont, SkScalar, SDFTMatrixRange> 50 getSDFFont(const SkFont& font, const SkMatrix& viewMatrix, const SkPoint& textLocation) const; 51 52 bool isSDFT(SkScalar approximateDeviceTextSize, const SkPaint& paint, 53 const SkMatrix& matrix) const; maxSize()54 SkScalar maxSize() const { return fMaxDistanceFieldFontSize; } 55 #else 56 SDFTControl() {} 57 #endif 58 bool isDirect(SkScalar approximateDeviceTextSize, const SkPaint& paint, 59 const SkMatrix& matrix) const; 60 61 62 private: 63 #if !defined(SK_DISABLE_SDF_TEXT) 64 static SkScalar MinSDFTRange(bool useSDFTForSmallText, SkScalar min); 65 66 // Below this size (in device space) distance field text will not be used. 67 const SkScalar fMinDistanceFieldFontSize; 68 69 // Above this size (in device space) distance field text will not be used and glyphs will 70 // be rendered from outline as individual paths. 71 const SkScalar fMaxDistanceFieldFontSize; 72 73 const bool fAbleToUseSDFT; 74 const bool fAbleToUsePerspectiveSDFT; 75 #endif 76 }; 77 78 } // namespace sktext::gpu 79 80 #endif // sktext_SDFTControl_DEFINED 81