1 /* 2 * Copyright 2015 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 GrTextUtils_DEFINED 9 #define GrTextUtils_DEFINED 10 11 #include "GrColor.h" 12 #include "SkColorFilter.h" 13 #include "SkGr.h" 14 #include "SkPaint.h" 15 #include "SkScalar.h" 16 #include "SkTLazy.h" 17 18 class GrAtlasGlyphCache; 19 class GrAtlasTextBlob; 20 class GrAtlasTextStrike; 21 class GrClip; 22 class GrColorSpaceXform; 23 class GrContext; 24 class GrPaint; 25 class GrRenderTargetContext; 26 class GrShaderCaps; 27 class SkColorSpace; 28 class SkDrawFilter; 29 class SkGlyph; 30 class SkMatrix; 31 struct SkIRect; 32 struct SkPoint; 33 class SkGlyphCache; 34 class SkTextBlobRunIterator; 35 class SkSurfaceProps; 36 37 /** 38 * A class to house a bunch of common text utilities. This class should *ONLY* have static 39 * functions. It is not a namespace only because we wish to friend SkPaint 40 */ 41 class GrTextUtils { 42 public: 43 /** 44 * This is used to wrap a SkPaint and its post-color filter color. It is also used by RunPaint 45 * (below). This keeps a pointer to the SkPaint it is initialized with and expects it to remain 46 * const. It is also used to transform to GrPaint. 47 */ 48 class Paint { 49 public: 50 explicit Paint(const SkPaint* paint, 51 SkColorSpace* dstColorSpace, 52 GrColorSpaceXform* colorXformFromSRGB) 53 : fPaint(paint) 54 , fDstColorSpace(dstColorSpace) 55 , fColorXformFromSRGB(colorXformFromSRGB) { 56 this->initFilteredColor(); 57 } 58 59 // These expose the paint's color run through its color filter (if any). This is only valid 60 // when drawing grayscale/lcd glyph masks and not when drawing color glyphs. 61 GrColor filteredPremulColor() const { return fFilteredPremulColor; } 62 SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); } 63 64 const SkPaint& skPaint() const { return *fPaint; } 65 operator const SkPaint&() const { return this->skPaint(); } 66 67 bool toGrPaint(GrMaskFormat, GrRenderTargetContext*, const SkMatrix& viewMatrix, 68 GrPaint*) const; 69 70 // Just for RunPaint's constructor 71 SkColorSpace* dstColorSpace() const { return fDstColorSpace; } 72 GrColorSpaceXform* colorXformFromSRGB() const { return fColorXformFromSRGB; } 73 74 protected: 75 void initFilteredColor(); 76 Paint() = default; 77 const SkPaint* fPaint; 78 SkColorSpace* fDstColorSpace; 79 GrColorSpaceXform* fColorXformFromSRGB; 80 // This is the paint's color run through its color filter, if present. This color should 81 // be used except when rendering bitmap text, in which case the bitmap must be filtered in 82 // the fragment shader. 83 GrColor fFilteredPremulColor; 84 }; 85 86 /** 87 * An extension of Paint that incorporated per-run modifications to the paint text settings and 88 * application of a draw filter. It expects its constructor arguments to remain alive and const 89 * during its lifetime. 90 */ 91 class RunPaint : public Paint { 92 public: 93 RunPaint(const Paint* paint, SkDrawFilter* filter, const SkSurfaceProps& props) 94 : fOriginalPaint(paint), fFilter(filter), fProps(props) { 95 // Initially we represent the original paint. 96 fPaint = &fOriginalPaint->skPaint(); 97 fDstColorSpace = fOriginalPaint->dstColorSpace(); 98 fColorXformFromSRGB = fOriginalPaint->colorXformFromSRGB(); 99 fFilteredPremulColor = fOriginalPaint->filteredPremulColor(); 100 } 101 102 bool modifyForRun(const SkTextBlobRunIterator&); 103 104 private: 105 SkTLazy<SkPaint> fModifiedPaint; 106 const Paint* fOriginalPaint; 107 SkDrawFilter* fFilter; 108 const SkSurfaceProps& fProps; 109 }; 110 111 // Functions for appending BMP text to GrAtlasTextBlob 112 static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, 113 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags, 114 const SkMatrix& viewMatrix, const char text[], size_t byteLength, 115 SkScalar x, SkScalar y); 116 117 static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, 118 const SkSurfaceProps&, const Paint& paint, 119 uint32_t scalerContextFlags, const SkMatrix& viewMatrix, 120 const char text[], size_t byteLength, const SkScalar pos[], 121 int scalarsPerPosition, const SkPoint& offset); 122 123 // functions for appending distance field text 124 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, 125 const SkSurfaceProps& props, const GrShaderCaps& caps); 126 127 static void DrawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, 128 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags, 129 const SkMatrix& viewMatrix, const char text[], size_t byteLength, 130 SkScalar x, SkScalar y); 131 132 static void DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, 133 const SkSurfaceProps&, const Paint& paint, 134 uint32_t scalerContextFlags, const SkMatrix& viewMatrix, 135 const char text[], size_t byteLength, const SkScalar pos[], 136 int scalarsPerPosition, const SkPoint& offset); 137 138 // Functions for drawing text as paths 139 static void DrawTextAsPath(GrContext*, GrRenderTargetContext*, const GrClip& clip, 140 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[], 141 size_t byteLength, SkScalar x, SkScalar y, 142 const SkIRect& clipBounds); 143 144 static void DrawPosTextAsPath(GrContext* context, GrRenderTargetContext* rtc, 145 const SkSurfaceProps& props, const GrClip& clip, 146 const SkPaint& paint, const SkMatrix& viewMatrix, 147 const char text[], size_t byteLength, const SkScalar pos[], 148 int scalarsPerPosition, const SkPoint& offset, 149 const SkIRect& clipBounds); 150 151 static bool ShouldDisableLCD(const SkPaint& paint); 152 153 154 private: 155 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint); 156 157 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob, 158 SkPaint* skPaint, 159 SkScalar* textRatio, 160 const SkMatrix& viewMatrix); 161 162 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, 163 GrAtlasTextStrike**, const SkGlyph&, int left, int top, 164 GrColor color, SkGlyphCache*); 165 166 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, 167 GrAtlasTextStrike**, const SkGlyph&, 168 SkScalar sx, SkScalar sy, GrColor color, 169 SkGlyphCache* cache, 170 SkScalar textRatio, const SkMatrix& viewMatrix); 171 }; 172 173 #endif 174