blob: 252cd7c62683b7b33d49972fa6d584b99fb66ced [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
Brian Salomon52db9402017-11-07 14:58:55 -0500127 static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint);
joshualitt8e84a1e2016-02-16 11:09:25 -0800128
129 static bool ShouldDisableLCD(const SkPaint& paint);
130
Jim Van Verthf4c13162018-01-11 16:40:24 -0500131 // Functions for drawing large text either as paths or (for color emoji) as scaled glyphs
Robert Phillipsf440cec2018-01-19 11:51:20 -0500132 static void DrawBigText(GrTextUtils::Target*, const GrClip& clip,
133 const SkPaint& paint, const SkMatrix& viewMatrix, const char text[],
134 size_t byteLength, SkScalar x, SkScalar y,
135 const SkIRect& clipBounds);
joshualitt8e84a1e2016-02-16 11:09:25 -0800136
Robert Phillipsf440cec2018-01-19 11:51:20 -0500137 static void DrawBigPosText(GrTextUtils::Target*,
138 const SkSurfaceProps& props, const GrClip& clip,
139 const SkPaint& paint, const SkMatrix& viewMatrix,
140 const char text[], size_t byteLength, const SkScalar pos[],
141 int scalarsPerPosition, const SkPoint& offset,
142 const SkIRect& clipBounds);
joshualitt0a42e682015-12-10 13:20:58 -0800143};
144
145#endif