blob: bafd2cb5e28fd66f2a6dbff1e557011d49725e68 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000014#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "Test.h"
bsalomon@google.com607d08b2012-08-20 13:55:09 +000016
17static const SkColor bgColor = SK_ColorWHITE;
18
19static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
20 bm->setConfig(config, bound.width(), bound.height());
21 bm->allocPixels();
22}
23
24static void drawBG(SkCanvas* canvas) {
25 canvas->drawColor(bgColor);
26}
27
28/** Assumes that the ref draw was completely inside ref canvas --
29 implies that everything outside is "bgColor".
30 Checks that all overlap is the same and that all non-overlap on the
31 ref is "bgColor".
32 */
33static bool compare(const SkBitmap& ref, const SkIRect& iref,
34 const SkBitmap& test, const SkIRect& itest)
35{
36 const int xOff = itest.fLeft - iref.fLeft;
37 const int yOff = itest.fTop - iref.fTop;
38
39 SkAutoLockPixels alpRef(ref);
40 SkAutoLockPixels alpTest(test);
41
42 for (int y = 0; y < test.height(); ++y) {
43 for (int x = 0; x < test.width(); ++x) {
44 SkColor testColor = test.getColor(x, y);
45 int refX = x + xOff;
46 int refY = y + yOff;
47 SkColor refColor;
48 if (refX >= 0 && refX < ref.width() &&
49 refY >= 0 && refY < ref.height())
50 {
51 refColor = ref.getColor(refX, refY);
52 } else {
53 refColor = bgColor;
54 }
55 if (refColor != testColor) {
56 return false;
57 }
58 }
59 }
60 return true;
61}
62
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000063DEF_TEST(DrawText, reporter) {
bsalomon@google.com607d08b2012-08-20 13:55:09 +000064 SkPaint paint;
65 paint.setColor(SK_ColorGRAY);
66 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
bsalomon@google.com607d08b2012-08-20 13:55:09 +000068 SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
69 SkBitmap drawTextBitmap;
70 create(&drawTextBitmap, drawTextRect, SkBitmap::kARGB_8888_Config);
71 SkCanvas drawTextCanvas(drawTextBitmap);
72
73 SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
74 SkBitmap drawPosTextBitmap;
75 create(&drawPosTextBitmap, drawPosTextRect, SkBitmap::kARGB_8888_Config);
76 SkCanvas drawPosTextCanvas(drawPosTextBitmap);
77
78 for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
79 for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000080 SkPoint point = SkPoint::Make(25.0f + offsetX,
81 25.0f + offsetY);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000082
83 for (int align = 0; align < SkPaint::kAlignCount; ++align) {
84 paint.setTextAlign(static_cast<SkPaint::Align>(align));
85
86 for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
87 static const unsigned int antiAliasFlag = 1;
88 static const unsigned int subpixelFlag = 1 << 1;
89 static const unsigned int lcdFlag = 1 << 2;
90
91 paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
92 paint.setSubpixelText(SkToBool(flags & subpixelFlag));
93 paint.setLCDRenderText(SkToBool(flags & lcdFlag));
94
95 // Test: drawText and drawPosText draw the same.
96 drawBG(&drawTextCanvas);
97 drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint);
98
99 drawBG(&drawPosTextCanvas);
100 drawPosTextCanvas.drawPosText("A", 1, &point, paint);
101
102 REPORTER_ASSERT(reporter,
103 compare(drawTextBitmap, drawTextRect,
104 drawPosTextBitmap, drawPosTextRect));
105 }
106 }
107 }
108 }
109}