blob: b9f39b311080aeac3b31414d162e5f44983c1e53 [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
8#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000010#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"
robertphillips@google.comcb3b6152013-11-14 14:47:56 +000018#include "SkStream.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000019#include "SkTypeface.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000020#include "SkTypes.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000021
22static const SkColor bgColor = SK_ColorWHITE;
23
24static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
25 bm->setConfig(config, bound.width(), bound.height());
26 bm->allocPixels();
27}
28
29static 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 */
38static 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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000068DEF_TEST(FontHostStream, reporter) {
bungeman@google.coma5501992012-05-18 19:06:41 +000069 {
70 SkPaint paint;
71 paint.setColor(SK_ColorGRAY);
72 paint.setTextSize(SkIntToScalar(30));
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
commit-bot@chromium.org66f5aaa2013-05-07 14:32:58 +000074 SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
75 SkTypeface::kNormal);
76 SkSafeUnref(paint.setTypeface(fTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +000077
78 SkIRect origRect = SkIRect::MakeWH(64, 64);
79 SkBitmap origBitmap;
80 create(&origBitmap, origRect, SkBitmap::kARGB_8888_Config);
81 SkCanvas origCanvas(origBitmap);
82
83 SkIRect streamRect = SkIRect::MakeWH(64, 64);
84 SkBitmap streamBitmap;
85 create(&streamBitmap, streamRect, SkBitmap::kARGB_8888_Config);
86 SkCanvas streamCanvas(streamBitmap);
87
88 SkPoint point = SkPoint::Make(24, 32);
89
90 // Test: origTypeface and streamTypeface from orig data draw the same
91 drawBG(&origCanvas);
92 origCanvas.drawText("A", 1, point.fX, point.fY, paint);
93
94 SkTypeface* origTypeface = paint.getTypeface();
reed@google.comfed86bd2013-03-14 15:04:57 +000095 SkAutoTUnref<SkTypeface> aur;
96 if (NULL == origTypeface) {
97 origTypeface = aur.reset(SkTypeface::RefDefault());
98 }
99
100 int ttcIndex;
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000101 SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex));
bungeman@google.coma5501992012-05-18 19:06:41 +0000102 SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData);
bungeman@google.com148a3962013-01-17 20:19:13 +0000103 SkSafeUnref(paint.setTypeface(streamTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +0000104 drawBG(&streamCanvas);
105 streamCanvas.drawPosText("A", 1, &point, paint);
106
107 REPORTER_ASSERT(reporter,
108 compare(origBitmap, origRect, streamBitmap, streamRect));
109 }
110 //Make sure the typeface is deleted and removed.
111 SkGraphics::PurgeFontCache();
112}