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" |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 14 | #include "SkPaintPriv.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 15 | #include "SkPoint.h" |
| 16 | #include "SkRect.h" |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 18 | #include "SkTypeface.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 20 | #include "Test.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 21 | |
| 22 | static const SkColor bgColor = SK_ColorWHITE; |
| 23 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 24 | static void create(SkBitmap* bm, SkIRect bound) { |
| 25 | bm->allocN32Pixels(bound.width(), bound.height()); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static void drawBG(SkCanvas* canvas) { |
| 29 | canvas->drawColor(bgColor); |
| 30 | } |
| 31 | |
| 32 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 33 | implies that everything outside is "bgColor". |
| 34 | Checks that all overlap is the same and that all non-overlap on the |
| 35 | ref is "bgColor". |
| 36 | */ |
| 37 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 38 | const SkBitmap& test, const SkIRect& itest) |
| 39 | { |
| 40 | const int xOff = itest.fLeft - iref.fLeft; |
| 41 | const int yOff = itest.fTop - iref.fTop; |
| 42 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 64 | DEF_TEST(FontHostStream, reporter) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 65 | { |
| 66 | SkPaint paint; |
| 67 | paint.setColor(SK_ColorGRAY); |
| 68 | paint.setTextSize(SkIntToScalar(30)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 69 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 70 | paint.setTypeface(SkTypeface::MakeFromName("Georgia", SkFontStyle())); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 71 | |
| 72 | SkIRect origRect = SkIRect::MakeWH(64, 64); |
| 73 | SkBitmap origBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 74 | create(&origBitmap, origRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 75 | SkCanvas origCanvas(origBitmap); |
| 76 | |
| 77 | SkIRect streamRect = SkIRect::MakeWH(64, 64); |
| 78 | SkBitmap streamBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 79 | create(&streamBitmap, streamRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 80 | SkCanvas streamCanvas(streamBitmap); |
| 81 | |
| 82 | SkPoint point = SkPoint::Make(24, 32); |
| 83 | |
| 84 | // Test: origTypeface and streamTypeface from orig data draw the same |
| 85 | drawBG(&origCanvas); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 86 | origCanvas.drawString("A", point.fX, point.fY, paint); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 87 | |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 88 | sk_sp<SkTypeface> typeface = SkPaintPriv::RefTypefaceOrDefault(paint); |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 89 | int ttcIndex; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 90 | std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex)); |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 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 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 97 | sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release())); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 98 | |
| 99 | SkFontDescriptor desc; |
| 100 | bool isLocalStream = false; |
| 101 | streamTypeface->getFontDescriptor(&desc, &isLocalStream); |
| 102 | REPORTER_ASSERT(reporter, isLocalStream); |
| 103 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 104 | paint.setTypeface(streamTypeface); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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 | } |