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 
      8 #include "SkBitmap.h"
      9 #include "SkCanvas.h"
     10 #include "SkColor.h"
     11 #include "SkPaint.h"
     12 #include "SkPoint.h"
     13 #include "SkRect.h"
     14 #include "SkSurface.h"
     15 #include "SkTypes.h"
     16 #include "Test.h"
     17 #include <math.h>
     18 
     19 static const SkColor bgColor = SK_ColorWHITE;
     20 
     21 static void create(SkBitmap* bm, SkIRect bound) {
     22     bm->allocN32Pixels(bound.width(), bound.height());
     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 
     64 DEF_TEST(DrawText, reporter) {
     65     SkPaint paint;
     66     paint.setColor(SK_ColorGRAY);
     67     paint.setTextSize(SkIntToScalar(20));
     68 
     69     SkIRect drawTextRect = SkIRect::MakeWH(64, 64);
     70     SkBitmap drawTextBitmap;
     71     create(&drawTextBitmap, drawTextRect);
     72     SkCanvas drawTextCanvas(drawTextBitmap);
     73 
     74     SkIRect drawPosTextRect = SkIRect::MakeWH(64, 64);
     75     SkBitmap drawPosTextBitmap;
     76     create(&drawPosTextBitmap, drawPosTextRect);
     77     SkCanvas drawPosTextCanvas(drawPosTextBitmap);
     78 
     79     // Two test cases "A" for the normal path through the code, and " " to check the
     80     // early return path.
     81     const char* cases[] = {"A", " "};
     82     for (auto c : cases) {
     83         for (float offsetY = 0.0f; offsetY < 1.0f; offsetY += (1.0f / 16.0f)) {
     84             for (float offsetX = 0.0f; offsetX < 1.0f; offsetX += (1.0f / 16.0f)) {
     85                 SkPoint point = SkPoint::Make(25.0f + offsetX,
     86                                               25.0f + offsetY);
     87 
     88                 for (int align = 0; align < SkPaint::kAlignCount; ++align) {
     89                     paint.setTextAlign(static_cast<SkPaint::Align>(align));
     90 
     91                     for (unsigned int flags = 0; flags < (1 << 3); ++flags) {
     92                         static const unsigned int antiAliasFlag = 1;
     93                         static const unsigned int subpixelFlag = 1 << 1;
     94                         static const unsigned int lcdFlag = 1 << 2;
     95 
     96                         paint.setAntiAlias(SkToBool(flags & antiAliasFlag));
     97                         paint.setSubpixelText(SkToBool(flags & subpixelFlag));
     98                         paint.setLCDRenderText(SkToBool(flags & lcdFlag));
     99 
    100                         // Test: drawText and drawPosText draw the same.
    101                         drawBG(&drawTextCanvas);
    102                         drawTextCanvas.drawText(c, 1, point.fX, point.fY, paint);
    103 
    104                         drawBG(&drawPosTextCanvas);
    105                         drawPosTextCanvas.drawPosText(c, 1, &point, paint);
    106 
    107                         REPORTER_ASSERT(reporter,
    108                                         compare(drawTextBitmap, drawTextRect,
    109                                                 drawPosTextBitmap, drawPosTextRect));
    110                     }
    111                 }
    112             }
    113         }
    114     }
    115 }
    116 
    117 // Test drawing text at some unusual coordinates.
    118 // We measure success by not crashing or asserting.
    119 DEF_TEST(DrawText_weirdCoordinates, r) {
    120     auto surface = SkSurface::MakeRasterN32Premul(10,10);
    121     auto canvas = surface->getCanvas();
    122 
    123     SkScalar oddballs[] = { 0.0f, (float)INFINITY, (float)NAN, 34359738368.0f };
    124 
    125     for (auto x : oddballs) {
    126         canvas->drawText("a", 1, +x, 0.0f, SkPaint());
    127         canvas->drawText("a", 1, -x, 0.0f, SkPaint());
    128     }
    129     for (auto y : oddballs) {
    130         canvas->drawText("a", 1, 0.0f, +y, SkPaint());
    131         canvas->drawText("a", 1, 0.0f, -y, SkPaint());
    132     }
    133 }
    134