blob: 8c51cae45e0221a8b90a704c69bdd50834dc7022 [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"
9
10#include "Resources.h"
11#include "SkBlurMask.h"
12#include "SkBlurMaskFilter.h"
13#include "SkCanvas.h"
14#include "SkGradientShader.h"
15#include "SkImage.h"
16#include "SkRandom.h"
17#include "SkStream.h"
18#include "SkSurface.h"
19#include "SkTextBlob.h"
20#include "SkTypeface.h"
21
22namespace skiagm {
23class TextBlobMixedSizes : public GM {
24public:
25 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
26 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
27
28protected:
29 void onOnceBeforeDraw() override {
joshualitte46cf962015-08-26 11:19:55 -070030 SkTextBlobBuilder builder;
31
32 // make textblob. To stress distance fields, we choose sizes appropriately
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setSubpixelText(true);
36 paint.setLCDRenderText(true);
bungeman13b9c952016-05-12 10:09:30 -070037 paint.setTypeface(MakeResourceAsTypeface("/fonts/HangingS.ttf"));
joshualitte46cf962015-08-26 11:19:55 -070038
joshualitt33e91f12015-09-18 11:28:59 -070039 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070040
41 // extra large
42 paint.setTextSize(262);
43
44 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
45
46 // large
47 SkRect bounds;
48 paint.measureText(text, strlen(text), &bounds);
49 SkScalar yOffset = bounds.height();
50 paint.setTextSize(162);
51
52 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
53
54 // Medium
55 paint.measureText(text, strlen(text), &bounds);
56 yOffset += bounds.height();
57 paint.setTextSize(72);
58
59 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
60
61 // Small
62 paint.measureText(text, strlen(text), &bounds);
63 yOffset += bounds.height();
64 paint.setTextSize(32);
65
66 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
67
68 // micro (will fall out of distance field text even if distance field text is enabled)
69 paint.measureText(text, strlen(text), &bounds);
70 yOffset += bounds.height();
71 paint.setTextSize(14);
72
73 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
74
kkinnunen68c63b32016-03-04 00:12:33 -080075 // Zero size.
76 paint.measureText(text, strlen(text), &bounds);
77 yOffset += bounds.height();
78 paint.setTextSize(0);
79
80 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
81
joshualitte46cf962015-08-26 11:19:55 -070082 // build
fmalita37283c22016-09-13 10:00:23 -070083 fBlob = builder.make();
joshualitte46cf962015-08-26 11:19:55 -070084 }
85
86 SkString onShortName() override {
87 SkString name("textblobmixedsizes");
88 if (fUseDFT) {
89 name.appendf("_df");
90 }
91 return name;
92 }
93
94 SkISize onISize() override {
95 return SkISize::Make(kWidth, kHeight);
96 }
97
98 void onDraw(SkCanvas* inputCanvas) override {
99 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -0700100 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700101 if (fUseDFT) {
102#if SK_SUPPORT_GPU
103 // Create a new Canvas to enable DFT
104 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -0700105 SkISize size = onISize();
106 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(inputCanvas->imageInfo().colorSpace());
107 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
108 kPremul_SkAlphaType, colorSpace);
brianosman3a0dbde2016-07-26 11:36:05 -0700109 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700110 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700111 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700112 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
113 // init our new canvas with the old canvas's matrix
114 canvas->setMatrix(inputCanvas->getTotalMatrix());
115#endif
116 }
117 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
118
119 SkRect bounds = fBlob->bounds();
120
mtkleindbfd7ab2016-09-01 11:24:54 -0700121 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
122 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700123
124 int rowCount = 0;
125 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
126 canvas->save();
127 SkRandom random;
128
129 SkPaint paint;
130 if (!fUseDFT) {
131 paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
132 }
133 paint.setAntiAlias(false);
134
mtkleindbfd7ab2016-09-01 11:24:54 -0700135 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700136
137 // setup blur paint
138 SkPaint blurPaint(paint);
139 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
reedefdfd512016-04-04 10:02:58 -0700140 blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700141
joshualitte46cf962015-08-26 11:19:55 -0700142 for (int i = 0; i < 4; i++) {
143 canvas->save();
144 switch (i % 2) {
145 case 0:
146 canvas->rotate(random.nextF() * 45.f);
147 break;
148 case 1:
149 canvas->rotate(-random.nextF() * 45.f);
150 break;
151 }
152 if (!fUseDFT) {
153 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
154 }
155 canvas->drawTextBlob(fBlob, 0, 0, paint);
156 canvas->restore();
157 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
158 ++rowCount;
159 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
160 canvas->restore();
161 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
162 canvas->save();
163 rowCount = 0;
164 }
165 }
166 canvas->restore();
167
168#if SK_SUPPORT_GPU
169 // render offscreen buffer
170 if (surface) {
171 SkAutoCanvasRestore acr(inputCanvas, true);
172 // since we prepended this matrix already, we blit using identity
173 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700174 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700175 }
176#endif
177 }
178
179private:
fmalita37283c22016-09-13 10:00:23 -0700180 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700181
mtkleindbfd7ab2016-09-01 11:24:54 -0700182 static constexpr int kWidth = 2100;
183 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700184
185 bool fUseDFT;
186
187 typedef GM INHERITED;
188};
189
190//////////////////////////////////////////////////////////////////////////////
191
halcanary385fe4d2015-08-26 13:07:48 -0700192DEF_GM( return new TextBlobMixedSizes(false); )
joshualitte46cf962015-08-26 11:19:55 -0700193#if SK_SUPPORT_GPU
halcanary385fe4d2015-08-26 13:07:48 -0700194DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700195#endif
196}