blob: d8d841f05125fac0815ffe60bc1f5d3857cf1bdb [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 Salomonf3569f02017-10-24 12:52:33 -040012#include "GrColorSpaceInfo.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050013#include "SkColorFilter.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 Salomonf18b1d82017-10-27 11:30:49 -040020class GrAtlasTextOp;
Brian Salomonf856fd12016-12-16 14:24:34 -050021class GrAtlasTextStrike;
joshualitt0a42e682015-12-10 13:20:58 -080022class GrClip;
Brian Osmanec8f8b02017-05-11 10:57:37 -040023class GrColorSpaceXform;
joshualitt0a42e682015-12-10 13:20:58 -080024class GrContext;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050025class GrPaint;
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 Salomonf18b1d82017-10-27 11:30:49 -040043 class Target {
44 public:
45 virtual ~Target() = default;
46
47 int width() const { return fWidth; }
48 int height() const { return fHeight; }
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:
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
Brian Salomon6f1d36c2017-01-13 12:02:17 -050069 /**
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:
Brian Salomon4cbb6e62017-10-25 15:12:19 -040076 explicit Paint(const SkPaint* paint, const GrColorSpaceInfo* dstColorSpaceInfo)
77 : fPaint(paint), fDstColorSpaceInfo(dstColorSpaceInfo) {
Brian Osmanec8f8b02017-05-11 10:57:37 -040078 this->initFilteredColor();
79 }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050080
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.
Brian Osmanec8f8b02017-05-11 10:57:37 -040083 GrColor filteredPremulColor() const { return fFilteredPremulColor; }
Jim Van Verthbc2cdd12017-06-08 11:14:35 -040084 SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050085
86 const SkPaint& skPaint() const { return *fPaint; }
87 operator const SkPaint&() const { return this->skPaint(); }
88
Brian Osmanec8f8b02017-05-11 10:57:37 -040089 // Just for RunPaint's constructor
Brian Salomon4cbb6e62017-10-25 15:12:19 -040090 const GrColorSpaceInfo* dstColorSpaceInfo() const { return fDstColorSpaceInfo; }
Brian Osmanec8f8b02017-05-11 10:57:37 -040091
Brian Salomon6f1d36c2017-01-13 12:02:17 -050092 protected:
Brian Osmanec8f8b02017-05-11 10:57:37 -040093 void initFilteredColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -050094 Paint() = default;
95 const SkPaint* fPaint;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040096 const GrColorSpaceInfo* fDstColorSpaceInfo;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050097 // 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.
Brian Osmanec8f8b02017-05-11 10:57:37 -0400100 GrColor fFilteredPremulColor;
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500101 };
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:
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();
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400114 fDstColorSpaceInfo = fOriginalPaint->dstColorSpaceInfo();
Brian Osmanec8f8b02017-05-11 10:57:37 -0400115 fFilteredPremulColor = fOriginalPaint->filteredPremulColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500116 }
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
joshualitt29677982015-12-11 06:08:59 -0800127 // Functions for appending BMP text to GrAtlasTextBlob
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500128 static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
129 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags,
130 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
joshualitt29677982015-12-11 06:08:59 -0800131 SkScalar x, SkScalar y);
joshualitt0a42e682015-12-10 13:20:58 -0800132
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500133 static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
134 const SkSurfaceProps&, const Paint& paint,
135 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
136 const char text[], size_t byteLength, const SkScalar pos[],
137 int scalarsPerPosition, const SkPoint& offset);
joshualitt0a42e682015-12-10 13:20:58 -0800138
joshualitt0d2199b2016-01-20 06:36:09 -0800139 // functions for appending distance field text
140 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
141 const SkSurfaceProps& props, const GrShaderCaps& caps);
142
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500143 static void DrawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*,
144 const SkSurfaceProps&, const Paint& paint, uint32_t scalerContextFlags,
145 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
joshualitt0d2199b2016-01-20 06:36:09 -0800146 SkScalar x, SkScalar y);
147
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500148 static void DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*,
149 const SkSurfaceProps&, const Paint& paint,
150 uint32_t scalerContextFlags, const SkMatrix& viewMatrix,
151 const char text[], size_t byteLength, const SkScalar pos[],
152 int scalarsPerPosition, const SkPoint& offset);
joshualitt0d2199b2016-01-20 06:36:09 -0800153
joshualitt29677982015-12-11 06:08:59 -0800154 // Functions for drawing text as paths
Brian Salomonf18b1d82017-10-27 11:30:49 -0400155 static void DrawTextAsPath(GrContext*, Target*, const GrClip& clip, const SkPaint& paint,
156 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
157 SkScalar x, SkScalar y, const SkIRect& clipBounds);
joshualitt29677982015-12-11 06:08:59 -0800158
Brian Salomonf18b1d82017-10-27 11:30:49 -0400159 static void DrawPosTextAsPath(GrContext* context, Target*, const SkSurfaceProps& props,
160 const GrClip& clip, const SkPaint& paint,
161 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
162 const SkScalar pos[], int scalarsPerPosition,
163 const SkPoint& offset, const SkIRect& clipBounds);
joshualitt8e84a1e2016-02-16 11:09:25 -0800164
165 static bool ShouldDisableLCD(const SkPaint& paint);
166
joshualitt8e84a1e2016-02-16 11:09:25 -0800167
joshualitt29677982015-12-11 06:08:59 -0800168private:
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500169 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
170
joshualitt0d2199b2016-01-20 06:36:09 -0800171 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
172 SkPaint* skPaint,
173 SkScalar* textRatio,
174 const SkMatrix& viewMatrix);
175
Brian Salomonf856fd12016-12-16 14:24:34 -0500176 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
177 GrAtlasTextStrike**, const SkGlyph&, int left, int top,
bsalomonc2878e22016-05-17 13:18:03 -0700178 GrColor color, SkGlyphCache*);
joshualitt0d2199b2016-01-20 06:36:09 -0800179
Brian Salomonf856fd12016-12-16 14:24:34 -0500180 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
181 GrAtlasTextStrike**, const SkGlyph&,
joshualitt0d2199b2016-01-20 06:36:09 -0800182 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700183 SkGlyphCache* cache,
joshualitt0d2199b2016-01-20 06:36:09 -0800184 SkScalar textRatio, const SkMatrix& viewMatrix);
joshualitt0a42e682015-12-10 13:20:58 -0800185};
186
187#endif