blob: 3f4c9ab6a8343e394f1d0961c8e5d0cb370bc940 [file] [log] [blame]
bsalomon@google.com607d08b2012-08-20 13:55:09 +00001/*
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.com607d08b2012-08-20 13:55:09 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Wagner9707a7e2019-05-06 17:17:19 -040020#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkTypes.h"
22#include "include/effects/SkDashPathEffect.h"
23#include "tests/Test.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050024
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040025#include <cmath>
bsalomon@google.com607d08b2012-08-20 13:55:09 +000026
27static const SkColor bgColor = SK_ColorWHITE;
28
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000029static void create(SkBitmap* bm, SkIRect bound) {
30 bm->allocN32Pixels(bound.width(), bound.height());
bsalomon@google.com607d08b2012-08-20 13:55:09 +000031}
32
bsalomon@google.com607d08b2012-08-20 13:55:09 +000033/** 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 */
38static 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.com607d08b2012-08-20 13:55:09 +000044 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 Wagnerba8feb52018-03-05 17:20:15 -050065/** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
66DEF_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 Canary3560ea72019-01-08 13:01:58 -050082 SkFont font(nullptr, 20);
83 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
84 font.setSubpixel(true);
85
Ben Wagnerba8feb52018-03-05 17:20:15 -050086 SkPaint paint;
87 paint.setColor(SK_ColorGRAY);
Ben Wagnerba8feb52018-03-05 17:20:15 -050088 paint.setStyle(SkPaint::kStroke_Style);
89
90 // Draw a stroked "A" without a dash which will draw something.
Hal Canary3560ea72019-01-08 13:01:58 -050091 drawTextCanvas.drawColor(SK_ColorWHITE);
92 drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -050093
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 Canary3560ea72019-01-08 13:01:58 -0500100 drawDashedTextCanvas.drawColor(SK_ColorWHITE);
101 drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500102
103 // Draw nothing.
Hal Canary3560ea72019-01-08 13:01:58 -0500104 emptyCanvas.drawColor(SK_ColorWHITE);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500105
106 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
107 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
108}
109
mtklein875e13c2016-06-19 05:28:33 -0700110// Test drawing text at some unusual coordinates.
111// We measure success by not crashing or asserting.
112DEF_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 Reedc4745d62019-01-07 09:31:58 -0500119 canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
120 canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700121 }
122 for (auto y : oddballs) {
Mike Reedc4745d62019-01-07 09:31:58 -0500123 canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
124 canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700125 }
126}
Ben Wagner453888f2017-06-15 15:41:42 -0400127
128// Test drawing text with some unusual matricies.
129// We measure success by not crashing or asserting.
130DEF_TEST(DrawText_weirdMatricies, r) {
131 auto surface = SkSurface::MakeRasterN32Premul(100,100);
132 auto canvas = surface->getCanvas();
133
Mike Reedc4745d62019-01-07 09:31:58 -0500134 SkFont font;
135 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Ben Wagner453888f2017-06-15 15:41:42 -0400136
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 Reedc4745d62019-01-07 09:31:58 -0500156 font.setSize(testCase.textSize);
Ben Wagner453888f2017-06-15 15:41:42 -0400157 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 Reedc4745d62019-01-07 09:31:58 -0500161 canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
Ben Wagner453888f2017-06-15 15:41:42 -0400162 }
163}