epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 7 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 8 | #include "Test.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkLayerDrawLooper.h" |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
| 13 | #include "SkPaint.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
| 15 | #include "SkTypeface.h" |
| 16 | #include "SkUtils.h" |
| 17 | |
| 18 | static 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 | |
| 27 | static 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 | |
| 36 | static 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 | |
| 44 | static 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 | |
| 50 | static 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 | |
| 59 | static 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 74 | SkRandom rand; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 75 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 86 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 87 | 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.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 91 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 92 | uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 93 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 94 | 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.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 111 | |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 112 | // temparary api for bicubic, just be sure we can set/clear it |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 113 | static void test_filterlevel(skiatest::Reporter* reporter) { |
| 114 | SkPaint p0, p1; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 115 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 116 | REPORTER_ASSERT(reporter, |
| 117 | SkPaint::kNone_FilterLevel == p0.getFilterLevel()); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 118 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 119 | 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.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 135 | } |
| 136 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 137 | static 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.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 146 | SkMaskFilter* mask = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 147 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 148 | 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.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 153 | |
| 154 | #ifdef SK_BUILD_FOR_ANDROID |
| 155 | // the copy constructor should preserve the Generation ID |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 156 | uint32_t paintGenID = paint.getGenerationID(); |
| 157 | uint32_t copiedPaintGenID = copiedPaint.getGenerationID(); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, paintGenID == copiedPaintGenID); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 159 | REPORTER_ASSERT(reporter, !memcmp(&paint, &copiedPaint, sizeof(paint))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | // copy the paint using the equal operator and check they are the same |
| 163 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 164 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 165 | |
| 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.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 179 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 180 | |
| 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.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 189 | |
| 190 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 191 | // mostly degenerate cubic flatness test |
| 192 | static void regression_cubic(skiatest::Reporter* reporter) { |
| 193 | SkPath path, stroke; |
| 194 | SkPaint paint; |
| 195 | |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 196 | path.moveTo(SkFloatToScalar(460.2881309415525f), |
| 197 | SkFloatToScalar(303.250847066498f)); |
| 198 | path.cubicTo(SkFloatToScalar(463.36378422175284f), |
| 199 | SkFloatToScalar(302.1169735073363f), |
| 200 | SkFloatToScalar(456.32239330810046f), |
| 201 | SkFloatToScalar(304.720354932878f), |
| 202 | SkFloatToScalar(453.15255460013304f), |
| 203 | SkFloatToScalar(305.788586869862f)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 205 | 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.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 224 | // found and fixed for android: not initializing rect for string's of length 0 |
| 225 | static void regression_measureText(skiatest::Reporter* reporter) { |
| 226 | |
| 227 | SkPaint paint; |
| 228 | paint.setTextSize(SkFloatToScalar(12.0f)); |
| 229 | |
| 230 | SkRect r; |
| 231 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 232 | |
| 233 | // test that the rect was reset |
| 234 | paint.measureText("", 0, &r, SkFloatToScalar(1.0f)); |
| 235 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 236 | } |
| 237 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 238 | static void TestPaint(skiatest::Reporter* reporter) { |
| 239 | // TODO add general paint tests |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 240 | test_copy(reporter); |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 241 | |
| 242 | // regression tests |
| 243 | regression_cubic(reporter); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 244 | regression_measureText(reporter); |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 245 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 246 | test_filterlevel(reporter); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 247 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 248 | // need to implement charsToGlyphs on other backends (e.g. linux, win) |
reed@google.com | c3eb56d | 2013-07-02 14:01:23 +0000 | [diff] [blame] | 249 | // before we can run this tests everywhere |
| 250 | if (false) { |
| 251 | test_cmap(reporter); |
| 252 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | #include "TestClassDef.h" |
| 256 | DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint) |