blob: bd0a1ad2ea8b21bf1b1eb9d79e8591577ff154ab [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"
14#include "SkGr.h"
brianosman32f77822016-04-07 06:25:45 -070015#include "SkPaint.h"
joshualitt0a42e682015-12-10 13:20:58 -080016#include "SkScalar.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050017#include "SkTLazy.h"
joshualitt0a42e682015-12-10 13:20:58 -080018
Brian Salomonf856fd12016-12-16 14:24:34 -050019class GrAtlasGlyphCache;
joshualitt29677982015-12-11 06:08:59 -080020class GrAtlasTextBlob;
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;
Brian Osman11052242016-10-27 14:47:55 -040026class GrRenderTargetContext;
joshualitt0d2199b2016-01-20 06:36:09 -080027class GrShaderCaps;
Brian Osmanec8f8b02017-05-11 10:57:37 -040028class SkColorSpace;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050029class SkDrawFilter;
joshualitt29677982015-12-11 06:08:59 -080030class SkGlyph;
joshualitt0a42e682015-12-10 13:20:58 -080031class SkMatrix;
32struct SkIRect;
joshualitt0a42e682015-12-10 13:20:58 -080033struct SkPoint;
joshualitt29677982015-12-11 06:08:59 -080034class SkGlyphCache;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050035class SkTextBlobRunIterator;
joshualitt0a42e682015-12-10 13:20:58 -080036class SkSurfaceProps;
37
Brian Salomon6f1d36c2017-01-13 12:02:17 -050038/**
joshualitt0a42e682015-12-10 13:20:58 -080039 * 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
joshualitt0a42e682015-12-10 13:20:58 -080041 */
42class GrTextUtils {
43public:
Brian Salomon6f1d36c2017-01-13 12:02:17 -050044 /**
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 Salomon4cbb6e62017-10-25 15:12:19 -040051 explicit Paint(const SkPaint* paint, const GrColorSpaceInfo* dstColorSpaceInfo)
52 : fPaint(paint), fDstColorSpaceInfo(dstColorSpaceInfo) {
Brian Osmanec8f8b02017-05-11 10:57:37 -040053 this->initFilteredColor();
54 }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050055
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 Osmanec8f8b02017-05-11 10:57:37 -040058 GrColor filteredPremulColor() const { return fFilteredPremulColor; }
Jim Van Verthbc2cdd12017-06-08 11:14:35 -040059 SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); }
Brian Salomon6f1d36c2017-01-13 12:02:17 -050060
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 Osmanec8f8b02017-05-11 10:57:37 -040067 // Just for RunPaint's constructor
Brian Salomon4cbb6e62017-10-25 15:12:19 -040068 const GrColorSpaceInfo* dstColorSpaceInfo() const { return fDstColorSpaceInfo; }
Brian Osmanec8f8b02017-05-11 10:57:37 -040069
Brian Salomon6f1d36c2017-01-13 12:02:17 -050070 protected:
Brian Osmanec8f8b02017-05-11 10:57:37 -040071 void initFilteredColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -050072 Paint() = default;
73 const SkPaint* fPaint;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040074 const GrColorSpaceInfo* fDstColorSpaceInfo;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050075 // 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 Osmanec8f8b02017-05-11 10:57:37 -040078 GrColor fFilteredPremulColor;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050079 };
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 Salomon4cbb6e62017-10-25 15:12:19 -040092 fDstColorSpaceInfo = fOriginalPaint->dstColorSpaceInfo();
Brian Osmanec8f8b02017-05-11 10:57:37 -040093 fFilteredPremulColor = fOriginalPaint->filteredPremulColor();
Brian Salomon6f1d36c2017-01-13 12:02:17 -050094 }
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
joshualitt29677982015-12-11 06:08:59 -0800105 // Functions for appending BMP text to GrAtlasTextBlob
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500106 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,
joshualitt29677982015-12-11 06:08:59 -0800109 SkScalar x, SkScalar y);
joshualitt0a42e682015-12-10 13:20:58 -0800110
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500111 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);
joshualitt0a42e682015-12-10 13:20:58 -0800116
joshualitt0d2199b2016-01-20 06:36:09 -0800117 // 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 Salomon6f1d36c2017-01-13 12:02:17 -0500121 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,
joshualitt0d2199b2016-01-20 06:36:09 -0800124 SkScalar x, SkScalar y);
125
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500126 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);
joshualitt0d2199b2016-01-20 06:36:09 -0800131
joshualitt29677982015-12-11 06:08:59 -0800132 // Functions for drawing text as paths
Brian Osman11052242016-10-27 14:47:55 -0400133 static void DrawTextAsPath(GrContext*, GrRenderTargetContext*, const GrClip& clip,
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500134 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[],
135 size_t byteLength, SkScalar x, SkScalar y,
joshualitt29677982015-12-11 06:08:59 -0800136 const SkIRect& clipBounds);
137
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500138 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);
joshualitt8e84a1e2016-02-16 11:09:25 -0800144
145 static bool ShouldDisableLCD(const SkPaint& paint);
146
joshualitt8e84a1e2016-02-16 11:09:25 -0800147
joshualitt29677982015-12-11 06:08:59 -0800148private:
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500149 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
150
joshualitt0d2199b2016-01-20 06:36:09 -0800151 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
152 SkPaint* skPaint,
153 SkScalar* textRatio,
154 const SkMatrix& viewMatrix);
155
Brian Salomonf856fd12016-12-16 14:24:34 -0500156 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
157 GrAtlasTextStrike**, const SkGlyph&, int left, int top,
bsalomonc2878e22016-05-17 13:18:03 -0700158 GrColor color, SkGlyphCache*);
joshualitt0d2199b2016-01-20 06:36:09 -0800159
Brian Salomonf856fd12016-12-16 14:24:34 -0500160 static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
161 GrAtlasTextStrike**, const SkGlyph&,
joshualitt0d2199b2016-01-20 06:36:09 -0800162 SkScalar sx, SkScalar sy, GrColor color,
bsalomonc2878e22016-05-17 13:18:03 -0700163 SkGlyphCache* cache,
joshualitt0d2199b2016-01-20 06:36:09 -0800164 SkScalar textRatio, const SkMatrix& viewMatrix);
joshualitt0a42e682015-12-10 13:20:58 -0800165};
166
167#endif