blob: 5778ce020068a8e56ba24f60708fc05a5b5f2fdc [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 */
7#include "gm.h"
8#include "Resources.h"
9#include "SkFixed.h"
10#include "SkFontDescriptor.h"
11#include "SkFontMgr.h"
12#include "SkTypeface.h"
13
14namespace skiagm {
15
16class FontScalerDistortableGM : public GM {
17public:
18 FontScalerDistortableGM() {
19 this->setBGColor(0xFFFFFFFF);
20 }
21
bungeman41868fe2015-05-20 09:21:04 -070022protected:
23
24 SkString onShortName() override {
25 return SkString("fontscalerdistortable");
26 }
27
28 SkISize onISize() override {
29 return SkISize::Make(550, 700);
30 }
31
bungeman41868fe2015-05-20 09:21:04 -070032 void onDraw(SkCanvas* canvas) override {
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setLCDRenderText(true);
Hal Canarycefc4312016-11-04 16:26:16 -040036 sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
bungeman41868fe2015-05-20 09:21:04 -070037
Bruce Wangf0aacaf2018-06-07 17:19:14 -040038 std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf"));
39 sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf"));
40
41 if (!distortableStream) {
bungeman41868fe2015-05-20 09:21:04 -070042 return;
43 }
44 const char* text = "abc";
45 const size_t textLen = strlen(text);
46
47 for (int j = 0; j < 2; ++j) {
48 for (int i = 0; i < 5; ++i) {
49 SkScalar x = SkIntToScalar(10);
50 SkScalar y = SkIntToScalar(20);
51
bungemanf6c71072016-01-21 14:17:47 -080052 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
Bruce Wangf0aacaf2018-06-07 17:19:14 -040053 SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5)));
Ben Wagnerfc497342017-02-24 11:15:26 -050054 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
55 SkFontArguments::VariationPosition position =
56 { coordinates, SK_ARRAY_COUNT(coordinates) };
Bruce Wangf0aacaf2018-06-07 17:19:14 -040057 if (j == 0 && distortable) {
58 paint.setTypeface(sk_sp<SkTypeface>(
59 distortable->makeClone(
60 SkFontArguments().setVariationDesignPosition(position))));
61 } else {
62 paint.setTypeface(sk_sp<SkTypeface>(fontMgr->makeFromStream(
63 distortableStream->duplicate(),
Ben Wagnerfc497342017-02-24 11:15:26 -050064 SkFontArguments().setVariationDesignPosition(position))));
Bruce Wangf0aacaf2018-06-07 17:19:14 -040065 }
bungeman41868fe2015-05-20 09:21:04 -070066
67 SkAutoCanvasRestore acr(canvas, true);
68 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
bungeman7438bfc2016-07-12 15:01:19 -070069 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
bungeman41868fe2015-05-20 09:21:04 -070070
71 {
72 SkPaint p;
73 p.setAntiAlias(true);
74 SkRect r;
75 r.set(x - SkIntToScalar(3), SkIntToScalar(15),
76 x - SkIntToScalar(1), SkIntToScalar(280));
77 canvas->drawRect(r, p);
78 }
79
80 for (int ps = 6; ps <= 22; ps++) {
81 paint.setTextSize(SkIntToScalar(ps));
82 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070083 y += paint.getFontMetrics(nullptr);
bungeman41868fe2015-05-20 09:21:04 -070084 }
85 }
86 canvas->translate(0, SkIntToScalar(360));
87 paint.setSubpixelText(true);
88 }
89 }
90
91private:
92 typedef GM INHERITED;
93};
94
95//////////////////////////////////////////////////////////////////////////////
96
97static GM* MyFactory(void*) { return new FontScalerDistortableGM; }
98static GMRegistry reg(MyFactory);
99
100}