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