1/*
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 GrTextContext_DEFINED
9#define GrTextContext_DEFINED
10
11#include "src/core/SkGlyphRun.h"
12#include "src/gpu/GrGeometryProcessor.h"
13#include "src/gpu/text/GrTextTarget.h"
14
15#if GR_TEST_UTILS
16#include "src/gpu/GrDrawOpTest.h"
17#endif
18
19class GrDrawOp;
20class GrRecordingContext;
21class GrRenderTargetContext;
22class GrTextBlobCache;
23class SkGlyph;
24class GrTextBlob;
25
26/*
27 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text
28 */
29class GrTextContext {
30public:
31 struct Options {
32 /**
33 * Below this size (in device space) distance field text will not be used. Negative means
34 * use a default value.
35 */
36 SkScalar fMinDistanceFieldFontSize = -1.f;
37 /**
38 * Above this size (in device space) distance field text will not be used and glyphs will
39 * be rendered from outline as individual paths. Negative means use a default value.
40 */
41 SkScalar fMaxDistanceFieldFontSize = -1.f;
42 /** Forces all distance field vertices to use 3 components, not just when in perspective. */
43 bool fDistanceFieldVerticesAlwaysHaveW = false;
44 };
45
46 static std::unique_ptr<GrTextContext> Make(const Options& options);
47
48 void drawGlyphRunList(GrRecordingContext*, GrTextTarget*, const GrClip&,
49 const SkMatrix& drawMatrix, const SkSurfaceProps&, const SkGlyphRunList&);
50
51 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrRecordingContext*,
52 GrTextContext*,
53 GrRenderTargetContext*,
54 const SkPaint&, const SkFont&,
55 const SkMatrix& drawMatrix,
56 const char* text,
57 int x,
58 int y);
59
60 static void SanitizeOptions(Options* options);
61 static bool CanDrawAsDistanceFields(const SkPaint&, const SkFont&, const SkMatrix& viewMatrix,
62 const SkSurfaceProps& props,
63 bool contextSupportsDistanceFieldText,
64 const Options& options);
65
66 static SkFont InitDistanceFieldFont(const SkFont& font,
67 const SkMatrix& viewMatrix,
68 const Options& options,
69 SkScalar* textRatio);
70
71 static SkPaint InitDistanceFieldPaint(const SkPaint& paint);
72
73 static std::pair<SkScalar, SkScalar> InitDistanceFieldMinMaxScale(SkScalar textSize,
74 const SkMatrix& viewMatrix,
75 const Options& options);
76
77private:
78 GrTextContext(const Options& options);
79
80 // sets up the descriptor on the blob and returns a detached cache. Client must attach
81 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
82 // Determines if we need to use fake gamma (and contrast boost):
83 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorInfo&);
84
85 Options fOptions;
86
87#if GR_TEST_UTILS
88 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
89 SkScalerContextFlags::kFakeGammaAndBoostContrast;
90 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
91#endif
92};
93
94#endif // GrTextContext_DEFINED
95