gradtext.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2011 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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkShader.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTileMode.h"
  20. #include "include/core/SkTypeface.h"
  21. #include "include/core/SkTypes.h"
  22. #include "include/effects/SkGradientShader.h"
  23. #include "tools/ToolUtils.h"
  24. namespace {
  25. // test shader w/ transparency
  26. static sk_sp<SkShader> make_grad(SkScalar width) {
  27. SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
  28. SkPoint pts[] = { { 0, 0 }, { width, 0 } };
  29. return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  30. SkTileMode::kMirror);
  31. }
  32. // test opaque shader
  33. static sk_sp<SkShader> make_grad2(SkScalar width) {
  34. SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
  35. SkPoint pts[] = { { 0, 0 }, { width, 0 } };
  36. return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  37. SkTileMode::kMirror);
  38. }
  39. static sk_sp<SkShader> make_chrome_solid() {
  40. SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
  41. SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
  42. return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
  43. }
  44. // Replicate chrome layout test - clipped pathed gradient-shaded text
  45. class ChromeGradTextGM1 : public skiagm::GM {
  46. SkString onShortName() override { return SkString("chrome_gradtext1"); }
  47. SkISize onISize() override { return {500, 480}; }
  48. void onDraw(SkCanvas* canvas) override {
  49. SkPaint paint;
  50. SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
  51. canvas->clipRect(r);
  52. paint.setColor(SK_ColorRED);
  53. canvas->drawRect(r, paint);
  54. // Minimal repro doesn't require AA, LCD, or a nondefault typeface
  55. paint.setShader(make_chrome_solid());
  56. SkFont font(ToolUtils::create_portable_typeface(), 500);
  57. font.setEdging(SkFont::Edging::kAlias);
  58. canvas->drawString("I", 0, 100, font, paint);
  59. }
  60. };
  61. // Replicate chrome layout test - switching between solid & gradient text
  62. class ChromeGradTextGM2 : public skiagm::GM {
  63. SkString onShortName() override { return SkString("chrome_gradtext2"); }
  64. SkISize onISize() override { return {500, 480}; }
  65. void onDraw(SkCanvas* canvas) override {
  66. SkPaint paint;
  67. SkFont font(ToolUtils::create_portable_typeface());
  68. font.setEdging(SkFont::Edging::kAlias);
  69. paint.setStyle(SkPaint::kFill_Style);
  70. canvas->drawString("Normal Fill Text", 0, 50, font, paint);
  71. paint.setStyle(SkPaint::kStroke_Style);
  72. canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
  73. // Minimal repro doesn't require AA, LCD, or a nondefault typeface
  74. paint.setShader(make_chrome_solid());
  75. paint.setStyle(SkPaint::kFill_Style);
  76. canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
  77. paint.setStyle(SkPaint::kStroke_Style);
  78. canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
  79. }
  80. };
  81. } // namespace
  82. DEF_GM( return new ChromeGradTextGM1; )
  83. DEF_GM( return new ChromeGradTextGM2; )
  84. DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
  85. static constexpr float kTextSize = 26.0f;
  86. SkFont font(ToolUtils::create_portable_typeface(), kTextSize);
  87. canvas->drawRect({0, 0, 500, 240}, SkPaint());
  88. canvas->translate(20.0f, kTextSize);
  89. SkPaint paints[2];
  90. paints[0].setShader(make_grad(80.0f));
  91. paints[1].setShader(make_grad2(80.0f));
  92. static const SkFont::Edging edgings[3] = {
  93. SkFont::Edging::kAlias,
  94. SkFont::Edging::kAntiAlias,
  95. SkFont::Edging::kSubpixelAntiAlias,
  96. };
  97. for (int i = 0; i < 2; ++i) {
  98. for (const SkPaint& paint : paints) {
  99. for (SkFont::Edging edging : edgings) {
  100. font.setEdging(edging);
  101. canvas->drawString("When in the course of human events", 0, 0, font, paint);
  102. canvas->translate(0, kTextSize * 4/3);
  103. }
  104. canvas->translate(0, kTextSize * 2/3);
  105. }
  106. }
  107. }