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 | |
| 22 | virtual ~FontScalerDistortableGM() { } |
| 23 | |
| 24 | protected: |
| 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); |
| 44 | |
| 45 | SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf")); |
| 46 | if (!distortable) { |
| 47 | return; |
| 48 | } |
| 49 | const char* text = "abc"; |
| 50 | const size_t textLen = strlen(text); |
| 51 | |
| 52 | for (int j = 0; j < 2; ++j) { |
| 53 | for (int i = 0; i < 5; ++i) { |
| 54 | SkScalar x = SkIntToScalar(10); |
| 55 | SkScalar y = SkIntToScalar(20); |
| 56 | |
| 57 | SkFixed axis = SkDoubleToFixed(0.5 + (5*j + i) * ((2.0 - 0.5) / (2 * 5))); |
| 58 | SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData( |
| 59 | new SkFontData(distortable->duplicate(), 0, &axis, 1))); |
| 60 | paint.setTypeface(typeface); |
| 61 | |
| 62 | SkAutoCanvasRestore acr(canvas, true); |
| 63 | canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20)); |
| 64 | rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10); |
| 65 | |
| 66 | { |
| 67 | SkPaint p; |
| 68 | p.setAntiAlias(true); |
| 69 | SkRect r; |
| 70 | r.set(x - SkIntToScalar(3), SkIntToScalar(15), |
| 71 | x - SkIntToScalar(1), SkIntToScalar(280)); |
| 72 | canvas->drawRect(r, p); |
| 73 | } |
| 74 | |
| 75 | for (int ps = 6; ps <= 22; ps++) { |
| 76 | paint.setTextSize(SkIntToScalar(ps)); |
| 77 | canvas->drawText(text, textLen, x, y, paint); |
| 78 | y += paint.getFontMetrics(NULL); |
| 79 | } |
| 80 | } |
| 81 | canvas->translate(0, SkIntToScalar(360)); |
| 82 | paint.setSubpixelText(true); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | typedef GM INHERITED; |
| 88 | }; |
| 89 | |
| 90 | ////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | static GM* MyFactory(void*) { return new FontScalerDistortableGM; } |
| 93 | static GMRegistry reg(MyFactory); |
| 94 | |
| 95 | } |