blob: c37bf1f1f16b4c6a44be90edc91251d41d792566 [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
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000023DEF_TEST(Paint_copy, reporter) {
djsollen@google.comb44cd652011-12-01 17:09:21 +000024 SkPaint paint;
25 // set a few member variables
26 paint.setStyle(SkPaint::kStrokeAndFill_Style);
djsollen@google.comb44cd652011-12-01 17:09:21 +000027 paint.setStrokeWidth(SkIntToScalar(2));
28 // set a few pointers
Mike Reed1be1f8d2018-03-14 13:01:17 -040029 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070030 SkBlurMask::ConvertRadiusToSigma(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +000031
32 // copy the paint using the copy constructor and check they are the same
33 SkPaint copiedPaint = paint;
Mike Reed5f528e52019-01-28 10:57:28 -050034 REPORTER_ASSERT(reporter, paint.getHash() == copiedPaint.getHash());
robertphillips@google.comb2657412013-08-07 22:36:29 +000035 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000036
djsollen@google.comb44cd652011-12-01 17:09:21 +000037 // copy the paint using the equal operator and check they are the same
38 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +000039 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000040
djsollen@google.comb44cd652011-12-01 17:09:21 +000041 // clean the paint and check they are back to their initial states
42 SkPaint cleanPaint;
43 paint.reset();
44 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +000045 REPORTER_ASSERT(reporter, cleanPaint == paint);
46 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000047}
reed@android.coma0f5d152009-06-22 17:38:10 +000048
49// found and fixed for webkit: mishandling when we hit recursion limit on
50// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000051DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +000052 SkPath path, stroke;
53 SkPaint paint;
54
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000055 path.moveTo(460.2881309415525f,
56 303.250847066498f);
57 path.cubicTo(463.36378422175284f,
58 302.1169735073363f,
59 456.32239330810046f,
60 304.720354932878f,
61 453.15255460013304f,
62 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000063
reed@android.coma0f5d152009-06-22 17:38:10 +000064 SkRect fillR, strokeR;
65 fillR = path.getBounds();
66
67 paint.setStyle(SkPaint::kStroke_Style);
68 paint.setStrokeWidth(SkIntToScalar(2));
69 paint.getFillPath(path, &stroke);
70 strokeR = stroke.getBounds();
71
72 SkRect maxR = fillR;
Brian Osman116b33e2020-02-05 13:34:09 -050073 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
reed@android.coma0f5d152009-06-22 17:38:10 +000074 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -050075 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +000076 paint.getStrokeWidth();
77 maxR.inset(-inset, -inset);
78
79 // test that our stroke didn't explode
80 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
81}
82
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000083DEF_TEST(Paint_flattening, reporter) {
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000084 const SkPaint::Cap caps[] = {
85 SkPaint::kButt_Cap,
86 SkPaint::kRound_Cap,
87 SkPaint::kSquare_Cap,
88 };
89 const SkPaint::Join joins[] = {
90 SkPaint::kMiter_Join,
91 SkPaint::kRound_Join,
92 SkPaint::kBevel_Join,
93 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000094 const SkPaint::Style styles[] = {
95 SkPaint::kFill_Style,
96 SkPaint::kStroke_Style,
97 SkPaint::kStrokeAndFill_Style,
98 };
99
100#define FOR_SETUP(index, array, setter) \
101 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
Mike Reed74b76772019-01-04 14:01:06 -0500102 paint.setter(array[index]);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000103
104 SkPaint paint;
Mike Reed74b76772019-01-04 14:01:06 -0500105 paint.setAntiAlias(true);
106
107 // we don't serialize hinting or encoding -- soon to be removed from paint
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000108
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000109 FOR_SETUP(l, caps, setStrokeCap)
110 FOR_SETUP(m, joins, setStrokeJoin)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000111 FOR_SETUP(p, styles, setStyle)
112
brianosmanfad98562016-05-04 11:06:28 -0700113 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500114 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000115
mtkleinb4c899d2016-04-29 13:58:09 -0700116 SkAutoMalloc buf(writer.bytesWritten());
117 writer.writeToMemory(buf.get());
118 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000119
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000120 SkPaint paint2;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500121 SkPaintPriv::Unflatten(&paint2, reader, nullptr);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000122 REPORTER_ASSERT(reporter, paint2 == paint);
123
Mike Reed1de89c42021-01-30 09:10:41 -0500124 }}}
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000125#undef FOR_SETUP
126
127}
128
djsollen@google.com46348e22013-03-04 19:47:42 +0000129// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000130DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000131
Mike Reed2e6db182018-12-15 13:45:33 -0500132 SkFont font;
133 font.setSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000134
135 SkRect r;
136 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
137
138 // test that the rect was reset
Ben Wagner51e15a62019-05-07 15:38:46 -0400139 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000140 REPORTER_ASSERT(reporter, r.isEmpty());
141}
142
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000143#define ASSERT(expr) REPORTER_ASSERT(r, expr)
144
mtklein610a0152014-09-25 11:57:53 -0700145DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000146 SkPaint paint;
147 paint.setColor(0x00AABBCC);
reed374772b2016-10-05 17:33:02 -0700148 paint.setBlendMode(SkBlendMode::kModulate);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000149
brianosmanfad98562016-05-04 11:06:28 -0700150 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500151 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000152
mtkleinb4c899d2016-04-29 13:58:09 -0700153 SkAutoMalloc buf(writer.bytesWritten());
154 writer.writeToMemory(buf.get());
155 SkReadBuffer reader(buf.get(), writer.bytesWritten());
156
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000157 SkPaint other;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500158 SkPaintPriv::Unflatten(&other, reader, nullptr);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000159 ASSERT(reader.offset() == writer.bytesWritten());
160
161 // No matter the encoding, these must always hold.
162 ASSERT(other.getColor() == paint.getColor());
reed374772b2016-10-05 17:33:02 -0700163 ASSERT(other.getBlendMode() == paint.getBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000164}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700165
166DEF_TEST(Paint_getHash, r) {
167 // Try not to inspect the actual hash values in here.
168 // We might want to change the hash function.
169
170 SkPaint paint;
171 const uint32_t defaultHash = paint.getHash();
172
173 // Check that some arbitrary field affects the hash.
174 paint.setColor(0xFF00FF00);
175 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
176 paint.setColor(SK_ColorBLACK); // Reset to default value.
177 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
178
Mike Reed3c03c852019-01-28 15:02:01 +0000179 // This is part of fBitfields, the last field we hash.
Mike Reed5f528e52019-01-28 10:57:28 -0500180 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reed3c03c852019-01-28 15:02:01 +0000181 REPORTER_ASSERT(r, paint.getHash() != defaultHash);
Mike Reed5f528e52019-01-28 10:57:28 -0500182 paint.setBlendMode(SkBlendMode::kSrcOver);
mtkleinfb1fe4f2014-10-07 09:26:10 -0700183 REPORTER_ASSERT(r, paint.getHash() == defaultHash);
184}
reedf539b8c2014-11-11 12:51:33 -0800185
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500186#include "include/effects/SkColorMatrixFilter.h"
reedf539b8c2014-11-11 12:51:33 -0800187
188DEF_TEST(Paint_nothingToDraw, r) {
189 SkPaint paint;
190
191 REPORTER_ASSERT(r, !paint.nothingToDraw());
192 paint.setAlpha(0);
193 REPORTER_ASSERT(r, paint.nothingToDraw());
194
195 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700196 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800197 REPORTER_ASSERT(r, paint.nothingToDraw());
198
199 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700200 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800201
202 SkColorMatrix cm;
203 cm.setIdentity(); // does not change alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400204 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800205 REPORTER_ASSERT(r, paint.nothingToDraw());
206
Mike Reed68eb8c22019-05-02 11:31:43 -0400207 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400208 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800209 REPORTER_ASSERT(r, !paint.nothingToDraw());
210}
Mike Reed477fb912018-11-11 20:37:45 -0500211
Mike Reedc16abee2018-11-24 13:27:27 -0500212DEF_TEST(Font_getpos, r) {
213 SkFont font;
214 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
Ben Wagner51e15a62019-05-07 15:38:46 -0400215 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reedc16abee2018-11-24 13:27:27 -0500216 SkAutoTArray<uint16_t> glyphStorage(count);
217 uint16_t* glyphs = glyphStorage.get();
Ben Wagner51e15a62019-05-07 15:38:46 -0400218 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
Mike Reedc16abee2018-11-24 13:27:27 -0500219
220 SkAutoTArray<SkScalar> widthStorage(count);
221 SkAutoTArray<SkScalar> xposStorage(count);
222 SkAutoTArray<SkPoint> posStorage(count);
223
224 SkScalar* widths = widthStorage.get();
225 SkScalar* xpos = xposStorage.get();
226 SkPoint* pos = posStorage.get();
227
228 for (bool subpix : { false, true }) {
229 font.setSubpixel(subpix);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400230 for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
Mike Reedc16abee2018-11-24 13:27:27 -0500231 font.setHinting(hint);
232 for (auto size : { 1.0f, 12.0f, 100.0f }) {
233 font.setSize(size);
234
235 font.getWidths(glyphs, count, widths);
236 font.getXPos(glyphs, count, xpos, 10);
237 font.getPos(glyphs, count, pos, {10, 20});
238
239 auto nearly_eq = [](SkScalar a, SkScalar b) {
240 return SkScalarAbs(a - b) < 0.000001f;
241 };
242
243 SkScalar x = 10;
244 for (int i = 0; i < count; ++i) {
245 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
246 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
247 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
248 x += widths[i];
249 }
250 }
251 }
252 }
253}