blob: e33f91dac44b7320b64cf452585681b3ea99ee8e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@google.combcb42ae2013-07-02 13:56:39 +00007
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
Mike Reed477fb912018-11-11 20:37:45 -050010#include "SkFont.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040012#include "SkMaskFilter.h"
Cary Clark60ca8672018-03-06 15:09:27 -050013#include "SkPaintPriv.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "SkPath.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000015#include "SkRandom.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000016#include "SkReadBuffer.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040017#include "SkTo.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000018#include "SkTypeface.h"
Hal Canaryea60b952018-08-21 11:45:46 -040019#include "SkUTF.h"
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +000020#include "SkWriteBuffer.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040022#undef ASSERT
reed@google.combcb42ae2013-07-02 13:56:39 +000023
24static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
25 char* u8 = (char*)dst;
26 for (int i = 0; i < count; ++i) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040027 int n = SkToInt(SkUTF::ToUTF8(src[i], u8));
reed@google.combcb42ae2013-07-02 13:56:39 +000028 u8 += n;
29 }
30 return u8 - (char*)dst;
31}
32
33static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
34 uint16_t* u16 = (uint16_t*)dst;
35 for (int i = 0; i < count; ++i) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040036 int n = SkToInt(SkUTF::ToUTF16(src[i], u16));
reed@google.combcb42ae2013-07-02 13:56:39 +000037 u16 += n;
38 }
39 return (char*)u16 - (char*)dst;
40}
41
42static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
43 SkUnichar* u32 = (SkUnichar*)dst;
44 if (src != u32) {
45 memcpy(u32, src, count * sizeof(SkUnichar));
46 }
47 return count * sizeof(SkUnichar);
48}
49
50static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
Mike Reed97f3cc22018-12-03 09:45:17 -050051 SkTextEncoding enc = (SkTextEncoding)paint.getTextEncoding();
52 SkASSERT(kGlyphID_SkTextEncoding != enc);
reed@google.combcb42ae2013-07-02 13:56:39 +000053 return (SkTypeface::Encoding)enc;
54}
55
56static int find_first_zero(const uint16_t glyphs[], int count) {
57 for (int i = 0; i < count; ++i) {
58 if (0 == glyphs[i]) {
59 return i;
60 }
61 }
62 return count;
63}
64
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000065DEF_TEST(Paint_cmap, reporter) {
66 // need to implement charsToGlyphs on other backends (e.g. linux, win)
67 // before we can run this tests everywhere
68 return;
69
reed@google.combcb42ae2013-07-02 13:56:39 +000070 static const int NGLYPHS = 64;
71
72 SkUnichar src[NGLYPHS];
73 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
74
75 static const struct {
76 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
Mike Reed97f3cc22018-12-03 09:45:17 -050077 SkTextEncoding fEncoding;
reed@google.combcb42ae2013-07-02 13:56:39 +000078 } gRec[] = {
Mike Reed97f3cc22018-12-03 09:45:17 -050079 { uni_to_utf8, kUTF8_SkTextEncoding },
80 { uni_to_utf16, kUTF16_SkTextEncoding },
81 { uni_to_utf32, kUTF32_SkTextEncoding },
reed@google.combcb42ae2013-07-02 13:56:39 +000082 };
83
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000084 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000085 SkPaint paint;
bungeman13b9c952016-05-12 10:09:30 -070086 paint.setTypeface(SkTypeface::MakeDefault());
reed@google.combcb42ae2013-07-02 13:56:39 +000087 SkTypeface* face = paint.getTypeface();
88
89 for (int i = 0; i < 1000; ++i) {
90 // generate some random text
91 for (int j = 0; j < NGLYPHS; ++j) {
92 src[j] = ' ' + j;
93 }
94 // inject some random chars, to sometimes abort early
95 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000096
reed@google.combcb42ae2013-07-02 13:56:39 +000097 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
98 paint.setTextEncoding(gRec[k].fEncoding);
99
100 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000101
reed@google.combcb42ae2013-07-02 13:56:39 +0000102 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000103
reed@google.combcb42ae2013-07-02 13:56:39 +0000104 bool contains = paint.containsText(dst, len);
105 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
106 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
107 int index = find_first_zero(glyphs1, NGLYPHS);
108
109 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
110 REPORTER_ASSERT(reporter, index == first);
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000111 REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
reed@google.combcb42ae2013-07-02 13:56:39 +0000112 if (contains) {
113 REPORTER_ASSERT(reporter, NGLYPHS == first);
114 } else {
115 REPORTER_ASSERT(reporter, NGLYPHS > first);
116 }
117 }
118 }
119}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000120
reed@google.com25b3bd52013-05-22 13:55:54 +0000121// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -0700122DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000123 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000124
reed93a12152015-03-16 10:08:34 -0700125 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000126
reed93a12152015-03-16 10:08:34 -0700127 static const SkFilterQuality gQualitys[] = {
128 kNone_SkFilterQuality,
129 kLow_SkFilterQuality,
130 kMedium_SkFilterQuality,
131 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000132 };
reed93a12152015-03-16 10:08:34 -0700133 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
134 p0.setFilterQuality(gQualitys[i]);
135 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000136 p1 = p0;
reed93a12152015-03-16 10:08:34 -0700137 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000138
139 p0.reset();
reed93a12152015-03-16 10:08:34 -0700140 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000141 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000142}
143
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000144DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +0000145 SkPaint paint;
146 // set a few member variables
147 paint.setStyle(SkPaint::kStrokeAndFill_Style);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000148 paint.setStrokeWidth(SkIntToScalar(2));
149 // set a few pointers
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000150 SkLayerDrawLooper::Builder looperBuilder;
reed7b380d02016-03-21 13:25:16 -0700151 paint.setLooper(looperBuilder.detach());
Mike Reed1be1f8d2018-03-14 13:01:17 -0400152 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -0700153 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000154
155 // copy the paint using the copy constructor and check they are the same
156 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000157 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000158
djsollen@google.comb44cd652011-12-01 17:09:21 +0000159 // copy the paint using the equal operator and check they are the same
160 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000161 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000162
djsollen@google.comb44cd652011-12-01 17:09:21 +0000163 // clean the paint and check they are back to their initial states
164 SkPaint cleanPaint;
165 paint.reset();
166 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000167 REPORTER_ASSERT(reporter, cleanPaint == paint);
168 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000169}
reed@android.coma0f5d152009-06-22 17:38:10 +0000170
171// found and fixed for webkit: mishandling when we hit recursion limit on
172// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000173DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000174 SkPath path, stroke;
175 SkPaint paint;
176
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000177 path.moveTo(460.2881309415525f,
178 303.250847066498f);
179 path.cubicTo(463.36378422175284f,
180 302.1169735073363f,
181 456.32239330810046f,
182 304.720354932878f,
183 453.15255460013304f,
184 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
reed@android.coma0f5d152009-06-22 17:38:10 +0000186 SkRect fillR, strokeR;
187 fillR = path.getBounds();
188
189 paint.setStyle(SkPaint::kStroke_Style);
190 paint.setStrokeWidth(SkIntToScalar(2));
191 paint.getFillPath(path, &stroke);
192 strokeR = stroke.getBounds();
193
194 SkRect maxR = fillR;
195 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
196 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -0500197 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +0000198 paint.getStrokeWidth();
199 maxR.inset(-inset, -inset);
200
201 // test that our stroke didn't explode
202 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
203}
204
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000205DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700206 const SkFilterQuality levels[] = {
207 kNone_SkFilterQuality,
208 kLow_SkFilterQuality,
209 kMedium_SkFilterQuality,
210 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000211 };
Mike Reed9edbf422018-11-07 19:54:33 -0500212 const SkFontHinting hinting[] = {
213 kNo_SkFontHinting,
214 kSlight_SkFontHinting,
215 kNormal_SkFontHinting,
216 kFull_SkFontHinting,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000217 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000218 const SkPaint::Cap caps[] = {
219 SkPaint::kButt_Cap,
220 SkPaint::kRound_Cap,
221 SkPaint::kSquare_Cap,
222 };
223 const SkPaint::Join joins[] = {
224 SkPaint::kMiter_Join,
225 SkPaint::kRound_Join,
226 SkPaint::kBevel_Join,
227 };
Mike Reed97f3cc22018-12-03 09:45:17 -0500228 const SkTextEncoding encodings[] = {
229 kUTF8_SkTextEncoding,
230 kUTF16_SkTextEncoding,
231 kUTF32_SkTextEncoding,
232 kGlyphID_SkTextEncoding,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000233 };
234 const SkPaint::Style styles[] = {
235 SkPaint::kFill_Style,
236 SkPaint::kStroke_Style,
237 SkPaint::kStrokeAndFill_Style,
238 };
239
240#define FOR_SETUP(index, array, setter) \
241 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
242 paint.setter(array[index]); \
243
244 SkPaint paint;
245 paint.setFlags(0x1234);
246
reed93a12152015-03-16 10:08:34 -0700247 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000248 FOR_SETUP(j, hinting, setHinting)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000249 FOR_SETUP(l, caps, setStrokeCap)
250 FOR_SETUP(m, joins, setStrokeJoin)
251 FOR_SETUP(n, encodings, setTextEncoding)
252 FOR_SETUP(p, styles, setStyle)
253
brianosmanfad98562016-05-04 11:06:28 -0700254 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500255 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000256
mtkleinb4c899d2016-04-29 13:58:09 -0700257 SkAutoMalloc buf(writer.bytesWritten());
258 writer.writeToMemory(buf.get());
259 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000260
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000261 SkPaint paint2;
Cary Clark60ca8672018-03-06 15:09:27 -0500262 SkPaintPriv::Unflatten(&paint2, reader);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000263 REPORTER_ASSERT(reporter, paint2 == paint);
264
Mike Reed6e24cd32018-10-27 14:39:00 +0000265 }}}}}}
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000266#undef FOR_SETUP
267
268}
269
djsollen@google.com46348e22013-03-04 19:47:42 +0000270// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000271DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000272
273 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000274 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000275
276 SkRect r;
277 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
278
279 // test that the rect was reset
reed99ae8812014-08-26 11:30:01 -0700280 paint.measureText("", 0, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000281 REPORTER_ASSERT(reporter, r.isEmpty());
282}
283
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000284#define ASSERT(expr) REPORTER_ASSERT(r, expr)
285
mtklein610a0152014-09-25 11:57:53 -0700286DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000287 SkPaint paint;
288 paint.setColor(0x00AABBCC);
mtkleinee902cd2014-09-22 11:40:33 -0700289 paint.setTextScaleX(1.0f); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000290 paint.setTextSize(19);
reed374772b2016-10-05 17:33:02 -0700291 paint.setBlendMode(SkBlendMode::kModulate);
halcanary96fcdcc2015-08-27 07:41:13 -0700292 paint.setLooper(nullptr); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000293
brianosmanfad98562016-05-04 11:06:28 -0700294 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500295 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000296
mtkleinb4c899d2016-04-29 13:58:09 -0700297 SkAutoMalloc buf(writer.bytesWritten());
298 writer.writeToMemory(buf.get());
299 SkReadBuffer reader(buf.get(), writer.bytesWritten());
300
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000301 SkPaint other;
Cary Clark60ca8672018-03-06 15:09:27 -0500302 SkPaintPriv::Unflatten(&other, reader);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000303 ASSERT(reader.offset() == writer.bytesWritten());
304
305 // No matter the encoding, these must always hold.
306 ASSERT(other.getColor() == paint.getColor());
307 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
308 ASSERT(other.getTextSize() == paint.getTextSize());
309 ASSERT(other.getLooper() == paint.getLooper());
reed374772b2016-10-05 17:33:02 -0700310 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000311}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700312
313DEF_TEST(Paint_getHash, r) {
314 // Try not to inspect the actual hash values in here.
315 // We might want to change the hash function.
316
317 SkPaint paint;
318 const uint32_t defaultHash = paint.getHash();
319
320 // Check that some arbitrary field affects the hash.
321 paint.setColor(0xFF00FF00);
322 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
323 paint.setColor(SK_ColorBLACK); // Reset to default value.
324 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
325
326 // SkTypeface is the first field we hash, so test it specially.
bungeman13b9c952016-05-12 10:09:30 -0700327 paint.setTypeface(SkTypeface::MakeDefault());
mtkleinfb1fe4f2014-10-07 09:26:10 -0700328 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
halcanary96fcdcc2015-08-27 07:41:13 -0700329 paint.setTypeface(nullptr);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700330 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
331
332 // This is part of fBitfields, the last field we hash.
Mike Reed9edbf422018-11-07 19:54:33 -0500333 paint.setHinting(kSlight_SkFontHinting);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700334 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
Mike Reed9edbf422018-11-07 19:54:33 -0500335 paint.setHinting(kNormal_SkFontHinting);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700336 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
337}
reedf539b8c2014-11-11 12:51:33 -0800338
339#include "SkColorMatrixFilter.h"
340
341DEF_TEST(Paint_nothingToDraw, r) {
342 SkPaint paint;
343
344 REPORTER_ASSERT(r, !paint.nothingToDraw());
345 paint.setAlpha(0);
346 REPORTER_ASSERT(r, paint.nothingToDraw());
347
348 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700349 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800350 REPORTER_ASSERT(r, paint.nothingToDraw());
351
352 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700353 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800354
355 SkColorMatrix cm;
356 cm.setIdentity(); // does not change alpha
reedd053ce92016-03-22 10:17:23 -0700357 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800358 REPORTER_ASSERT(r, paint.nothingToDraw());
359
360 cm.postTranslate(0, 0, 0, 1); // wacks alpha
reedd053ce92016-03-22 10:17:23 -0700361 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800362 REPORTER_ASSERT(r, !paint.nothingToDraw());
363}
Mike Reed477fb912018-11-11 20:37:45 -0500364
365DEF_TEST(Paint_getwidths, r) {
366 SkPaint paint;
367 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
368 int count = paint.countText(text, strlen(text));
369 SkAutoTArray<uint16_t> glyphStorage(count * 2);
370 uint16_t* glyphs = glyphStorage.get();
371
372 (void)paint.textToGlyphs(text, strlen(text), glyphs);
Mike Reed97f3cc22018-12-03 09:45:17 -0500373 paint.setTextEncoding(kGlyphID_SkTextEncoding);
Mike Reed477fb912018-11-11 20:37:45 -0500374
375 SkAutoTArray<SkScalar> widthStorage(count * 2);
376 SkScalar* widths = widthStorage.get();
377 SkAutoTArray<SkRect> rectStorage(count * 2);
378 SkRect* bounds = rectStorage.get();
379
380 for (bool subpix : { false, true }) {
381 paint.setSubpixelText(subpix);
382 for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) {
383 paint.setHinting(hint);
384 for (auto size : { 1.0f, 12.0f, 100.0f }) {
385 paint.setTextSize(size);
386 paint.getTextWidths(glyphs, count * 2, widths, bounds);
387
388 SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
389 font.getWidths(glyphs, count, widths + count, bounds + count);
390
391 for (int i = 0; i < count; ++i) {
392 REPORTER_ASSERT(r, widths[i] == widths[i + count]);
393 REPORTER_ASSERT(r, bounds[i] == bounds[i + count]);
394 }
395 }
396 }
397 }
398}
Mike Reedc16abee2018-11-24 13:27:27 -0500399
400DEF_TEST(Font_getpos, r) {
401 SkFont font;
402 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
403 int count = font.countText(text, strlen(text), kUTF8_SkTextEncoding);
404 SkAutoTArray<uint16_t> glyphStorage(count);
405 uint16_t* glyphs = glyphStorage.get();
406 (void)font.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, glyphs, count);
407
408 SkAutoTArray<SkScalar> widthStorage(count);
409 SkAutoTArray<SkScalar> xposStorage(count);
410 SkAutoTArray<SkPoint> posStorage(count);
411
412 SkScalar* widths = widthStorage.get();
413 SkScalar* xpos = xposStorage.get();
414 SkPoint* pos = posStorage.get();
415
416 for (bool subpix : { false, true }) {
417 font.setSubpixel(subpix);
418 for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) {
419 font.setHinting(hint);
420 for (auto size : { 1.0f, 12.0f, 100.0f }) {
421 font.setSize(size);
422
423 font.getWidths(glyphs, count, widths);
424 font.getXPos(glyphs, count, xpos, 10);
425 font.getPos(glyphs, count, pos, {10, 20});
426
427 auto nearly_eq = [](SkScalar a, SkScalar b) {
428 return SkScalarAbs(a - b) < 0.000001f;
429 };
430
431 SkScalar x = 10;
432 for (int i = 0; i < count; ++i) {
433 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
434 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
435 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
436 x += widths[i];
437 }
438 }
439 }
440 }
441}