blob: 36eea357f38f265bd5b3e50f7d6ce6406b4e32f4 [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
reed@google.combcb42ae2013-07-02 13:56:39 +000050static int find_first_zero(const uint16_t glyphs[], int count) {
51 for (int i = 0; i < count; ++i) {
52 if (0 == glyphs[i]) {
53 return i;
54 }
55 }
56 return count;
57}
58
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000059DEF_TEST(Paint_cmap, reporter) {
60 // need to implement charsToGlyphs on other backends (e.g. linux, win)
61 // before we can run this tests everywhere
62 return;
63
reed@google.combcb42ae2013-07-02 13:56:39 +000064 static const int NGLYPHS = 64;
65
66 SkUnichar src[NGLYPHS];
67 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
68
69 static const struct {
70 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
Mike Reed97f3cc22018-12-03 09:45:17 -050071 SkTextEncoding fEncoding;
reed@google.combcb42ae2013-07-02 13:56:39 +000072 } gRec[] = {
Mike Reed97f3cc22018-12-03 09:45:17 -050073 { uni_to_utf8, kUTF8_SkTextEncoding },
74 { uni_to_utf16, kUTF16_SkTextEncoding },
75 { uni_to_utf32, kUTF32_SkTextEncoding },
reed@google.combcb42ae2013-07-02 13:56:39 +000076 };
77
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000078 SkRandom rand;
Mike Reedab8f2972018-12-05 13:20:29 -050079 SkFont font;
80 font.setTypeface(SkTypeface::MakeDefault());
81 SkTypeface* face = font.getTypeface();
reed@google.combcb42ae2013-07-02 13:56:39 +000082
83 for (int i = 0; i < 1000; ++i) {
84 // generate some random text
85 for (int j = 0; j < NGLYPHS; ++j) {
86 src[j] = ' ' + j;
87 }
88 // inject some random chars, to sometimes abort early
89 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000090
reed@google.combcb42ae2013-07-02 13:56:39 +000091 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
reed@google.combcb42ae2013-07-02 13:56:39 +000092 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000093
reed@google.combcb42ae2013-07-02 13:56:39 +000094 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000095
Mike Reedab8f2972018-12-05 13:20:29 -050096 bool contains = font.containsText(dst, len, gRec[k].fEncoding);
97 int nglyphs = font.textToGlyphs(dst, len, gRec[k].fEncoding, glyphs0, NGLYPHS);
98 int first = face->charsToGlyphs(dst, (SkTypeface::Encoding)gRec[k].fEncoding,
99 glyphs1, NGLYPHS);
reed@google.combcb42ae2013-07-02 13:56:39 +0000100 int index = find_first_zero(glyphs1, NGLYPHS);
101
102 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
103 REPORTER_ASSERT(reporter, index == first);
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000104 REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
reed@google.combcb42ae2013-07-02 13:56:39 +0000105 if (contains) {
106 REPORTER_ASSERT(reporter, NGLYPHS == first);
107 } else {
108 REPORTER_ASSERT(reporter, NGLYPHS > first);
109 }
110 }
111 }
112}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000113
reed@google.com25b3bd52013-05-22 13:55:54 +0000114// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -0700115DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000116 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000117
reed93a12152015-03-16 10:08:34 -0700118 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000119
reed93a12152015-03-16 10:08:34 -0700120 static const SkFilterQuality gQualitys[] = {
121 kNone_SkFilterQuality,
122 kLow_SkFilterQuality,
123 kMedium_SkFilterQuality,
124 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000125 };
reed93a12152015-03-16 10:08:34 -0700126 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
127 p0.setFilterQuality(gQualitys[i]);
128 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000129 p1 = p0;
reed93a12152015-03-16 10:08:34 -0700130 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000131
132 p0.reset();
reed93a12152015-03-16 10:08:34 -0700133 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +0000134 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000135}
136
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000137DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +0000138 SkPaint paint;
139 // set a few member variables
140 paint.setStyle(SkPaint::kStrokeAndFill_Style);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000141 paint.setStrokeWidth(SkIntToScalar(2));
142 // set a few pointers
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000143 SkLayerDrawLooper::Builder looperBuilder;
reed7b380d02016-03-21 13:25:16 -0700144 paint.setLooper(looperBuilder.detach());
Mike Reed1be1f8d2018-03-14 13:01:17 -0400145 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -0700146 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000147
148 // copy the paint using the copy constructor and check they are the same
149 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000150 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000151
djsollen@google.comb44cd652011-12-01 17:09:21 +0000152 // copy the paint using the equal operator and check they are the same
153 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000154 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000155
djsollen@google.comb44cd652011-12-01 17:09:21 +0000156 // clean the paint and check they are back to their initial states
157 SkPaint cleanPaint;
158 paint.reset();
159 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000160 REPORTER_ASSERT(reporter, cleanPaint == paint);
161 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000162}
reed@android.coma0f5d152009-06-22 17:38:10 +0000163
164// found and fixed for webkit: mishandling when we hit recursion limit on
165// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000166DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000167 SkPath path, stroke;
168 SkPaint paint;
169
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000170 path.moveTo(460.2881309415525f,
171 303.250847066498f);
172 path.cubicTo(463.36378422175284f,
173 302.1169735073363f,
174 456.32239330810046f,
175 304.720354932878f,
176 453.15255460013304f,
177 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178
reed@android.coma0f5d152009-06-22 17:38:10 +0000179 SkRect fillR, strokeR;
180 fillR = path.getBounds();
181
182 paint.setStyle(SkPaint::kStroke_Style);
183 paint.setStrokeWidth(SkIntToScalar(2));
184 paint.getFillPath(path, &stroke);
185 strokeR = stroke.getBounds();
186
187 SkRect maxR = fillR;
188 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
189 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -0500190 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +0000191 paint.getStrokeWidth();
192 maxR.inset(-inset, -inset);
193
194 // test that our stroke didn't explode
195 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
196}
197
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000198DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700199 const SkFilterQuality levels[] = {
200 kNone_SkFilterQuality,
201 kLow_SkFilterQuality,
202 kMedium_SkFilterQuality,
203 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000204 };
Mike Reed9edbf422018-11-07 19:54:33 -0500205 const SkFontHinting hinting[] = {
206 kNo_SkFontHinting,
207 kSlight_SkFontHinting,
208 kNormal_SkFontHinting,
209 kFull_SkFontHinting,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000210 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000211 const SkPaint::Cap caps[] = {
212 SkPaint::kButt_Cap,
213 SkPaint::kRound_Cap,
214 SkPaint::kSquare_Cap,
215 };
216 const SkPaint::Join joins[] = {
217 SkPaint::kMiter_Join,
218 SkPaint::kRound_Join,
219 SkPaint::kBevel_Join,
220 };
Mike Reed97f3cc22018-12-03 09:45:17 -0500221 const SkTextEncoding encodings[] = {
222 kUTF8_SkTextEncoding,
223 kUTF16_SkTextEncoding,
224 kUTF32_SkTextEncoding,
225 kGlyphID_SkTextEncoding,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000226 };
227 const SkPaint::Style styles[] = {
228 SkPaint::kFill_Style,
229 SkPaint::kStroke_Style,
230 SkPaint::kStrokeAndFill_Style,
231 };
232
233#define FOR_SETUP(index, array, setter) \
234 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
235 paint.setter(array[index]); \
236
237 SkPaint paint;
238 paint.setFlags(0x1234);
239
reed93a12152015-03-16 10:08:34 -0700240 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000241 FOR_SETUP(j, hinting, setHinting)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000242 FOR_SETUP(l, caps, setStrokeCap)
243 FOR_SETUP(m, joins, setStrokeJoin)
244 FOR_SETUP(n, encodings, setTextEncoding)
245 FOR_SETUP(p, styles, setStyle)
246
brianosmanfad98562016-05-04 11:06:28 -0700247 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500248 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000249
mtkleinb4c899d2016-04-29 13:58:09 -0700250 SkAutoMalloc buf(writer.bytesWritten());
251 writer.writeToMemory(buf.get());
252 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000253
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000254 SkPaint paint2;
Cary Clark60ca8672018-03-06 15:09:27 -0500255 SkPaintPriv::Unflatten(&paint2, reader);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000256 REPORTER_ASSERT(reporter, paint2 == paint);
257
Mike Reed6e24cd32018-10-27 14:39:00 +0000258 }}}}}}
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000259#undef FOR_SETUP
260
261}
262
djsollen@google.com46348e22013-03-04 19:47:42 +0000263// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000264DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000265
266 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000267 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000268
269 SkRect r;
270 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
271
272 // test that the rect was reset
reed99ae8812014-08-26 11:30:01 -0700273 paint.measureText("", 0, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000274 REPORTER_ASSERT(reporter, r.isEmpty());
275}
276
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000277#define ASSERT(expr) REPORTER_ASSERT(r, expr)
278
mtklein610a0152014-09-25 11:57:53 -0700279DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000280 SkPaint paint;
281 paint.setColor(0x00AABBCC);
mtkleinee902cd2014-09-22 11:40:33 -0700282 paint.setTextScaleX(1.0f); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000283 paint.setTextSize(19);
reed374772b2016-10-05 17:33:02 -0700284 paint.setBlendMode(SkBlendMode::kModulate);
halcanary96fcdcc2015-08-27 07:41:13 -0700285 paint.setLooper(nullptr); // Default value, ignored.
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000286
brianosmanfad98562016-05-04 11:06:28 -0700287 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500288 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000289
mtkleinb4c899d2016-04-29 13:58:09 -0700290 SkAutoMalloc buf(writer.bytesWritten());
291 writer.writeToMemory(buf.get());
292 SkReadBuffer reader(buf.get(), writer.bytesWritten());
293
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000294 SkPaint other;
Cary Clark60ca8672018-03-06 15:09:27 -0500295 SkPaintPriv::Unflatten(&other, reader);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000296 ASSERT(reader.offset() == writer.bytesWritten());
297
298 // No matter the encoding, these must always hold.
299 ASSERT(other.getColor() == paint.getColor());
300 ASSERT(other.getTextScaleX() == paint.getTextScaleX());
301 ASSERT(other.getTextSize() == paint.getTextSize());
302 ASSERT(other.getLooper() == paint.getLooper());
reed374772b2016-10-05 17:33:02 -0700303 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000304}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700305
306DEF_TEST(Paint_getHash, r) {
307 // Try not to inspect the actual hash values in here.
308 // We might want to change the hash function.
309
310 SkPaint paint;
311 const uint32_t defaultHash = paint.getHash();
312
313 // Check that some arbitrary field affects the hash.
314 paint.setColor(0xFF00FF00);
315 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
316 paint.setColor(SK_ColorBLACK); // Reset to default value.
317 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
318
319 // SkTypeface is the first field we hash, so test it specially.
bungeman13b9c952016-05-12 10:09:30 -0700320 paint.setTypeface(SkTypeface::MakeDefault());
mtkleinfb1fe4f2014-10-07 09:26:10 -0700321 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
halcanary96fcdcc2015-08-27 07:41:13 -0700322 paint.setTypeface(nullptr);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700323 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
324
325 // This is part of fBitfields, the last field we hash.
Mike Reed9edbf422018-11-07 19:54:33 -0500326 paint.setHinting(kSlight_SkFontHinting);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700327 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
Mike Reed9edbf422018-11-07 19:54:33 -0500328 paint.setHinting(kNormal_SkFontHinting);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700329 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
330}
reedf539b8c2014-11-11 12:51:33 -0800331
332#include "SkColorMatrixFilter.h"
333
334DEF_TEST(Paint_nothingToDraw, r) {
335 SkPaint paint;
336
337 REPORTER_ASSERT(r, !paint.nothingToDraw());
338 paint.setAlpha(0);
339 REPORTER_ASSERT(r, paint.nothingToDraw());
340
341 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700342 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800343 REPORTER_ASSERT(r, paint.nothingToDraw());
344
345 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700346 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800347
348 SkColorMatrix cm;
349 cm.setIdentity(); // does not change alpha
reedd053ce92016-03-22 10:17:23 -0700350 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800351 REPORTER_ASSERT(r, paint.nothingToDraw());
352
353 cm.postTranslate(0, 0, 0, 1); // wacks alpha
reedd053ce92016-03-22 10:17:23 -0700354 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf539b8c2014-11-11 12:51:33 -0800355 REPORTER_ASSERT(r, !paint.nothingToDraw());
356}
Mike Reed477fb912018-11-11 20:37:45 -0500357
358DEF_TEST(Paint_getwidths, r) {
359 SkPaint paint;
360 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
361 int count = paint.countText(text, strlen(text));
362 SkAutoTArray<uint16_t> glyphStorage(count * 2);
363 uint16_t* glyphs = glyphStorage.get();
364
365 (void)paint.textToGlyphs(text, strlen(text), glyphs);
Mike Reed97f3cc22018-12-03 09:45:17 -0500366 paint.setTextEncoding(kGlyphID_SkTextEncoding);
Mike Reed477fb912018-11-11 20:37:45 -0500367
368 SkAutoTArray<SkScalar> widthStorage(count * 2);
369 SkScalar* widths = widthStorage.get();
370 SkAutoTArray<SkRect> rectStorage(count * 2);
371 SkRect* bounds = rectStorage.get();
372
373 for (bool subpix : { false, true }) {
374 paint.setSubpixelText(subpix);
375 for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) {
376 paint.setHinting(hint);
377 for (auto size : { 1.0f, 12.0f, 100.0f }) {
378 paint.setTextSize(size);
379 paint.getTextWidths(glyphs, count * 2, widths, bounds);
380
381 SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
382 font.getWidths(glyphs, count, widths + count, bounds + count);
383
384 for (int i = 0; i < count; ++i) {
385 REPORTER_ASSERT(r, widths[i] == widths[i + count]);
386 REPORTER_ASSERT(r, bounds[i] == bounds[i + count]);
387 }
388 }
389 }
390 }
391}
Mike Reedc16abee2018-11-24 13:27:27 -0500392
393DEF_TEST(Font_getpos, r) {
394 SkFont font;
395 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
396 int count = font.countText(text, strlen(text), kUTF8_SkTextEncoding);
397 SkAutoTArray<uint16_t> glyphStorage(count);
398 uint16_t* glyphs = glyphStorage.get();
399 (void)font.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, glyphs, count);
400
401 SkAutoTArray<SkScalar> widthStorage(count);
402 SkAutoTArray<SkScalar> xposStorage(count);
403 SkAutoTArray<SkPoint> posStorage(count);
404
405 SkScalar* widths = widthStorage.get();
406 SkScalar* xpos = xposStorage.get();
407 SkPoint* pos = posStorage.get();
408
409 for (bool subpix : { false, true }) {
410 font.setSubpixel(subpix);
411 for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) {
412 font.setHinting(hint);
413 for (auto size : { 1.0f, 12.0f, 100.0f }) {
414 font.setSize(size);
415
416 font.getWidths(glyphs, count, widths);
417 font.getXPos(glyphs, count, xpos, 10);
418 font.getPos(glyphs, count, pos, {10, 20});
419
420 auto nearly_eq = [](SkScalar a, SkScalar b) {
421 return SkScalarAbs(a - b) < 0.000001f;
422 };
423
424 SkScalar x = 10;
425 for (int i = 0; i < count; ++i) {
426 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
427 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
428 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
429 x += widths[i];
430 }
431 }
432 }
433 }
434}