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 #include "tools/SkMetaData.h" 25 #include "tools/ToolUtils.h" 26 #include "tools/fonts/FontToolUtils.h" 27 28 #include <string.h> 29 #include <memory> 30 #include <utility> 31 32 namespace skiagm { 33 34 class FontScalerDistortableGM : public GM { 35 public: FontScalerDistortableGM()36 FontScalerDistortableGM() { 37 this->setBGColor(0xFFFFFFFF); 38 } 39 40 private: getName() const41 SkString getName() const override { return SkString("fontscalerdistortable"); } 42 getISize()43 SkISize getISize() override { return SkISize::Make(550, 700); } 44 45 bool fDirty = true; 46 bool fOverride = false; 47 48 ToolUtils::VariationSliders fVariationSliders; 49 onGetControls(SkMetaData * controls)50 bool onGetControls(SkMetaData* controls) override { 51 controls->setBool("Override", fOverride); 52 return fVariationSliders.writeControls(controls); 53 } 54 onSetControls(const SkMetaData & controls)55 void onSetControls(const SkMetaData& controls) override { 56 bool oldOverride = fOverride; 57 controls.findBool("Override", &fOverride); 58 if (fOverride != oldOverride) { 59 fDirty = true; 60 } 61 62 return fVariationSliders.readControls(controls, &fDirty); 63 } 64 65 struct Info { 66 sk_sp<SkTypeface> distortable; 67 SkFourByteTag axisTag; 68 SkScalar axisMin; 69 SkScalar axisMax; 70 } fInfo; 71 onOnceBeforeDraw()72 void onOnceBeforeDraw() override { 73 constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t'); 74 //constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h'); 75 fInfo = { 76 ToolUtils::CreateTypefaceFromResource("fonts/Distortable.ttf"), wght, 0.5f, 2.0f 77 //SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f 78 //ToolUtils::CreateTestTypeface("Skia", SkFontStyle()), wdth, 0.62f, 1.3f 79 //SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f 80 //ToolUtils::CreateTestTypeface(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f 81 }; 82 83 if (!fInfo.distortable) { 84 fInfo.distortable = ToolUtils::DefaultPortableTypeface(); 85 } 86 SkASSERT(fInfo.distortable); 87 fVariationSliders = ToolUtils::VariationSliders(fInfo.distortable.get()); 88 } 89 90 inline static constexpr int rows = 2; 91 inline static constexpr int cols = 5; 92 sk_sp<SkTypeface> typeface[rows][cols]; 93 updateTypefaces()94 void updateTypefaces() { 95 sk_sp<SkFontMgr> fontMgr = ToolUtils::TestFontMgr(); 96 97 std::unique_ptr<SkStreamAsset> distortableStream( fInfo.distortable 98 ? fInfo.distortable->openStream(nullptr) 99 : nullptr); 100 for (int row = 0; row < rows; ++row) { 101 for (int col = 0; col < cols; ++col) { 102 using Coordinate = SkFontArguments::VariationPosition::Coordinate; 103 SkFontArguments::VariationPosition position; 104 Coordinate coordinates[2]; 105 106 if (fOverride) { 107 SkSpan<const Coordinate> user_coordinates = fVariationSliders.getCoordinates(); 108 position = {user_coordinates.data(), static_cast<int>(user_coordinates.size())}; 109 110 } else { 111 const int coordinateCount = 2; 112 SkScalar styleValue = SkScalarInterp(fInfo.axisMin, fInfo.axisMax, 113 SkScalar(row*cols + col) / (rows*cols)); 114 coordinates[0] = {fInfo.axisTag, styleValue}; 115 coordinates[1] = {fInfo.axisTag, styleValue}; 116 position = {coordinates, static_cast<int>(coordinateCount)}; 117 } 118 119 typeface[row][col] = [&]() -> sk_sp<SkTypeface> { 120 if (row == 0 && fInfo.distortable) { 121 return fInfo.distortable->makeClone( 122 SkFontArguments().setVariationDesignPosition(position)); 123 } 124 if (distortableStream) { 125 return fontMgr->makeFromStream(distortableStream->duplicate(), 126 SkFontArguments().setVariationDesignPosition(position)); 127 } 128 return nullptr; 129 }(); 130 } 131 } 132 fDirty = false; 133 } 134 onDraw(SkCanvas * canvas,SkString * errorMsg)135 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 136 if (fDirty) { 137 this->updateTypefaces(); 138 } 139 140 SkPaint paint; 141 paint.setAntiAlias(true); 142 SkFont font; 143 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 144 145 const char* text = "abc"; 146 const size_t textLen = strlen(text); 147 148 for (int row = 0; row < rows; ++row) { 149 for (int col = 0; col < cols; ++col) { 150 SkScalar x = SkIntToScalar(10); 151 SkScalar y = SkIntToScalar(20); 152 153 font.setTypeface(typeface[row][col] ? typeface[row][col] : 154 ToolUtils::DefaultPortableTypeface()); 155 156 SkAutoCanvasRestore acr(canvas, true); 157 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20)); 158 canvas->rotate(SkIntToScalar(col * 5), x, y * 10); 159 160 { 161 SkPaint p; 162 p.setAntiAlias(true); 163 SkRect r; 164 r.setLTRB(x - 3, 15, x - 1, 280); 165 canvas->drawRect(r, p); 166 } 167 168 for (int ps = 6; ps <= 22; ps++) { 169 font.setSize(SkIntToScalar(ps)); 170 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); 171 y += font.getMetrics(nullptr); 172 } 173 } 174 canvas->translate(0, SkIntToScalar(360)); 175 font.setSubpixel(true); 176 font.setLinearMetrics(true); 177 font.setBaselineSnap(false); 178 } 179 return DrawResult::kOk; 180 } 181 }; 182 183 ////////////////////////////////////////////////////////////////////////////// 184 185 DEF_GM( return new FontScalerDistortableGM; ) 186 187 } // namespace skiagm 188