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 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf")); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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 | |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 50 | SkFourByteTag tag = SkSetFourByteTag('w','g','h','t'); |
| 51 | 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] | 52 | SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}}; |
| 53 | SkFontArguments::VariationPosition position = |
| 54 | { coordinates, SK_ARRAY_COUNT(coordinates) }; |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 55 | paint.setTypeface(sk_sp<SkTypeface>(fontMgr->makeFromStream( |
| 56 | distortable->duplicate(), |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 57 | SkFontArguments().setVariationDesignPosition(position)))); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 58 | |
| 59 | SkAutoCanvasRestore acr(canvas, true); |
| 60 | canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20)); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 61 | canvas->rotate(SkIntToScalar(i * 5), x, y * 10); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | y += paint.getFontMetrics(nullptr); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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 | |
| 89 | static GM* MyFactory(void*) { return new FontScalerDistortableGM; } |
| 90 | static GMRegistry reg(MyFactory); |
| 91 | |
| 92 | } |