blob: 438f55845522ed6b6d72ff307500ce13a9e2ef6e [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>
Mike Reedc4745d62019-01-07 09:31:58 -050024#include <SkFont.h>
bsalomon@google.com607d08b2012-08-20 13:55:09 +000025
26static const SkColor bgColor = SK_ColorWHITE;
27
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000028static void create(SkBitmap* bm, SkIRect bound) {
29 bm->allocN32Pixels(bound.width(), bound.height());
bsalomon@google.com607d08b2012-08-20 13:55:09 +000030}
31
bsalomon@google.com607d08b2012-08-20 13:55:09 +000032/** 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 */
37static 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.com607d08b2012-08-20 13:55:09 +000043 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 Wagnerba8feb52018-03-05 17:20:15 -050064/** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
65DEF_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 Canary3560ea72019-01-08 13:01:58 -050081 SkFont font(nullptr, 20);
82 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
83 font.setSubpixel(true);
84
Ben Wagnerba8feb52018-03-05 17:20:15 -050085 SkPaint paint;
86 paint.setColor(SK_ColorGRAY);
Ben Wagnerba8feb52018-03-05 17:20:15 -050087 paint.setStyle(SkPaint::kStroke_Style);
88
89 // Draw a stroked "A" without a dash which will draw something.
Hal Canary3560ea72019-01-08 13:01:58 -050090 drawTextCanvas.drawColor(SK_ColorWHITE);
91 drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -050092
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 Canary3560ea72019-01-08 13:01:58 -050099 drawDashedTextCanvas.drawColor(SK_ColorWHITE);
100 drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500101
102 // Draw nothing.
Hal Canary3560ea72019-01-08 13:01:58 -0500103 emptyCanvas.drawColor(SK_ColorWHITE);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500104
105 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
106 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
107}
108
mtklein875e13c2016-06-19 05:28:33 -0700109// Test drawing text at some unusual coordinates.
110// We measure success by not crashing or asserting.
111DEF_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 Reedc4745d62019-01-07 09:31:58 -0500118 canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
119 canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700120 }
121 for (auto y : oddballs) {
Mike Reedc4745d62019-01-07 09:31:58 -0500122 canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
123 canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700124 }
125}
Ben Wagner453888f2017-06-15 15:41:42 -0400126
127// Test drawing text with some unusual matricies.
128// We measure success by not crashing or asserting.
129DEF_TEST(DrawText_weirdMatricies, r) {
130 auto surface = SkSurface::MakeRasterN32Premul(100,100);
131 auto canvas = surface->getCanvas();
132
Mike Reedc4745d62019-01-07 09:31:58 -0500133 SkFont font;
134 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Ben Wagner453888f2017-06-15 15:41:42 -0400135
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 Reedc4745d62019-01-07 09:31:58 -0500155 font.setSize(testCase.textSize);
Ben Wagner453888f2017-06-15 15:41:42 -0400156 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 Reedc4745d62019-01-07 09:31:58 -0500160 canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
Ben Wagner453888f2017-06-15 15:41:42 -0400161 }
162}