blob: 7ac3da363ab4cd253afba6a3680dbef1e90f392f [file] [log] [blame]
bungeman@google.coma5501992012-05-18 19:06:41 +00001/*
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.coma5501992012-05-18 19:06:41 +00007
bungeman@google.coma5501992012-05-18 19:06:41 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColor.h"
caseq26337e92014-06-30 12:14:52 -070011#include "SkFontDescriptor.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000012#include "SkGraphics.h"
13#include "SkPaint.h"
14#include "SkPoint.h"
15#include "SkRect.h"
robertphillips@google.comcb3b6152013-11-14 14:47:56 +000016#include "SkStream.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000017#include "SkTypeface.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000018#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "Test.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000020
21static const SkColor bgColor = SK_ColorWHITE;
22
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000023static void create(SkBitmap* bm, SkIRect bound) {
24 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.coma5501992012-05-18 19:06:41 +000025}
26
27static 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 */
36static 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.orge4fafb12013-12-12 21:11:12 +000066DEF_TEST(FontHostStream, reporter) {
bungeman@google.coma5501992012-05-18 19:06:41 +000067 {
68 SkPaint paint;
69 paint.setColor(SK_ColorGRAY);
70 paint.setTextSize(SkIntToScalar(30));
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
commit-bot@chromium.org66f5aaa2013-05-07 14:32:58 +000072 SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
73 SkTypeface::kNormal);
74 SkSafeUnref(paint.setTypeface(fTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +000075
76 SkIRect origRect = SkIRect::MakeWH(64, 64);
77 SkBitmap origBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000078 create(&origBitmap, origRect);
bungeman@google.coma5501992012-05-18 19:06:41 +000079 SkCanvas origCanvas(origBitmap);
80
81 SkIRect streamRect = SkIRect::MakeWH(64, 64);
82 SkBitmap streamBitmap;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000083 create(&streamBitmap, streamRect);
bungeman@google.coma5501992012-05-18 19:06:41 +000084 SkCanvas streamCanvas(streamBitmap);
85
86 SkPoint point = SkPoint::Make(24, 32);
87
88 // Test: origTypeface and streamTypeface from orig data draw the same
89 drawBG(&origCanvas);
90 origCanvas.drawText("A", 1, point.fX, point.fY, paint);
91
92 SkTypeface* origTypeface = paint.getTypeface();
reed@google.comfed86bd2013-03-14 15:04:57 +000093 SkAutoTUnref<SkTypeface> aur;
94 if (NULL == origTypeface) {
95 origTypeface = aur.reset(SkTypeface::RefDefault());
96 }
97
98 int ttcIndex;
bungeman5f213d92015-01-27 05:39:10 -080099 SkAutoTDelete<SkStreamAsset> fontData(origTypeface->openStream(&ttcIndex));
scroggoa1193e42015-01-21 12:09:53 -0800100 SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData.detach());
caseq26337e92014-06-30 12:14:52 -0700101
102 SkFontDescriptor desc;
103 bool isLocalStream = false;
104 streamTypeface->getFontDescriptor(&desc, &isLocalStream);
105 REPORTER_ASSERT(reporter, isLocalStream);
106
bungeman@google.com148a3962013-01-17 20:19:13 +0000107 SkSafeUnref(paint.setTypeface(streamTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +0000108 drawBG(&streamCanvas);
109 streamCanvas.drawPosText("A", 1, &point, paint);
110
111 REPORTER_ASSERT(reporter,
112 compare(origBitmap, origRect, streamBitmap, streamRect));
113 }
114 //Make sure the typeface is deleted and removed.
115 SkGraphics::PurgeFontCache();
116}