blob: bf9bd39f8d1e7a4d4c5f78ba086ed42964d9e962 [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
bsalomon@google.com607d08b2012-08-20 13:55:09 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColor.h"
11#include "SkPaint.h"
12#include "SkPoint.h"
13#include "SkRect.h"
mtklein875e13c2016-06-19 05:28:33 -070014#include "SkSurface.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000015#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "Test.h"
mtklein875e13c2016-06-19 05:28:33 -070017#include <math.h>
bsalomon@google.com607d08b2012-08-20 13:55:09 +000018
19static const SkColor bgColor = SK_ColorWHITE;
20
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000021static void create(SkBitmap* bm, SkIRect bound) {
22 bm->allocN32Pixels(bound.width(), bound.height());
bsalomon@google.com607d08b2012-08-20 13:55:09 +000023}
24
25static void drawBG(SkCanvas* canvas) {
26 canvas->drawColor(bgColor);
27}
28
29/** Assumes that the ref draw was completely inside ref canvas --
30 implies that everything outside is "bgColor".
31 Checks that all overlap is the same and that all non-overlap on the
32 ref is "bgColor".
33 */
34static bool compare(const SkBitmap& ref, const SkIRect& iref,
35 const SkBitmap& test, const SkIRect& itest)
36{
37 const int xOff = itest.fLeft - iref.fLeft;
38 const int yOff = itest.fTop - iref.fTop;
39
40 SkAutoLockPixels alpRef(ref);
41 SkAutoLockPixels alpTest(test);
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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000064DEF_TEST(DrawText, reporter) {
bsalomon@google.com607d08b2012-08-20 13:55:09 +000065 SkPaint paint;
66 paint.setColor(SK_ColorGRAY);
67 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
bsalomon@google.com607d08b2012-08-20 13:55:09 +000069 SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
70 SkBitmap drawTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000071 create(&drawTextBitmap, drawTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000072 SkCanvas drawTextCanvas(drawTextBitmap);
73
74 SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
75 SkBitmap drawPosTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000076 create(&drawPosTextBitmap, drawPosTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000077 SkCanvas drawPosTextCanvas(drawPosTextBitmap);
78
herb386127f2015-11-12 08:53:42 -080079 // Two test cases "A" for the normal path through the code, and " " to check the
80 // early return path.
81 const char* cases[] = {"A", " "};
82 for (auto c : cases) {
83 for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
84 for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
85 SkPoint point = SkPoint::Make(25.0f + offsetX,
86 25.0f + offsetY);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000087
herb386127f2015-11-12 08:53:42 -080088 for (int align = 0; align < SkPaint::kAlignCount; ++align) {
89 paint.setTextAlign(static_cast<SkPaint::Align>(align));
bsalomon@google.com607d08b2012-08-20 13:55:09 +000090
herb386127f2015-11-12 08:53:42 -080091 for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
92 static const unsigned int antiAliasFlag = 1;
93 static const unsigned int subpixelFlag = 1 << 1;
94 static const unsigned int lcdFlag = 1 << 2;
bsalomon@google.com607d08b2012-08-20 13:55:09 +000095
herb386127f2015-11-12 08:53:42 -080096 paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
97 paint.setSubpixelText(SkToBool(flags & subpixelFlag));
98 paint.setLCDRenderText(SkToBool(flags & lcdFlag));
bsalomon@google.com607d08b2012-08-20 13:55:09 +000099
herb386127f2015-11-12 08:53:42 -0800100 // Test: drawText and drawPosText draw the same.
101 drawBG(&drawTextCanvas);
102 drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000103
herb386127f2015-11-12 08:53:42 -0800104 drawBG(&drawPosTextCanvas);
105 drawPosTextCanvas.drawPosText(c, 1, &point, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000106
herb386127f2015-11-12 08:53:42 -0800107 REPORTER_ASSERT(reporter,
108 compare(drawTextBitmap, drawTextRect,
109 drawPosTextBitmap, drawPosTextRect));
110 }
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000111 }
112 }
113 }
114 }
115}
mtklein875e13c2016-06-19 05:28:33 -0700116
117// Test drawing text at some unusual coordinates.
118// We measure success by not crashing or asserting.
119DEF_TEST(DrawText_weirdCoordinates, r) {
120 auto surface = SkSurface::MakeRasterN32Premul(10,10);
121 auto canvas = surface->getCanvas();
122
123 SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
124
125 for (auto x : oddballs) {
126 canvas->drawText("a", 1, +x, 0.0f, SkPaint());
127 canvas->drawText("a", 1, -x, 0.0f, SkPaint());
128 }
129 for (auto y : oddballs) {
130 canvas->drawText("a", 1, 0.0f, +y, SkPaint());
131 canvas->drawText("a", 1, 0.0f, -y, SkPaint());
132 }
133}