blob: 50ec0ab59e8eac4d27b7c9871c93aaa13fdbb473 [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);
Robert Phillips38580452018-06-28 12:00:35 +000051 void drawTextBlob(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
52 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*,
Ben Wagner2c312c42018-06-27 14:46:46 -040053 SkScalar x, SkScalar y, 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&);
Robert Phillips38580452018-06-28 12:00:35 +0000118 void regenerateTextBlob(GrTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500119 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500120 const GrShaderCaps&,
121 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500122 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500123 const SkMatrix& viewMatrix,
124 const SkSurfaceProps&,
Ben Wagner2c312c42018-06-27 14:46:46 -0400125 const SkTextBlob* blob, SkScalar x, SkScalar y) const;
Robert Phillips38580452018-06-28 12:00:35 +0000126
127 static bool HasLCD(const SkTextBlob*);
joshualitt9bd2daf2015-04-17 09:30:06 -0700128
Herb Derby86240592018-05-24 16:12:31 -0400129 sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500130 const GrShaderCaps&,
131 const GrTextUtils::Paint&,
132 SkScalerContextFlags scalerContextFlags,
133 const SkMatrix& viewMatrix,
134 const SkSurfaceProps&,
135 const char text[], size_t byteLength,
136 const SkScalar pos[],
137 int scalarsPerPosition,
138 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500139
Herb Derby86240592018-05-24 16:12:31 -0400140 // Functions for appending BMP text to GrTextBlob
Herb Derby86240592018-05-24 16:12:31 -0400141 static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500142 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500143 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500144 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthc401bb92018-02-15 14:05:24 -0500145 int scalarsPerPosition, const SkPoint& offset);
Brian Salomon52db9402017-11-07 14:58:55 -0500146
Herb Derby86240592018-05-24 16:12:31 -0400147 static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500148 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
149 SkScalerContextFlags scalerContextFlags,
150 const SkMatrix& viewMatrix,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500151 const char text[], size_t byteLength,
152 const SkScalar pos[], int scalarsPerPosition,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500153 const SkPoint& offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500154
Brian Salomon52db9402017-11-07 14:58:55 -0500155 // functions for appending distance field text
Herb Derby86240592018-05-24 16:12:31 -0400156 void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500157 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500158 SkScalerContextFlags scalerContextFlags,
159 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500160 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
161 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500162
Herb Derby86240592018-05-24 16:12:31 -0400163 static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500164 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400165 GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);
Brian Salomon52db9402017-11-07 14:58:55 -0500166
Herb Derby86240592018-05-24 16:12:31 -0400167 static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500168 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500169 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500170
Hal Canary144caf52016-11-07 17:57:18 -0500171 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700172
Hal Canary144caf52016-11-07 17:57:18 -0500173 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700174
Khushal3e7548c2018-05-23 15:45:01 -0700175 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500176
Hal Canary6f6961e2017-01-31 13:50:44 -0500177#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500178 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
179 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400180 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700181#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700182};
183
Herb Derby86240592018-05-24 16:12:31 -0400184#endif // GrTextContext_DEFINED