gammatext.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/SkFontStyle.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkTileMode.h"
  21. #include "include/core/SkTypeface.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkGradientShader.h"
  24. static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
  25. const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
  26. return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw), SkTileMode::kClamp);
  27. }
  28. /**
  29. Test a set of clipping problems discovered while writing blitAntiRect,
  30. and test all the code paths through the clipping blitters.
  31. Each region should show as a blue center surrounded by a 2px green
  32. border, with no red.
  33. */
  34. #define HEIGHT 480
  35. class GammaTextGM : public skiagm::GM {
  36. protected:
  37. SkString onShortName() override {
  38. return SkString("gammatext");
  39. }
  40. SkISize onISize() override {
  41. return SkISize::Make(1024, HEIGHT);
  42. }
  43. static void drawGrad(SkCanvas* canvas) {
  44. const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
  45. canvas->clear(SK_ColorRED);
  46. SkPaint paint;
  47. paint.setShader(make_heatGradient(pts));
  48. SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
  49. canvas->drawRect(r, paint);
  50. }
  51. void onDraw(SkCanvas* canvas) override {
  52. drawGrad(canvas);
  53. const SkColor fg[] = {
  54. 0xFFFFFFFF,
  55. 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
  56. 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
  57. 0xFF000000,
  58. };
  59. const char* text = "Hamburgefons";
  60. SkPaint paint;
  61. SkFont font(nullptr, 16);
  62. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  63. SkScalar x = SkIntToScalar(10);
  64. for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
  65. paint.setColor(fg[i]);
  66. SkScalar y = SkIntToScalar(40);
  67. SkScalar stopy = SkIntToScalar(HEIGHT);
  68. while (y < stopy) {
  69. canvas->drawString(text, x, y, font, paint);
  70. y += font.getSize() * 2;
  71. }
  72. x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
  73. }
  74. }
  75. private:
  76. typedef skiagm::GM INHERITED;
  77. };
  78. DEF_GM( return new GammaTextGM; )
  79. //////////////////////////////////////////////////////////////////////////////
  80. static sk_sp<SkShader> make_gradient(SkColor c) {
  81. const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
  82. SkColor colors[2];
  83. colors[0] = c;
  84. colors[1] = SkColorSetA(c, 0);
  85. return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
  86. }
  87. static void draw_pair(SkCanvas* canvas, const SkFont& font, SkColor color,
  88. const sk_sp<SkShader>& shader) {
  89. static const char text[] = "Now is the time for all good";
  90. SkPaint paint;
  91. paint.setColor(color);
  92. canvas->drawString(text, 10, 20, font, paint);
  93. paint.setShader(SkShaders::Color(paint.getColor()));
  94. canvas->drawString(text, 10, 40, font, paint);
  95. paint.setShader(shader);
  96. canvas->drawString(text, 10, 60, font, paint);
  97. }
  98. class GammaShaderTextGM : public skiagm::GM {
  99. sk_sp<SkShader> fShaders[3];
  100. SkColor fColors[3];
  101. public:
  102. GammaShaderTextGM() {
  103. const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
  104. for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
  105. fColors[i] = colors[i];
  106. }
  107. }
  108. protected:
  109. SkString onShortName() override {
  110. return SkString("gammagradienttext");
  111. }
  112. SkISize onISize() override {
  113. return SkISize::Make(300, 300);
  114. }
  115. void onOnceBeforeDraw() override {
  116. for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
  117. fShaders[i] = make_gradient(fColors[i]);
  118. }
  119. }
  120. void onDraw(SkCanvas* canvas) override {
  121. SkPaint paint;
  122. paint.setAntiAlias(true);
  123. SkFont font(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()), 18);
  124. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  125. for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
  126. draw_pair(canvas, font, fColors[i], fShaders[i]);
  127. canvas->translate(0, 80);
  128. }
  129. }
  130. private:
  131. typedef skiagm::GM INHERITED;
  132. };
  133. DEF_GM( return new GammaShaderTextGM; )