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-05-01 21:28:53 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontArguments.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFontMgr.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2022-05-18 20:02:42 +00:00
|
|
|
#include "tools/SkMetaData.h"
|
2022-07-08 14:35:18 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-05-20 16:21:04 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2015-05-20 16:21:04 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class FontScalerDistortableGM : public GM {
|
|
|
|
public:
|
|
|
|
FontScalerDistortableGM() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
2019-08-08 22:33:05 +00:00
|
|
|
private:
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("fontscalerdistortable");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(550, 700);
|
|
|
|
}
|
|
|
|
|
2022-05-18 20:02:42 +00:00
|
|
|
bool fDirty = true;
|
|
|
|
bool fOverride = false;
|
|
|
|
|
2022-07-08 14:35:18 +00:00
|
|
|
ToolUtils::VariationSliders fVariationSliders;
|
2022-05-18 20:02:42 +00:00
|
|
|
|
|
|
|
bool onGetControls(SkMetaData* controls) override {
|
|
|
|
controls->setBool("Override", fOverride);
|
2022-07-08 14:35:18 +00:00
|
|
|
return fVariationSliders.writeControls(controls);
|
2022-05-18 20:02:42 +00:00
|
|
|
}
|
2020-01-17 20:55:14 +00:00
|
|
|
|
2022-05-18 20:02:42 +00:00
|
|
|
void onSetControls(const SkMetaData& controls) override {
|
|
|
|
bool oldOverride = fOverride;
|
|
|
|
controls.findBool("Override", &fOverride);
|
|
|
|
if (fOverride != oldOverride) {
|
|
|
|
fDirty = true;
|
|
|
|
}
|
|
|
|
|
2022-07-08 14:35:18 +00:00
|
|
|
return fVariationSliders.readControls(controls, &fDirty);
|
2022-05-18 20:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Info {
|
|
|
|
sk_sp<SkTypeface> distortable;
|
|
|
|
SkFourByteTag axisTag;
|
|
|
|
SkScalar axisMin;
|
|
|
|
SkScalar axisMax;
|
|
|
|
} fInfo;
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2020-01-17 20:55:14 +00:00
|
|
|
constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t');
|
|
|
|
//constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h');
|
2022-05-18 20:02:42 +00:00
|
|
|
fInfo = {
|
2020-01-17 20:55:14 +00:00
|
|
|
MakeResourceAsTypeface("fonts/Distortable.ttf"), wght, 0.5f, 2.0f
|
|
|
|
//SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f
|
|
|
|
//SkTypeface::MakeFromName("Skia", SkFontStyle()), wdth, 0.62f, 1.3f
|
|
|
|
//SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f
|
|
|
|
//SkTypeface::MakeFromName(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f
|
|
|
|
};
|
2022-05-18 20:02:42 +00:00
|
|
|
|
|
|
|
if (fInfo.distortable) {
|
2022-07-08 14:35:18 +00:00
|
|
|
fVariationSliders = ToolUtils::VariationSliders(fInfo.distortable.get());
|
2022-05-18 20:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static constexpr int rows = 2;
|
|
|
|
inline static constexpr int cols = 5;
|
|
|
|
sk_sp<SkTypeface> typeface[rows][cols];
|
|
|
|
|
|
|
|
void updateTypefaces() {
|
|
|
|
sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
|
|
|
|
|
|
|
|
std::unique_ptr<SkStreamAsset> distortableStream( fInfo.distortable
|
|
|
|
? fInfo.distortable->openStream(nullptr)
|
2020-01-17 20:55:14 +00:00
|
|
|
: nullptr);
|
|
|
|
for (int row = 0; row < rows; ++row) {
|
|
|
|
for (int col = 0; col < cols; ++col) {
|
2022-05-18 20:02:42 +00:00
|
|
|
using Coordinate = SkFontArguments::VariationPosition::Coordinate;
|
2022-07-08 14:35:18 +00:00
|
|
|
SkFontArguments::VariationPosition position;
|
|
|
|
Coordinate coordinates[2];
|
|
|
|
|
2022-05-18 20:02:42 +00:00
|
|
|
if (fOverride) {
|
2022-07-08 14:35:18 +00:00
|
|
|
SkSpan<const Coordinate> user_coordinates = fVariationSliders.getCoordinates();
|
|
|
|
position = {user_coordinates.data(), static_cast<int>(user_coordinates.size())};
|
|
|
|
|
2022-05-18 20:02:42 +00:00
|
|
|
} else {
|
2022-07-08 14:35:18 +00:00
|
|
|
const int coordinateCount = 2;
|
2022-05-18 20:02:42 +00:00
|
|
|
SkScalar styleValue = SkScalarInterp(fInfo.axisMin, fInfo.axisMax,
|
|
|
|
SkScalar(row*cols + col) / (rows*cols));
|
|
|
|
coordinates[0] = {fInfo.axisTag, styleValue};
|
|
|
|
coordinates[1] = {fInfo.axisTag, styleValue};
|
2022-07-08 14:35:18 +00:00
|
|
|
position = {coordinates, static_cast<int>(coordinateCount)};
|
2022-05-18 20:02:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 20:55:14 +00:00
|
|
|
typeface[row][col] = [&]() -> sk_sp<SkTypeface> {
|
2022-05-18 20:02:42 +00:00
|
|
|
if (row == 0 && fInfo.distortable) {
|
|
|
|
return fInfo.distortable->makeClone(
|
2020-01-17 20:55:14 +00:00
|
|
|
SkFontArguments().setVariationDesignPosition(position));
|
|
|
|
}
|
|
|
|
if (distortableStream) {
|
|
|
|
return fontMgr->makeFromStream(distortableStream->duplicate(),
|
|
|
|
SkFontArguments().setVariationDesignPosition(position));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 20:02:42 +00:00
|
|
|
fDirty = false;
|
2020-01-17 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2022-05-18 20:02:42 +00:00
|
|
|
if (fDirty) {
|
|
|
|
this->updateTypefaces();
|
|
|
|
}
|
|
|
|
|
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);
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
const char* text = "abc";
|
|
|
|
const size_t textLen = strlen(text);
|
|
|
|
|
2019-08-08 22:33:05 +00:00
|
|
|
for (int row = 0; row < rows; ++row) {
|
|
|
|
for (int col = 0; col < cols; ++col) {
|
2015-05-20 16:21:04 +00:00
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
SkScalar y = SkIntToScalar(20);
|
|
|
|
|
2020-01-17 20:55:14 +00:00
|
|
|
font.setTypeface(typeface[row][col] ? typeface[row][col] : nullptr);
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2019-08-08 22:33:05 +00:00
|
|
|
canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20));
|
|
|
|
canvas->rotate(SkIntToScalar(col * 5), x, y * 10);
|
2015-05-20 16:21:04 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setLTRB(x - 3, 15, x - 1, 280);
|
2015-05-20 16:21:04 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ps = 6; ps <= 22; ps++) {
|
2018-12-15 18:45:33 +00:00
|
|
|
font.setSize(SkIntToScalar(ps));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
|
2018-12-15 18:45:33 +00:00
|
|
|
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);
|
2019-04-18 20:37:45 +00:00
|
|
|
font.setLinearMetrics(true);
|
2019-08-27 20:20:39 +00:00
|
|
|
font.setBaselineSnap(false);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new FontScalerDistortableGM; )
|
2015-05-20 16:21:04 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|