bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 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 | */ |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkFont.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkFontArguments.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFontMgr.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/Resources.h" |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 24 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <memory> |
| 27 | #include <utility> |
| 28 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 29 | namespace skiagm { |
| 30 | |
| 31 | class FontScalerDistortableGM : public GM { |
| 32 | public: |
| 33 | FontScalerDistortableGM() { |
| 34 | this->setBGColor(0xFFFFFFFF); |
| 35 | } |
| 36 | |
Ben Wagner | fbac83a | 2019-08-08 18:33:05 -0400 | [diff] [blame] | 37 | private: |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 38 | |
| 39 | SkString onShortName() override { |
| 40 | return SkString("fontscalerdistortable"); |
| 41 | } |
| 42 | |
| 43 | SkISize onISize() override { |
| 44 | return SkISize::Make(550, 700); |
| 45 | } |
| 46 | |
Ben Wagner | 782a957 | 2020-01-17 15:55:14 -0500 | [diff] [blame] | 47 | static constexpr int rows = 2; |
| 48 | static constexpr int cols = 5; |
| 49 | sk_sp<SkTypeface> typeface[rows][cols]; |
| 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 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 96 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 97 | SkPaint paint; |
| 98 | paint.setAntiAlias(true); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 99 | SkFont font; |
| 100 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 101 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 102 | const char* text = "abc"; |
| 103 | const size_t textLen = strlen(text); |
| 104 | |
Ben Wagner | fbac83a | 2019-08-08 18:33:05 -0400 | [diff] [blame] | 105 | for (int row = 0; row < rows; ++row) { |
| 106 | for (int col = 0; col < cols; ++col) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 107 | SkScalar x = SkIntToScalar(10); |
| 108 | SkScalar y = SkIntToScalar(20); |
| 109 | |
Ben Wagner | 782a957 | 2020-01-17 15:55:14 -0500 | [diff] [blame] | 110 | font.setTypeface(typeface[row][col] ? typeface[row][col] : nullptr); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 111 | |
| 112 | SkAutoCanvasRestore acr(canvas, true); |
Ben Wagner | fbac83a | 2019-08-08 18:33:05 -0400 | [diff] [blame] | 113 | canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20)); |
| 114 | canvas->rotate(SkIntToScalar(col * 5), x, y * 10); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 115 | |
| 116 | { |
| 117 | SkPaint p; |
| 118 | p.setAntiAlias(true); |
| 119 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 120 | r.setLTRB(x - 3, 15, x - 1, 280); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 121 | canvas->drawRect(r, p); |
| 122 | } |
| 123 | |
| 124 | for (int ps = 6; ps <= 22; ps++) { |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 125 | font.setSize(SkIntToScalar(ps)); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 126 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 127 | y += font.getMetrics(nullptr); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | canvas->translate(0, SkIntToScalar(360)); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 131 | font.setSubpixel(true); |
Ben Wagner | f460eee | 2019-04-18 16:37:45 -0400 | [diff] [blame] | 132 | font.setLinearMetrics(true); |
Ben Wagner | 54aa884 | 2019-08-27 16:20:39 -0400 | [diff] [blame] | 133 | font.setBaselineSnap(false); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 134 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 135 | return DrawResult::kOk; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 136 | } |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 141 | DEF_GM( return new FontScalerDistortableGM; ) |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 142 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 143 | } // namespace skiagm |