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"
|
2018-12-25 22:35:49 +00:00
|
|
|
#include "SkFont.h"
|
2014-06-30 19:14:52 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2019-01-10 02:48:09 +00:00
|
|
|
#include "SkFontPriv.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
|
|
|
|
2019-01-09 17:51:52 +00:00
|
|
|
static const SkColor bgColor = SK_ColorWHITE;
|
|
|
|
|
|
|
|
static void create(SkBitmap* bm, SkIRect bound) {
|
|
|
|
bm->allocN32Pixels(bound.width(), bound.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-05-18 19:06:41 +00:00
|
|
|
for (int y = 0; y < test.height(); ++y) {
|
|
|
|
for (int x = 0; x < test.width(); ++x) {
|
2019-01-09 17:51:52 +00:00
|
|
|
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) {
|
2012-05-18 19:06:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:01:58 +00:00
|
|
|
DEF_TEST(FontHostStream, reporter) {
|
2019-01-09 17:51:52 +00:00
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGRAY);
|
|
|
|
|
|
|
|
SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30);
|
2019-01-10 02:48:09 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
2019-01-09 17:51:52 +00:00
|
|
|
|
|
|
|
SkIRect origRect = SkIRect::MakeWH(64, 64);
|
|
|
|
SkBitmap origBitmap;
|
|
|
|
create(&origBitmap, origRect);
|
|
|
|
SkCanvas origCanvas(origBitmap);
|
2012-05-18 19:06:41 +00:00
|
|
|
|
2019-01-09 17:51:52 +00:00
|
|
|
SkIRect streamRect = SkIRect::MakeWH(64, 64);
|
|
|
|
SkBitmap streamBitmap;
|
|
|
|
create(&streamBitmap, streamRect);
|
|
|
|
SkCanvas streamCanvas(streamBitmap);
|
2017-11-09 18:45:10 +00:00
|
|
|
|
2019-01-09 17:51:52 +00:00
|
|
|
SkPoint point = SkPoint::Make(24, 32);
|
2014-06-30 19:14:52 +00:00
|
|
|
|
2019-01-09 17:51:52 +00:00
|
|
|
// Test: origTypeface and streamTypeface from orig data draw the same
|
|
|
|
drawBG(&origCanvas);
|
2019-01-10 02:48:09 +00:00
|
|
|
origCanvas.drawString("A", point.fX, point.fY, font, paint);
|
2014-06-30 19:14:52 +00:00
|
|
|
|
2019-01-22 19:45:16 +00:00
|
|
|
sk_sp<SkTypeface> typeface = font.refTypefaceOrDefault();
|
2019-01-09 17:51:52 +00:00
|
|
|
int ttcIndex;
|
|
|
|
std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData)));
|
|
|
|
|
|
|
|
SkFontDescriptor desc;
|
|
|
|
bool isLocalStream = false;
|
|
|
|
streamTypeface->getFontDescriptor(&desc, &isLocalStream);
|
|
|
|
REPORTER_ASSERT(reporter, isLocalStream);
|
2012-05-18 19:06:41 +00:00
|
|
|
|
2019-01-10 02:48:09 +00:00
|
|
|
font.setTypeface(streamTypeface);
|
2019-01-09 17:51:52 +00:00
|
|
|
drawBG(&streamCanvas);
|
2019-01-10 02:48:09 +00:00
|
|
|
streamCanvas.drawString("A", point.fX, point.fY, font, paint);
|
2019-01-09 17:51:52 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
compare(origBitmap, origRect, streamBitmap, streamRect));
|
|
|
|
}
|
2012-05-18 19:06:41 +00:00
|
|
|
//Make sure the typeface is deleted and removed.
|
|
|
|
SkGraphics::PurgeFontCache();
|
|
|
|
}
|