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 "GrColorSpaceInfo.h" 13 #include "SkColorFilter.h" 14 #include "SkPaint.h" 15 #include "SkScalar.h" 16 #include "SkTLazy.h" 17 18 class GrAtlasGlyphCache; 19 class GrAtlasTextBlob; 20 class GrAtlasTextOp; 21 class GrAtlasTextStrike; 22 class GrClip; 23 class GrColorSpaceXform; 24 class GrContext; 25 class GrPaint; 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 class Target { 44 public: 45 virtual ~Target() = default; 46 width()47 int width() const { return fWidth; } height()48 int height() const { return fHeight; } colorSpaceInfo()49 const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; } 50 51 virtual void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) = 0; 52 53 virtual void drawPath(const GrClip&, const SkPath&, const SkPaint&, 54 const SkMatrix& viewMatrix, const SkMatrix* pathMatrix, 55 const SkIRect& clipBounds) = 0; 56 virtual void makeGrPaint(GrMaskFormat, const SkPaint&, const SkMatrix& viewMatrix, 57 GrPaint*) = 0; 58 59 protected: Target(int width,int height,const GrColorSpaceInfo & colorSpaceInfo)60 Target(int width, int height, const GrColorSpaceInfo& colorSpaceInfo) 61 : fWidth(width), fHeight(height), fColorSpaceInfo(colorSpaceInfo) {} 62 63 private: 64 int fWidth; 65 int fHeight; 66 const GrColorSpaceInfo& fColorSpaceInfo; 67 }; 68 69 /** 70 * This is used to wrap a SkPaint and its post-color filter color. It is also used by RunPaint 71 * (below). This keeps a pointer to the SkPaint it is initialized with and expects it to remain 72 * const. It is also used to transform to GrPaint. 73 */ 74 class Paint { 75 public: Paint(const SkPaint * paint,const GrColorSpaceInfo * dstColorSpaceInfo)76 explicit Paint(const SkPaint* paint, const GrColorSpaceInfo* dstColorSpaceInfo) 77 : fPaint(paint), fDstColorSpaceInfo(dstColorSpaceInfo) { 78 this->initFilteredColor(); 79 } 80 81 // These expose the paint's color run through its color filter (if any). This is only valid 82 // when drawing grayscale/lcd glyph masks and not when drawing color glyphs. filteredPremulColor()83 GrColor filteredPremulColor() const { return fFilteredPremulColor; } luminanceColor()84 SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); } 85 skPaint()86 const SkPaint& skPaint() const { return *fPaint; } 87 operator const SkPaint&() const { return this->skPaint(); } 88 89 // Just for RunPaint's constructor dstColorSpaceInfo()90 const GrColorSpaceInfo* dstColorSpaceInfo() const { return fDstColorSpaceInfo; } 91 92 protected: 93 void initFilteredColor(); 94 Paint() = default; 95 const SkPaint* fPaint; 96 const GrColorSpaceInfo* fDstColorSpaceInfo; 97 // This is the paint's color run through its color filter, if present. This color should 98 // be used except when rendering bitmap text, in which case the bitmap must be filtered in 99 // the fragment shader. 100 GrColor fFilteredPremulColor; 101 }; 102 103 /** 104 * An extension of Paint that incorporated per-run modifications to the paint text settings and 105 * application of a draw filter. It expects its constructor arguments to remain alive and const 106 * during its lifetime. 107 */ 108 class RunPaint : public Paint { 109 public: RunPaint(const Paint * paint,SkDrawFilter * filter,const SkSurfaceProps & props)110 RunPaint(const Paint* paint, SkDrawFilter* filter, const SkSurfaceProps& props) 111 : fOriginalPaint(paint), fFilter(filter), fProps(props) { 112 // Initially we represent the original paint. 113 fPaint = &fOriginalPaint->skPaint(); 114 fDstColorSpaceInfo = fOriginalPaint->dstColorSpaceInfo(); 115 fFilteredPremulColor = fOriginalPaint->filteredPremulColor(); 116 } 117 118 bool modifyForRun(const SkTextBlobRunIterator&); 119 120 private: 121 SkTLazy<SkPaint> fModifiedPaint; 122 const Paint* fOriginalPaint; 123 SkDrawFilter* fFilter; 124 const SkSurfaceProps& fProps; 125 }; 126 127 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint); 128 129 static bool ShouldDisableLCD(const SkPaint& paint); 130 131 // Functions for drawing large text either as paths or (for color emoji) as scaled glyphs 132 static void DrawBigText(GrTextUtils::Target*, const GrClip& clip, 133 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[], 134 size_t byteLength, SkScalar x, SkScalar y, 135 const SkIRect& clipBounds); 136 137 static void DrawBigPosText(GrTextUtils::Target*, 138 const SkSurfaceProps& props, const GrClip& clip, 139 const SkPaint& paint, const SkMatrix& viewMatrix, 140 const char text[], size_t byteLength, const SkScalar pos[], 141 int scalarsPerPosition, const SkPoint& offset, 142 const SkIRect& clipBounds); 143 }; 144 145 #endif 146