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 "SkFontHost.h" |
| 13 | #include "SkGraphics.h" |
| 14 | #include "SkPaint.h" |
| 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 | |
| 43 | SkAutoLockPixels alpRef(ref); |
| 44 | SkAutoLockPixels alpTest(test); |
| 45 | |
| 46 | for (int y = 0; y < test.height(); ++y) { |
| 47 | for (int x = 0; x < test.width(); ++x) { |
| 48 | SkColor testColor = test.getColor(x, y); |
| 49 | int refX = x + xOff; |
| 50 | int refY = y + yOff; |
| 51 | SkColor refColor; |
| 52 | if (refX >= 0 && refX < ref.width() && |
| 53 | refY >= 0 && refY < ref.height()) |
| 54 | { |
| 55 | refColor = ref.getColor(refX, refY); |
| 56 | } else { |
| 57 | refColor = bgColor; |
| 58 | } |
| 59 | if (refColor != testColor) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 67 | DEF_TEST(FontHostStream, reporter) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 68 | { |
| 69 | SkPaint paint; |
| 70 | paint.setColor(SK_ColorGRAY); |
| 71 | paint.setTextSize(SkIntToScalar(30)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 66f5aaa | 2013-05-07 14:32:58 +0000 | [diff] [blame] | 73 | SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia", |
| 74 | SkTypeface::kNormal); |
| 75 | SkSafeUnref(paint.setTypeface(fTypeface)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 76 | |
| 77 | SkIRect origRect = SkIRect::MakeWH(64, 64); |
| 78 | SkBitmap origBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 79 | create(&origBitmap, origRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 80 | SkCanvas origCanvas(origBitmap); |
| 81 | |
| 82 | SkIRect streamRect = SkIRect::MakeWH(64, 64); |
| 83 | SkBitmap streamBitmap; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 84 | create(&streamBitmap, streamRect); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 85 | SkCanvas streamCanvas(streamBitmap); |
| 86 | |
| 87 | SkPoint point = SkPoint::Make(24, 32); |
| 88 | |
| 89 | // Test: origTypeface and streamTypeface from orig data draw the same |
| 90 | drawBG(&origCanvas); |
| 91 | origCanvas.drawText("A", 1, point.fX, point.fY, paint); |
| 92 | |
| 93 | SkTypeface* origTypeface = paint.getTypeface(); |
reed@google.com | fed86bd | 2013-03-14 15:04:57 +0000 | [diff] [blame] | 94 | SkAutoTUnref<SkTypeface> aur; |
| 95 | if (NULL == origTypeface) { |
| 96 | origTypeface = aur.reset(SkTypeface::RefDefault()); |
| 97 | } |
| 98 | |
| 99 | int ttcIndex; |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 100 | SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 101 | SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData); |
caseq | 26337e9 | 2014-06-30 12:14:52 -0700 | [diff] [blame] | 102 | |
| 103 | SkFontDescriptor desc; |
| 104 | bool isLocalStream = false; |
| 105 | streamTypeface->getFontDescriptor(&desc, &isLocalStream); |
| 106 | REPORTER_ASSERT(reporter, isLocalStream); |
| 107 | |
bungeman@google.com | 148a396 | 2013-01-17 20:19:13 +0000 | [diff] [blame] | 108 | SkSafeUnref(paint.setTypeface(streamTypeface)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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 | } |