fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 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 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPoint.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkTextBlob.h" |
| 15 | #include "SkTDArray.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
| 18 | // This GM exercises drawTextBlob offset vs. shader space behavior. |
| 19 | class TextBlobShaderGM : public skiagm::GM { |
| 20 | public: |
| 21 | TextBlobShaderGM(const char* txt) { |
| 22 | SkPaint p; |
caryclark | efa1ece | 2015-07-28 11:55:49 -0700 | [diff] [blame] | 23 | sk_tool_utils::set_portable_typeface(&p); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 24 | size_t txtLen = strlen(txt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | fGlyphs.append(p.textToGlyphs(txt, txtLen, nullptr)); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 26 | p.textToGlyphs(txt, txtLen, fGlyphs.begin()); |
| 27 | } |
| 28 | |
| 29 | protected: |
| 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | void onOnceBeforeDraw() override { |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 32 | SkPaint p; |
| 33 | p.setAntiAlias(true); |
| 34 | p.setSubpixelText(true); |
| 35 | p.setTextSize(30); |
| 36 | p.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 37 | sk_tool_utils::set_portable_typeface(&p); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 38 | |
| 39 | SkTextBlobBuilder builder; |
| 40 | int glyphCount = fGlyphs.count(); |
| 41 | const SkTextBlobBuilder::RunBuffer* run; |
| 42 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | run = &builder.allocRun(p, glyphCount, 10, 10, nullptr); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 44 | memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t)); |
| 45 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | run = &builder.allocRunPosH(p, glyphCount, 80, nullptr); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 47 | memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t)); |
| 48 | for (int i = 0; i < glyphCount; ++i) { |
| 49 | run->pos[i] = p.getTextSize() * i * .75f; |
| 50 | } |
| 51 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | run = &builder.allocRunPos(p, glyphCount, nullptr); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 53 | memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t)); |
| 54 | for (int i = 0; i < glyphCount; ++i) { |
| 55 | run->pos[i * 2] = p.getTextSize() * i * .75f; |
| 56 | run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount); |
| 57 | } |
| 58 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 59 | fBlob = builder.make(); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 60 | |
| 61 | SkColor colors[2]; |
| 62 | colors[0] = SK_ColorRED; |
| 63 | colors[1] = SK_ColorGREEN; |
| 64 | |
| 65 | SkScalar pos[SK_ARRAY_COUNT(colors)]; |
| 66 | for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 67 | pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1); |
| 68 | } |
| 69 | |
| 70 | SkISize sz = this->onISize(); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 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 | SkShader::kRepeat_TileMode); |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 76 | } |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | SkString onShortName() override { |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 79 | return SkString("textblobshader"); |
| 80 | } |
| 81 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | SkISize onISize() override { |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 83 | return SkISize::Make(640, 480); |
| 84 | } |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | void onDraw(SkCanvas* canvas) override { |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 87 | SkPaint p; |
| 88 | p.setStyle(SkPaint::kFill_Style); |
| 89 | p.setShader(fShader); |
| 90 | |
| 91 | SkISize sz = this->onISize(); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 92 | constexpr int kXCount = 4; |
| 93 | constexpr int kYCount = 3; |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 94 | for (int i = 0; i < kXCount; ++i) { |
| 95 | for (int j = 0; j < kYCount; ++j) { |
| 96 | canvas->drawTextBlob(fBlob, |
| 97 | SkIntToScalar(i * sz.width() / kXCount), |
| 98 | SkIntToScalar(j * sz.height() / kYCount), |
| 99 | p); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 105 | SkTDArray<uint16_t> fGlyphs; |
| 106 | sk_sp<SkTextBlob> fBlob; |
| 107 | sk_sp<SkShader> fShader; |
fmalita | 8483326 | 2014-09-19 11:40:51 -0700 | [diff] [blame] | 108 | |
| 109 | typedef skiagm::GM INHERITED; |
| 110 | }; |
| 111 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 112 | DEF_GM(return new TextBlobShaderGM("Blobber");) |