• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
14 
15 #include "tests/Test.h"
16 #include "tools/ToolUtils.h"
17 
18 class TextBlobTester {
19 public:
20     // 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.
TestBuilder(skiatest::Reporter * reporter)22     static void TestBuilder(skiatest::Reporter* reporter) {
23         SkTextBlobBuilder builder;
24 
25         // empty run set
26         RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0);
27 
28         RunDef set1[] = {
29             { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 100 },
30         };
31         RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1));
32 
33         RunDef set2[] = {
34             { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 100 },
35         };
36         RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2));
37 
38         RunDef set3[] = {
39             { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
40         };
41         RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3));
42 
43         RunDef set4[] = {
44             { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
45             { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
46             { 128, SkTextBlobRunIterator::kDefault_Positioning, 100, 150 },
47         };
48         RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4));
49 
50         RunDef set5[] = {
51             { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 100, 150 },
52             { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 200, 150 },
53             { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 300, 250 },
54         };
55         RunDef mergedSet5[] = {
56             { 256, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 150 },
57             { 128, SkTextBlobRunIterator::kHorizontal_Positioning, 0, 250 },
58         };
59         RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5,
60                        SK_ARRAY_COUNT(mergedSet5));
61 
62         RunDef set6[] = {
63             { 128, SkTextBlobRunIterator::kFull_Positioning, 100, 100 },
64             { 128, SkTextBlobRunIterator::kFull_Positioning, 200, 200 },
65             { 128, SkTextBlobRunIterator::kFull_Positioning, 300, 300 },
66         };
67         RunDef mergedSet6[] = {
68             { 384, SkTextBlobRunIterator::kFull_Positioning, 0, 0 },
69         };
70         RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6,
71                        SK_ARRAY_COUNT(mergedSet6));
72 
73         RunDef set7[] = {
74             { 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 },
86         };
87         RunDef mergedSet7[] = {
88             { 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 },
97         };
98         RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7,
99                        SK_ARRAY_COUNT(mergedSet7));
100     }
101 
102     // This unit test verifies blob bounds computation.
TestBounds(skiatest::Reporter * reporter)103     static void TestBounds(skiatest::Reporter* reporter) {
104         SkTextBlobBuilder builder;
105         SkFont font;
106 
107         // Explicit bounds.
108         {
109             sk_sp<SkTextBlob> blob(builder.make());
110             REPORTER_ASSERT(reporter, !blob);
111         }
112 
113         {
114             SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
115             builder.allocRun(font, 16, 0, 0, &r1);
116             sk_sp<SkTextBlob> blob(builder.make());
117             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);
123             sk_sp<SkTextBlob> blob(builder.make());
124             REPORTER_ASSERT(reporter, blob->bounds() == r1);
125         }
126 
127         {
128             SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
129             builder.allocRunPos(font, 16, &r1);
130             sk_sp<SkTextBlob> blob(builder.make());
131             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 
143             sk_sp<SkTextBlob> blob(builder.make());
144             REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
145         }
146 
147         {
148             sk_sp<SkTextBlob> blob(builder.make());
149             REPORTER_ASSERT(reporter, !blob);
150         }
151 
152         // Implicit bounds
153 
154         {
155             // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
156             // don't trigger asserts (http://crbug.com/542643).
157             SkFont font;
158             font.setSize(0);
159 
160             const char* txt = "BOOO";
161             const size_t txtLen = strlen(txt);
162             const int glyphCount = font.countText(txt, txtLen, SkTextEncoding::kUTF8);
163             const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount);
164 
165             font.textToGlyphs(txt, txtLen, SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
166 
167             memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
168             sk_sp<SkTextBlob> blob(builder.make());
169             REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
170         }
171     }
172 
173     // Verify that text-related properties are captured in run paints.
TestPaintProps(skiatest::Reporter * reporter)174     static void TestPaintProps(skiatest::Reporter* reporter) {
175         SkFont font;
176         // Kitchen sink font.
177         font.setSize(42);
178         font.setScaleX(4.2f);
179         font.setTypeface(ToolUtils::create_portable_typeface());
180         font.setSkewX(0.42f);
181         font.setHinting(SkFontHinting::kFull);
182         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);
188 
189         // Ensure we didn't pick default values by mistake.
190         SkFont defaultFont;
191         REPORTER_ASSERT(reporter, defaultFont.getSize() != font.getSize());
192         REPORTER_ASSERT(reporter, defaultFont.getScaleX() != font.getScaleX());
193         REPORTER_ASSERT(reporter, defaultFont.getTypefaceOrDefault() != font.getTypefaceOrDefault());
194         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());
200         REPORTER_ASSERT(reporter,
201                         defaultFont.isEmbeddedBitmaps() != font.isEmbeddedBitmaps());
202         REPORTER_ASSERT(reporter, defaultFont.isForceAutoHinting() != font.isForceAutoHinting());
203 
204         SkTextBlobBuilder builder;
205         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);
209         sk_sp<SkTextBlob> blob(builder.make());
210 
211         SkTextBlobRunIterator it(blob.get());
212         while (!it.done()) {
213             REPORTER_ASSERT(reporter, it.font() == font);
214             it.next();
215         }
216 
217     }
218 
219 private:
220     struct RunDef {
221         unsigned                                count;
222         SkTextBlobRunIterator::GlyphPositioning pos;
223         SkScalar                                x, y;
224     };
225 
RunBuilderTest(skiatest::Reporter * reporter,SkTextBlobBuilder & builder,const RunDef in[],unsigned inCount,const RunDef out[],unsigned outCount)226     static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
227                                const RunDef in[], unsigned inCount,
228                                const RunDef out[], unsigned outCount) {
229         SkFont font;
230 
231         unsigned glyphCount = 0;
232         unsigned posCount = 0;
233 
234         for (unsigned i = 0; i < inCount; ++i) {
235             AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
236             glyphCount += in[i].count;
237             posCount += in[i].count * in[i].pos;
238         }
239 
240         sk_sp<SkTextBlob> blob(builder.make());
241         REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
242         if (!blob) {
243             return;
244         }
245 
246         SkTextBlobRunIterator it(blob.get());
247         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());
251             if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
252                 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
253                 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
254             } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
255                 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]);
260                 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
261                     REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
262                 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
263                     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 
AddRun(const SkFont & font,int count,SkTextBlobRunIterator::GlyphPositioning pos,const SkPoint & offset,SkTextBlobBuilder & builder,const SkRect * bounds=nullptr)274     static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
275                        const SkPoint& offset, SkTextBlobBuilder& builder,
276                        const SkRect* bounds = nullptr) {
277         switch (pos) {
278         case SkTextBlobRunIterator::kDefault_Positioning: {
279             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;
285         case SkTextBlobRunIterator::kHorizontal_Positioning: {
286             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;
293         case SkTextBlobRunIterator::kFull_Positioning: {
294             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:
302             SK_ABORT("unhandled positioning value");
303         }
304     }
305 };
306 
DEF_TEST(TextBlob_builder,reporter)307 DEF_TEST(TextBlob_builder, reporter) {
308     TextBlobTester::TestBuilder(reporter);
309     TextBlobTester::TestBounds(reporter);
310 }
311 
DEF_TEST(TextBlob_paint,reporter)312 DEF_TEST(TextBlob_paint, reporter) {
313     TextBlobTester::TestPaintProps(reporter);
314 }
315 
DEF_TEST(TextBlob_extended,reporter)316 DEF_TEST(TextBlob_extended, reporter) {
317     SkTextBlobBuilder textBlobBuilder;
318     SkFont font;
319     const char text1[] = "Foo";
320     const char text2[] = "Bar";
321 
322     int glyphCount = font.countText(text1, strlen(text1), SkTextEncoding::kUTF8);
323     SkAutoTMalloc<uint16_t> glyphs(glyphCount);
324     (void)font.textToGlyphs(text1, strlen(text1), SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
325 
326     auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
327             font, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
328     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     }
333     sk_sp<SkTextBlob> blob(textBlobBuilder.make());
334     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         }
341         REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
342         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 }
351 
352 ///////////////////////////////////////////////////////////////////////////////////////////////////
353 #include "include/core/SkCanvas.h"
354 #include "include/core/SkSurface.h"
355 #include "include/private/SkTArray.h"
356 
add_run(SkTextBlobBuilder * builder,const char text[],SkScalar x,SkScalar y,sk_sp<SkTypeface> tf)357 static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
358                     sk_sp<SkTypeface> tf) {
359     SkFont font;
360     font.setEdging(SkFont::Edging::kAntiAlias);
361     font.setSubpixel(true);
362     font.setSize(16);
363     font.setTypeface(tf);
364 
365     int glyphCount = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
366 
367     SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y);
368 
369     (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, buffer.glyphs, glyphCount);
370 }
371 
render(const SkTextBlob * blob)372 static sk_sp<SkImage> render(const SkTextBlob* blob) {
373     auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
374                                                SkScalarRoundToInt(blob->bounds().height()));
375     if (!surf) {
376         return nullptr; // bounds are empty?
377     }
378     surf->getCanvas()->clear(SK_ColorWHITE);
379     surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
380     return surf->makeImageSnapshot();
381 }
382 
SerializeTypeface(SkTypeface * tf,void * ctx)383 static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
384     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));
389 }
390 
DeserializeTypeface(const void * data,size_t length,void * ctx)391 static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
392     auto array = (SkTArray<sk_sp<SkTypeface>>*)ctx;
393     if (length != sizeof(size_t)) {
394         SkASSERT(false);
395         return nullptr;
396     }
397     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];
403 }
404 
405 /*
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  */
DEF_TEST(TextBlob_serialize,reporter)411 DEF_TEST(TextBlob_serialize, reporter) {
412     sk_sp<SkTextBlob> blob0 = []() {
413         sk_sp<SkTypeface> tf = SkTypeface::MakeFromName(nullptr, SkFontStyle::BoldItalic());
414 
415         SkTextBlobBuilder builder;
416         add_run(&builder, "Hello", 10, 20, nullptr);    // don't flatten a typeface
417         add_run(&builder, "World", 10, 40, tf);         // do flatten this typeface
418         return builder.make();
419     }();
420 
421     SkTArray<sk_sp<SkTypeface>> array;
422     SkSerialProcs serializeProcs;
423     serializeProcs.fTypefaceProc = &SerializeTypeface;
424     serializeProcs.fTypefaceCtx = (void*) &array;
425     sk_sp<SkData> data = blob0->serialize(serializeProcs);
426     REPORTER_ASSERT(reporter, array.count() == 1);
427     SkDeserialProcs deserializeProcs;
428     deserializeProcs.fTypefaceProc = &DeserializeTypeface;
429     deserializeProcs.fTypefaceCtx = (void*) &array;
430     sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
431 
432     sk_sp<SkImage> img0 = render(blob0.get());
433     sk_sp<SkImage> img1 = render(blob1.get());
434     if (img0 && img1) {
435         REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img0.get(), img1.get()));
436     }
437 }
438 
DEF_TEST(TextBlob_MakeAsDrawText,reporter)439 DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
440     const char text[] = "Hello";
441     auto blob = SkTextBlob::MakeFromString(text, SkFont(), SkTextEncoding::kUTF8);
442 
443     int runs = 0;
444     for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
445         REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
446         REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
447         runs += 1;
448     }
449     REPORTER_ASSERT(reporter, runs == 1);
450 
451 }
452