2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkFontDescriptor.h"
12 #include "SkFontHost.h"
13 #include "SkGraphics.h"
18 #include "SkTypeface.h"
22 static const SkColor bgColor = SK_ColorWHITE;
24 static void create(SkBitmap* bm, SkIRect bound) {
25 bm->allocN32Pixels(bound.width(), bound.height());
28 static void drawBG(SkCanvas* canvas) {
29 canvas->drawColor(bgColor);
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
37 static bool compare(const SkBitmap& ref, const SkIRect& iref,
38 const SkBitmap& test, const SkIRect& itest)
40 const int xOff = itest.fLeft - iref.fLeft;
41 const int yOff = itest.fTop - iref.fTop;
43 SkAutoLockPixels alpRef(ref);
44 SkAutoLockPixels alpTest(test);
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);
52 if (refX >= 0 && refX < ref.width() &&
53 refY >= 0 && refY < ref.height())
55 refColor = ref.getColor(refX, refY);
59 if (refColor != testColor) {
67 DEF_TEST(FontHostStream, reporter) {
70 paint.setColor(SK_ColorGRAY);
71 paint.setTextSize(SkIntToScalar(30));
73 SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
75 SkSafeUnref(paint.setTypeface(fTypeface));
77 SkIRect origRect = SkIRect::MakeWH(64, 64);
79 create(&origBitmap, origRect);
80 SkCanvas origCanvas(origBitmap);
82 SkIRect streamRect = SkIRect::MakeWH(64, 64);
83 SkBitmap streamBitmap;
84 create(&streamBitmap, streamRect);
85 SkCanvas streamCanvas(streamBitmap);
87 SkPoint point = SkPoint::Make(24, 32);
89 // Test: origTypeface and streamTypeface from orig data draw the same
91 origCanvas.drawText("A", 1, point.fX, point.fY, paint);
93 SkTypeface* origTypeface = paint.getTypeface();
94 SkAutoTUnref<SkTypeface> aur;
95 if (NULL == origTypeface) {
96 origTypeface = aur.reset(SkTypeface::RefDefault());
100 SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex));
101 SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData);
103 SkFontDescriptor desc;
104 bool isLocalStream = false;
105 streamTypeface->getFontDescriptor(&desc, &isLocalStream);
106 REPORTER_ASSERT(reporter, isLocalStream);
108 SkSafeUnref(paint.setTypeface(streamTypeface));
109 drawBG(&streamCanvas);
110 streamCanvas.drawPosText("A", 1, &point, paint);
112 REPORTER_ASSERT(reporter,
113 compare(origBitmap, origRect, streamBitmap, streamRect));
115 //Make sure the typeface is deleted and removed.
116 SkGraphics::PurgeFontCache();