2018-06-22 21:11:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
2019-01-08 18:01:58 +00:00
|
|
|
#include "SkFont.h"
|
2018-06-22 21:11:00 +00:00
|
|
|
#include "SkFontMgr.h"
|
|
|
|
#include "SkFontMgr_fontconfig.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
2018-09-19 15:31:27 +00:00
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
|
2018-06-22 21:11:00 +00:00
|
|
|
static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
|
|
|
|
for (int y = 0; y < test.height(); ++y) {
|
|
|
|
for (int x = 0; x < test.width(); ++x) {
|
|
|
|
SkColor testColor = test.getColor(x, y);
|
|
|
|
SkColor refColor = ref.getColor(x, y);
|
|
|
|
if (refColor != testColor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(FontMgrFontConfig, reporter) {
|
|
|
|
FcConfig* config = FcConfigCreate();
|
|
|
|
SkString distortablePath = GetResourcePath("fonts/Distortable.ttf");
|
|
|
|
FcConfigAppFontAddFile(
|
|
|
|
config, reinterpret_cast<const FcChar8*>(distortablePath.c_str()));
|
|
|
|
FcConfigBuildFonts(config);
|
|
|
|
|
|
|
|
sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
|
|
|
|
sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
|
|
|
|
|
|
|
|
SkBitmap bitmapStream;
|
|
|
|
bitmapStream.allocN32Pixels(64, 64);
|
|
|
|
SkCanvas canvasStream(bitmapStream);
|
|
|
|
canvasStream.drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkBitmap bitmapClone;
|
|
|
|
bitmapClone.allocN32Pixels(64, 64);
|
|
|
|
SkCanvas canvasClone(bitmapClone);
|
|
|
|
canvasStream.drawColor(SK_ColorWHITE);
|
|
|
|
|
2019-01-08 18:01:58 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGRAY);
|
2018-06-22 21:11:00 +00:00
|
|
|
|
2019-01-08 18:01:58 +00:00
|
|
|
constexpr float kTextSize = 20;
|
2018-06-22 21:11:00 +00:00
|
|
|
|
|
|
|
std::unique_ptr<SkStreamAsset> distortableStream(
|
|
|
|
GetResourceAsStream("fonts/Distortable.ttf"));
|
|
|
|
if (!distortableStream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPoint point = SkPoint::Make(20.0f, 20.0f);
|
|
|
|
SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
SkScalar styleValue =
|
|
|
|
SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
|
|
|
|
SkFontArguments::VariationPosition::Coordinate
|
|
|
|
coordinates[] = {{tag, styleValue}};
|
|
|
|
SkFontArguments::VariationPosition
|
|
|
|
position = {coordinates, SK_ARRAY_COUNT(coordinates)};
|
|
|
|
|
2019-01-08 18:01:58 +00:00
|
|
|
SkFont fontStream(
|
2018-06-22 21:11:00 +00:00
|
|
|
fontMgr->makeFromStream(distortableStream->duplicate(),
|
2019-01-08 18:01:58 +00:00
|
|
|
SkFontArguments().setVariationDesignPosition(position)),
|
|
|
|
kTextSize);
|
|
|
|
fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
|
|
|
|
SkFont fontClone(
|
|
|
|
typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
|
|
|
|
fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
|
|
|
|
constexpr char text[] = "abc";
|
2018-06-22 21:11:00 +00:00
|
|
|
|
|
|
|
canvasStream.drawColor(SK_ColorWHITE);
|
2019-01-08 18:01:58 +00:00
|
|
|
canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
|
2018-06-22 21:11:00 +00:00
|
|
|
|
|
|
|
canvasClone.drawColor(SK_ColorWHITE);
|
2019-01-08 18:01:58 +00:00
|
|
|
canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
|
2018-06-22 21:11:00 +00:00
|
|
|
|
|
|
|
bool success = bitmap_compare(bitmapStream, bitmapClone);
|
|
|
|
REPORTER_ASSERT(reporter, success);
|
|
|
|
}
|
2018-09-19 15:31:27 +00:00
|
|
|
}
|