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 | |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 23 | #include <cmath> |
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 | |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 67 | /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */ |
| 68 | DEF_TEST(DrawText_dashout, reporter) { |
| 69 | SkIRect size = SkIRect::MakeWH(64, 64); |
| 70 | |
| 71 | SkBitmap drawTextBitmap; |
| 72 | create(&drawTextBitmap, size); |
| 73 | SkCanvas drawTextCanvas(drawTextBitmap); |
| 74 | |
| 75 | SkBitmap drawDashedTextBitmap; |
| 76 | create(&drawDashedTextBitmap, size); |
| 77 | SkCanvas drawDashedTextCanvas(drawDashedTextBitmap); |
| 78 | |
| 79 | SkBitmap emptyBitmap; |
| 80 | create(&emptyBitmap, size); |
| 81 | SkCanvas emptyCanvas(emptyBitmap); |
| 82 | |
| 83 | SkPoint point = SkPoint::Make(25.0f, 25.0f); |
| 84 | SkPaint paint; |
| 85 | paint.setColor(SK_ColorGRAY); |
| 86 | paint.setTextSize(SkIntToScalar(20)); |
| 87 | paint.setAntiAlias(true); |
| 88 | paint.setSubpixelText(true); |
| 89 | paint.setLCDRenderText(true); |
| 90 | paint.setStyle(SkPaint::kStroke_Style); |
| 91 | |
| 92 | // Draw a stroked "A" without a dash which will draw something. |
| 93 | drawBG(&drawTextCanvas); |
| 94 | drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 95 | |
| 96 | // Draw an "A" but with a dash which will never draw anything. |
| 97 | paint.setStrokeWidth(2); |
| 98 | constexpr SkScalar bigInterval = 10000; |
| 99 | static constexpr SkScalar intervals[] = { 1, bigInterval }; |
| 100 | paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2)); |
| 101 | |
| 102 | drawBG(&drawDashedTextCanvas); |
| 103 | drawDashedTextCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 104 | |
| 105 | // Draw nothing. |
| 106 | drawBG(&emptyCanvas); |
| 107 | |
| 108 | REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size)); |
| 109 | REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size)); |
| 110 | } |
| 111 | |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 112 | // Test drawing text at some unusual coordinates. |
| 113 | // We measure success by not crashing or asserting. |
| 114 | DEF_TEST(DrawText_weirdCoordinates, r) { |
| 115 | auto surface = SkSurface::MakeRasterN32Premul(10,10); |
| 116 | auto canvas = surface->getCanvas(); |
| 117 | |
| 118 | SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f }; |
| 119 | |
| 120 | for (auto x : oddballs) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 121 | canvas->drawString("a", +x, 0.0f, SkPaint()); |
| 122 | canvas->drawString("a", -x, 0.0f, SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 123 | } |
| 124 | for (auto y : oddballs) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 125 | canvas->drawString("a", 0.0f, +y, SkPaint()); |
| 126 | canvas->drawString("a", 0.0f, -y, SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 129 | |
| 130 | // Test drawing text with some unusual matricies. |
| 131 | // We measure success by not crashing or asserting. |
| 132 | DEF_TEST(DrawText_weirdMatricies, r) { |
| 133 | auto surface = SkSurface::MakeRasterN32Premul(100,100); |
| 134 | auto canvas = surface->getCanvas(); |
| 135 | |
| 136 | SkPaint paint; |
| 137 | paint.setAntiAlias(true); |
| 138 | paint.setLCDRenderText(true); |
| 139 | |
| 140 | struct { |
| 141 | SkScalar textSize; |
| 142 | SkScalar matrix[9]; |
| 143 | } testCases[] = { |
| 144 | // 2x2 singular |
| 145 | {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 146 | {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}}, |
| 147 | {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 148 | {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}}, |
| 149 | {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}}, |
| 150 | {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 151 | {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 152 | {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}}, |
| 153 | {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}}, |
| 154 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 . |
| 155 | { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}}, |
| 156 | }; |
| 157 | |
| 158 | for (const auto& testCase : testCases) { |
| 159 | paint.setTextSize(testCase.textSize); |
| 160 | const SkScalar(&m)[9] = testCase.matrix; |
| 161 | SkMatrix mat; |
| 162 | mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); |
| 163 | canvas->setMatrix(mat); |
| 164 | canvas->drawString("Hamburgefons", 10, 10, paint); |
| 165 | } |
| 166 | } |