bungeman@google.com | a550199 | 2012-05-18 19:06:41 +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 | */ |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 7 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColor.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 11 | #include "SkFontDescriptor.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkPoint.h" |
| 15 | #include "SkRect.h" |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 17 | #include "SkTypeface.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 18 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 19 | #include "Test.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 20 | |
| 21 | static const SkColor bgColor = SK_ColorWHITE; |
| 22 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 23 | static void create(SkBitmap* bm, SkIRect bound) { |
| 24 | bm->allocN32Pixels(bound.width(), bound.height()); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 66 | DEF_TEST(FontHostStream, reporter) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 67 | { |
| 68 | SkPaint paint; |
| 69 | paint.setColor(SK_ColorGRAY); |
| 70 | paint.setTextSize(SkIntToScalar(30)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 72 | paint.setTypeface(SkTypeface::MakeFromName("Georgia", SkFontStyle())); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 73 | |
| 74 | SkIRect origRect = SkIRect::MakeWH(64, 64); |
| 75 | SkBitmap origBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 76 | create(&origBitmap, origRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 77 | SkCanvas origCanvas(origBitmap); |
| 78 | |
| 79 | SkIRect streamRect = SkIRect::MakeWH(64, 64); |
| 80 | SkBitmap streamBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 81 | create(&streamBitmap, streamRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 82 | SkCanvas streamCanvas(streamBitmap); |
| 83 | |
| 84 | SkPoint point = SkPoint::Make(24, 32); |
| 85 | |
| 86 | // Test: origTypeface and streamTypeface from orig data draw the same |
| 87 | drawBG(&origCanvas); |
| 88 | origCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 89 | |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame^] | 90 | sk_sp<SkTypeface> typeface(paint.getTypeface() ? paint.refTypeface() |
| 91 | : SkTypeface::MakeDefault()); |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 92 | int ttcIndex; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 93 | std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex)); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 94 | sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 95 | |
| 96 | SkFontDescriptor desc; |
| 97 | bool isLocalStream = false; |
| 98 | streamTypeface->getFontDescriptor(&desc, &isLocalStream); |
| 99 | REPORTER_ASSERT(reporter, isLocalStream); |
| 100 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 101 | paint.setTypeface(streamTypeface); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 102 | drawBG(&streamCanvas); |
| 103 | streamCanvas.drawPosText("A", 1, &point, paint); |
| 104 | |
| 105 | REPORTER_ASSERT(reporter, |
| 106 | compare(origBitmap, origRect, streamBitmap, streamRect)); |
| 107 | } |
| 108 | //Make sure the typeface is deleted and removed. |
| 109 | SkGraphics::PurgeFontCache(); |
| 110 | } |