blob: acb8dc254bff8a55b50f0fd0eb851d185386e1bf [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 SkFont font;
158 font.setSize(0);
fmalita9ae8fe12015-10-13 08:59:23 -0700159
160 const char* txt = "BOOO";
fmalita0b01da72015-10-14 08:11:40 -0700161 const size_t txtLen = strlen(txt);
Ben Wagner51e15a62019-05-07 15:38:46 -0400162 const int glyphCount = font.countText(txt, txtLen, SkTextEncoding::kUTF8);
Mike Reed2ed78202018-11-21 15:10:08 -0500163 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount);
fmalita0b01da72015-10-14 08:11:40 -0700164
Ben Wagner51e15a62019-05-07 15:38:46 -0400165 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
fmalita0b01da72015-10-14 08:11:40 -0700166
167 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
fmalita37283c22016-09-13 10:00:23 -0700168 sk_sp<SkTextBlob> blob(builder.make());
fmalita9ae8fe12015-10-13 08:59:23 -0700169 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
170 }
fmalitac6765d62014-08-21 15:03:04 -0700171 }
172
fmalita32d6b872015-11-17 11:39:32 -0800173 // Verify that text-related properties are captured in run paints.
174 static void TestPaintProps(skiatest::Reporter* reporter) {
Mike Reed2ed78202018-11-21 15:10:08 -0500175 SkFont font;
fmalita32d6b872015-11-17 11:39:32 -0800176 // Kitchen sink font.
Mike Reed2ed78202018-11-21 15:10:08 -0500177 font.setSize(42);
178 font.setScaleX(4.2f);
Mike Kleinea3f0142019-03-20 11:12:10 -0500179 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed2ed78202018-11-21 15:10:08 -0500180 font.setSkewX(0.42f);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400181 font.setHinting(SkFontHinting::kFull);
Mike Reed2ed78202018-11-21 15:10:08 -0500182 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
183 font.setEmbolden(true);
184 font.setLinearMetrics(true);
185 font.setSubpixel(true);
186 font.setEmbeddedBitmaps(true);
187 font.setForceAutoHinting(true);
fmalita32d6b872015-11-17 11:39:32 -0800188
189 // Ensure we didn't pick default values by mistake.
Mike Reed2ed78202018-11-21 15:10:08 -0500190 SkFont defaultFont;
191 REPORTER_ASSERT(reporter, defaultFont.getSize() != font.getSize());
192 REPORTER_ASSERT(reporter, defaultFont.getScaleX() != font.getScaleX());
Herb Derby087fad72019-01-22 14:45:16 -0500193 REPORTER_ASSERT(reporter, defaultFont.getTypefaceOrDefault() != font.getTypefaceOrDefault());
Mike Reed2ed78202018-11-21 15:10:08 -0500194 REPORTER_ASSERT(reporter, defaultFont.getSkewX() != font.getSkewX());
195 REPORTER_ASSERT(reporter, defaultFont.getHinting() != font.getHinting());
196 REPORTER_ASSERT(reporter, defaultFont.getEdging() != font.getEdging());
197 REPORTER_ASSERT(reporter, defaultFont.isEmbolden() != font.isEmbolden());
198 REPORTER_ASSERT(reporter, defaultFont.isLinearMetrics() != font.isLinearMetrics());
199 REPORTER_ASSERT(reporter, defaultFont.isSubpixel() != font.isSubpixel());
Florin Malitaab54e732018-07-27 09:47:15 -0400200 REPORTER_ASSERT(reporter,
Mike Reed2ed78202018-11-21 15:10:08 -0500201 defaultFont.isEmbeddedBitmaps() != font.isEmbeddedBitmaps());
202 REPORTER_ASSERT(reporter, defaultFont.isForceAutoHinting() != font.isForceAutoHinting());
fmalita32d6b872015-11-17 11:39:32 -0800203
204 SkTextBlobBuilder builder;
Florin Malitaab54e732018-07-27 09:47:15 -0400205 AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder);
206 AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0),
207 builder);
208 AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder);
fmalita37283c22016-09-13 10:00:23 -0700209 sk_sp<SkTextBlob> blob(builder.make());
fmalita32d6b872015-11-17 11:39:32 -0800210
fmalita37283c22016-09-13 10:00:23 -0700211 SkTextBlobRunIterator it(blob.get());
fmalita32d6b872015-11-17 11:39:32 -0800212 while (!it.done()) {
Mike Reed60a0e872019-01-04 09:41:49 -0500213 REPORTER_ASSERT(reporter, it.font() == font);
fmalita32d6b872015-11-17 11:39:32 -0800214 it.next();
215 }
216
217 }
218
fmalitac6765d62014-08-21 15:03:04 -0700219private:
220 struct RunDef {
Florin Malitaab54e732018-07-27 09:47:15 -0400221 unsigned count;
222 SkTextBlobRunIterator::GlyphPositioning pos;
223 SkScalar x, y;
fmalitac6765d62014-08-21 15:03:04 -0700224 };
225
fmalita51bf9572014-08-22 07:50:45 -0700226 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700227 const RunDef in[], unsigned inCount,
228 const RunDef out[], unsigned outCount) {
Mike Reed2ed78202018-11-21 15:10:08 -0500229 SkFont font;
fmalita51bf9572014-08-22 07:50:45 -0700230
fmalitac6765d62014-08-21 15:03:04 -0700231 unsigned glyphCount = 0;
232 unsigned posCount = 0;
233
234 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700235 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700236 glyphCount += in[i].count;
237 posCount += in[i].count * in[i].pos;
238 }
239
fmalita37283c22016-09-13 10:00:23 -0700240 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400241 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
242 if (!blob) {
243 return;
244 }
fmalitac6765d62014-08-21 15:03:04 -0700245
fmalita37283c22016-09-13 10:00:23 -0700246 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700247 for (unsigned i = 0; i < outCount; ++i) {
248 REPORTER_ASSERT(reporter, !it.done());
249 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
250 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
Florin Malitaab54e732018-07-27 09:47:15 -0400251 if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700252 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
253 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
Florin Malitaab54e732018-07-27 09:47:15 -0400254 } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700255 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
256 }
257
258 for (unsigned k = 0; k < it.glyphCount(); ++k) {
259 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400260 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700261 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400262 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700263 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
264 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
265 }
266 }
267
268 it.next();
269 }
270
271 REPORTER_ASSERT(reporter, it.done());
272 }
273
Mike Reed2ed78202018-11-21 15:10:08 -0500274 static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700275 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700276 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700277 switch (pos) {
Florin Malitaab54e732018-07-27 09:47:15 -0400278 case SkTextBlobRunIterator::kDefault_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700279 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
280 offset.y(), bounds);
281 for (int i = 0; i < count; ++i) {
282 rb.glyphs[i] = i;
283 }
284 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400285 case SkTextBlobRunIterator::kHorizontal_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700286 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
287 bounds);
288 for (int i = 0; i < count; ++i) {
289 rb.glyphs[i] = i;
290 rb.pos[i] = SkIntToScalar(i);
291 }
292 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400293 case SkTextBlobRunIterator::kFull_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700294 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
295 for (int i = 0; i < count; ++i) {
296 rb.glyphs[i] = i;
297 rb.pos[i * 2] = SkIntToScalar(i);
298 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
299 }
300 } break;
301 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400302 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700303 }
304 }
305};
306
307DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700308 TextBlobTester::TestBuilder(reporter);
309 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700310}
fmalita32d6b872015-11-17 11:39:32 -0800311
312DEF_TEST(TextBlob_paint, reporter) {
313 TextBlobTester::TestPaintProps(reporter);
314}
halcanary4f0a23a2016-08-30 11:58:33 -0700315
316DEF_TEST(TextBlob_extended, reporter) {
317 SkTextBlobBuilder textBlobBuilder;
Mike Reed6d595682018-12-05 17:28:14 -0500318 SkFont font;
halcanary4f0a23a2016-08-30 11:58:33 -0700319 const char text1[] = "Foo";
320 const char text2[] = "Bar";
321
Ben Wagner51e15a62019-05-07 15:38:46 -0400322 int glyphCount = font.countText(text1, strlen(text1), SkTextEncoding::kUTF8);
halcanary4f0a23a2016-08-30 11:58:33 -0700323 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
Ben Wagner51e15a62019-05-07 15:38:46 -0400324 (void)font.textToGlyphs(text1, strlen(text1), SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
halcanary4f0a23a2016-08-30 11:58:33 -0700325
Cary Clarke12a0902018-08-09 10:07:33 -0400326 auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
Mike Reed6d595682018-12-05 17:28:14 -0500327 font, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
halcanary4f0a23a2016-08-30 11:58:33 -0700328 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
329 memcpy(run.utf8text, text2, strlen(text2));
330 for (int i = 0; i < glyphCount; ++i) {
331 run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
332 }
fmalita37283c22016-09-13 10:00:23 -0700333 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700334 REPORTER_ASSERT(reporter, blob);
335
336 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
337 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
338 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
339 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
340 }
Florin Malitaab54e732018-07-27 09:47:15 -0400341 REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
halcanary4f0a23a2016-08-30 11:58:33 -0700342 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
343 REPORTER_ASSERT(reporter, it.textSize() > 0);
344 REPORTER_ASSERT(reporter, it.clusters());
345 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
346 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
347 }
348 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
349 }
350}
Mike Reedb99bedd2017-07-11 10:27:40 -0400351
352///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500353#include "include/core/SkCanvas.h"
354#include "include/core/SkSurface.h"
Ben Wagner96fdfe02019-07-09 16:39:29 -0400355#include "include/private/SkTArray.h"
Mike Reedb99bedd2017-07-11 10:27:40 -0400356
357static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
358 sk_sp<SkTypeface> tf) {
Mike Reed70914f52018-11-23 13:08:33 -0500359 SkFont font;
360 font.setEdging(SkFont::Edging::kAntiAlias);
361 font.setSubpixel(true);
362 font.setSize(16);
363 font.setTypeface(tf);
Mike Reedb99bedd2017-07-11 10:27:40 -0400364
Ben Wagner51e15a62019-05-07 15:38:46 -0400365 int glyphCount = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reedb99bedd2017-07-11 10:27:40 -0400366
Mike Reed70914f52018-11-23 13:08:33 -0500367 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y);
Mike Reedb99bedd2017-07-11 10:27:40 -0400368
Ben Wagner51e15a62019-05-07 15:38:46 -0400369 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
Mike Reedb99bedd2017-07-11 10:27:40 -0400370}
371
372static sk_sp<SkImage> render(const SkTextBlob* blob) {
373 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
374 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400375 if (!surf) {
376 return nullptr; // bounds are empty?
377 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400378 surf->getCanvas()->clear(SK_ColorWHITE);
379 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
380 return surf->makeImageSnapshot();
381}
382
Cary Clark785586a2018-07-19 10:07:01 -0400383static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400384 auto array = (SkTArray<sk_sp<SkTypeface>>*)ctx;
385 const size_t idx = array->size();
386 array->emplace_back(sk_ref_sp(tf));
387 // In this test, we are deserializing on the same machine, so we don't worry about endianness.
388 return SkData::MakeWithCopy(&idx, sizeof(idx));
Cary Clark785586a2018-07-19 10:07:01 -0400389}
390
391static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400392 auto array = (SkTArray<sk_sp<SkTypeface>>*)ctx;
393 if (length != sizeof(size_t)) {
394 SkASSERT(false);
395 return nullptr;
Cary Clark785586a2018-07-19 10:07:01 -0400396 }
Ben Wagner96fdfe02019-07-09 16:39:29 -0400397 size_t idx = *reinterpret_cast<const size_t*>(data);
398 if (idx >= array->size()) {
399 SkASSERT(false);
400 return nullptr;
401 }
402 return (*array)[idx];
Cary Clark785586a2018-07-19 10:07:01 -0400403}
404
Mike Reedb99bedd2017-07-11 10:27:40 -0400405/*
406 * Build a blob with more than one typeface.
407 * Draw it into an offscreen,
408 * then serialize and deserialize,
409 * Then draw the new instance and assert it draws the same as the original.
410 */
411DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500412 sk_sp<SkTextBlob> blob0 = []() {
Ben Wagner96fdfe02019-07-09 16:39:29 -0400413 sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
Mike Reedb99bedd2017-07-11 10:27:40 -0400414
Mike Reed4bcef3c2017-12-08 11:46:09 -0500415 SkTextBlobBuilder builder;
Mike Reed0951cc22018-12-21 16:19:24 -0500416 add_run(&builder, "Hello", 10, 20, nullptr); // don't flatten a typeface
417 add_run(&builder, "World", 10, 40, tf); // do flatten this typeface
Mike Reed4bcef3c2017-12-08 11:46:09 -0500418 return builder.make();
419 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400420
Ben Wagner96fdfe02019-07-09 16:39:29 -0400421 SkTArray<sk_sp<SkTypeface>> array;
Cary Clark785586a2018-07-19 10:07:01 -0400422 SkSerialProcs serializeProcs;
423 serializeProcs.fTypefaceProc = &SerializeTypeface;
424 serializeProcs.fTypefaceCtx = (void*) &array;
425 sk_sp<SkData> data = blob0->serialize(serializeProcs);
Mike Reed0951cc22018-12-21 16:19:24 -0500426 REPORTER_ASSERT(reporter, array.count() == 1);
Cary Clark785586a2018-07-19 10:07:01 -0400427 SkDeserialProcs deserializeProcs;
428 deserializeProcs.fTypefaceProc = &DeserializeTypeface;
429 deserializeProcs.fTypefaceCtx = (void*) &array;
430 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
Mike Reed7e49d532017-07-11 15:07:08 -0400431
432 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400433 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400434 if (img0 && img1) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500435 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400436 }
437}
Herb Derby4b3a5152018-07-17 16:10:30 -0400438
439DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
Herb Derby4b3a5152018-07-17 16:10:30 -0400440 const char text[] = "Hello";
Ben Wagner51e15a62019-05-07 15:38:46 -0400441 auto blob = SkTextBlob::MakeFromString(text, SkFont(), SkTextEncoding::kUTF8);
Herb Derby4b3a5152018-07-17 16:10:30 -0400442
443 int runs = 0;
444 for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
445 REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
Florin Malitaab54e732018-07-27 09:47:15 -0400446 REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
Herb Derby4b3a5152018-07-17 16:10:30 -0400447 runs += 1;
448 }
449 REPORTER_ASSERT(reporter, runs == 1);
450
451}
Mike Reeda32654c2019-10-04 10:18:34 -0400452
453DEF_TEST(TextBlob_iter, reporter) {
454 sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
455
456 SkTextBlobBuilder builder;
457 add_run(&builder, "Hello", 10, 20, nullptr);
458 add_run(&builder, "World", 10, 40, tf);
459 auto blob = builder.make();
460
461 SkTextBlob::Iter::Run expected[] = {
462 { nullptr, 5, nullptr },
463 { tf.get(), 5, nullptr },
464 };
465
466 SkTextBlob::Iter iter(*blob);
467 SkTextBlob::Iter::Run run;
468 for (auto exp : expected) {
469 REPORTER_ASSERT(reporter, iter.next(&run));
470 REPORTER_ASSERT(reporter, run.fTypeface == exp.fTypeface);
471 REPORTER_ASSERT(reporter, run.fGlyphCount == exp.fGlyphCount);
472 for (int i = 0; i < run.fGlyphCount; ++i) {
473 REPORTER_ASSERT(reporter, run.fGlyphIndices[i] != 0);
474 }
475 }
476 REPORTER_ASSERT(reporter, !iter.next(&run)); // we're done
477
478 SkTextBlob::Iter iter2(*blob);
479 REPORTER_ASSERT(reporter, iter2.next(&run));
480 // Hello should have the same glyph repeated for the 'l'
481 REPORTER_ASSERT(reporter, run.fGlyphIndices[2] == run.fGlyphIndices[3]);
482}
Lee Salzman634d1502019-10-15 13:53:09 -0400483
484DEF_TEST(TextBlob_getIntercepts, reporter) {
485 SkFont font;
486 font.setSize(16);
487
488 SkPoint lowPos[1] = { SkPoint::Make(0, 5) };
489 SkPoint highPos[1] = { SkPoint::Make(0, -8) };
490 SkPoint zeroPos[1] = { SkPoint::Make(0, 0) };
491
492 // 'x' sitting on baseline
493 auto blobZeroX = SkTextBlob::MakeFromPosText("x", 1, zeroPos, font);
494 // 'x' lowered to intersect baseline
495 auto blobLowX = SkTextBlob::MakeFromPosText("x", 1, lowPos, font);
496 // 'y' sitting on baseline
497 auto blobZeroY = SkTextBlob::MakeFromPosText("y", 1, zeroPos, font);
498 // 'y' raised to not intersect baseline
499 auto blobHighY = SkTextBlob::MakeFromPosText("y", 1, highPos, font);
500
501 // bounds right below baseline
502 SkScalar bounds[2] = { 1, 2 };
503
504 // 'x' on baseline should not intersect
505 REPORTER_ASSERT(reporter, blobZeroX->getIntercepts(bounds, nullptr) == 0);
506 // lowered 'x' should intersect
507 REPORTER_ASSERT(reporter, blobLowX->getIntercepts(bounds, nullptr) == 2);
508 // 'y' on baseline should intersect
509 REPORTER_ASSERT(reporter, blobZeroY->getIntercepts(bounds, nullptr) == 2);
510 // raised 'y' should not intersect
511 REPORTER_ASSERT(reporter, blobHighY->getIntercepts(bounds, nullptr) == 0);
512}