blob: 64bb9bc44d4cbb082b1e68c3420bbecd1f9905d7 [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
Ben Wagner145dbcd2016-11-03 14:40:50 -040038 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
bungeman41868fe2015-05-20 09:21:04 -070039 if (!distortable) {
40 return;
41 }
42 const char* text = "abc";
43 const size_t textLen = strlen(text);
44
45 for (int j = 0; j < 2; ++j) {
46 for (int i = 0; i < 5; ++i) {
47 SkScalar x = SkIntToScalar(10);
48 SkScalar y = SkIntToScalar(20);
49
bungemanf6c71072016-01-21 14:17:47 -080050 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
51 SkScalar styleValue = SkDoubleToScalar(0.5 + (5*j + i) * ((2.0 - 0.5) / (2 * 5)));
Ben Wagnerfc497342017-02-24 11:15:26 -050052 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
53 SkFontArguments::VariationPosition position =
54 { coordinates, SK_ARRAY_COUNT(coordinates) };
Mike Reed59227392017-09-26 09:46:08 -040055 paint.setTypeface(sk_sp<SkTypeface>(fontMgr->makeFromStream(
56 distortable->duplicate(),
Ben Wagnerfc497342017-02-24 11:15:26 -050057 SkFontArguments().setVariationDesignPosition(position))));
bungeman41868fe2015-05-20 09:21:04 -070058
59 SkAutoCanvasRestore acr(canvas, true);
60 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
bungeman7438bfc2016-07-12 15:01:19 -070061 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
bungeman41868fe2015-05-20 09:21:04 -070062
63 {
64 SkPaint p;
65 p.setAntiAlias(true);
66 SkRect r;
67 r.set(x - SkIntToScalar(3), SkIntToScalar(15),
68 x - SkIntToScalar(1), SkIntToScalar(280));
69 canvas->drawRect(r, p);
70 }
71
72 for (int ps = 6; ps <= 22; ps++) {
73 paint.setTextSize(SkIntToScalar(ps));
74 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070075 y += paint.getFontMetrics(nullptr);
bungeman41868fe2015-05-20 09:21:04 -070076 }
77 }
78 canvas->translate(0, SkIntToScalar(360));
79 paint.setSubpixelText(true);
80 }
81 }
82
83private:
84 typedef GM INHERITED;
85};
86
87//////////////////////////////////////////////////////////////////////////////
88
89static GM* MyFactory(void*) { return new FontScalerDistortableGM; }
90static GMRegistry reg(MyFactory);
91
92}