blob: 973c345fbd3fd50ddd88d94ca1873cc2e0904368 [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
35class GrContext;
joshualitte46cf962015-08-26 11:19:55 -070036
37namespace skiagm {
38class TextBlobMixedSizes : public GM {
39public:
40 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
41 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
42
43protected:
44 void onOnceBeforeDraw() override {
joshualitte46cf962015-08-26 11:19:55 -070045 SkTextBlobBuilder builder;
46
47 // make textblob. To stress distance fields, we choose sizes appropriately
Mike Reed28bd8822018-12-22 22:29:45 -050048 SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262);
49 font.setSubpixel(true);
50 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
joshualitte46cf962015-08-26 11:19:55 -070051
joshualitt33e91f12015-09-18 11:28:59 -070052 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070053
Mike Kleinea3f0142019-03-20 11:12:10 -050054 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
joshualitte46cf962015-08-26 11:19:55 -070055
56 // large
57 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040058 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070059 SkScalar yOffset = bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050060 font.setSize(162);
joshualitte46cf962015-08-26 11:19:55 -070061
Mike Kleinea3f0142019-03-20 11:12:10 -050062 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070063
64 // Medium
Ben Wagner51e15a62019-05-07 15:38:46 -040065 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070066 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050067 font.setSize(72);
joshualitte46cf962015-08-26 11:19:55 -070068
Mike Kleinea3f0142019-03-20 11:12:10 -050069 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070070
71 // Small
Ben Wagner51e15a62019-05-07 15:38:46 -040072 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070073 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050074 font.setSize(32);
joshualitte46cf962015-08-26 11:19:55 -070075
Mike Kleinea3f0142019-03-20 11:12:10 -050076 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070077
78 // micro (will fall out of distance field text even if distance field text is enabled)
Ben Wagner51e15a62019-05-07 15:38:46 -040079 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070080 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050081 font.setSize(14);
joshualitte46cf962015-08-26 11:19:55 -070082
Mike Kleinea3f0142019-03-20 11:12:10 -050083 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070084
kkinnunen68c63b32016-03-04 00:12:33 -080085 // Zero size.
Ben Wagner51e15a62019-05-07 15:38:46 -040086 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
kkinnunen68c63b32016-03-04 00:12:33 -080087 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050088 font.setSize(0);
kkinnunen68c63b32016-03-04 00:12:33 -080089
Mike Kleinea3f0142019-03-20 11:12:10 -050090 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
kkinnunen68c63b32016-03-04 00:12:33 -080091
joshualitte46cf962015-08-26 11:19:55 -070092 // build
fmalita37283c22016-09-13 10:00:23 -070093 fBlob = builder.make();
joshualitte46cf962015-08-26 11:19:55 -070094 }
95
96 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060097 return SkStringPrintf("textblobmixedsizes%s",
Hal Canary58822d62017-11-14 12:08:34 -050098 fUseDFT ? "_df" : "");
joshualitte46cf962015-08-26 11:19:55 -070099 }
100
101 SkISize onISize() override {
102 return SkISize::Make(kWidth, kHeight);
103 }
104
105 void onDraw(SkCanvas* inputCanvas) override {
106 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -0700107 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700108 if (fUseDFT) {
joshualitte46cf962015-08-26 11:19:55 -0700109 // Create a new Canvas to enable DFT
110 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -0700111 SkISize size = onISize();
Mike Reed693fdbd2017-01-12 10:13:40 -0500112 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
brianosman52ede1d2016-06-20 08:25:02 -0700113 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
114 kPremul_SkAlphaType, colorSpace);
brianosman3a0dbde2016-07-26 11:36:05 -0700115 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700116 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700117 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700118 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
119 // init our new canvas with the old canvas's matrix
120 canvas->setMatrix(inputCanvas->getTotalMatrix());
joshualitte46cf962015-08-26 11:19:55 -0700121 }
Mike Kleind46dce32018-08-16 10:17:03 -0400122 canvas->drawColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700123
124 SkRect bounds = fBlob->bounds();
125
mtkleindbfd7ab2016-09-01 11:24:54 -0700126 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
127 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700128
129 int rowCount = 0;
130 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
131 canvas->save();
132 SkRandom random;
133
134 SkPaint paint;
135 if (!fUseDFT) {
Mike Kleind46dce32018-08-16 10:17:03 -0400136 paint.setColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700137 }
138 paint.setAntiAlias(false);
139
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700141
142 // setup blur paint
143 SkPaint blurPaint(paint);
Mike Kleind46dce32018-08-16 10:17:03 -0400144 blurPaint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400145 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700146
joshualitte46cf962015-08-26 11:19:55 -0700147 for (int i = 0; i < 4; i++) {
148 canvas->save();
149 switch (i % 2) {
150 case 0:
151 canvas->rotate(random.nextF() * 45.f);
152 break;
153 case 1:
154 canvas->rotate(-random.nextF() * 45.f);
155 break;
156 }
157 if (!fUseDFT) {
158 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
159 }
160 canvas->drawTextBlob(fBlob, 0, 0, paint);
161 canvas->restore();
162 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
163 ++rowCount;
164 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
165 canvas->restore();
166 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
167 canvas->save();
168 rowCount = 0;
169 }
170 }
171 canvas->restore();
172
joshualitte46cf962015-08-26 11:19:55 -0700173 // render offscreen buffer
174 if (surface) {
175 SkAutoCanvasRestore acr(inputCanvas, true);
176 // since we prepended this matrix already, we blit using identity
177 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700178 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700179 }
joshualitte46cf962015-08-26 11:19:55 -0700180 }
181
182private:
fmalita37283c22016-09-13 10:00:23 -0700183 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700184
mtkleindbfd7ab2016-09-01 11:24:54 -0700185 static constexpr int kWidth = 2100;
186 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700187
188 bool fUseDFT;
189
190 typedef GM INHERITED;
191};
192
193//////////////////////////////////////////////////////////////////////////////
194
halcanary385fe4d2015-08-26 13:07:48 -0700195DEF_GM( return new TextBlobMixedSizes(false); )
halcanary385fe4d2015-08-26 13:07:48 -0700196DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700197}