skia2/tests/FontHostStreamTest.cpp
Mike Reed 22bee5ac77 Update FontHostStreamTest to use SkFont
This file was adjusted 3 times, beginning here

commit 212e9060ed
Author: Mike Reed <reed@google.com>
Date:   Tue Dec 25 17:35:49 2018 -0500

to try to update it to use SkFont. Along the way we broke the part where
it compares drawing with the typeface pre and post streaming. This CL
restores the original test, and then updated it to use SkFont.

After this CL, we should be able to move SK_SUPPORT_LEGACY_PAINT_FONT_FIELDS
to clients, having them opt-in.

Bug: skia:
Change-Id: Id786160322f8530f5d0c65b30a0c228139b39d61
Reviewed-on: https://skia-review.googlesource.com/c/182805
Reviewed-by: Mike Reed <reed@google.com>
Auto-Submit: Mike Reed <reed@google.com>
2019-01-10 12:06:05 +00:00

115 lines
3.4 KiB
C++

/*
* 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"
#include "SkFont.h"
#include "SkFontDescriptor.h"
#include "SkFontPriv.h"
#include "SkGraphics.h"
#include "SkPaint.h"
#include "SkPoint.h"
#include "SkRect.h"
#include "SkStream.h"
#include "SkTypeface.h"
#include "SkTypes.h"
#include "Test.h"
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;
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;
}
DEF_TEST(FontHostStream, reporter) {
{
SkPaint paint;
paint.setColor(SK_ColorGRAY);
SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30);
font.setEdging(SkFont::Edging::kAlias);
SkIRect origRect = SkIRect::MakeWH(64, 64);
SkBitmap origBitmap;
create(&origBitmap, origRect);
SkCanvas origCanvas(origBitmap);
SkIRect streamRect = SkIRect::MakeWH(64, 64);
SkBitmap streamBitmap;
create(&streamBitmap, streamRect);
SkCanvas streamCanvas(streamBitmap);
SkPoint point = SkPoint::Make(24, 32);
// Test: origTypeface and streamTypeface from orig data draw the same
drawBG(&origCanvas);
origCanvas.drawString("A", point.fX, point.fY, font, paint);
sk_sp<SkTypeface> typeface = SkFontPriv::RefTypefaceOrDefault(font);
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);
font.setTypeface(streamTypeface);
drawBG(&streamCanvas);
streamCanvas.drawString("A", point.fX, point.fY, font, paint);
REPORTER_ASSERT(reporter,
compare(origBitmap, origRect, streamBitmap, streamRect));
}
//Make sure the typeface is deleted and removed.
SkGraphics::PurgeFontCache();
}