blob: 4354607e850cb9a7d6d1b460652d32c4c5b4e4ed [file] [log] [blame]
joshualitte46cf962015-08-26 11:19:55 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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 Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkMaskFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#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 Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkSurfaceProps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkTextBlob.h"
26#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/utils/SkRandom.h"
29#include "src/core/SkBlurMask.h"
30#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "tools/ToolUtils.h"
32
33#include <string.h>
34
joshualitte46cf962015-08-26 11:19:55 -070035namespace skiagm {
36class TextBlobMixedSizes : public GM {
37public:
38 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
39 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
40
41protected:
42 void onOnceBeforeDraw() override {
joshualitte46cf962015-08-26 11:19:55 -070043 SkTextBlobBuilder builder;
44
45 // make textblob. To stress distance fields, we choose sizes appropriately
Mike Reed28bd8822018-12-22 22:29:45 -050046 SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262);
47 font.setSubpixel(true);
48 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
joshualitte46cf962015-08-26 11:19:55 -070049
joshualitt33e91f12015-09-18 11:28:59 -070050 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070051
Mike Kleinea3f0142019-03-20 11:12:10 -050052 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
joshualitte46cf962015-08-26 11:19:55 -070053
54 // large
55 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040056 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070057 SkScalar yOffset = bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050058 font.setSize(162);
joshualitte46cf962015-08-26 11:19:55 -070059
Mike Kleinea3f0142019-03-20 11:12:10 -050060 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070061
62 // Medium
Ben Wagner51e15a62019-05-07 15:38:46 -040063 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070064 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050065 font.setSize(72);
joshualitte46cf962015-08-26 11:19:55 -070066
Mike Kleinea3f0142019-03-20 11:12:10 -050067 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070068
69 // Small
Ben Wagner51e15a62019-05-07 15:38:46 -040070 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070071 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050072 font.setSize(32);
joshualitte46cf962015-08-26 11:19:55 -070073
Mike Kleinea3f0142019-03-20 11:12:10 -050074 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070075
76 // micro (will fall out of distance field text even if distance field text is enabled)
Ben Wagner51e15a62019-05-07 15:38:46 -040077 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070078 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050079 font.setSize(14);
joshualitte46cf962015-08-26 11:19:55 -070080
Mike Kleinea3f0142019-03-20 11:12:10 -050081 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070082
kkinnunen68c63b32016-03-04 00:12:33 -080083 // Zero size.
Ben Wagner51e15a62019-05-07 15:38:46 -040084 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
kkinnunen68c63b32016-03-04 00:12:33 -080085 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050086 font.setSize(0);
kkinnunen68c63b32016-03-04 00:12:33 -080087
Mike Kleinea3f0142019-03-20 11:12:10 -050088 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
kkinnunen68c63b32016-03-04 00:12:33 -080089
joshualitte46cf962015-08-26 11:19:55 -070090 // build
fmalita37283c22016-09-13 10:00:23 -070091 fBlob = builder.make();
joshualitte46cf962015-08-26 11:19:55 -070092 }
93
94 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060095 return SkStringPrintf("textblobmixedsizes%s",
Hal Canary58822d62017-11-14 12:08:34 -050096 fUseDFT ? "_df" : "");
joshualitte46cf962015-08-26 11:19:55 -070097 }
98
99 SkISize onISize() override {
100 return SkISize::Make(kWidth, kHeight);
101 }
102
103 void onDraw(SkCanvas* inputCanvas) override {
104 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -0700105 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700106 if (fUseDFT) {
joshualitte46cf962015-08-26 11:19:55 -0700107 // Create a new Canvas to enable DFT
Robert Phillips16bf7d32020-07-07 10:20:27 -0400108 auto ctx = inputCanvas->recordingContext();
brianosman52ede1d2016-06-20 08:25:02 -0700109 SkISize size = onISize();
Mike Reed693fdbd2017-01-12 10:13:40 -0500110 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
brianosman52ede1d2016-06-20 08:25:02 -0700111 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
112 kPremul_SkAlphaType, colorSpace);
Ben Wagnerae4bb982020-09-24 14:49:00 -0400113 SkSurfaceProps inputProps;
114 inputCanvas->getProps(&inputProps);
115 SkSurfaceProps props(
116 SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(),
117 inputProps.pixelGeometry());
reede8f30622016-03-23 18:59:25 -0700118 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
John Stilesa008b0f2020-08-16 08:48:02 -0400119 canvas = surface ? surface->getCanvas() : inputCanvas;
joshualitte46cf962015-08-26 11:19:55 -0700120 // init our new canvas with the old canvas's matrix
121 canvas->setMatrix(inputCanvas->getTotalMatrix());
joshualitte46cf962015-08-26 11:19:55 -0700122 }
Mike Kleind46dce32018-08-16 10:17:03 -0400123 canvas->drawColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700124
125 SkRect bounds = fBlob->bounds();
126
mtkleindbfd7ab2016-09-01 11:24:54 -0700127 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
128 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700129
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 Kleind46dce32018-08-16 10:17:03 -0400137 paint.setColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700138 }
139 paint.setAntiAlias(false);
140
mtkleindbfd7ab2016-09-01 11:24:54 -0700141 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700142
143 // setup blur paint
144 SkPaint blurPaint(paint);
Mike Kleind46dce32018-08-16 10:17:03 -0400145 blurPaint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400146 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700147
joshualitte46cf962015-08-26 11:19:55 -0700148 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
joshualitte46cf962015-08-26 11:19:55 -0700174 // 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 Reed8d29ab62021-01-23 18:10:39 -0500179 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
joshualitte46cf962015-08-26 11:19:55 -0700180 }
joshualitte46cf962015-08-26 11:19:55 -0700181 }
182
183private:
fmalita37283c22016-09-13 10:00:23 -0700184 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700185
mtkleindbfd7ab2016-09-01 11:24:54 -0700186 static constexpr int kWidth = 2100;
187 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700188
189 bool fUseDFT;
190
John Stiles7571f9e2020-09-02 22:42:33 -0400191 using INHERITED = GM;
joshualitte46cf962015-08-26 11:19:55 -0700192};
193
194//////////////////////////////////////////////////////////////////////////////
195
halcanary385fe4d2015-08-26 13:07:48 -0700196DEF_GM( return new TextBlobMixedSizes(false); )
halcanary385fe4d2015-08-26 13:07:48 -0700197DEF_GM( return new TextBlobMixedSizes(true); )
John Stilesa6841be2020-08-06 14:11:56 -0400198} // namespace skiagm