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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkFont.h" |
| 9 | #include "include/core/SkMaskFilter.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/SkTo.h" |
| 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "src/core/SkAutoMalloc.h" |
| 15 | #include "src/core/SkBlurMask.h" |
| 16 | #include "src/core/SkPaintPriv.h" |
| 17 | #include "src/core/SkReadBuffer.h" |
| 18 | #include "src/core/SkWriteBuffer.h" |
| 19 | #include "src/utils/SkUTF.h" |
| 20 | #include "tests/Test.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 21 | #undef ASSERT |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 23 | DEF_TEST(Paint_copy, reporter) { |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 24 | SkPaint paint; |
| 25 | // set a few member variables |
| 26 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 27 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 28 | // set a few pointers |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 29 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 30 | SkBlurMask::ConvertRadiusToSigma(1))); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 31 | |
| 32 | // copy the paint using the copy constructor and check they are the same |
| 33 | SkPaint copiedPaint = paint; |
Mike Reed | 5f528e5 | 2019-01-28 10:57:28 -0500 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, paint.getHash() == copiedPaint.getHash()); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 36 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 37 | // copy the paint using the equal operator and check they are the same |
| 38 | copiedPaint = paint; |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, paint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 40 | |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 41 | // clean the paint and check they are back to their initial states |
| 42 | SkPaint cleanPaint; |
| 43 | paint.reset(); |
| 44 | copiedPaint.reset(); |
robertphillips@google.com | b265741 | 2013-08-07 22:36:29 +0000 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, cleanPaint == paint); |
| 46 | REPORTER_ASSERT(reporter, cleanPaint == copiedPaint); |
djsollen@google.com | b44cd65 | 2011-12-01 17:09:21 +0000 | [diff] [blame] | 47 | } |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 48 | |
| 49 | // found and fixed for webkit: mishandling when we hit recursion limit on |
| 50 | // mostly degenerate cubic flatness test |
commit-bot@chromium.org | e8807f4 | 2014-03-24 23:03:11 +0000 | [diff] [blame] | 51 | DEF_TEST(Paint_regression_cubic, reporter) { |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 52 | SkPath path, stroke; |
| 53 | SkPaint paint; |
| 54 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 55 | path.moveTo(460.2881309415525f, |
| 56 | 303.250847066498f); |
| 57 | path.cubicTo(463.36378422175284f, |
| 58 | 302.1169735073363f, |
| 59 | 456.32239330810046f, |
| 60 | 304.720354932878f, |
| 61 | 453.15255460013304f, |
| 62 | 305.788586869862f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 64 | SkRect fillR, strokeR; |
| 65 | fillR = path.getBounds(); |
| 66 | |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
| 68 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 69 | paint.getFillPath(path, &stroke); |
| 70 | strokeR = stroke.getBounds(); |
| 71 | |
| 72 | SkRect maxR = fillR; |
Brian Osman | 116b33e | 2020-02-05 13:34:09 -0500 | [diff] [blame] | 73 | SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter()); |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 74 | SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ? |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 75 | paint.getStrokeWidth() * miter : |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 76 | paint.getStrokeWidth(); |
| 77 | maxR.inset(-inset, -inset); |
| 78 | |
| 79 | // test that our stroke didn't explode |
| 80 | REPORTER_ASSERT(reporter, maxR.contains(strokeR)); |
| 81 | } |
| 82 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 83 | DEF_TEST(Paint_flattening, reporter) { |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 84 | const SkPaint::Cap caps[] = { |
| 85 | SkPaint::kButt_Cap, |
| 86 | SkPaint::kRound_Cap, |
| 87 | SkPaint::kSquare_Cap, |
| 88 | }; |
| 89 | const SkPaint::Join joins[] = { |
| 90 | SkPaint::kMiter_Join, |
| 91 | SkPaint::kRound_Join, |
| 92 | SkPaint::kBevel_Join, |
| 93 | }; |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 94 | const SkPaint::Style styles[] = { |
| 95 | SkPaint::kFill_Style, |
| 96 | SkPaint::kStroke_Style, |
| 97 | SkPaint::kStrokeAndFill_Style, |
| 98 | }; |
| 99 | |
| 100 | #define FOR_SETUP(index, array, setter) \ |
| 101 | for (size_t index = 0; index < SK_ARRAY_COUNT(array); ++index) { \ |
Mike Reed | 74b7677 | 2019-01-04 14:01:06 -0500 | [diff] [blame] | 102 | paint.setter(array[index]); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 103 | |
| 104 | SkPaint paint; |
Mike Reed | 74b7677 | 2019-01-04 14:01:06 -0500 | [diff] [blame] | 105 | paint.setAntiAlias(true); |
| 106 | |
| 107 | // 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] | 108 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 109 | FOR_SETUP(l, caps, setStrokeCap) |
| 110 | FOR_SETUP(m, joins, setStrokeJoin) |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 111 | FOR_SETUP(p, styles, setStyle) |
| 112 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 113 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 114 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 115 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 116 | SkAutoMalloc buf(writer.bytesWritten()); |
| 117 | writer.writeToMemory(buf.get()); |
| 118 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
skia.committer@gmail.com | 667b98d | 2014-04-17 03:05:10 +0000 | [diff] [blame] | 119 | |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 120 | SkPaint paint2; |
Mike Reed | 31ba6fe | 2019-01-14 17:36:54 -0500 | [diff] [blame] | 121 | SkPaintPriv::Unflatten(&paint2, reader, nullptr); |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, paint2 == paint); |
| 123 | |
Mike Reed | 1de89c4 | 2021-01-30 09:10:41 -0500 | [diff] [blame] | 124 | }}} |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 125 | #undef FOR_SETUP |
| 126 | |
| 127 | } |
| 128 | |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 129 | // 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] | 130 | DEF_TEST(Paint_regression_measureText, reporter) { |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 131 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 132 | SkFont font; |
| 133 | font.setSize(12.0f); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 134 | |
| 135 | SkRect r; |
| 136 | r.setLTRB(SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN, SK_ScalarNaN); |
| 137 | |
| 138 | // test that the rect was reset |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 139 | font.measureText("", 0, SkTextEncoding::kUTF8, &r); |
djsollen@google.com | 46348e2 | 2013-03-04 19:47:42 +0000 | [diff] [blame] | 140 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 141 | } |
| 142 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 143 | #define ASSERT(expr) REPORTER_ASSERT(r, expr) |
| 144 | |
mtklein | 610a015 | 2014-09-25 11:57:53 -0700 | [diff] [blame] | 145 | DEF_TEST(Paint_MoreFlattening, r) { |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 146 | SkPaint paint; |
| 147 | paint.setColor(0x00AABBCC); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 148 | paint.setBlendMode(SkBlendMode::kModulate); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 149 | |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 150 | SkBinaryWriteBuffer writer; |
Cary Clark | 60ca867 | 2018-03-06 15:09:27 -0500 | [diff] [blame] | 151 | SkPaintPriv::Flatten(paint, writer); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 152 | |
mtklein | b4c899d | 2016-04-29 13:58:09 -0700 | [diff] [blame] | 153 | SkAutoMalloc buf(writer.bytesWritten()); |
| 154 | writer.writeToMemory(buf.get()); |
| 155 | SkReadBuffer reader(buf.get(), writer.bytesWritten()); |
| 156 | |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 157 | SkPaint other; |
Mike Reed | 31ba6fe | 2019-01-14 17:36:54 -0500 | [diff] [blame] | 158 | SkPaintPriv::Unflatten(&other, reader, nullptr); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 159 | ASSERT(reader.offset() == writer.bytesWritten()); |
| 160 | |
| 161 | // No matter the encoding, these must always hold. |
| 162 | ASSERT(other.getColor() == paint.getColor()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 163 | ASSERT(other.getBlendMode() == paint.getBlendMode()); |
commit-bot@chromium.org | aca1c01 | 2014-02-21 18:18:05 +0000 | [diff] [blame] | 164 | } |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 165 | |
| 166 | DEF_TEST(Paint_getHash, r) { |
| 167 | // Try not to inspect the actual hash values in here. |
| 168 | // We might want to change the hash function. |
| 169 | |
| 170 | SkPaint paint; |
| 171 | const uint32_t defaultHash = paint.getHash(); |
| 172 | |
| 173 | // Check that some arbitrary field affects the hash. |
| 174 | paint.setColor(0xFF00FF00); |
| 175 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
| 176 | paint.setColor(SK_ColorBLACK); // Reset to default value. |
| 177 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 178 | |
Mike Reed | 3c03c85 | 2019-01-28 15:02:01 +0000 | [diff] [blame] | 179 | // This is part of fBitfields, the last field we hash. |
Mike Reed | 5f528e5 | 2019-01-28 10:57:28 -0500 | [diff] [blame] | 180 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | 3c03c85 | 2019-01-28 15:02:01 +0000 | [diff] [blame] | 181 | REPORTER_ASSERT(r, paint.getHash() != defaultHash); |
Mike Reed | 5f528e5 | 2019-01-28 10:57:28 -0500 | [diff] [blame] | 182 | paint.setBlendMode(SkBlendMode::kSrcOver); |
mtklein | fb1fe4f | 2014-10-07 09:26:10 -0700 | [diff] [blame] | 183 | REPORTER_ASSERT(r, paint.getHash() == defaultHash); |
| 184 | } |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 185 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 186 | #include "include/effects/SkColorMatrixFilter.h" |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 187 | |
| 188 | DEF_TEST(Paint_nothingToDraw, r) { |
| 189 | SkPaint paint; |
| 190 | |
| 191 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 192 | paint.setAlpha(0); |
| 193 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 194 | |
| 195 | paint.setAlpha(0xFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 196 | paint.setBlendMode(SkBlendMode::kDst); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 197 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 198 | |
| 199 | paint.setAlpha(0); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 200 | paint.setBlendMode(SkBlendMode::kSrcOver); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 201 | |
| 202 | SkColorMatrix cm; |
| 203 | cm.setIdentity(); // does not change alpha |
Mike Reed | 50d79af | 2019-04-21 22:17:03 -0400 | [diff] [blame] | 204 | paint.setColorFilter(SkColorFilters::Matrix(cm)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 205 | REPORTER_ASSERT(r, paint.nothingToDraw()); |
| 206 | |
Mike Reed | 68eb8c2 | 2019-05-02 11:31:43 -0400 | [diff] [blame] | 207 | cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha |
Mike Reed | 50d79af | 2019-04-21 22:17:03 -0400 | [diff] [blame] | 208 | paint.setColorFilter(SkColorFilters::Matrix(cm)); |
reed | f539b8c | 2014-11-11 12:51:33 -0800 | [diff] [blame] | 209 | REPORTER_ASSERT(r, !paint.nothingToDraw()); |
| 210 | } |
Mike Reed | 477fb91 | 2018-11-11 20:37:45 -0500 | [diff] [blame] | 211 | |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 212 | DEF_TEST(Font_getpos, r) { |
| 213 | SkFont font; |
| 214 | const char text[] = "Hamburgefons!@#!#23425,./;'[]"; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 215 | int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8); |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 216 | SkAutoTArray<uint16_t> glyphStorage(count); |
| 217 | uint16_t* glyphs = glyphStorage.get(); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 218 | (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count); |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 219 | |
| 220 | SkAutoTArray<SkScalar> widthStorage(count); |
| 221 | SkAutoTArray<SkScalar> xposStorage(count); |
| 222 | SkAutoTArray<SkPoint> posStorage(count); |
| 223 | |
| 224 | SkScalar* widths = widthStorage.get(); |
| 225 | SkScalar* xpos = xposStorage.get(); |
| 226 | SkPoint* pos = posStorage.get(); |
| 227 | |
| 228 | for (bool subpix : { false, true }) { |
| 229 | font.setSubpixel(subpix); |
Ben Wagner | 5785e4a | 2019-05-07 16:50:29 -0400 | [diff] [blame] | 230 | for (auto hint : { SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull}) { |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 231 | font.setHinting(hint); |
| 232 | for (auto size : { 1.0f, 12.0f, 100.0f }) { |
| 233 | font.setSize(size); |
| 234 | |
| 235 | font.getWidths(glyphs, count, widths); |
| 236 | font.getXPos(glyphs, count, xpos, 10); |
| 237 | font.getPos(glyphs, count, pos, {10, 20}); |
| 238 | |
| 239 | auto nearly_eq = [](SkScalar a, SkScalar b) { |
| 240 | return SkScalarAbs(a - b) < 0.000001f; |
| 241 | }; |
| 242 | |
| 243 | SkScalar x = 10; |
| 244 | for (int i = 0; i < count; ++i) { |
| 245 | REPORTER_ASSERT(r, nearly_eq(x, xpos[i])); |
| 246 | REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX)); |
| 247 | REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY)); |
| 248 | x += widths[i]; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |