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 | |
| 19 | static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) { |
| 20 | bm->setConfig(config, bound.width(), bound.height()); |
| 21 | bm->allocPixels(); |
| 22 | } |
| 23 | |
| 24 | static 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 | */ |
| 33 | static 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 63 | DEF_TEST(DrawText, reporter) { |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 64 | SkPaint paint; |
| 65 | paint.setColor(SK_ColorGRAY); |
| 66 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 68 | 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 80 | SkPoint point = SkPoint::Make(25.0f + offsetX, |
| 81 | 25.0f + offsetY); |
bsalomon@google.com | 607d08b | 2012-08-20 13:55:09 +0000 | [diff] [blame] | 82 | |
| 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 | } |