bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +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 | */ |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColor.h" |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame^] | 11 | #include "SkDashPathEffect.h" |
| 12 | #include "SkMatrix.h" |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 13 | #include "SkPaint.h" |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame^] | 14 | #include "SkPathEffect.h" |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 15 | #include "SkPoint.h" |
| 16 | #include "SkRect.h" |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame^] | 17 | #include "SkRefCnt.h" |
| 18 | #include "SkScalar.h" |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 19 | #include "SkSurface.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "Test.h" |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame^] | 22 | |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 23 | #include <math.h> |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 24 | |
| 25 | static const SkColor bgColor = SK_ColorWHITE; |
| 26 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 27 | static void create(SkBitmap* bm, SkIRect bound) { |
| 28 | bm->allocN32Pixels(bound.width(), bound.height()); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void drawBG(SkCanvas* canvas) { |
| 32 | canvas->drawColor(bgColor); |
| 33 | } |
| 34 | |
| 35 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 36 | implies that everything outside is "bgColor". |
| 37 | Checks that all overlap is the same and that all non-overlap on the |
| 38 | ref is "bgColor". |
| 39 | */ |
| 40 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 41 | const SkBitmap& test, const SkIRect& itest) |
| 42 | { |
| 43 | const int xOff = itest.fLeft - iref.fLeft; |
| 44 | const int yOff = itest.fTop - iref.fTop; |
| 45 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 46 | for (int y = 0; y < test.height(); ++y) { |
| 47 | for (int x = 0; x < test.width(); ++x) { |
| 48 | SkColor testColor = test.getColor(x, y); |
| 49 | int refX = x + xOff; |
| 50 | int refY = y + yOff; |
| 51 | SkColor refColor; |
| 52 | if (refX >= 0 && refX < ref.width() && |
| 53 | refY >= 0 && refY < ref.height()) |
| 54 | { |
| 55 | refColor = ref.getColor(refX, refY); |
| 56 | } else { |
| 57 | refColor = bgColor; |
| 58 | } |
| 59 | if (refColor != testColor) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 67 | DEF_TEST(DrawText, reporter) { |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 68 | SkPaint paint; |
| 69 | paint.setColor(SK_ColorGRAY); |
| 70 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 72 | SkIRect drawTextRect = SkIRect::MakeWH(64, 64); |
| 73 | SkBitmap drawTextBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 74 | create(&drawTextBitmap, drawTextRect); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 75 | SkCanvas drawTextCanvas(drawTextBitmap); |
| 76 | |
| 77 | SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64); |
| 78 | SkBitmap drawPosTextBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 79 | create(&drawPosTextBitmap, drawPosTextRect); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 80 | SkCanvas drawPosTextCanvas(drawPosTextBitmap); |
| 81 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 82 | // Two test cases "A" for the normal path through the code, and " " to check the |
| 83 | // early return path. |
| 84 | const char* cases[] = {"A", " "}; |
| 85 | for (auto c : cases) { |
| 86 | for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) { |
| 87 | for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) { |
| 88 | SkPoint point = SkPoint::Make(25.0f + offsetX, |
| 89 | 25.0f + offsetY); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 90 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 91 | for (int align = 0; align < SkPaint::kAlignCount; ++align) { |
| 92 | paint.setTextAlign(static_cast<SkPaint::Align>(align)); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 93 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 94 | for (unsigned int flags = 0; flags < (1 << 3); ++flags) { |
| 95 | static const unsigned int antiAliasFlag = 1; |
| 96 | static const unsigned int subpixelFlag = 1 << 1; |
| 97 | static const unsigned int lcdFlag = 1 << 2; |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 98 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 99 | paint.setAntiAlias(SkToBool(flags & antiAliasFlag)); |
| 100 | paint.setSubpixelText(SkToBool(flags & subpixelFlag)); |
| 101 | paint.setLCDRenderText(SkToBool(flags & lcdFlag)); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 102 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 103 | // Test: drawText and drawPosText draw the same. |
| 104 | drawBG(&drawTextCanvas); |
| 105 | drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 106 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 107 | drawBG(&drawPosTextCanvas); |
| 108 | drawPosTextCanvas.drawPosText(c, 1, &point, paint); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 109 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, |
| 111 | compare(drawTextBitmap, drawTextRect, |
| 112 | drawPosTextBitmap, drawPosTextRect)); |
| 113 | } |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 119 | |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame^] | 120 | /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */ |
| 121 | DEF_TEST(DrawText_dashout, reporter) { |
| 122 | SkIRect size = SkIRect::MakeWH(64, 64); |
| 123 | |
| 124 | SkBitmap drawTextBitmap; |
| 125 | create(&drawTextBitmap, size); |
| 126 | SkCanvas drawTextCanvas(drawTextBitmap); |
| 127 | |
| 128 | SkBitmap drawDashedTextBitmap; |
| 129 | create(&drawDashedTextBitmap, size); |
| 130 | SkCanvas drawDashedTextCanvas(drawDashedTextBitmap); |
| 131 | |
| 132 | SkBitmap emptyBitmap; |
| 133 | create(&emptyBitmap, size); |
| 134 | SkCanvas emptyCanvas(emptyBitmap); |
| 135 | |
| 136 | SkPoint point = SkPoint::Make(25.0f, 25.0f); |
| 137 | SkPaint paint; |
| 138 | paint.setColor(SK_ColorGRAY); |
| 139 | paint.setTextSize(SkIntToScalar(20)); |
| 140 | paint.setAntiAlias(true); |
| 141 | paint.setSubpixelText(true); |
| 142 | paint.setLCDRenderText(true); |
| 143 | paint.setStyle(SkPaint::kStroke_Style); |
| 144 | |
| 145 | // Draw a stroked "A" without a dash which will draw something. |
| 146 | drawBG(&drawTextCanvas); |
| 147 | drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 148 | |
| 149 | // Draw an "A" but with a dash which will never draw anything. |
| 150 | paint.setStrokeWidth(2); |
| 151 | constexpr SkScalar bigInterval = 10000; |
| 152 | static constexpr SkScalar intervals[] = { 1, bigInterval }; |
| 153 | paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2)); |
| 154 | |
| 155 | drawBG(&drawDashedTextCanvas); |
| 156 | drawDashedTextCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 157 | |
| 158 | // Draw nothing. |
| 159 | drawBG(&emptyCanvas); |
| 160 | |
| 161 | REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size)); |
| 162 | REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size)); |
| 163 | } |
| 164 | |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 165 | // Test drawing text at some unusual coordinates. |
| 166 | // We measure success by not crashing or asserting. |
| 167 | DEF_TEST(DrawText_weirdCoordinates, r) { |
| 168 | auto surface = SkSurface::MakeRasterN32Premul(10,10); |
| 169 | auto canvas = surface->getCanvas(); |
| 170 | |
| 171 | SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f }; |
| 172 | |
| 173 | for (auto x : oddballs) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 174 | canvas->drawString("a", +x, 0.0f, SkPaint()); |
| 175 | canvas->drawString("a", -x, 0.0f, SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 176 | } |
| 177 | for (auto y : oddballs) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 178 | canvas->drawString("a", 0.0f, +y, SkPaint()); |
| 179 | canvas->drawString("a", 0.0f, -y, SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 182 | |
| 183 | // Test drawing text with some unusual matricies. |
| 184 | // We measure success by not crashing or asserting. |
| 185 | DEF_TEST(DrawText_weirdMatricies, r) { |
| 186 | auto surface = SkSurface::MakeRasterN32Premul(100,100); |
| 187 | auto canvas = surface->getCanvas(); |
| 188 | |
| 189 | SkPaint paint; |
| 190 | paint.setAntiAlias(true); |
| 191 | paint.setLCDRenderText(true); |
| 192 | |
| 193 | struct { |
| 194 | SkScalar textSize; |
| 195 | SkScalar matrix[9]; |
| 196 | } testCases[] = { |
| 197 | // 2x2 singular |
| 198 | {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 199 | {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}}, |
| 200 | {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 201 | {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}}, |
| 202 | {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}}, |
| 203 | {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 204 | {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 205 | {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}}, |
| 206 | {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}}, |
| 207 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 . |
| 208 | { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}}, |
| 209 | }; |
| 210 | |
| 211 | for (const auto& testCase : testCases) { |
| 212 | paint.setTextSize(testCase.textSize); |
| 213 | const SkScalar(&m)[9] = testCase.matrix; |
| 214 | SkMatrix mat; |
| 215 | mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); |
| 216 | canvas->setMatrix(mat); |
| 217 | canvas->drawString("Hamburgefons", 10, 10, paint); |
| 218 | } |
| 219 | } |