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 8 #include "gm/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkFont.h" 11 #include "include/core/SkFontArguments.h" 12 #include "include/core/SkFontMgr.h" 13 #include "include/core/SkFontTypes.h" 14 #include "include/core/SkPaint.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkRefCnt.h" 17 #include "include/core/SkScalar.h" 18 #include "include/core/SkSize.h" 19 #include "include/core/SkStream.h" 20 #include "include/core/SkString.h" 21 #include "include/core/SkTypeface.h" 22 #include "include/core/SkTypes.h" 23 #include "tools/Resources.h" 24 25 #include <string.h> 26 #include <memory> 27 #include <utility> 28 29 namespace skiagm { 30 31 class FontScalerDistortableGM : public GM { 32 public: FontScalerDistortableGM()33 FontScalerDistortableGM() { 34 this->setBGColor(0xFFFFFFFF); 35 } 36 37 private: 38 onShortName()39 SkString onShortName() override { 40 return SkString("fontscalerdistortable"); 41 } 42 onISize()43 SkISize onISize() override { 44 return SkISize::Make(550, 700); 45 } 46 47 inline static constexpr int rows = 2; 48 inline static constexpr int cols = 5; 49 sk_sp<SkTypeface> typeface[rows][cols]; onOnceBeforeDraw()50 void onOnceBeforeDraw() override { 51 sk_sp<SkFontMgr> fontMgr(SkFontMgr::RefDefault()); 52 53 constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t'); 54 //constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h'); 55 struct { 56 sk_sp<SkTypeface> distortable; 57 SkFourByteTag axisTag; 58 SkScalar axisMin; 59 SkScalar axisMax; 60 } info = { 61 MakeResourceAsTypeface("fonts/Distortable.ttf"), wght, 0.5f, 2.0f 62 //SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f 63 //SkTypeface::MakeFromName("Skia", SkFontStyle()), wdth, 0.62f, 1.3f 64 //SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f 65 //SkTypeface::MakeFromName(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f 66 }; 67 std::unique_ptr<SkStreamAsset> distortableStream( info.distortable 68 ? info.distortable->openStream(nullptr) 69 : nullptr); 70 for (int row = 0; row < rows; ++row) { 71 for (int col = 0; col < cols; ++col) { 72 SkScalar styleValue = SkScalarInterp(info.axisMin, info.axisMax, 73 SkScalar(row * cols + col) / (rows * cols)); 74 SkFontArguments::VariationPosition::Coordinate coordinates[] = { 75 {info.axisTag, styleValue}, 76 {info.axisTag, styleValue} 77 }; 78 SkFontArguments::VariationPosition position = { 79 coordinates, SK_ARRAY_COUNT(coordinates) 80 }; 81 typeface[row][col] = [&]() -> sk_sp<SkTypeface> { 82 if (row == 0 && info.distortable) { 83 return info.distortable->makeClone( 84 SkFontArguments().setVariationDesignPosition(position)); 85 } 86 if (distortableStream) { 87 return fontMgr->makeFromStream(distortableStream->duplicate(), 88 SkFontArguments().setVariationDesignPosition(position)); 89 } 90 return nullptr; 91 }(); 92 } 93 } 94 } 95 onDraw(SkCanvas * canvas,SkString * errorMsg)96 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 97 SkPaint paint; 98 paint.setAntiAlias(true); 99 SkFont font; 100 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 101 102 const char* text = "abc"; 103 const size_t textLen = strlen(text); 104 105 for (int row = 0; row < rows; ++row) { 106 for (int col = 0; col < cols; ++col) { 107 SkScalar x = SkIntToScalar(10); 108 SkScalar y = SkIntToScalar(20); 109 110 font.setTypeface(typeface[row][col] ? typeface[row][col] : nullptr); 111 112 SkAutoCanvasRestore acr(canvas, true); 113 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20)); 114 canvas->rotate(SkIntToScalar(col * 5), x, y * 10); 115 116 { 117 SkPaint p; 118 p.setAntiAlias(true); 119 SkRect r; 120 r.setLTRB(x - 3, 15, x - 1, 280); 121 canvas->drawRect(r, p); 122 } 123 124 for (int ps = 6; ps <= 22; ps++) { 125 font.setSize(SkIntToScalar(ps)); 126 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); 127 y += font.getMetrics(nullptr); 128 } 129 } 130 canvas->translate(0, SkIntToScalar(360)); 131 font.setSubpixel(true); 132 font.setLinearMetrics(true); 133 font.setBaselineSnap(false); 134 } 135 return DrawResult::kOk; 136 } 137 }; 138 139 ////////////////////////////////////////////////////////////////////////////// 140 141 DEF_GM( return new FontScalerDistortableGM; ) 142 143 } // namespace skiagm 144