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/GrDistanceFieldAdjustTable.h" 14 #include "src/gpu/text/GrTextTarget.h" 15 16 #if GR_TEST_UTILS 17 #include "src/gpu/GrDrawOpTest.h" 18 #endif 19 20 class GrDrawOp; 21 class GrRecordingContext; 22 class GrTextBlobCache; 23 class SkGlyph; 24 class GrTextBlob; 25 26 /* 27 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text 28 */ 29 class GrTextContext { 30 public: 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& viewMatrix, 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& viewMatrix, 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 77 private: 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 GrColorSpaceInfo&); 84 dfAdjustTable()85 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } 86 87 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; 88 89 Options fOptions; 90 91 #if GR_TEST_UTILS 92 static const SkScalerContextFlags kTextBlobOpScalerContextFlags = 93 SkScalerContextFlags::kFakeGammaAndBoostContrast; 94 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp); 95 #endif 96 }; 97 98 #endif // GrTextContext_DEFINED 99