blob: ac1314050753a434a338736d18132e5dc8cd2c39 [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
bsalomon@google.com607d08b2012-08-20 13:55:09 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColor.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050011#include "SkDashPathEffect.h"
12#include "SkMatrix.h"
bsalomon@google.com607d08b2012-08-20 13:55:09 +000013#include "SkPaint.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050014#include "SkPathEffect.h"
bsalomon@google.com607d08b2012-08-20 13:55:09 +000015#include "SkPoint.h"
16#include "SkRect.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050017#include "SkRefCnt.h"
18#include "SkScalar.h"
mtklein875e13c2016-06-19 05:28:33 -070019#include "SkSurface.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000020#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050022
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040023#include <cmath>
bsalomon@google.com607d08b2012-08-20 13:55:09 +000024
25static const SkColor bgColor = SK_ColorWHITE;
26
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000027static void create(SkBitmap* bm, SkIRect bound) {
28 bm->allocN32Pixels(bound.width(), bound.height());
bsalomon@google.com607d08b2012-08-20 13:55:09 +000029}
30
31static 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 */
40static 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.com607d08b2012-08-20 13:55:09 +000046 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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000067DEF_TEST(DrawText, reporter) {
bsalomon@google.com607d08b2012-08-20 13:55:09 +000068 SkPaint paint;
69 paint.setColor(SK_ColorGRAY);
70 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
bsalomon@google.com607d08b2012-08-20 13:55:09 +000072 SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
73 SkBitmap drawTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000074 create(&drawTextBitmap, drawTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000075 SkCanvas drawTextCanvas(drawTextBitmap);
76
77 SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
78 SkBitmap drawPosTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000079 create(&drawPosTextBitmap, drawPosTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000080 SkCanvas drawPosTextCanvas(drawPosTextBitmap);
81
herb386127f2015-11-12 08:53:42 -080082 // Two test cases "A" for the normal path through the code, and " " to check the
83 // early return path.
84 const char* cases[] = {"A", " "};
85 for (auto c : cases) {
86 for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
87 for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
88 SkPoint point = SkPoint::Make(25.0f + offsetX,
89 25.0f + offsetY);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000090
Herb Derby1e7c6582018-05-21 16:10:17 -040091 for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
92 static const unsigned int antiAliasFlag = 1;
93 static const unsigned int subpixelFlag = 1 << 1;
94 static const unsigned int lcdFlag = 1 << 2;
bsalomon@google.com607d08b2012-08-20 13:55:09 +000095
Herb Derby1e7c6582018-05-21 16:10:17 -040096 paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
97 paint.setSubpixelText(SkToBool(flags & subpixelFlag));
98 paint.setLCDRenderText(SkToBool(flags & lcdFlag));
bsalomon@google.com607d08b2012-08-20 13:55:09 +000099
Herb Derby1e7c6582018-05-21 16:10:17 -0400100 // Test: drawText and drawPosText draw the same.
101 drawBG(&drawTextCanvas);
102 drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000103
Herb Derby1e7c6582018-05-21 16:10:17 -0400104 drawBG(&drawPosTextCanvas);
105 drawPosTextCanvas.drawPosText(c, 1, &point, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000106
Herb Derby1e7c6582018-05-21 16:10:17 -0400107 REPORTER_ASSERT(reporter,
108 compare(drawTextBitmap, drawTextRect,
109 drawPosTextBitmap, drawPosTextRect));
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000110 }
111 }
112 }
113 }
114}
mtklein875e13c2016-06-19 05:28:33 -0700115
Ben Wagnerba8feb52018-03-05 17:20:15 -0500116/** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
117DEF_TEST(DrawText_dashout, reporter) {
118 SkIRect size = SkIRect::MakeWH(64, 64);
119
120 SkBitmap drawTextBitmap;
121 create(&drawTextBitmap, size);
122 SkCanvas drawTextCanvas(drawTextBitmap);
123
124 SkBitmap drawDashedTextBitmap;
125 create(&drawDashedTextBitmap, size);
126 SkCanvas drawDashedTextCanvas(drawDashedTextBitmap);
127
128 SkBitmap emptyBitmap;
129 create(&emptyBitmap, size);
130 SkCanvas emptyCanvas(emptyBitmap);
131
132 SkPoint point = SkPoint::Make(25.0f, 25.0f);
133 SkPaint paint;
134 paint.setColor(SK_ColorGRAY);
135 paint.setTextSize(SkIntToScalar(20));
136 paint.setAntiAlias(true);
137 paint.setSubpixelText(true);
138 paint.setLCDRenderText(true);
139 paint.setStyle(SkPaint::kStroke_Style);
140
141 // Draw a stroked "A" without a dash which will draw something.
142 drawBG(&drawTextCanvas);
143 drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint);
144
145 // Draw an "A" but with a dash which will never draw anything.
146 paint.setStrokeWidth(2);
147 constexpr SkScalar bigInterval = 10000;
148 static constexpr SkScalar intervals[] = { 1, bigInterval };
149 paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2));
150
151 drawBG(&drawDashedTextCanvas);
152 drawDashedTextCanvas.drawText("A", 1, point.fX, point.fY, paint);
153
154 // Draw nothing.
155 drawBG(&emptyCanvas);
156
157 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
158 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
159}
160
mtklein875e13c2016-06-19 05:28:33 -0700161// Test drawing text at some unusual coordinates.
162// We measure success by not crashing or asserting.
163DEF_TEST(DrawText_weirdCoordinates, r) {
164 auto surface = SkSurface::MakeRasterN32Premul(10,10);
165 auto canvas = surface->getCanvas();
166
167 SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
168
169 for (auto x : oddballs) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400170 canvas->drawString("a", +x, 0.0f, SkPaint());
171 canvas->drawString("a", -x, 0.0f, SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700172 }
173 for (auto y : oddballs) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400174 canvas->drawString("a", 0.0f, +y, SkPaint());
175 canvas->drawString("a", 0.0f, -y, SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700176 }
177}
Ben Wagner453888f2017-06-15 15:41:42 -0400178
179// Test drawing text with some unusual matricies.
180// We measure success by not crashing or asserting.
181DEF_TEST(DrawText_weirdMatricies, r) {
182 auto surface = SkSurface::MakeRasterN32Premul(100,100);
183 auto canvas = surface->getCanvas();
184
185 SkPaint paint;
186 paint.setAntiAlias(true);
187 paint.setLCDRenderText(true);
188
189 struct {
190 SkScalar textSize;
191 SkScalar matrix[9];
192 } testCases[] = {
193 // 2x2 singular
194 {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}},
195 {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}},
196 {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}},
197 {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}},
198 {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}},
199 {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}},
200 {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}},
201 {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}},
202 {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}},
203 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 .
204 { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}},
205 };
206
207 for (const auto& testCase : testCases) {
208 paint.setTextSize(testCase.textSize);
209 const SkScalar(&m)[9] = testCase.matrix;
210 SkMatrix mat;
211 mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
212 canvas->setMatrix(mat);
213 canvas->drawString("Hamburgefons", 10, 10, paint);
214 }
215}