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 "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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-31 18:42:36 +00:00
|
|
|
paint.setTypeface(SkTypeface::MakeFromName("Georgia", SkFontStyle()));
|
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);
|
2017-04-28 19:35:12 +00:00
|
|
|
origCanvas.drawString("A", point.fX, point.fY, paint);
|
2012-05-18 19:06:41 +00:00
|
|
|
|
2017-01-12 15:13:40 +00:00
|
|
|
sk_sp<SkTypeface> typeface(paint.getTypeface() ? paint.refTypeface()
|
|
|
|
: SkTypeface::MakeDefault());
|
2013-03-14 15:04:57 +00:00
|
|
|
int ttcIndex;
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
|
2017-11-09 18:45:10 +00:00
|
|
|
if (!fontData) {
|
|
|
|
// We're using a SkTypeface that can't give us a stream.
|
|
|
|
// This happens with portable or system fonts. End the test now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release()));
|
2014-06-30 19:14:52 +00:00
|
|
|
|
|
|
|
SkFontDescriptor desc;
|
|
|
|
bool isLocalStream = false;
|
|
|
|
streamTypeface->getFontDescriptor(&desc, &isLocalStream);
|
|
|
|
REPORTER_ASSERT(reporter, isLocalStream);
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
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();
|
|
|
|
}
|