textblobshader.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2014 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/SkFontTypes.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPoint.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/SkTextBlob.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. #include "include/private/SkTDArray.h"
  25. #include "tools/ToolUtils.h"
  26. #include <math.h>
  27. #include <string.h>
  28. // This GM exercises drawTextBlob offset vs. shader space behavior.
  29. class TextBlobShaderGM : public skiagm::GM {
  30. public:
  31. TextBlobShaderGM() {}
  32. private:
  33. void onOnceBeforeDraw() override {
  34. {
  35. SkFont font(ToolUtils::create_portable_typeface());
  36. const char* txt = "Blobber";
  37. size_t txtLen = strlen(txt);
  38. fGlyphs.append(font.countText(txt, txtLen, SkTextEncoding::kUTF8));
  39. font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count());
  40. }
  41. SkFont font;
  42. font.setSubpixel(true);
  43. font.setEdging(SkFont::Edging::kAntiAlias);
  44. font.setSize(30);
  45. font.setTypeface(ToolUtils::create_portable_typeface());
  46. SkTextBlobBuilder builder;
  47. int glyphCount = fGlyphs.count();
  48. const SkTextBlobBuilder::RunBuffer* run;
  49. run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
  50. memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
  51. run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
  52. memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
  53. for (int i = 0; i < glyphCount; ++i) {
  54. run->pos[i] = font.getSize() * i * .75f;
  55. }
  56. run = &builder.allocRunPos(font, glyphCount, nullptr);
  57. memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
  58. for (int i = 0; i < glyphCount; ++i) {
  59. run->pos[i * 2] = font.getSize() * i * .75f;
  60. run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
  61. }
  62. fBlob = builder.make();
  63. SkColor colors[2];
  64. colors[0] = SK_ColorRED;
  65. colors[1] = SK_ColorGREEN;
  66. SkScalar pos[SK_ARRAY_COUNT(colors)];
  67. for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
  68. pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
  69. }
  70. SkISize sz = this->onISize();
  71. fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
  72. SkIntToScalar(sz.height() / 2)),
  73. sz.width() * .66f, colors, pos,
  74. SK_ARRAY_COUNT(colors),
  75. SkTileMode::kRepeat);
  76. }
  77. SkString onShortName() override {
  78. return SkString("textblobshader");
  79. }
  80. SkISize onISize() override {
  81. return SkISize::Make(640, 480);
  82. }
  83. void onDraw(SkCanvas* canvas) override {
  84. SkPaint p;
  85. p.setAntiAlias(true);
  86. p.setStyle(SkPaint::kFill_Style);
  87. p.setShader(fShader);
  88. SkISize sz = this->onISize();
  89. constexpr int kXCount = 4;
  90. constexpr int kYCount = 3;
  91. for (int i = 0; i < kXCount; ++i) {
  92. for (int j = 0; j < kYCount; ++j) {
  93. canvas->drawTextBlob(fBlob,
  94. SkIntToScalar(i * sz.width() / kXCount),
  95. SkIntToScalar(j * sz.height() / kYCount),
  96. p);
  97. }
  98. }
  99. }
  100. SkTDArray<uint16_t> fGlyphs;
  101. sk_sp<SkTextBlob> fBlob;
  102. sk_sp<SkShader> fShader;
  103. typedef skiagm::GM INHERITED;
  104. };
  105. DEF_GM(return new TextBlobShaderGM;)