blob: 2db0ed6b80bd2a7fa857b3aef524f6cfd880d990 [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;
robertphillips@google.comb2657412013-08-07 22:36:29 +000034 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000035
djsollen@google.comb44cd652011-12-01 17:09:21 +000036 // copy the paint using the equal operator and check they are the same
37 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +000038 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000039
djsollen@google.comb44cd652011-12-01 17:09:21 +000040 // clean the paint and check they are back to their initial states
41 SkPaint cleanPaint;
42 paint.reset();
43 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +000044 REPORTER_ASSERT(reporter, cleanPaint == paint);
45 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +000046}
reed@android.coma0f5d152009-06-22 17:38:10 +000047
48// found and fixed for webkit: mishandling when we hit recursion limit on
49// mostly degenerate cubic flatness test
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +000050DEF_TEST(Paint_regression_cubic, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +000051 SkPath path, stroke;
52 SkPaint paint;
53
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054 path.moveTo(460.2881309415525f,
55 303.250847066498f);
56 path.cubicTo(463.36378422175284f,
57 302.1169735073363f,
58 456.32239330810046f,
59 304.720354932878f,
60 453.15255460013304f,
61 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@android.coma0f5d152009-06-22 17:38:10 +000063 SkRect fillR, strokeR;
64 fillR = path.getBounds();
65
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(SkIntToScalar(2));
68 paint.getFillPath(path, &stroke);
69 strokeR = stroke.getBounds();
70
71 SkRect maxR = fillR;
Brian Osman116b33e2020-02-05 13:34:09 -050072 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
reed@android.coma0f5d152009-06-22 17:38:10 +000073 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
Mike Reeddf85c382017-02-14 10:59:19 -050074 paint.getStrokeWidth() * miter :
reed@android.coma0f5d152009-06-22 17:38:10 +000075 paint.getStrokeWidth();
76 maxR.inset(-inset, -inset);
77
78 // test that our stroke didn't explode
79 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
80}
81
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000082DEF_TEST(Paint_flattening, reporter) {
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000083 const SkPaint::Cap caps[] = {
84 SkPaint::kButt_Cap,
85 SkPaint::kRound_Cap,
86 SkPaint::kSquare_Cap,
87 };
88 const SkPaint::Join joins[] = {
89 SkPaint::kMiter_Join,
90 SkPaint::kRound_Join,
91 SkPaint::kBevel_Join,
92 };
commit-bot@chromium.org85faf502014-04-16 12:58:02 +000093 const SkPaint::Style styles[] = {
94 SkPaint::kFill_Style,
95 SkPaint::kStroke_Style,
96 SkPaint::kStrokeAndFill_Style,
97 };
98
99#define FOR_SETUP(index, array, setter) \
100 for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \
Mike Reed74b76772019-01-04 14:01:06 -0500101 paint.setter(array[index]);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000102
103 SkPaint paint;
Mike Reed74b76772019-01-04 14:01:06 -0500104 paint.setAntiAlias(true);
105
106 // we don't serialize hinting or encoding -- soon to be removed from paint
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000107
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000108 FOR_SETUP(l, caps, setStrokeCap)
109 FOR_SETUP(m, joins, setStrokeJoin)
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000110 FOR_SETUP(p, styles, setStyle)
111
brianosmanfad98562016-05-04 11:06:28 -0700112 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500113 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000114
mtkleinb4c899d2016-04-29 13:58:09 -0700115 SkAutoMalloc buf(writer.bytesWritten());
116 writer.writeToMemory(buf.get());
117 SkReadBuffer reader(buf.get(), writer.bytesWritten());
skia.committer@gmail.com667b98d2014-04-17 03:05:10 +0000118
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000119 SkPaint paint2;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500120 SkPaintPriv::Unflatten(&paint2, reader, nullptr);
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000121 REPORTER_ASSERT(reporter, paint2 == paint);
122
Mike Reed1de89c42021-01-30 09:10:41 -0500123 }}}
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000124#undef FOR_SETUP
125
126}
127
djsollen@google.com46348e22013-03-04 19:47:42 +0000128// found and fixed for android: not initializing rect for string's of length 0
commit-bot@chromium.orge8807f42014-03-24 23:03:11 +0000129DEF_TEST(Paint_regression_measureText, reporter) {
djsollen@google.com46348e22013-03-04 19:47:42 +0000130
Mike Reed2e6db182018-12-15 13:45:33 -0500131 SkFont font;
132 font.setSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000133
134 SkRect r;
135 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
136
137 // test that the rect was reset
Ben Wagner51e15a62019-05-07 15:38:46 -0400138 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
djsollen@google.com46348e22013-03-04 19:47:42 +0000139 REPORTER_ASSERT(reporter, r.isEmpty());
140}
141
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000142#define ASSERT(expr) REPORTER_ASSERT(r, expr)
143
mtklein610a0152014-09-25 11:57:53 -0700144DEF_TEST(Paint_MoreFlattening, r) {
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000145 SkPaint paint;
146 paint.setColor(0x00AABBCC);
reed374772b2016-10-05 17:33:02 -0700147 paint.setBlendMode(SkBlendMode::kModulate);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000148
brianosmanfad98562016-05-04 11:06:28 -0700149 SkBinaryWriteBuffer writer;
Cary Clark60ca8672018-03-06 15:09:27 -0500150 SkPaintPriv::Flatten(paint, writer);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000151
mtkleinb4c899d2016-04-29 13:58:09 -0700152 SkAutoMalloc buf(writer.bytesWritten());
153 writer.writeToMemory(buf.get());
154 SkReadBuffer reader(buf.get(), writer.bytesWritten());
155
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000156 SkPaint other;
Mike Reed31ba6fe2019-01-14 17:36:54 -0500157 SkPaintPriv::Unflatten(&other, reader, nullptr);
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000158 ASSERT(reader.offset() == writer.bytesWritten());
159
160 // No matter the encoding, these must always hold.
Mike Reed569f29d2021-06-30 18:15:43 -0400161 ASSERT(other.getColor() == paint.getColor());
162 ASSERT(other.asBlendMode() == paint.asBlendMode());
commit-bot@chromium.orgaca1c012014-02-21 18:18:05 +0000163}
mtkleinfb1fe4f2014-10-07 09:26:10 -0700164
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500165#include "include/effects/SkColorMatrixFilter.h"
reedf539b8c2014-11-11 12:51:33 -0800166
167DEF_TEST(Paint_nothingToDraw, r) {
168 SkPaint paint;
169
170 REPORTER_ASSERT(r, !paint.nothingToDraw());
171 paint.setAlpha(0);
172 REPORTER_ASSERT(r, paint.nothingToDraw());
173
174 paint.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700175 paint.setBlendMode(SkBlendMode::kDst);
reedf539b8c2014-11-11 12:51:33 -0800176 REPORTER_ASSERT(r, paint.nothingToDraw());
177
178 paint.setAlpha(0);
reed374772b2016-10-05 17:33:02 -0700179 paint.setBlendMode(SkBlendMode::kSrcOver);
reedf539b8c2014-11-11 12:51:33 -0800180
181 SkColorMatrix cm;
182 cm.setIdentity(); // does not change alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400183 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800184 REPORTER_ASSERT(r, paint.nothingToDraw());
185
Mike Reed68eb8c22019-05-02 11:31:43 -0400186 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
Mike Reed50d79af2019-04-21 22:17:03 -0400187 paint.setColorFilter(SkColorFilters::Matrix(cm));
reedf539b8c2014-11-11 12:51:33 -0800188 REPORTER_ASSERT(r, !paint.nothingToDraw());
189}
Mike Reed477fb912018-11-11 20:37:45 -0500190
Mike Reedc16abee2018-11-24 13:27:27 -0500191DEF_TEST(Font_getpos, r) {
192 SkFont font;
193 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
Ben Wagner51e15a62019-05-07 15:38:46 -0400194 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reedc16abee2018-11-24 13:27:27 -0500195 SkAutoTArray<uint16_t> glyphStorage(count);
196 uint16_t* glyphs = glyphStorage.get();
Ben Wagner51e15a62019-05-07 15:38:46 -0400197 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
Mike Reedc16abee2018-11-24 13:27:27 -0500198
199 SkAutoTArray<SkScalar> widthStorage(count);
200 SkAutoTArray<SkScalar> xposStorage(count);
201 SkAutoTArray<SkPoint> posStorage(count);
202
203 SkScalar* widths = widthStorage.get();
204 SkScalar* xpos = xposStorage.get();
205 SkPoint* pos = posStorage.get();
206
207 for (bool subpix : { false, true }) {
208 font.setSubpixel(subpix);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400209 for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
Mike Reedc16abee2018-11-24 13:27:27 -0500210 font.setHinting(hint);
211 for (auto size : { 1.0f, 12.0f, 100.0f }) {
212 font.setSize(size);
213
214 font.getWidths(glyphs, count, widths);
215 font.getXPos(glyphs, count, xpos, 10);
216 font.getPos(glyphs, count, pos, {10, 20});
217
218 auto nearly_eq = [](SkScalar a, SkScalar b) {
219 return SkScalarAbs(a - b) < 0.000001f;
220 };
221
222 SkScalar x = 10;
223 for (int i = 0; i < count; ++i) {
224 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
225 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
226 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
227 x += widths[i];
228 }
229 }
230 }
231 }
232}