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