blob: d71d12fc7428106773c0b393215d8881b3d8d125 [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
reed@android.coma0f5d152009-06-22 17:38:10 +00008#include "Test.h"
9#include "SkPath.h"
10#include "SkPaint.h"
djsollen@google.comb44cd652011-12-01 17:09:21 +000011#include "SkLayerDrawLooper.h"
12#include "SkBlurMaskFilter.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000013#include "SkRandom.h"
14#include "SkTypeface.h"
15#include "SkUtils.h"
16
17static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
18 char* u8 = (char*)dst;
19 for (int i = 0; i < count; ++i) {
20 int n = SkUTF8_FromUnichar(src[i], u8);
21 u8 += n;
22 }
23 return u8 - (char*)dst;
24}
25
26static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
27 uint16_t* u16 = (uint16_t*)dst;
28 for (int i = 0; i < count; ++i) {
29 int n = SkUTF16_FromUnichar(src[i], u16);
30 u16 += n;
31 }
32 return (char*)u16 - (char*)dst;
33}
34
35static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
36 SkUnichar* u32 = (SkUnichar*)dst;
37 if (src != u32) {
38 memcpy(u32, src, count * sizeof(SkUnichar));
39 }
40 return count * sizeof(SkUnichar);
41}
42
43static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
44 SkPaint::TextEncoding enc = paint.getTextEncoding();
45 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
46 return (SkTypeface::Encoding)enc;
47}
48
49static int find_first_zero(const uint16_t glyphs[], int count) {
50 for (int i = 0; i < count; ++i) {
51 if (0 == glyphs[i]) {
52 return i;
53 }
54 }
55 return count;
56}
57
58static void test_cmap(skiatest::Reporter* reporter) {
59 static const int NGLYPHS = 64;
60
61 SkUnichar src[NGLYPHS];
62 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
63
64 static const struct {
65 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
66 SkPaint::TextEncoding fEncoding;
67 } gRec[] = {
68 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
69 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
70 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
71 };
72
73 SkRandom rand;
74 SkPaint paint;
75 paint.setTypeface(SkTypeface::RefDefault())->unref();
76 SkTypeface* face = paint.getTypeface();
77
78 for (int i = 0; i < 1000; ++i) {
79 // generate some random text
80 for (int j = 0; j < NGLYPHS; ++j) {
81 src[j] = ' ' + j;
82 }
83 // inject some random chars, to sometimes abort early
84 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000085
reed@google.combcb42ae2013-07-02 13:56:39 +000086 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
87 paint.setTextEncoding(gRec[k].fEncoding);
88
89 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000090
reed@google.combcb42ae2013-07-02 13:56:39 +000091 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000092
reed@google.combcb42ae2013-07-02 13:56:39 +000093 bool contains = paint.containsText(dst, len);
94 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
95 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
96 int index = find_first_zero(glyphs1, NGLYPHS);
97
98 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
99 REPORTER_ASSERT(reporter, index == first);
100 REPORTER_ASSERT(reporter,
101 !memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
102 if (contains) {
103 REPORTER_ASSERT(reporter, NGLYPHS == first);
104 } else {
105 REPORTER_ASSERT(reporter, NGLYPHS > first);
106 }
107 }
108 }
109}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000110
reed@google.com25b3bd52013-05-22 13:55:54 +0000111// temparary api for bicubic, just be sure we can set/clear it
reed@google.com9cfc83c2013-07-22 17:18:18 +0000112static void test_filterlevel(skiatest::Reporter* reporter) {
113 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000114
reed@google.com9cfc83c2013-07-22 17:18:18 +0000115 REPORTER_ASSERT(reporter,
116 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000117
reed@google.com9cfc83c2013-07-22 17:18:18 +0000118 static const SkPaint::FilterLevel gLevels[] = {
119 SkPaint::kNone_FilterLevel,
120 SkPaint::kLow_FilterLevel,
121 SkPaint::kMedium_FilterLevel,
122 SkPaint::kHigh_FilterLevel
123 };
124 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
125 p0.setFilterLevel(gLevels[i]);
126 REPORTER_ASSERT(reporter, gLevels[i] == p0.getFilterLevel());
127 p1 = p0;
128 REPORTER_ASSERT(reporter, gLevels[i] == p1.getFilterLevel());
129
130 p0.reset();
131 REPORTER_ASSERT(reporter,
132 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
133 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000134}
135
commit-bot@chromium.org37ffe8a2013-08-07 15:22:05 +0000136// Only useful for test_copy. Checks the fields that are set.
137static bool check_equal(const SkPaint& a, const SkPaint &b) {
138 return a.getLooper() == b.getLooper() &&
139 a.getMaskFilter() == b.getMaskFilter() &&
140 a.getStrokeWidth() == b.getStrokeWidth() &&
141 a.getStyle() == b.getStyle() &&
142 a.getTextAlign() == b.getTextAlign();
143}
144
djsollen@google.comb44cd652011-12-01 17:09:21 +0000145static void test_copy(skiatest::Reporter* reporter) {
146 SkPaint paint;
147 // set a few member variables
148 paint.setStyle(SkPaint::kStrokeAndFill_Style);
149 paint.setTextAlign(SkPaint::kLeft_Align);
150 paint.setStrokeWidth(SkIntToScalar(2));
151 // set a few pointers
152 SkLayerDrawLooper* looper = new SkLayerDrawLooper();
153 paint.setLooper(looper)->unref();
154 SkMaskFilter* mask = SkBlurMaskFilter::Create(1, SkBlurMaskFilter::kNormal_BlurStyle);
155 paint.setMaskFilter(mask)->unref();
156
157 // copy the paint using the copy constructor and check they are the same
158 SkPaint copiedPaint = paint;
commit-bot@chromium.org37ffe8a2013-08-07 15:22:05 +0000159 REPORTER_ASSERT(reporter, check_equal(paint, copiedPaint));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000160
161#ifdef SK_BUILD_FOR_ANDROID
162 // the copy constructor should preserve the Generation ID
djsollen@google.comefbe8e92013-02-07 18:58:35 +0000163 uint32_t paintGenID = paint.getGenerationID();
164 uint32_t copiedPaintGenID = copiedPaint.getGenerationID();
djsollen@google.comb44cd652011-12-01 17:09:21 +0000165 REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID);
commit-bot@chromium.org37ffe8a2013-08-07 15:22:05 +0000166 REPORTER_ASSERT(reporter, check_equal(paint, copiedPaint));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000167#endif
168
169 // copy the paint using the equal operator and check they are the same
170 copiedPaint = paint;
commit-bot@chromium.org37ffe8a2013-08-07 15:22:05 +0000171 REPORTER_ASSERT(reporter, check_equal(paint, copiedPaint));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000172
173#ifdef SK_BUILD_FOR_ANDROID
174 // the equals operator should increment the Generation ID
175 REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID);
176 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
177 copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value
178 REPORTER_ASSERT(reporter, memcmp(&paint, &copiedPaint, sizeof(paint)));
179#endif
180
181 // clean the paint and check they are back to their initial states
182 SkPaint cleanPaint;
183 paint.reset();
184 copiedPaint.reset();
commit-bot@chromium.org37ffe8a2013-08-07 15:22:05 +0000185 REPORTER_ASSERT(reporter, check_equal(cleanPaint, paint));
186 REPORTER_ASSERT(reporter, check_equal(cleanPaint, copiedPaint));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000187
188#ifdef SK_BUILD_FOR_ANDROID
189 // the reset function should increment the Generation ID
190 REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID);
191 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
192 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &paint, sizeof(cleanPaint)));
193 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &copiedPaint, sizeof(cleanPaint)));
194#endif
195}
reed@android.coma0f5d152009-06-22 17:38:10 +0000196
197// found and fixed for webkit: mishandling when we hit recursion limit on
198// mostly degenerate cubic flatness test
199static void regression_cubic(skiatest::Reporter* reporter) {
200 SkPath path, stroke;
201 SkPaint paint;
202
robertphillips@google.com6853e802012-04-16 15:50:18 +0000203 path.moveTo(SkFloatToScalar(460.2881309415525f),
204 SkFloatToScalar(303.250847066498f));
205 path.cubicTo(SkFloatToScalar(463.36378422175284f),
206 SkFloatToScalar(302.1169735073363f),
207 SkFloatToScalar(456.32239330810046f),
208 SkFloatToScalar(304.720354932878f),
209 SkFloatToScalar(453.15255460013304f),
210 SkFloatToScalar(305.788586869862f));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211
reed@android.coma0f5d152009-06-22 17:38:10 +0000212 SkRect fillR, strokeR;
213 fillR = path.getBounds();
214
215 paint.setStyle(SkPaint::kStroke_Style);
216 paint.setStrokeWidth(SkIntToScalar(2));
217 paint.getFillPath(path, &stroke);
218 strokeR = stroke.getBounds();
219
220 SkRect maxR = fillR;
221 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
222 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
223 SkScalarMul(paint.getStrokeWidth(), miter) :
224 paint.getStrokeWidth();
225 maxR.inset(-inset, -inset);
226
227 // test that our stroke didn't explode
228 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
229}
230
djsollen@google.com46348e22013-03-04 19:47:42 +0000231// found and fixed for android: not initializing rect for string's of length 0
232static void regression_measureText(skiatest::Reporter* reporter) {
233
234 SkPaint paint;
235 paint.setTextSize(SkFloatToScalar(12.0f));
236
237 SkRect r;
238 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
239
240 // test that the rect was reset
241 paint.measureText("", 0, &r, SkFloatToScalar(1.0f));
242 REPORTER_ASSERT(reporter, r.isEmpty());
243}
244
reed@android.coma0f5d152009-06-22 17:38:10 +0000245static void TestPaint(skiatest::Reporter* reporter) {
246 // TODO add general paint tests
djsollen@google.comb44cd652011-12-01 17:09:21 +0000247 test_copy(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000248
249 // regression tests
250 regression_cubic(reporter);
djsollen@google.com46348e22013-03-04 19:47:42 +0000251 regression_measureText(reporter);
reed@google.com25b3bd52013-05-22 13:55:54 +0000252
reed@google.com9cfc83c2013-07-22 17:18:18 +0000253 test_filterlevel(reporter);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000254
reed@google.combcb42ae2013-07-02 13:56:39 +0000255 // need to implement charsToGlyphs on other backends (e.g. linux, win)
reed@google.comc3eb56d2013-07-02 14:01:23 +0000256 // before we can run this tests everywhere
257 if (false) {
258 test_cmap(reporter);
259 }
reed@android.coma0f5d152009-06-22 17:38:10 +0000260}
261
262#include "TestClassDef.h"
263DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint)