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 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 8 | #include "SkAutoMalloc.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
Mike Reed | 477fb91 | 2018-11-11 20:37:45 -0500 | [diff] [blame] | 10 | #include "SkFont.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkLayerDrawLooper.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 12 | #include "SkMaskFilter.h" |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 13 | #include "SkPaintPriv.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 16 | #include "SkReadBuffer.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 17 | #include "SkTo.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 18 | #include "SkTypeface.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 19 | #include "SkUTF.h" |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 20 | #include "SkWriteBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "Test.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 22 | #undef ASSERT |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 23 | |
| 24 | static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) { |
| 25 | char* u8 = (char*)dst; |
| 26 | for (int i = 0; i < count; ++i) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 27 | int n = SkToInt(SkUTF::ToUTF8(src[i], u8)); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 28 | u8 += n; |
| 29 | } |
| 30 | return u8 - (char*)dst; |
| 31 | } |
| 32 | |
| 33 | static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) { |
| 34 | uint16_t* u16 = (uint16_t*)dst; |
| 35 | for (int i = 0; i < count; ++i) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 36 | int n = SkToInt(SkUTF::ToUTF16(src[i], u16)); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 37 | u16 += n; |
| 38 | } |
| 39 | return (char*)u16 - (char*)dst; |
| 40 | } |
| 41 | |
| 42 | static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) { |
| 43 | SkUnichar* u32 = (SkUnichar*)dst; |
| 44 | if (src != u32) { |
| 45 | memcpy(u32, src, count * sizeof(SkUnichar)); |
| 46 | } |
| 47 | return count * sizeof(SkUnichar); |
| 48 | } |
| 49 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 59 | DEF_TEST(Paint_cmap, reporter) { |
| 60 | // need to implement charsToGlyphs on other backends (e.g. linux, win) |
| 61 | // before we can run this tests everywhere |
| 62 | return; |
| 63 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 64 | static const int NGLYPHS = 64; |
| 65 | |
| 66 | SkUnichar src[NGLYPHS]; |
| 67 | SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage |
| 68 | |
| 69 | static const struct { |
| 70 | size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count); |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 71 | SkTextEncoding fEncoding; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 72 | } gRec[] = { |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 73 | { uni_to_utf8, kUTF8_SkTextEncoding }, |
| 74 | { uni_to_utf16, kUTF16_SkTextEncoding }, |
| 75 | { uni_to_utf32, kUTF32_SkTextEncoding }, |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 78 | SkRandom rand; |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 79 | SkFont font; |
| 80 | font.setTypeface(SkTypeface::MakeDefault()); |
| 81 | SkTypeface* face = font.getTypeface(); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 82 | |
| 83 | for (int i = 0; i < 1000; ++i) { |
| 84 | // generate some random text |
| 85 | for (int j = 0; j < NGLYPHS; ++j) { |
| 86 | src[j] = ' ' + j; |
| 87 | } |
| 88 | // inject some random chars, to sometimes abort early |
| 89 | src[rand.nextU() & 63] = rand.nextU() & 0xFFF; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 90 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 91 | for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 92 | size_t len = gRec[k].fSeedTextProc(src, dst, 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 | uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS]; |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 95 | |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 96 | bool contains = font.containsText(dst, len, gRec[k].fEncoding); |
| 97 | int nglyphs = font.textToGlyphs(dst, len, gRec[k].fEncoding, glyphs0, NGLYPHS); |
| 98 | int first = face->charsToGlyphs(dst, (SkTypeface::Encoding)gRec[k].fEncoding, |
| 99 | glyphs1, NGLYPHS); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 100 | int index = find_first_zero(glyphs1, NGLYPHS); |
| 101 | |
| 102 | REPORTER_ASSERT(reporter, NGLYPHS == nglyphs); |
| 103 | REPORTER_ASSERT(reporter, index == first); |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, 0 == memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t))); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 105 | if (contains) { |
| 106 | REPORTER_ASSERT(reporter, NGLYPHS == first); |
| 107 | } else { |
| 108 | REPORTER_ASSERT(reporter, NGLYPHS > first); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 113 | |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 114 | // temparary api for bicubic, just be sure we can set/clear it |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 115 | DEF_TEST(Paint_filterQuality, reporter) { |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 116 | SkPaint p0, p1; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 117 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality()); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 119 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 120 | static const SkFilterQuality gQualitys[] = { |
| 121 | kNone_SkFilterQuality, |
| 122 | kLow_SkFilterQuality, |
| 123 | kMedium_SkFilterQuality, |
| 124 | kHigh_SkFilterQuality |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 125 | }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 127 | p0.setFilterQuality(gQualitys[i]); |
| 128 | REPORTER_ASSERT(reporter, gQualitys[i] == p0.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 129 | p1 = p0; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, gQualitys[i] == p1.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 131 | |
| 132 | p0.reset(); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, kNone_SkFilterQuality == p0.getFilterQuality()); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 134 | } |
reed@google.com | 25b3bd5 | 2013-05-22 13:55:54 +0000 | [diff] [blame] | 135 | } |
| 136 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 137 | DEF_TEST(Paint_copy, reporter) { |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 138 | SkPaint paint; |
| 139 | // set a few member variables |
| 140 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 141 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 142 | // set a few pointers |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 143 | SkLayerDrawLooper::Builder looperBuilder; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 144 | paint.setLooper(looperBuilder.detach()); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 145 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 146 | SkBlurMask::ConvertRadiusToSigma(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 147 | |
| 148 | // copy the paint using the copy constructor and check they are the same |
| 149 | SkPaint copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 151 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 152 | // copy the paint using the equal operator and check they are the same |
| 153 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 155 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 156 | // clean the paint and check they are back to their initial states |
| 157 | SkPaint cleanPaint; |
| 158 | paint.reset(); |
| 159 | copiedPaint.reset(); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 161 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 162 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 163 | |
| 164 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 165 | // mostly degenerate cubic flatness test |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 166 | DEF_TEST(Paint_regression_cubic, reporter) { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 167 | SkPath path, stroke; |
| 168 | SkPaint paint; |
| 169 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 170 | path.moveTo(460.2881309415525f, |
| 171 | 303.250847066498f); |
| 172 | path.cubicTo(463.36378422175284f, |
| 173 | 302.1169735073363f, |
| 174 | 456.32239330810046f, |
| 175 | 304.720354932878f, |
| 176 | 453.15255460013304f, |
| 177 | 305.788586869862f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 178 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 179 | SkRect fillR, strokeR; |
| 180 | fillR = path.getBounds(); |
| 181 | |
| 182 | paint.setStyle(SkPaint::kStroke_Style); |
| 183 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 184 | paint.getFillPath(path, &stroke); |
| 185 | strokeR = stroke.getBounds(); |
| 186 | |
| 187 | SkRect maxR = fillR; |
| 188 | SkScalar miter = SkMaxScalar(SK_Scalar1, paint.getStrokeMiter()); |
| 189 | SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ? |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 190 | paint.getStrokeWidth() * miter : |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 191 | paint.getStrokeWidth(); |
| 192 | maxR.inset(-inset, -inset); |
| 193 | |
| 194 | // test that our stroke didn't explode |
| 195 | REPORTER_ASSERT(reporter, maxR.contains(strokeR)); |
| 196 | } |
| 197 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 198 | DEF_TEST(Paint_flattening, reporter) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 199 | const SkFilterQuality levels[] = { |
| 200 | kNone_SkFilterQuality, |
| 201 | kLow_SkFilterQuality, |
| 202 | kMedium_SkFilterQuality, |
| 203 | kHigh_SkFilterQuality, |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 204 | }; |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 205 | const SkPaint::Cap caps[] = { |
| 206 | SkPaint::kButt_Cap, |
| 207 | SkPaint::kRound_Cap, |
| 208 | SkPaint::kSquare_Cap, |
| 209 | }; |
| 210 | const SkPaint::Join joins[] = { |
| 211 | SkPaint::kMiter_Join, |
| 212 | SkPaint::kRound_Join, |
| 213 | SkPaint::kBevel_Join, |
| 214 | }; |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 215 | const SkPaint::Style styles[] = { |
| 216 | SkPaint::kFill_Style, |
| 217 | SkPaint::kStroke_Style, |
| 218 | SkPaint::kStrokeAndFill_Style, |
| 219 | }; |
| 220 | |
| 221 | #define FOR_SETUP(index, array, setter) \ |
| 222 | for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \ |
Mike Reed | 74b7677 | 2019-01-04 14:01:06 -0500 | [diff] [blame] | 223 | paint.setter(array[index]); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 224 | |
| 225 | SkPaint paint; |
Mike Reed | 74b7677 | 2019-01-04 14:01:06 -0500 | [diff] [blame] | 226 | paint.setAntiAlias(true); |
| 227 | |
| 228 | // we don't serialize hinting or encoding -- soon to be removed from paint |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 229 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 230 | FOR_SETUP(i, levels, setFilterQuality) |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 231 | FOR_SETUP(l, caps, setStrokeCap) |
| 232 | FOR_SETUP(m, joins, setStrokeJoin) |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 233 | FOR_SETUP(p, styles, setStyle) |
| 234 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 235 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 236 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 237 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 238 | SkAutoMalloc buf(writer.bytesWritten()); |
| 239 | writer.writeToMemory(buf.get()); |
| 240 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
skia.committer@gmail.com | 667b98d | 2014-04-17 03:05:10 +0000 | [diff] [blame] | 241 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 242 | SkPaint paint2; |
Mike Reed | 31ba6fe | 2019-01-14 17:36:54 -0500 | [diff] [blame] | 243 | SkPaintPriv::Unflatten(&paint2, reader, nullptr); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 244 | REPORTER_ASSERT(reporter, paint2 == paint); |
| 245 | |
Mike Reed | 74b7677 | 2019-01-04 14:01:06 -0500 | [diff] [blame] | 246 | }}}} |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 247 | #undef FOR_SETUP |
| 248 | |
| 249 | } |
| 250 | |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 251 | // found and fixed for android: not initializing rect for string's of length 0 |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 252 | DEF_TEST(Paint_regression_measureText, reporter) { |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 253 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 254 | SkFont font; |
| 255 | font.setSize(12.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 256 | |
| 257 | SkRect r; |
| 258 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 259 | |
| 260 | // test that the rect was reset |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 261 | font.measureText("", 0, kUTF8_SkTextEncoding, &r); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 263 | } |
| 264 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 265 | #define ASSERT(expr) REPORTER_ASSERT(r, expr) |
| 266 | |
mtklein | 610a015 | 2014-09-25 11:57:53 -0700 | [diff] [blame] | 267 | DEF_TEST(Paint_MoreFlattening, r) { |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 268 | SkPaint paint; |
| 269 | paint.setColor(0x00AABBCC); |
Hal Canary | f1921f4 | 2019-01-08 15:23:11 -0500 | [diff] [blame] | 270 | #ifdef SK_SUPPORT_LEGACY_PAINT_FONT_FIELDS |
mtklein | ee902cd | 2014-09-22 11:40:33 -0700 | [diff] [blame] | 271 | paint.setTextScaleX(1.0f); // Default value, ignored. |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 272 | paint.setTextSize(19); |
Hal Canary | f1921f4 | 2019-01-08 15:23:11 -0500 | [diff] [blame] | 273 | #endif |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 274 | paint.setBlendMode(SkBlendMode::kModulate); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | paint.setLooper(nullptr); // Default value, ignored. |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 276 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 277 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 278 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 279 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 280 | SkAutoMalloc buf(writer.bytesWritten()); |
| 281 | writer.writeToMemory(buf.get()); |
| 282 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 283 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 284 | SkPaint other; |
Mike Reed | 31ba6fe | 2019-01-14 17:36:54 -0500 | [diff] [blame] | 285 | SkPaintPriv::Unflatten(&other, reader, nullptr); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 286 | ASSERT(reader.offset() == writer.bytesWritten()); |
| 287 | |
| 288 | // No matter the encoding, these must always hold. |
| 289 | ASSERT(other.getColor() == paint.getColor()); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 290 | ASSERT(other.getLooper() == paint.getLooper()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 291 | ASSERT(other.getBlendMode() == paint.getBlendMode()); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 292 | } |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 293 | |
| 294 | DEF_TEST(Paint_getHash, r) { |
| 295 | // Try not to inspect the actual hash values in here. |
| 296 | // We might want to change the hash function. |
| 297 | |
| 298 | SkPaint paint; |
| 299 | const uint32_t defaultHash = paint.getHash(); |
| 300 | |
| 301 | // Check that some arbitrary field affects the hash. |
| 302 | paint.setColor(0xFF00FF00); |
| 303 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
| 304 | paint.setColor(SK_ColorBLACK); // Reset to default value. |
| 305 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 306 | |
Hal Canary | f1921f4 | 2019-01-08 15:23:11 -0500 | [diff] [blame] | 307 | #ifdef SK_SUPPORT_LEGACY_PAINT_FONT_FIELDS |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 308 | // SkTypeface is the first field we hash, so test it specially. |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 309 | paint.setTypeface(SkTypeface::MakeDefault()); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 310 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 311 | paint.setTypeface(nullptr); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 312 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
Hal Canary | 02738a8 | 2019-01-21 18:51:32 +0000 | [diff] [blame] | 313 | #endif |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 314 | |
| 315 | // This is part of fBitfields, the last field we hash. |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame] | 316 | paint.setHinting(kSlight_SkFontHinting); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 317 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame] | 318 | paint.setHinting(kNormal_SkFontHinting); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 319 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 320 | } |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 321 | |
| 322 | #include "SkColorMatrixFilter.h" |
| 323 | |
| 324 | DEF_TEST(Paint_nothingToDraw, r) { |
| 325 | SkPaint paint; |
| 326 | |
| 327 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 328 | paint.setAlpha(0); |
| 329 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 330 | |
| 331 | paint.setAlpha(0xFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 332 | paint.setBlendMode(SkBlendMode::kDst); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 333 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 334 | |
| 335 | paint.setAlpha(0); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 336 | paint.setBlendMode(SkBlendMode::kSrcOver); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 337 | |
| 338 | SkColorMatrix cm; |
| 339 | cm.setIdentity(); // does not change alpha |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 340 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 341 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 342 | |
| 343 | cm.postTranslate(0, 0, 0, 1); // wacks alpha |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 344 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 345 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 346 | } |
Mike Reed | 477fb91 | 2018-11-11 20:37:45 -0500 | [diff] [blame] | 347 | |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 348 | #ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE |
Mike Reed | 477fb91 | 2018-11-11 20:37:45 -0500 | [diff] [blame] | 349 | DEF_TEST(Paint_getwidths, r) { |
| 350 | SkPaint paint; |
| 351 | const char text[] = "Hamburgefons!@#!#23425,./;'[]"; |
| 352 | int count = paint.countText(text, strlen(text)); |
| 353 | SkAutoTArray<uint16_t> glyphStorage(count * 2); |
| 354 | uint16_t* glyphs = glyphStorage.get(); |
| 355 | |
| 356 | (void)paint.textToGlyphs(text, strlen(text), glyphs); |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 357 | paint.setTextEncoding(kGlyphID_SkTextEncoding); |
Mike Reed | 477fb91 | 2018-11-11 20:37:45 -0500 | [diff] [blame] | 358 | |
| 359 | SkAutoTArray<SkScalar> widthStorage(count * 2); |
| 360 | SkScalar* widths = widthStorage.get(); |
| 361 | SkAutoTArray<SkRect> rectStorage(count * 2); |
| 362 | SkRect* bounds = rectStorage.get(); |
| 363 | |
| 364 | for (bool subpix : { false, true }) { |
| 365 | paint.setSubpixelText(subpix); |
| 366 | for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) { |
| 367 | paint.setHinting(hint); |
| 368 | for (auto size : { 1.0f, 12.0f, 100.0f }) { |
| 369 | paint.setTextSize(size); |
| 370 | paint.getTextWidths(glyphs, count * 2, widths, bounds); |
| 371 | |
| 372 | SkFont font = SkFont::LEGACY_ExtractFromPaint(paint); |
| 373 | font.getWidths(glyphs, count, widths + count, bounds + count); |
| 374 | |
| 375 | for (int i = 0; i < count; ++i) { |
| 376 | REPORTER_ASSERT(r, widths[i] == widths[i + count]); |
| 377 | REPORTER_ASSERT(r, bounds[i] == bounds[i + count]); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 383 | #endif |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 384 | |
| 385 | DEF_TEST(Font_getpos, r) { |
| 386 | SkFont font; |
| 387 | const char text[] = "Hamburgefons!@#!#23425,./;'[]"; |
| 388 | int count = font.countText(text, strlen(text), kUTF8_SkTextEncoding); |
| 389 | SkAutoTArray<uint16_t> glyphStorage(count); |
| 390 | uint16_t* glyphs = glyphStorage.get(); |
| 391 | (void)font.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, glyphs, count); |
| 392 | |
| 393 | SkAutoTArray<SkScalar> widthStorage(count); |
| 394 | SkAutoTArray<SkScalar> xposStorage(count); |
| 395 | SkAutoTArray<SkPoint> posStorage(count); |
| 396 | |
| 397 | SkScalar* widths = widthStorage.get(); |
| 398 | SkScalar* xpos = xposStorage.get(); |
| 399 | SkPoint* pos = posStorage.get(); |
| 400 | |
| 401 | for (bool subpix : { false, true }) { |
| 402 | font.setSubpixel(subpix); |
| 403 | for (auto hint : { kNo_SkFontHinting, kSlight_SkFontHinting, kNormal_SkFontHinting, kFull_SkFontHinting}) { |
| 404 | font.setHinting(hint); |
| 405 | for (auto size : { 1.0f, 12.0f, 100.0f }) { |
| 406 | font.setSize(size); |
| 407 | |
| 408 | font.getWidths(glyphs, count, widths); |
| 409 | font.getXPos(glyphs, count, xpos, 10); |
| 410 | font.getPos(glyphs, count, pos, {10, 20}); |
| 411 | |
| 412 | auto nearly_eq = [](SkScalar a, SkScalar b) { |
| 413 | return SkScalarAbs(a - b) < 0.000001f; |
| 414 | }; |
| 415 | |
| 416 | SkScalar x = 10; |
| 417 | for (int i = 0; i < count; ++i) { |
| 418 | REPORTER_ASSERT(r, nearly_eq(x, xpos[i])); |
| 419 | REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX)); |
| 420 | REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY)); |
| 421 | x += widths[i]; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |