blob: c94955838cbc7cad369e7509f1d409a46d805e03 [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
8#include "SkPaint.h"
9#include "SkPoint.h"
halcanary33779752015-10-27 14:01:05 -070010#include "SkTextBlobRunIterator.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkTo.h"
fmalita32d6b872015-11-17 11:39:32 -080012#include "SkTypeface.h"
fmalitac6765d62014-08-21 15:03:04 -070013
14#include "Test.h"
Mike Reed4bcef3c2017-12-08 11:46:09 -050015#include "sk_tool_utils.h"
fmalitac6765d62014-08-21 15:03:04 -070016
fmalitac6765d62014-08-21 15:03:04 -070017class TextBlobTester {
18public:
fmalita51bf9572014-08-22 07:50:45 -070019 // This unit test feeds an SkTextBlobBuilder various runs then checks to see if
20 // the result contains the provided data and merges runs when appropriate.
21 static void TestBuilder(skiatest::Reporter* reporter) {
fmalitac6765d62014-08-21 15:03:04 -070022 SkTextBlobBuilder builder;
23
24 // empty run set
halcanary96fcdcc2015-08-27 07:41:13 -070025 RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
fmalitac6765d62014-08-21 15:03:04 -070026
fmalita51bf9572014-08-22 07:50:45 -070027 RunDef set1[] = {
fmalitac6765d62014-08-21 15:03:04 -070028 { 128, SkTextBlob::kDefault_Positioning, 100, 100 },
29 };
fmalita51bf9572014-08-22 07:50:45 -070030 RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1));
fmalitac6765d62014-08-21 15:03:04 -070031
fmalita51bf9572014-08-22 07:50:45 -070032 RunDef set2[] = {
fmalitac6765d62014-08-21 15:03:04 -070033 { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 },
34 };
fmalita51bf9572014-08-22 07:50:45 -070035 RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2));
fmalitac6765d62014-08-21 15:03:04 -070036
fmalita51bf9572014-08-22 07:50:45 -070037 RunDef set3[] = {
fmalitac6765d62014-08-21 15:03:04 -070038 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
39 };
fmalita51bf9572014-08-22 07:50:45 -070040 RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3));
fmalitac6765d62014-08-21 15:03:04 -070041
fmalita51bf9572014-08-22 07:50:45 -070042 RunDef set4[] = {
fmalitac6765d62014-08-21 15:03:04 -070043 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
44 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
45 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
46 };
fmalita51bf9572014-08-22 07:50:45 -070047 RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4));
fmalitac6765d62014-08-21 15:03:04 -070048
fmalita51bf9572014-08-22 07:50:45 -070049 RunDef set5[] = {
fmalitac6765d62014-08-21 15:03:04 -070050 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
51 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
52 { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 },
53 };
fmalita51bf9572014-08-22 07:50:45 -070054 RunDef mergedSet5[] = {
fmalitac6765d62014-08-21 15:03:04 -070055 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
56 { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 },
57 };
fmalita51bf9572014-08-22 07:50:45 -070058 RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5,
59 SK_ARRAY_COUNT(mergedSet5));
fmalitac6765d62014-08-21 15:03:04 -070060
fmalita51bf9572014-08-22 07:50:45 -070061 RunDef set6[] = {
fmalitac6765d62014-08-21 15:03:04 -070062 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
63 { 128, SkTextBlob::kFull_Positioning, 200, 200 },
64 { 128, SkTextBlob::kFull_Positioning, 300, 300 },
65 };
fmalita51bf9572014-08-22 07:50:45 -070066 RunDef mergedSet6[] = {
fmalitac6765d62014-08-21 15:03:04 -070067 { 384, SkTextBlob::kFull_Positioning, 0, 0 },
68 };
fmalita51bf9572014-08-22 07:50:45 -070069 RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6,
70 SK_ARRAY_COUNT(mergedSet6));
fmalitac6765d62014-08-21 15:03:04 -070071
fmalita51bf9572014-08-22 07:50:45 -070072 RunDef set7[] = {
fmalitac6765d62014-08-21 15:03:04 -070073 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
74 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
75 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
76 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
77 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
78 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
79 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
80 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
81 { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 },
82 { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 },
83 { 128, SkTextBlob::kFull_Positioning, 400, 750 },
84 { 128, SkTextBlob::kFull_Positioning, 400, 850 },
85 };
fmalita51bf9572014-08-22 07:50:45 -070086 RunDef mergedSet7[] = {
fmalitac6765d62014-08-21 15:03:04 -070087 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
88 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
89 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
90 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
91 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
92 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
93 { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 },
94 { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 },
95 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
96 };
fmalita51bf9572014-08-22 07:50:45 -070097 RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7,
98 SK_ARRAY_COUNT(mergedSet7));
99 }
100
101 // This unit test verifies blob bounds computation.
102 static void TestBounds(skiatest::Reporter* reporter) {
103 SkTextBlobBuilder builder;
104 SkPaint font;
105 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
106
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).
157 SkPaint p;
158 p.setTextSize(0);
fmalita0b01da72015-10-14 08:11:40 -0700159 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
fmalita9ae8fe12015-10-13 08:59:23 -0700160
161 const char* txt = "BOOO";
fmalita0b01da72015-10-14 08:11:40 -0700162 const size_t txtLen = strlen(txt);
163 const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr);
164
165 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
166 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount);
167
168 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
169 p.textToGlyphs(txt, txtLen, buffer.glyphs);
170
171 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
fmalita37283c22016-09-13 10:00:23 -0700172 sk_sp<SkTextBlob> blob(builder.make());
fmalita9ae8fe12015-10-13 08:59:23 -0700173 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
174 }
fmalitac6765d62014-08-21 15:03:04 -0700175 }
176
fmalita32d6b872015-11-17 11:39:32 -0800177 // Verify that text-related properties are captured in run paints.
178 static void TestPaintProps(skiatest::Reporter* reporter) {
179 SkPaint font;
180 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
181
fmalita32d6b872015-11-17 11:39:32 -0800182 // Kitchen sink font.
183 font.setTextSize(42);
184 font.setTextScaleX(4.2f);
bungeman13b9c952016-05-12 10:09:30 -0700185 font.setTypeface(SkTypeface::MakeDefault());
fmalita32d6b872015-11-17 11:39:32 -0800186 font.setTextSkewX(0.42f);
187 font.setTextAlign(SkPaint::kCenter_Align);
188 font.setHinting(SkPaint::kFull_Hinting);
189 font.setAntiAlias(true);
fmalita32d6b872015-11-17 11:39:32 -0800190 font.setFakeBoldText(true);
191 font.setLinearText(true);
192 font.setSubpixelText(true);
193 font.setDevKernText(true);
194 font.setLCDRenderText(true);
195 font.setEmbeddedBitmapText(true);
196 font.setAutohinted(true);
197 font.setVerticalText(true);
fmalita32d6b872015-11-17 11:39:32 -0800198
199 // Ensure we didn't pick default values by mistake.
200 SkPaint defaultPaint;
201 REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize());
202 REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX());
203 REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface());
204 REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX());
205 REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign());
206 REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting());
207 REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800208 REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText());
209 REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText());
210 REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800211 REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText());
212 REPORTER_ASSERT(reporter, defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText());
213 REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted());
214 REPORTER_ASSERT(reporter, defaultPaint.isVerticalText() != font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800215
216 SkTextBlobBuilder builder;
217 AddRun(font, 1, SkTextBlob::kDefault_Positioning, SkPoint::Make(0, 0), builder);
218 AddRun(font, 1, SkTextBlob::kHorizontal_Positioning, SkPoint::Make(0, 0), builder);
219 AddRun(font, 1, SkTextBlob::kFull_Positioning, SkPoint::Make(0, 0), builder);
fmalita37283c22016-09-13 10:00:23 -0700220 sk_sp<SkTextBlob> blob(builder.make());
fmalita32d6b872015-11-17 11:39:32 -0800221
fmalita37283c22016-09-13 10:00:23 -0700222 SkTextBlobRunIterator it(blob.get());
fmalita32d6b872015-11-17 11:39:32 -0800223 while (!it.done()) {
224 SkPaint paint;
225 it.applyFontToPaint(&paint);
226
227 REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize());
228 REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX());
229 REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface());
230 REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX());
231 REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign());
232 REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting());
233 REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800234 REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText());
235 REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText());
236 REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800237 REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText());
238 REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText());
239 REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted());
240 REPORTER_ASSERT(reporter, paint.isVerticalText() == font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800241
242 it.next();
243 }
244
245 }
246
fmalitac6765d62014-08-21 15:03:04 -0700247private:
248 struct RunDef {
249 unsigned count;
250 SkTextBlob::GlyphPositioning pos;
251 SkScalar x, y;
252 };
253
fmalita51bf9572014-08-22 07:50:45 -0700254 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700255 const RunDef in[], unsigned inCount,
256 const RunDef out[], unsigned outCount) {
fmalita51bf9572014-08-22 07:50:45 -0700257 SkPaint font;
258 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
259
fmalitac6765d62014-08-21 15:03:04 -0700260 unsigned glyphCount = 0;
261 unsigned posCount = 0;
262
263 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700264 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700265 glyphCount += in[i].count;
266 posCount += in[i].count * in[i].pos;
267 }
268
fmalita37283c22016-09-13 10:00:23 -0700269 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400270 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
271 if (!blob) {
272 return;
273 }
fmalitac6765d62014-08-21 15:03:04 -0700274
fmalita37283c22016-09-13 10:00:23 -0700275 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700276 for (unsigned i = 0; i < outCount; ++i) {
277 REPORTER_ASSERT(reporter, !it.done());
278 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
279 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
280 if (SkTextBlob::kDefault_Positioning == out[i].pos) {
281 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
282 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
283 } else if (SkTextBlob::kHorizontal_Positioning == out[i].pos) {
284 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
285 }
286
287 for (unsigned k = 0; k < it.glyphCount(); ++k) {
288 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
289 if (SkTextBlob::kHorizontal_Positioning == it.positioning()) {
290 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
291 } else if (SkTextBlob::kFull_Positioning == it.positioning()) {
292 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
293 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
294 }
295 }
296
297 it.next();
298 }
299
300 REPORTER_ASSERT(reporter, it.done());
301 }
302
fmalita51bf9572014-08-22 07:50:45 -0700303 static void AddRun(const SkPaint& font, int count, SkTextBlob::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700304 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700305 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700306 switch (pos) {
307 case SkTextBlob::kDefault_Positioning: {
308 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
309 offset.y(), bounds);
310 for (int i = 0; i < count; ++i) {
311 rb.glyphs[i] = i;
312 }
313 } break;
314 case SkTextBlob::kHorizontal_Positioning: {
315 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
316 bounds);
317 for (int i = 0; i < count; ++i) {
318 rb.glyphs[i] = i;
319 rb.pos[i] = SkIntToScalar(i);
320 }
321 } break;
322 case SkTextBlob::kFull_Positioning: {
323 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
324 for (int i = 0; i < count; ++i) {
325 rb.glyphs[i] = i;
326 rb.pos[i * 2] = SkIntToScalar(i);
327 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
328 }
329 } break;
330 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400331 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700332 }
333 }
334};
335
336DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700337 TextBlobTester::TestBuilder(reporter);
338 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700339}
fmalita32d6b872015-11-17 11:39:32 -0800340
341DEF_TEST(TextBlob_paint, reporter) {
342 TextBlobTester::TestPaintProps(reporter);
343}
halcanary4f0a23a2016-08-30 11:58:33 -0700344
345DEF_TEST(TextBlob_extended, reporter) {
346 SkTextBlobBuilder textBlobBuilder;
347 SkPaint paint;
348 const char text1[] = "Foo";
349 const char text2[] = "Bar";
350
351 int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
352 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
353 (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
354 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
355
356 auto run = textBlobBuilder.allocRunText(
357 paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
358 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
359 memcpy(run.utf8text, text2, strlen(text2));
360 for (int i = 0; i < glyphCount; ++i) {
361 run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
362 }
fmalita37283c22016-09-13 10:00:23 -0700363 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700364 REPORTER_ASSERT(reporter, blob);
365
366 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
367 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
368 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
369 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
370 }
371 REPORTER_ASSERT(reporter, SkTextBlob::kDefault_Positioning == it.positioning());
372 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
373 REPORTER_ASSERT(reporter, it.textSize() > 0);
374 REPORTER_ASSERT(reporter, it.clusters());
375 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
376 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
377 }
378 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
379 }
380}
Mike Reedb99bedd2017-07-11 10:27:40 -0400381
382///////////////////////////////////////////////////////////////////////////////////////////////////
383#include "SkCanvas.h"
384#include "SkSurface.h"
385#include "SkTDArray.h"
386
387static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
388 sk_sp<SkTypeface> tf) {
389 SkPaint paint;
390 paint.setAntiAlias(true);
391 paint.setSubpixelText(true);
392 paint.setTextSize(16);
393 paint.setTypeface(tf);
394
395 int glyphCount = paint.textToGlyphs(text, strlen(text), nullptr);
396
397 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
398 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(paint, glyphCount, x, y);
399
400 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
401 (void)paint.textToGlyphs(text, strlen(text), buffer.glyphs);
402}
403
404static sk_sp<SkImage> render(const SkTextBlob* blob) {
405 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
406 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400407 if (!surf) {
408 return nullptr; // bounds are empty?
409 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400410 surf->getCanvas()->clear(SK_ColorWHITE);
411 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
412 return surf->makeImageSnapshot();
413}
414
415/*
416 * Build a blob with more than one typeface.
417 * Draw it into an offscreen,
418 * then serialize and deserialize,
419 * Then draw the new instance and assert it draws the same as the original.
420 */
421DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500422 sk_sp<SkTextBlob> blob0 = []() {
Mike Reed8e74cbc2017-12-08 13:20:01 -0500423 sk_sp<SkTypeface> tf = SkTypeface::MakeDefault();
Mike Reedb99bedd2017-07-11 10:27:40 -0400424
Mike Reed4bcef3c2017-12-08 11:46:09 -0500425 SkTextBlobBuilder builder;
Mike Reed8e74cbc2017-12-08 13:20:01 -0500426 add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint
427 add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint
Mike Reed4bcef3c2017-12-08 11:46:09 -0500428 return builder.make();
429 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400430
431 SkTDArray<SkTypeface*> array;
Mike Reedaaa30562017-07-21 11:53:23 -0400432 sk_sp<SkData> data = blob0->serialize([](SkTypeface* tf, void* ctx) {
433 auto array = (SkTDArray<SkTypeface*>*)ctx;
434 if (array->find(tf) < 0) {
435 *array->append() = tf;
Mike Reedb99bedd2017-07-11 10:27:40 -0400436 }
Mike Reedaaa30562017-07-21 11:53:23 -0400437 }, &array);
Mike Reed8e74cbc2017-12-08 13:20:01 -0500438 // we only expect 1, since null would not have been serialized, but the default would
439 REPORTER_ASSERT(reporter, array.count() == 1);
Mike Reedb99bedd2017-07-11 10:27:40 -0400440
441 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(),
Mike Reedaaa30562017-07-21 11:53:23 -0400442 [](uint32_t uniqueID, void* ctx) {
443 auto array = (SkTDArray<SkTypeface*>*)ctx;
444 for (int i = 0; i < array->count(); ++i) {
445 if ((*array)[i]->uniqueID() == uniqueID) {
446 return sk_ref_sp((*array)[i]);
Mike Reedb99bedd2017-07-11 10:27:40 -0400447 }
448 }
Mike Reedaaa30562017-07-21 11:53:23 -0400449 SkASSERT(false);
Mike Reedb99bedd2017-07-11 10:27:40 -0400450 return sk_sp<SkTypeface>(nullptr);
Mike Reedaaa30562017-07-21 11:53:23 -0400451 }, &array);
Mike Reed7e49d532017-07-11 15:07:08 -0400452
453 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400454 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400455 if (img0 && img1) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500456 REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400457 }
458}