blob: f83365bb3e59821dac80a97f0e896a2452dfdc95 [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
22 virtual ~FontScalerDistortableGM() { }
23
24protected:
25
26 SkString onShortName() override {
27 return SkString("fontscalerdistortable");
28 }
29
30 SkISize onISize() override {
31 return SkISize::Make(550, 700);
32 }
33
34 static void rotate_about(SkCanvas* canvas, SkScalar degrees, SkScalar px, SkScalar py) {
35 canvas->translate(px, py);
36 canvas->rotate(degrees);
37 canvas->translate(-px, -py);
38 }
39
40 void onDraw(SkCanvas* canvas) override {
41 SkPaint paint;
42 paint.setAntiAlias(true);
43 paint.setLCDRenderText(true);
bungemanf6c71072016-01-21 14:17:47 -080044 SkAutoTUnref<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
bungeman41868fe2015-05-20 09:21:04 -070045
46 SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
47 if (!distortable) {
48 return;
49 }
50 const char* text = "abc";
51 const size_t textLen = strlen(text);
52
53 for (int j = 0; j < 2; ++j) {
54 for (int i = 0; i < 5; ++i) {
55 SkScalar x = SkIntToScalar(10);
56 SkScalar y = SkIntToScalar(20);
57
bungemanf6c71072016-01-21 14:17:47 -080058 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
59 SkScalar styleValue = SkDoubleToScalar(0.5 + (5*j + i) * ((2.0 - 0.5) / (2 * 5)));
60 SkFontMgr::FontParameters::Axis axes[] = { { tag, styleValue } };
61 SkAutoTUnref<SkTypeface> typeface(fontMgr->createFromStream(
62 distortable->duplicate(), SkFontMgr::FontParameters().setAxes(axes, 1)));
bungeman41868fe2015-05-20 09:21:04 -070063 paint.setTypeface(typeface);
64
65 SkAutoCanvasRestore acr(canvas, true);
66 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
67 rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
68
69 {
70 SkPaint p;
71 p.setAntiAlias(true);
72 SkRect r;
73 r.set(x - SkIntToScalar(3), SkIntToScalar(15),
74 x - SkIntToScalar(1), SkIntToScalar(280));
75 canvas->drawRect(r, p);
76 }
77
78 for (int ps = 6; ps <= 22; ps++) {
79 paint.setTextSize(SkIntToScalar(ps));
80 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070081 y += paint.getFontMetrics(nullptr);
bungeman41868fe2015-05-20 09:21:04 -070082 }
83 }
84 canvas->translate(0, SkIntToScalar(360));
85 paint.setSubpixelText(true);
86 }
87 }
88
89private:
90 typedef GM INHERITED;
91};
92
93//////////////////////////////////////////////////////////////////////////////
94
95static GM* MyFactory(void*) { return new FontScalerDistortableGM; }
96static GMRegistry reg(MyFactory);
97
98}