blob: b6c4ef50bc4bcd8314b71ff56881291c3cefe114 [file] [log] [blame]
bungeman41868fe2015-05-20 09:21:04 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkFontArguments.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFontMgr.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkStream.h"
20#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/Resources.h"
Ben Wagner884d4592022-05-18 16:02:42 -040024#include "tools/SkMetaData.h"
Dominik Röttschesf27608b2022-07-08 17:35:18 +030025#include "tools/ToolUtils.h"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040026#include "tools/fonts/FontToolUtils.h"
bungeman41868fe2015-05-20 09:21:04 -070027
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include <string.h>
29#include <memory>
30#include <utility>
31
bungeman41868fe2015-05-20 09:21:04 -070032namespace skiagm {
33
34class FontScalerDistortableGM : public GM {
35public:
36 FontScalerDistortableGM() {
37 this->setBGColor(0xFFFFFFFF);
38 }
39
Ben Wagnerfbac83a2019-08-08 18:33:05 -040040private:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000041 SkString getName() const override { return SkString("fontscalerdistortable"); }
bungeman41868fe2015-05-20 09:21:04 -070042
Leandro Lovisolo8f023882023-08-15 21:13:52 +000043 SkISize getISize() override { return SkISize::Make(550, 700); }
bungeman41868fe2015-05-20 09:21:04 -070044
Ben Wagner884d4592022-05-18 16:02:42 -040045 bool fDirty = true;
46 bool fOverride = false;
Ben Wagner782a9572020-01-17 15:55:14 -050047
Dominik Röttschesf27608b2022-07-08 17:35:18 +030048 ToolUtils::VariationSliders fVariationSliders;
Ben Wagner884d4592022-05-18 16:02:42 -040049
50 bool onGetControls(SkMetaData* controls) override {
51 controls->setBool("Override", fOverride);
Dominik Röttschesf27608b2022-07-08 17:35:18 +030052 return fVariationSliders.writeControls(controls);
Ben Wagner884d4592022-05-18 16:02:42 -040053 }
54
55 void onSetControls(const SkMetaData& controls) override {
56 bool oldOverride = fOverride;
57 controls.findBool("Override", &fOverride);
58 if (fOverride != oldOverride) {
59 fDirty = true;
60 }
61
Dominik Röttschesf27608b2022-07-08 17:35:18 +030062 return fVariationSliders.readControls(controls, &fDirty);
Ben Wagner884d4592022-05-18 16:02:42 -040063 }
64
65 struct Info {
66 sk_sp<SkTypeface> distortable;
67 SkFourByteTag axisTag;
68 SkScalar axisMin;
69 SkScalar axisMax;
70 } fInfo;
71
72 void onOnceBeforeDraw() override {
Ben Wagner782a9572020-01-17 15:55:14 -050073 constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t');
74 //constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h');
Ben Wagner884d4592022-05-18 16:02:42 -040075 fInfo = {
Kevin Lubick1e971192023-11-10 16:14:44 -050076 ToolUtils::CreateTypefaceFromResource("fonts/Distortable.ttf"), wght, 0.5f, 2.0f
Ben Wagner782a9572020-01-17 15:55:14 -050077 //SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f
Kevin Lubick1e971192023-11-10 16:14:44 -050078 //ToolUtils::CreateTestTypeface("Skia", SkFontStyle()), wdth, 0.62f, 1.3f
Ben Wagner782a9572020-01-17 15:55:14 -050079 //SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f
Kevin Lubick1e971192023-11-10 16:14:44 -050080 //ToolUtils::CreateTestTypeface(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f
Ben Wagner782a9572020-01-17 15:55:14 -050081 };
Ben Wagner884d4592022-05-18 16:02:42 -040082
Kevin Lubickbca43ec2023-10-30 10:11:22 -040083 if (!fInfo.distortable) {
84 fInfo.distortable = ToolUtils::DefaultPortableTypeface();
Ben Wagner884d4592022-05-18 16:02:42 -040085 }
Kevin Lubickbca43ec2023-10-30 10:11:22 -040086 SkASSERT(fInfo.distortable);
87 fVariationSliders = ToolUtils::VariationSliders(fInfo.distortable.get());
Ben Wagner884d4592022-05-18 16:02:42 -040088 }
89
90 inline static constexpr int rows = 2;
91 inline static constexpr int cols = 5;
92 sk_sp<SkTypeface> typeface[rows][cols];
93
94 void updateTypefaces() {
Kevin Lubickbca43ec2023-10-30 10:11:22 -040095 sk_sp<SkFontMgr> fontMgr = ToolUtils::TestFontMgr();
Ben Wagner884d4592022-05-18 16:02:42 -040096
97 std::unique_ptr<SkStreamAsset> distortableStream( fInfo.distortable
98 ? fInfo.distortable->openStream(nullptr)
Ben Wagner782a9572020-01-17 15:55:14 -050099 : nullptr);
100 for (int row = 0; row < rows; ++row) {
101 for (int col = 0; col < cols; ++col) {
Ben Wagner884d4592022-05-18 16:02:42 -0400102 using Coordinate = SkFontArguments::VariationPosition::Coordinate;
Dominik Röttschesf27608b2022-07-08 17:35:18 +0300103 SkFontArguments::VariationPosition position;
104 Coordinate coordinates[2];
105
Ben Wagner884d4592022-05-18 16:02:42 -0400106 if (fOverride) {
Dominik Röttschesf27608b2022-07-08 17:35:18 +0300107 SkSpan<const Coordinate> user_coordinates = fVariationSliders.getCoordinates();
108 position = {user_coordinates.data(), static_cast<int>(user_coordinates.size())};
109
Ben Wagner884d4592022-05-18 16:02:42 -0400110 } else {
Dominik Röttschesf27608b2022-07-08 17:35:18 +0300111 const int coordinateCount = 2;
Ben Wagner884d4592022-05-18 16:02:42 -0400112 SkScalar styleValue = SkScalarInterp(fInfo.axisMin, fInfo.axisMax,
113 SkScalar(row*cols + col) / (rows*cols));
114 coordinates[0] = {fInfo.axisTag, styleValue};
115 coordinates[1] = {fInfo.axisTag, styleValue};
Dominik Röttschesf27608b2022-07-08 17:35:18 +0300116 position = {coordinates, static_cast<int>(coordinateCount)};
Ben Wagner884d4592022-05-18 16:02:42 -0400117 }
Ben Wagner884d4592022-05-18 16:02:42 -0400118
Ben Wagner782a9572020-01-17 15:55:14 -0500119 typeface[row][col] = [&]() -> sk_sp<SkTypeface> {
Ben Wagner884d4592022-05-18 16:02:42 -0400120 if (row == 0 && fInfo.distortable) {
121 return fInfo.distortable->makeClone(
Ben Wagner782a9572020-01-17 15:55:14 -0500122 SkFontArguments().setVariationDesignPosition(position));
123 }
124 if (distortableStream) {
125 return fontMgr->makeFromStream(distortableStream->duplicate(),
126 SkFontArguments().setVariationDesignPosition(position));
127 }
128 return nullptr;
129 }();
130 }
131 }
Ben Wagner884d4592022-05-18 16:02:42 -0400132 fDirty = false;
Ben Wagner782a9572020-01-17 15:55:14 -0500133 }
134
Chris Dalton50e24d72019-02-07 16:20:09 -0700135 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Ben Wagner884d4592022-05-18 16:02:42 -0400136 if (fDirty) {
137 this->updateTypefaces();
138 }
139
bungeman41868fe2015-05-20 09:21:04 -0700140 SkPaint paint;
141 paint.setAntiAlias(true);
Mike Reed2e6db182018-12-15 13:45:33 -0500142 SkFont font;
143 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
bungeman41868fe2015-05-20 09:21:04 -0700144
bungeman41868fe2015-05-20 09:21:04 -0700145 const char* text = "abc";
146 const size_t textLen = strlen(text);
147
Ben Wagnerfbac83a2019-08-08 18:33:05 -0400148 for (int row = 0; row < rows; ++row) {
149 for (int col = 0; col < cols; ++col) {
bungeman41868fe2015-05-20 09:21:04 -0700150 SkScalar x = SkIntToScalar(10);
151 SkScalar y = SkIntToScalar(20);
152
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400153 font.setTypeface(typeface[row][col] ? typeface[row][col] :
154 ToolUtils::DefaultPortableTypeface());
bungeman41868fe2015-05-20 09:21:04 -0700155
156 SkAutoCanvasRestore acr(canvas, true);
Ben Wagnerfbac83a2019-08-08 18:33:05 -0400157 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20));
158 canvas->rotate(SkIntToScalar(col * 5), x, y * 10);
bungeman41868fe2015-05-20 09:21:04 -0700159
160 {
161 SkPaint p;
162 p.setAntiAlias(true);
163 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400164 r.setLTRB(x - 3, 15, x - 1, 280);
bungeman41868fe2015-05-20 09:21:04 -0700165 canvas->drawRect(r, p);
166 }
167
168 for (int ps = 6; ps <= 22; ps++) {
Mike Reed2e6db182018-12-15 13:45:33 -0500169 font.setSize(SkIntToScalar(ps));
Ben Wagner51e15a62019-05-07 15:38:46 -0400170 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
Mike Reed2e6db182018-12-15 13:45:33 -0500171 y += font.getMetrics(nullptr);
bungeman41868fe2015-05-20 09:21:04 -0700172 }
173 }
174 canvas->translate(0, SkIntToScalar(360));
Mike Reed2e6db182018-12-15 13:45:33 -0500175 font.setSubpixel(true);
Ben Wagnerf460eee2019-04-18 16:37:45 -0400176 font.setLinearMetrics(true);
Ben Wagner54aa8842019-08-27 16:20:39 -0400177 font.setBaselineSnap(false);
bungeman41868fe2015-05-20 09:21:04 -0700178 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700179 return DrawResult::kOk;
bungeman41868fe2015-05-20 09:21:04 -0700180 }
bungeman41868fe2015-05-20 09:21:04 -0700181};
182
183//////////////////////////////////////////////////////////////////////////////
184
Hal Canarye964c182019-01-23 10:22:01 -0500185DEF_GM( return new FontScalerDistortableGM; )
bungeman41868fe2015-05-20 09:21:04 -0700186
John Stilesa6841be2020-08-06 14:11:56 -0400187} // namespace skiagm