blob: 51ab34640860decd8394a4b8ff408dfde5630110 [file] [log] [blame]
fmalitac6765d62014-08-21 15:03:04 -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 "include/core/SkPaint.h"
9#include "include/core/SkPoint.h"
10#include "include/core/SkSerialProcs.h"
11#include "include/core/SkTypeface.h"
12#include "include/private/SkTo.h"
13#include "src/core/SkTextBlobPriv.h"
fmalitac6765d62014-08-21 15:03:04 -070014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
16#include "tools/ToolUtils.h"
fmalitac6765d62014-08-21 15:03:04 -070017
fmalitac6765d62014-08-21 15:03:04 -070018class TextBlobTester {
19public:
fmalita51bf9572014-08-22 07:50:45 -070020 // This unit test feeds an SkTextBlobBuilder various runs then checks to see if
21 // the result contains the provided data and merges runs when appropriate.
22 static void TestBuilder(skiatest::Reporter* reporter) {
fmalitac6765d62014-08-21 15:03:04 -070023 SkTextBlobBuilder builder;
24
25 // empty run set
halcanary96fcdcc2015-08-27 07:41:13 -070026 RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
fmalitac6765d62014-08-21 15:03:04 -070027
fmalita51bf9572014-08-22 07:50:45 -070028 RunDef set1[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040029 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 100 },
fmalitac6765d62014-08-21 15:03:04 -070030 };
fmalita51bf9572014-08-22 07:50:45 -070031 RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1));
fmalitac6765d62014-08-21 15:03:04 -070032
fmalita51bf9572014-08-22 07:50:45 -070033 RunDef set2[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040034 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 100 },
fmalitac6765d62014-08-21 15:03:04 -070035 };
fmalita51bf9572014-08-22 07:50:45 -070036 RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2));
fmalitac6765d62014-08-21 15:03:04 -070037
fmalita51bf9572014-08-22 07:50:45 -070038 RunDef set3[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040039 { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
fmalitac6765d62014-08-21 15:03:04 -070040 };
fmalita51bf9572014-08-22 07:50:45 -070041 RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3));
fmalitac6765d62014-08-21 15:03:04 -070042
fmalita51bf9572014-08-22 07:50:45 -070043 RunDef set4[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040044 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
45 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
46 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
fmalitac6765d62014-08-21 15:03:04 -070047 };
fmalita51bf9572014-08-22 07:50:45 -070048 RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4));
fmalitac6765d62014-08-21 15:03:04 -070049
fmalita51bf9572014-08-22 07:50:45 -070050 RunDef set5[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040051 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
52 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
53 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 300, 250 },
fmalitac6765d62014-08-21 15:03:04 -070054 };
fmalita51bf9572014-08-22 07:50:45 -070055 RunDef mergedSet5[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040056 { 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
57 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 250 },
fmalitac6765d62014-08-21 15:03:04 -070058 };
fmalita51bf9572014-08-22 07:50:45 -070059 RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5,
60 SK_ARRAY_COUNT(mergedSet5));
fmalitac6765d62014-08-21 15:03:04 -070061
fmalita51bf9572014-08-22 07:50:45 -070062 RunDef set6[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040063 { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
64 { 128, SkTextBlobRunIterator::kFull_Positioning, 200, 200 },
65 { 128, SkTextBlobRunIterator::kFull_Positioning, 300, 300 },
fmalitac6765d62014-08-21 15:03:04 -070066 };
fmalita51bf9572014-08-22 07:50:45 -070067 RunDef mergedSet6[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040068 { 384, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
fmalitac6765d62014-08-21 15:03:04 -070069 };
fmalita51bf9572014-08-22 07:50:45 -070070 RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6,
71 SK_ARRAY_COUNT(mergedSet6));
fmalitac6765d62014-08-21 15:03:04 -070072
fmalita51bf9572014-08-22 07:50:45 -070073 RunDef set7[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040074 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
75 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
76 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
77 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
78 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
79 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
80 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
81 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
82 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 550 },
83 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 650 },
84 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 750 },
85 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 850 },
fmalitac6765d62014-08-21 15:03:04 -070086 };
fmalita51bf9572014-08-22 07:50:45 -070087 RunDef mergedSet7[] = {
Florin Malitaab54e732018-07-27 09:47:15 -040088 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
89 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
90 { 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
91 { 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
92 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
93 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
94 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 550 },
95 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 650 },
96 { 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
fmalitac6765d62014-08-21 15:03:04 -070097 };
fmalita51bf9572014-08-22 07:50:45 -070098 RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7,
99 SK_ARRAY_COUNT(mergedSet7));
100 }
101
102 // This unit test verifies blob bounds computation.
103 static void TestBounds(skiatest::Reporter* reporter) {
104 SkTextBlobBuilder builder;
Mike Reed2ed78202018-11-21 15:10:08 -0500105 SkFont font;
fmalita51bf9572014-08-22 07:50:45 -0700106
107 // Explicit bounds.
108 {
fmalita37283c22016-09-13 10:00:23 -0700109 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400110 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700111 }
112
113 {
114 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
115 builder.allocRun(font, 16, 0, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700116 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700117 REPORTER_ASSERT(reporter, blob->bounds() == r1);
118 }
119
120 {
121 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
122 builder.allocRunPosH(font, 16, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700123 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700124 REPORTER_ASSERT(reporter, blob->bounds() == r1);
125 }
126
127 {
128 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
129 builder.allocRunPos(font, 16, &r1);
fmalita37283c22016-09-13 10:00:23 -0700130 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700131 REPORTER_ASSERT(reporter, blob->bounds() == r1);
132 }
133
134 {
135 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
136 SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
137 SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
138
139 builder.allocRun(font, 16, 0, 0, &r1);
140 builder.allocRunPosH(font, 16, 0, &r2);
141 builder.allocRunPos(font, 16, &r3);
142
fmalita37283c22016-09-13 10:00:23 -0700143 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700144 REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
145 }
146
147 {
fmalita37283c22016-09-13 10:00:23 -0700148 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400149 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700150 }
151
152 // Implicit bounds
fmalita9ae8fe12015-10-13 08:59:23 -0700153
154 {
155 // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
156 // don't trigger asserts (http://crbug.com/542643).
Mike Reed2ed78202018-11-21 15:10:08 -0500157 font.setSize(0);
fmalita9ae8fe12015-10-13 08:59:23 -0700158
159 const char* txt = "BOOO";
fmalita0b01da72015-10-14 08:11:40 -0700160 const size_t txtLen = strlen(txt);
Ben Wagner51e15a62019-05-07 15:38:46 -0400161 const int glyphCount = font.countText(txt, txtLen, SkTextEncoding::kUTF8);
Mike Reed2ed78202018-11-21 15:10:08 -0500162 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount);
fmalita0b01da72015-10-14 08:11:40 -0700163
Ben Wagner51e15a62019-05-07 15:38:46 -0400164 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
fmalita0b01da72015-10-14 08:11:40 -0700165
166 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
fmalita37283c22016-09-13 10:00:23 -0700167 sk_sp<SkTextBlob> blob(builder.make());
fmalita9ae8fe12015-10-13 08:59:23 -0700168 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
169 }
fmalitac6765d62014-08-21 15:03:04 -0700170 }
171
fmalita32d6b872015-11-17 11:39:32 -0800172 // Verify that text-related properties are captured in run paints.
173 static void TestPaintProps(skiatest::Reporter* reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500174 SkFont font;
fmalita32d6b872015-11-17 11:39:32 -0800175 // Kitchen sink font.
Mike Reed2ed78202018-11-21 15:10:08 -0500176 font.setSize(42);
177 font.setScaleX(4.2f);
Mike Kleinea3f0142019-03-20 11:12:10 -0500178 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed2ed78202018-11-21 15:10:08 -0500179 font.setSkewX(0.42f);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400180 font.setHinting(SkFontHinting::kFull);
Mike Reed2ed78202018-11-21 15:10:08 -0500181 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
182 font.setEmbolden(true);
183 font.setLinearMetrics(true);
184 font.setSubpixel(true);
185 font.setEmbeddedBitmaps(true);
186 font.setForceAutoHinting(true);
fmalita32d6b872015-11-17 11:39:32 -0800187
188 // Ensure we didn't pick default values by mistake.
Mike Reed2ed78202018-11-21 15:10:08 -0500189 SkFont defaultFont;
190 REPORTER_ASSERT(reporter, defaultFont.getSize() != font.getSize());
191 REPORTER_ASSERT(reporter, defaultFont.getScaleX() != font.getScaleX());
Herb Derby087fad72019-01-22 14:45:16 -0500192 REPORTER_ASSERT(reporter, defaultFont.getTypefaceOrDefault() != font.getTypefaceOrDefault());
Mike Reed2ed78202018-11-21 15:10:08 -0500193 REPORTER_ASSERT(reporter, defaultFont.getSkewX() != font.getSkewX());
194 REPORTER_ASSERT(reporter, defaultFont.getHinting() != font.getHinting());
195 REPORTER_ASSERT(reporter, defaultFont.getEdging() != font.getEdging());
196 REPORTER_ASSERT(reporter, defaultFont.isEmbolden() != font.isEmbolden());
197 REPORTER_ASSERT(reporter, defaultFont.isLinearMetrics() != font.isLinearMetrics());
198 REPORTER_ASSERT(reporter, defaultFont.isSubpixel() != font.isSubpixel());
Florin Malitaab54e732018-07-27 09:47:15 -0400199 REPORTER_ASSERT(reporter,
Mike Reed2ed78202018-11-21 15:10:08 -0500200 defaultFont.isEmbeddedBitmaps() != font.isEmbeddedBitmaps());
201 REPORTER_ASSERT(reporter, defaultFont.isForceAutoHinting() != font.isForceAutoHinting());
fmalita32d6b872015-11-17 11:39:32 -0800202
203 SkTextBlobBuilder builder;
Florin Malitaab54e732018-07-27 09:47:15 -0400204 AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder);
205 AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0),
206 builder);
207 AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder);
fmalita37283c22016-09-13 10:00:23 -0700208 sk_sp<SkTextBlob> blob(builder.make());
fmalita32d6b872015-11-17 11:39:32 -0800209
fmalita37283c22016-09-13 10:00:23 -0700210 SkTextBlobRunIterator it(blob.get());
fmalita32d6b872015-11-17 11:39:32 -0800211 while (!it.done()) {
Mike Reed60a0e872019-01-04 09:41:49 -0500212 REPORTER_ASSERT(reporter, it.font() == font);
fmalita32d6b872015-11-17 11:39:32 -0800213 it.next();
214 }
215
216 }
217
fmalitac6765d62014-08-21 15:03:04 -0700218private:
219 struct RunDef {
Florin Malitaab54e732018-07-27 09:47:15 -0400220 unsigned count;
221 SkTextBlobRunIterator::GlyphPositioning pos;
222 SkScalar x, y;
fmalitac6765d62014-08-21 15:03:04 -0700223 };
224
fmalita51bf9572014-08-22 07:50:45 -0700225 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700226 const RunDef in[], unsigned inCount,
227 const RunDef out[], unsigned outCount) {
Mike Reed2ed78202018-11-21 15:10:08 -0500228 SkFont font;
fmalita51bf9572014-08-22 07:50:45 -0700229
fmalitac6765d62014-08-21 15:03:04 -0700230 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700231 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700232 }
233
fmalita37283c22016-09-13 10:00:23 -0700234 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400235 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
236 if (!blob) {
237 return;
238 }
fmalitac6765d62014-08-21 15:03:04 -0700239
fmalita37283c22016-09-13 10:00:23 -0700240 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700241 for (unsigned i = 0; i < outCount; ++i) {
242 REPORTER_ASSERT(reporter, !it.done());
243 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
244 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
Florin Malitaab54e732018-07-27 09:47:15 -0400245 if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700246 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
247 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
Florin Malitaab54e732018-07-27 09:47:15 -0400248 } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700249 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
250 }
251
252 for (unsigned k = 0; k < it.glyphCount(); ++k) {
253 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400254 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700255 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400256 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700257 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
258 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
259 }
260 }
261
262 it.next();
263 }
264
265 REPORTER_ASSERT(reporter, it.done());
266 }
267
Mike Reed2ed78202018-11-21 15:10:08 -0500268 static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700269 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700270 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700271 switch (pos) {
Florin Malitaab54e732018-07-27 09:47:15 -0400272 case SkTextBlobRunIterator::kDefault_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700273 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
274 offset.y(), bounds);
275 for (int i = 0; i < count; ++i) {
276 rb.glyphs[i] = i;
277 }
278 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400279 case SkTextBlobRunIterator::kHorizontal_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700280 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
281 bounds);
282 for (int i = 0; i < count; ++i) {
283 rb.glyphs[i] = i;
284 rb.pos[i] = SkIntToScalar(i);
285 }
286 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400287 case SkTextBlobRunIterator::kFull_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700288 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
289 for (int i = 0; i < count; ++i) {
290 rb.glyphs[i] = i;
291 rb.pos[i * 2] = SkIntToScalar(i);
292 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
293 }
294 } break;
295 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400296 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700297 }
298 }
299};
300
301DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700302 TextBlobTester::TestBuilder(reporter);
303 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700304}
fmalita32d6b872015-11-17 11:39:32 -0800305
306DEF_TEST(TextBlob_paint, reporter) {
307 TextBlobTester::TestPaintProps(reporter);
308}
halcanary4f0a23a2016-08-30 11:58:33 -0700309
310DEF_TEST(TextBlob_extended, reporter) {
311 SkTextBlobBuilder textBlobBuilder;
Mike Reed6d595682018-12-05 17:28:14 -0500312 SkFont font;
halcanary4f0a23a2016-08-30 11:58:33 -0700313 const char text1[] = "Foo";
314 const char text2[] = "Bar";
315
Ben Wagner51e15a62019-05-07 15:38:46 -0400316 int glyphCount = font.countText(text1, strlen(text1), SkTextEncoding::kUTF8);
halcanary4f0a23a2016-08-30 11:58:33 -0700317 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
Ben Wagner51e15a62019-05-07 15:38:46 -0400318 (void)font.textToGlyphs(text1, strlen(text1), SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
halcanary4f0a23a2016-08-30 11:58:33 -0700319
Ben Wagner5d9c20e2021-02-24 11:43:07 -0500320 auto run = textBlobBuilder.allocRunText(font, glyphCount, 0, 0, SkToInt(strlen(text2)));
halcanary4f0a23a2016-08-30 11:58:33 -0700321 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
322 memcpy(run.utf8text, text2, strlen(text2));
323 for (int i = 0; i < glyphCount; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500324 run.clusters[i] = std::min(SkToU32(i), SkToU32(strlen(text2)));
halcanary4f0a23a2016-08-30 11:58:33 -0700325 }
fmalita37283c22016-09-13 10:00:23 -0700326 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700327 REPORTER_ASSERT(reporter, blob);
328
329 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
330 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
331 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
332 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
333 }
Florin Malitaab54e732018-07-27 09:47:15 -0400334 REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
halcanary4f0a23a2016-08-30 11:58:33 -0700335 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
336 REPORTER_ASSERT(reporter, it.textSize() > 0);
337 REPORTER_ASSERT(reporter, it.clusters());
338 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
339 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
340 }
341 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
342 }
343}
Mike Reedb99bedd2017-07-11 10:27:40 -0400344
345///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500346#include "include/core/SkCanvas.h"
347#include "include/core/SkSurface.h"
Ben Wagner96fdfe02019-07-09 16:39:29 -0400348#include "include/private/SkTArray.h"
Mike Reedb99bedd2017-07-11 10:27:40 -0400349
350static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
351 sk_sp<SkTypeface> tf) {
Mike Reed70914f52018-11-23 13:08:33 -0500352 SkFont font;
353 font.setEdging(SkFont::Edging::kAntiAlias);
354 font.setSubpixel(true);
355 font.setSize(16);
356 font.setTypeface(tf);
Mike Reedb99bedd2017-07-11 10:27:40 -0400357
Ben Wagner51e15a62019-05-07 15:38:46 -0400358 int glyphCount = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reedb99bedd2017-07-11 10:27:40 -0400359
Mike Reed70914f52018-11-23 13:08:33 -0500360 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y);
Mike Reedb99bedd2017-07-11 10:27:40 -0400361
Ben Wagner51e15a62019-05-07 15:38:46 -0400362 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
Mike Reedb99bedd2017-07-11 10:27:40 -0400363}
364
365static sk_sp<SkImage> render(const SkTextBlob* blob) {
366 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
367 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400368 if (!surf) {
369 return nullptr; // bounds are empty?
370 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400371 surf->getCanvas()->clear(SK_ColorWHITE);
372 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
373 return surf->makeImageSnapshot();
374}
375
Cary Clark785586a2018-07-19 10:07:01 -0400376static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400377 auto array = (SkTArray<sk_sp<SkTypeface>>*)ctx;
378 const size_t idx = array->size();
379 array->emplace_back(sk_ref_sp(tf));
380 // In this test, we are deserializing on the same machine, so we don't worry about endianness.
381 return SkData::MakeWithCopy(&idx, sizeof(idx));
Cary Clark785586a2018-07-19 10:07:01 -0400382}
383
384static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400385 auto array = (SkTArray<sk_sp<SkTypeface>>*)ctx;
386 if (length != sizeof(size_t)) {
387 SkASSERT(false);
388 return nullptr;
Cary Clark785586a2018-07-19 10:07:01 -0400389 }
Ben Wagner96fdfe02019-07-09 16:39:29 -0400390 size_t idx = *reinterpret_cast<const size_t*>(data);
391 if (idx >= array->size()) {
392 SkASSERT(false);
393 return nullptr;
394 }
395 return (*array)[idx];
Cary Clark785586a2018-07-19 10:07:01 -0400396}
397
Mike Reedb99bedd2017-07-11 10:27:40 -0400398/*
399 * Build a blob with more than one typeface.
400 * Draw it into an offscreen,
401 * then serialize and deserialize,
402 * Then draw the new instance and assert it draws the same as the original.
403 */
404DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500405 sk_sp<SkTextBlob> blob0 = []() {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400406 sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
Mike Reedb99bedd2017-07-11 10:27:40 -0400407
Mike Reed4bcef3c2017-12-08 11:46:09 -0500408 SkTextBlobBuilder builder;
Mike Reed0951cc22018-12-21 16:19:24 -0500409 add_run(&builder, "Hello", 10, 20, nullptr); // don't flatten a typeface
410 add_run(&builder, "World", 10, 40, tf); // do flatten this typeface
Mike Reed4bcef3c2017-12-08 11:46:09 -0500411 return builder.make();
412 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400413
Ben Wagner96fdfe02019-07-09 16:39:29 -0400414 SkTArray<sk_sp<SkTypeface>> array;
Cary Clark785586a2018-07-19 10:07:01 -0400415 SkSerialProcs serializeProcs;
416 serializeProcs.fTypefaceProc = &SerializeTypeface;
417 serializeProcs.fTypefaceCtx = (void*) &array;
418 sk_sp<SkData> data = blob0->serialize(serializeProcs);
Mike Reed0951cc22018-12-21 16:19:24 -0500419 REPORTER_ASSERT(reporter, array.count() == 1);
Cary Clark785586a2018-07-19 10:07:01 -0400420 SkDeserialProcs deserializeProcs;
421 deserializeProcs.fTypefaceProc = &DeserializeTypeface;
422 deserializeProcs.fTypefaceCtx = (void*) &array;
423 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
Mike Reed7e49d532017-07-11 15:07:08 -0400424
425 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400426 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400427 if (img0 && img1) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500428 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400429 }
430}
Herb Derby4b3a5152018-07-17 16:10:30 -0400431
432DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
Herb Derby4b3a5152018-07-17 16:10:30 -0400433 const char text[] = "Hello";
Ben Wagner51e15a62019-05-07 15:38:46 -0400434 auto blob = SkTextBlob::MakeFromString(text, SkFont(), SkTextEncoding::kUTF8);
Herb Derby4b3a5152018-07-17 16:10:30 -0400435
436 int runs = 0;
437 for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
438 REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
Florin Malitaab54e732018-07-27 09:47:15 -0400439 REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
Herb Derby4b3a5152018-07-17 16:10:30 -0400440 runs += 1;
441 }
442 REPORTER_ASSERT(reporter, runs == 1);
443
444}
Mike Reeda32654c2019-10-04 10:18:34 -0400445
446DEF_TEST(TextBlob_iter, reporter) {
447 sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
448
449 SkTextBlobBuilder builder;
450 add_run(&builder, "Hello", 10, 20, nullptr);
451 add_run(&builder, "World", 10, 40, tf);
452 auto blob = builder.make();
453
454 SkTextBlob::Iter::Run expected[] = {
455 { nullptr, 5, nullptr },
456 { tf.get(), 5, nullptr },
457 };
458
459 SkTextBlob::Iter iter(*blob);
460 SkTextBlob::Iter::Run run;
461 for (auto exp : expected) {
462 REPORTER_ASSERT(reporter, iter.next(&run));
463 REPORTER_ASSERT(reporter, run.fTypeface == exp.fTypeface);
464 REPORTER_ASSERT(reporter, run.fGlyphCount == exp.fGlyphCount);
465 for (int i = 0; i < run.fGlyphCount; ++i) {
466 REPORTER_ASSERT(reporter, run.fGlyphIndices[i] != 0);
467 }
468 }
469 REPORTER_ASSERT(reporter, !iter.next(&run)); // we're done
470
471 SkTextBlob::Iter iter2(*blob);
472 REPORTER_ASSERT(reporter, iter2.next(&run));
473 // Hello should have the same glyph repeated for the 'l'
474 REPORTER_ASSERT(reporter, run.fGlyphIndices[2] == run.fGlyphIndices[3]);
475}
Lee Salzman634d1502019-10-15 13:53:09 -0400476
477DEF_TEST(TextBlob_getIntercepts, reporter) {
478 SkFont font;
479 font.setSize(16);
480
481 SkPoint lowPos[1] = { SkPoint::Make(0, 5) };
482 SkPoint highPos[1] = { SkPoint::Make(0, -8) };
483 SkPoint zeroPos[1] = { SkPoint::Make(0, 0) };
484
485 // 'x' sitting on baseline
486 auto blobZeroX = SkTextBlob::MakeFromPosText("x", 1, zeroPos, font);
487 // 'x' lowered to intersect baseline
488 auto blobLowX = SkTextBlob::MakeFromPosText("x", 1, lowPos, font);
489 // 'y' sitting on baseline
490 auto blobZeroY = SkTextBlob::MakeFromPosText("y", 1, zeroPos, font);
491 // 'y' raised to not intersect baseline
492 auto blobHighY = SkTextBlob::MakeFromPosText("y", 1, highPos, font);
493
494 // bounds right below baseline
495 SkScalar bounds[2] = { 1, 2 };
496
497 // 'x' on baseline should not intersect
498 REPORTER_ASSERT(reporter, blobZeroX->getIntercepts(bounds, nullptr) == 0);
499 // lowered 'x' should intersect
500 REPORTER_ASSERT(reporter, blobLowX->getIntercepts(bounds, nullptr) == 2);
501 // 'y' on baseline should intersect
502 REPORTER_ASSERT(reporter, blobZeroY->getIntercepts(bounds, nullptr) == 2);
503 // raised 'y' should not intersect
504 REPORTER_ASSERT(reporter, blobHighY->getIntercepts(bounds, nullptr) == 0);
505}