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 | |
| 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 10 | #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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 16 | #include "SkTypes.h" |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 17 | |
| 18 | static const SkColor bgColor = SK_ColorWHITE; |
| 19 | |
| 20 | static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) { |
| 21 | bm->setConfig(config, bound.width(), bound.height()); |
| 22 | bm->allocPixels(); |
| 23 | } |
| 24 | |
| 25 | static 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 | */ |
| 34 | static 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 64 | DEF_TEST(DrawText, reporter) { |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 65 | SkPaint paint; |
| 66 | paint.setColor(SK_ColorGRAY); |
| 67 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 69 | 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 81 | SkPoint point = SkPoint::Make(25.0f + offsetX, |
| 82 | 25.0f + offsetY); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 83 | |
| 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 | } |