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" |
Mike Reed | 212e906 | 2018-12-25 17:35:49 -0500 | [diff] [blame] | 11 | #include "SkFont.h" |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 12 | #include "SkFontDescriptor.h" |
Mike Reed | 22bee5a | 2019-01-09 21:48:09 -0500 | [diff] [blame] | 13 | #include "SkFontPriv.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 14 | #include "SkGraphics.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkPoint.h" |
| 17 | #include "SkRect.h" |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 18 | #include "SkStream.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 19 | #include "SkTypeface.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "Test.h" |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 22 | |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 23 | static const SkColor bgColor = SK_ColorWHITE; |
| 24 | |
| 25 | static void create(SkBitmap* bm, SkIRect bound) { |
| 26 | bm->allocN32Pixels(bound.width(), bound.height()); |
| 27 | } |
| 28 | |
| 29 | static void drawBG(SkCanvas* canvas) { |
| 30 | canvas->drawColor(bgColor); |
| 31 | } |
| 32 | |
| 33 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 34 | implies that everything outside is "bgColor". |
| 35 | Checks that all overlap is the same and that all non-overlap on the |
| 36 | ref is "bgColor". |
| 37 | */ |
| 38 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 39 | const SkBitmap& test, const SkIRect& itest) |
| 40 | { |
| 41 | const int xOff = itest.fLeft - iref.fLeft; |
| 42 | const int yOff = itest.fTop - iref.fTop; |
| 43 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 44 | for (int y = 0; y < test.height(); ++y) { |
| 45 | for (int x = 0; x < test.width(); ++x) { |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 46 | SkColor testColor = test.getColor(x, y); |
| 47 | int refX = x + xOff; |
| 48 | int refY = y + yOff; |
| 49 | SkColor refColor; |
| 50 | if (refX >= 0 && refX < ref.width() && |
| 51 | refY >= 0 && refY < ref.height()) |
| 52 | { |
| 53 | refColor = ref.getColor(refX, refY); |
| 54 | } else { |
| 55 | refColor = bgColor; |
| 56 | } |
| 57 | if (refColor != testColor) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 65 | DEF_TEST(FontHostStream, reporter) { |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 66 | { |
| 67 | SkPaint paint; |
| 68 | paint.setColor(SK_ColorGRAY); |
| 69 | |
| 70 | SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30); |
Mike Reed | 22bee5a | 2019-01-09 21:48:09 -0500 | [diff] [blame] | 71 | font.setEdging(SkFont::Edging::kAlias); |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 72 | |
| 73 | SkIRect origRect = SkIRect::MakeWH(64, 64); |
| 74 | SkBitmap origBitmap; |
| 75 | create(&origBitmap, origRect); |
| 76 | SkCanvas origCanvas(origBitmap); |
| 77 | |
| 78 | SkIRect streamRect = SkIRect::MakeWH(64, 64); |
| 79 | SkBitmap streamBitmap; |
| 80 | create(&streamBitmap, streamRect); |
| 81 | SkCanvas streamCanvas(streamBitmap); |
| 82 | |
| 83 | SkPoint point = SkPoint::Make(24, 32); |
| 84 | |
| 85 | // Test: origTypeface and streamTypeface from orig data draw the same |
| 86 | drawBG(&origCanvas); |
Mike Reed | 22bee5a | 2019-01-09 21:48:09 -0500 | [diff] [blame] | 87 | origCanvas.drawString("A", point.fX, point.fY, font, paint); |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 88 | |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 89 | sk_sp<SkTypeface> typeface = font.refTypefaceOrDefault(); |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 90 | int ttcIndex; |
| 91 | std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex)); |
| 92 | if (!fontData) { |
| 93 | // We're using a SkTypeface that can't give us a stream. |
| 94 | // This happens with portable or system fonts. End the test now. |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData))); |
| 99 | |
| 100 | SkFontDescriptor desc; |
| 101 | bool isLocalStream = false; |
| 102 | streamTypeface->getFontDescriptor(&desc, &isLocalStream); |
| 103 | REPORTER_ASSERT(reporter, isLocalStream); |
| 104 | |
Mike Reed | 22bee5a | 2019-01-09 21:48:09 -0500 | [diff] [blame] | 105 | font.setTypeface(streamTypeface); |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 106 | drawBG(&streamCanvas); |
Mike Reed | 22bee5a | 2019-01-09 21:48:09 -0500 | [diff] [blame] | 107 | streamCanvas.drawString("A", point.fX, point.fY, font, paint); |
Hal Canary | 21c3482 | 2019-01-09 12:51:52 -0500 | [diff] [blame] | 108 | |
| 109 | REPORTER_ASSERT(reporter, |
| 110 | compare(origBitmap, origRect, streamBitmap, streamRect)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 111 | } |
| 112 | //Make sure the typeface is deleted and removed. |
| 113 | SkGraphics::PurgeFontCache(); |
| 114 | } |