1 /*
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 */
7
8 #include "include/core/SkBlendMode.h"
9 #include "include/core/SkBlurTypes.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkMaskFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkPathUtils.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkScalar.h"
21 #include "include/effects/SkColorMatrix.h"
22 #include "include/private/base/SkTemplates.h"
23 #include "src/base/SkAutoMalloc.h"
24 #include "src/core/SkBlurMask.h"
25 #include "src/core/SkPaintPriv.h"
26 #include "src/core/SkReadBuffer.h"
27 #include "src/core/SkWriteBuffer.h"
28 #include "tests/Test.h"
29 #include "tools/fonts/FontToolUtils.h"
30
31 #include <algorithm>
32 #include <array>
33 #include <cstddef>
34 #include <cstdint>
35 #include <cstring>
36 #include <initializer_list>
37 #include <optional>
38
39 #undef ASSERT
40
41 using namespace skia_private;
42
DEF_TEST(Paint_copy,reporter)43 DEF_TEST(Paint_copy, reporter) {
44 SkPaint paint;
45 // set a few member variables
46 paint.setStyle(SkPaint::kStrokeAndFill_Style);
47 paint.setStrokeWidth(SkIntToScalar(2));
48 // set a few pointers
49 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
50 SkBlurMask::ConvertRadiusToSigma(1)));
51
52 // copy the paint using the copy constructor and check they are the same
53 SkPaint copiedPaint = paint;
54 REPORTER_ASSERT(reporter, paint == copiedPaint);
55
56 // copy the paint using the equal operator and check they are the same
57 copiedPaint = paint;
58 REPORTER_ASSERT(reporter, paint == copiedPaint);
59
60 // clean the paint and check they are back to their initial states
61 SkPaint cleanPaint;
62 paint.reset();
63 copiedPaint.reset();
64 REPORTER_ASSERT(reporter, cleanPaint == paint);
65 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
66 }
67
68 // found and fixed for webkit: mishandling when we hit recursion limit on
69 // mostly degenerate cubic flatness test
DEF_TEST(Paint_regression_cubic,reporter)70 DEF_TEST(Paint_regression_cubic, reporter) {
71 SkPath path, stroke;
72 SkPaint paint;
73
74 path.moveTo(460.2881309415525f,
75 303.250847066498f);
76 path.cubicTo(463.36378422175284f,
77 302.1169735073363f,
78 456.32239330810046f,
79 304.720354932878f,
80 453.15255460013304f,
81 305.788586869862f);
82
83 SkRect fillR, strokeR;
84 fillR = path.getBounds();
85
86 paint.setStyle(SkPaint::kStroke_Style);
87 paint.setStrokeWidth(SkIntToScalar(2));
88 skpathutils::FillPathWithPaint(path, paint, &stroke);
89 strokeR = stroke.getBounds();
90
91 SkRect maxR = fillR;
92 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
93 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
94 paint.getStrokeWidth() * miter :
95 paint.getStrokeWidth();
96 maxR.inset(-inset, -inset);
97
98 // test that our stroke didn't explode
99 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
100 }
101
DEF_TEST(Paint_flattening,reporter)102 DEF_TEST(Paint_flattening, reporter) {
103 const SkPaint::Cap caps[] = {
104 SkPaint::kButt_Cap,
105 SkPaint::kRound_Cap,
106 SkPaint::kSquare_Cap,
107 };
108 const SkPaint::Join joins[] = {
109 SkPaint::kMiter_Join,
110 SkPaint::kRound_Join,
111 SkPaint::kBevel_Join,
112 };
113 const SkPaint::Style styles[] = {
114 SkPaint::kFill_Style,
115 SkPaint::kStroke_Style,
116 SkPaint::kStrokeAndFill_Style,
117 };
118
119 #define FOR_SETUP(index, array, setter) \
120 for (size_t index = 0; index < std::size(array); ++index) { \
121 paint.setter(array[index]);
122
123 SkPaint paint;
124 paint.setAntiAlias(true);
125
126 // we don't serialize hinting or encoding -- soon to be removed from paint
127
128 FOR_SETUP(l, caps, setStrokeCap)
129 FOR_SETUP(m, joins, setStrokeJoin)
130 FOR_SETUP(p, styles, setStyle)
131
132 SkBinaryWriteBuffer writer({});
133 SkPaintPriv::Flatten(paint, writer);
134
135 SkAutoMalloc buf(writer.bytesWritten());
136 writer.writeToMemory(buf.get());
137 SkReadBuffer reader(buf.get(), writer.bytesWritten());
138
139 SkPaint paint2 = reader.readPaint();
140 REPORTER_ASSERT(reporter, paint2 == paint);
141
142 }}}
143 #undef FOR_SETUP
144
145 }
146
147 // found and fixed for android: not initializing rect for string's of length 0
148 DEF_TEST(Paint_regression_measureText, reporter) {
149
150 SkFont font = ToolUtils::DefaultFont();
151 font.setSize(12.0f);
152
153 SkRect r;
154 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
155
156 // test that the rect was reset
157 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
158 REPORTER_ASSERT(reporter, r.isEmpty());
159 }
160
161 #define ASSERT(expr) REPORTER_ASSERT(r, expr)
162
163 DEF_TEST(Paint_MoreFlattening, r) {
164 SkPaint paint;
165 paint.setColor(0x00AABBCC);
166 paint.setBlendMode(SkBlendMode::kModulate);
167
168 SkBinaryWriteBuffer writer({});
169 SkPaintPriv::Flatten(paint, writer);
170
171 SkAutoMalloc buf(writer.bytesWritten());
172 writer.writeToMemory(buf.get());
173 SkReadBuffer reader(buf.get(), writer.bytesWritten());
174
175 SkPaint other = reader.readPaint();
176 ASSERT(reader.offset() == writer.bytesWritten());
177
178 // No matter the encoding, these must always hold.
179 ASSERT(other.getColor() == paint.getColor());
180 ASSERT(other.asBlendMode() == paint.asBlendMode());
181 }
182
183 DEF_TEST(Paint_nothingToDraw, r) {
184 SkPaint paint;
185
186 REPORTER_ASSERT(r, !paint.nothingToDraw());
187 paint.setAlpha(0);
188 REPORTER_ASSERT(r, paint.nothingToDraw());
189
190 paint.setAlpha(0xFF);
191 paint.setBlendMode(SkBlendMode::kDst);
192 REPORTER_ASSERT(r, paint.nothingToDraw());
193
194 paint.setAlpha(0);
195 paint.setBlendMode(SkBlendMode::kSrcOver);
196
197 SkColorMatrix cm;
198 cm.setIdentity(); // does not change alpha
199 paint.setColorFilter(SkColorFilters::Matrix(cm));
200 REPORTER_ASSERT(r, paint.nothingToDraw());
201
202 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
203 paint.setColorFilter(SkColorFilters::Matrix(cm));
204 REPORTER_ASSERT(r, !paint.nothingToDraw());
205 }
206
207 DEF_TEST(Font_getpos, r) {
208 SkFont font = ToolUtils::DefaultFont();
209 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
210 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
211 AutoTArray<uint16_t> glyphStorage(count);
212 uint16_t* glyphs = glyphStorage.get();
213 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
214
215 AutoTArray<SkScalar> widthStorage(count);
216 AutoTArray<SkScalar> xposStorage(count);
217 AutoTArray<SkPoint> posStorage(count);
218
219 SkScalar* widths = widthStorage.get();
220 SkScalar* xpos = xposStorage.get();
221 SkPoint* pos = posStorage.get();
222
223 for (bool subpix : { false, true }) {
224 font.setSubpixel(subpix);
225 for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) {
226 font.setHinting(hint);
227 for (auto size : { 1.0f, 12.0f, 100.0f }) {
228 font.setSize(size);
229
230 font.getWidths(glyphs, count, widths);
231 font.getXPos(glyphs, count, xpos, 10);
232 font.getPos(glyphs, count, pos, {10, 20});
233
234 auto nearly_eq = [](SkScalar a, SkScalar b) {
235 return SkScalarAbs(a - b) < 0.000001f;
236 };
237
238 SkScalar x = 10;
239 for (int i = 0; i < count; ++i) {
240 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
241 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
242 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
243 x += widths[i];
244 }
245 }
246 }
247 }
248 }
249
250 DEF_TEST(Paint_dither, reporter) {
251 SkPaint p;
252 p.setDither(true);
253
254 bool shouldDither = SkPaintPriv::ShouldDither(p, kBGRA_8888_SkColorType);
255
256 REPORTER_ASSERT(reporter, !shouldDither);
257 }
258