xref: /aosp_15_r20/external/skia/tests/DrawTextTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPathEffect.h"  // IWYU pragma: keep
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTextBlob.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkDashPathEffect.h"
25 #include "tests/Test.h"
26 #include "tools/fonts/FontToolUtils.h"
27 
28 #include <array>
29 #include <cmath>
30 #include <cstring>
31 
32 static const SkColor bgColor = SK_ColorWHITE;
33 
create(SkBitmap * bm,SkIRect bound)34 static void create(SkBitmap* bm, SkIRect bound) {
35     bm->allocN32Pixels(bound.width(), bound.height());
36 }
37 
38 /** Assumes that the ref draw was completely inside ref canvas --
39     implies that everything outside is "bgColor".
40     Checks that all overlap is the same and that all non-overlap on the
41     ref is "bgColor".
42  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)43 static bool compare(const SkBitmap& ref, const SkIRect& iref,
44                     const SkBitmap& test, const SkIRect& itest)
45 {
46     const int xOff = itest.fLeft - iref.fLeft;
47     const int yOff = itest.fTop - iref.fTop;
48 
49     for (int y = 0; y < test.height(); ++y) {
50         for (int x = 0; x < test.width(); ++x) {
51             SkColor testColor = test.getColor(x, y);
52             int refX = x + xOff;
53             int refY = y + yOff;
54             SkColor refColor;
55             if (refX >= 0 && refX < ref.width() &&
56                 refY >= 0 && refY < ref.height())
57             {
58                 refColor = ref.getColor(refX, refY);
59             } else {
60                 refColor = bgColor;
61             }
62             if (refColor != testColor) {
63                 return false;
64             }
65         }
66     }
67     return true;
68 }
69 
70 /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
DEF_TEST(DrawText_dashout,reporter)71 DEF_TEST(DrawText_dashout, reporter) {
72     SkIRect size = SkIRect::MakeWH(64, 64);
73 
74     SkBitmap drawTextBitmap;
75     create(&drawTextBitmap, size);
76     SkCanvas drawTextCanvas(drawTextBitmap);
77 
78     SkBitmap drawDashedTextBitmap;
79     create(&drawDashedTextBitmap, size);
80     SkCanvas drawDashedTextCanvas(drawDashedTextBitmap);
81 
82     SkBitmap emptyBitmap;
83     create(&emptyBitmap, size);
84     SkCanvas emptyCanvas(emptyBitmap);
85 
86     SkPoint point = SkPoint::Make(25.0f, 25.0f);
87     SkFont font(ToolUtils::DefaultTypeface(), 20);
88     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
89     font.setSubpixel(true);
90 
91     SkPaint paint;
92     paint.setColor(SK_ColorGRAY);
93     paint.setStyle(SkPaint::kStroke_Style);
94 
95     // Draw a stroked "A" without a dash which will draw something.
96     drawTextCanvas.drawColor(SK_ColorWHITE);
97     drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
98 
99     // Draw an "A" but with a dash which will never draw anything.
100     paint.setStrokeWidth(2);
101     constexpr SkScalar bigInterval = 10000;
102     static constexpr SkScalar intervals[] = { 1, bigInterval };
103     paint.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 2));
104 
105     drawDashedTextCanvas.drawColor(SK_ColorWHITE);
106     drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
107 
108     // Draw nothing.
109     emptyCanvas.drawColor(SK_ColorWHITE);
110 
111     REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
112     REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
113 }
114 
115 // Test drawing text at some unusual coordinates.
116 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdCoordinates,r)117 DEF_TEST(DrawText_weirdCoordinates, r) {
118     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(10, 10));
119     auto canvas = surface->getCanvas();
120     SkFont font = ToolUtils::DefaultFont();
121 
122     SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
123 
124     for (auto x : oddballs) {
125         canvas->drawString("a", +x, 0.0f, font, SkPaint());
126         canvas->drawString("a", -x, 0.0f, font, SkPaint());
127     }
128     for (auto y : oddballs) {
129         canvas->drawString("a", 0.0f, +y, font, SkPaint());
130         canvas->drawString("a", 0.0f, -y, font, SkPaint());
131     }
132 }
133 
134 // Test drawing text with some unusual matrices.
135 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdMatricies,r)136 DEF_TEST(DrawText_weirdMatricies, r) {
137     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
138     auto canvas = surface->getCanvas();
139 
140     SkFont font = ToolUtils::DefaultFont();
141     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
142 
143     struct {
144         SkScalar textSize;
145         SkScalar matrix[9];
146     } testCases[] = {
147         // 2x2 singular
148         {10, { 0,  0,  0,  0,  0,  0,  0,  0,  1}},
149         {10, { 0,  0,  0,  0,  1,  0,  0,  0,  1}},
150         {10, { 0,  0,  0,  1,  0,  0,  0,  0,  1}},
151         {10, { 0,  0,  0,  1,  1,  0,  0,  0,  1}},
152         {10, { 0,  1,  0,  0,  1,  0,  0,  0,  1}},
153         {10, { 1,  0,  0,  0,  0,  0,  0,  0,  1}},
154         {10, { 1,  0,  0,  1,  0,  0,  0,  0,  1}},
155         {10, { 1,  1,  0,  0,  0,  0,  0,  0,  1}},
156         {10, { 1,  1,  0,  1,  1,  0,  0,  0,  1}},
157         // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 .
158         { 1, {10, 20,  0, 20, 40,  0,  0,  0,  1}},
159     };
160 
161     for (const auto& testCase : testCases) {
162         font.setSize(testCase.textSize);
163         const SkScalar(&m)[9] = testCase.matrix;
164         SkMatrix mat;
165         mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
166         canvas->setMatrix(mat);
167         canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
168     }
169 }
170 
171 // This produces no glyphs, and is to check that buffers from previous draws don't get
172 // reused.
DEF_TEST(DrawText_noglyphs,r)173 DEF_TEST(DrawText_noglyphs, r) {
174     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
175     auto canvas = surface->getCanvas();
176     SkFont font = ToolUtils::DefaultFont();
177     auto text = "Hamburgfons";
178     {
179         // scoped to ensure blob is deleted.
180         auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
181         canvas->drawTextBlob(blob, 10, 10, SkPaint());
182     }
183     canvas->drawString(
184             "\x0d\xf3\xf2\xf2\xe9\x0d\x0d\x0d\x05\x0d\x0d\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3\xe3",
185             10, 20, font, SkPaint());
186 }
187