blob: eed9de3a5c6cfac987d046c11ce94c1d663f5626 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
11#include "SkBlurMaskFilter.h"
12#include "SkLayerDrawLooper.h"
reed@android.coma0f5d152009-06-22 17:38:10 +000013#include "SkPath.h"
14#include "SkPaint.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000015#include "SkRandom.h"
16#include "SkTypeface.h"
17#include "SkUtils.h"
18
19static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
20 char* u8 = (char*)dst;
21 for (int i = 0; i < count; ++i) {
22 int n = SkUTF8_FromUnichar(src[i], u8);
23 u8 += n;
24 }
25 return u8 - (char*)dst;
26}
27
28static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
29 uint16_t* u16 = (uint16_t*)dst;
30 for (int i = 0; i < count; ++i) {
31 int n = SkUTF16_FromUnichar(src[i], u16);
32 u16 += n;
33 }
34 return (char*)u16 - (char*)dst;
35}
36
37static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
38 SkUnichar* u32 = (SkUnichar*)dst;
39 if (src != u32) {
40 memcpy(u32, src, count * sizeof(SkUnichar));
41 }
42 return count * sizeof(SkUnichar);
43}
44
45static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
46 SkPaint::TextEncoding enc = paint.getTextEncoding();
47 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
48 return (SkTypeface::Encoding)enc;
49}
50
51static int find_first_zero(const uint16_t glyphs[], int count) {
52 for (int i = 0; i < count; ++i) {
53 if (0 == glyphs[i]) {
54 return i;
55 }
56 }
57 return count;
58}
59
60static void test_cmap(skiatest::Reporter* reporter) {
61 static const int NGLYPHS = 64;
62
63 SkUnichar src[NGLYPHS];
64 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
65
66 static const struct {
67 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
68 SkPaint::TextEncoding fEncoding;
69 } gRec[] = {
70 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
71 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
72 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
73 };
74
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000075 SkRandom rand;
reed@google.combcb42ae2013-07-02 13:56:39 +000076 SkPaint paint;
77 paint.setTypeface(SkTypeface::RefDefault())->unref();
78 SkTypeface* face = paint.getTypeface();
79
80 for (int i = 0; i < 1000; ++i) {
81 // generate some random text
82 for (int j = 0; j < NGLYPHS; ++j) {
83 src[j] = ' ' + j;
84 }
85 // inject some random chars, to sometimes abort early
86 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000087
reed@google.combcb42ae2013-07-02 13:56:39 +000088 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
89 paint.setTextEncoding(gRec[k].fEncoding);
90
91 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000092
reed@google.combcb42ae2013-07-02 13:56:39 +000093 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000094
reed@google.combcb42ae2013-07-02 13:56:39 +000095 bool contains = paint.containsText(dst, len);
96 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
97 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
98 int index = find_first_zero(glyphs1, NGLYPHS);
99
100 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
101 REPORTER_ASSERT(reporter, index == first);
102 REPORTER_ASSERT(reporter,
103 !memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
104 if (contains) {
105 REPORTER_ASSERT(reporter, NGLYPHS == first);
106 } else {
107 REPORTER_ASSERT(reporter, NGLYPHS > first);
108 }
109 }
110 }
111}
djsollen@google.comb44cd652011-12-01 17:09:21 +0000112
reed@google.com25b3bd52013-05-22 13:55:54 +0000113// temparary api for bicubic, just be sure we can set/clear it
reed@google.com9cfc83c2013-07-22 17:18:18 +0000114static void test_filterlevel(skiatest::Reporter* reporter) {
115 SkPaint p0, p1;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000116
reed@google.com9cfc83c2013-07-22 17:18:18 +0000117 REPORTER_ASSERT(reporter,
118 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000119
reed@google.com9cfc83c2013-07-22 17:18:18 +0000120 static const SkPaint::FilterLevel gLevels[] = {
121 SkPaint::kNone_FilterLevel,
122 SkPaint::kLow_FilterLevel,
123 SkPaint::kMedium_FilterLevel,
124 SkPaint::kHigh_FilterLevel
125 };
126 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
127 p0.setFilterLevel(gLevels[i]);
128 REPORTER_ASSERT(reporter, gLevels[i] == p0.getFilterLevel());
129 p1 = p0;
130 REPORTER_ASSERT(reporter, gLevels[i] == p1.getFilterLevel());
131
132 p0.reset();
133 REPORTER_ASSERT(reporter,
134 SkPaint::kNone_FilterLevel == p0.getFilterLevel());
135 }
reed@google.com25b3bd52013-05-22 13:55:54 +0000136}
137
djsollen@google.comb44cd652011-12-01 17:09:21 +0000138static void test_copy(skiatest::Reporter* reporter) {
139 SkPaint paint;
140 // set a few member variables
141 paint.setStyle(SkPaint::kStrokeAndFill_Style);
142 paint.setTextAlign(SkPaint::kLeft_Align);
143 paint.setStrokeWidth(SkIntToScalar(2));
144 // set a few pointers
145 SkLayerDrawLooper* looper = new SkLayerDrawLooper();
146 paint.setLooper(looper)->unref();
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +0000147 SkMaskFilter* mask = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000148 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000149 paint.setMaskFilter(mask)->unref();
150
151 // copy the paint using the copy constructor and check they are the same
152 SkPaint copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000153 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000154
155#ifdef SK_BUILD_FOR_ANDROID
156 // the copy constructor should preserve the Generation ID
djsollen@google.comefbe8e92013-02-07 18:58:35 +0000157 uint32_t paintGenID = paint.getGenerationID();
158 uint32_t copiedPaintGenID = copiedPaint.getGenerationID();
djsollen@google.comb44cd652011-12-01 17:09:21 +0000159 REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID);
robertphillips@google.comb2657412013-08-07 22:36:29 +0000160 REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint)));
djsollen@google.comb44cd652011-12-01 17:09:21 +0000161#endif
162
163 // copy the paint using the equal operator and check they are the same
164 copiedPaint = paint;
robertphillips@google.comb2657412013-08-07 22:36:29 +0000165 REPORTER_ASSERT(reporter, paint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000166
167#ifdef SK_BUILD_FOR_ANDROID
168 // the equals operator should increment the Generation ID
169 REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID);
170 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
171 copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value
172 REPORTER_ASSERT(reporter, memcmp(&paint, &copiedPaint, sizeof(paint)));
173#endif
174
175 // clean the paint and check they are back to their initial states
176 SkPaint cleanPaint;
177 paint.reset();
178 copiedPaint.reset();
robertphillips@google.comb2657412013-08-07 22:36:29 +0000179 REPORTER_ASSERT(reporter, cleanPaint == paint);
180 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
djsollen@google.comb44cd652011-12-01 17:09:21 +0000181
182#ifdef SK_BUILD_FOR_ANDROID
183 // the reset function should increment the Generation ID
184 REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID);
185 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
186 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &paint, sizeof(cleanPaint)));
187 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &copiedPaint, sizeof(cleanPaint)));
188#endif
189}
reed@android.coma0f5d152009-06-22 17:38:10 +0000190
191// found and fixed for webkit: mishandling when we hit recursion limit on
192// mostly degenerate cubic flatness test
193static void regression_cubic(skiatest::Reporter* reporter) {
194 SkPath path, stroke;
195 SkPaint paint;
196
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000197 path.moveTo(460.2881309415525f,
198 303.250847066498f);
199 path.cubicTo(463.36378422175284f,
200 302.1169735073363f,
201 456.32239330810046f,
202 304.720354932878f,
203 453.15255460013304f,
204 305.788586869862f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000205
reed@android.coma0f5d152009-06-22 17:38:10 +0000206 SkRect fillR, strokeR;
207 fillR = path.getBounds();
208
209 paint.setStyle(SkPaint::kStroke_Style);
210 paint.setStrokeWidth(SkIntToScalar(2));
211 paint.getFillPath(path, &stroke);
212 strokeR = stroke.getBounds();
213
214 SkRect maxR = fillR;
215 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
216 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
217 SkScalarMul(paint.getStrokeWidth(), miter) :
218 paint.getStrokeWidth();
219 maxR.inset(-inset, -inset);
220
221 // test that our stroke didn't explode
222 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
223}
224
djsollen@google.com46348e22013-03-04 19:47:42 +0000225// found and fixed for android: not initializing rect for string's of length 0
226static void regression_measureText(skiatest::Reporter* reporter) {
227
228 SkPaint paint;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000229 paint.setTextSize(12.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000230
231 SkRect r;
232 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
233
234 // test that the rect was reset
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000235 paint.measureText("", 0, &r, 1.0f);
djsollen@google.com46348e22013-03-04 19:47:42 +0000236 REPORTER_ASSERT(reporter, r.isEmpty());
237}
238
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000239DEF_TEST(Paint, reporter) {
reed@android.coma0f5d152009-06-22 17:38:10 +0000240 // TODO add general paint tests
djsollen@google.comb44cd652011-12-01 17:09:21 +0000241 test_copy(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000242
243 // regression tests
244 regression_cubic(reporter);
djsollen@google.com46348e22013-03-04 19:47:42 +0000245 regression_measureText(reporter);
reed@google.com25b3bd52013-05-22 13:55:54 +0000246
reed@google.com9cfc83c2013-07-22 17:18:18 +0000247 test_filterlevel(reporter);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +0000248
reed@google.combcb42ae2013-07-02 13:56:39 +0000249 // need to implement charsToGlyphs on other backends (e.g. linux, win)
reed@google.comc3eb56d2013-07-02 14:01:23 +0000250 // before we can run this tests everywhere
251 if (false) {
252 test_cmap(reporter);
253 }
reed@android.coma0f5d152009-06-22 17:38:10 +0000254}