blob: bbf3ca49a9f5336124af3b2d36caef7b91b25e56 [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; }
Jim Van Verthbc2cdd12017-06-08 11:14:35 -040062 SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); }
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 fFilteredPremulColor;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050084 };
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();
Brian Osmanec8f8b02017-05-11 10:57:37 -040097 fDstColorSpace = fOriginalPaint->dstColorSpace();
98 fColorXformFromSRGB = fOriginalPaint->colorXformFromSRGB();
99 fFilteredPremulColor = fOriginalPaint->filteredPremulColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500100 }
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
joshualitt29677982015-12-11 06:08:59 -0800111 // Functions for appending BMP text to GrAtlasTextBlob
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500112 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,
joshualitt29677982015-12-11 06:08:59 -0800115 SkScalar x, SkScalar y);
joshualitt0a42e682015-12-10 13:20:58 -0800116
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500117 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);
joshualitt0a42e682015-12-10 13:20:58 -0800122
joshualitt0d2199b2016-01-20 06:36:09 -0800123 // functions for appending distance field text
124 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
125 const SkSurfaceProps& props, const GrShaderCaps& caps);
126
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500127 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,
joshualitt0d2199b2016-01-20 06:36:09 -0800130 SkScalar x, SkScalar y);
131
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500132 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);
joshualitt0d2199b2016-01-20 06:36:09 -0800137
joshualitt29677982015-12-11 06:08:59 -0800138 // Functions for drawing text as paths
Brian Osman11052242016-10-27 14:47:55 -0400139 static void DrawTextAsPath(GrContext*, GrRenderTargetContext*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500140 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[],
141 size_t byteLength, SkScalar x, SkScalar y,
joshualitt29677982015-12-11 06:08:59 -0800142 const SkIRect& clipBounds);
143
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500144 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);
joshualitt8e84a1e2016-02-16 11:09:25 -0800150
151 static bool ShouldDisableLCD(const SkPaint& paint);
152
joshualitt8e84a1e2016-02-16 11:09:25 -0800153
joshualitt29677982015-12-11 06:08:59 -0800154private:
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500155 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
156
joshualitt0d2199b2016-01-20 06:36:09 -0800157 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
158 SkPaint* skPaint,
159 SkScalar* textRatio,
160 const SkMatrix& viewMatrix);
161
Brian Salomonf856fd12016-12-16 14:24:34 -0500162 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
163 GrAtlasTextStrike**, const SkGlyph&, int left, int top,
bsalomonc2878e22016-05-17 13:18:03 -0700164 GrColor color, SkGlyphCache*);
joshualitt0d2199b2016-01-20 06:36:09 -0800165
Brian Salomonf856fd12016-12-16 14:24:34 -0500166 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
167 GrAtlasTextStrike**, const SkGlyph&,
joshualitt0d2199b2016-01-20 06:36:09 -0800168 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700169 SkGlyphCache* cache,
joshualitt0d2199b2016-01-20 06:36:09 -0800170 SkScalar textRatio, const SkMatrix& viewMatrix);
joshualitt0a42e682015-12-10 13:20:58 -0800171};
172
173#endif