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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "src/core/SkFontDescriptor.h"
|
|
|
|
#include "src/core/SkFontPriv.h"
|
|
|
|
#include "tests/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;
|
2019-02-26 20:39:41 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
|
2019-01-09 17:51:52 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|