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 <SkFont.h> 8 #include "gm.h" 9 #include "Resources.h" 10 #include "SkFixed.h" 11 #include "SkFontDescriptor.h" 12 #include "SkFontMgr.h" 13 #include "SkTypeface.h" 14 15 namespace skiagm { 16 17 class FontScalerDistortableGM : public GM { 18 public: FontScalerDistortableGM()19 FontScalerDistortableGM() { 20 this->setBGColor(0xFFFFFFFF); 21 } 22 23 protected: 24 onShortName()25 SkString onShortName() override { 26 return SkString("fontscalerdistortable"); 27 } 28 onISize()29 SkISize onISize() override { 30 return SkISize::Make(550, 700); 31 } 32 onDraw(SkCanvas * canvas)33 void onDraw(SkCanvas* canvas) override { 34 SkPaint paint; 35 paint.setAntiAlias(true); 36 SkFont font; 37 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 38 sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault()); 39 40 std::unique_ptr<SkStreamAsset> distortableStream(GetResourceAsStream("fonts/Distortable.ttf")); 41 sk_sp<SkTypeface> distortable(MakeResourceAsTypeface("fonts/Distortable.ttf")); 42 43 if (!distortableStream) { 44 return; 45 } 46 const char* text = "abc"; 47 const size_t textLen = strlen(text); 48 49 for (int j = 0; j < 2; ++j) { 50 for (int i = 0; i < 5; ++i) { 51 SkScalar x = SkIntToScalar(10); 52 SkScalar y = SkIntToScalar(20); 53 54 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t'); 55 SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5))); 56 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}}; 57 SkFontArguments::VariationPosition position = 58 { coordinates, SK_ARRAY_COUNT(coordinates) }; 59 if (j == 0 && distortable) { 60 font.setTypeface(sk_sp<SkTypeface>( 61 distortable->makeClone( 62 SkFontArguments().setVariationDesignPosition(position)))); 63 } else { 64 font.setTypeface(sk_sp<SkTypeface>(fontMgr->makeFromStream( 65 distortableStream->duplicate(), 66 SkFontArguments().setVariationDesignPosition(position)))); 67 } 68 69 SkAutoCanvasRestore acr(canvas, true); 70 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20)); 71 canvas->rotate(SkIntToScalar(i * 5), x, y * 10); 72 73 { 74 SkPaint p; 75 p.setAntiAlias(true); 76 SkRect r; 77 r.set(x - SkIntToScalar(3), SkIntToScalar(15), 78 x - SkIntToScalar(1), SkIntToScalar(280)); 79 canvas->drawRect(r, p); 80 } 81 82 for (int ps = 6; ps <= 22; ps++) { 83 font.setSize(SkIntToScalar(ps)); 84 canvas->drawSimpleText(text, textLen, kUTF8_SkTextEncoding, x, y, font, paint); 85 y += font.getMetrics(nullptr); 86 } 87 } 88 canvas->translate(0, SkIntToScalar(360)); 89 font.setSubpixel(true); 90 } 91 } 92 93 private: 94 typedef GM INHERITED; 95 }; 96 97 ////////////////////////////////////////////////////////////////////////////// 98 99 DEF_GM( return new FontScalerDistortableGM; ) 100 101 } 102