blob: 2f8fe056eb5ee7bc9da79df3c1b7c3fd879d0e92 [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
bsalomon@google.com607d08b2012-08-20 13:55:09 +000040 for (int y = 0; y < test.height(); ++y) {
41 for (int x = 0; x < test.width(); ++x) {
42 SkColor testColor = test.getColor(x, y);
43 int refX = x + xOff;
44 int refY = y + yOff;
45 SkColor refColor;
46 if (refX >= 0 && refX < ref.width() &&
47 refY >= 0 && refY < ref.height())
48 {
49 refColor = ref.getColor(refX, refY);
50 } else {
51 refColor = bgColor;
52 }
53 if (refColor != testColor) {
54 return false;
55 }
56 }
57 }
58 return true;
59}
60
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000061DEF_TEST(DrawText, reporter) {
bsalomon@google.com607d08b2012-08-20 13:55:09 +000062 SkPaint paint;
63 paint.setColor(SK_ColorGRAY);
64 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
bsalomon@google.com607d08b2012-08-20 13:55:09 +000066 SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
67 SkBitmap drawTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000068 create(&drawTextBitmap, drawTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000069 SkCanvas drawTextCanvas(drawTextBitmap);
70
71 SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
72 SkBitmap drawPosTextBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000073 create(&drawPosTextBitmap, drawPosTextRect);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000074 SkCanvas drawPosTextCanvas(drawPosTextBitmap);
75
herb386127f2015-11-12 08:53:42 -080076 // Two test cases "A" for the normal path through the code, and " " to check the
77 // early return path.
78 const char* cases[] = {"A", " "};
79 for (auto c : cases) {
80 for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
81 for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
82 SkPoint point = SkPoint::Make(25.0f + offsetX,
83 25.0f + offsetY);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000084
herb386127f2015-11-12 08:53:42 -080085 for (int align = 0; align < SkPaint::kAlignCount; ++align) {
86 paint.setTextAlign(static_cast<SkPaint::Align>(align));
bsalomon@google.com607d08b2012-08-20 13:55:09 +000087
herb386127f2015-11-12 08:53:42 -080088 for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
89 static const unsigned int antiAliasFlag = 1;
90 static const unsigned int subpixelFlag = 1 << 1;
91 static const unsigned int lcdFlag = 1 << 2;
bsalomon@google.com607d08b2012-08-20 13:55:09 +000092
herb386127f2015-11-12 08:53:42 -080093 paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
94 paint.setSubpixelText(SkToBool(flags & subpixelFlag));
95 paint.setLCDRenderText(SkToBool(flags & lcdFlag));
bsalomon@google.com607d08b2012-08-20 13:55:09 +000096
herb386127f2015-11-12 08:53:42 -080097 // Test: drawText and drawPosText draw the same.
98 drawBG(&drawTextCanvas);
99 drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000100
herb386127f2015-11-12 08:53:42 -0800101 drawBG(&drawPosTextCanvas);
102 drawPosTextCanvas.drawPosText(c, 1, &point, paint);
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000103
herb386127f2015-11-12 08:53:42 -0800104 REPORTER_ASSERT(reporter,
105 compare(drawTextBitmap, drawTextRect,
106 drawPosTextBitmap, drawPosTextRect));
107 }
bsalomon@google.com607d08b2012-08-20 13:55:09 +0000108 }
109 }
110 }
111 }
112}
mtklein875e13c2016-06-19 05:28:33 -0700113
114// Test drawing text at some unusual coordinates.
115// We measure success by not crashing or asserting.
116DEF_TEST(DrawText_weirdCoordinates, r) {
117 auto surface = SkSurface::MakeRasterN32Premul(10,10);
118 auto canvas = surface->getCanvas();
119
120 SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
121
122 for (auto x : oddballs) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400123 canvas->drawString("a", +x, 0.0f, SkPaint());
124 canvas->drawString("a", -x, 0.0f, SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700125 }
126 for (auto y : oddballs) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400127 canvas->drawString("a", 0.0f, +y, SkPaint());
128 canvas->drawString("a", 0.0f, -y, SkPaint());
mtklein875e13c2016-06-19 05:28:33 -0700129 }
130}