joshualitt | e46cf96 | 2015-08-26 11:19:55 -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 | */ |
| 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/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkScalar.h" |
| 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "include/core/SkSurfaceProps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkTextBlob.h" |
| 26 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/utils/SkRandom.h" |
| 29 | #include "src/core/SkBlurMask.h" |
| 30 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include "tools/ToolUtils.h" |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 35 | namespace skiagm { |
| 36 | class TextBlobMixedSizes : public GM { |
| 37 | public: |
| 38 | // This gm tests that textblobs of mixed sizes with a large glyph will render properly |
| 39 | TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {} |
| 40 | |
| 41 | protected: |
| 42 | void onOnceBeforeDraw() override { |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 43 | SkTextBlobBuilder builder; |
| 44 | |
| 45 | // make textblob. To stress distance fields, we choose sizes appropriately |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 46 | SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262); |
| 47 | font.setSubpixel(true); |
| 48 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 49 | |
joshualitt | 33e91f1 | 2015-09-18 11:28:59 -0700 | [diff] [blame] | 50 | const char* text = "Skia"; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 51 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 52 | ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 53 | |
| 54 | // large |
| 55 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 56 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 57 | SkScalar yOffset = bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 58 | font.setSize(162); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 59 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 60 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 61 | |
| 62 | // Medium |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 63 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 64 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 65 | font.setSize(72); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 66 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 67 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 68 | |
| 69 | // Small |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 70 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 71 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 72 | font.setSize(32); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 73 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 74 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 75 | |
| 76 | // micro (will fall out of distance field text even if distance field text is enabled) |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 77 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 78 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 79 | font.setSize(14); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 80 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 81 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 82 | |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 83 | // Zero size. |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 84 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 85 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 86 | font.setSize(0); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 87 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 88 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 89 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 90 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 91 | fBlob = builder.make(); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame] | 95 | return SkStringPrintf("textblobmixedsizes%s", |
Hal Canary | 58822d6 | 2017-11-14 12:08:34 -0500 | [diff] [blame] | 96 | fUseDFT ? "_df" : ""); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | SkISize onISize() override { |
| 100 | return SkISize::Make(kWidth, kHeight); |
| 101 | } |
| 102 | |
| 103 | void onDraw(SkCanvas* inputCanvas) override { |
| 104 | SkCanvas* canvas = inputCanvas; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 105 | sk_sp<SkSurface> surface; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 106 | if (fUseDFT) { |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 107 | // Create a new Canvas to enable DFT |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 108 | auto ctx = inputCanvas->recordingContext(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 109 | SkISize size = onISize(); |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 110 | sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 111 | SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), |
| 112 | kPremul_SkAlphaType, colorSpace); |
Ben Wagner | ae4bb98 | 2020-09-24 14:49:00 -0400 | [diff] [blame] | 113 | SkSurfaceProps inputProps; |
| 114 | inputCanvas->getProps(&inputProps); |
| 115 | SkSurfaceProps props( |
| 116 | SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(), |
| 117 | inputProps.pixelGeometry()); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 118 | surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 119 | canvas = surface ? surface->getCanvas() : inputCanvas; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 120 | // init our new canvas with the old canvas's matrix |
| 121 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 122 | } |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 123 | canvas->drawColor(SK_ColorWHITE); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 124 | |
| 125 | SkRect bounds = fBlob->bounds(); |
| 126 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 127 | const int kPadX = SkScalarFloorToInt(bounds.width() / 3); |
| 128 | const int kPadY = SkScalarFloorToInt(bounds.height() / 3); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 129 | |
| 130 | int rowCount = 0; |
| 131 | canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); |
| 132 | canvas->save(); |
| 133 | SkRandom random; |
| 134 | |
| 135 | SkPaint paint; |
| 136 | if (!fUseDFT) { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 137 | paint.setColor(SK_ColorWHITE); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 138 | } |
| 139 | paint.setAntiAlias(false); |
| 140 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 141 | const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8)); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 142 | |
| 143 | // setup blur paint |
| 144 | SkPaint blurPaint(paint); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 145 | blurPaint.setColor(SK_ColorBLACK); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 146 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 147 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 148 | for (int i = 0; i < 4; i++) { |
| 149 | canvas->save(); |
| 150 | switch (i % 2) { |
| 151 | case 0: |
| 152 | canvas->rotate(random.nextF() * 45.f); |
| 153 | break; |
| 154 | case 1: |
| 155 | canvas->rotate(-random.nextF() * 45.f); |
| 156 | break; |
| 157 | } |
| 158 | if (!fUseDFT) { |
| 159 | canvas->drawTextBlob(fBlob, 0, 0, blurPaint); |
| 160 | } |
| 161 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 162 | canvas->restore(); |
| 163 | canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0); |
| 164 | ++rowCount; |
| 165 | if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) { |
| 166 | canvas->restore(); |
| 167 | canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY); |
| 168 | canvas->save(); |
| 169 | rowCount = 0; |
| 170 | } |
| 171 | } |
| 172 | canvas->restore(); |
| 173 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 174 | // render offscreen buffer |
| 175 | if (surface) { |
| 176 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 177 | // since we prepended this matrix already, we blit using identity |
| 178 | inputCanvas->resetMatrix(); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 179 | inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 180 | } |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 184 | sk_sp<SkTextBlob> fBlob; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 185 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 186 | static constexpr int kWidth = 2100; |
| 187 | static constexpr int kHeight = 1900; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 188 | |
| 189 | bool fUseDFT; |
| 190 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 191 | using INHERITED = GM; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////// |
| 195 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 196 | DEF_GM( return new TextBlobMixedSizes(false); ) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 197 | DEF_GM( return new TextBlobMixedSizes(true); ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 198 | } // namespace skiagm |