bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 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 | */ |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 8 | #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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 14 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "Test.h" |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 16 | |
| 17 | static const SkColor bgColor = SK_ColorWHITE; |
| 18 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 19 | static void create(SkBitmap* bm, SkIRect bound) { |
| 20 | bm->allocN32Pixels(bound.width(), bound.height()); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static void drawBG(SkCanvas* canvas) { |
| 24 | canvas->drawColor(bgColor); |
| 25 | } |
| 26 | |
| 27 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 28 | implies that everything outside is "bgColor". |
| 29 | Checks that all overlap is the same and that all non-overlap on the |
| 30 | ref is "bgColor". |
| 31 | */ |
| 32 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 33 | const SkBitmap& test, const SkIRect& itest) |
| 34 | { |
| 35 | const int xOff = itest.fLeft - iref.fLeft; |
| 36 | const int yOff = itest.fTop - iref.fTop; |
| 37 | |
| 38 | SkAutoLockPixels alpRef(ref); |
| 39 | SkAutoLockPixels alpTest(test); |
| 40 | |
| 41 | for (int y = 0; y < test.height(); ++y) { |
| 42 | for (int x = 0; x < test.width(); ++x) { |
| 43 | SkColor testColor = test.getColor(x, y); |
| 44 | int refX = x + xOff; |
| 45 | int refY = y + yOff; |
| 46 | SkColor refColor; |
| 47 | if (refX >= 0 && refX < ref.width() && |
| 48 | refY >= 0 && refY < ref.height()) |
| 49 | { |
| 50 | refColor = ref.getColor(refX, refY); |
| 51 | } else { |
| 52 | refColor = bgColor; |
| 53 | } |
| 54 | if (refColor != testColor) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 62 | DEF_TEST(DrawText, reporter) { |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 63 | SkPaint paint; |
| 64 | paint.setColor(SK_ColorGRAY); |
| 65 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 67 | SkIRect drawTextRect = SkIRect::MakeWH(64, 64); |
| 68 | SkBitmap drawTextBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 69 | create(&drawTextBitmap, drawTextRect); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 70 | SkCanvas drawTextCanvas(drawTextBitmap); |
| 71 | |
| 72 | SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64); |
| 73 | SkBitmap drawPosTextBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 74 | create(&drawPosTextBitmap, drawPosTextRect); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 75 | SkCanvas drawPosTextCanvas(drawPosTextBitmap); |
| 76 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 77 | // Two test cases "A" for the normal path through the code, and " " to check the |
| 78 | // early return path. |
| 79 | const char* cases[] = {"A", " "}; |
| 80 | for (auto c : cases) { |
| 81 | for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) { |
| 82 | for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) { |
| 83 | SkPoint point = SkPoint::Make(25.0f + offsetX, |
| 84 | 25.0f + offsetY); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 85 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 86 | for (int align = 0; align < SkPaint::kAlignCount; ++align) { |
| 87 | paint.setTextAlign(static_cast<SkPaint::Align>(align)); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 88 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 89 | for (unsigned int flags = 0; flags < (1 << 3); ++flags) { |
| 90 | static const unsigned int antiAliasFlag = 1; |
| 91 | static const unsigned int subpixelFlag = 1 << 1; |
| 92 | static const unsigned int lcdFlag = 1 << 2; |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 93 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 94 | paint.setAntiAlias(SkToBool(flags & antiAliasFlag)); |
| 95 | paint.setSubpixelText(SkToBool(flags & subpixelFlag)); |
| 96 | paint.setLCDRenderText(SkToBool(flags & lcdFlag)); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 97 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 98 | // Test: drawText and drawPosText draw the same. |
| 99 | drawBG(&drawTextCanvas); |
| 100 | drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 101 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 102 | drawBG(&drawPosTextCanvas); |
| 103 | drawPosTextCanvas.drawPosText(c, 1, &point, paint); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 104 | |
herb | 386127f | 2015-11-12 08:53:42 -0800 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, |
| 106 | compare(drawTextBitmap, drawTextRect, |
| 107 | drawPosTextBitmap, drawPosTextRect)); |
| 108 | } |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |