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 SkAutoTUnref<SkFontMgr> fontMgr(SkFontMgr::RefDefault()); 45 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 58 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))); 63 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); 81 y += paint.getFontMetrics(nullptr); 82 } 83 } 84 canvas->translate(0, SkIntToScalar(360)); 85 paint.setSubpixelText(true); 86 } 87 } 88 89 private: 90 typedef GM INHERITED; 91 }; 92 93 ////////////////////////////////////////////////////////////////////////////// 94 95 static GM* MyFactory(void*) { return new FontScalerDistortableGM; } 96 static GMRegistry reg(MyFactory); 97 98 } 99