blob: e15bd5a355ea0334a8138413cd31e95628a9830d [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 */
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"
robertphillips@google.comcb3b6152013-11-14 14:47:56 +000018#include "SkStream.h"
bungeman@google.coma5501992012-05-18 19:06:41 +000019#include "SkTypeface.h"
20
21///////////////////////////////////////////////////////////////////////////////
22
23static const SkColor bgColor = SK_ColorWHITE;
24
25static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
26 bm->setConfig(config, bound.width(), bound.height());
27 bm->allocPixels();
28}
29
30static void drawBG(SkCanvas* canvas) {
31 canvas->drawColor(bgColor);
32}
33
34/** Assumes that the ref draw was completely inside ref canvas --
35 implies that everything outside is "bgColor".
36 Checks that all overlap is the same and that all non-overlap on the
37 ref is "bgColor".
38 */
39static bool compare(const SkBitmap& ref, const SkIRect& iref,
40 const SkBitmap& test, const SkIRect& itest)
41{
42 const int xOff = itest.fLeft - iref.fLeft;
43 const int yOff = itest.fTop - iref.fTop;
44
45 SkAutoLockPixels alpRef(ref);
46 SkAutoLockPixels alpTest(test);
47
48 for (int y = 0; y < test.height(); ++y) {
49 for (int x = 0; x < test.width(); ++x) {
50 SkColor testColor = test.getColor(x, y);
51 int refX = x + xOff;
52 int refY = y + yOff;
53 SkColor refColor;
54 if (refX >= 0 && refX < ref.width() &&
55 refY >= 0 && refY < ref.height())
56 {
57 refColor = ref.getColor(refX, refY);
58 } else {
59 refColor = bgColor;
60 }
61 if (refColor != testColor) {
62 return false;
63 }
64 }
65 }
66 return true;
67}
68
69static void test_fontHostStream(skiatest::Reporter* reporter) {
70
71 {
72 SkPaint paint;
73 paint.setColor(SK_ColorGRAY);
74 paint.setTextSize(SkIntToScalar(30));
rmistry@google.comd6176b02012-08-23 18:14:13 +000075
commit-bot@chromium.org66f5aaa2013-05-07 14:32:58 +000076 SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
77 SkTypeface::kNormal);
78 SkSafeUnref(paint.setTypeface(fTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +000079
80 SkIRect origRect = SkIRect::MakeWH(64, 64);
81 SkBitmap origBitmap;
82 create(&origBitmap, origRect, SkBitmap::kARGB_8888_Config);
83 SkCanvas origCanvas(origBitmap);
84
85 SkIRect streamRect = SkIRect::MakeWH(64, 64);
86 SkBitmap streamBitmap;
87 create(&streamBitmap, streamRect, SkBitmap::kARGB_8888_Config);
88 SkCanvas streamCanvas(streamBitmap);
89
90 SkPoint point = SkPoint::Make(24, 32);
91
92 // Test: origTypeface and streamTypeface from orig data draw the same
93 drawBG(&origCanvas);
94 origCanvas.drawText("A", 1, point.fX, point.fY, paint);
95
96 SkTypeface* origTypeface = paint.getTypeface();
reed@google.comfed86bd2013-03-14 15:04:57 +000097 SkAutoTUnref<SkTypeface> aur;
98 if (NULL == origTypeface) {
99 origTypeface = aur.reset(SkTypeface::RefDefault());
100 }
101
102 int ttcIndex;
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000103 SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex));
bungeman@google.coma5501992012-05-18 19:06:41 +0000104 SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData);
bungeman@google.com148a3962013-01-17 20:19:13 +0000105 SkSafeUnref(paint.setTypeface(streamTypeface));
bungeman@google.coma5501992012-05-18 19:06:41 +0000106 drawBG(&streamCanvas);
107 streamCanvas.drawPosText("A", 1, &point, paint);
108
109 REPORTER_ASSERT(reporter,
110 compare(origBitmap, origRect, streamBitmap, streamRect));
111 }
112 //Make sure the typeface is deleted and removed.
113 SkGraphics::PurgeFontCache();
114}
115
116#include "TestClassDef.h"
117DEFINE_TESTCLASS("FontHost::CreateTypefaceFromStream", FontHostStreamTestClass, test_fontHostStream)