Home | History | Annotate | Download | only in tests
      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  */
      7 #include "SkTypes.h"
      8 
      9 #include "Test.h"
     10 #include "SkBitmap.h"
     11 #include "SkCanvas.h"
     12 #include "SkColor.h"
     13 #include "SkPaint.h"
     14 #include "SkPoint.h"
     15 #include "SkRect.h"
     16 
     17 ///////////////////////////////////////////////////////////////////////////////
     18 
     19 static const SkColor bgColor = SK_ColorWHITE;
     20 
     21 static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
     22     bm->setConfig(config, bound.width(), bound.height());
     23     bm->allocPixels();
     24 }
     25 
     26 static void drawBG(SkCanvas* canvas) {
     27     canvas->drawColor(bgColor);
     28 }
     29 
     30 /** Assumes that the ref draw was completely inside ref canvas --
     31     implies that everything outside is "bgColor".
     32     Checks that all overlap is the same and that all non-overlap on the
     33     ref is "bgColor".
     34  */
     35 static bool compare(const SkBitmap& ref, const SkIRect& iref,
     36                     const SkBitmap& test, const SkIRect& itest)
     37 {
     38     const int xOff = itest.fLeft - iref.fLeft;
     39     const int yOff = itest.fTop - iref.fTop;
     40 
     41     SkAutoLockPixels alpRef(ref);
     42     SkAutoLockPixels alpTest(test);
     43 
     44     for (int y = 0; y < test.height(); ++y) {
     45         for (int x = 0; x < test.width(); ++x) {
     46             SkColor testColor = test.getColor(x, y);
     47             int refX = x + xOff;
     48             int refY = y + yOff;
     49             SkColor refColor;
     50             if (refX >= 0 && refX < ref.width() &&
     51                 refY >= 0 && refY < ref.height())
     52             {
     53                 refColor = ref.getColor(refX, refY);
     54             } else {
     55                 refColor = bgColor;
     56             }
     57             if (refColor != testColor) {
     58                 return false;
     59             }
     60         }
     61     }
     62     return true;
     63 }
     64 
     65 static void test_drawText(skiatest::Reporter* reporter) {
     66 
     67     SkPaint paint;
     68     paint.setColor(SK_ColorGRAY);
     69     paint.setTextSize(SkIntToScalar(20));
     70 
     71     SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
     72     SkBitmap drawTextBitmap;
     73     create(&drawTextBitmap, drawTextRect, SkBitmap::kARGB_8888_Config);
     74     SkCanvas drawTextCanvas(drawTextBitmap);
     75 
     76     SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
     77     SkBitmap drawPosTextBitmap;
     78     create(&drawPosTextBitmap, drawPosTextRect, SkBitmap::kARGB_8888_Config);
     79     SkCanvas drawPosTextCanvas(drawPosTextBitmap);
     80 
     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(SkFloatToScalar(25.0f + offsetX),
     84                                           SkFloatToScalar(25.0f + offsetY));
     85 
     86             for (int align = 0; align < SkPaint::kAlignCount; ++align) {
     87                 paint.setTextAlign(static_cast<SkPaint::Align>(align));
     88 
     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;
     93 
     94                     paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
     95                     paint.setSubpixelText(SkToBool(flags & subpixelFlag));
     96                     paint.setLCDRenderText(SkToBool(flags & lcdFlag));
     97 
     98                     // Test: drawText and drawPosText draw the same.
     99                     drawBG(&drawTextCanvas);
    100                     drawTextCanvas.drawText("A", 1, point.fX, point.fY, paint);
    101 
    102                     drawBG(&drawPosTextCanvas);
    103                     drawPosTextCanvas.drawPosText("A", 1, &point, paint);
    104 
    105                     REPORTER_ASSERT(reporter,
    106                         compare(drawTextBitmap, drawTextRect,
    107                                 drawPosTextBitmap, drawPosTextRect));
    108                 }
    109             }
    110         }
    111     }
    112 }
    113 
    114 #include "TestClassDef.h"
    115 DEFINE_TESTCLASS("DrawText_DrawPosText", DrawTextTestClass, test_drawText)
    116