blob: 9f386990109cd5519ec8987da9a61147b5855c96 [file] [log] [blame]
joshualitt0a42e682015-12-10 13:20:58 -08001/*
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
joshualitt29677982015-12-11 06:08:59 -080011#include "GrColor.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050012#include "SkColorFilter.h"
13#include "SkGr.h"
brianosman32f77822016-04-07 06:25:45 -070014#include "SkPaint.h"
joshualitt0a42e682015-12-10 13:20:58 -080015#include "SkScalar.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050016#include "SkTLazy.h"
joshualitt0a42e682015-12-10 13:20:58 -080017
Brian Salomonf856fd12016-12-16 14:24:34 -050018class GrAtlasGlyphCache;
joshualitt29677982015-12-11 06:08:59 -080019class GrAtlasTextBlob;
Brian Salomonf856fd12016-12-16 14:24:34 -050020class GrAtlasTextStrike;
joshualitt0a42e682015-12-10 13:20:58 -080021class GrClip;
Brian Osmanec8f8b02017-05-11 10:57:37 -040022class GrColorSpaceXform;
joshualitt0a42e682015-12-10 13:20:58 -080023class GrContext;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050024class GrPaint;
Brian Osman11052242016-10-27 14:47:55 -040025class GrRenderTargetContext;
joshualitt0d2199b2016-01-20 06:36:09 -080026class GrShaderCaps;
Brian Osmanec8f8b02017-05-11 10:57:37 -040027class SkColorSpace;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050028class SkDrawFilter;
joshualitt29677982015-12-11 06:08:59 -080029class SkGlyph;
joshualitt0a42e682015-12-10 13:20:58 -080030class SkMatrix;
31struct SkIRect;
joshualitt0a42e682015-12-10 13:20:58 -080032struct SkPoint;
joshualitt29677982015-12-11 06:08:59 -080033class SkGlyphCache;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050034class SkTextBlobRunIterator;
joshualitt0a42e682015-12-10 13:20:58 -080035class SkSurfaceProps;
36
Brian Salomon6f1d36c2017-01-13 12:02:17 -050037/**
joshualitt0a42e682015-12-10 13:20:58 -080038 * 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
joshualitt0a42e682015-12-10 13:20:58 -080040 */
41class GrTextUtils {
42public:
Brian Salomon6f1d36c2017-01-13 12:02:17 -050043 /**
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:
Brian Osmanec8f8b02017-05-11 10:57:37 -040050 explicit Paint(const SkPaint* paint,
51 SkColorSpace* dstColorSpace,
52 GrColorSpaceXform* colorXformFromSRGB)
53 : fPaint(paint)
54 , fDstColorSpace(dstColorSpace)
55 , fColorXformFromSRGB(colorXformFromSRGB) {
56 this->initFilteredColor();
57 }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050058
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.
Brian Osmanec8f8b02017-05-11 10:57:37 -040061 GrColor filteredPremulColor() const { return fFilteredPremulColor; }
62 GrColor filteredUnpremulColor() const { return fFilteredUnpremulColor; }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050063
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
Brian Osmanec8f8b02017-05-11 10:57:37 -040070 // Just for RunPaint's constructor
71 SkColorSpace* dstColorSpace() const { return fDstColorSpace; }
72 GrColorSpaceXform* colorXformFromSRGB() const { return fColorXformFromSRGB; }
73
Brian Salomon6f1d36c2017-01-13 12:02:17 -050074 protected:
Brian Osmanec8f8b02017-05-11 10:57:37 -040075 void initFilteredColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -050076 Paint() = default;
77 const SkPaint* fPaint;
Brian Osmanec8f8b02017-05-11 10:57:37 -040078 SkColorSpace* fDstColorSpace;
79 GrColorSpaceXform* fColorXformFromSRGB;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050080 // 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.
Brian Osmanec8f8b02017-05-11 10:57:37 -040083 GrColor fFilteredUnpremulColor;
84 GrColor fFilteredPremulColor;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050085 };
86
87 /**
88 * An extension of Paint that incorporated per-run modifications to the paint text settings and
89 * application of a draw filter. It expects its constructor arguments to remain alive and const
90 * during its lifetime.
91 */
92 class RunPaint : public Paint {
93 public:
94 RunPaint(const Paint* paint, SkDrawFilter* filter, const SkSurfaceProps& props)
95 : fOriginalPaint(paint), fFilter(filter), fProps(props) {
96 // Initially we represent the original paint.
97 fPaint = &fOriginalPaint->skPaint();
Brian Osmanec8f8b02017-05-11 10:57:37 -040098 fDstColorSpace = fOriginalPaint->dstColorSpace();
99 fColorXformFromSRGB = fOriginalPaint->colorXformFromSRGB();
100 fFilteredPremulColor = fOriginalPaint->filteredPremulColor();
101 fFilteredUnpremulColor = fOriginalPaint->filteredUnpremulColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500102 }
103
104 bool modifyForRun(const SkTextBlobRunIterator&);
105
106 private:
107 SkTLazy<SkPaint> fModifiedPaint;
108 const Paint* fOriginalPaint;
109 SkDrawFilter* fFilter;
110 const SkSurfaceProps& fProps;
111 };
112
joshualitt29677982015-12-11 06:08:59 -0800113 // Functions for appending BMP text to GrAtlasTextBlob
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500114 static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
115 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags,
116 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
joshualitt29677982015-12-11 06:08:59 -0800117 SkScalar x, SkScalar y);
joshualitt0a42e682015-12-10 13:20:58 -0800118
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500119 static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
120 const SkSurfaceProps&, const Paint& paint,
121 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
122 const char text[], size_t byteLength, const SkScalar pos[],
123 int scalarsPerPosition, const SkPoint& offset);
joshualitt0a42e682015-12-10 13:20:58 -0800124
joshualitt0d2199b2016-01-20 06:36:09 -0800125 // functions for appending distance field text
126 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
127 const SkSurfaceProps& props, const GrShaderCaps& caps);
128
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500129 static void DrawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*,
130 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags,
131 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
joshualitt0d2199b2016-01-20 06:36:09 -0800132 SkScalar x, SkScalar y);
133
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500134 static void DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*,
135 const SkSurfaceProps&, const Paint& paint,
136 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
137 const char text[], size_t byteLength, const SkScalar pos[],
138 int scalarsPerPosition, const SkPoint& offset);
joshualitt0d2199b2016-01-20 06:36:09 -0800139
joshualitt29677982015-12-11 06:08:59 -0800140 // Functions for drawing text as paths
Brian Osman11052242016-10-27 14:47:55 -0400141 static void DrawTextAsPath(GrContext*, GrRenderTargetContext*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500142 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[],
143 size_t byteLength, SkScalar x, SkScalar y,
joshualitt29677982015-12-11 06:08:59 -0800144 const SkIRect& clipBounds);
145
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500146 static void DrawPosTextAsPath(GrContext* context, GrRenderTargetContext* rtc,
147 const SkSurfaceProps& props, const GrClip& clip,
148 const SkPaint& paint, const SkMatrix& viewMatrix,
149 const char text[], size_t byteLength, const SkScalar pos[],
150 int scalarsPerPosition, const SkPoint& offset,
151 const SkIRect& clipBounds);
joshualitt8e84a1e2016-02-16 11:09:25 -0800152
153 static bool ShouldDisableLCD(const SkPaint& paint);
154
joshualitt8e84a1e2016-02-16 11:09:25 -0800155
joshualitt29677982015-12-11 06:08:59 -0800156private:
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500157 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
158
joshualitt0d2199b2016-01-20 06:36:09 -0800159 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
160 SkPaint* skPaint,
161 SkScalar* textRatio,
162 const SkMatrix& viewMatrix);
163
Brian Salomonf856fd12016-12-16 14:24:34 -0500164 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
165 GrAtlasTextStrike**, const SkGlyph&, int left, int top,
bsalomonc2878e22016-05-17 13:18:03 -0700166 GrColor color, SkGlyphCache*);
joshualitt0d2199b2016-01-20 06:36:09 -0800167
Brian Salomonf856fd12016-12-16 14:24:34 -0500168 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
169 GrAtlasTextStrike**, const SkGlyph&,
joshualitt0d2199b2016-01-20 06:36:09 -0800170 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700171 SkGlyphCache* cache,
joshualitt0d2199b2016-01-20 06:36:09 -0800172 SkScalar textRatio, const SkMatrix& viewMatrix);
joshualitt0a42e682015-12-10 13:20:58 -0800173};
174
175#endif