blob: 6d12ebec28ba98eefd4699c786aea38097648166 [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;
85
86 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);
90
91 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
92
93 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
112static void test_bicubic(skiatest::Reporter* reporter) {
113 SkPaint p0;
114 REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag));
115 p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag);
116 REPORTER_ASSERT(reporter, 0 != (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag));
117 SkPaint p1(p0);
118 REPORTER_ASSERT(reporter, 0 != (p1.getFlags() & SkPaint::kBicubicFilterBitmap_Flag));
119 p0.reset();
120 REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitmap_Flag));
121 p0 = p1;
122 p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag);
123}
124
djsollen@google.comb44cd652011-12-01 17:09:21 +0000125static void test_copy(skiatest::Reporter* reporter) {
126 SkPaint paint;
127 // set a few member variables
128 paint.setStyle(SkPaint::kStrokeAndFill_Style);
129 paint.setTextAlign(SkPaint::kLeft_Align);
130 paint.setStrokeWidth(SkIntToScalar(2));
131 // set a few pointers
132 SkLayerDrawLooper* looper = new SkLayerDrawLooper();
133 paint.setLooper(looper)->unref();
134 SkMaskFilter* mask = SkBlurMaskFilter::Create(1, SkBlurMaskFilter::kNormal_BlurStyle);
135 paint.setMaskFilter(mask)->unref();
136
137 // copy the paint using the copy constructor and check they are the same
138 SkPaint copiedPaint = paint;
139 REPORTER_ASSERT(reporter, paint == copiedPaint);
140
141#ifdef SK_BUILD_FOR_ANDROID
142 // the copy constructor should preserve the Generation ID
djsollen@google.comefbe8e92013-02-07 18:58:35 +0000143 uint32_t paintGenID = paint.getGenerationID();
144 uint32_t copiedPaintGenID = copiedPaint.getGenerationID();
djsollen@google.comb44cd652011-12-01 17:09:21 +0000145 REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID);
146 REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint)));
147#endif
148
149 // copy the paint using the equal operator and check they are the same
150 copiedPaint = paint;
151 REPORTER_ASSERT(reporter, paint == copiedPaint);
152
153#ifdef SK_BUILD_FOR_ANDROID
154 // the equals operator should increment the Generation ID
155 REPORTER_ASSERT(reporter, paint.getGenerationID() == paintGenID);
156 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
157 copiedPaintGenID = copiedPaint.getGenerationID(); // reset to the new value
158 REPORTER_ASSERT(reporter, memcmp(&paint, &copiedPaint, sizeof(paint)));
159#endif
160
161 // clean the paint and check they are back to their initial states
162 SkPaint cleanPaint;
163 paint.reset();
164 copiedPaint.reset();
165 REPORTER_ASSERT(reporter, cleanPaint == paint);
166 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
167
168#ifdef SK_BUILD_FOR_ANDROID
169 // the reset function should increment the Generation ID
170 REPORTER_ASSERT(reporter, paint.getGenerationID() != paintGenID);
171 REPORTER_ASSERT(reporter, copiedPaint.getGenerationID() != copiedPaintGenID);
172 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &paint, sizeof(cleanPaint)));
173 REPORTER_ASSERT(reporter, memcmp(&cleanPaint, &copiedPaint, sizeof(cleanPaint)));
174#endif
175}
reed@android.coma0f5d152009-06-22 17:38:10 +0000176
177// found and fixed for webkit: mishandling when we hit recursion limit on
178// mostly degenerate cubic flatness test
179static void regression_cubic(skiatest::Reporter* reporter) {
180 SkPath path, stroke;
181 SkPaint paint;
182
robertphillips@google.com6853e802012-04-16 15:50:18 +0000183 path.moveTo(SkFloatToScalar(460.2881309415525f),
184 SkFloatToScalar(303.250847066498f));
185 path.cubicTo(SkFloatToScalar(463.36378422175284f),
186 SkFloatToScalar(302.1169735073363f),
187 SkFloatToScalar(456.32239330810046f),
188 SkFloatToScalar(304.720354932878f),
189 SkFloatToScalar(453.15255460013304f),
190 SkFloatToScalar(305.788586869862f));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191
reed@android.coma0f5d152009-06-22 17:38:10 +0000192 SkRect fillR, strokeR;
193 fillR = path.getBounds();
194
195 paint.setStyle(SkPaint::kStroke_Style);
196 paint.setStrokeWidth(SkIntToScalar(2));
197 paint.getFillPath(path, &stroke);
198 strokeR = stroke.getBounds();
199
200 SkRect maxR = fillR;
201 SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter());
202 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
203 SkScalarMul(paint.getStrokeWidth(), miter) :
204 paint.getStrokeWidth();
205 maxR.inset(-inset, -inset);
206
207 // test that our stroke didn't explode
208 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
209}
210
djsollen@google.com46348e22013-03-04 19:47:42 +0000211// found and fixed for android: not initializing rect for string's of length 0
212static void regression_measureText(skiatest::Reporter* reporter) {
213
214 SkPaint paint;
215 paint.setTextSize(SkFloatToScalar(12.0f));
216
217 SkRect r;
218 r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN);
219
220 // test that the rect was reset
221 paint.measureText("", 0, &r, SkFloatToScalar(1.0f));
222 REPORTER_ASSERT(reporter, r.isEmpty());
223}
224
reed@android.coma0f5d152009-06-22 17:38:10 +0000225static void TestPaint(skiatest::Reporter* reporter) {
226 // TODO add general paint tests
djsollen@google.comb44cd652011-12-01 17:09:21 +0000227 test_copy(reporter);
reed@android.coma0f5d152009-06-22 17:38:10 +0000228
229 // regression tests
230 regression_cubic(reporter);
djsollen@google.com46348e22013-03-04 19:47:42 +0000231 regression_measureText(reporter);
reed@google.com25b3bd52013-05-22 13:55:54 +0000232
233 test_bicubic(reporter);
reed@google.combcb42ae2013-07-02 13:56:39 +0000234#ifdef SK_BUILD_FOR_MAC
235 // need to implement charsToGlyphs on other backends (e.g. linux, win)
236 // before we can run this tests everywhre
237 test_cmap(reporter);
238#endif
reed@android.coma0f5d152009-06-22 17:38:10 +0000239}
240
241#include "TestClassDef.h"
242DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint)