blob: 1c6bd0783ba65e338255491dd3816696b5117ce7 [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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050025#include "include/private/base/SkTDArray.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040027#include "tools/fonts/FontToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028
29#include <math.h>
30#include <string.h>
fmalita84833262014-09-19 11:40:51 -070031
32// This GM exercises drawTextBlob offset vs. shader space behavior.
33class TextBlobShaderGM : public skiagm::GM {
34public:
Mike Klein0fe39ba2018-12-28 11:12:50 -050035 TextBlobShaderGM() {}
fmalita84833262014-09-19 11:40:51 -070036
Mike Klein0fe39ba2018-12-28 11:12:50 -050037private:
mtklein36352bf2015-03-25 18:17:31 -070038 void onOnceBeforeDraw() override {
Mike Klein0fe39ba2018-12-28 11:12:50 -050039 {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040040 SkFont font = ToolUtils::DefaultPortableFont();
Mike Klein0fe39ba2018-12-28 11:12:50 -050041 const char* txt = "Blobber";
42 size_t txtLen = strlen(txt);
Ben Wagner51e15a62019-05-07 15:38:46 -040043 fGlyphs.append(font.countText(txt, txtLen, SkTextEncoding::kUTF8));
Herb Derby3050ef52022-09-29 21:12:37 -040044 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.size());
Mike Klein0fe39ba2018-12-28 11:12:50 -050045 }
46
Kevin Lubickbca43ec2023-10-30 10:11:22 -040047 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reed5f50f572018-11-12 13:19:37 -050048 font.setSubpixel(true);
49 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed3185f902018-10-26 16:33:00 -040050 font.setSize(30);
fmalita84833262014-09-19 11:40:51 -070051
52 SkTextBlobBuilder builder;
Herb Derby3050ef52022-09-29 21:12:37 -040053 int glyphCount = fGlyphs.size();
fmalita84833262014-09-19 11:40:51 -070054 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
Herb Derbyc37b3862022-06-21 09:49:17 -040078 SkScalar pos[std::size(colors)];
79 for (unsigned i = 0; i < std::size(pos); ++i) {
80 pos[i] = (float)i / (std::size(pos) - 1);
fmalita84833262014-09-19 11:40:51 -070081 }
82
Leandro Lovisolo8f023882023-08-15 21:13:52 +000083 SkISize sz = this->getISize();
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,
Herb Derbyc37b3862022-06-21 09:49:17 -040087 std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040088 SkTileMode::kRepeat);
fmalita84833262014-09-19 11:40:51 -070089 }
90
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000091 SkString getName() const override { return SkString("textblobshader"); }
fmalita84833262014-09-19 11:40:51 -070092
Leandro Lovisolo8f023882023-08-15 21:13:52 +000093 SkISize getISize() override { return SkISize::Make(640, 480); }
fmalita84833262014-09-19 11:40:51 -070094
mtklein36352bf2015-03-25 18:17:31 -070095 void onDraw(SkCanvas* canvas) override {
fmalita84833262014-09-19 11:40:51 -070096 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -040097 p.setAntiAlias(true);
fmalita84833262014-09-19 11:40:51 -070098 p.setStyle(SkPaint::kFill_Style);
99 p.setShader(fShader);
100
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000101 SkISize sz = this->getISize();
mtkleindbfd7ab2016-09-01 11:24:54 -0700102 constexpr int kXCount = 4;
103 constexpr int kYCount = 3;
fmalita84833262014-09-19 11:40:51 -0700104 for (int i = 0; i < kXCount; ++i) {
105 for (int j = 0; j < kYCount; ++j) {
106 canvas->drawTextBlob(fBlob,
107 SkIntToScalar(i * sz.width() / kXCount),
108 SkIntToScalar(j * sz.height() / kYCount),
109 p);
110 }
111 }
112 }
113
fmalita37283c22016-09-13 10:00:23 -0700114 SkTDArray<uint16_t> fGlyphs;
115 sk_sp<SkTextBlob> fBlob;
116 sk_sp<SkShader> fShader;
fmalita84833262014-09-19 11:40:51 -0700117
John Stiles7571f9e2020-09-02 22:42:33 -0400118 using INHERITED = skiagm::GM;
fmalita84833262014-09-19 11:40:51 -0700119};
120
Mike Klein0fe39ba2018-12-28 11:12:50 -0500121DEF_GM(return new TextBlobShaderGM;)