2015-05-20 16:21:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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 "gm/gm.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontMgr.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/private/SkFixed.h"
|
|
|
|
#include "src/core/SkFontDescriptor.h"
|
|
|
|
#include "tools/Resources.h"
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class FontScalerDistortableGM : public GM {
|
|
|
|
public:
|
|
|
|
FontScalerDistortableGM() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("fontscalerdistortable");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(550, 700);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2015-05-20 16:21:04 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2018-12-15 18:45:33 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
|
2015-05-20 16:21:04 +00:00
|
|
|
|
2018-06-07 21:19:14 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf"));
|
|
|
|
sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf"));
|
2018-07-11 15:31:34 +00:00
|
|
|
|
2018-06-07 21:19:14 +00:00
|
|
|
if (!distortableStream) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "No distortableStream";
|
|
|
|
return DrawResult::kFail;
|
2015-05-20 16:21:04 +00:00
|
|
|
}
|
|
|
|
const char* text = "abc";
|
|
|
|
const size_t textLen = strlen(text);
|
|
|
|
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
SkScalar y = SkIntToScalar(20);
|
|
|
|
|
2016-01-21 22:17:47 +00:00
|
|
|
SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
|
2018-06-07 21:19:14 +00:00
|
|
|
SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5)));
|
2017-02-24 16:15:26 +00:00
|
|
|
SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
|
|
|
|
SkFontArguments::VariationPosition position =
|
|
|
|
{ coordinates, SK_ARRAY_COUNT(coordinates) };
|
2018-06-07 21:19:14 +00:00
|
|
|
if (j == 0 && distortable) {
|
2019-04-17 20:04:30 +00:00
|
|
|
sk_sp<SkTypeface> clone = distortable->makeClone(
|
|
|
|
SkFontArguments().setVariationDesignPosition(position));
|
|
|
|
font.setTypeface(clone ? std::move(clone) : distortable);
|
2018-06-07 21:19:14 +00:00
|
|
|
} else {
|
2019-04-17 20:04:30 +00:00
|
|
|
font.setTypeface(fontMgr->makeFromStream(
|
2018-06-07 21:19:14 +00:00
|
|
|
distortableStream->duplicate(),
|
2019-04-17 20:04:30 +00:00
|
|
|
SkFontArguments().setVariationDesignPosition(position)));
|
2018-06-07 21:19:14 +00:00
|
|
|
}
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
|
2016-07-12 22:01:19 +00:00
|
|
|
canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
SkRect r;
|
|
|
|
r.set(x - SkIntToScalar(3), SkIntToScalar(15),
|
|
|
|
x - SkIntToScalar(1), SkIntToScalar(280));
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ps = 6; ps <= 22; ps++) {
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setSize(SkIntToScalar(ps));
|
|
|
|
canvas->drawSimpleText(text, textLen, kUTF8_SkTextEncoding, x, y, font, paint);
|
|
|
|
y += font.getMetrics(nullptr);
|
2015-05-20 16:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->translate(0, SkIntToScalar(360));
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setSubpixel(true);
|
2015-05-20 16:21:04 +00:00
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2015-05-20 16:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new FontScalerDistortableGM; )
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
}
|