blob: 09e4bb1981667c7cfeb128a7beb9f4b5de00fcda [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"
Cary Clark785586a2018-07-19 10:07:01 -040010#include "SkSerialProcs.h"
Florin Malitaab54e732018-07-27 09:47:15 -040011#include "SkTextBlobPriv.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkTo.h"
fmalita32d6b872015-11-17 11:39:32 -080013#include "SkTypeface.h"
fmalitac6765d62014-08-21 15:03:04 -070014
15#include "Test.h"
Mike Reed4bcef3c2017-12-08 11:46:09 -050016#include "sk_tool_utils.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);
Mike Reed2ed78202018-11-21 15:10:08 -0500162 const int glyphCount = font.countText(txt, txtLen, kUTF8_SkTextEncoding);
163 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount);
fmalita0b01da72015-10-14 08:11:40 -0700164
Mike Reed2ed78202018-11-21 15:10:08 -0500165 font.textToGlyphs(txt, txtLen, kUTF8_SkTextEncoding, 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);
179 font.setTypeface(sk_tool_utils::create_portable_typeface());
180 font.setSkewX(0.42f);
Mike Reed9edbf422018-11-07 19:54:33 -0500181 font.setHinting(kFull_SkFontHinting);
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());
193 REPORTER_ASSERT(reporter, defaultFont.getTypeface() != font.getTypeface());
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());
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()) {
213 SkPaint paint;
Mike Reed2ed78202018-11-21 15:10:08 -0500214 it.applyFontToPaint(&paint); // need iter for fonts
215 SkFont iterFont = SkFont::LEGACY_ExtractFromPaint(paint);
fmalita32d6b872015-11-17 11:39:32 -0800216
Mike Reed2ed78202018-11-21 15:10:08 -0500217 REPORTER_ASSERT(reporter, iterFont == font);
fmalita32d6b872015-11-17 11:39:32 -0800218
219 it.next();
220 }
221
222 }
223
fmalitac6765d62014-08-21 15:03:04 -0700224private:
225 struct RunDef {
Florin Malitaab54e732018-07-27 09:47:15 -0400226 unsigned count;
227 SkTextBlobRunIterator::GlyphPositioning pos;
228 SkScalar x, y;
fmalitac6765d62014-08-21 15:03:04 -0700229 };
230
fmalita51bf9572014-08-22 07:50:45 -0700231 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700232 const RunDef in[], unsigned inCount,
233 const RunDef out[], unsigned outCount) {
Mike Reed2ed78202018-11-21 15:10:08 -0500234 SkFont font;
fmalita51bf9572014-08-22 07:50:45 -0700235
fmalitac6765d62014-08-21 15:03:04 -0700236 unsigned glyphCount = 0;
237 unsigned posCount = 0;
238
239 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700240 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700241 glyphCount += in[i].count;
242 posCount += in[i].count * in[i].pos;
243 }
244
fmalita37283c22016-09-13 10:00:23 -0700245 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400246 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
247 if (!blob) {
248 return;
249 }
fmalitac6765d62014-08-21 15:03:04 -0700250
fmalita37283c22016-09-13 10:00:23 -0700251 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700252 for (unsigned i = 0; i < outCount; ++i) {
253 REPORTER_ASSERT(reporter, !it.done());
254 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
255 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
Florin Malitaab54e732018-07-27 09:47:15 -0400256 if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700257 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
258 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
Florin Malitaab54e732018-07-27 09:47:15 -0400259 } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700260 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
261 }
262
263 for (unsigned k = 0; k < it.glyphCount(); ++k) {
264 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400265 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700266 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400267 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700268 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
269 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
270 }
271 }
272
273 it.next();
274 }
275
276 REPORTER_ASSERT(reporter, it.done());
277 }
278
Mike Reed2ed78202018-11-21 15:10:08 -0500279 static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700280 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700281 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700282 switch (pos) {
Florin Malitaab54e732018-07-27 09:47:15 -0400283 case SkTextBlobRunIterator::kDefault_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700284 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
285 offset.y(), bounds);
286 for (int i = 0; i < count; ++i) {
287 rb.glyphs[i] = i;
288 }
289 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400290 case SkTextBlobRunIterator::kHorizontal_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700291 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
292 bounds);
293 for (int i = 0; i < count; ++i) {
294 rb.glyphs[i] = i;
295 rb.pos[i] = SkIntToScalar(i);
296 }
297 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400298 case SkTextBlobRunIterator::kFull_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700299 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
300 for (int i = 0; i < count; ++i) {
301 rb.glyphs[i] = i;
302 rb.pos[i * 2] = SkIntToScalar(i);
303 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
304 }
305 } break;
306 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400307 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700308 }
309 }
310};
311
312DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700313 TextBlobTester::TestBuilder(reporter);
314 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700315}
fmalita32d6b872015-11-17 11:39:32 -0800316
317DEF_TEST(TextBlob_paint, reporter) {
318 TextBlobTester::TestPaintProps(reporter);
319}
halcanary4f0a23a2016-08-30 11:58:33 -0700320
321DEF_TEST(TextBlob_extended, reporter) {
322 SkTextBlobBuilder textBlobBuilder;
323 SkPaint paint;
324 const char text1[] = "Foo";
325 const char text2[] = "Bar";
326
327 int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
328 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
329 (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
Mike Reed97f3cc22018-12-03 09:45:17 -0500330 paint.setTextEncoding(kGlyphID_SkTextEncoding);
halcanary4f0a23a2016-08-30 11:58:33 -0700331
Cary Clarke12a0902018-08-09 10:07:33 -0400332 auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
halcanary4f0a23a2016-08-30 11:58:33 -0700333 paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
334 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
335 memcpy(run.utf8text, text2, strlen(text2));
336 for (int i = 0; i < glyphCount; ++i) {
337 run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
338 }
fmalita37283c22016-09-13 10:00:23 -0700339 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700340 REPORTER_ASSERT(reporter, blob);
341
342 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
343 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
344 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
345 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
346 }
Florin Malitaab54e732018-07-27 09:47:15 -0400347 REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
halcanary4f0a23a2016-08-30 11:58:33 -0700348 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
349 REPORTER_ASSERT(reporter, it.textSize() > 0);
350 REPORTER_ASSERT(reporter, it.clusters());
351 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
352 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
353 }
354 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
355 }
356}
Mike Reedb99bedd2017-07-11 10:27:40 -0400357
358///////////////////////////////////////////////////////////////////////////////////////////////////
359#include "SkCanvas.h"
360#include "SkSurface.h"
361#include "SkTDArray.h"
362
363static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
364 sk_sp<SkTypeface> tf) {
Mike Reed70914f52018-11-23 13:08:33 -0500365 SkFont font;
366 font.setEdging(SkFont::Edging::kAntiAlias);
367 font.setSubpixel(true);
368 font.setSize(16);
369 font.setTypeface(tf);
Mike Reedb99bedd2017-07-11 10:27:40 -0400370
Mike Reed70914f52018-11-23 13:08:33 -0500371 int glyphCount = font.countText(text, strlen(text), kUTF8_SkTextEncoding);
Mike Reedb99bedd2017-07-11 10:27:40 -0400372
Mike Reed70914f52018-11-23 13:08:33 -0500373 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y);
Mike Reedb99bedd2017-07-11 10:27:40 -0400374
Mike Reed70914f52018-11-23 13:08:33 -0500375 (void)font.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, buffer.glyphs, glyphCount);
Mike Reedb99bedd2017-07-11 10:27:40 -0400376}
377
378static sk_sp<SkImage> render(const SkTextBlob* blob) {
379 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
380 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400381 if (!surf) {
382 return nullptr; // bounds are empty?
383 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400384 surf->getCanvas()->clear(SK_ColorWHITE);
385 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
386 return surf->makeImageSnapshot();
387}
388
Cary Clark785586a2018-07-19 10:07:01 -0400389static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
390 auto array = (SkTDArray<SkTypeface*>*)ctx;
391 *array->append() = tf;
392 return sk_sp<SkData>(nullptr);
393}
394
395static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
396 auto array = (SkTDArray<SkTypeface*>*)ctx;
397 for (int i = 0; i < array->count(); ++i) {
398 auto result = (*array)[i];
399 if (result) {
400 (*array)[i] = nullptr;
401 return sk_ref_sp(result);
402 }
403 }
404 SkASSERT(false);
405 return sk_sp<SkTypeface>(nullptr);
406}
407
Mike Reedb99bedd2017-07-11 10:27:40 -0400408/*
409 * Build a blob with more than one typeface.
410 * Draw it into an offscreen,
411 * then serialize and deserialize,
412 * Then draw the new instance and assert it draws the same as the original.
413 */
414DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500415 sk_sp<SkTextBlob> blob0 = []() {
Mike Reed8e74cbc2017-12-08 13:20:01 -0500416 sk_sp<SkTypeface> tf = SkTypeface::MakeDefault();
Mike Reedb99bedd2017-07-11 10:27:40 -0400417
Mike Reed4bcef3c2017-12-08 11:46:09 -0500418 SkTextBlobBuilder builder;
Mike Reed8e74cbc2017-12-08 13:20:01 -0500419 add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint
420 add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint
Mike Reed4bcef3c2017-12-08 11:46:09 -0500421 return builder.make();
422 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400423
424 SkTDArray<SkTypeface*> array;
Cary Clark785586a2018-07-19 10:07:01 -0400425 SkSerialProcs serializeProcs;
426 serializeProcs.fTypefaceProc = &SerializeTypeface;
427 serializeProcs.fTypefaceCtx = (void*) &array;
428 sk_sp<SkData> data = blob0->serialize(serializeProcs);
429 REPORTER_ASSERT(reporter, array.count() == 2);
430 SkDeserialProcs deserializeProcs;
431 deserializeProcs.fTypefaceProc = &DeserializeTypeface;
432 deserializeProcs.fTypefaceCtx = (void*) &array;
433 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
Mike Reed7e49d532017-07-11 15:07:08 -0400434
435 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400436 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400437 if (img0 && img1) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500438 REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400439 }
440}
Herb Derby4b3a5152018-07-17 16:10:30 -0400441
442DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
Herb Derby4b3a5152018-07-17 16:10:30 -0400443 const char text[] = "Hello";
Mike Reed97f3cc22018-12-03 09:45:17 -0500444 auto blob = SkTextBlob::MakeFromString(text, SkFont(), kUTF8_SkTextEncoding);
Herb Derby4b3a5152018-07-17 16:10:30 -0400445
446 int runs = 0;
447 for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
448 REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
Florin Malitaab54e732018-07-27 09:47:15 -0400449 REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
Herb Derby4b3a5152018-07-17 16:10:30 -0400450 runs += 1;
451 }
452 REPORTER_ASSERT(reporter, runs == 1);
453
454}