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     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 
     63 DEF_TEST(FontHostStream, reporter) {
     64     {
     65         SkPaint paint;
     66         paint.setColor(SK_ColorGRAY);
     67         paint.setTextSize(SkIntToScalar(30));
     68 
     69         paint.setTypeface(SkTypeface::MakeFromName("Georgia", SkFontStyle()));
     70 
     71         SkIRect origRect = SkIRect::MakeWH(64, 64);
     72         SkBitmap origBitmap;
     73         create(&origBitmap, origRect);
     74         SkCanvas origCanvas(origBitmap);
     75 
     76         SkIRect streamRect = SkIRect::MakeWH(64, 64);
     77         SkBitmap streamBitmap;
     78         create(&streamBitmap, streamRect);
     79         SkCanvas streamCanvas(streamBitmap);
     80 
     81         SkPoint point = SkPoint::Make(24, 32);
     82 
     83         // Test: origTypeface and streamTypeface from orig data draw the same
     84         drawBG(&origCanvas);
     85         origCanvas.drawString("A", point.fX, point.fY, paint);
     86 
     87         sk_sp<SkTypeface> typeface(paint.getTypeface() ? paint.refTypeface()
     88                                                        : SkTypeface::MakeDefault());
     89         int ttcIndex;
     90         std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
     91         if (!fontData) {
     92             // We're using a SkTypeface that can't give us a stream.
     93             // This happens with portable or system fonts.  End the test now.
     94             return;
     95         }
     96 
     97         sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release()));
     98 
     99         SkFontDescriptor desc;
    100         bool isLocalStream = false;
    101         streamTypeface->getFontDescriptor(&desc, &isLocalStream);
    102         REPORTER_ASSERT(reporter, isLocalStream);
    103 
    104         paint.setTypeface(streamTypeface);
    105         drawBG(&streamCanvas);
    106         streamCanvas.drawPosText("A", 1, &point, paint);
    107 
    108         REPORTER_ASSERT(reporter,
    109                         compare(origBitmap, origRect, streamBitmap, streamRect));
    110     }
    111     //Make sure the typeface is deleted and removed.
    112     SkGraphics::PurgeFontCache();
    113 }
    114