• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
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/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/effects/SkDashPathEffect.h"
23 #include "tests/Test.h"
24 
25 #include <cmath>
26 
27 static const SkColor bgColor = SK_ColorWHITE;
28 
create(SkBitmap * bm,SkIRect bound)29 static void create(SkBitmap* bm, SkIRect bound) {
30     bm->allocN32Pixels(bound.width(), bound.height());
31 }
32 
33 /** Assumes that the ref draw was completely inside ref canvas --
34     implies that everything outside is "bgColor".
35     Checks that all overlap is the same and that all non-overlap on the
36     ref is "bgColor".
37  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)38 static bool compare(const SkBitmap& ref, const SkIRect& iref,
39                     const SkBitmap& test, const SkIRect& itest)
40 {
41     const int xOff = itest.fLeft - iref.fLeft;
42     const int yOff = itest.fTop - iref.fTop;
43 
44     for (int y = 0; y < test.height(); ++y) {
45         for (int x = 0; x < test.width(); ++x) {
46             SkColor testColor = test.getColor(x, y);
47             int refX = x + xOff;
48             int refY = y + yOff;
49             SkColor refColor;
50             if (refX >= 0 && refX < ref.width() &&
51                 refY >= 0 && refY < ref.height())
52             {
53                 refColor = ref.getColor(refX, refY);
54             } else {
55                 refColor = bgColor;
56             }
57             if (refColor != testColor) {
58                 return false;
59             }
60         }
61     }
62     return true;
63 }
64 
65 /** Test that drawing glyphs with empty paths is different from drawing glyphs without paths. */
DEF_TEST(DrawText_dashout,reporter)66 DEF_TEST(DrawText_dashout, reporter) {
67     SkIRect size = SkIRect::MakeWH(64, 64);
68 
69     SkBitmap drawTextBitmap;
70     create(&drawTextBitmap, size);
71     SkCanvas drawTextCanvas(drawTextBitmap);
72 
73     SkBitmap drawDashedTextBitmap;
74     create(&drawDashedTextBitmap, size);
75     SkCanvas drawDashedTextCanvas(drawDashedTextBitmap);
76 
77     SkBitmap emptyBitmap;
78     create(&emptyBitmap, size);
79     SkCanvas emptyCanvas(emptyBitmap);
80 
81     SkPoint point = SkPoint::Make(25.0f, 25.0f);
82     SkFont font(nullptr, 20);
83     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
84     font.setSubpixel(true);
85 
86     SkPaint paint;
87     paint.setColor(SK_ColorGRAY);
88     paint.setStyle(SkPaint::kStroke_Style);
89 
90     // Draw a stroked "A" without a dash which will draw something.
91     drawTextCanvas.drawColor(SK_ColorWHITE);
92     drawTextCanvas.drawString("A", point.fX, point.fY, font, paint);
93 
94     // Draw an "A" but with a dash which will never draw anything.
95     paint.setStrokeWidth(2);
96     constexpr SkScalar bigInterval = 10000;
97     static constexpr SkScalar intervals[] = { 1, bigInterval };
98     paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 2));
99 
100     drawDashedTextCanvas.drawColor(SK_ColorWHITE);
101     drawDashedTextCanvas.drawString("A", point.fX, point.fY, font, paint);
102 
103     // Draw nothing.
104     emptyCanvas.drawColor(SK_ColorWHITE);
105 
106     REPORTER_ASSERT(reporter, !compare(drawTextBitmap, size, emptyBitmap, size));
107     REPORTER_ASSERT(reporter, compare(drawDashedTextBitmap, size, emptyBitmap, size));
108 }
109 
110 // Test drawing text at some unusual coordinates.
111 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdCoordinates,r)112 DEF_TEST(DrawText_weirdCoordinates, r) {
113     auto surface = SkSurface::MakeRasterN32Premul(10,10);
114     auto canvas = surface->getCanvas();
115 
116     SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
117 
118     for (auto x : oddballs) {
119         canvas->drawString("a", +x, 0.0f, SkFont(), SkPaint());
120         canvas->drawString("a", -x, 0.0f, SkFont(), SkPaint());
121     }
122     for (auto y : oddballs) {
123         canvas->drawString("a", 0.0f, +y, SkFont(), SkPaint());
124         canvas->drawString("a", 0.0f, -y, SkFont(), SkPaint());
125     }
126 }
127 
128 // Test drawing text with some unusual matricies.
129 // We measure success by not crashing or asserting.
DEF_TEST(DrawText_weirdMatricies,r)130 DEF_TEST(DrawText_weirdMatricies, r) {
131     auto surface = SkSurface::MakeRasterN32Premul(100,100);
132     auto canvas = surface->getCanvas();
133 
134     SkFont font;
135     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
136 
137     struct {
138         SkScalar textSize;
139         SkScalar matrix[9];
140     } testCases[] = {
141         // 2x2 singular
142         {10, { 0,  0,  0,  0,  0,  0,  0,  0,  1}},
143         {10, { 0,  0,  0,  0,  1,  0,  0,  0,  1}},
144         {10, { 0,  0,  0,  1,  0,  0,  0,  0,  1}},
145         {10, { 0,  0,  0,  1,  1,  0,  0,  0,  1}},
146         {10, { 0,  1,  0,  0,  1,  0,  0,  0,  1}},
147         {10, { 1,  0,  0,  0,  0,  0,  0,  0,  1}},
148         {10, { 1,  0,  0,  1,  0,  0,  0,  0,  1}},
149         {10, { 1,  1,  0,  0,  0,  0,  0,  0,  1}},
150         {10, { 1,  1,  0,  1,  1,  0,  0,  0,  1}},
151         // See https://bugzilla.mozilla.org/show_bug.cgi?id=1305085 .
152         { 1, {10, 20,  0, 20, 40,  0,  0,  0,  1}},
153     };
154 
155     for (const auto& testCase : testCases) {
156         font.setSize(testCase.textSize);
157         const SkScalar(&m)[9] = testCase.matrix;
158         SkMatrix mat;
159         mat.setAll(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
160         canvas->setMatrix(mat);
161         canvas->drawString("Hamburgefons", 10, 10, font, SkPaint());
162     }
163 }
164