blob: 981b6f66f39c7bf072263b9e5ba59a26d7c48abf [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"
Herb Derbyb6cce2d2021-05-03 10:46:47 -040020#include "include/core/SkTextBlob.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040021#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypes.h"
23#include "include/effects/SkDashPathEffect.h"
24#include "tests/Test.h"
Ben Wagnerba8feb52018-03-05 17:20:15 -050025
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040026#include <cmath>
bsalomon@google.com607d08b2012-08-20 13:55:09 +000027
28static const SkColor bgColor = SK_ColorWHITE;
29
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000030static void create(SkBitmap* bm, SkIRect bound) {
31 bm->allocN32Pixels(bound.width(), bound.height());
bsalomon@google.com607d08b2012-08-20 13:55:09 +000032}
33
bsalomon@google.com607d08b2012-08-20 13:55:09 +000034/** Assumes that the ref draw was completely inside ref canvas --
35 implies that everything outside is "bgColor".
36 Checks that all overlap is the same and that all non-overlap on the
37 ref is "bgColor".
38 */
39static bool compare(const SkBitmap& ref, const SkIRect& iref,
40 const SkBitmap& test, const SkIRect& itest)
41{
42 const int xOff = itest.fLeft - iref.fLeft;
43 const int yOff = itest.fTop - iref.fTop;
44
bsalomon@google.com607d08b2012-08-20 13:55:09 +000045 for (int y = 0; y < test.height(); ++y) {
46 for (int x = 0; x < test.width(); ++x) {
47 SkColor testColor = test.getColor(x, y);
48 int refX = x + xOff;
49 int refY = y + yOff;
50 SkColor refColor;
51 if (refX >= 0 && refX < ref.width() &&
52 refY >= 0 && refY < ref.height())
53 {
54 refColor = ref.getColor(refX, refY);
55 } else {
56 refColor = bgColor;
57 }
58 if (refColor != testColor) {
59 return false;
60 }
61 }
62 }
63 return true;
64}
65
Ben Wagnerba8feb52018-03-05 17:20:15 -050066/** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
67DEF_TEST(DrawText_dashout, reporter) {
68 SkIRect size = SkIRect::MakeWH(64, 64);
69
70 SkBitmap drawTextBitmap;
71 create(&drawTextBitmap, size);
72 SkCanvas drawTextCanvas(drawTextBitmap);
73
74 SkBitmap drawDashedTextBitmap;
75 create(&drawDashedTextBitmap, size);
76 SkCanvas drawDashedTextCanvas(drawDashedTextBitmap);
77
78 SkBitmap emptyBitmap;
79 create(&emptyBitmap, size);
80 SkCanvas emptyCanvas(emptyBitmap);
81
82 SkPoint point = SkPoint::Make(25.0f, 25.0f);
Hal Canary3560ea72019-01-08 13:01:58 -050083 SkFont font(nullptr, 20);
84 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
85 font.setSubpixel(true);
86
Ben Wagnerba8feb52018-03-05 17:20:15 -050087 SkPaint paint;
88 paint.setColor(SK_ColorGRAY);
Ben Wagnerba8feb52018-03-05 17:20:15 -050089 paint.setStyle(SkPaint::kStroke_Style);
90
91 // Draw a stroked "A" without a dash which will draw something.
Hal Canary3560ea72019-01-08 13:01:58 -050092 drawTextCanvas.drawColor(SK_ColorWHITE);
93 drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -050094
95 // Draw an "A" but with a dash which will never draw anything.
96 paint.setStrokeWidth(2);
97 constexpr SkScalar bigInterval = 10000;
98 static constexpr SkScalar intervals[] = { 1, bigInterval };
99 paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2));
100
Hal Canary3560ea72019-01-08 13:01:58 -0500101 drawDashedTextCanvas.drawColor(SK_ColorWHITE);
102 drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500103
104 // Draw nothing.
Hal Canary3560ea72019-01-08 13:01:58 -0500105 emptyCanvas.drawColor(SK_ColorWHITE);
Ben Wagnerba8feb52018-03-05 17:20:15 -0500106
107 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
108 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
109}
110
mtklein875e13c2016-06-19 05:28:33 -0700111// Test drawing text at some unusual coordinates.
112// We measure success by not crashing or asserting.
113DEF_TEST(DrawText_weirdCoordinates, r) {
114 auto surface = SkSurface::MakeRasterN32Premul(10,10);
115 auto canvas = surface->getCanvas();
116
117 SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
118
119 for (auto x : oddballs) {
Mike Reedc4745d62019-01-07 09:31:58 -0500120 canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
121 canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700122 }
123 for (auto y : oddballs) {
Mike Reedc4745d62019-01-07 09:31:58 -0500124 canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
125 canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700126 }
127}
Ben Wagner453888f2017-06-15 15:41:42 -0400128
129// Test drawing text with some unusual matricies.
130// We measure success by not crashing or asserting.
131DEF_TEST(DrawText_weirdMatricies, r) {
132 auto surface = SkSurface::MakeRasterN32Premul(100,100);
133 auto canvas = surface->getCanvas();
134
Mike Reedc4745d62019-01-07 09:31:58 -0500135 SkFont font;
136 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Ben Wagner453888f2017-06-15 15:41:42 -0400137
138 struct {
139 SkScalar textSize;
140 SkScalar matrix[9];
141 } testCases[] = {
142 // 2x2 singular
143 {10, { 0, 0, 0, 0, 0, 0, 0, 0, 1}},
144 {10, { 0, 0, 0, 0, 1, 0, 0, 0, 1}},
145 {10, { 0, 0, 0, 1, 0, 0, 0, 0, 1}},
146 {10, { 0, 0, 0, 1, 1, 0, 0, 0, 1}},
147 {10, { 0, 1, 0, 0, 1, 0, 0, 0, 1}},
148 {10, { 1, 0, 0, 0, 0, 0, 0, 0, 1}},
149 {10, { 1, 0, 0, 1, 0, 0, 0, 0, 1}},
150 {10, { 1, 1, 0, 0, 0, 0, 0, 0, 1}},
151 {10, { 1, 1, 0, 1, 1, 0, 0, 0, 1}},
152 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 .
153 { 1, {10, 20, 0, 20, 40, 0, 0, 0, 1}},
154 };
155
156 for (const auto& testCase : testCases) {
Mike Reedc4745d62019-01-07 09:31:58 -0500157 font.setSize(testCase.textSize);
Ben Wagner453888f2017-06-15 15:41:42 -0400158 const SkScalar(&m)[9] = testCase.matrix;
159 SkMatrix mat;
160 mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
161 canvas->setMatrix(mat);
Mike Reedc4745d62019-01-07 09:31:58 -0500162 canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
Ben Wagner453888f2017-06-15 15:41:42 -0400163 }
164}
Herb Derbyb6cce2d2021-05-03 10:46:47 -0400165
166// This produces no glyphs, and is to check that buffers from previous draws don't get
167// reused.
168DEF_TEST(DrawText_noglyphs, r) {
169 auto surface = SkSurface::MakeRasterN32Premul(100,100);
170 auto canvas = surface->getCanvas();
171 auto text = "Hamburgfons";
172 {
173 // scoped to ensure blob is deleted.
174 auto blob = SkTextBlob::MakeFromText(text, strlen(text), SkFont());
175 canvas->drawTextBlob(blob, 10, 10, SkPaint());
176 }
177 canvas->drawString(
178 "\x0d\xf3\xf2\xf2\xe9\x0d\x0d\x0d\x05\x0d\x0d\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3",
179 10, 20, SkFont(), SkPaint());
180}