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