bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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: |
| 18 | FontScalerDistortableGM() { |
| 19 | this->setBGColor(0xFFFFFFFF); |
| 20 | } |
| 21 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 22 | protected: |
| 23 | |
| 24 | SkString onShortName() override { |
| 25 | return SkString("fontscalerdistortable"); |
| 26 | } |
| 27 | |
| 28 | SkISize onISize() override { |
| 29 | return SkISize::Make(550, 700); |
| 30 | } |
| 31 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 32 | void onDraw(SkCanvas* canvas) override { |
| 33 | SkPaint paint; |
| 34 | paint.setAntiAlias(true); |
| 35 | paint.setLCDRenderText(true); |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 36 | sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 37 | |
Bruce Wang | f0aacaf | 2018-06-07 17:19:14 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf")); |
| 39 | sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf")); |
| 40 | |
| 41 | if (!distortableStream) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 42 | 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 | |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 52 | SkFourByteTag tag = SkSetFourByteTag('w','g','h','t'); |
Bruce Wang | f0aacaf | 2018-06-07 17:19:14 -0400 | [diff] [blame] | 53 | SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5))); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 54 | SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}}; |
| 55 | SkFontArguments::VariationPosition position = |
| 56 | { coordinates, SK_ARRAY_COUNT(coordinates) }; |
Bruce Wang | f0aacaf | 2018-06-07 17:19:14 -0400 | [diff] [blame] | 57 | 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 Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 64 | SkFontArguments().setVariationDesignPosition(position)))); |
Bruce Wang | f0aacaf | 2018-06-07 17:19:14 -0400 | [diff] [blame] | 65 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 66 | |
| 67 | SkAutoCanvasRestore acr(canvas, true); |
| 68 | canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20)); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 69 | canvas->rotate(SkIntToScalar(i * 5), x, y * 10); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 70 | |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 83 | y += paint.getFontMetrics(nullptr); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | canvas->translate(0, SkIntToScalar(360)); |
| 87 | paint.setSubpixelText(true); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | typedef GM INHERITED; |
| 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | static GM* MyFactory(void*) { return new FontScalerDistortableGM; } |
| 98 | static GMRegistry reg(MyFactory); |
| 99 | |
| 100 | } |