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  */
7 
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" // IWYU pragma: keep
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"
20 #include "include/core/SkTextBlob.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "include/effects/SkDashPathEffect.h"
24 #include "tests/Test.h"
25 
26 #include <cmath>
27 #include <string>
28 
29 static const SkColor bgColor = SK_ColorWHITE;
30 
create(SkBitmap * bm,SkIRect bound)31 static void create(SkBitmap* bm, SkIRect bound) {
32     bm->allocN32Pixels(bound.width(), bound.height());
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  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)40 static 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 
46     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 
67 /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
DEF_TEST(DrawText_dashout,reporter)68 DEF_TEST(DrawText_dashout, reporter) {
69     SkIRect size = SkIRect::MakeWH(64, 64);
70 
71     SkBitmap drawTextBitmap;
72     create(&drawTextBitmap, size);
73     SkCanvas drawTextCanvas(drawTextBitmap);
74 
75     SkBitmap drawDashedTextBitmap;
76     create(&drawDashedTextBitmap, size);
77     SkCanvas drawDashedTextCanvas(drawDashedTextBitmap);
78 
79     SkBitmap emptyBitmap;
80     create(&emptyBitmap, size);
81     SkCanvas emptyCanvas(emptyBitmap);
82 
83     SkPoint point = SkPoint::Make(25.0f, 25.0f);
84     SkFont font(nullptr, 20);
85     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
86     font.setSubpixel(true);
87 
88     SkPaint paint;
89     paint.setColor(SK_ColorGRAY);
90     paint.setStyle(SkPaint::kStroke_Style);
91 
92     // Draw a stroked "A" without a dash which will draw something.
93     drawTextCanvas.drawColor(SK_ColorWHITE);
94     drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
95 
96     // Draw an "A" but with a dash which will never draw anything.
97     paint.setStrokeWidth(2);
98     constexpr SkScalar bigInterval = 10000;
99     static constexpr SkScalar intervals[] = { 1, bigInterval };
100     paint.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 2));
101 
102     drawDashedTextCanvas.drawColor(SK_ColorWHITE);
103     drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
104 
105     // Draw nothing.
106     emptyCanvas.drawColor(SK_ColorWHITE);
107 
108     REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
109     REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
110 }
111 
112 // Test drawing text at some unusual coordinates.
113 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdCoordinates,r)114 DEF_TEST(DrawText_weirdCoordinates, r) {
115     auto surface = SkSurface::MakeRasterN32Premul(10,10);
116     auto canvas = surface->getCanvas();
117 
118     SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
119 
120     for (auto x : oddballs) {
121         canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
122         canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
123     }
124     for (auto y : oddballs) {
125         canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
126         canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
127     }
128 }
129 
130 // Test drawing text with some unusual matricies.
131 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdMatricies,r)132 DEF_TEST(DrawText_weirdMatricies, r) {
133     auto surface = SkSurface::MakeRasterN32Premul(100,100);
134     auto canvas = surface->getCanvas();
135 
136     SkFont font;
137     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
138 
139     struct {
140         SkScalar textSize;
141         SkScalar matrix[9];
142     } testCases[] = {
143         // 2x2 singular
144         {10, { 0,  0,  0,  0,  0,  0,  0,  0,  1}},
145         {10, { 0,  0,  0,  0,  1,  0,  0,  0,  1}},
146         {10, { 0,  0,  0,  1,  0,  0,  0,  0,  1}},
147         {10, { 0,  0,  0,  1,  1,  0,  0,  0,  1}},
148         {10, { 0,  1,  0,  0,  1,  0,  0,  0,  1}},
149         {10, { 1,  0,  0,  0,  0,  0,  0,  0,  1}},
150         {10, { 1,  0,  0,  1,  0,  0,  0,  0,  1}},
151         {10, { 1,  1,  0,  0,  0,  0,  0,  0,  1}},
152         {10, { 1,  1,  0,  1,  1,  0,  0,  0,  1}},
153         // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 .
154         { 1, {10, 20,  0, 20, 40,  0,  0,  0,  1}},
155     };
156 
157     for (const auto& testCase : testCases) {
158         font.setSize(testCase.textSize);
159         const SkScalar(&m)[9] = testCase.matrix;
160         SkMatrix mat;
161         mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
162         canvas->setMatrix(mat);
163         canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
164     }
165 }
166 
167 // This produces no glyphs, and is to check that buffers from previous draws don't get
168 // reused.
DEF_TEST(DrawText_noglyphs,r)169 DEF_TEST(DrawText_noglyphs, r) {
170     auto surface = SkSurface::MakeRasterN32Premul(100,100);
171     auto canvas = surface->getCanvas();
172     auto text = "Hamburgfons";
173     {
174         // scoped to ensure blob is deleted.
175         auto blob = SkTextBlob::MakeFromText(text, strlen(text), SkFont());
176         canvas->drawTextBlob(blob, 10, 10, SkPaint());
177     }
178     canvas->drawString(
179             "\x0d\xf3\xf2\xf2\xe9\x0d\x0d\x0d\x05\x0d\x0d\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3",
180             10, 20, SkFont(), SkPaint());
181 }
182