2012-05-18 19:06:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColor.h"
|
2014-06-30 19:14:52 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2012-05-18 19:06:41 +00:00
|
|
|
#include "SkFontHost.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
2013-11-14 14:47:56 +00:00
|
|
|
#include "SkStream.h"
|
2012-05-18 19:06:41 +00:00
|
|
|
#include "SkTypeface.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "SkTypes.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-05-18 19:06:41 +00:00
|
|
|
|
|
|
|
static const SkColor bgColor = SK_ColorWHITE;
|
|
|
|
|
2014-03-05 13:43:15 +00:00
|
|
|
static void create(SkBitmap* bm, SkIRect bound) {
|
|
|
|
bm->allocN32Pixels(bound.width(), bound.height());
|
2012-05-18 19:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void drawBG(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(bgColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assumes that the ref draw was completely inside ref canvas --
|
|
|
|
implies that everything outside is "bgColor".
|
|
|
|
Checks that all overlap is the same and that all non-overlap on the
|
|
|
|
ref is "bgColor".
|
|
|
|
*/
|
|
|
|
static bool compare(const SkBitmap& ref, const SkIRect& iref,
|
|
|
|
const SkBitmap& test, const SkIRect& itest)
|
|
|
|
{
|
|
|
|
const int xOff = itest.fLeft - iref.fLeft;
|
|
|
|
const int yOff = itest.fTop - iref.fTop;
|
|
|
|
|
|
|
|
SkAutoLockPixels alpRef(ref);
|
|
|
|
SkAutoLockPixels alpTest(test);
|
|
|
|
|
|
|
|
for (int y = 0; y < test.height(); ++y) {
|
|
|
|
for (int x = 0; x < test.width(); ++x) {
|
|
|
|
SkColor testColor = test.getColor(x, y);
|
|
|
|
int refX = x + xOff;
|
|
|
|
int refY = y + yOff;
|
|
|
|
SkColor refColor;
|
|
|
|
if (refX >= 0 && refX < ref.width() &&
|
|
|
|
refY >= 0 && refY < ref.height())
|
|
|
|
{
|
|
|
|
refColor = ref.getColor(refX, refY);
|
|
|
|
} else {
|
|
|
|
refColor = bgColor;
|
|
|
|
}
|
|
|
|
if (refColor != testColor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(FontHostStream, reporter) {
|
2012-05-18 19:06:41 +00:00
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGRAY);
|
|
|
|
paint.setTextSize(SkIntToScalar(30));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-05-07 14:32:58 +00:00
|
|
|
SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
|
|
|
|
SkTypeface::kNormal);
|
|
|
|
SkSafeUnref(paint.setTypeface(fTypeface));
|
2012-05-18 19:06:41 +00:00
|
|
|
|
|
|
|
SkIRect origRect = SkIRect::MakeWH(64, 64);
|
|
|
|
SkBitmap origBitmap;
|
2014-03-05 13:43:15 +00:00
|
|
|
create(&origBitmap, origRect);
|
2012-05-18 19:06:41 +00:00
|
|
|
SkCanvas origCanvas(origBitmap);
|
|
|
|
|
|
|
|
SkIRect streamRect = SkIRect::MakeWH(64, 64);
|
|
|
|
SkBitmap streamBitmap;
|
2014-03-05 13:43:15 +00:00
|
|
|
create(&streamBitmap, streamRect);
|
2012-05-18 19:06:41 +00:00
|
|
|
SkCanvas streamCanvas(streamBitmap);
|
|
|
|
|
|
|
|
SkPoint point = SkPoint::Make(24, 32);
|
|
|
|
|
|
|
|
// Test: origTypeface and streamTypeface from orig data draw the same
|
|
|
|
drawBG(&origCanvas);
|
|
|
|
origCanvas.drawText("A", 1, point.fX, point.fY, paint);
|
|
|
|
|
|
|
|
SkTypeface* origTypeface = paint.getTypeface();
|
2013-03-14 15:04:57 +00:00
|
|
|
SkAutoTUnref<SkTypeface> aur;
|
|
|
|
if (NULL == origTypeface) {
|
|
|
|
origTypeface = aur.reset(SkTypeface::RefDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
int ttcIndex;
|
2013-11-14 14:47:56 +00:00
|
|
|
SkAutoTUnref<SkStream> fontData(origTypeface->openStream(&ttcIndex));
|
2012-05-18 19:06:41 +00:00
|
|
|
SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData);
|
2014-06-30 19:14:52 +00:00
|
|
|
|
|
|
|
SkFontDescriptor desc;
|
|
|
|
bool isLocalStream = false;
|
|
|
|
streamTypeface->getFontDescriptor(&desc, &isLocalStream);
|
|
|
|
REPORTER_ASSERT(reporter, isLocalStream);
|
|
|
|
|
2013-01-17 20:19:13 +00:00
|
|
|
SkSafeUnref(paint.setTypeface(streamTypeface));
|
2012-05-18 19:06:41 +00:00
|
|
|
drawBG(&streamCanvas);
|
|
|
|
streamCanvas.drawPosText("A", 1, &point, paint);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
compare(origBitmap, origRect, streamBitmap, streamRect));
|
|
|
|
}
|
|
|
|
//Make sure the typeface is deleted and removed.
|
|
|
|
SkGraphics::PurgeFontCache();
|
|
|
|
}
|