blob: ba43b4dd694a59c16e3e8cfeadf8ea3b17791990 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkFont.h"
9#include "include/core/SkMaskFilter.h"
10#include "include/core/SkPath.h"
11#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTo.h"
13#include "include/utils/SkRandom.h"
14#include "src/core/SkAutoMalloc.h"
15#include "src/core/SkBlurMask.h"
16#include "src/core/SkPaintPriv.h"
17#include "src/core/SkReadBuffer.h"
18#include "src/core/SkWriteBuffer.h"
19#include "src/utils/SkUTF.h"
20#include "tests/Test.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040021#undef ASSERT
reed@google.combcb42ae2013-07-02 13:56:39 +000022
reed@google.com25b3bd52013-05-22 13:55:54 +000023// temparary api for bicubic, just be sure we can set/clear it
reed93a12152015-03-16 10:08:34 -070024DEF_TEST(Paint_filterQuality, reporter) {
reed@google.com9cfc83c2013-07-22 17:18:18 +000025 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000026
reed93a12152015-03-16 10:08:34 -070027 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000028
reed93a12152015-03-16 10:08:34 -070029 static const SkFilterQuality gQualitys[] = {
30 kNone_SkFilterQuality,
31 kLow_SkFilterQuality,
32 kMedium_SkFilterQuality,
33 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +000034 };
reed93a12152015-03-16 10:08:34 -070035 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
36 p0.setFilterQuality(gQualitys[i]);
37 REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +000038 p1 = p0;
reed93a12152015-03-16 10:08:34 -070039 REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +000040
41 p0.reset();
reed93a12152015-03-16 10:08:34 -070042 REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality());
reed@google.com9cfc83c2013-07-22 17:18:18 +000043 }
reed@google.com25b3bd52013-05-22 13:55:54 +000044}
45
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000046DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +000047 SkPaint paint;
48 // set a few member variables
49 paint.setStyle(SkPaint::kStrokeAndFill_Style);
djsollen@google.comb44cd652011-12-01 17:09:21 +000050 paint.setStrokeWidth(SkIntToScalar(2));
51 // set a few pointers
Mike Reed1be1f8d2018-03-14 13:01:17 -040052 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070053 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +000054
55 // copy the paint using the copy constructor and check they are the same
56 SkPaint copiedPaint = paint;
Mike Reed5f528e52019-01-28 10:57:28 -050057 REPORTER_ASSERT(reporter, paint.getHash() == copiedPaint.getHash());
robertphillips@google.comb2657412013-08-07 22:36:29 +000058 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000059
djsollen@google.comb44cd652011-12-01 17:09:21 +000060 // copy the paint using the equal operator and check they are the same
61 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +000062 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000063
djsollen@google.comb44cd652011-12-01 17:09:21 +000064 // clean the paint and check they are back to their initial states
65 SkPaint cleanPaint;
66 paint.reset();
67 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +000068 REPORTER_ASSERT(reporter, cleanPaint == paint);
69 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000070}
reed@android.coma0f5d152009-06-22 17:38:10 +000071
72// found and fixed for webkit: mishandling when we hit recursion limit on
73// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000074DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +000075 SkPath path, stroke;
76 SkPaint paint;
77
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000078 path.moveTo(460.2881309415525f,
79 303.250847066498f);
80 path.cubicTo(463.36378422175284f,
81 302.1169735073363f,
82 456.32239330810046f,
83 304.720354932878f,
84 453.15255460013304f,
85 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
reed@android.coma0f5d152009-06-22 17:38:10 +000087 SkRect fillR, strokeR;
88 fillR = path.getBounds();
89
90 paint.setStyle(SkPaint::kStroke_Style);
91 paint.setStrokeWidth(SkIntToScalar(2));
92 paint.getFillPath(path, &stroke);
93 strokeR = stroke.getBounds();
94
95 SkRect maxR = fillR;
Brian Osman116b33e2020-02-05 13:34:09 -050096 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
reed@android.coma0f5d152009-06-22 17:38:10 +000097 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -050098 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +000099 paint.getStrokeWidth();
100 maxR.inset(-inset, -inset);
101
102 // test that our stroke didn't explode
103 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
104}
105
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000106DEF_TEST(Paint_flattening, reporter) {
reed93a12152015-03-16 10:08:34 -0700107 const SkFilterQuality levels[] = {
108 kNone_SkFilterQuality,
109 kLow_SkFilterQuality,
110 kMedium_SkFilterQuality,
111 kHigh_SkFilterQuality,
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000112 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000113 const SkPaint::Cap caps[] = {
114 SkPaint::kButt_Cap,
115 SkPaint::kRound_Cap,
116 SkPaint::kSquare_Cap,
117 };
118 const SkPaint::Join joins[] = {
119 SkPaint::kMiter_Join,
120 SkPaint::kRound_Join,
121 SkPaint::kBevel_Join,
122 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000123 const SkPaint::Style styles[] = {
124 SkPaint::kFill_Style,
125 SkPaint::kStroke_Style,
126 SkPaint::kStrokeAndFill_Style,
127 };
128
129#define FOR_SETUP(index, array, setter) \
130 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
Mike Reed74b76772019-01-04 14:01:06 -0500131 paint.setter(array[index]);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000132
133 SkPaint paint;
Mike Reed74b76772019-01-04 14:01:06 -0500134 paint.setAntiAlias(true);
135
136 // we don't serialize hinting or encoding -- soon to be removed from paint
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000137
reed93a12152015-03-16 10:08:34 -0700138 FOR_SETUP(i, levels, setFilterQuality)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000139 FOR_SETUP(l, caps, setStrokeCap)
140 FOR_SETUP(m, joins, setStrokeJoin)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000141 FOR_SETUP(p, styles, setStyle)
142
brianosmanfad98562016-05-04 11:06:28 -0700143 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500144 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000145
mtkleinb4c899d2016-04-29 13:58:09 -0700146 SkAutoMalloc buf(writer.bytesWritten());
147 writer.writeToMemory(buf.get());
148 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000149
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000150 SkPaint paint2;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500151 SkPaintPriv::Unflatten(&paint2, reader, nullptr);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000152 REPORTER_ASSERT(reporter, paint2 == paint);
153
Mike Reed74b76772019-01-04 14:01:06 -0500154 }}}}
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000155#undef FOR_SETUP
156
157}
158
djsollen@google.com46348e22013-03-04 19:47:42 +0000159// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000160DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000161
Mike Reed2e6db182018-12-15 13:45:33 -0500162 SkFont font;
163 font.setSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000164
165 SkRect r;
166 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
167
168 // test that the rect was reset
Ben Wagner51e15a62019-05-07 15:38:46 -0400169 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000170 REPORTER_ASSERT(reporter, r.isEmpty());
171}
172
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000173#define ASSERT(expr) REPORTER_ASSERT(r, expr)
174
mtklein610a0152014-09-25 11:57:53 -0700175DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000176 SkPaint paint;
177 paint.setColor(0x00AABBCC);
reed374772b2016-10-05 17:33:02 -0700178 paint.setBlendMode(SkBlendMode::kModulate);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000179
brianosmanfad98562016-05-04 11:06:28 -0700180 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500181 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000182
mtkleinb4c899d2016-04-29 13:58:09 -0700183 SkAutoMalloc buf(writer.bytesWritten());
184 writer.writeToMemory(buf.get());
185 SkReadBuffer reader(buf.get(), writer.bytesWritten());
186
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000187 SkPaint other;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500188 SkPaintPriv::Unflatten(&other, reader, nullptr);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000189 ASSERT(reader.offset() == writer.bytesWritten());
190
191 // No matter the encoding, these must always hold.
192 ASSERT(other.getColor() == paint.getColor());
reed374772b2016-10-05 17:33:02 -0700193 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000194}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700195
196DEF_TEST(Paint_getHash, r) {
197 // Try not to inspect the actual hash values in here.
198 // We might want to change the hash function.
199
200 SkPaint paint;
201 const uint32_t defaultHash = paint.getHash();
202
203 // Check that some arbitrary field affects the hash.
204 paint.setColor(0xFF00FF00);
205 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
206 paint.setColor(SK_ColorBLACK); // Reset to default value.
207 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
208
Mike Reed3c03c852019-01-28 15:02:01 +0000209 // This is part of fBitfields, the last field we hash.
Mike Reed5f528e52019-01-28 10:57:28 -0500210 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reed3c03c852019-01-28 15:02:01 +0000211 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
Mike Reed5f528e52019-01-28 10:57:28 -0500212 paint.setBlendMode(SkBlendMode::kSrcOver);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700213 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
214}
reedf539b8c2014-11-11 12:51:33 -0800215
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500216#include "include/effects/SkColorMatrixFilter.h"
reedf539b8c2014-11-11 12:51:33 -0800217
218DEF_TEST(Paint_nothingToDraw, r) {
219 SkPaint paint;
220
221 REPORTER_ASSERT(r, !paint.nothingToDraw());
222 paint.setAlpha(0);
223 REPORTER_ASSERT(r, paint.nothingToDraw());
224
225 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700226 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800227 REPORTER_ASSERT(r, paint.nothingToDraw());
228
229 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700230 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800231
232 SkColorMatrix cm;
233 cm.setIdentity(); // does not change alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400234 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800235 REPORTER_ASSERT(r, paint.nothingToDraw());
236
Mike Reed68eb8c22019-05-02 11:31:43 -0400237 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400238 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800239 REPORTER_ASSERT(r, !paint.nothingToDraw());
240}
Mike Reed477fb912018-11-11 20:37:45 -0500241
Mike Reedc16abee2018-11-24 13:27:27 -0500242DEF_TEST(Font_getpos, r) {
243 SkFont font;
244 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
Ben Wagner51e15a62019-05-07 15:38:46 -0400245 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reedc16abee2018-11-24 13:27:27 -0500246 SkAutoTArray<uint16_t> glyphStorage(count);
247 uint16_t* glyphs = glyphStorage.get();
Ben Wagner51e15a62019-05-07 15:38:46 -0400248 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
Mike Reedc16abee2018-11-24 13:27:27 -0500249
250 SkAutoTArray<SkScalar> widthStorage(count);
251 SkAutoTArray<SkScalar> xposStorage(count);
252 SkAutoTArray<SkPoint> posStorage(count);
253
254 SkScalar* widths = widthStorage.get();
255 SkScalar* xpos = xposStorage.get();
256 SkPoint* pos = posStorage.get();
257
258 for (bool subpix : { false, true }) {
259 font.setSubpixel(subpix);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400260 for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
Mike Reedc16abee2018-11-24 13:27:27 -0500261 font.setHinting(hint);
262 for (auto size : { 1.0f, 12.0f, 100.0f }) {
263 font.setSize(size);
264
265 font.getWidths(glyphs, count, widths);
266 font.getXPos(glyphs, count, xpos, 10);
267 font.getPos(glyphs, count, pos, {10, 20});
268
269 auto nearly_eq = [](SkScalar a, SkScalar b) {
270 return SkScalarAbs(a - b) < 0.000001f;
271 };
272
273 SkScalar x = 10;
274 for (int i = 0; i < count; ++i) {
275 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
276 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
277 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
278 x += widths[i];
279 }
280 }
281 }
282 }
283}