blob: 6d6c16cf2a6203ddc6af6c7996f13e798bef5df0 [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"
bungeman41868fe2015-05-20 09:21:04 -070024
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include <string.h>
26#include <memory>
27#include <utility>
28
bungeman41868fe2015-05-20 09:21:04 -070029namespace skiagm {
30
31class FontScalerDistortableGM : public GM {
32public:
33 FontScalerDistortableGM() {
34 this->setBGColor(0xFFFFFFFF);
35 }
36
Ben Wagnerfbac83a2019-08-08 18:33:05 -040037private:
bungeman41868fe2015-05-20 09:21:04 -070038
39 SkString onShortName() override {
40 return SkString("fontscalerdistortable");
41 }
42
43 SkISize onISize() override {
44 return SkISize::Make(550, 700);
45 }
46
Ben Wagner782a9572020-01-17 15:55:14 -050047 static constexpr int rows = 2;
48 static constexpr int cols = 5;
49 sk_sp<SkTypeface> typeface[rows][cols];
50 void onOnceBeforeDraw() override {
51 sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
52
53 constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t');
54 //constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h');
55 struct {
56 sk_sp<SkTypeface> distortable;
57 SkFourByteTag axisTag;
58 SkScalar axisMin;
59 SkScalar axisMax;
60 } info = {
61 MakeResourceAsTypeface("fonts/Distortable.ttf"), wght, 0.5f, 2.0f
62 //SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f
63 //SkTypeface::MakeFromName("Skia", SkFontStyle()), wdth, 0.62f, 1.3f
64 //SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f
65 //SkTypeface::MakeFromName(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f
66 };
67 std::unique_ptr<SkStreamAsset> distortableStream( info.distortable
68 ? info.distortable->openStream(nullptr)
69 : nullptr);
70 for (int row = 0; row < rows; ++row) {
71 for (int col = 0; col < cols; ++col) {
72 SkScalar styleValue = SkScalarInterp(info.axisMin, info.axisMax,
73 SkScalar(row * cols + col) / (rows * cols));
74 SkFontArguments::VariationPosition::Coordinate coordinates[] = {
75 {info.axisTag, styleValue},
76 {info.axisTag, styleValue}
77 };
78 SkFontArguments::VariationPosition position = {
79 coordinates, SK_ARRAY_COUNT(coordinates)
80 };
81 typeface[row][col] = [&]() -> sk_sp<SkTypeface> {
82 if (row == 0 && info.distortable) {
83 return info.distortable->makeClone(
84 SkFontArguments().setVariationDesignPosition(position));
85 }
86 if (distortableStream) {
87 return fontMgr->makeFromStream(distortableStream->duplicate(),
88 SkFontArguments().setVariationDesignPosition(position));
89 }
90 return nullptr;
91 }();
92 }
93 }
94 }
95
Chris Dalton50e24d72019-02-07 16:20:09 -070096 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
bungeman41868fe2015-05-20 09:21:04 -070097 SkPaint paint;
98 paint.setAntiAlias(true);
Mike Reed2e6db182018-12-15 13:45:33 -050099 SkFont font;
100 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
bungeman41868fe2015-05-20 09:21:04 -0700101
bungeman41868fe2015-05-20 09:21:04 -0700102 const char* text = "abc";
103 const size_t textLen = strlen(text);
104
Ben Wagnerfbac83a2019-08-08 18:33:05 -0400105 for (int row = 0; row < rows; ++row) {
106 for (int col = 0; col < cols; ++col) {
bungeman41868fe2015-05-20 09:21:04 -0700107 SkScalar x = SkIntToScalar(10);
108 SkScalar y = SkIntToScalar(20);
109
Ben Wagner782a9572020-01-17 15:55:14 -0500110 font.setTypeface(typeface[row][col] ? typeface[row][col] : nullptr);
bungeman41868fe2015-05-20 09:21:04 -0700111
112 SkAutoCanvasRestore acr(canvas, true);
Ben Wagnerfbac83a2019-08-08 18:33:05 -0400113 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20));
114 canvas->rotate(SkIntToScalar(col * 5), x, y * 10);
bungeman41868fe2015-05-20 09:21:04 -0700115
116 {
117 SkPaint p;
118 p.setAntiAlias(true);
119 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400120 r.setLTRB(x - 3, 15, x - 1, 280);
bungeman41868fe2015-05-20 09:21:04 -0700121 canvas->drawRect(r, p);
122 }
123
124 for (int ps = 6; ps <= 22; ps++) {
Mike Reed2e6db182018-12-15 13:45:33 -0500125 font.setSize(SkIntToScalar(ps));
Ben Wagner51e15a62019-05-07 15:38:46 -0400126 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
Mike Reed2e6db182018-12-15 13:45:33 -0500127 y += font.getMetrics(nullptr);
bungeman41868fe2015-05-20 09:21:04 -0700128 }
129 }
130 canvas->translate(0, SkIntToScalar(360));
Mike Reed2e6db182018-12-15 13:45:33 -0500131 font.setSubpixel(true);
Ben Wagnerf460eee2019-04-18 16:37:45 -0400132 font.setLinearMetrics(true);
Ben Wagner54aa8842019-08-27 16:20:39 -0400133 font.setBaselineSnap(false);
bungeman41868fe2015-05-20 09:21:04 -0700134 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700135 return DrawResult::kOk;
bungeman41868fe2015-05-20 09:21:04 -0700136 }
bungeman41868fe2015-05-20 09:21:04 -0700137};
138
139//////////////////////////////////////////////////////////////////////////////
140
Hal Canarye964c182019-01-23 10:22:01 -0500141DEF_GM( return new FontScalerDistortableGM; )
bungeman41868fe2015-05-20 09:21:04 -0700142
143}