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> |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 24 | #include <SkFont.h> |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 25 | |
| 26 | static const SkColor bgColor = SK_ColorWHITE; |
| 27 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 28 | static void create(SkBitmap* bm, SkIRect bound) { |
| 29 | bm->allocN32Pixels(bound.width(), bound.height()); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 32 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 33 | implies that everything outside is "bgColor". |
| 34 | Checks that all overlap is the same and that all non-overlap on the |
| 35 | ref is "bgColor". |
| 36 | */ |
| 37 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 38 | const SkBitmap& test, const SkIRect& itest) |
| 39 | { |
| 40 | const int xOff = itest.fLeft - iref.fLeft; |
| 41 | const int yOff = itest.fTop - iref.fTop; |
| 42 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 43 | for (int y = 0; y < test.height(); ++y) { |
| 44 | for (int x = 0; x < test.width(); ++x) { |
| 45 | SkColor testColor = test.getColor(x, y); |
| 46 | int refX = x + xOff; |
| 47 | int refY = y + yOff; |
| 48 | SkColor refColor; |
| 49 | if (refX >= 0 && refX < ref.width() && |
| 50 | refY >= 0 && refY < ref.height()) |
| 51 | { |
| 52 | refColor = ref.getColor(refX, refY); |
| 53 | } else { |
| 54 | refColor = bgColor; |
| 55 | } |
| 56 | if (refColor != testColor) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 64 | /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */ |
| 65 | DEF_TEST(DrawText_dashout, reporter) { |
| 66 | SkIRect size = SkIRect::MakeWH(64, 64); |
| 67 | |
| 68 | SkBitmap drawTextBitmap; |
| 69 | create(&drawTextBitmap, size); |
| 70 | SkCanvas drawTextCanvas(drawTextBitmap); |
| 71 | |
| 72 | SkBitmap drawDashedTextBitmap; |
| 73 | create(&drawDashedTextBitmap, size); |
| 74 | SkCanvas drawDashedTextCanvas(drawDashedTextBitmap); |
| 75 | |
| 76 | SkBitmap emptyBitmap; |
| 77 | create(&emptyBitmap, size); |
| 78 | SkCanvas emptyCanvas(emptyBitmap); |
| 79 | |
| 80 | SkPoint point = SkPoint::Make(25.0f, 25.0f); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 81 | SkFont font(nullptr, 20); |
| 82 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 83 | font.setSubpixel(true); |
| 84 | |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 85 | SkPaint paint; |
| 86 | paint.setColor(SK_ColorGRAY); |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 87 | paint.setStyle(SkPaint::kStroke_Style); |
| 88 | |
| 89 | // Draw a stroked "A" without a dash which will draw something. |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 90 | drawTextCanvas.drawColor(SK_ColorWHITE); |
| 91 | drawTextCanvas.drawString("A", point.fX, point.fY, font, paint); |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 92 | |
| 93 | // Draw an "A" but with a dash which will never draw anything. |
| 94 | paint.setStrokeWidth(2); |
| 95 | constexpr SkScalar bigInterval = 10000; |
| 96 | static constexpr SkScalar intervals[] = { 1, bigInterval }; |
| 97 | paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2)); |
| 98 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 99 | drawDashedTextCanvas.drawColor(SK_ColorWHITE); |
| 100 | drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint); |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 101 | |
| 102 | // Draw nothing. |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 103 | emptyCanvas.drawColor(SK_ColorWHITE); |
Ben Wagner | ba8feb5 | 2018-03-05 17:20:15 -0500 | [diff] [blame] | 104 | |
| 105 | REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size)); |
| 106 | REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size)); |
| 107 | } |
| 108 | |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 109 | // Test drawing text at some unusual coordinates. |
| 110 | // We measure success by not crashing or asserting. |
| 111 | DEF_TEST(DrawText_weirdCoordinates, r) { |
| 112 | auto surface = SkSurface::MakeRasterN32Premul(10,10); |
| 113 | auto canvas = surface->getCanvas(); |
| 114 | |
| 115 | SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f }; |
| 116 | |
| 117 | for (auto x : oddballs) { |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 118 | canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint()); |
| 119 | canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 120 | } |
| 121 | for (auto y : oddballs) { |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 122 | canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint()); |
| 123 | canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint()); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 126 | |
| 127 | // Test drawing text with some unusual matricies. |
| 128 | // We measure success by not crashing or asserting. |
| 129 | DEF_TEST(DrawText_weirdMatricies, r) { |
| 130 | auto surface = SkSurface::MakeRasterN32Premul(100,100); |
| 131 | auto canvas = surface->getCanvas(); |
| 132 | |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 133 | SkFont font; |
| 134 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 135 | |
| 136 | struct { |
| 137 | SkScalar textSize; |
| 138 | SkScalar matrix[9]; |
| 139 | } testCases[] = { |
| 140 | // 2x2 singular |
| 141 | {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 142 | {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}}, |
| 143 | {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 144 | {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}}, |
| 145 | {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}}, |
| 146 | {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}}, |
| 147 | {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}}, |
| 148 | {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}}, |
| 149 | {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}}, |
| 150 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 . |
| 151 | { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}}, |
| 152 | }; |
| 153 | |
| 154 | for (const auto& testCase : testCases) { |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 155 | font.setSize(testCase.textSize); |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 156 | const SkScalar(&m)[9] = testCase.matrix; |
| 157 | SkMatrix mat; |
| 158 | mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); |
| 159 | canvas->setMatrix(mat); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 160 | canvas->drawString("Hamburgefons", 10, 10, font, SkPaint()); |
Ben Wagner | 453888f | 2017-06-15 15:41:42 -0400 | [diff] [blame] | 161 | } |
| 162 | } |