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