blob: 7ada13a3c50a15d8b08090b82a9365b57d23eb23 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualitte46cf962015-08-26 11:19:55 -070010
11#include "Resources.h"
12#include "SkBlurMask.h"
joshualitte46cf962015-08-26 11:19:55 -070013#include "SkCanvas.h"
14#include "SkGradientShader.h"
15#include "SkImage.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040016#include "SkMaskFilter.h"
joshualitte46cf962015-08-26 11:19:55 -070017#include "SkRandom.h"
18#include "SkStream.h"
19#include "SkSurface.h"
20#include "SkTextBlob.h"
21#include "SkTypeface.h"
22
23namespace skiagm {
24class TextBlobMixedSizes : public GM {
25public:
26 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
27 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
28
29protected:
30 void onOnceBeforeDraw() override {
joshualitte46cf962015-08-26 11:19:55 -070031 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 Canary53e5e7d2017-12-08 14:25:14 -050038 paint.setTypeface(MakeResourceAsTypeface("fonts/HangingS.ttf"));
joshualitte46cf962015-08-26 11:19:55 -070039
joshualitt33e91f12015-09-18 11:28:59 -070040 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070041
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
kkinnunen68c63b32016-03-04 00:12:33 -080076 // 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
joshualitte46cf962015-08-26 11:19:55 -070083 // build
fmalita37283c22016-09-13 10:00:23 -070084 fBlob = builder.make();
joshualitte46cf962015-08-26 11:19:55 -070085 }
86
87 SkString onShortName() override {
Hal Canary58822d62017-11-14 12:08:34 -050088 return SkStringPrintf("textblobmixedsizes%s%s",
89 sk_tool_utils::platform_font_manager(),
90 fUseDFT ? "_df" : "");
joshualitte46cf962015-08-26 11:19:55 -070091 }
92
93 SkISize onISize() override {
94 return SkISize::Make(kWidth, kHeight);
95 }
96
97 void onDraw(SkCanvas* inputCanvas) override {
98 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -070099 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700100 if (fUseDFT) {
101#if SK_SUPPORT_GPU
102 // Create a new Canvas to enable DFT
103 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -0700104 SkISize size = onISize();
Mike Reed693fdbd2017-01-12 10:13:40 -0500105 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
brianosman52ede1d2016-06-20 08:25:02 -0700106 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
107 kPremul_SkAlphaType, colorSpace);
brianosman3a0dbde2016-07-26 11:36:05 -0700108 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700109 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700110 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700111 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
112 // init our new canvas with the old canvas's matrix
113 canvas->setMatrix(inputCanvas->getTotalMatrix());
114#endif
115 }
116 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
117
118 SkRect bounds = fBlob->bounds();
119
mtkleindbfd7ab2016-09-01 11:24:54 -0700120 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
121 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700122
123 int rowCount = 0;
124 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
125 canvas->save();
126 SkRandom random;
127
128 SkPaint paint;
129 if (!fUseDFT) {
130 paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
131 }
132 paint.setAntiAlias(false);
133
mtkleindbfd7ab2016-09-01 11:24:54 -0700134 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700135
136 // setup blur paint
137 SkPaint blurPaint(paint);
138 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
Mike Reed1be1f8d2018-03-14 13:01:17 -0400139 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700140
joshualitte46cf962015-08-26 11:19:55 -0700141 for (int i = 0; i < 4; i++) {
142 canvas->save();
143 switch (i % 2) {
144 case 0:
145 canvas->rotate(random.nextF() * 45.f);
146 break;
147 case 1:
148 canvas->rotate(-random.nextF() * 45.f);
149 break;
150 }
151 if (!fUseDFT) {
152 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
153 }
154 canvas->drawTextBlob(fBlob, 0, 0, paint);
155 canvas->restore();
156 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
157 ++rowCount;
158 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
159 canvas->restore();
160 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
161 canvas->save();
162 rowCount = 0;
163 }
164 }
165 canvas->restore();
166
167#if SK_SUPPORT_GPU
168 // render offscreen buffer
169 if (surface) {
170 SkAutoCanvasRestore acr(inputCanvas, true);
171 // since we prepended this matrix already, we blit using identity
172 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700173 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700174 }
175#endif
176 }
177
178private:
fmalita37283c22016-09-13 10:00:23 -0700179 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700180
mtkleindbfd7ab2016-09-01 11:24:54 -0700181 static constexpr int kWidth = 2100;
182 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700183
184 bool fUseDFT;
185
186 typedef GM INHERITED;
187};
188
189//////////////////////////////////////////////////////////////////////////////
190
halcanary385fe4d2015-08-26 13:07:48 -0700191DEF_GM( return new TextBlobMixedSizes(false); )
joshualitte46cf962015-08-26 11:19:55 -0700192#if SK_SUPPORT_GPU
halcanary385fe4d2015-08-26 13:07:48 -0700193DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700194#endif
195}