blob: 0201da4a7d6ec256247fa68ec6ab3f650c99639e [file] [log] [blame]
fmalita00d5c2c2014-08-21 08:53:26 -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"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFontStyle.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTextBlob.h"
22#include "include/core/SkTypeface.h"
23#include "include/core/SkTypes.h"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050024#include "include/private/base/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040026#include "tools/fonts/FontToolUtils.h"
fmalita00d5c2c2014-08-21 08:53:26 -070027
Ben Wagnercb3d49c2018-03-14 15:07:43 -040028#include <cstring>
fmalita00d5c2c2014-08-21 08:53:26 -070029
30namespace {
31
32enum Pos {
33 kDefault_Pos = 0,
34 kScalar_Pos = 1,
35 kPoint_Pos = 2,
36};
37
38const struct BlobCfg {
39 unsigned count;
40 Pos pos;
41 SkScalar scale;
42} blobConfigs[][3][3] = {
43 {
44 { { 1024, kDefault_Pos, 1 }, { 0, kDefault_Pos, 0 }, { 0, kDefault_Pos, 0 } },
45 { { 1024, kScalar_Pos, 1 }, { 0, kScalar_Pos, 0 }, { 0, kScalar_Pos, 0 } },
46 { { 1024, kPoint_Pos, 1 }, { 0, kPoint_Pos, 0 }, { 0, kPoint_Pos, 0 } },
47 },
48 {
fmalita37ecbaf2014-08-22 09:01:19 -070049 { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 } },
50 { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 } },
51 { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 } },
fmalita00d5c2c2014-08-21 08:53:26 -070052 },
53
54 {
fmalita37ecbaf2014-08-22 09:01:19 -070055 { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
56 { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
57 { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
fmalita00d5c2c2014-08-21 08:53:26 -070058 },
59
60 {
fmalita37ecbaf2014-08-22 09:01:19 -070061 { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
62 { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
63 { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
64 },
65
66 {
67 { { 4, kDefault_Pos, .75f }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1.25f } },
68 { { 4, kScalar_Pos, .75f }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1.25f } },
69 { { 4, kPoint_Pos, .75f }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1.25f } },
70 },
71
72 {
73 { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, .75f }, { 4, kPoint_Pos, 1.25f } },
74 { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, .75f }, { 4, kDefault_Pos, 1.25f } },
75 { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, .75f }, { 4, kScalar_Pos, 1.25f } },
fmalita00d5c2c2014-08-21 08:53:26 -070076 },
77};
78
79const SkScalar kFontSize = 16;
John Stilesa6841be2020-08-06 14:11:56 -040080} // namespace
fmalita00d5c2c2014-08-21 08:53:26 -070081
82class TextBlobGM : public skiagm::GM {
83public:
fmalitaacb882c2014-09-16 17:58:34 -070084 TextBlobGM(const char* txt)
kkinnunenb4a797f2015-05-21 06:15:28 -070085 : fText(txt) {
fmalita00d5c2c2014-08-21 08:53:26 -070086 }
87
88protected:
kkinnunenb4a797f2015-05-21 06:15:28 -070089 void onOnceBeforeDraw() override {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040090 fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle());
Mike Reedf78b7ea2018-12-25 22:06:17 -050091 SkFont font(fTypeface);
kkinnunenb4a797f2015-05-21 06:15:28 -070092 size_t txtLen = strlen(fText);
Ben Wagner51e15a62019-05-07 15:38:46 -040093 int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8);
kkinnunenb4a797f2015-05-21 06:15:28 -070094
95 fGlyphs.append(glyphCount);
Ben Wagner51e15a62019-05-07 15:38:46 -040096 font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount);
kkinnunenb4a797f2015-05-21 06:15:28 -070097 }
98
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000099 SkString getName() const override { return SkString("textblob"); }
fmalita00d5c2c2014-08-21 08:53:26 -0700100
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000101 SkISize getISize() override { return SkISize::Make(640, 480); }
fmalita00d5c2c2014-08-21 08:53:26 -0700102
mtklein36352bf2015-03-25 18:17:31 -0700103 void onDraw(SkCanvas* canvas) override {
Herb Derbyc37b3862022-06-21 09:49:17 -0400104 for (unsigned b = 0; b < std::size(blobConfigs); ++b) {
fmalita37283c22016-09-13 10:00:23 -0700105 sk_sp<SkTextBlob> blob(this->makeBlob(b));
fmalita00d5c2c2014-08-21 08:53:26 -0700106
107 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -0400108 p.setAntiAlias(true);
fmalita00d5c2c2014-08-21 08:53:26 -0700109 SkPoint offset = SkPoint::Make(SkIntToScalar(10 + 300 * (b % 2)),
110 SkIntToScalar(20 + 150 * (b / 2)));
111
112 canvas->drawTextBlob(blob, offset.x(), offset.y(), p);
113
114 p.setColor(SK_ColorBLUE);
115 p.setStyle(SkPaint::kStroke_Style);
116 SkRect box = blob->bounds();
117 box.offset(offset);
Mike Reed3185f902018-10-26 16:33:00 -0400118 p.setAntiAlias(false);
fmalita00d5c2c2014-08-21 08:53:26 -0700119 canvas->drawRect(box, p);
120
121 }
122 }
123
124private:
fmalita37283c22016-09-13 10:00:23 -0700125 sk_sp<SkTextBlob> makeBlob(unsigned blobIndex) {
fmalita00d5c2c2014-08-21 08:53:26 -0700126 SkTextBlobBuilder builder;
127
Mike Reed3185f902018-10-26 16:33:00 -0400128 SkFont font;
Mike Reed5f50f572018-11-12 13:19:37 -0500129 font.setSubpixel(true);
130 font.setEdging(SkFont::Edging::kAntiAlias);
fmalitaacb882c2014-09-16 17:58:34 -0700131 font.setTypeface(fTypeface);
fmalita00d5c2c2014-08-21 08:53:26 -0700132
Herb Derbyc37b3862022-06-21 09:49:17 -0400133 for (unsigned l = 0; l < std::size(blobConfigs[blobIndex]); ++l) {
fmalita00d5c2c2014-08-21 08:53:26 -0700134 unsigned currentGlyph = 0;
135
Herb Derbyc37b3862022-06-21 09:49:17 -0400136 for (unsigned c = 0; c < std::size(blobConfigs[blobIndex][l]); ++c) {
fmalita00d5c2c2014-08-21 08:53:26 -0700137 const BlobCfg* cfg = &blobConfigs[blobIndex][l][c];
138 unsigned count = cfg->count;
139
Herb Derby3050ef52022-09-29 21:12:37 -0400140 if (count > fGlyphs.size() - currentGlyph) {
141 count = fGlyphs.size() - currentGlyph;
fmalita00d5c2c2014-08-21 08:53:26 -0700142 }
143 if (0 == count) {
144 break;
145 }
146
Mike Reed3185f902018-10-26 16:33:00 -0400147 font.setSize(kFontSize * cfg->scale);
148 const SkScalar advanceX = font.getSize() * 0.85f;
149 const SkScalar advanceY = font.getSize() * 1.5f;
fmalita00d5c2c2014-08-21 08:53:26 -0700150
151 SkPoint offset = SkPoint::Make(currentGlyph * advanceX + c * advanceX,
152 advanceY * l);
153 switch (cfg->pos) {
154 case kDefault_Pos: {
155 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, count,
156 offset.x(),
157 offset.y());
158 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
159 } break;
160 case kScalar_Pos: {
161 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPosH(font, count,
162 offset.y());
163 SkTDArray<SkScalar> pos;
164 for (unsigned i = 0; i < count; ++i) {
165 *pos.append() = offset.x() + i * advanceX;
166 }
167
168 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
169 memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar));
170 } break;
171 case kPoint_Pos: {
172 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPos(font, count);
173
174 SkTDArray<SkScalar> pos;
175 for (unsigned i = 0; i < count; ++i) {
176 *pos.append() = offset.x() + i * advanceX;
177 *pos.append() = offset.y() + i * (advanceY / count);
178 }
179
180 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
181 memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar) * 2);
182 } break;
183 default:
djsollenf2b340f2016-01-29 08:51:04 -0800184 SK_ABORT("unhandled pos value");
fmalita00d5c2c2014-08-21 08:53:26 -0700185 }
186
187 currentGlyph += count;
188 }
189 }
190
fmalita37283c22016-09-13 10:00:23 -0700191 return builder.make();
fmalita00d5c2c2014-08-21 08:53:26 -0700192 }
193
bungeman13b9c952016-05-12 10:09:30 -0700194 SkTDArray<uint16_t> fGlyphs;
195 sk_sp<SkTypeface> fTypeface;
196 const char* fText;
John Stiles7571f9e2020-09-02 22:42:33 -0400197 using INHERITED = skiagm::GM;
fmalita00d5c2c2014-08-21 08:53:26 -0700198};
199
halcanary385fe4d2015-08-26 13:07:48 -0700200DEF_GM(return new TextBlobGM("hamburgefons");)