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 | */ |
| 7 | #include "SkTypes.h" |
| 8 | |
| 9 | #include "Test.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColor.h" |
| 13 | #include "SkFontHost.h" |
| 14 | #include "SkGraphics.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkPoint.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkTypeface.h" |
| 19 | |
| 20 | /////////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | static const SkColor bgColor = SK_ColorWHITE; |
| 23 | |
| 24 | static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) { |
| 25 | bm->setConfig(config, bound.width(), bound.height()); |
| 26 | bm->allocPixels(); |
| 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 | |
| 44 | SkAutoLockPixels alpRef(ref); |
| 45 | SkAutoLockPixels alpTest(test); |
| 46 | |
| 47 | for (int y = 0; y < test.height(); ++y) { |
| 48 | for (int x = 0; x < test.width(); ++x) { |
| 49 | SkColor testColor = test.getColor(x, y); |
| 50 | int refX = x + xOff; |
| 51 | int refY = y + yOff; |
| 52 | SkColor refColor; |
| 53 | if (refX >= 0 && refX < ref.width() && |
| 54 | refY >= 0 && refY < ref.height()) |
| 55 | { |
| 56 | refColor = ref.getColor(refX, refY); |
| 57 | } else { |
| 58 | refColor = bgColor; |
| 59 | } |
| 60 | if (refColor != testColor) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | static void test_fontHostStream(skiatest::Reporter* reporter) { |
| 69 | |
| 70 | { |
| 71 | SkPaint paint; |
| 72 | paint.setColor(SK_ColorGRAY); |
| 73 | paint.setTextSize(SkIntToScalar(30)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 75 | paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kNormal))->unref(); |
| 76 | |
| 77 | SkIRect origRect = SkIRect::MakeWH(64, 64); |
| 78 | SkBitmap origBitmap; |
| 79 | create(&origBitmap, origRect, SkBitmap::kARGB_8888_Config); |
| 80 | SkCanvas origCanvas(origBitmap); |
| 81 | |
| 82 | SkIRect streamRect = SkIRect::MakeWH(64, 64); |
| 83 | SkBitmap streamBitmap; |
| 84 | create(&streamBitmap, streamRect, SkBitmap::kARGB_8888_Config); |
| 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(); |
| 94 | const SkFontID typefaceID = SkTypeface::UniqueID(origTypeface); |
| 95 | SkStream* fontData = SkFontHost::OpenStream(typefaceID); |
| 96 | SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData); |
bungeman@google.com | 148a396 | 2013-01-17 20:19:13 +0000 | [diff] [blame] | 97 | SkSafeUnref(paint.setTypeface(streamTypeface)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 98 | drawBG(&streamCanvas); |
| 99 | streamCanvas.drawPosText("A", 1, &point, paint); |
| 100 | |
| 101 | REPORTER_ASSERT(reporter, |
| 102 | compare(origBitmap, origRect, streamBitmap, streamRect)); |
| 103 | } |
| 104 | //Make sure the typeface is deleted and removed. |
| 105 | SkGraphics::PurgeFontCache(); |
| 106 | } |
| 107 | |
| 108 | #include "TestClassDef.h" |
| 109 | DEFINE_TESTCLASS("FontHost::CreateTypefaceFromStream", FontHostStreamTestClass, test_fontHostStream) |