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