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