blob: c5daa42a1530261dc01a4b93ef783b982c1d431c [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;
22class GrContext;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050023class GrPaint;
Brian Osman11052242016-10-27 14:47:55 -040024class GrRenderTargetContext;
joshualitt0d2199b2016-01-20 06:36:09 -080025class GrShaderCaps;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050026class SkDrawFilter;
joshualitt29677982015-12-11 06:08:59 -080027class SkGlyph;
joshualitt0a42e682015-12-10 13:20:58 -080028class SkMatrix;
29struct SkIRect;
joshualitt0a42e682015-12-10 13:20:58 -080030struct SkPoint;
joshualitt29677982015-12-11 06:08:59 -080031class SkGlyphCache;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050032class SkTextBlobRunIterator;
joshualitt0a42e682015-12-10 13:20:58 -080033class SkSurfaceProps;
34
Brian Salomon6f1d36c2017-01-13 12:02:17 -050035/**
joshualitt0a42e682015-12-10 13:20:58 -080036 * 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
joshualitt0a42e682015-12-10 13:20:58 -080038 */
39class GrTextUtils {
40public:
Brian Salomon6f1d36c2017-01-13 12:02:17 -050041 /**
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
joshualitt29677982015-12-11 06:08:59 -0800102 // Functions for appending BMP text to GrAtlasTextBlob
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500103 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,
joshualitt29677982015-12-11 06:08:59 -0800106 SkScalar x, SkScalar y);
joshualitt0a42e682015-12-10 13:20:58 -0800107
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500108 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);
joshualitt0a42e682015-12-10 13:20:58 -0800113
joshualitt0d2199b2016-01-20 06:36:09 -0800114 // 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 Salomon6f1d36c2017-01-13 12:02:17 -0500118 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,
joshualitt0d2199b2016-01-20 06:36:09 -0800121 SkScalar x, SkScalar y);
122
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500123 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);
joshualitt0d2199b2016-01-20 06:36:09 -0800128
joshualitt29677982015-12-11 06:08:59 -0800129 // Functions for drawing text as paths
Brian Osman11052242016-10-27 14:47:55 -0400130 static void DrawTextAsPath(GrContext*, GrRenderTargetContext*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500131 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[],
132 size_t byteLength, SkScalar x, SkScalar y,
joshualitt29677982015-12-11 06:08:59 -0800133 const SkIRect& clipBounds);
134
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500135 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);
joshualitt8e84a1e2016-02-16 11:09:25 -0800141
142 static bool ShouldDisableLCD(const SkPaint& paint);
143
joshualitt8e84a1e2016-02-16 11:09:25 -0800144
joshualitt29677982015-12-11 06:08:59 -0800145private:
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500146 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
147
joshualitt0d2199b2016-01-20 06:36:09 -0800148 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
149 SkPaint* skPaint,
150 SkScalar* textRatio,
151 const SkMatrix& viewMatrix);
152
Brian Salomonf856fd12016-12-16 14:24:34 -0500153 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
154 GrAtlasTextStrike**, const SkGlyph&, int left, int top,
bsalomonc2878e22016-05-17 13:18:03 -0700155 GrColor color, SkGlyphCache*);
joshualitt0d2199b2016-01-20 06:36:09 -0800156
Brian Salomonf856fd12016-12-16 14:24:34 -0500157 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
158 GrAtlasTextStrike**, const SkGlyph&,
joshualitt0d2199b2016-01-20 06:36:09 -0800159 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700160 SkGlyphCache* cache,
joshualitt0d2199b2016-01-20 06:36:09 -0800161 SkScalar textRatio, const SkMatrix& viewMatrix);
joshualitt0a42e682015-12-10 13:20:58 -0800162};
163
164#endif