1 /*
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 "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkSerialProcs.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkTextBlob.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/private/base/SkTArray.h"
27 #include "include/private/base/SkTemplates.h"
28 #include "include/private/base/SkTo.h"
29 #include "src/core/SkFontPriv.h"
30 #include "src/core/SkTextBlobPriv.h"
31 #include "tests/Test.h"
32 #include "tools/ToolUtils.h"
33 #include "tools/fonts/FontToolUtils.h"
34
35 #include <algorithm>
36 #include <cstdint>
37 #include <cstring>
38 #include <string>
39
40 using namespace skia_private;
41
42 class TextBlobTester {
43 public:
44 // This unit test feeds an SkTextBlobBuilder various runs then checks to see if
45 // the result contains the provided data and merges runs when appropriate.
TestBuilder(skiatest::Reporter * reporter)46 static void TestBuilder(skiatest::Reporter* reporter) {
47 SkTextBlobBuilder builder;
48
49 // empty run set
50 RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
51
52 RunDef set1[] = {
53 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 100 },
54 };
55 RunBuilderTest(reporter, builder, set1, std::size(set1), set1, std::size(set1));
56
57 RunDef set2[] = {
58 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 100 },
59 };
60 RunBuilderTest(reporter, builder, set2, std::size(set2), set2, std::size(set2));
61
62 RunDef set3[] = {
63 { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
64 };
65 RunBuilderTest(reporter, builder, set3, std::size(set3), set3, std::size(set3));
66
67 RunDef set4[] = {
68 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
69 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
70 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
71 };
72 RunBuilderTest(reporter, builder, set4, std::size(set4), set4, std::size(set4));
73
74 RunDef set5[] = {
75 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
76 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
77 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 300, 250 },
78 };
79 RunDef mergedSet5[] = {
80 { 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
81 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 250 },
82 };
83 RunBuilderTest(reporter, builder, set5, std::size(set5), mergedSet5,
84 std::size(mergedSet5));
85
86 RunDef set6[] = {
87 { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
88 { 128, SkTextBlobRunIterator::kFull_Positioning, 200, 200 },
89 { 128, SkTextBlobRunIterator::kFull_Positioning, 300, 300 },
90 };
91 RunDef mergedSet6[] = {
92 { 384, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
93 };
94 RunBuilderTest(reporter, builder, set6, std::size(set6), mergedSet6,
95 std::size(mergedSet6));
96
97 RunDef set7[] = {
98 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
99 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
100 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
101 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
102 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
103 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 350 },
104 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
105 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
106 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 550 },
107 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 650 },
108 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 750 },
109 { 128, SkTextBlobRunIterator::kFull_Positioning, 400, 850 },
110 };
111 RunDef mergedSet7[] = {
112 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
113 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
114 { 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
115 { 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
116 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
117 { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 450 },
118 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 550 },
119 { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 650 },
120 { 256, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
121 };
122 RunBuilderTest(reporter, builder, set7, std::size(set7), mergedSet7,
123 std::size(mergedSet7));
124 }
125
126 // This unit test verifies blob bounds computation.
TestBounds(skiatest::Reporter * reporter)127 static void TestBounds(skiatest::Reporter* reporter) {
128 SkTextBlobBuilder builder;
129 SkFont font = ToolUtils::DefaultFont();
130
131 // Explicit bounds.
132 {
133 sk_sp<SkTextBlob> blob(builder.make());
134 REPORTER_ASSERT(reporter, !blob);
135 }
136
137 {
138 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
139 builder.allocRun(font, 16, 0, 0, &r1);
140 sk_sp<SkTextBlob> blob(builder.make());
141 REPORTER_ASSERT(reporter, blob->bounds() == r1);
142 }
143
144 {
145 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
146 builder.allocRunPosH(font, 16, 0, &r1);
147 sk_sp<SkTextBlob> blob(builder.make());
148 REPORTER_ASSERT(reporter, blob->bounds() == r1);
149 }
150
151 {
152 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
153 builder.allocRunPos(font, 16, &r1);
154 sk_sp<SkTextBlob> blob(builder.make());
155 REPORTER_ASSERT(reporter, blob->bounds() == r1);
156 }
157
158 {
159 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
160 SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
161 SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
162
163 builder.allocRun(font, 16, 0, 0, &r1);
164 builder.allocRunPosH(font, 16, 0, &r2);
165 builder.allocRunPos(font, 16, &r3);
166
167 sk_sp<SkTextBlob> blob(builder.make());
168 REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
169 }
170
171 {
172 sk_sp<SkTextBlob> blob(builder.make());
173 REPORTER_ASSERT(reporter, !blob);
174 }
175
176 // Implicit bounds
177
178 {
179 // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
180 // don't trigger asserts (http://crbug.com/542643).
181 font.setSize(0);
182
183 const char* txt = "BOOO";
184 const size_t txtLen = strlen(txt);
185 const int glyphCount = font.countText(txt, txtLen, SkTextEncoding::kUTF8);
186 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount);
187
188 font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
189
190 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
191 sk_sp<SkTextBlob> blob(builder.make());
192 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
193 }
194 }
195
196 // Verify that text-related properties are captured in run paints.
TestPaintProps(skiatest::Reporter * reporter)197 static void TestPaintProps(skiatest::Reporter* reporter) {
198 SkFont font;
199 // Kitchen sink font.
200 font.setSize(42);
201 font.setScaleX(4.2f);
202 font.setTypeface(ToolUtils::CreatePortableTypeface("Sans", SkFontStyle::Bold()));
203 font.setSkewX(0.42f);
204 font.setHinting(SkFontHinting::kFull);
205 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
206 font.setEmbolden(true);
207 font.setLinearMetrics(true);
208 font.setSubpixel(true);
209 font.setEmbeddedBitmaps(true);
210 font.setForceAutoHinting(true);
211
212 // Ensure we didn't pick default values by mistake.
213 SkFont defaultFont = ToolUtils::DefaultFont();
214 REPORTER_ASSERT(reporter, defaultFont.getSize() != font.getSize());
215 REPORTER_ASSERT(reporter, defaultFont.getScaleX() != font.getScaleX());
216 REPORTER_ASSERT(reporter, defaultFont.getTypeface() != font.getTypeface());
217 REPORTER_ASSERT(reporter, defaultFont.getSkewX() != font.getSkewX());
218 REPORTER_ASSERT(reporter, defaultFont.getHinting() != font.getHinting());
219 REPORTER_ASSERT(reporter, defaultFont.getEdging() != font.getEdging());
220 REPORTER_ASSERT(reporter, defaultFont.isEmbolden() != font.isEmbolden());
221 REPORTER_ASSERT(reporter, defaultFont.isLinearMetrics() != font.isLinearMetrics());
222 REPORTER_ASSERT(reporter, defaultFont.isSubpixel() != font.isSubpixel());
223 REPORTER_ASSERT(reporter,
224 defaultFont.isEmbeddedBitmaps() != font.isEmbeddedBitmaps());
225 REPORTER_ASSERT(reporter, defaultFont.isForceAutoHinting() != font.isForceAutoHinting());
226
227 SkTextBlobBuilder builder;
228 AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder);
229 AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0),
230 builder);
231 AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder);
232 sk_sp<SkTextBlob> blob(builder.make());
233
234 SkTextBlobRunIterator it(blob.get());
235 while (!it.done()) {
236 REPORTER_ASSERT(reporter, it.font() == font);
237 it.next();
238 }
239
240 }
241
242 private:
243 struct RunDef {
244 unsigned count;
245 SkTextBlobRunIterator::GlyphPositioning pos;
246 SkScalar x, y;
247 };
248
RunBuilderTest(skiatest::Reporter * reporter,SkTextBlobBuilder & builder,const RunDef in[],unsigned inCount,const RunDef out[],unsigned outCount)249 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
250 const RunDef in[], unsigned inCount,
251 const RunDef out[], unsigned outCount) {
252 SkFont font = ToolUtils::DefaultFont();
253
254 for (unsigned i = 0; i < inCount; ++i) {
255 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
256 }
257
258 sk_sp<SkTextBlob> blob(builder.make());
259 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
260 if (!blob) {
261 return;
262 }
263
264 SkTextBlobRunIterator it(blob.get());
265 for (unsigned i = 0; i < outCount; ++i) {
266 REPORTER_ASSERT(reporter, !it.done());
267 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
268 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
269 if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
270 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
271 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
272 } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
273 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
274 }
275
276 for (unsigned k = 0; k < it.glyphCount(); ++k) {
277 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
278 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
279 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
280 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
281 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
282 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
283 }
284 }
285
286 it.next();
287 }
288
289 REPORTER_ASSERT(reporter, it.done());
290 }
291
AddRun(const SkFont & font,int count,SkTextBlobRunIterator::GlyphPositioning pos,const SkPoint & offset,SkTextBlobBuilder & builder,const SkRect * bounds=nullptr)292 static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
293 const SkPoint& offset, SkTextBlobBuilder& builder,
294 const SkRect* bounds = nullptr) {
295 switch (pos) {
296 case SkTextBlobRunIterator::kDefault_Positioning: {
297 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
298 offset.y(), bounds);
299 for (int i = 0; i < count; ++i) {
300 rb.glyphs[i] = i;
301 }
302 } break;
303 case SkTextBlobRunIterator::kHorizontal_Positioning: {
304 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
305 bounds);
306 for (int i = 0; i < count; ++i) {
307 rb.glyphs[i] = i;
308 rb.pos[i] = SkIntToScalar(i);
309 }
310 } break;
311 case SkTextBlobRunIterator::kFull_Positioning: {
312 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
313 for (int i = 0; i < count; ++i) {
314 rb.glyphs[i] = i;
315 rb.pos[i * 2] = SkIntToScalar(i);
316 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
317 }
318 } break;
319 default:
320 SK_ABORT("unhandled positioning value");
321 }
322 }
323 };
324
DEF_TEST(TextBlob_builder,reporter)325 DEF_TEST(TextBlob_builder, reporter) {
326 TextBlobTester::TestBuilder(reporter);
327 TextBlobTester::TestBounds(reporter);
328 }
329
DEF_TEST(TextBlob_paint,reporter)330 DEF_TEST(TextBlob_paint, reporter) {
331 TextBlobTester::TestPaintProps(reporter);
332 }
333
DEF_TEST(TextBlob_extended,reporter)334 DEF_TEST(TextBlob_extended, reporter) {
335 SkTextBlobBuilder textBlobBuilder;
336 SkFont font = ToolUtils::DefaultFont();
337 const char text1[] = "Foo";
338 const char text2[] = "Bar";
339
340 int glyphCount = font.countText(text1, strlen(text1), SkTextEncoding::kUTF8);
341 AutoTMalloc<uint16_t> glyphs(glyphCount);
342 (void)font.textToGlyphs(text1, strlen(text1), SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
343
344 auto run = textBlobBuilder.allocRunText(font, glyphCount, 0, 0, SkToInt(strlen(text2)));
345 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
346 memcpy(run.utf8text, text2, strlen(text2));
347 for (int i = 0; i < glyphCount; ++i) {
348 run.clusters[i] = std::min(SkToU32(i), SkToU32(strlen(text2)));
349 }
350 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
351 REPORTER_ASSERT(reporter, blob);
352
353 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
354 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
355 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
356 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
357 }
358 REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
359 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
360 REPORTER_ASSERT(reporter, it.textSize() > 0);
361 REPORTER_ASSERT(reporter, it.clusters());
362 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
363 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
364 }
365 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
366 }
367 }
368
369 ///////////////////////////////////////////////////////////////////////////////////////////////////
add_run(SkTextBlobBuilder * builder,const char text[],SkScalar x,SkScalar y,sk_sp<SkTypeface> tf)370 static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
371 sk_sp<SkTypeface> tf) {
372 SkFont font;
373 font.setEdging(SkFont::Edging::kAntiAlias);
374 font.setSubpixel(true);
375 font.setSize(16);
376 font.setTypeface(tf);
377
378 int glyphCount = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
379
380 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y);
381
382 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
383 }
384
render(const SkTextBlob * blob)385 static sk_sp<SkImage> render(const SkTextBlob* blob) {
386 SkASSERT(blob);
387 auto surf = SkSurfaces::Raster(
388 SkImageInfo::MakeN32Premul(SkScalarRoundToInt(blob->bounds().width()),
389 SkScalarRoundToInt(blob->bounds().height())));
390 if (!surf) {
391 return nullptr; // bounds are empty?
392 }
393 surf->getCanvas()->clear(SK_ColorWHITE);
394 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
395 return surf->makeImageSnapshot();
396 }
397
SerializeTypeface(SkTypeface * tf,void * ctx)398 static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
399 // Do not serialize the empty font.
400 if (!tf || (tf->countGlyphs() == 0 && tf->getBounds().isEmpty())) {
401 return nullptr;
402 }
403 auto array = (TArray<sk_sp<SkTypeface>>*)ctx;
404 const size_t idx = array->size();
405 array->emplace_back(sk_ref_sp(tf));
406 // In this test, we are deserializing on the same machine, so we don't worry about endianness.
407 return SkData::MakeWithCopy(&idx, sizeof(idx));
408 }
409
DeserializeTypeface(const void * data,size_t length,void * ctx)410 static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
411 auto array = (TArray<sk_sp<SkTypeface>>*)ctx;
412 if (length != sizeof(size_t)) {
413 SkDEBUGFAIL("Did not serialize an index");
414 return nullptr;
415 }
416 if (!data) {
417 return nullptr;
418 }
419 size_t idx = 0;
420 std::memcpy(&idx, data, sizeof(size_t));
421 if (idx >= SkToSizeT(array->size())) {
422 SkDEBUGFAIL("Index too big");
423 return nullptr;
424 }
425 return (*array)[idx];
426 }
427
428 /*
429 * Build a blob with more than one typeface.
430 * Draw it into an offscreen,
431 * then serialize and deserialize,
432 * Then draw the new instance and assert it draws the same as the original.
433 */
DEF_TEST(TextBlob_serialize,reporter)434 DEF_TEST(TextBlob_serialize, reporter) {
435 sk_sp<SkTextBlob> blob0 = [reporter]() {
436 sk_sp<SkTypeface> tf = ToolUtils::CreateTestTypeface(nullptr, SkFontStyle::BoldItalic());
437 REPORTER_ASSERT(reporter, tf, "Test typeface was nullptr");
438 REPORTER_ASSERT(reporter, tf->countGlyphs() > 0, "Test typeface had no glyphs");
439
440 SkTextBlobBuilder builder;
441 add_run(&builder, "Hello", 10, 20, nullptr); // don't flatten a typeface
442 add_run(&builder, "World", 10, 40, tf); // do flatten this typeface
443 return builder.make();
444 }();
445
446 TArray<sk_sp<SkTypeface>> array;
447 SkSerialProcs serializeProcs;
448 serializeProcs.fTypefaceProc = &SerializeTypeface;
449 serializeProcs.fTypefaceCtx = (void*) &array;
450 sk_sp<SkData> data = blob0->serialize(serializeProcs);
451 REPORTER_ASSERT(reporter, array.size() == 1,
452 "Did not serialize exactly one non-empty font, instead %d", array.size());
453 REPORTER_ASSERT(reporter, array[0]->countGlyphs() > 0, "Serialized typeface had no glyphs");
454 SkDeserialProcs deserializeProcs;
455 deserializeProcs.fTypefaceProc = &DeserializeTypeface;
456 deserializeProcs.fTypefaceCtx = (void*) &array;
457 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
458 REPORTER_ASSERT(reporter, blob1);
459
460 sk_sp<SkImage> img0 = render(blob0.get());
461 sk_sp<SkImage> img1 = render(blob1.get());
462 if (img0 && img1) {
463 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
464 }
465 }
466
DEF_TEST(TextBlob_MakeAsDrawText,reporter)467 DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
468 const char text[] = "Hello";
469 auto blob = SkTextBlob::MakeFromString(text, ToolUtils::DefaultFont(), SkTextEncoding::kUTF8);
470
471 int runs = 0;
472 for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
473 REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
474 REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
475 runs += 1;
476 }
477 REPORTER_ASSERT(reporter, runs == 1);
478
479 }
480
DEF_TEST(TextBlob_iter,reporter)481 DEF_TEST(TextBlob_iter, reporter) {
482 sk_sp<SkTypeface> tf = ToolUtils::CreateTestTypeface(nullptr, SkFontStyle::BoldItalic());
483
484 SkTextBlobBuilder builder;
485 add_run(&builder, "Hello", 10, 20, tf);
486 add_run(&builder, "World!", 10, 40, tf);
487 auto blob = builder.make();
488
489 SkTextBlob::Iter::Run expected[] = {
490 { tf.get(), 5, nullptr },
491 { tf.get(), 6, nullptr },
492 };
493
494 SkTextBlob::Iter iter(*blob);
495 SkTextBlob::Iter::Run run;
496 for (auto exp : expected) {
497 REPORTER_ASSERT(reporter, iter.next(&run));
498 REPORTER_ASSERT(reporter, run.fTypeface == exp.fTypeface);
499 REPORTER_ASSERT(reporter, run.fGlyphCount == exp.fGlyphCount);
500 for (int i = 0; i < run.fGlyphCount; ++i) {
501 REPORTER_ASSERT(reporter, run.fGlyphIndices[i] != 0,
502 "Glyph Index %d is unexpectedly 0", i);
503 }
504 }
505 REPORTER_ASSERT(reporter, !iter.next(&run)); // we're done
506
507 SkTextBlob::Iter iter2(*blob);
508 REPORTER_ASSERT(reporter, iter2.next(&run));
509 // Hello should have the same glyph repeated for the 'l'
510 REPORTER_ASSERT(reporter, run.fGlyphIndices[2] == run.fGlyphIndices[3]);
511 }
512
DEF_TEST(TextBlob_getIntercepts,reporter)513 DEF_TEST(TextBlob_getIntercepts, reporter) {
514 SkFont font = ToolUtils::DefaultFont();
515 font.setSize(16);
516
517 SkPoint lowPos[1] = { SkPoint::Make(0, 5) };
518 SkPoint highPos[1] = { SkPoint::Make(0, -8) };
519 SkPoint zeroPos[1] = { SkPoint::Make(0, 0) };
520
521 // 'x' sitting on baseline
522 auto blobZeroX = SkTextBlob::MakeFromPosText("x", 1, zeroPos, font);
523 // 'x' lowered to intersect baseline
524 auto blobLowX = SkTextBlob::MakeFromPosText("x", 1, lowPos, font);
525 // 'y' sitting on baseline
526 auto blobZeroY = SkTextBlob::MakeFromPosText("y", 1, zeroPos, font);
527 // 'y' raised to not intersect baseline
528 auto blobHighY = SkTextBlob::MakeFromPosText("y", 1, highPos, font);
529
530 // bounds right below baseline
531 SkScalar bounds[2] = { 1, 2 };
532
533 // 'x' on baseline should not intersect
534 REPORTER_ASSERT(reporter, blobZeroX->getIntercepts(bounds, nullptr) == 0);
535 // lowered 'x' should intersect
536 REPORTER_ASSERT(reporter, blobLowX->getIntercepts(bounds, nullptr) == 2);
537 // 'y' on baseline should intersect
538 REPORTER_ASSERT(reporter, blobZeroY->getIntercepts(bounds, nullptr) == 2);
539 // raised 'y' should not intersect
540 REPORTER_ASSERT(reporter, blobHighY->getIntercepts(bounds, nullptr) == 0);
541 }
542