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