shadertext3.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFilterQuality.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkFontTypes.h"
  14. #include "include/core/SkMatrix.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkShader.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTileMode.h"
  22. #include "include/core/SkTypeface.h"
  23. #include "include/core/SkTypes.h"
  24. #include "include/effects/SkGradientShader.h"
  25. #include "tools/ToolUtils.h"
  26. #include <string.h>
  27. namespace skiagm {
  28. static void makebm(SkBitmap* bm, int w, int h) {
  29. bm->allocN32Pixels(w, h);
  30. bm->eraseColor(SK_ColorTRANSPARENT);
  31. SkCanvas canvas(*bm);
  32. SkScalar s = SkIntToScalar(SkMin32(w, h));
  33. const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
  34. const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
  35. const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
  36. const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
  37. const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
  38. SkPaint paint;
  39. paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
  40. SK_ARRAY_COUNT(kColors0), SkTileMode::kClamp));
  41. canvas.drawPaint(paint);
  42. paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
  43. SK_ARRAY_COUNT(kColors1), SkTileMode::kClamp));
  44. canvas.drawPaint(paint);
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////
  47. struct LabeledMatrix {
  48. SkMatrix fMatrix;
  49. const char* fLabel;
  50. };
  51. constexpr int kPointSize = 300;
  52. class ShaderText3GM : public GM {
  53. public:
  54. ShaderText3GM() {
  55. this->setBGColor(0xFFDDDDDD);
  56. }
  57. protected:
  58. SkString onShortName() override {
  59. return SkString("shadertext3");
  60. }
  61. SkISize onISize() override { return SkISize::Make(820, 930); }
  62. void onOnceBeforeDraw() override {
  63. makebm(&fBmp, kPointSize / 4, kPointSize / 4);
  64. }
  65. void onDraw(SkCanvas* canvas) override {
  66. SkPaint bmpPaint;
  67. bmpPaint.setAntiAlias(true);
  68. bmpPaint.setFilterQuality(kLow_SkFilterQuality);
  69. bmpPaint.setAlphaf(0.5f);
  70. canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
  71. SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kPointSize));
  72. SkPaint outlinePaint;
  73. outlinePaint.setStyle(SkPaint::kStroke_Style);
  74. outlinePaint.setStrokeWidth(0.f);
  75. canvas->translate(15.f, 15.f);
  76. // draw glyphs scaled up
  77. canvas->scale(2.f, 2.f);
  78. constexpr SkTileMode kTileModes[] = {
  79. SkTileMode::kRepeat,
  80. SkTileMode::kMirror,
  81. };
  82. // position the baseline of the first run
  83. canvas->translate(0.f, 0.75f * kPointSize);
  84. canvas->save();
  85. int i = 0;
  86. for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
  87. for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
  88. SkMatrix localM;
  89. localM.setTranslate(5.f, 5.f);
  90. localM.postRotate(20);
  91. localM.postScale(1.15f, .85f);
  92. SkPaint fillPaint;
  93. fillPaint.setAntiAlias(true);
  94. fillPaint.setFilterQuality(kLow_SkFilterQuality);
  95. fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1], &localM));
  96. constexpr char kText[] = "B";
  97. canvas->drawString(kText, 0, 0, font, fillPaint);
  98. canvas->drawString(kText, 0, 0, font, outlinePaint);
  99. SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8);
  100. canvas->translate(w + 10.f, 0.f);
  101. ++i;
  102. if (!(i % 2)) {
  103. canvas->restore();
  104. canvas->translate(0, 0.75f * kPointSize);
  105. canvas->save();
  106. }
  107. }
  108. }
  109. canvas->restore();
  110. }
  111. private:
  112. SkBitmap fBmp;
  113. typedef GM INHERITED;
  114. };
  115. ///////////////////////////////////////////////////////////////////////////////
  116. DEF_GM( return new ShaderText3GM; )
  117. }