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 "GrDistanceFieldAdjustTable.h" 12 #include "GrGeometryProcessor.h" 13 #include "GrTextTarget.h" 14 #include "SkGlyphRun.h" 15 16 #if GR_TEST_UTILS 17 #include "GrDrawOpTest.h" 18 #endif 19 20 class GrDrawOp; 21 class GrTextBlobCache; 22 class SkGlyph; 23 class GrTextBlob; 24 25 /* 26 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text 27 */ 28 class GrTextContext { 29 public: 30 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; 41 /** Forces all distance field vertices to use 3 components, not just when in perspective. */ 42 bool fDistanceFieldVerticesAlwaysHaveW = false; 43 }; 44 45 static std::unique_ptr<GrTextContext> Make(const Options& options); 46 47 void drawGlyphRunList(GrContext*, GrTextTarget*, const GrClip&, 48 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&); 49 50 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*, 51 GrTextContext*, 52 GrRenderTargetContext*, 53 const SkPaint&, const SkFont&, 54 const SkMatrix& viewMatrix, 55 const char* text, 56 int x, 57 int y); 58 59 static void SanitizeOptions(Options* options); 60 static bool CanDrawAsDistanceFields(const SkPaint&, const SkFont&, const SkMatrix& viewMatrix, 61 const SkSurfaceProps& props, 62 bool contextSupportsDistanceFieldText, 63 const Options& options); 64 static void InitDistanceFieldPaint(SkScalar textSize, 65 const SkMatrix& viewMatrix, 66 const Options& options, 67 GrTextBlob* blob, 68 SkPaint* skPaint, 69 SkFont* skFont, 70 SkScalar* textRatio, 71 SkScalerContextFlags* flags); 72 73 private: 74 GrTextContext(const Options& options); 75 76 // sets up the descriptor on the blob and returns a detached cache. Client must attach 77 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd); 78 // Determines if we need to use fake gamma (and contrast boost): 79 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&); 80 dfAdjustTable()81 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } 82 83 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; 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