blob: 768c93603d8b9d1d639fb2b4f5473203ea77115a [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
8#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
bsalomon@google.com607d08b2012-08-20 13:55:09 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkColor.h"
13#include "SkPaint.h"
14#include "SkPoint.h"
15#include "SkRect.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000016#include "SkTypes.h"
bsalomon@google.com607d08b2012-08-20 13:55:09 +000017
18static const SkColor bgColor = SK_ColorWHITE;
19
20static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
21 bm->setConfig(config, bound.width(), bound.height());
22 bm->allocPixels();
23}
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;
71 create(&drawTextBitmap, drawTextRect, SkBitmap::kARGB_8888_Config);
72 SkCanvas drawTextCanvas(drawTextBitmap);
73
74 SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
75 SkBitmap drawPosTextBitmap;
76 create(&drawPosTextBitmap, drawPosTextRect, SkBitmap::kARGB_8888_Config);
77 SkCanvas drawPosTextCanvas(drawPosTextBitmap);
78
79 for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
80 for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000081 SkPoint point = SkPoint::Make(25.0f + offsetX,
82 25.0f + offsetY);
bsalomon@google.com607d08b2012-08-20 13:55:09 +000083
84 for (int align = 0; align < SkPaint::kAlignCount; ++align) {
85 paint.setTextAlign(static_cast<SkPaint::Align>(align));
86
87 for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
88 static const unsigned int antiAliasFlag = 1;
89 static const unsigned int subpixelFlag = 1 << 1;
90 static const unsigned int lcdFlag = 1 << 2;
91
92 paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
93 paint.setSubpixelText(SkToBool(flags & subpixelFlag));
94 paint.setLCDRenderText(SkToBool(flags & lcdFlag));
95
96 // Test: drawText and drawPosText draw the same.
97 drawBG(&drawTextCanvas);
98 drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint);
99
100 drawBG(&drawPosTextCanvas);
101 drawPosTextCanvas.drawPosText("A", 1, &point, paint);
102
103 REPORTER_ASSERT(reporter,
104 compare(drawTextBitmap, drawTextRect,
105 drawPosTextBitmap, drawPosTextRect));
106 }
107 }
108 }
109 }
110}