blob: 867e6cd9d0eb123ef137cd84475999ee07f3afe0 [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
Ben Wagnercb3d49c2018-03-14 15:07:43 -04008#include "SkCanvas.h"
9#include "SkColor.h"
10#include "SkFontStyle.h"
11#include "SkPaint.h"
12#include "SkPoint.h"
13#include "SkRect.h"
14#include "SkRefCnt.h"
15#include "SkScalar.h"
16#include "SkSize.h"
17#include "SkString.h"
18#include "SkTDArray.h"
19#include "SkTextBlob.h"
20#include "SkTypeface.h"
21#include "SkTypes.h"
fmalita00d5c2c2014-08-21 08:53:26 -070022#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040023#include "sk_tool_utils.h"
fmalita00d5c2c2014-08-21 08:53:26 -070024
Ben Wagnercb3d49c2018-03-14 15:07:43 -040025#include <cstring>
fmalita00d5c2c2014-08-21 08:53:26 -070026
27namespace {
28
29enum Pos {
30 kDefault_Pos = 0,
31 kScalar_Pos = 1,
32 kPoint_Pos = 2,
33};
34
35const struct BlobCfg {
36 unsigned count;
37 Pos pos;
38 SkScalar scale;
39} blobConfigs[][3][3] = {
40 {
41 { { 1024, kDefault_Pos, 1 }, { 0, kDefault_Pos, 0 }, { 0, kDefault_Pos, 0 } },
42 { { 1024, kScalar_Pos, 1 }, { 0, kScalar_Pos, 0 }, { 0, kScalar_Pos, 0 } },
43 { { 1024, kPoint_Pos, 1 }, { 0, kPoint_Pos, 0 }, { 0, kPoint_Pos, 0 } },
44 },
45 {
fmalita37ecbaf2014-08-22 09:01:19 -070046 { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 } },
47 { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 } },
48 { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 } },
fmalita00d5c2c2014-08-21 08:53:26 -070049 },
50
51 {
fmalita37ecbaf2014-08-22 09:01:19 -070052 { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
53 { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
54 { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
fmalita00d5c2c2014-08-21 08:53:26 -070055 },
56
57 {
fmalita37ecbaf2014-08-22 09:01:19 -070058 { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } },
59 { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } },
60 { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } },
61 },
62
63 {
64 { { 4, kDefault_Pos, .75f }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1.25f } },
65 { { 4, kScalar_Pos, .75f }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1.25f } },
66 { { 4, kPoint_Pos, .75f }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1.25f } },
67 },
68
69 {
70 { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, .75f }, { 4, kPoint_Pos, 1.25f } },
71 { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, .75f }, { 4, kDefault_Pos, 1.25f } },
72 { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, .75f }, { 4, kScalar_Pos, 1.25f } },
fmalita00d5c2c2014-08-21 08:53:26 -070073 },
74};
75
76const SkScalar kFontSize = 16;
77}
78
79class TextBlobGM : public skiagm::GM {
80public:
fmalitaacb882c2014-09-16 17:58:34 -070081 TextBlobGM(const char* txt)
kkinnunenb4a797f2015-05-21 06:15:28 -070082 : fText(txt) {
fmalita00d5c2c2014-08-21 08:53:26 -070083 }
84
85protected:
kkinnunenb4a797f2015-05-21 06:15:28 -070086 void onOnceBeforeDraw() override {
mbocee6a9912016-05-31 11:42:36 -070087 fTypeface = sk_tool_utils::create_portable_typeface("serif", SkFontStyle());
kkinnunenb4a797f2015-05-21 06:15:28 -070088 SkPaint p;
89 p.setTypeface(fTypeface);
90 size_t txtLen = strlen(fText);
halcanary96fcdcc2015-08-27 07:41:13 -070091 int glyphCount = p.textToGlyphs(fText, txtLen, nullptr);
kkinnunenb4a797f2015-05-21 06:15:28 -070092
93 fGlyphs.append(glyphCount);
94 p.textToGlyphs(fText, txtLen, fGlyphs.begin());
95 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 SkString onShortName() override {
fmalita00d5c2c2014-08-21 08:53:26 -070098 return SkString("textblob");
99 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 SkISize onISize() override {
fmalita00d5c2c2014-08-21 08:53:26 -0700102 return SkISize::Make(640, 480);
103 }
104
mtklein36352bf2015-03-25 18:17:31 -0700105 void onDraw(SkCanvas* canvas) override {
fmalita00d5c2c2014-08-21 08:53:26 -0700106 for (unsigned b = 0; b < SK_ARRAY_COUNT(blobConfigs); ++b) {
fmalita37283c22016-09-13 10:00:23 -0700107 sk_sp<SkTextBlob> blob(this->makeBlob(b));
fmalita00d5c2c2014-08-21 08:53:26 -0700108
109 SkPaint p;
Mike Reed3185f902018-10-26 16:33:00 -0400110 p.setAntiAlias(true);
fmalita00d5c2c2014-08-21 08:53:26 -0700111 SkPoint offset = SkPoint::Make(SkIntToScalar(10 + 300 * (b % 2)),
112 SkIntToScalar(20 + 150 * (b / 2)));
113
114 canvas->drawTextBlob(blob, offset.x(), offset.y(), p);
115
116 p.setColor(SK_ColorBLUE);
117 p.setStyle(SkPaint::kStroke_Style);
118 SkRect box = blob->bounds();
119 box.offset(offset);
Mike Reed3185f902018-10-26 16:33:00 -0400120 p.setAntiAlias(false);
fmalita00d5c2c2014-08-21 08:53:26 -0700121 canvas->drawRect(box, p);
122
123 }
124 }
125
126private:
fmalita37283c22016-09-13 10:00:23 -0700127 sk_sp<SkTextBlob> makeBlob(unsigned blobIndex) {
fmalita00d5c2c2014-08-21 08:53:26 -0700128 SkTextBlobBuilder builder;
129
Mike Reed3185f902018-10-26 16:33:00 -0400130 SkFont font;
Mike Reed5f50f572018-11-12 13:19:37 -0500131 font.setSubpixel(true);
132 font.setEdging(SkFont::Edging::kAntiAlias);
fmalitaacb882c2014-09-16 17:58:34 -0700133 font.setTypeface(fTypeface);
fmalita00d5c2c2014-08-21 08:53:26 -0700134
135 for (unsigned l = 0; l < SK_ARRAY_COUNT(blobConfigs[blobIndex]); ++l) {
136 unsigned currentGlyph = 0;
137
138 for (unsigned c = 0; c < SK_ARRAY_COUNT(blobConfigs[blobIndex][l]); ++c) {
139 const BlobCfg* cfg = &blobConfigs[blobIndex][l][c];
140 unsigned count = cfg->count;
141
142 if (count > fGlyphs.count() - currentGlyph) {
143 count = fGlyphs.count() - currentGlyph;
144 }
145 if (0 == count) {
146 break;
147 }
148
Mike Reed3185f902018-10-26 16:33:00 -0400149 font.setSize(kFontSize * cfg->scale);
150 const SkScalar advanceX = font.getSize() * 0.85f;
151 const SkScalar advanceY = font.getSize() * 1.5f;
fmalita00d5c2c2014-08-21 08:53:26 -0700152
153 SkPoint offset = SkPoint::Make(currentGlyph * advanceX + c * advanceX,
154 advanceY * l);
155 switch (cfg->pos) {
156 case kDefault_Pos: {
157 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, count,
158 offset.x(),
159 offset.y());
160 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
161 } break;
162 case kScalar_Pos: {
163 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPosH(font, count,
164 offset.y());
165 SkTDArray<SkScalar> pos;
166 for (unsigned i = 0; i < count; ++i) {
167 *pos.append() = offset.x() + i * advanceX;
168 }
169
170 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
171 memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar));
172 } break;
173 case kPoint_Pos: {
174 const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPos(font, count);
175
176 SkTDArray<SkScalar> pos;
177 for (unsigned i = 0; i < count; ++i) {
178 *pos.append() = offset.x() + i * advanceX;
179 *pos.append() = offset.y() + i * (advanceY / count);
180 }
181
182 memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t));
183 memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar) * 2);
184 } break;
185 default:
djsollenf2b340f2016-01-29 08:51:04 -0800186 SK_ABORT("unhandled pos value");
fmalita00d5c2c2014-08-21 08:53:26 -0700187 }
188
189 currentGlyph += count;
190 }
191 }
192
fmalita37283c22016-09-13 10:00:23 -0700193 return builder.make();
fmalita00d5c2c2014-08-21 08:53:26 -0700194 }
195
bungeman13b9c952016-05-12 10:09:30 -0700196 SkTDArray<uint16_t> fGlyphs;
197 sk_sp<SkTypeface> fTypeface;
198 const char* fText;
fmalita00d5c2c2014-08-21 08:53:26 -0700199 typedef skiagm::GM INHERITED;
200};
201
halcanary385fe4d2015-08-26 13:07:48 -0700202DEF_GM(return new TextBlobGM("hamburgefons");)