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: 19 FontScalerDistortableGM() { 20 this->setBGColor(0xFFFFFFFF); 21 } 22 23 protected: 24 25 SkString onShortName() override { 26 return SkString("fontscalerdistortable"); 27 } 28 29 SkISize onISize() override { 30 return SkISize::Make(550, 700); 31 } 32 33 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) 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 *errorMsg = "No distortableStream"; 45 return DrawResult::kFail; 46 } 47 const char* text = "abc"; 48 const size_t textLen = strlen(text); 49 50 for (int j = 0; j < 2; ++j) { 51 for (int i = 0; i < 5; ++i) { 52 SkScalar x = SkIntToScalar(10); 53 SkScalar y = SkIntToScalar(20); 54 55 SkFourByteTag tag = SkSetFourByteTag('w','g','h','t'); 56 SkScalar styleValue = SkDoubleToScalar(0.5 + (5 * j + i) * ((2.0 - 0.5) / (2 * 5))); 57 SkFontArguments::VariationPosition::Coordinate coordinates[] = {{tag, styleValue}}; 58 SkFontArguments::VariationPosition position = 59 { coordinates, SK_ARRAY_COUNT(coordinates) }; 60 if (j == 0 && distortable) { 61 font.setTypeface(sk_sp<SkTypeface>( 62 distortable->makeClone( 63 SkFontArguments().setVariationDesignPosition(position)))); 64 } else { 65 font.setTypeface(sk_sp<SkTypeface>(fontMgr->makeFromStream( 66 distortableStream->duplicate(), 67 SkFontArguments().setVariationDesignPosition(position)))); 68 } 69 70 SkAutoCanvasRestore acr(canvas, true); 71 canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20)); 72 canvas->rotate(SkIntToScalar(i * 5), x, y * 10); 73 74 { 75 SkPaint p; 76 p.setAntiAlias(true); 77 SkRect r; 78 r.set(x - SkIntToScalar(3), SkIntToScalar(15), 79 x - SkIntToScalar(1), SkIntToScalar(280)); 80 canvas->drawRect(r, p); 81 } 82 83 for (int ps = 6; ps <= 22; ps++) { 84 font.setSize(SkIntToScalar(ps)); 85 canvas->drawSimpleText(text, textLen, kUTF8_SkTextEncoding, x, y, font, paint); 86 y += font.getMetrics(nullptr); 87 } 88 } 89 canvas->translate(0, SkIntToScalar(360)); 90 font.setSubpixel(true); 91 } 92 return DrawResult::kOk; 93 } 94 95 private: 96 typedef GM INHERITED; 97 }; 98 99 ////////////////////////////////////////////////////////////////////////////// 100 101 DEF_GM( return new FontScalerDistortableGM; ) 102 103 } 104