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 "SkFontDescriptor.h"
     12 #include "SkGraphics.h"
     13 #include "SkPaint.h"
     14 #include "SkPoint.h"
     15 #include "SkRect.h"
     16 #include "SkStream.h"
     17 #include "SkTypeface.h"
     18 #include "SkTypes.h"
     19 #include "Test.h"
     20 
     21 static const SkColor bgColor = SK_ColorWHITE;
     22 
     23 static void create(SkBitmap* bm, SkIRect bound) {
     24     bm->allocN32Pixels(bound.width(), bound.height());
     25 }
     26 
     27 static void drawBG(SkCanvas* canvas) {
     28     canvas->drawColor(bgColor);
     29 }
     30 
     31 /** Assumes that the ref draw was completely inside ref canvas --
     32     implies that everything outside is "bgColor".
     33     Checks that all overlap is the same and that all non-overlap on the
     34     ref is "bgColor".
     35  */
     36 static bool compare(const SkBitmap& ref, const SkIRect& iref,
     37                     const SkBitmap& test, const SkIRect& itest)
     38 {
     39     const int xOff = itest.fLeft - iref.fLeft;
     40     const int yOff = itest.fTop - iref.fTop;
     41 
     42     SkAutoLockPixels alpRef(ref);
     43     SkAutoLockPixels alpTest(test);
     44 
     45     for (int y = 0; y < test.height(); ++y) {
     46         for (int x = 0; x < test.width(); ++x) {
     47             SkColor testColor = test.getColor(x, y);
     48             int refX = x + xOff;
     49             int refY = y + yOff;
     50             SkColor refColor;
     51             if (refX >= 0 && refX < ref.width() &&
     52                 refY >= 0 && refY < ref.height())
     53             {
     54                 refColor = ref.getColor(refX, refY);
     55             } else {
     56                 refColor = bgColor;
     57             }
     58             if (refColor != testColor) {
     59                 return false;
     60             }
     61         }
     62     }
     63     return true;
     64 }
     65 
     66 DEF_TEST(FontHostStream, reporter) {
     67     {
     68         SkPaint paint;
     69         paint.setColor(SK_ColorGRAY);
     70         paint.setTextSize(SkIntToScalar(30));
     71 
     72         SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
     73                                                            SkTypeface::kNormal);
     74         SkSafeUnref(paint.setTypeface(fTypeface));
     75 
     76         SkIRect origRect = SkIRect::MakeWH(64, 64);
     77         SkBitmap origBitmap;
     78         create(&origBitmap, origRect);
     79         SkCanvas origCanvas(origBitmap);
     80 
     81         SkIRect streamRect = SkIRect::MakeWH(64, 64);
     82         SkBitmap streamBitmap;
     83         create(&streamBitmap, streamRect);
     84         SkCanvas streamCanvas(streamBitmap);
     85 
     86         SkPoint point = SkPoint::Make(24, 32);
     87 
     88         // Test: origTypeface and streamTypeface from orig data draw the same
     89         drawBG(&origCanvas);
     90         origCanvas.drawText("A", 1, point.fX, point.fY, paint);
     91 
     92         SkTypeface* origTypeface = paint.getTypeface();
     93         SkAutoTUnref<SkTypeface> aur;
     94         if (nullptr == origTypeface) {
     95             aur.reset(SkTypeface::RefDefault());
     96             origTypeface = aur.get();
     97         }
     98 
     99         int ttcIndex;
    100         SkAutoTDelete<SkStreamAsset> fontData(origTypeface->openStream(&ttcIndex));
    101         SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData.detach());
    102 
    103         SkFontDescriptor desc;
    104         bool isLocalStream = false;
    105         streamTypeface->getFontDescriptor(&desc, &isLocalStream);
    106         REPORTER_ASSERT(reporter, isLocalStream);
    107 
    108         SkSafeUnref(paint.setTypeface(streamTypeface));
    109         drawBG(&streamCanvas);
    110         streamCanvas.drawPosText("A", 1, &point, paint);
    111 
    112         REPORTER_ASSERT(reporter,
    113                         compare(origBitmap, origRect, streamBitmap, streamRect));
    114     }
    115     //Make sure the typeface is deleted and removed.
    116     SkGraphics::PurgeFontCache();
    117 }
    118