blob: 1a4af06dd1c4362c19469801eabe408bfa8e775f [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;
105 SkPaint font;
106 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
107
108 // Explicit bounds.
109 {
fmalita37283c22016-09-13 10:00:23 -0700110 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400111 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700112 }
113
114 {
115 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
116 builder.allocRun(font, 16, 0, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700117 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700118 REPORTER_ASSERT(reporter, blob->bounds() == r1);
119 }
120
121 {
122 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
123 builder.allocRunPosH(font, 16, 0, &r1);
fmalita37283c22016-09-13 10:00:23 -0700124 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700125 REPORTER_ASSERT(reporter, blob->bounds() == r1);
126 }
127
128 {
129 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
130 builder.allocRunPos(font, 16, &r1);
fmalita37283c22016-09-13 10:00:23 -0700131 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700132 REPORTER_ASSERT(reporter, blob->bounds() == r1);
133 }
134
135 {
136 SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20);
137 SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50);
138 SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5);
139
140 builder.allocRun(font, 16, 0, 0, &r1);
141 builder.allocRunPosH(font, 16, 0, &r2);
142 builder.allocRunPos(font, 16, &r3);
143
fmalita37283c22016-09-13 10:00:23 -0700144 sk_sp<SkTextBlob> blob(builder.make());
fmalita51bf9572014-08-22 07:50:45 -0700145 REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65));
146 }
147
148 {
fmalita37283c22016-09-13 10:00:23 -0700149 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400150 REPORTER_ASSERT(reporter, !blob);
fmalita51bf9572014-08-22 07:50:45 -0700151 }
152
153 // Implicit bounds
fmalita9ae8fe12015-10-13 08:59:23 -0700154
155 {
156 // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers
157 // don't trigger asserts (http://crbug.com/542643).
158 SkPaint p;
159 p.setTextSize(0);
fmalita0b01da72015-10-14 08:11:40 -0700160 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
fmalita9ae8fe12015-10-13 08:59:23 -0700161
162 const char* txt = "BOOO";
fmalita0b01da72015-10-14 08:11:40 -0700163 const size_t txtLen = strlen(txt);
164 const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr);
165
166 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
167 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount);
168
169 p.setTextEncoding(SkPaint::kUTF8_TextEncoding);
170 p.textToGlyphs(txt, txtLen, buffer.glyphs);
171
172 memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2);
fmalita37283c22016-09-13 10:00:23 -0700173 sk_sp<SkTextBlob> blob(builder.make());
fmalita9ae8fe12015-10-13 08:59:23 -0700174 REPORTER_ASSERT(reporter, blob->bounds().isEmpty());
175 }
fmalitac6765d62014-08-21 15:03:04 -0700176 }
177
fmalita32d6b872015-11-17 11:39:32 -0800178 // Verify that text-related properties are captured in run paints.
179 static void TestPaintProps(skiatest::Reporter* reporter) {
180 SkPaint font;
181 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
182
fmalita32d6b872015-11-17 11:39:32 -0800183 // Kitchen sink font.
184 font.setTextSize(42);
185 font.setTextScaleX(4.2f);
bungeman13b9c952016-05-12 10:09:30 -0700186 font.setTypeface(SkTypeface::MakeDefault());
fmalita32d6b872015-11-17 11:39:32 -0800187 font.setTextSkewX(0.42f);
188 font.setTextAlign(SkPaint::kCenter_Align);
189 font.setHinting(SkPaint::kFull_Hinting);
190 font.setAntiAlias(true);
fmalita32d6b872015-11-17 11:39:32 -0800191 font.setFakeBoldText(true);
192 font.setLinearText(true);
193 font.setSubpixelText(true);
194 font.setDevKernText(true);
195 font.setLCDRenderText(true);
196 font.setEmbeddedBitmapText(true);
197 font.setAutohinted(true);
198 font.setVerticalText(true);
fmalita32d6b872015-11-17 11:39:32 -0800199
200 // Ensure we didn't pick default values by mistake.
201 SkPaint defaultPaint;
202 REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize());
203 REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX());
204 REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface());
205 REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX());
206 REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign());
207 REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting());
208 REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800209 REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText());
210 REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText());
211 REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800212 REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText());
Florin Malitaab54e732018-07-27 09:47:15 -0400213 REPORTER_ASSERT(reporter,
214 defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText());
fmalita32d6b872015-11-17 11:39:32 -0800215 REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted());
216 REPORTER_ASSERT(reporter, defaultPaint.isVerticalText() != font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800217
218 SkTextBlobBuilder builder;
Florin Malitaab54e732018-07-27 09:47:15 -0400219 AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder);
220 AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0),
221 builder);
222 AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder);
fmalita37283c22016-09-13 10:00:23 -0700223 sk_sp<SkTextBlob> blob(builder.make());
fmalita32d6b872015-11-17 11:39:32 -0800224
fmalita37283c22016-09-13 10:00:23 -0700225 SkTextBlobRunIterator it(blob.get());
fmalita32d6b872015-11-17 11:39:32 -0800226 while (!it.done()) {
227 SkPaint paint;
228 it.applyFontToPaint(&paint);
229
230 REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize());
231 REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX());
232 REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface());
233 REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX());
234 REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign());
235 REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting());
236 REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias());
fmalita32d6b872015-11-17 11:39:32 -0800237 REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText());
238 REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText());
239 REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText());
fmalita32d6b872015-11-17 11:39:32 -0800240 REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText());
241 REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText());
242 REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted());
243 REPORTER_ASSERT(reporter, paint.isVerticalText() == font.isVerticalText());
fmalita32d6b872015-11-17 11:39:32 -0800244
245 it.next();
246 }
247
248 }
249
fmalitac6765d62014-08-21 15:03:04 -0700250private:
251 struct RunDef {
Florin Malitaab54e732018-07-27 09:47:15 -0400252 unsigned count;
253 SkTextBlobRunIterator::GlyphPositioning pos;
254 SkScalar x, y;
fmalitac6765d62014-08-21 15:03:04 -0700255 };
256
fmalita51bf9572014-08-22 07:50:45 -0700257 static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder,
fmalitac6765d62014-08-21 15:03:04 -0700258 const RunDef in[], unsigned inCount,
259 const RunDef out[], unsigned outCount) {
fmalita51bf9572014-08-22 07:50:45 -0700260 SkPaint font;
261 font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
262
fmalitac6765d62014-08-21 15:03:04 -0700263 unsigned glyphCount = 0;
264 unsigned posCount = 0;
265
266 for (unsigned i = 0; i < inCount; ++i) {
fmalita51bf9572014-08-22 07:50:45 -0700267 AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder);
fmalitac6765d62014-08-21 15:03:04 -0700268 glyphCount += in[i].count;
269 posCount += in[i].count * in[i].pos;
270 }
271
fmalita37283c22016-09-13 10:00:23 -0700272 sk_sp<SkTextBlob> blob(builder.make());
Florin Malita3a9a7a32017-03-13 09:03:24 -0400273 REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob));
274 if (!blob) {
275 return;
276 }
fmalitac6765d62014-08-21 15:03:04 -0700277
fmalita37283c22016-09-13 10:00:23 -0700278 SkTextBlobRunIterator it(blob.get());
fmalitac6765d62014-08-21 15:03:04 -0700279 for (unsigned i = 0; i < outCount; ++i) {
280 REPORTER_ASSERT(reporter, !it.done());
281 REPORTER_ASSERT(reporter, out[i].pos == it.positioning());
282 REPORTER_ASSERT(reporter, out[i].count == it.glyphCount());
Florin Malitaab54e732018-07-27 09:47:15 -0400283 if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700284 REPORTER_ASSERT(reporter, out[i].x == it.offset().x());
285 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
Florin Malitaab54e732018-07-27 09:47:15 -0400286 } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) {
fmalitac6765d62014-08-21 15:03:04 -0700287 REPORTER_ASSERT(reporter, out[i].y == it.offset().y());
288 }
289
290 for (unsigned k = 0; k < it.glyphCount(); ++k) {
291 REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400292 if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700293 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]);
Florin Malitaab54e732018-07-27 09:47:15 -0400294 } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) {
fmalitac6765d62014-08-21 15:03:04 -0700295 REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]);
296 REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]);
297 }
298 }
299
300 it.next();
301 }
302
303 REPORTER_ASSERT(reporter, it.done());
304 }
305
Florin Malitaab54e732018-07-27 09:47:15 -0400306 static void AddRun(const SkPaint& font, int count, SkTextBlobRunIterator::GlyphPositioning pos,
fmalitac6765d62014-08-21 15:03:04 -0700307 const SkPoint& offset, SkTextBlobBuilder& builder,
halcanary96fcdcc2015-08-27 07:41:13 -0700308 const SkRect* bounds = nullptr) {
fmalitac6765d62014-08-21 15:03:04 -0700309 switch (pos) {
Florin Malitaab54e732018-07-27 09:47:15 -0400310 case SkTextBlobRunIterator::kDefault_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700311 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(),
312 offset.y(), bounds);
313 for (int i = 0; i < count; ++i) {
314 rb.glyphs[i] = i;
315 }
316 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400317 case SkTextBlobRunIterator::kHorizontal_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700318 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(),
319 bounds);
320 for (int i = 0; i < count; ++i) {
321 rb.glyphs[i] = i;
322 rb.pos[i] = SkIntToScalar(i);
323 }
324 } break;
Florin Malitaab54e732018-07-27 09:47:15 -0400325 case SkTextBlobRunIterator::kFull_Positioning: {
fmalitac6765d62014-08-21 15:03:04 -0700326 const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds);
327 for (int i = 0; i < count; ++i) {
328 rb.glyphs[i] = i;
329 rb.pos[i * 2] = SkIntToScalar(i);
330 rb.pos[i * 2 + 1] = -SkIntToScalar(i);
331 }
332 } break;
333 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400334 SK_ABORT("unhandled positioning value");
fmalitac6765d62014-08-21 15:03:04 -0700335 }
336 }
337};
338
339DEF_TEST(TextBlob_builder, reporter) {
fmalita51bf9572014-08-22 07:50:45 -0700340 TextBlobTester::TestBuilder(reporter);
341 TextBlobTester::TestBounds(reporter);
fmalitac6765d62014-08-21 15:03:04 -0700342}
fmalita32d6b872015-11-17 11:39:32 -0800343
344DEF_TEST(TextBlob_paint, reporter) {
345 TextBlobTester::TestPaintProps(reporter);
346}
halcanary4f0a23a2016-08-30 11:58:33 -0700347
348DEF_TEST(TextBlob_extended, reporter) {
349 SkTextBlobBuilder textBlobBuilder;
350 SkPaint paint;
351 const char text1[] = "Foo";
352 const char text2[] = "Bar";
353
354 int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr);
355 SkAutoTMalloc<uint16_t> glyphs(glyphCount);
356 (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get());
357 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
358
Cary Clarke12a0902018-08-09 10:07:33 -0400359 auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
halcanary4f0a23a2016-08-30 11:58:33 -0700360 paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
361 memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
362 memcpy(run.utf8text, text2, strlen(text2));
363 for (int i = 0; i < glyphCount; ++i) {
364 run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
365 }
fmalita37283c22016-09-13 10:00:23 -0700366 sk_sp<SkTextBlob> blob(textBlobBuilder.make());
halcanary4f0a23a2016-08-30 11:58:33 -0700367 REPORTER_ASSERT(reporter, blob);
368
369 for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
370 REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
371 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
372 REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
373 }
Florin Malitaab54e732018-07-27 09:47:15 -0400374 REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
halcanary4f0a23a2016-08-30 11:58:33 -0700375 REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
376 REPORTER_ASSERT(reporter, it.textSize() > 0);
377 REPORTER_ASSERT(reporter, it.clusters());
378 for (uint32_t i = 0; i < it.glyphCount(); ++i) {
379 REPORTER_ASSERT(reporter, i == it.clusters()[i]);
380 }
381 REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
382 }
383}
Mike Reedb99bedd2017-07-11 10:27:40 -0400384
385///////////////////////////////////////////////////////////////////////////////////////////////////
386#include "SkCanvas.h"
387#include "SkSurface.h"
388#include "SkTDArray.h"
389
390static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y,
391 sk_sp<SkTypeface> tf) {
392 SkPaint paint;
393 paint.setAntiAlias(true);
394 paint.setSubpixelText(true);
395 paint.setTextSize(16);
396 paint.setTypeface(tf);
397
398 int glyphCount = paint.textToGlyphs(text, strlen(text), nullptr);
399
400 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
401 SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(paint, glyphCount, x, y);
402
403 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
404 (void)paint.textToGlyphs(text, strlen(text), buffer.glyphs);
405}
406
407static sk_sp<SkImage> render(const SkTextBlob* blob) {
408 auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()),
409 SkScalarRoundToInt(blob->bounds().height()));
Mike Reed7e49d532017-07-11 15:07:08 -0400410 if (!surf) {
411 return nullptr; // bounds are empty?
412 }
Mike Reedb99bedd2017-07-11 10:27:40 -0400413 surf->getCanvas()->clear(SK_ColorWHITE);
414 surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint());
415 return surf->makeImageSnapshot();
416}
417
Cary Clark785586a2018-07-19 10:07:01 -0400418static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) {
419 auto array = (SkTDArray<SkTypeface*>*)ctx;
420 *array->append() = tf;
421 return sk_sp<SkData>(nullptr);
422}
423
424static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) {
425 auto array = (SkTDArray<SkTypeface*>*)ctx;
426 for (int i = 0; i < array->count(); ++i) {
427 auto result = (*array)[i];
428 if (result) {
429 (*array)[i] = nullptr;
430 return sk_ref_sp(result);
431 }
432 }
433 SkASSERT(false);
434 return sk_sp<SkTypeface>(nullptr);
435}
436
Mike Reedb99bedd2017-07-11 10:27:40 -0400437/*
438 * Build a blob with more than one typeface.
439 * Draw it into an offscreen,
440 * then serialize and deserialize,
441 * Then draw the new instance and assert it draws the same as the original.
442 */
443DEF_TEST(TextBlob_serialize, reporter) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500444 sk_sp<SkTextBlob> blob0 = []() {
Mike Reed8e74cbc2017-12-08 13:20:01 -0500445 sk_sp<SkTypeface> tf = SkTypeface::MakeDefault();
Mike Reedb99bedd2017-07-11 10:27:40 -0400446
Mike Reed4bcef3c2017-12-08 11:46:09 -0500447 SkTextBlobBuilder builder;
Mike Reed8e74cbc2017-12-08 13:20:01 -0500448 add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint
449 add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint
Mike Reed4bcef3c2017-12-08 11:46:09 -0500450 return builder.make();
451 }();
Mike Reedb99bedd2017-07-11 10:27:40 -0400452
453 SkTDArray<SkTypeface*> array;
Cary Clark785586a2018-07-19 10:07:01 -0400454 SkSerialProcs serializeProcs;
455 serializeProcs.fTypefaceProc = &SerializeTypeface;
456 serializeProcs.fTypefaceCtx = (void*) &array;
457 sk_sp<SkData> data = blob0->serialize(serializeProcs);
458 REPORTER_ASSERT(reporter, array.count() == 2);
459 SkDeserialProcs deserializeProcs;
460 deserializeProcs.fTypefaceProc = &DeserializeTypeface;
461 deserializeProcs.fTypefaceCtx = (void*) &array;
462 sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs);
Mike Reed7e49d532017-07-11 15:07:08 -0400463
464 sk_sp<SkImage> img0 = render(blob0.get());
Mike Reedb99bedd2017-07-11 10:27:40 -0400465 sk_sp<SkImage> img1 = render(blob1.get());
Mike Reed7e49d532017-07-11 15:07:08 -0400466 if (img0 && img1) {
Mike Reed4bcef3c2017-12-08 11:46:09 -0500467 REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get()));
Mike Reedb99bedd2017-07-11 10:27:40 -0400468 }
469}
Herb Derby4b3a5152018-07-17 16:10:30 -0400470
471DEF_TEST(TextBlob_MakeAsDrawText, reporter) {
472 SkPaint paint;
473 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
474 const char text[] = "Hello";
Herb Derbyc3bc69f2018-07-27 16:28:01 -0400475 auto blob = SkTextBlob::MakeFromText(text, strlen(text), paint);
Herb Derby4b3a5152018-07-17 16:10:30 -0400476
477 int runs = 0;
478 for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
479 REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text));
Florin Malitaab54e732018-07-27 09:47:15 -0400480 REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning);
Herb Derby4b3a5152018-07-17 16:10:30 -0400481 runs += 1;
482 }
483 REPORTER_ASSERT(reporter, runs == 1);
484
485}