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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkColor.h"
11 #include "SkDashPathEffect.h"
12 #include "SkMatrix.h"
13 #include "SkPaint.h"
14 #include "SkPathEffect.h"
15 #include "SkPoint.h"
16 #include "SkRect.h"
17 #include "SkRefCnt.h"
18 #include "SkScalar.h"
19 #include "SkSurface.h"
20 #include "SkTypes.h"
21 #include "Test.h"
22
23 #include <cmath>
24 #include <SkFont.h>
25
26 static const SkColor bgColor = SK_ColorWHITE;
27
create(SkBitmap * bm,SkIRect bound)28 static void create(SkBitmap* bm, SkIRect bound) {
29 bm->allocN32Pixels(bound.width(), bound.height());
30 }
31
32 /** 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 */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)37 static 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
43 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
64 /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
DEF_TEST(DrawText_dashout,reporter)65 DEF_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);
81 SkFont font(nullptr, 20);
82 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
83 font.setSubpixel(true);
84
85 SkPaint paint;
86 paint.setColor(SK_ColorGRAY);
87 paint.setStyle(SkPaint::kStroke_Style);
88
89 // Draw a stroked "A" without a dash which will draw something.
90 drawTextCanvas.drawColor(SK_ColorWHITE);
91 drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
92
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
99 drawDashedTextCanvas.drawColor(SK_ColorWHITE);
100 drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
101
102 // Draw nothing.
103 emptyCanvas.drawColor(SK_ColorWHITE);
104
105 REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
106 REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
107 }
108
109 // Test drawing text at some unusual coordinates.
110 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdCoordinates,r)111 DEF_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) {
118 canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
119 canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
120 }
121 for (auto y : oddballs) {
122 canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
123 canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
124 }
125 }
126
127 // Test drawing text with some unusual matricies.
128 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdMatricies,r)129 DEF_TEST(DrawText_weirdMatricies, r) {
130 auto surface = SkSurface::MakeRasterN32Premul(100,100);
131 auto canvas = surface->getCanvas();
132
133 SkFont font;
134 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
135
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) {
155 font.setSize(testCase.textSize);
156 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);
160 canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
161 }
162 }
163