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