1 /*
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
14 namespace skiagm {
15
16 class FontScalerDistortableGM : public GM {
17 public:
FontScalerDistortableGM()18 FontScalerDistortableGM() {
19 this->setBGColor(0xFFFFFFFF);
20 }
21
22 protected:
23
onShortName()24 SkString onShortName() override {
25 return SkString("fontscalerdistortable");
26 }
27
onISize()28 SkISize onISize() override {
29 return SkISize::Make(550, 700);
30 }
31
onDraw(SkCanvas * canvas)32 void onDraw(SkCanvas* canvas) override {
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setLCDRenderText(true);
36 sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
37
38 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
39 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
50 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
51 SkScalar styleValue = SkDoubleToScalar(0.5 + (5*j + i) * ((2.0 - 0.5) / (2 * 5)));
52 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}};
53 SkFontArguments::VariationPosition position =
54 { coordinates, SK_ARRAY_COUNT(coordinates) };
55 paint.setTypeface(sk_sp<SkTypeface>(fontMgr->createFromStream(
56 distortable->duplicate(),
57 SkFontArguments().setVariationDesignPosition(position))));
58
59 SkAutoCanvasRestore acr(canvas, true);
60 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
61 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
62
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);
75 y += paint.getFontMetrics(nullptr);
76 }
77 }
78 canvas->translate(0, SkIntToScalar(360));
79 paint.setSubpixelText(true);
80 }
81 }
82
83 private:
84 typedef GM INHERITED;
85 };
86
87 //////////////////////////////////////////////////////////////////////////////
88
MyFactory(void *)89 static GM* MyFactory(void*) { return new FontScalerDistortableGM; }
90 static GMRegistry reg(MyFactory);
91
92 }
93