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