blob: 25aeea0c7a5cef43d530db5268bdb5bf9bcf741a [file] [log] [blame]
joshualitt1d89e8d2015-04-01 12:40:54 -07001/*
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
Herb Derby86240592018-05-24 16:12:31 -04008#ifndef GrTextContext_DEFINED
9#define GrTextContext_DEFINED
joshualitt1d89e8d2015-04-01 12:40:54 -070010
Herb Derby86240592018-05-24 16:12:31 -040011#include "GrTextBlob.h"
joshualitt1acabf32015-12-10 09:10:10 -080012#include "GrDistanceFieldAdjustTable.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070013#include "GrGeometryProcessor.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050014#include "GrTextUtils.h"
halcanary33779752015-10-27 14:01:05 -070015#include "SkTextBlobRunIterator.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070016
Hal Canary6f6961e2017-01-31 13:50:44 -050017#if GR_TEST_UTILS
Brian Salomon5ec9def2016-12-20 15:34:05 -050018#include "GrDrawOpTest.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070019#endif
20
Brian Salomon9afd3712016-12-01 10:59:09 -050021class GrDrawOp;
joshualittb7133be2015-04-08 09:08:31 -070022class GrTextBlobCache;
joshualitt6c2c2b02015-07-24 10:37:00 -070023class SkGlyph;
joshualitt1d89e8d2015-04-01 12:40:54 -070024
25/*
joshualitt8e84a1e2016-02-16 11:09:25 -080026 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text
joshualitt1d89e8d2015-04-01 12:40:54 -070027 */
Herb Derby26cbe512018-05-24 14:39:01 -040028class GrTextContext {
joshualitt1d89e8d2015-04-01 12:40:54 -070029public:
Brian Salomonaf597482017-11-07 16:23:34 -050030 struct Options {
31 /**
32 * Below this size (in device space) distance field text will not be used. Negative means
33 * use a default value.
34 */
35 SkScalar fMinDistanceFieldFontSize = -1.f;
36 /**
37 * Above this size (in device space) distance field text will not be used and glyphs will
38 * be rendered from outline as individual paths. Negative means use a default value.
39 */
40 SkScalar fMaxDistanceFieldFontSize = -1.f;
Brian Salomonb5086962017-12-13 10:59:33 -050041 /** Forces all distance field vertices to use 3 components, not just when in perspective. */
42 bool fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonaf597482017-11-07 16:23:34 -050043 };
44
Herb Derby26cbe512018-05-24 14:39:01 -040045 static std::unique_ptr<GrTextContext> Make(const Options& options);
joshualitt1d89e8d2015-04-01 12:40:54 -070046
Brian Salomonf18b1d82017-10-27 11:30:49 -040047 void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
Brian Salomon82f44312017-01-11 13:42:54 -050048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
49 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
joshualitt8e84a1e2016-02-16 11:09:25 -080050 const SkPoint& offset, const SkIRect& regionClipBounds);
Herb Derbycddab252018-07-16 11:19:04 -040051 void drawGlyphRunList(GrContext*, GrTextUtils::Target*, const GrClip&,
52 const SkMatrix& viewMatrix, const SkSurfaceProps&, SkGlyphRunList*,
53 const SkIRect& clipBounds);
joshualitt1d89e8d2015-04-01 12:40:54 -070054
Robert Phillips7c525e62018-06-12 10:11:12 -040055 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*,
56 GrTextContext*,
57 GrRenderTargetContext*,
58 const SkPaint&,
59 const SkMatrix& viewMatrix,
60 const char* text,
61 int x,
62 int y);
Robert Phillipsd2e9f762018-03-07 11:54:37 -050063
Khushal3e7548c2018-05-23 15:45:01 -070064 static void SanitizeOptions(Options* options);
65 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
66 const SkSurfaceProps& props,
67 bool contextSupportsDistanceFieldText,
68 const Options& options);
Herb Derby86240592018-05-24 16:12:31 -040069 static void InitDistanceFieldPaint(GrTextBlob* blob,
Khushal3e7548c2018-05-23 15:45:01 -070070 SkPaint* skPaint,
71 const SkMatrix& viewMatrix,
72 const Options& options,
73 SkScalar* textRatio,
74 SkScalerContextFlags* flags);
75
Jim Van Verthc401bb92018-02-15 14:05:24 -050076 class FallbackTextHelper {
77 public:
78 FallbackTextHelper(const SkMatrix& viewMatrix,
Jim Van Verth080a9282018-03-02 10:41:43 -050079 const SkPaint& pathPaint,
Khushalfa8ff092018-06-06 17:46:38 -070080 SkScalar maxTextSize,
Jim Van Verthc401bb92018-02-15 14:05:24 -050081 SkScalar textRatio)
Khushalfa8ff092018-06-06 17:46:38 -070082 : fViewMatrix(viewMatrix)
83 , fTextSize(pathPaint.getTextSize())
84 , fMaxTextSize(maxTextSize)
85 , fTextRatio(textRatio)
86 , fTransformedFallbackTextSize(fMaxTextSize)
87 , fUseTransformedFallback(false) {
Jim Van Verthc401bb92018-02-15 14:05:24 -050088 fMaxScale = viewMatrix.getMaxScale();
89 }
90
91 void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos);
Herb Derby86240592018-05-24 16:12:31 -040092 void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050093 const GrTextUtils::Paint&, SkScalerContextFlags);
Jim Van Verthc401bb92018-02-15 14:05:24 -050094
Khushalfa8ff092018-06-06 17:46:38 -070095 void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const;
96 const SkTDArray<char>& fallbackText() const { return fFallbackTxt; }
97
Jim Van Verthc401bb92018-02-15 14:05:24 -050098 private:
99 SkTDArray<char> fFallbackTxt;
100 SkTDArray<SkPoint> fFallbackPos;
101
102 const SkMatrix& fViewMatrix;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500103 SkScalar fTextSize;
104 SkScalar fMaxTextSize;
Jim Van Verth080a9282018-03-02 10:41:43 -0500105 SkScalar fTextRatio;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400106 SkScalar fTransformedFallbackTextSize;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500107 SkScalar fMaxScale;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400108 bool fUseTransformedFallback;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500109 };
110
Khushalfa8ff092018-06-06 17:46:38 -0700111private:
112 GrTextContext(const Options& options);
113
joshualitt1d89e8d2015-04-01 12:40:54 -0700114 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -0500115 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -0700116 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -0500117 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derbycddab252018-07-16 11:19:04 -0400118
119 void regenerateGlyphRunList(GrTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500120 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500121 const GrShaderCaps&,
122 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500123 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500124 const SkMatrix& viewMatrix,
125 const SkSurfaceProps&,
Herb Derbycddab252018-07-16 11:19:04 -0400126 SkGlyphRunList* glyphRunList) const;
joshualitt9bd2daf2015-04-17 09:30:06 -0700127
Herb Derby86240592018-05-24 16:12:31 -0400128 sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500129 const GrShaderCaps&,
130 const GrTextUtils::Paint&,
131 SkScalerContextFlags scalerContextFlags,
132 const SkMatrix& viewMatrix,
133 const SkSurfaceProps&,
134 const char text[], size_t byteLength,
135 const SkScalar pos[],
136 int scalarsPerPosition,
137 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500138
Herb Derby86240592018-05-24 16:12:31 -0400139 // Functions for appending BMP text to GrTextBlob
Herb Derby86240592018-05-24 16:12:31 -0400140 static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500141 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500142 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500143 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthc401bb92018-02-15 14:05:24 -0500144 int scalarsPerPosition, const SkPoint& offset);
Brian Salomon52db9402017-11-07 14:58:55 -0500145
Herb Derby86240592018-05-24 16:12:31 -0400146 static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500147 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
148 SkScalerContextFlags scalerContextFlags,
149 const SkMatrix& viewMatrix,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500150 const char text[], size_t byteLength,
151 const SkScalar pos[], int scalarsPerPosition,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500152 const SkPoint& offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500153
Brian Salomon52db9402017-11-07 14:58:55 -0500154 // functions for appending distance field text
Herb Derby86240592018-05-24 16:12:31 -0400155 void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500156 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500157 SkScalerContextFlags scalerContextFlags,
158 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500159 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
160 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500161
Herb Derby86240592018-05-24 16:12:31 -0400162 static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500163 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400164 GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);
Brian Salomon52db9402017-11-07 14:58:55 -0500165
Herb Derby86240592018-05-24 16:12:31 -0400166 static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500167 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500168 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500169
Hal Canary144caf52016-11-07 17:57:18 -0500170 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700171
Hal Canary144caf52016-11-07 17:57:18 -0500172 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700173
Khushal3e7548c2018-05-23 15:45:01 -0700174 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500175
Hal Canary6f6961e2017-01-31 13:50:44 -0500176#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500177 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
178 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400179 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700180#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700181};
182
Herb Derby86240592018-05-24 16:12:31 -0400183#endif // GrTextContext_DEFINED