blob: 455667a126c5b3e3d5ffb1b8ddebbd2cf9c1b4ba [file] [log] [blame]
fmalita84833262014-09-19 11:40:51 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkFontTypes.h"
13#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPoint.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkGradientShader.h"
25#include "include/private/SkTDArray.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "tools/ToolUtils.h"
27
28#include <math.h>
29#include <string.h>
fmalita84833262014-09-19 11:40:51 -070030
31// This GM exercises drawTextBlob offset vs. shader space behavior.
32class TextBlobShaderGM : public skiagm::GM {
33public:
Mike Klein0fe39ba2018-12-28 11:12:50 -050034 TextBlobShaderGM() {}
fmalita84833262014-09-19 11:40:51 -070035
Mike Klein0fe39ba2018-12-28 11:12:50 -050036private:
mtklein36352bf2015-03-25 18:17:31 -070037 void onOnceBeforeDraw() override {
Mike Klein0fe39ba2018-12-28 11:12:50 -050038 {
Mike Kleinea3f0142019-03-20 11:12:10 -050039 SkFont font(ToolUtils::create_portable_typeface());
Mike Klein0fe39ba2018-12-28 11:12:50 -050040 const char* txt = "Blobber";
41 size_t txtLen = strlen(txt);
Ben Wagner51e15a62019-05-07 15:38:46 -040042 fGlyphs.append(font.countText(txt, txtLen, SkTextEncoding::kUTF8));
43 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count());
Mike Klein0fe39ba2018-12-28 11:12:50 -050044 }
45
Mike Reed3185f902018-10-26 16:33:00 -040046 SkFont font;
Mike Reed5f50f572018-11-12 13:19:37 -050047 font.setSubpixel(true);
48 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed3185f902018-10-26 16:33:00 -040049 font.setSize(30);
Mike Kleinea3f0142019-03-20 11:12:10 -050050 font.setTypeface(ToolUtils::create_portable_typeface());
fmalita84833262014-09-19 11:40:51 -070051
52 SkTextBlobBuilder builder;
53 int glyphCount = fGlyphs.count();
54 const SkTextBlobBuilder::RunBuffer* run;
55
Mike Reed3185f902018-10-26 16:33:00 -040056 run = &builder.allocRun(font, glyphCount, 10, 10, nullptr);
fmalita84833262014-09-19 11:40:51 -070057 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
58
Mike Reed3185f902018-10-26 16:33:00 -040059 run = &builder.allocRunPosH(font, glyphCount, 80, nullptr);
fmalita84833262014-09-19 11:40:51 -070060 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
61 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040062 run->pos[i] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070063 }
64
Mike Reed3185f902018-10-26 16:33:00 -040065 run = &builder.allocRunPos(font, glyphCount, nullptr);
fmalita84833262014-09-19 11:40:51 -070066 memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
67 for (int i = 0; i < glyphCount; ++i) {
Mike Reed3185f902018-10-26 16:33:00 -040068 run->pos[i * 2] = font.getSize() * i * .75f;
fmalita84833262014-09-19 11:40:51 -070069 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
70 }
71
fmalita37283c22016-09-13 10:00:23 -070072 fBlob = builder.make();
fmalita84833262014-09-19 11:40:51 -070073
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();
reed1a9b9642016-03-13 14:13:58 -070084 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 Reedfae8fce2019-04-03 10:27:45 -040088 SkTileMode::kRepeat);
fmalita84833262014-09-19 11:40:51 -070089 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 SkString onShortName() override {
fmalita84833262014-09-19 11:40:51 -070092 return SkString("textblobshader");
93 }
94
mtklein36352bf2015-03-25 18:17:31 -070095 SkISize onISize() override {
fmalita84833262014-09-19 11:40:51 -070096 return SkISize::Make(640, 480);
97 }
98
mtklein36352bf2015-03-25 18:17:31 -070099 void onDraw(SkCanvas* canvas) override {
fmalita84833262014-09-19 11:40:51 -0700100 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -0400101 p.setAntiAlias(true);
fmalita84833262014-09-19 11:40:51 -0700102 p.setStyle(SkPaint::kFill_Style);
103 p.setShader(fShader);
104
105 SkISize sz = this->onISize();
mtkleindbfd7ab2016-09-01 11:24:54 -0700106 constexpr int kXCount = 4;
107 constexpr int kYCount = 3;
fmalita84833262014-09-19 11:40:51 -0700108 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
fmalita37283c22016-09-13 10:00:23 -0700118 SkTDArray<uint16_t> fGlyphs;
119 sk_sp<SkTextBlob> fBlob;
120 sk_sp<SkShader> fShader;
fmalita84833262014-09-19 11:40:51 -0700121
John Stiles7571f9e2020-09-02 22:42:33 -0400122 using INHERITED = skiagm::GM;
fmalita84833262014-09-19 11:40:51 -0700123};
124
Mike Klein0fe39ba2018-12-28 11:12:50 -0500125DEF_GM(return new TextBlobShaderGM;)