blob: 6aabc45a2aefcfcfbb5403e0a0b071d8ca2dbaf7 [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"
fmalita32d6b872015-11-17 11:39:32 -080011#include "SkTypeface.h"
fmalitac6765d62014-08-21 15:03:04 -070012
13#include "Test.h"
Mike Reed4bcef3c2017-12-08 11:46:09 -050014#include "sk_tool_utils.h"
fmalitac6765d62014-08-21 15:03:04 -070015
fmalitac6765d62014-08-21 15:03:04 -070016class TextBlobTester {
17public:
fmalita51bf9572014-08-22 07:50:45 -070018 // This unit test feeds an SkTextBlobBuilder various runs then checks to see if
19 // the result contains the provided data and merges runs when appropriate.
20 static void TestBuilder(skiatest::Reporter* reporter) {
fmalitac6765d62014-08-21 15:03:04 -070021 SkTextBlobBuilder builder;
22
23 // empty run set
halcanary96fcdcc2015-08-27 07:41:13 -070024 RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
fmalitac6765d62014-08-21 15:03:04 -070025
fmalita51bf9572014-08-22 07:50:45 -070026 RunDef set1[] = {
fmalitac6765d62014-08-21 15:03:04 -070027 { 128, SkTextBlob::kDefault_Positioning, 100, 100 },
28 };
fmalita51bf9572014-08-22 07:50:45 -070029 RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1));
fmalitac6765d62014-08-21 15:03:04 -070030
fmalita51bf9572014-08-22 07:50:45 -070031 RunDef set2[] = {
fmalitac6765d62014-08-21 15:03:04 -070032 { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 },
33 };
fmalita51bf9572014-08-22 07:50:45 -070034 RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2));
fmalitac6765d62014-08-21 15:03:04 -070035
fmalita51bf9572014-08-22 07:50:45 -070036 RunDef set3[] = {
fmalitac6765d62014-08-21 15:03:04 -070037 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
38 };
fmalita51bf9572014-08-22 07:50:45 -070039 RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3));
fmalitac6765d62014-08-21 15:03:04 -070040
fmalita51bf9572014-08-22 07:50:45 -070041 RunDef set4[] = {
fmalitac6765d62014-08-21 15:03:04 -070042 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
43 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
44 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
45 };
fmalita51bf9572014-08-22 07:50:45 -070046 RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4));
fmalitac6765d62014-08-21 15:03:04 -070047
fmalita51bf9572014-08-22 07:50:45 -070048 RunDef set5[] = {
fmalitac6765d62014-08-21 15:03:04 -070049 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
50 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
51 { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 },
52 };
fmalita51bf9572014-08-22 07:50:45 -070053 RunDef mergedSet5[] = {
fmalitac6765d62014-08-21 15:03:04 -070054 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
55 { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 },
56 };
fmalita51bf9572014-08-22 07:50:45 -070057 RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5,
58 SK_ARRAY_COUNT(mergedSet5));
fmalitac6765d62014-08-21 15:03:04 -070059
fmalita51bf9572014-08-22 07:50:45 -070060 RunDef set6[] = {
fmalitac6765d62014-08-21 15:03:04 -070061 { 128, SkTextBlob::kFull_Positioning, 100, 100 },
62 { 128, SkTextBlob::kFull_Positioning, 200, 200 },
63 { 128, SkTextBlob::kFull_Positioning, 300, 300 },
64 };
fmalita51bf9572014-08-22 07:50:45 -070065 RunDef mergedSet6[] = {
fmalitac6765d62014-08-21 15:03:04 -070066 { 384, SkTextBlob::kFull_Positioning, 0, 0 },
67 };
fmalita51bf9572014-08-22 07:50:45 -070068 RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6,
69 SK_ARRAY_COUNT(mergedSet6));
fmalitac6765d62014-08-21 15:03:04 -070070
fmalita51bf9572014-08-22 07:50:45 -070071 RunDef set7[] = {
fmalitac6765d62014-08-21 15:03:04 -070072 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
73 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
74 { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 },
75 { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 },
76 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
77 { 128, SkTextBlob::kFull_Positioning, 400, 350 },
78 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
79 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
80 { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 },
81 { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 },
82 { 128, SkTextBlob::kFull_Positioning, 400, 750 },
83 { 128, SkTextBlob::kFull_Positioning, 400, 850 },
84 };
fmalita51bf9572014-08-22 07:50:45 -070085 RunDef mergedSet7[] = {
fmalitac6765d62014-08-21 15:03:04 -070086 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
87 { 128, SkTextBlob::kDefault_Positioning, 100, 150 },
88 { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 },
89 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
90 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
91 { 128, SkTextBlob::kDefault_Positioning, 100, 450 },
92 { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 },
93 { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 },
94 { 256, SkTextBlob::kFull_Positioning, 0, 0 },
95 };
fmalita51bf9572014-08-22 07:50:45 -070096 RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7,
97 SK_ARRAY_COUNT(mergedSet7));
98 }
99
100 // This unit test verifies blob bounds computation.
101 static void TestBounds(skiatest::Reporter* reporter) {
102 SkTextBlobBuilder builder;
103 SkPaint font;
104 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
105
106 // Explicit bounds.
107 {
fmalita37283c22016-09-13 10:00:23 -0700108 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400109 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700110 }
111
112 {
113 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
114 builder.allocRun(font, 16, 0, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700115 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700116 REPORTER_ASSERT(reporter, blob->bounds() == r1);
117 }
118
119 {
120 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
121 builder.allocRunPosH(font, 16, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700122 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700123 REPORTER_ASSERT(reporter, blob->bounds() == r1);
124 }
125
126 {
127 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
128 builder.allocRunPos(font, 16, &r1);
fmalita37283c22016-09-13 10:00:23 -0700129 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700130 REPORTER_ASSERT(reporter, blob->bounds() == r1);
131 }
132
133 {
134 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
135 SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
136 SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
137
138 builder.allocRun(font, 16, 0, 0, &r1);
139 builder.allocRunPosH(font, 16, 0, &r2);
140 builder.allocRunPos(font, 16, &r3);
141
fmalita37283c22016-09-13 10:00:23 -0700142 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700143 REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
144 }
145
146 {
fmalita37283c22016-09-13 10:00:23 -0700147 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400148 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700149 }
150
151 // Implicit bounds
fmalita9ae8fe12015-10-13 08:59:23 -0700152
153 {
154 // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
155 // don't trigger asserts (http://crbug.com/542643).
156 SkPaint p;
157 p.setTextSize(0);
fmalita0b01da72015-10-14 08:11:40 -0700158 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
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);
162 const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr);
163
164 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
165 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount);
166
167 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
168 p.textToGlyphs(txt, txtLen, buffer.glyphs);
169
170 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
fmalita37283c22016-09-13 10:00:23 -0700171 sk_sp<SkTextBlob> blob(builder.make());
fmalita9ae8fe12015-10-13 08:59:23 -0700172 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
173 }
fmalitac6765d62014-08-21 15:03:04 -0700174 }
175
fmalita32d6b872015-11-17 11:39:32 -0800176 // Verify that text-related properties are captured in run paints.
177 static void TestPaintProps(skiatest::Reporter* reporter) {
178 SkPaint font;
179 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
180
fmalita32d6b872015-11-17 11:39:32 -0800181 // Kitchen sink font.
182 font.setTextSize(42);
183 font.setTextScaleX(4.2f);
bungeman13b9c952016-05-12 10:09:30 -0700184 font.setTypeface(SkTypeface::MakeDefault());
fmalita32d6b872015-11-17 11:39:32 -0800185 font.setTextSkewX(0.42f);
186 font.setTextAlign(SkPaint::kCenter_Align);
187 font.setHinting(SkPaint::kFull_Hinting);
188 font.setAntiAlias(true);
fmalita32d6b872015-11-17 11:39:32 -0800189 font.setFakeBoldText(true);
190 font.setLinearText(true);
191 font.setSubpixelText(true);
192 font.setDevKernText(true);
193 font.setLCDRenderText(true);
194 font.setEmbeddedBitmapText(true);
195 font.setAutohinted(true);
196 font.setVerticalText(true);
fmalita32d6b872015-11-17 11:39:32 -0800197
198 // Ensure we didn't pick default values by mistake.
199 SkPaint defaultPaint;
200 REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize());
201 REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX());
202 REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface());
203 REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX());
204 REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign());
205 REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting());
206 REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800207 REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText());
208 REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText());
209 REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800210 REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText());
211 REPORTER_ASSERT(reporter, defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText());
212 REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted());
213 REPORTER_ASSERT(reporter, defaultPaint.isVerticalText() != font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800214
215 SkTextBlobBuilder builder;
216 AddRun(font, 1, SkTextBlob::kDefault_Positioning, SkPoint::Make(0, 0), builder);
217 AddRun(font, 1, SkTextBlob::kHorizontal_Positioning, SkPoint::Make(0, 0), builder);
218 AddRun(font, 1, SkTextBlob::kFull_Positioning, SkPoint::Make(0, 0), builder);
fmalita37283c22016-09-13 10:00:23 -0700219 sk_sp<SkTextBlob> blob(builder.make());
fmalita32d6b872015-11-17 11:39:32 -0800220
fmalita37283c22016-09-13 10:00:23 -0700221 SkTextBlobRunIterator it(blob.get());
fmalita32d6b872015-11-17 11:39:32 -0800222 while (!it.done()) {
223 SkPaint paint;
224 it.applyFontToPaint(&paint);
225
226 REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize());
227 REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX());
228 REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface());
229 REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX());
230 REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign());
231 REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting());
232 REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800233 REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText());
234 REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText());
235 REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800236 REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText());
237 REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText());
238 REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted());
239 REPORTER_ASSERT(reporter, paint.isVerticalText() == font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800240
241 it.next();
242 }
243
244 }
245
fmalitac6765d62014-08-21 15:03:04 -0700246private:
247 struct RunDef {
248 unsigned count;
249 SkTextBlob::GlyphPositioning pos;
250 SkScalar x, y;
251 };
252
fmalita51bf9572014-08-22 07:50:45 -0700253 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700254 const RunDef in[], unsigned inCount,
255 const RunDef out[], unsigned outCount) {
fmalita51bf9572014-08-22 07:50:45 -0700256 SkPaint font;
257 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
258
fmalitac6765d62014-08-21 15:03:04 -0700259 unsigned glyphCount = 0;
260 unsigned posCount = 0;
261
262 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700263 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700264 glyphCount += in[i].count;
265 posCount += in[i].count * in[i].pos;
266 }
267
fmalita37283c22016-09-13 10:00:23 -0700268 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400269 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
270 if (!blob) {
271 return;
272 }
fmalitac6765d62014-08-21 15:03:04 -0700273
fmalita37283c22016-09-13 10:00:23 -0700274 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700275 for (unsigned i = 0; i < outCount; ++i) {
276 REPORTER_ASSERT(reporter, !it.done());
277 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
278 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
279 if (SkTextBlob::kDefault_Positioning == out[i].pos) {
280 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
281 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
282 } else if (SkTextBlob::kHorizontal_Positioning == out[i].pos) {
283 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
284 }
285
286 for (unsigned k = 0; k < it.glyphCount(); ++k) {
287 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
288 if (SkTextBlob::kHorizontal_Positioning == it.positioning()) {
289 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
290 } else if (SkTextBlob::kFull_Positioning == it.positioning()) {
291 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
292 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
293 }
294 }
295
296 it.next();
297 }
298
299 REPORTER_ASSERT(reporter, it.done());
300 }
301
fmalita51bf9572014-08-22 07:50:45 -0700302 static void AddRun(const SkPaint& font, int count, SkTextBlob::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700303 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700304 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700305 switch (pos) {
306 case SkTextBlob::kDefault_Positioning: {
307 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
308 offset.y(), bounds);
309 for (int i = 0; i < count; ++i) {
310 rb.glyphs[i] = i;
311 }
312 } break;
313 case SkTextBlob::kHorizontal_Positioning: {
314 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
315 bounds);
316 for (int i = 0; i < count; ++i) {
317 rb.glyphs[i] = i;
318 rb.pos[i] = SkIntToScalar(i);
319 }
320 } break;
321 case SkTextBlob::kFull_Positioning: {
322 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
323 for (int i = 0; i < count; ++i) {
324 rb.glyphs[i] = i;
325 rb.pos[i * 2] = SkIntToScalar(i);
326 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
327 }
328 } break;
329 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400330 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700331 }
332 }
333};
334
335DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700336 TextBlobTester::TestBuilder(reporter);
337 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700338}
fmalita32d6b872015-11-17 11:39:32 -0800339
340DEF_TEST(TextBlob_paint, reporter) {
341 TextBlobTester::TestPaintProps(reporter);
342}
halcanary4f0a23a2016-08-30 11:58:33 -0700343
344DEF_TEST(TextBlob_extended, reporter) {
345 SkTextBlobBuilder textBlobBuilder;
346 SkPaint paint;
347 const char text1[] = "Foo";
348 const char text2[] = "Bar";
349
350 int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
351 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
352 (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
353 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
354
355 auto run = textBlobBuilder.allocRunText(
356 paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
357 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
358 memcpy(run.utf8text, text2, strlen(text2));
359 for (int i = 0; i < glyphCount; ++i) {
360 run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
361 }
fmalita37283c22016-09-13 10:00:23 -0700362 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700363 REPORTER_ASSERT(reporter, blob);
364
365 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
366 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
367 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
368 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
369 }
370 REPORTER_ASSERT(reporter, SkTextBlob::kDefault_Positioning == it.positioning());
371 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
372 REPORTER_ASSERT(reporter, it.textSize() > 0);
373 REPORTER_ASSERT(reporter, it.clusters());
374 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
375 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
376 }
377 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
378 }
379}
Mike Reedb99bedd2017-07-11 10:27:40 -0400380
381///////////////////////////////////////////////////////////////////////////////////////////////////
382#include "SkCanvas.h"
383#include "SkSurface.h"
384#include "SkTDArray.h"
385
386static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
387 sk_sp<SkTypeface> tf) {
388 SkPaint paint;
389 paint.setAntiAlias(true);
390 paint.setSubpixelText(true);
391 paint.setTextSize(16);
392 paint.setTypeface(tf);
393
394 int glyphCount = paint.textToGlyphs(text, strlen(text), nullptr);
395
396 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
397 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(paint, glyphCount, x, y);
398
399 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
400 (void)paint.textToGlyphs(text, strlen(text), buffer.glyphs);
401}
402
403static sk_sp<SkImage> render(const SkTextBlob* blob) {
404 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
405 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400406 if (!surf) {
407 return nullptr; // bounds are empty?
408 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400409 surf->getCanvas()->clear(SK_ColorWHITE);
410 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
411 return surf->makeImageSnapshot();
412}
413
414/*
415 * Build a blob with more than one typeface.
416 * Draw it into an offscreen,
417 * then serialize and deserialize,
418 * Then draw the new instance and assert it draws the same as the original.
419 */
420DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500421 sk_sp<SkTextBlob> blob0 = []() {
Mike Reed8e74cbc2017-12-08 13:20:01 -0500422 sk_sp<SkTypeface> tf = SkTypeface::MakeDefault();
Mike Reedb99bedd2017-07-11 10:27:40 -0400423
Mike Reed4bcef3c2017-12-08 11:46:09 -0500424 SkTextBlobBuilder builder;
Mike Reed8e74cbc2017-12-08 13:20:01 -0500425 add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint
426 add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint
Mike Reed4bcef3c2017-12-08 11:46:09 -0500427 return builder.make();
428 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400429
430 SkTDArray<SkTypeface*> array;
Mike Reedaaa30562017-07-21 11:53:23 -0400431 sk_sp<SkData> data = blob0->serialize([](SkTypeface* tf, void* ctx) {
432 auto array = (SkTDArray<SkTypeface*>*)ctx;
433 if (array->find(tf) < 0) {
434 *array->append() = tf;
Mike Reedb99bedd2017-07-11 10:27:40 -0400435 }
Mike Reedaaa30562017-07-21 11:53:23 -0400436 }, &array);
Mike Reed8e74cbc2017-12-08 13:20:01 -0500437 // we only expect 1, since null would not have been serialized, but the default would
438 REPORTER_ASSERT(reporter, array.count() == 1);
Mike Reedb99bedd2017-07-11 10:27:40 -0400439
440 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(),
Mike Reedaaa30562017-07-21 11:53:23 -0400441 [](uint32_t uniqueID, void* ctx) {
442 auto array = (SkTDArray<SkTypeface*>*)ctx;
443 for (int i = 0; i < array->count(); ++i) {
444 if ((*array)[i]->uniqueID() == uniqueID) {
445 return sk_ref_sp((*array)[i]);
Mike Reedb99bedd2017-07-11 10:27:40 -0400446 }
447 }
Mike Reedaaa30562017-07-21 11:53:23 -0400448 SkASSERT(false);
Mike Reedb99bedd2017-07-11 10:27:40 -0400449 return sk_sp<SkTypeface>(nullptr);
Mike Reedaaa30562017-07-21 11:53:23 -0400450 }, &array);
Mike Reed7e49d532017-07-11 15:07:08 -0400451
452 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400453 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400454 if (img0 && img1) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500455 REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400456 }
457}